2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #define MSGBUFSIZE 8192
32 static gint verbose = 1;
33 static gchar esmtp_response[MSGBUFSIZE];
35 gint esmtp_ehlo(SockInfo *sock, const gchar *hostname)
37 smtp_auth_methods[0] = smtp_auth_methods[1] = smtp_auth_methods[2] = smtp_auth_methods[3] = FALSE;
38 sock_printf(sock, "EHLO %s\r\n", hostname);
40 log_print("ESMTP> EHLO %s\n", hostname);
42 return esmtp_ok(sock);
45 gint esmtp_starttls(SockInfo *sock)
47 sock_printf(sock, "STARTTLS\r\n");
49 log_print("ESMTP> STARTTLS\n");
51 return esmtp_ok(sock);
54 gint esmtp_auth_cram_md5(SockInfo *sock)
56 sock_printf(sock, "AUTH CRAM-MD5\r\n");
58 log_print("ESMTP> AUTH CRAM-MD5\n");
60 return esmtp_ok(sock);
63 gint esmtp_auth_login(SockInfo *sock)
65 sock_printf(sock, "AUTH LOGIN\r\n");
67 log_print("ESMTP> AUTH LOGIN\n");
69 return esmtp_ok(sock);
72 gint esmtp_auth(SockInfo *sock, SMTPAuthType authtype,
73 const gchar *userid, const gchar *passwd)
75 gchar buf[MSGBUFSIZE];
77 gchar *challenge, *response, *response64;
82 if (!strncmp(esmtp_response, "334 ", 4))
83 to64frombits(buf, userid, strlen(userid));
85 /* Server rejects AUTH */
86 g_snprintf(buf, sizeof(buf), "*");
88 sock_printf(sock, "%s\r\n", buf);
89 if (verbose) log_print("ESMTP> USERID\n");
91 esmtp_ok(sock); /* to read the answer from the server */
93 if (!strncmp(esmtp_response, "334 ", 4))
94 to64frombits(buf, passwd, strlen(passwd));
96 /* Server rejects AUTH */
97 g_snprintf(buf, sizeof(buf), "*");
99 sock_printf(sock, "%s\r\n", buf);
100 if (verbose) log_print("ESMTP> PASSWORD\n");
102 case SMTPAUTH_CRAM_MD5:
103 if (!strncmp(esmtp_response, "334 ", 4)) {
104 /* remove 334 from esmtp_response */
105 gchar *p = esmtp_response + 4;
107 challenge = g_malloc(strlen(p) + 1);
108 challengelen = from64tobits(challenge, p);
109 challenge[challengelen] = '\0';
111 log_print("ESMTP< [Decoded: %s]\n", challenge);
113 g_snprintf(buf, sizeof(buf), "%s", passwd);
114 md5_hex_hmac(hexdigest, challenge, challengelen,
115 buf, strlen(passwd));
118 response = g_strdup_printf("%s %s", userid, hexdigest);
120 log_print("ESMTP> [Encoded: %s]\n", response);
122 response64 = g_malloc((strlen(response) + 3) * 2 + 1);
123 to64frombits(response64, response, strlen(response));
126 sock_printf(sock, "%s\r\n", response64);
127 if (verbose) log_print("ESMTP> %s\n", response64);
130 /* Server rejects AUTH */
131 g_snprintf(buf, sizeof(buf), "*");
132 sock_printf(sock, "%s\r\n", buf);
134 log_print("ESMTP> %s\n",buf);
137 case SMTPAUTH_DIGEST_MD5:
139 /* stop esmtp_auth when no correct authtype */
140 g_snprintf(buf, sizeof(buf), "*");
141 sock_printf(sock, "%s\r\n", buf);
142 if (verbose) log_print("ESMTP> %s\n", buf);
146 return esmtp_ok(sock);
149 gint esmtp_ok(SockInfo *sock)
151 while (sock_gets(sock, esmtp_response, sizeof(esmtp_response) - 1)
153 if (strlen(esmtp_response) < 4)
155 strretchomp(esmtp_response);
158 log_print("ESMTP< %s\n", esmtp_response);
160 if (strncasecmp("250-AUTH", esmtp_response, 8) == 0) {
161 smtp_auth_methods[SMTPAUTH_LOGIN] = (strstr(esmtp_response, " LOGIN") != NULL);
162 smtp_auth_methods[SMTPAUTH_CRAM_MD5] = (strstr(esmtp_response, " CRAM-MD5") != NULL);
163 smtp_auth_methods[SMTPAUTH_DIGEST_MD5] = FALSE; /* not implemented yet */
166 if ((esmtp_response[0] == '1' || esmtp_response[0] == '2' ||
167 esmtp_response[0] == '3') && esmtp_response[3] == ' ')
169 else if (esmtp_response[3] != '-')
171 else if (esmtp_response[0] == '5' &&
172 esmtp_response[1] == '0' &&
173 (esmtp_response[2] == '4' ||
174 esmtp_response[2] == '3' ||
175 esmtp_response[2] == '1'))
179 return SM_UNRECOVERABLE;