APOPServer.C 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. *+SNOTICE
  25. *
  26. * $TOG: APOPServer.C /main/5 1998/11/10 17:06:52 mgreess $
  27. *
  28. * RESTRICTED CONFIDENTIAL INFORMATION:
  29. *
  30. * The information in this document is subject to special
  31. * restrictions in a confidential disclosure agreement between
  32. * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
  33. * document outside HP, IBM, Sun, USL, SCO, or Univel without
  34. * Sun's specific written approval. This document and all copies
  35. * and derivative works thereof must be returned or destroyed at
  36. * Sun's request.
  37. *
  38. * Copyright 1993, 1995, 1995 Sun Microsystems, Inc. All rights reserved.
  39. *
  40. *+ENOTICE
  41. */
  42. /*
  43. * Common Desktop Environment
  44. *
  45. * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
  46. * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
  47. * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
  48. * (c) Copyright 1993, 1994, 1995 Novell, Inc.
  49. * (c) Copyright 1995 Digital Equipment Corp.
  50. * (c) Copyright 1995 Fujitsu Limited
  51. * (c) Copyright 1995 Hitachi, Ltd.
  52. *
  53. *
  54. * RESTRICTED RIGHTS LEGEND
  55. *
  56. *Use, duplication, or disclosure by the U.S. Government is subject to
  57. *restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  58. *Technical Data and Computer Software clause in DFARS 252.227-7013. Rights
  59. *for non-DOD U.S. Government Departments and Agencies are as set forth in
  60. *FAR 52.227-19(c)(1,2).
  61. *Hewlett-Packard Company, 3000 Hanover Street, Palo Alto, CA 94304 U.S.A.
  62. *International Business Machines Corp., Route 100, Somers, NY 10589 U.S.A.
  63. *Sun Microsystems, Inc., 2550 Garcia Avenue, Mountain View, CA 94043 U.S.A.
  64. *Novell, Inc., 190 River Road, Summit, NJ 07901 U.S.A.
  65. *Digital Equipment Corp., 111 Powdermill Road, Maynard, MA 01754, U.S.A.
  66. *Fujitsu Limited, 1015, Kamikodanaka Nakahara-Ku, Kawasaki 211, Japan
  67. *Hitachi, Ltd., 6, Kanda Surugadai 4-Chome, Chiyoda-ku, Tokyo 101, Japan
  68. */
  69. #include <stdio.h>
  70. #include <string.h>
  71. #include <ctype.h>
  72. #include <unistd.h>
  73. #include <stdlib.h>
  74. #include <DtMail/DtMailServer.hh>
  75. #include "md5.h"
  76. APOPServer::APOPServer(
  77. char *folder,
  78. DtMail::Session *session,
  79. DtMail::MailBox *mailbox,
  80. DtMailAppendCallback append_mailbox_cb,
  81. void *append_mailbox_cb_data)
  82. : POP3Server(folder, session, mailbox,
  83. append_mailbox_cb, append_mailbox_cb_data)
  84. {
  85. }
  86. APOPServer::~APOPServer()
  87. {
  88. }
  89. //
  90. // Apply for connection authorization.
  91. //
  92. DTMailError_t
  93. APOPServer::ptrans_authorize(char *greeting)
  94. {
  95. static const char
  96. *pname = "APOPServer::ptrans_authorize";
  97. static char ascii_digest [33];
  98. char *start,*end;
  99. char *msg;
  100. DTMailError_t ok;
  101. // Build MD5 digest from greeting timestamp + password.
  102. // Find start of timestamp.
  103. for (start = greeting; *start != 0 && *start != '<'; start++)
  104. continue;
  105. if (*start == 0)
  106. {
  107. _logger.logError(
  108. DTM_FALSE,
  109. "%s: APOP timestamp not found in greeting",
  110. pname);
  111. return DTME_MailServerAccess_AuthorizationFailed;
  112. }
  113. // Find end of timestamp.
  114. for (end = start; *end != 0 && *end != '>'; end++)
  115. continue;
  116. if (*end == 0 || end == start + 1)
  117. {
  118. _logger.logError(
  119. DTM_FALSE,
  120. "%s: APOP timestamp not found in greeting",
  121. pname);
  122. return DTME_MailServerAccess_AuthorizationFailed;
  123. }
  124. else
  125. *++end = '\0';
  126. {
  127. int i;
  128. MD5_CTX context;
  129. unsigned char digest[16];
  130. // Copy timestamp and password into digestion buffer.
  131. msg = (char*) malloc((end-start+1) + strlen(_password) + 1);
  132. strcpy(msg, start);
  133. strcat(msg, _password);
  134. MD5Init(&context);
  135. MD5Update(&context, (unsigned char*) msg, strlen(msg));
  136. for (i = 0; i < 16; i++)
  137. sprintf(ascii_digest+2*i, "%02x", digest[i]);
  138. free(msg);
  139. }
  140. ok = do_transaction("APOP %s %s", _username, ascii_digest);
  141. if (DTME_NoError != ok) return DTME_MailServerAccess_AuthorizationFailed;
  142. return DTME_NoError;
  143. }