2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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.
31 #define MSGBUFSIZE 8192
33 static gint verbose = 1;
34 static gchar esmtp_response[MSGBUFSIZE];
36 gint esmtp_ehlo(SockInfo *sock, const gchar *hostname)
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;
83 if (!strncmp(esmtp_response, "334 ", 4))
84 to64frombits(buf, userid, strlen(userid));
86 /* Server rejects AUTH */
87 g_snprintf(buf, sizeof(buf), "*");
89 sock_printf(sock, "%s\r\n", buf);
90 if (verbose) log_print("ESMTP> USERID\n");
92 esmtp_ok(sock); /* to read the answer from the server */
94 if (!strncmp(esmtp_response, "334 ", 4))
95 to64frombits(buf, passwd, strlen(passwd));
97 /* Server rejects AUTH */
98 g_snprintf(buf, sizeof(buf), "*");
100 sock_printf(sock, "%s\r\n", buf);
101 if (verbose) log_print("ESMTP> PASSWORD\n");
103 case SMTPAUTH_CRAM_MD5:
104 if (!strncmp(esmtp_response, "334 ", 4)) {
105 /* remove 334 from esmtp_response */
106 g_snprintf(buf, sizeof(buf), "%s",esmtp_response);
107 token = strtok(buf, " ");
108 token = strtok(NULL, " ");
109 challenge = g_malloc(strlen(token)+1);
110 challengelen = from64tobits(challenge,token);
112 log_print("ESMTP< Decoded: %s\n", challenge);
114 g_snprintf(buf, sizeof(buf), "%s", passwd);
115 md5_hex_hmac(hexdigest, challenge, challengelen,
116 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 ((esmtp_response[0] == '1' || esmtp_response[0] == '2' ||
161 esmtp_response[0] == '3') && esmtp_response[3] == ' ')
163 else if (esmtp_response[3] != '-')
165 else if (esmtp_response[0] == '5' &&
166 esmtp_response[1] == '0' &&
167 (esmtp_response[2] == '4' ||
168 esmtp_response[2] == '3' ||
169 esmtp_response[2] == '1'))
173 return SM_UNRECOVERABLE;