Commit d01da801 d01da801a3543c80e172992ffb7d26a8d128556b by Sergey Poznyakoff

Fix TLS support in smtp.

* include/mailutils/tls.h (mu_tls_readline_fn)
(mu_tls_writeline_fn, mu_tls_stream_ctl_fn): Remove typedefs.
(mu_tls_begin): Remove prototype.
* libmu_auth/tls.c (mu_tls_begin): Remove function.
* libproto/mailer/smtp.c: Revamp STARTTLS support.
1 parent 46e3b517
...@@ -47,19 +47,6 @@ extern int mu_check_tls_environment (void); ...@@ -47,19 +47,6 @@ extern int mu_check_tls_environment (void);
47 extern int mu_init_tls_libs (void); 47 extern int mu_init_tls_libs (void);
48 extern void mu_deinit_tls_libs (void); 48 extern void mu_deinit_tls_libs (void);
49 49
50 typedef int (*mu_tls_readline_fn) (void *iodata, int n);
51 typedef int (*mu_tls_writeline_fn) (void *iodata, char *buf);
52
53 #define MU_TLS_SESS_GET_STREAMS 0
54 #define MU_TLS_SESS_SET_STREAMS 1
55 typedef int (*mu_tls_stream_ctl_fn) (void *iodata, int __op,
56 mu_stream_t *pstr);
57
58 extern int mu_tls_begin (void *iodata, mu_tls_readline_fn reader,
59 mu_tls_writeline_fn writer,
60 mu_tls_stream_ctl_fn stream_ctl,
61 char *keywords[]);
62
63 extern int mu_tls_enable; 50 extern int mu_tls_enable;
64 51
65 #ifdef __cplusplus 52 #ifdef __cplusplus
......
...@@ -164,94 +164,6 @@ initialize_tls_session (void) ...@@ -164,94 +164,6 @@ initialize_tls_session (void)
164 return session; 164 return session;
165 } 165 }
166 166
167 int
168 mu_tls_begin (void *iodata,
169 mu_tls_readline_fn reader,
170 mu_tls_writeline_fn writer,
171 mu_tls_stream_ctl_fn stream_ctl,
172 char *keywords[])
173 {
174 int i = 0;
175 int status;
176 mu_stream_t streams[2], newstr;
177
178 if (keywords == NULL)
179 return EINVAL;
180
181 for (i = 0; keywords[i]; i++)
182 {
183 switch (i)
184 {
185 case 0:
186 /*
187 * Send STLS/STARTTLS
188 */
189 status = writer (iodata, keywords[i]);
190 if (status != 0)
191 {
192 mu_error ("mu_tls_begin: writer (0): %s", mu_strerror (status));
193 return status;
194 }
195
196 status = reader (iodata, i);
197 if (status != 0)
198 {
199 mu_error ("mu_tls_begin: reader (0): %s", mu_strerror (status));
200 return status;
201 }
202
203 status = stream_ctl (iodata, MU_TLS_SESS_GET_STREAMS, streams);
204 if (status)
205 return status;
206 status = mu_tls_client_stream_create (&newstr,
207 streams[0], streams[1], 0);
208 if (status != 0)
209 {
210 mu_error ("mu_tls_begin: mu_tls_client_stream_create(0): %s",
211 mu_strerror (status));
212 stream_ctl (iodata, MU_TLS_SESS_SET_STREAMS, streams);
213 return status;
214 }
215
216 status = mu_stream_open (newstr);
217 if (status != 0)
218 {
219 mu_error ("mu_tls_begin: mu_stream_open (0): %s",
220 mu_strerror (status));
221 stream_ctl (iodata, MU_TLS_SESS_SET_STREAMS, streams);
222 return status;
223 }
224
225 streams[0] = streams[1] = newstr;
226 stream_ctl (iodata, MU_TLS_SESS_SET_STREAMS, streams);
227 /* FIXME: Unref newstr */
228 break;
229
230 case 1:
231 /*
232 * Send CAPABILITIES request
233 */
234 status = writer (iodata, keywords[i]);
235 if (status != 0)
236 {
237 mu_error ("mu_tls_begin: writer (1): %s", mu_strerror (status));
238 return status;
239 }
240
241 status = reader (iodata, i);
242 if (status != 0)
243 {
244 mu_error ("mu_tls_begin: reader (1): %s", mu_strerror (status));
245 return status;
246 }
247 break;
248
249 default:
250 return 1;
251 }
252 }
253 return 0;
254 }
255 167
256 /* ************************* TLS Stream Support **************************** */ 168 /* ************************* TLS Stream Support **************************** */
257 169
......
...@@ -554,68 +554,40 @@ smtp_close (mu_mailer_t mailer) ...@@ -554,68 +554,40 @@ smtp_close (mu_mailer_t mailer)
554 return mu_stream_close (mailer->stream); 554 return mu_stream_close (mailer->stream);
555 } 555 }
556 556
557 #ifdef WITH_TLS
558 /* 557 /*
559 Client side STARTTLS support. 558 Client side STARTTLS support.
560 */ 559 */
561 560
562 static int 561 static int
563 smtp_reader (void *iodata)
564 {
565 int status = 0;
566 smtp_t iop = iodata;
567
568 status = smtp_read_ack (iop);
569 CHECK_EAGAIN (iop, status);
570 return status;
571 }
572
573 static int
574 smtp_writer (void *iodata, char *buf)
575 {
576 smtp_t iop = iodata;
577 int status;
578
579 if (mu_c_strncasecmp (buf, "EHLO", 4) == 0)
580 status = smtp_writeline (iop, "%s %s\r\n", buf, iop->localhost);
581 else
582 status = smtp_writeline (iop, "%s\r\n", buf);
583 CHECK_ERROR (iop, status);
584 status = smtp_write (iop);
585 CHECK_EAGAIN (iop, status);
586 return status;
587 }
588
589 static void
590 smtp_stream_ctl (void *iodata, mu_stream_t * pold, mu_stream_t new)
591 {
592 smtp_t iop = iodata;
593
594 if (pold)
595 *pold = iop->mailer->stream;
596 if (new)
597 iop->mailer->stream = new;
598 }
599 #endif
600
601 static int
602 smtp_starttls (smtp_t smtp) 562 smtp_starttls (smtp_t smtp)
603 { 563 {
604 #ifdef WITH_TLS 564 #ifdef WITH_TLS
605 int status; 565 int status;
606 mu_mailer_t mailer = smtp->mailer; 566 mu_mailer_t mailer = smtp->mailer;
607 char *keywords[] = { "STARTTLS", NULL }; 567 mu_stream_t newstr;
608 568
609 if (!mu_tls_enable || !(smtp->capa & CAPA_STARTTLS)) 569 if (!mu_tls_enable || !(smtp->capa & CAPA_STARTTLS))
610 return -1; 570 return -1;
611 571
612 smtp->capa = 0; 572 smtp->capa = 0;
613 smtp->auth_mechs = 0; 573 smtp->auth_mechs = 0;
614 status = mu_tls_begin (smtp, smtp_reader, smtp_writer,
615 smtp_stream_ctl, keywords);
616 574
575 status = smtp_writeline (smtp, "STARTTLS\r\n");
576 CHECK_ERROR (smtp, status);
577 status = smtp_write (smtp);
578 CHECK_EAGAIN (smtp, status);
579 status = smtp_read_ack (smtp);
580 CHECK_ERROR (smtp, status);
581 mu_stream_flush (mailer->stream);
582 status = mu_tls_client_stream_create (&newstr, mailer->stream,
583 mailer->stream, 0);
584 CHECK_ERROR (smtp, status);
585 status = mu_stream_open (newstr);
617 MU_DEBUG1 (mailer->debug, MU_DEBUG_PROT, "TLS negotiation %s\n", 586 MU_DEBUG1 (mailer->debug, MU_DEBUG_PROT, "TLS negotiation %s\n",
618 status == 0 ? "succeeded" : "failed"); 587 status == 0 ? "succeeded" : "failed");
588 CHECK_ERROR (smtp, status);
589
590 mailer->stream = newstr;
619 591
620 return status; 592 return status;
621 #else 593 #else
...@@ -1399,6 +1371,8 @@ smtp_parse_ehlo_ack (smtp_t smtp) ...@@ -1399,6 +1371,8 @@ smtp_parse_ehlo_ack (smtp_t smtp)
1399 int status; 1371 int status;
1400 int multi; 1372 int multi;
1401 1373
1374 smtp->ptr = smtp->buffer;
1375
1402 do 1376 do
1403 { 1377 {
1404 multi = 0; 1378 multi = 0;
......