]> diplodocus.org Git - nmh/blob - mts/smtp/smtp.c
Replace strchr() condition with TrimSuffixC(), fixing assumptions.
[nmh] / mts / smtp / smtp.c
1 /*
2 * smtp.c -- nmh SMTP interface
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include "smtp.h"
11 #include <h/mts.h>
12 #include <h/signals.h>
13 #include <h/utils.h>
14 #include <h/netsec.h>
15
16 #include <sys/socket.h>
17
18 /*
19 * This module implements an interface to SendMail very similar
20 * to the MMDF mm_(3) routines. The sm_() routines herein talk
21 * SMTP to a sendmail process, mapping SMTP reply codes into
22 * RP_-style codes.
23 */
24
25 /*
26 * On older 4.2BSD machines without the POSIX function `sigaction',
27 * the alarm handing stuff for time-outs will NOT work due to the way
28 * syscalls get restarted. This is not really crucial, since SendMail
29 * is generally well-behaved in this area.
30 */
31
32 #ifdef SENDMAILBUG
33 /*
34 * It appears that some versions of Sendmail will return Code 451
35 * when they don't really want to indicate a failure.
36 * "Code 451 almost always means sendmail has deferred; we don't
37 * really want bomb out at this point since sendmail will rectify
38 * things later." So, if you define SENDMAILBUG, Code 451 is
39 * considered the same as Code 250. Yuck!
40 */
41 #endif
42
43 #define TRUE 1
44 #define FALSE 0
45
46 #define NBITS ((sizeof (int)) * 8)
47
48 /*
49 * these codes must all be different!
50 */
51 #define SM_OPEN 300 /* Changed to 5 minutes to comply with a SHOULD in RFC 1123 */
52 #define SM_HELO 20
53 #define SM_RSET 15
54 #define SM_MAIL 301 /* changed to 5 minutes and a second (for uniqueness), see above */
55 #define SM_RCPT 302 /* see above */
56 #define SM_DATA 120 /* see above */
57 #define SM_TEXT 180 /* see above */
58 #define SM_DOT 600 /* see above */
59 #define SM_QUIT 30
60 #define SM_CLOS 10
61 #define SM_AUTH 45
62
63 static int sm_addrs = 0;
64 static int sm_child = NOTOK;
65 static int sm_debug = 0;
66 static int sm_nl = TRUE;
67 static int sm_verbose = 0;
68 static netsec_context *nsc = NULL;
69
70 static char *sm_noreply = "No reply text given";
71 static char *sm_moreply = "; ";
72 static struct smtp sm_reply;
73
74 #define MAXEHLO 20
75
76 static int doingEHLO;
77 static char *EHLOkeys[MAXEHLO + 1];
78
79 /*
80 * static prototypes
81 */
82 static int smtp_init (char *, char *, char *, int, int, int, int, const char *,
83 const char *, const char *, int);
84 static int sendmail_init (char *, int, int, int, int, const char *,
85 const char *);
86
87 static int rclient (char *, char *);
88 static int sm_ierror (const char *fmt, ...);
89 static int sm_nerror (char *);
90 static int smtalk (int time, char *fmt, ...);
91 static int sm_wstream (char *, int);
92 static int smhear (void);
93 static char *EHLOset (char *);
94 static int sm_sasl_callback(enum sasl_message_type, unsigned const char *,
95 unsigned int, unsigned char **, unsigned int *,
96 char **);
97
98 int
99 sm_init (char *client, char *server, char *port, int watch, int verbose,
100 int debug, int sasl, const char *saslmech, const char *user,
101 const char *oauth_svc, int tls)
102 {
103 if (sm_mts == MTS_SMTP)
104 return smtp_init (client, server, port, watch, verbose,
105 debug, sasl, saslmech, user, oauth_svc, tls);
106 else
107 return sendmail_init (client, watch, verbose, debug, sasl,
108 saslmech, user);
109 }
110
111 static int
112 smtp_init (char *client, char *server, char *port, int watch, int verbose,
113 int debug, int sasl, const char *saslmech, const char *user,
114 const char *oauth_svc, int tls)
115 {
116 int result, sd1;
117 char *errstr;
118
119 if (watch)
120 verbose = TRUE;
121
122 sm_verbose = verbose;
123 sm_debug = debug;
124
125 if (nsc != NULL)
126 goto send_options;
127
128 if (client == NULL || *client == '\0') {
129 if (clientname) {
130 client = clientname;
131 } else {
132 client = LocalName(1); /* no clientname -> LocalName */
133 }
134 }
135
136 /*
137 * Last-ditch check just in case client still isn't set to anything
138 */
139
140 if (client == NULL || *client == '\0')
141 client = "localhost";
142
143 nsc = netsec_init();
144
145 if (user)
146 netsec_set_userid(nsc, user);
147
148 if (sm_debug)
149 netsec_set_snoop(nsc, 1);
150
151 if (sasl) {
152 if (netsec_set_sasl_params(nsc, server, "smtp", saslmech,
153 sm_sasl_callback, &errstr) != OK)
154 return sm_nerror(errstr);
155 }
156
157 if (oauth_svc) {
158 if (netsec_set_oauth_service(nsc, oauth_svc) != OK)
159 return sm_ierror("OAuth2 not supported");
160 }
161
162 if ((sd1 = rclient (server, port)) == NOTOK)
163 return RP_BHST;
164
165 SIGNAL (SIGPIPE, SIG_IGN);
166
167 netsec_set_fd(nsc, sd1, sd1);
168
169 if (tls) {
170 if (netsec_set_tls(nsc, 1, &errstr) != OK)
171 return sm_nerror(errstr);
172 }
173
174 /*
175 * If tls == 2, that means that the user requested "initial" TLS,
176 * which happens right after the connection has opened. Do that
177 * negotiation now
178 */
179
180 if (tls == 2) {
181 if (netsec_negotiate_tls(nsc, &errstr) != OK) {
182 sm_end(NOTOK);
183 return sm_nerror(errstr);
184 }
185 }
186
187 netsec_set_timeout(nsc, SM_OPEN);
188 result = smhear ();
189
190 switch (result) {
191 case 220:
192 break;
193
194 default:
195 sm_end (NOTOK);
196 return RP_RPLY;
197 }
198
199 /*
200 * Give EHLO or HELO command
201 */
202
203 doingEHLO = 1;
204 result = smtalk (SM_HELO, "EHLO %s", client);
205 doingEHLO = 0;
206
207 if (result >= 500 && result <= 599)
208 result = smtalk (SM_HELO, "HELO %s", client);
209
210 if (result != 250) {
211 sm_end (NOTOK);
212 return RP_RPLY;
213 }
214
215 /*
216 * If the user requested TLS support, then try to do the STARTTLS command
217 * as part of the initial dialog. Assuming this works, we then need to
218 * restart the EHLO dialog after TLS negotiation is complete.
219 */
220
221 if (tls == 1) {
222 if (! EHLOset("STARTTLS")) {
223 sm_end(NOTOK);
224 return sm_ierror("SMTP server does not support TLS");
225 }
226
227 result = smtalk(SM_HELO, "STARTTLS");
228
229 if (result != 220) {
230 sm_end(NOTOK);
231 return RP_RPLY;
232 }
233
234 /*
235 * Okay, the other side should be waiting for us to start TLS
236 * negotiation. Oblige them.
237 */
238
239 if (netsec_negotiate_tls(nsc, &errstr) != OK) {
240 sm_end(NOTOK);
241 return sm_nerror(errstr);
242 }
243
244 doingEHLO = 1;
245 result = smtalk (SM_HELO, "EHLO %s", client);
246 doingEHLO = 0;
247
248 if (result != 250) {
249 sm_end (NOTOK);
250 return RP_RPLY;
251 }
252 }
253
254 /*
255 * If the user asked for SASL, then check to see if the SMTP server
256 * supports it. Otherwise, error out (because the SMTP server
257 * might have been spoofed; we don't want to just silently not
258 * do authentication
259 */
260
261 if (sasl) {
262 char *server_mechs;
263 if (! (server_mechs = EHLOset("AUTH"))) {
264 sm_end(NOTOK);
265 return sm_ierror("SMTP server does not support SASL");
266 }
267
268 if (netsec_negotiate_sasl(nsc, server_mechs, &errstr) != OK) {
269 sm_end(NOTOK);
270 return sm_nerror(errstr);
271 }
272 }
273
274 send_options: ;
275 if (watch && EHLOset ("XVRB"))
276 smtalk (SM_HELO, "VERB on");
277
278 return RP_OK;
279 }
280
281 int
282 sendmail_init (char *client, int watch, int verbose, int debug, int sasl,
283 const char *saslmech, const char *user)
284 {
285 unsigned int i, result, vecp;
286 int pdi[2], pdo[2];
287 char *vec[15], *errstr;
288
289 if (watch)
290 verbose = TRUE;
291
292 sm_verbose = verbose;
293 sm_debug = debug;
294 if (nsc)
295 return RP_OK;
296
297 if (client == NULL || *client == '\0') {
298 if (clientname)
299 client = clientname;
300 else
301 client = LocalName(1); /* no clientname -> LocalName */
302 }
303
304 /*
305 * Last-ditch check just in case client still isn't set to anything
306 */
307
308 if (client == NULL || *client == '\0')
309 client = "localhost";
310
311 nsc = netsec_init();
312
313 if (user)
314 netsec_set_userid(nsc, user);
315
316 if (sm_debug)
317 netsec_set_snoop(nsc, 1);
318
319 if (sasl) {
320 if (netsec_set_sasl_params(nsc, client, "smtp", saslmech,
321 sm_sasl_callback, &errstr) != OK)
322 return sm_nerror(errstr);
323 }
324
325 if (pipe (pdi) == NOTOK)
326 return sm_ierror ("no pipes");
327 if (pipe (pdo) == NOTOK) {
328 close (pdi[0]);
329 close (pdi[1]);
330 return sm_ierror ("no pipes");
331 }
332
333 for (i = 0; (sm_child = fork ()) == NOTOK && i < 5; i++)
334 sleep (5);
335
336 switch (sm_child) {
337 case NOTOK:
338 close (pdo[0]);
339 close (pdo[1]);
340 close (pdi[0]);
341 close (pdi[1]);
342 return sm_ierror ("unable to fork");
343
344 case OK:
345 if (pdo[0] != fileno (stdin))
346 dup2 (pdo[0], fileno (stdin));
347 if (pdi[1] != fileno (stdout))
348 dup2 (pdi[1], fileno (stdout));
349 if (pdi[1] != fileno (stderr))
350 dup2 (pdi[1], fileno (stderr));
351 for (i = fileno (stderr) + 1; i < NBITS; i++)
352 close (i);
353
354 vecp = 0;
355 vec[vecp++] = r1bindex (sendmail, '/');
356 vec[vecp++] = "-bs";
357 vec[vecp++] = watch ? "-odi" : "-odb";
358 vec[vecp++] = "-oem";
359 vec[vecp++] = "-om";
360 if (verbose)
361 vec[vecp++] = "-ov";
362 vec[vecp++] = NULL;
363
364 execvp (sendmail, vec);
365 fprintf (stderr, "unable to exec ");
366 perror (sendmail);
367 _exit (-1); /* NOTREACHED */
368
369 default:
370 SIGNAL (SIGPIPE, SIG_IGN);
371
372 close (pdi[1]);
373 close (pdo[0]);
374
375 netsec_set_fd(nsc, pdi[i], pdo[1]);
376 netsec_set_timeout(nsc, SM_OPEN);
377 result = smhear ();
378 switch (result) {
379 case 220:
380 break;
381
382 default:
383 sm_end (NOTOK);
384 return RP_RPLY;
385 }
386
387 doingEHLO = 1;
388 result = smtalk (SM_HELO, "EHLO %s", client);
389 doingEHLO = 0;
390
391 if (500 <= result && result <= 599)
392 result = smtalk (SM_HELO, "HELO %s", client);
393
394 switch (result) {
395 case 250:
396 break;
397
398 default:
399 sm_end (NOTOK);
400 return RP_RPLY;
401 }
402
403 /*
404 * If the user asked for SASL, then check to see if the SMTP server
405 * supports it. Otherwise, error out (because the SMTP server
406 * might have been spoofed; we don't want to just silently not
407 * do authentication
408 */
409
410 if (sasl) {
411 char *server_mechs;
412 if (! (server_mechs = EHLOset("AUTH"))) {
413 sm_end(NOTOK);
414 return sm_ierror("SMTP server does not support SASL");
415 }
416 if (netsec_negotiate_sasl(nsc, server_mechs, &errstr) != OK) {
417 sm_end(NOTOK);
418 return sm_nerror(errstr);
419 }
420 }
421
422 if (watch)
423 smtalk (SM_HELO, "VERB on");
424
425 return RP_OK;
426 }
427 }
428
429 static int
430 rclient (char *server, char *service)
431 {
432 int sd;
433 char response[BUFSIZ];
434
435 if ((sd = client (server, service, response, sizeof(response),
436 sm_debug)) != NOTOK)
437 return sd;
438
439 sm_ierror ("%s", response);
440 return NOTOK;
441 }
442
443 int
444 sm_winit (char *from, int smtputf8, int eightbit)
445 {
446 const char *mail_parameters = "";
447
448 if (smtputf8) {
449 /* Just for information, if an attempt is made to send to an 8-bit
450 address without specifying SMTPUTF8, Gmail responds with
451 555 5.5.2 Syntax error.
452 Gmail doesn't require the 8BITMIME, but RFC 6531 Sec. 1.2 does. */
453 if (EHLOset ("8BITMIME") && EHLOset ("SMTPUTF8")) {
454 mail_parameters = " BODY=8BITMIME SMTPUTF8";
455 } else {
456 advise (NULL, "SMTP server does not support %s, not sending.\n"
457 "Rebuild message with 7-bit headers, WITHOUT -headerencoding utf-8.",
458 EHLOset ("SMTPUTF8") ? "8BITMIME" : "SMTPUTF8");
459 sm_end (NOTOK);
460 return RP_UCMD;
461 }
462 } else if (eightbit) {
463 /* Comply with RFC 6152, for messages that have any 8-bit characters
464 in their body. */
465 if (EHLOset ("8BITMIME")) {
466 mail_parameters = " BODY=8BITMIME";
467 } else {
468 advise (NULL, "SMTP server does not support 8BITMIME, not sending.\n"
469 "Suggest encoding message for 7-bit transport by setting your\n"
470 "locale to C, and/or specifying *b64 in mhbuild directives.");
471 sm_end (NOTOK);
472 return RP_UCMD;
473 }
474 }
475
476 switch (smtalk (SM_MAIL, "MAIL FROM:<%s>%s", from, mail_parameters)) {
477 case 250:
478 sm_addrs = 0;
479 return RP_OK;
480
481 case 500:
482 case 501:
483 case 552:
484 return RP_PARM;
485
486 default:
487 return RP_RPLY;
488 }
489 }
490
491
492 int
493 sm_wadr (char *mbox, char *host, char *path)
494 {
495 switch (smtalk (SM_RCPT, host && *host ? "RCPT TO:<%s%s@%s>"
496 : "RCPT TO:<%s%s>",
497 path ? path : "", mbox, host)) {
498 case 250:
499 case 251:
500 sm_addrs++;
501 return RP_OK;
502
503 case 451:
504 #ifdef SENDMAILBUG
505 sm_addrs++;
506 return RP_OK;
507 #endif /* SENDMAILBUG */
508 case 421:
509 case 450:
510 case 452:
511 return RP_NO;
512
513 case 500:
514 case 501:
515 return RP_PARM;
516
517 case 550:
518 case 551:
519 case 552:
520 case 553:
521 return RP_USER;
522
523 default:
524 return RP_RPLY;
525 }
526 }
527
528
529 int
530 sm_waend (void)
531 {
532 switch (smtalk (SM_DATA, "DATA")) {
533 case 354:
534 sm_nl = TRUE;
535 return RP_OK;
536
537 case 451:
538 #ifdef SENDMAILBUG
539 sm_nl = TRUE;
540 return RP_OK;
541 #endif /* SENDMAILBUG */
542 case 421:
543 return RP_NO;
544
545 case 500:
546 case 501:
547 case 503:
548 case 554:
549 return RP_NDEL;
550
551 default:
552 return RP_RPLY;
553 }
554 }
555
556
557 int
558 sm_wtxt (char *buffer, int len)
559 {
560 int result;
561
562 result = sm_wstream (buffer, len);
563
564 return (result == NOTOK ? RP_BHST : RP_OK);
565 }
566
567
568 int
569 sm_wtend (void)
570 {
571 if (sm_wstream ((char *) NULL, 0) == NOTOK)
572 return RP_BHST;
573
574 switch (smtalk (SM_DOT + 3 * sm_addrs, ".")) {
575 case 250:
576 case 251:
577 return RP_OK;
578
579 case 451:
580 #ifdef SENDMAILBUG
581 return RP_OK;
582 #endif /* SENDMAILBUG */
583 case 452:
584 default:
585 return RP_NO;
586
587 case 552:
588 case 554:
589 return RP_NDEL;
590 }
591 }
592
593
594 int
595 sm_end (int type)
596 {
597 int status;
598 struct smtp sm_note;
599
600 if (sm_mts == MTS_SENDMAIL_SMTP) {
601 switch (sm_child) {
602 case NOTOK:
603 case OK:
604 return RP_OK;
605
606 default:
607 break;
608 }
609 }
610
611 if (nsc == NULL)
612 return RP_OK;
613
614 switch (type) {
615 case OK:
616 smtalk (SM_QUIT, "QUIT");
617 break;
618
619 case NOTOK:
620 sm_note.code = sm_reply.code;
621 sm_note.length = sm_reply.length;
622 memcpy (sm_note.text, sm_reply.text, sm_reply.length + 1);/* fall */
623 case DONE:
624 if (smtalk (SM_RSET, "RSET") == 250 && type == DONE)
625 return RP_OK;
626 if (sm_mts == MTS_SMTP)
627 smtalk (SM_QUIT, "QUIT");
628 else {
629 /* The SIGPIPE block replaces old calls to discard ().
630 We're not sure what the discard () calls were for,
631 maybe to prevent deadlock on old systems. In any
632 case, blocking SIGPIPE should be harmless.
633 Because the file handles are closed below, leave it
634 blocked. */
635 sigset_t set, oset;
636 sigemptyset (&set);
637 sigaddset (&set, SIGPIPE);
638 sigprocmask (SIG_BLOCK, &set, &oset);
639
640 kill (sm_child, SIGKILL);
641 sm_child = NOTOK;
642 }
643 if (type == NOTOK) {
644 sm_reply.code = sm_note.code;
645 sm_reply.length = sm_note.length;
646 memcpy (sm_reply.text, sm_note.text, sm_note.length + 1);
647 }
648 break;
649 }
650
651 if (nsc != NULL) {
652 netsec_shutdown(nsc, 1);
653 nsc = NULL;
654 }
655
656 if (sm_mts == MTS_SMTP) {
657 status = 0;
658 } else if (sm_child != NOTOK) {
659 status = pidwait (sm_child, OK);
660 sm_child = NOTOK;
661 } else {
662 status = OK;
663 }
664
665 return (status ? RP_BHST : RP_OK);
666 }
667
668
669 static int
670 sm_ierror (const char *fmt, ...)
671 {
672 va_list ap;
673
674 va_start(ap, fmt);
675 vsnprintf (sm_reply.text, sizeof(sm_reply.text), fmt, ap);
676 va_end(ap);
677
678 sm_reply.length = strlen (sm_reply.text);
679 sm_reply.code = NOTOK;
680
681 return RP_BHST;
682 }
683
684 /*
685 * Like sm_ierror, but assume it's an allocated error string we need to free.
686 */
687
688 static int
689 sm_nerror (char *str)
690 {
691 strncpy(sm_reply.text, str, sizeof(sm_reply.text));
692 sm_reply.text[sizeof(sm_reply.text) - 1] = '\0';
693 sm_reply.length = strlen(sm_reply.text);
694 sm_reply.code = NOTOK;
695 free(str);
696
697 return RP_BHST;
698 }
699
700 static int
701 smtalk (int time, char *fmt, ...)
702 {
703 va_list ap;
704 char *errstr;
705 int result;
706
707 va_start(ap, fmt);
708 result = netsec_vprintf (nsc, &errstr, fmt, ap);
709 va_end(ap);
710
711 if (result != OK)
712 return sm_nerror(errstr);
713
714 if (netsec_printf (nsc, &errstr, "\r\n") != OK)
715 return sm_nerror(errstr);
716
717 if (netsec_flush (nsc, &errstr) != OK)
718 return sm_nerror(errstr);
719
720 netsec_set_timeout(nsc, time);
721
722 return smhear ();
723 }
724
725
726 static int
727 sm_wstream (char *buffer, int len)
728 {
729 char *bp, *errstr;
730 static char lc = '\0';
731 int rc;
732
733 if (nsc == NULL) {
734 sm_ierror("No socket opened");
735 return NOTOK;
736 }
737
738 if (buffer == NULL && len == 0) {
739 rc = OK;
740 if (lc != '\n')
741 rc = netsec_write(nsc, "\r\n", 2, &errstr);
742 lc = '\0';
743 if (rc != OK)
744 sm_nerror(errstr);
745 return rc;
746 }
747
748 for (bp = buffer; bp && len > 0; bp++, len--) {
749 switch (*bp) {
750 case '\n':
751 sm_nl = TRUE;
752 if (netsec_write(nsc, "\r", 1, &errstr) != OK) {
753 sm_nerror(errstr);
754 return NOTOK;
755 }
756 break;
757
758 case '.':
759 if (sm_nl)
760 if (netsec_write(nsc, ".", 1, &errstr) != OK) {
761 sm_nerror(errstr);
762 return NOTOK;
763 } /* FALL THROUGH */
764
765 default:
766 sm_nl = FALSE;
767 }
768 if (netsec_write(nsc, bp, 1, &errstr) != OK) {
769 sm_nerror(errstr);
770 return NOTOK;
771 }
772 }
773
774 if (bp > buffer)
775 lc = *--bp;
776 return OK;
777 }
778
779
780 static int
781 smhear (void)
782 {
783 int i, code, cont, more;
784 size_t buflen, rc;
785 unsigned char *bp;
786 char *rp;
787 char *errstr;
788 char **ehlo = EHLOkeys, *buffer;
789
790 if (doingEHLO) {
791 static int at_least_once = 0;
792
793 if (at_least_once) {
794 char *ep;
795
796 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
797 ep = *ehlo;
798 free (ep);
799 }
800 } else {
801 at_least_once = 1;
802 }
803
804 ehlo = EHLOkeys;
805 *ehlo = NULL;
806 }
807
808 again: ;
809
810 sm_reply.length = 0;
811 sm_reply.text[0] = 0;
812 rp = sm_reply.text;
813 rc = sizeof(sm_reply.text) - 1;
814
815 for (more = FALSE; (buffer = netsec_readline(nsc, &buflen,
816 &errstr)) != NULL ; ) {
817
818 if (doingEHLO
819 && strncmp (buffer, "250", sizeof("250") - 1) == 0
820 && (buffer[3] == '-' || doingEHLO == 2)
821 && buffer[4]) {
822 if (doingEHLO == 2) {
823 if ((*ehlo = malloc ((size_t) (strlen (buffer + 4) + 1)))) {
824 strcpy (*ehlo++, buffer + 4);
825 *ehlo = NULL;
826 if (ehlo >= EHLOkeys + MAXEHLO)
827 doingEHLO = 0;
828 }
829 else
830 doingEHLO = 0;
831 }
832 else
833 doingEHLO = 2;
834 }
835
836 bp = (unsigned char *) buffer;
837
838 for (; buflen > 0 && (!isascii (*bp) || !isdigit (*bp)); bp++, buflen--)
839 continue;
840
841 cont = FALSE;
842 code = atoi ((char *) bp);
843 bp += 3, buflen -= 3;
844 for (; buflen > 0 && isspace (*bp); bp++, buflen--)
845 continue;
846 if (buflen > 0 && *bp == '-') {
847 cont = TRUE;
848 bp++, buflen--;
849 for (; buflen > 0 && isspace (*bp); bp++, buflen--)
850 continue;
851 }
852
853 if (more) {
854 if (code != sm_reply.code || cont)
855 continue;
856 more = FALSE;
857 } else {
858 sm_reply.code = code;
859 more = cont;
860 if (buflen <= 0) {
861 bp = (unsigned char *) sm_noreply;
862 buflen = strlen (sm_noreply);
863 }
864 }
865
866 if ((i = min (buflen, rc)) > 0) {
867 memcpy (rp, bp, i);
868 rp += i;
869 rc -= i;
870 i = strlen(sm_moreply);
871 if (more && (int) rc > i + 1) {
872 memcpy (rp, sm_moreply, i); /* safe because of check in if() */
873 rp += i;
874 rc -= i;
875 }
876 }
877 if (more)
878 continue;
879 if (sm_reply.code < 100) {
880 if (sm_verbose) {
881 printf ("%s\n", sm_reply.text);
882 fflush (stdout);
883 }
884 goto again;
885 }
886
887 sm_reply.length = rp - sm_reply.text;
888 sm_reply.text[sm_reply.length] = 0;
889 return sm_reply.code;
890 }
891
892 sm_nerror(errstr);
893 return NOTOK;
894 }
895
896
897 char *
898 rp_string (int code)
899 {
900 char *text;
901 static char buffer[BUFSIZ];
902
903 switch (sm_reply.code != NOTOK ? code : NOTOK) {
904 case RP_AOK:
905 text = "AOK";
906 break;
907
908 case RP_MOK:
909 text = "MOK";
910 break;
911
912 case RP_OK:
913 text = "OK";
914 break;
915
916 case RP_RPLY:
917 text = "RPLY";
918 break;
919
920 case RP_BHST:
921 default:
922 text = "BHST";
923 snprintf (buffer, sizeof(buffer), "[%s] %s", text, sm_reply.text);
924 return buffer;
925
926 case RP_PARM:
927 text = "PARM";
928 break;
929
930 case RP_NO:
931 text = "NO";
932 break;
933
934 case RP_USER:
935 text = "USER";
936 break;
937
938 case RP_NDEL:
939 text = "NDEL";
940 break;
941 }
942
943 snprintf (buffer, sizeof(buffer), "[%s] %3d %s",
944 text, sm_reply.code, sm_reply.text);
945 return buffer;
946 }
947
948 static char *
949 EHLOset (char *s)
950 {
951 size_t len;
952 char *ep, **ehlo;
953
954 len = strlen (s);
955
956 for (ehlo = EHLOkeys; *ehlo; ehlo++) {
957 ep = *ehlo;
958 if (strncmp (ep, s, len) == 0) {
959 for (ep += len; *ep == ' '; ep++)
960 continue;
961 return ep;
962 }
963 }
964
965 return 0;
966 }
967
968 /*
969 * Our SASL callback; we are either given SASL tokens to generate network
970 * protocols messages for, or we decode incoming protocol messages and
971 * convert them to binary SASL tokens to pass up into the SASL library.
972 */
973
974 static int
975 sm_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata,
976 unsigned int indatalen, unsigned char **outdata,
977 unsigned int *outdatalen, char **errstr)
978 {
979 int rc, snoopoffset;
980 char *mech, *line;
981 size_t len;
982
983 switch (mtype) {
984 case NETSEC_SASL_START:
985 /*
986 * Generate an AUTH message; if we were given an input token
987 * then generate a an AUTH message that includes the initial
988 * response.
989 */
990
991 mech = netsec_get_sasl_mechanism(nsc);
992
993 if (indatalen) {
994 char *b64data;
995
996 b64data = mh_xmalloc(BASE64SIZE(indatalen));
997 writeBase64raw(indata, indatalen, (unsigned char *) b64data);
998
999 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder,
1000 &snoopoffset);
1001 snoopoffset = 6 + strlen(mech);
1002 rc = netsec_printf(nsc, errstr, "AUTH %s %s\r\n", mech, b64data);
1003 free(b64data);
1004 netsec_set_snoop_callback(nsc, NULL, NULL);
1005 } else {
1006 rc = netsec_printf(nsc, errstr, "AUTH %s\r\n", mech);
1007 }
1008
1009 if (rc != OK)
1010 return NOTOK;
1011
1012 if (netsec_flush(nsc, errstr) != OK)
1013 return NOTOK;
1014
1015 break;
1016
1017 case NETSEC_SASL_READ:
1018 /*
1019 * Read in a line that should contain a 334 response code, followed
1020 * by base64 response data.
1021 */
1022
1023 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, &snoopoffset);
1024 snoopoffset = 4;
1025 line = netsec_readline(nsc, &len, errstr);
1026 netsec_set_snoop_callback(nsc, NULL, NULL);
1027
1028 if (line == NULL)
1029 return NOTOK;
1030
1031 if (len < 4) {
1032 netsec_err(errstr, "Invalid format for SASL response");
1033 return NOTOK;
1034 }
1035
1036 if (strncmp(line, "334 ", 4) != 0) {
1037 netsec_err(errstr, "Improper SASL protocol response: %s", line);
1038 return NOTOK;
1039 }
1040
1041 if (len == 4) {
1042 *outdata = NULL;
1043 *outdatalen = 0;
1044 } else {
1045 rc = decodeBase64(line + 4, outdata, &len, 0, NULL);
1046 if (rc != OK) {
1047 netsec_err(errstr, "Unable to decode base64 response");
1048 return NOTOK;
1049 }
1050 *outdatalen = len;
1051 }
1052 break;
1053
1054 case NETSEC_SASL_WRITE:
1055 /*
1056 * The output encoding is pretty simple, so this is easy.
1057 */
1058 if (indatalen == 0) {
1059 rc = netsec_printf(nsc, errstr, "\r\n");
1060 } else {
1061 unsigned char *b64data;
1062 b64data = mh_xmalloc(BASE64SIZE(indatalen));
1063 writeBase64raw(indata, indatalen, b64data);
1064 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, NULL);
1065 rc = netsec_printf(nsc, errstr, "%s\r\n", b64data);
1066 netsec_set_snoop_callback(nsc, NULL, NULL);
1067 free(b64data);
1068 }
1069
1070 if (rc != OK)
1071 return NOTOK;
1072
1073 if (netsec_flush(nsc, errstr) != OK)
1074 return NOTOK;
1075 break;
1076
1077 case NETSEC_SASL_FINISH:
1078 /*
1079 * Finish the protocol; we're looking for a 235 message.
1080 */
1081 line = netsec_readline(nsc, &len, errstr);
1082 if (line == NULL)
1083 return NOTOK;
1084
1085 if (strncmp(line, "235 ", 4) != 0) {
1086 if (len > 4)
1087 netsec_err(errstr, "Authentication failed: %s", line + 4);
1088 else
1089 netsec_err(errstr, "Authentication failed: %s", line);
1090 return NOTOK;
1091 }
1092 break;
1093
1094 case NETSEC_SASL_CANCEL:
1095 /*
1096 * Cancel the SASL exchange; this is done by sending a single "*".
1097 */
1098 rc = netsec_printf(nsc, errstr, "*\r\n");
1099 if (rc == OK)
1100 rc = netsec_flush(nsc, errstr);
1101 if (rc != OK)
1102 return NOTOK;
1103 break;
1104 }
1105
1106 return OK;
1107 }