SMTP-Client: Implement AUTH LOGIN
[citadel.git] / citadel / modules / smtp / smtp_clienthandlers.c
1 /*
2  * This module is an SMTP and ESMTP implementation for the Citadel system.
3  * It is compliant with all of the following:
4  *
5  * RFC  821 - Simple Mail Transfer Protocol
6  * RFC  876 - Survey of SMTP Implementations
7  * RFC 1047 - Duplicate messages and SMTP
8  * RFC 1652 - 8 bit MIME
9  * RFC 1869 - Extended Simple Mail Transfer Protocol
10  * RFC 1870 - SMTP Service Extension for Message Size Declaration
11  * RFC 2033 - Local Mail Transfer Protocol
12  * RFC 2197 - SMTP Service Extension for Command Pipelining
13  * RFC 2476 - Message Submission
14  * RFC 2487 - SMTP Service Extension for Secure SMTP over TLS
15  * RFC 2554 - SMTP Service Extension for Authentication
16  * RFC 2821 - Simple Mail Transfer Protocol
17  * RFC 2822 - Internet Message Format
18  * RFC 2920 - SMTP Service Extension for Command Pipelining
19  *
20  * Copyright (c) 1998-2012 by the citadel.org team
21  *
22  *  This program is open source software; you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License version 3.
24  *  
25  *  
26  *
27  *  This program is distributed in the hope that it will be useful,
28  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  *  GNU General Public License for more details.
31  *
32  *  
33  *  
34  *  
35  */
36
37 #include "sysdep.h"
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <stdio.h>
41 #include <termios.h>
42 #include <fcntl.h>
43 #include <signal.h>
44 #include <pwd.h>
45 #include <errno.h>
46 #include <sys/types.h>
47 #include <syslog.h>
48
49 #if TIME_WITH_SYS_TIME
50 # include <sys/time.h>
51 # include <time.h>
52 #else
53 # if HAVE_SYS_TIME_H
54 #  include <sys/time.h>
55 # else
56 #  include <time.h>
57 # endif
58 #endif
59 #include <sys/wait.h>
60 #include <ctype.h>
61 #include <string.h>
62 #include <limits.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <libcitadel.h>
67 #include "citadel.h"
68 #include "server.h"
69 #include "citserver.h"
70 #include "support.h"
71 #include "config.h"
72 #include "control.h"
73 #include "user_ops.h"
74 #include "database.h"
75 #include "msgbase.h"
76 #include "internet_addressing.h"
77 #include "genstamp.h"
78 #include "domain.h"
79 #include "clientsocket.h"
80 #include "locate_host.h"
81 #include "citadel_dirs.h"
82
83 #include "ctdl_module.h"
84
85 #include "smtp_util.h"
86 #include "event_client.h"
87 #include "smtpqueue.h"
88 #include "smtp_clienthandlers.h"
89
90
91 #define SMTP_ERROR(WHICH_ERR, ERRSTR) do {                             \
92                 Msg->MyQEntry->Status = WHICH_ERR;                     \
93                 StrBufAppendBufPlain(Msg->MyQEntry->StatusMessage,     \
94                                      HKEY(ERRSTR), 0);                 \
95                 StrBufTrim(Msg->MyQEntry->StatusMessage);              \
96                 return eAbort; }                                       \
97         while (0)
98
99 #define SMTP_VERROR(WHICH_ERR) do {                            \
100                 Msg->MyQEntry->Status = WHICH_ERR;             \
101                 StrBufPlain(Msg->MyQEntry->StatusMessage,      \
102                             ChrPtr(Msg->IO.IOBuf) + 4,         \
103                             StrLength(Msg->IO.IOBuf) - 4);     \
104                 StrBufTrim(Msg->MyQEntry->StatusMessage);      \
105                 return eAbort; }                               \
106         while (0)
107
108 #define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(Msg->IO.IOBuf)[0] == WHICH_STATE)
109
110 #define SMTP_DBG_SEND() \
111         EVS_syslog(LOG_DEBUG, "> %s\n", ChrPtr(Msg->IO.SendBuf.Buf))
112
113 #define SMTP_DBG_READ() \
114         EVS_syslog(LOG_DEBUG, "< %s\n", ChrPtr(Msg->IO.IOBuf))
115
116 /*
117  * if a Read handler wants to skip to a specific part use this macro.
118  * the -1 is here since the auto-forward following has to be taken into account.
119  */
120 #define READ_NEXT_STATE(state) Msg->State = state - 1
121
122 /*****************************************************************************/
123 /*                     SMTP CLIENT STATE CALLBACKS                           */
124 /*****************************************************************************/
125 eNextState SMTPC_read_greeting(SmtpOutMsg *Msg)
126 {
127         /* Process the SMTP greeting from the server */
128         AsyncIO *IO = &Msg->IO;
129         SMTP_DBG_READ();
130         SetSMTPState(IO, eSTMPsmtp);
131
132         if (!SMTP_IS_STATE('2')) {
133                 if (SMTP_IS_STATE('4'))
134                         SMTP_VERROR(4);
135                 else
136                         SMTP_VERROR(5);
137         }
138         return eSendReply;
139 }
140
141 eNextState SMTPC_send_EHLO(SmtpOutMsg *Msg)
142 {
143         AsyncIO *IO = &Msg->IO;
144         /* At this point we know we are talking to a real SMTP server */
145
146         /* Do a EHLO command.  If it fails, try the HELO command. */
147         StrBufPrintf(Msg->IO.SendBuf.Buf,
148                      "EHLO %s\r\n", config.c_fqdn);
149
150         SMTP_DBG_SEND();
151         return eReadMessage;
152 }
153
154 eNextState SMTPC_read_EHLO_reply(SmtpOutMsg *Msg)
155 {
156         AsyncIO *IO = &Msg->IO;
157         SMTP_DBG_READ();
158
159         if (SMTP_IS_STATE('2')) {
160                 READ_NEXT_STATE(eSMTPAuth);
161
162                 if ((Msg->pCurrRelay == NULL) ||
163                     (Msg->pCurrRelay->User == NULL))
164                         READ_NEXT_STATE(eFROM); /* Skip auth... */
165                 if (Msg->pCurrRelay != NULL)
166                 {
167                         if (strstr(ChrPtr(Msg->IO.IOBuf), "LOGIN") != NULL)
168                                 Msg->SendLogin = 1;
169                 }
170         }
171         /* else we fall back to 'helo' */
172         return eSendReply;
173 }
174
175 eNextState STMPC_send_HELO(SmtpOutMsg *Msg)
176 {
177         AsyncIO *IO = &Msg->IO;
178         StrBufPrintf(Msg->IO.SendBuf.Buf,
179                      "HELO %s\r\n", config.c_fqdn);
180
181         SMTP_DBG_SEND();
182         return eReadMessage;
183 }
184
185 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *Msg)
186 {
187         AsyncIO *IO = &Msg->IO;
188         SMTP_DBG_READ();
189
190         if (!SMTP_IS_STATE('2'))
191         {
192                 if (SMTP_IS_STATE('4'))
193                         SMTP_VERROR(4);
194                 else
195                         SMTP_VERROR(5);
196         }
197         if (Msg->pCurrRelay != NULL)
198         {
199                 if (strstr(ChrPtr(Msg->IO.IOBuf), "LOGIN") != NULL)
200                         Msg->SendLogin = 1;
201         }
202         if ((Msg->pCurrRelay == NULL) ||
203             (Msg->pCurrRelay->User == NULL))
204                 READ_NEXT_STATE(eFROM); /* Skip auth... */
205
206         return eSendReply;
207 }
208
209 eNextState SMTPC_send_auth(SmtpOutMsg *Msg)
210 {
211         AsyncIO *IO = &Msg->IO;
212         char buf[SIZ];
213         char encoded[1024];
214
215         if ((Msg->pCurrRelay == NULL) ||
216             (Msg->pCurrRelay->User == NULL))
217                 READ_NEXT_STATE(eFROM); /* Skip auth, shouldn't even come here!... */
218         else {
219                 /* Do an AUTH command if necessary */
220                 if (Msg->SendLogin)
221                 {
222                         StrBufPlain(Msg->IO.SendBuf.Buf,
223                                     HKEY("AUTH LOGIN\r\n"));
224                 }
225                 else
226                 {
227                         sprintf(buf, "%s%c%s%c%s",
228                                 Msg->pCurrRelay->User, '\0',
229                                 Msg->pCurrRelay->User, '\0',
230                                 Msg->pCurrRelay->Pass);
231                         
232                         CtdlEncodeBase64(encoded, buf,
233                                          strlen(Msg->pCurrRelay->User) * 2 +
234                                          strlen(Msg->pCurrRelay->Pass) + 2, 0);
235                         
236                         StrBufPrintf(Msg->IO.SendBuf.Buf,
237                                      "AUTH PLAIN %s\r\n",
238                                      encoded);
239                 }
240         }
241         SMTP_DBG_SEND();
242         return eReadMessage;
243 }
244
245
246 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
247 {
248         AsyncIO *IO = &Msg->IO;
249         /* Do an AUTH command if necessary */
250
251         SMTP_DBG_READ();
252
253         if (Msg->SendLogin)
254         {
255                 if (!SMTP_IS_STATE('3'))
256                         SMTP_VERROR(5);
257         }
258         else
259         {
260                 if (!SMTP_IS_STATE('2')) {
261                         if (SMTP_IS_STATE('4'))
262                                 SMTP_VERROR(4);
263                         else
264                                 SMTP_VERROR(5);
265                 }
266                 READ_NEXT_STATE(eFROM);
267         }
268         return eSendReply;
269 }
270
271
272 eNextState SMTPC_send_authplain_1(SmtpOutMsg *Msg)
273 {
274         AsyncIO *IO = &Msg->IO;
275         char buf[SIZ];
276         char encoded[1024];
277         long encodedlen;
278
279         sprintf(buf, "%s",
280                 Msg->pCurrRelay->User);
281         
282         encodedlen = CtdlEncodeBase64(
283                 encoded,
284                 Msg->pCurrRelay->User,
285                 strlen(Msg->pCurrRelay->User),
286                 0);
287
288         StrBufPlain(Msg->IO.SendBuf.Buf,
289                     encoded,
290                     encodedlen);
291
292         StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
293                              HKEY("\r\n"), 0);
294
295         SMTP_DBG_SEND();
296
297         return eReadMessage;
298 }
299 eNextState SMTPC_read_auth_plain_reply_1(SmtpOutMsg *Msg)
300 {
301         AsyncIO *IO = &Msg->IO;
302         /* Do an AUTH command if necessary */
303
304         SMTP_DBG_READ();
305
306         if (!SMTP_IS_STATE('3'))
307                 SMTP_VERROR(5);
308         return eSendReply;
309 }
310
311
312 eNextState SMTPC_send_authplain_2(SmtpOutMsg *Msg)
313 {
314         AsyncIO *IO = &Msg->IO;
315         char buf[SIZ];
316         char encoded[1024];
317         long encodedlen;
318
319         sprintf(buf, "%s",
320                 Msg->pCurrRelay->Pass);
321         
322         encodedlen = CtdlEncodeBase64(
323                 encoded,
324                 Msg->pCurrRelay->User,
325                 strlen(Msg->pCurrRelay->User),
326                 0);
327
328         StrBufPlain(Msg->IO.SendBuf.Buf,
329                     encoded,
330                     encodedlen);
331
332         StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
333                              HKEY("\r\n"), 0);
334
335         SMTP_DBG_SEND();
336
337         return eReadMessage;
338 }
339 eNextState SMTPC_read_auth_plain_reply_2(SmtpOutMsg *Msg)
340 {
341         AsyncIO *IO = &Msg->IO;
342         /* Do an AUTH command if necessary */
343
344         SMTP_DBG_READ();
345
346         if (!SMTP_IS_STATE('2')) {
347                 if (SMTP_IS_STATE('4'))
348                         SMTP_VERROR(4);
349                 else
350                         SMTP_VERROR(5);
351         }
352         return eSendReply;
353 }
354
355 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
356 {
357         AsyncIO *IO = &Msg->IO;
358         /* previous command succeeded, now try the MAIL FROM: command */
359         StrBufPrintf(Msg->IO.SendBuf.Buf,
360                      "MAIL FROM:<%s>\r\n",
361                      Msg->envelope_from);
362
363         SMTP_DBG_SEND();
364         return eReadMessage;
365 }
366
367 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
368 {
369         AsyncIO *IO = &Msg->IO;
370         SMTP_DBG_READ();
371
372         if (!SMTP_IS_STATE('2')) {
373                 if (SMTP_IS_STATE('4'))
374                         SMTP_VERROR(4);
375                 else
376                         SMTP_VERROR(5);
377         }
378         return eSendReply;
379 }
380
381
382 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
383 {
384         AsyncIO *IO = &Msg->IO;
385         /* MAIL succeeded, now try the RCPT To: command */
386         StrBufPrintf(Msg->IO.SendBuf.Buf,
387                      "RCPT TO:<%s@%s>\r\n",
388                      Msg->user,
389                      Msg->node);
390
391         SMTP_DBG_SEND();
392         return eReadMessage;
393 }
394
395 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
396 {
397         AsyncIO *IO = &Msg->IO;
398         SMTP_DBG_READ();
399
400         if (!SMTP_IS_STATE('2')) {
401                 if (SMTP_IS_STATE('4'))
402                         SMTP_VERROR(4);
403                 else
404                         SMTP_VERROR(5);
405         }
406         return eSendReply;
407 }
408
409 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
410 {
411         AsyncIO *IO = &Msg->IO;
412         /* RCPT succeeded, now try the DATA command */
413         StrBufPlain(Msg->IO.SendBuf.Buf,
414                     HKEY("DATA\r\n"));
415
416         SMTP_DBG_SEND();
417         return eReadMessage;
418 }
419
420 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
421 {
422         AsyncIO *IO = &Msg->IO;
423         SMTP_DBG_READ();
424
425         if (!SMTP_IS_STATE('3')) {
426                 SetSMTPState(IO, eSTMPfailOne);
427                 if (SMTP_IS_STATE('4'))
428                         SMTP_VERROR(3);
429                 else
430                         SMTP_VERROR(5);
431         }
432         SetSMTPState(IO, eSTMPsmtpdata);
433         return eSendReply;
434 }
435
436 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
437 {
438         StrBuf *Buf;
439         /* If we reach this point, the server is expecting data.*/
440
441         Buf = Msg->IO.SendBuf.Buf;
442         Msg->IO.SendBuf.Buf = Msg->msgtext;
443         Msg->msgtext = Buf;
444         /* 
445          * sending the message itself doesn't use this state machine.
446          * so we have to operate it here by ourselves.
447          */
448         Msg->State ++;
449
450         return eSendMore;
451 }
452
453 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
454 {
455         StrBuf *Buf;
456
457         Buf = Msg->IO.SendBuf.Buf;
458         Msg->IO.SendBuf.Buf = Msg->msgtext;
459         Msg->msgtext = Buf;
460
461         StrBufPlain(Msg->IO.SendBuf.Buf,
462                     HKEY(".\r\n"));
463
464         return eReadMessage;
465
466 }
467
468 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
469 {
470         AsyncIO *IO = &Msg->IO;
471         SMTP_DBG_READ();
472
473         if (!SMTP_IS_STATE('2')) {
474                 if (SMTP_IS_STATE('4'))
475                         SMTP_VERROR(4);
476                 else
477                         SMTP_VERROR(5);
478         }
479
480         SetSMTPState(IO, eSTMPsmtpdone);
481         /* We did it! */
482         StrBufPlain(Msg->MyQEntry->StatusMessage,
483                     &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
484                     StrLength(Msg->IO.RecvBuf.Buf) - 4);
485         StrBufTrim(Msg->MyQEntry->StatusMessage);
486         Msg->MyQEntry->Status = 2;
487         return eSendReply;
488 }
489
490 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
491 {
492         AsyncIO *IO = &Msg->IO;
493         StrBufPlain(Msg->IO.SendBuf.Buf,
494                     HKEY("QUIT\r\n"));
495
496         SMTP_DBG_SEND();
497         return eReadMessage;
498 }
499
500 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
501 {
502         AsyncIO *IO = &Msg->IO;
503         SMTP_DBG_READ();
504
505         EVS_syslog(LOG_DEBUG,
506                    "delivery to <%s> @ <%s> (%s) succeeded\n",
507                    Msg->user,
508                    Msg->node,
509                    Msg->name);
510
511         return eTerminateConnection;
512 }
513
514 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
515 {
516         return eSendReply;
517 }
518
519 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
520 {
521         return eReadMessage;
522 }
523
524 /*****************************************************************************/
525 /*                     SMTP CLIENT DISPATCHER                                */
526 /*****************************************************************************/
527 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
528         SMTPC_read_greeting,
529         SMTPC_read_EHLO_reply,
530         SMTPC_read_HELO_reply,
531         SMTPC_read_auth_reply,
532         SMTPC_read_auth_plain_reply_1,
533         SMTPC_read_auth_plain_reply_2,
534         SMTPC_read_FROM_reply,
535         SMTPC_read_RCPT_reply,
536         SMTPC_read_DATAcmd_reply,
537         SMTPC_read_dummy,
538         SMTPC_read_data_body_reply,
539         SMTPC_read_QUIT_reply
540 };
541 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
542         SMTPC_send_dummy, /* we don't send a greeting, the server does... */
543         SMTPC_send_EHLO,
544         STMPC_send_HELO,
545         SMTPC_send_auth,
546         SMTPC_send_authplain_1,
547         SMTPC_send_authplain_2,
548         SMTPC_send_FROM,
549         SMTPC_send_RCPT,
550         SMTPC_send_DATAcmd,
551         SMTPC_send_data_body,
552         SMTPC_send_terminate_data_body,
553         SMTPC_send_QUIT
554 };
555
556 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
557
558 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
559         300., /* Greeting... */
560         30., /* EHLO */
561         30., /* HELO */
562         30., /* Auth */
563         30., /* Auth */
564         30., /* Auth */
565         30., /* From */
566         90., /* RCPT */
567         30., /* DATA */
568         90., /* DATABody */
569         90., /* end of body... */
570         30.  /* QUIT */
571 };
572 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
573         90., /* Greeting... */
574         30., /* EHLO */
575         30., /* HELO */
576         30., /* Auth */
577         30., /* Auth */
578         30., /* Auth */
579         30., /* From */
580         30., /* RCPT */
581         30., /* DATA */
582         90., /* DATABody */
583         900., /* end of body... */
584         30.  /* QUIT */
585 };
586
587 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
588         {HKEY("Connection broken during SMTP conversation")},
589         {HKEY("Connection broken during SMTP EHLO")},
590         {HKEY("Connection broken during SMTP HELO")},
591         {HKEY("Connection broken during SMTP AUTH")},
592         {HKEY("Connection broken during SMTP AUTH PLAIN I")},
593         {HKEY("Connection broken during SMTP AUTH PLAIN II")},
594         {HKEY("Connection broken during SMTP MAIL FROM")},
595         {HKEY("Connection broken during SMTP RCPT")},
596         {HKEY("Connection broken during SMTP DATA")},
597         {HKEY("Connection broken during SMTP message transmit")},
598         {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
599         {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
600         {HKEY("")}/* quit reply, don't care. */
601 };
602
603
604
605
606
607 int smtp_resolve_recipients(SmtpOutMsg *Msg)
608 {
609         AsyncIO *IO = &Msg->IO;
610         const char *ptr;
611         char buf[1024];
612         int scan_done;
613         int lp, rp;
614         int i;
615
616         EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
617
618         if ((Msg==NULL) ||
619             (Msg->MyQEntry == NULL) ||
620             (StrLength(Msg->MyQEntry->Recipient) == 0)) {
621                 return 0;
622         }
623
624         /* Parse out the host portion of the recipient address */
625         process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
626                             Msg->user,
627                             Msg->node,
628                             Msg->name);
629
630         EVNCS_syslog(LOG_DEBUG,
631                      "Attempting delivery to <%s> @ <%s> (%s)\n",
632                      Msg->user,
633                      Msg->node,
634                      Msg->name);
635
636         /* If no envelope_from is supplied, extract one from the message */
637         Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
638         if ( (Msg->envelope_from == NULL) ||
639              (IsEmptyStr(Msg->envelope_from)) ) {
640                 Msg->mailfrom[0] = '\0';
641                 scan_done = 0;
642                 ptr = ChrPtr(Msg->msgtext);
643                 do {
644                         if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
645                         {
646                                 scan_done = 1;
647                         }
648                         if (!strncasecmp(buf, "From:", 5))
649                         {
650                                 safestrncpy(Msg->mailfrom,
651                                             &buf[5],
652                                             sizeof Msg->mailfrom);
653
654                                 striplt(Msg->mailfrom);
655                                 for (i=0; Msg->mailfrom[i]; ++i) {
656                                         if (!isprint(Msg->mailfrom[i]))
657                                         {
658                                                 strcpy(&Msg->mailfrom[i],
659                                                        &Msg->mailfrom[i+1]);
660                                                 i=0;
661                                         }
662                                 }
663
664                                 /* Strip out parenthesized names */
665                                 lp = (-1);
666                                 rp = (-1);
667                                 for (i=0;
668                                      !IsEmptyStr(Msg->mailfrom + i);
669                                      ++i)
670                                 {
671                                         if (Msg->mailfrom[i] == '(') lp = i;
672                                         if (Msg->mailfrom[i] == ')') rp = i;
673                                 }
674                                 if ((lp>0)&&(rp>lp))
675                                 {
676                                         strcpy(&Msg->mailfrom[lp-1],
677                                                &Msg->mailfrom[rp+1]);
678                                 }
679
680                                 /* Prefer brokketized names */
681                                 lp = (-1);
682                                 rp = (-1);
683                                 for (i=0;
684                                      !IsEmptyStr(Msg->mailfrom + i);
685                                      ++i)
686                                 {
687                                         if (Msg->mailfrom[i] == '<') lp = i;
688                                         if (Msg->mailfrom[i] == '>') rp = i;
689                                 }
690                                 if ( (lp>=0) && (rp>lp) ) {
691                                         Msg->mailfrom[rp] = 0;
692                                         memmove(Msg->mailfrom,
693                                                 &Msg->mailfrom[lp + 1],
694                                                 rp - lp);
695                                 }
696
697                                 scan_done = 1;
698                         }
699                 } while (scan_done == 0);
700                 if (IsEmptyStr(Msg->mailfrom))
701                         strcpy(Msg->mailfrom, "someone@somewhere.org");
702
703                 stripallbut(Msg->mailfrom, '<', '>');
704                 Msg->envelope_from = Msg->mailfrom;
705         }
706
707         return 1;
708 }