Bump versions.
[gsasl.git] / src / smtp.c
blobcbd654001b719ed57656ad05fe8f678f7dcdad28
1 /* smtp.c --- Implement SMTP profile of SASL login.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2007 Simon Josefsson
4 * This file is part of GNU SASL.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "smtp.h"
23 #define MAX_LINE_LENGTH BUFSIZ
25 int
26 smtp_greeting (void)
28 char *in;
30 if (!readln (&in))
31 return 0;
33 return 1;
36 int
37 smtp_has_starttls (void)
39 char *in, *capa;
40 int has_tls = 0;
42 if (!writeln ("EHLO [127.0.0.1]"))
43 return 0;
47 if (!readln (&in))
48 return 0;
50 #define TLSGREETING "250-STARTTLS"
51 if (strncmp (in, TLSGREETING, strlen (TLSGREETING)) == 0)
52 has_tls = 1;
54 while (strncmp (in, "250 ", 4) != 0);
56 return has_tls;
59 int
60 smtp_starttls (void)
62 char *in;
64 if (!writeln ("STARTTLS"))
65 return 0;
67 if (!readln (&in))
68 return 0;
70 return 1;
73 int
74 smtp_select_mechanism (char **mechlist)
76 char *in;
78 if (args_info.server_flag)
80 if (!args_info.quiet_given)
81 fprintf (stderr, _("Chose SASL mechanisms:\n"));
82 if (!readln (&in))
83 return 0;
84 *mechlist = in;
86 else
88 if (!writeln ("EHLO [127.0.0.1]"))
89 return 0;
93 if (!readln (&in))
94 return 0;
96 #define GREETING "250-AUTH "
97 if (strncmp (in, GREETING, strlen (GREETING)) == 0)
98 *mechlist = in + strlen (GREETING);
100 while (strncmp (in, "250 ", 4) != 0);
103 return 1;
107 smtp_authenticate (const char *mech)
109 if (args_info.server_flag)
111 if (!args_info.quiet_given)
112 fprintf (stderr, _("Using mechanism:\n"));
113 puts (mech);
115 else
117 char input[MAX_LINE_LENGTH];
119 sprintf (input, "AUTH %s", mech);
120 if (!writeln (input))
121 return 0;
124 return 1;
128 smtp_step_send (const char *data)
130 char input[MAX_LINE_LENGTH];
132 if (args_info.server_flag)
133 sprintf (input, "334 %s", data);
134 else
135 sprintf (input, "%s", data);
136 if (!writeln (input))
137 return 0;
139 return 1;
143 smtp_step_recv (char **data)
145 char *p;
147 if (!readln (data))
148 return 0;
150 p = *data;
152 if (p[0] != '3' || p[1] != '3' || p[2] != '4' || p[3] != ' ')
154 fprintf (stderr, _("error: Server did not return expected SASL "
155 "data (it must begin with '334 '):\n%s\n"), p);
156 return 0;
159 memmove (&p[0], &p[4], strlen (p) - 3);
161 if (p[strlen (p) - 1] == '\n')
162 p[strlen (p) - 1] = '\0';
163 if (p[strlen (p) - 1] == '\r')
164 p[strlen (p) - 1] = '\0';
166 return 1;
170 smtp_auth_finish (void)
172 char *in;
174 if (!readln (&in))
175 return 0;
177 return 1;
181 smtp_logout (void)
183 char *in;
185 if (!writeln ("QUIT"))
186 return 0;
188 /* read "221 2.0.0 foo closing ..." */
189 if (!readln (&in))
190 return 0;
192 free (in);
194 return 1;