Work around config.rpath problem.
[gsasl.git] / src / smtp.c
blob40ba3a924539c339ec10a0f1dd15523a15dd4fa8
1 /* smtp.c --- Implement SMTP profile of SASL login.
2 * Copyright (C) 2002, 2003, 2004, 2005 Simon Josefsson
4 * This file is part of GNU SASL.
6 * GNU SASL 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 2 of the License, or
9 * (at your option) any later version.
11 * GNU SASL 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 GNU SASL; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "smtp.h"
24 #define MAX_LINE_LENGTH BUFSIZ
26 int
27 smtp_greeting (void)
29 char *in;
31 if (!readln (&in))
32 return 0;
34 return 1;
37 int
38 smtp_has_starttls (void)
40 char *in, *capa;
41 int has_tls = 0;
43 if (!writeln ("EHLO [127.0.0.1]"))
44 return 0;
48 if (!readln (&in))
49 return 0;
51 #define TLSGREETING "250-STARTTLS"
52 if (strncmp (in, TLSGREETING, strlen (TLSGREETING)) == 0)
53 has_tls = 1;
55 while (strncmp (in, "250 ", 4) != 0);
57 return has_tls;
60 int
61 smtp_starttls (void)
63 char *in;
65 if (!writeln ("STARTTLS"))
66 return 0;
68 if (!readln (&in))
69 return 0;
71 return 1;
74 int
75 smtp_select_mechanism (char **mechlist)
77 char *in;
79 if (args_info.server_flag)
81 if (!args_info.quiet_given)
82 fprintf (stderr, _("Chose SASL mechanisms:\n"));
83 if (!readln (&in))
84 return 0;
85 *mechlist = in;
87 else
89 if (!writeln ("EHLO [127.0.0.1]"))
90 return 0;
94 if (!readln (&in))
95 return 0;
97 #define GREETING "250-AUTH "
98 if (strncmp (in, GREETING, strlen (GREETING)) == 0)
99 *mechlist = in + strlen (GREETING);
101 while (strncmp (in, "250 ", 4) != 0);
104 return 1;
108 smtp_authenticate (const char *mech)
110 if (args_info.server_flag)
112 if (!args_info.quiet_given)
113 fprintf (stderr, _("Using mechanism:\n"));
114 puts (mech);
116 else
118 char input[MAX_LINE_LENGTH];
120 sprintf (input, "AUTH %s", mech);
121 if (!writeln (input))
122 return 0;
125 return 1;
129 smtp_step_send (const char *data)
131 char input[MAX_LINE_LENGTH];
133 if (args_info.server_flag)
134 sprintf (input, "334 %s", data);
135 else
136 sprintf (input, "%s", data);
137 if (!writeln (input))
138 return 0;
140 return 1;
144 smtp_step_recv (char **data)
146 char *p;
148 if (!readln (data))
149 return 0;
151 p = *data;
153 if (p[0] != '3' || p[1] != '3' || p[2] != '4' || p[3] != ' ')
155 fprintf (stderr, _("error: Server did not return expected SASL "
156 "data (it must begin with '334 '):\n%s\n"), p);
157 return 0;
160 memmove (&p[0], &p[4], strlen (p) - 3);
162 if (p[strlen (p) - 1] == '\n')
163 p[strlen (p) - 1] = '\0';
164 if (p[strlen (p) - 1] == '\r')
165 p[strlen (p) - 1] = '\0';
167 return 1;
171 smtp_auth_finish (void)
173 char *in;
175 if (!readln (&in))
176 return 0;
178 return 1;
182 smtp_logout (void)
184 char *in;
186 if (!writeln ("QUIT"))
187 return 0;
189 /* read "221 2.0.0 foo closing ..." */
190 if (!readln (&in))
191 return 0;
193 free (in);
195 return 1;