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/>.
23 #define MAX_LINE_LENGTH BUFSIZ
37 smtp_has_starttls (void)
42 if (!writeln ("EHLO [127.0.0.1]"))
50 #define TLSGREETING "250-STARTTLS"
51 if (strncmp (in
, TLSGREETING
, strlen (TLSGREETING
)) == 0)
54 while (strncmp (in
, "250 ", 4) != 0);
64 if (!writeln ("STARTTLS"))
74 smtp_select_mechanism (char **mechlist
)
78 if (args_info
.server_flag
)
80 if (!args_info
.quiet_given
)
81 fprintf (stderr
, _("Chose SASL mechanisms:\n"));
88 if (!writeln ("EHLO [127.0.0.1]"))
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);
107 smtp_authenticate (const char *mech
)
109 if (args_info
.server_flag
)
111 if (!args_info
.quiet_given
)
112 fprintf (stderr
, _("Using mechanism:\n"));
117 char input
[MAX_LINE_LENGTH
];
119 sprintf (input
, "AUTH %s", mech
);
120 if (!writeln (input
))
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
);
135 sprintf (input
, "%s", data
);
136 if (!writeln (input
))
143 smtp_step_recv (char **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
);
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';
170 smtp_auth_finish (void)
185 if (!writeln ("QUIT"))
188 /* read "221 2.0.0 foo closing ..." */