bbdbc4684f95131d5e3fa992828ef35da34722ef
[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                         else if ((Msg->MultiLineBuf != NULL) &&
170                                  strstr(ChrPtr(Msg->MultiLineBuf), "LOGIN") != NULL)
171                         {
172                                 Msg->SendLogin = 1;
173                         }
174                 }
175         }
176         /* else we fall back to 'helo' */
177         return eSendReply;
178 }
179
180 eNextState STMPC_send_HELO(SmtpOutMsg *Msg)
181 {
182         AsyncIO *IO = &Msg->IO;
183         StrBufPrintf(Msg->IO.SendBuf.Buf,
184                      "HELO %s\r\n", config.c_fqdn);
185
186         SMTP_DBG_SEND();
187         return eReadMessage;
188 }
189
190 eNextState SMTPC_read_HELO_reply(SmtpOutMsg *Msg)
191 {
192         AsyncIO *IO = &Msg->IO;
193         SMTP_DBG_READ();
194
195         if (!SMTP_IS_STATE('2'))
196         {
197                 if (SMTP_IS_STATE('4'))
198                         SMTP_VERROR(4);
199                 else
200                         SMTP_VERROR(5);
201         }
202         if (Msg->pCurrRelay != NULL)
203         {
204                 if (strstr(ChrPtr(Msg->IO.IOBuf), "LOGIN") != NULL)
205                         Msg->SendLogin = 1;
206         }
207         if ((Msg->pCurrRelay == NULL) ||
208             (Msg->pCurrRelay->User == NULL))
209                 READ_NEXT_STATE(eFROM); /* Skip auth... */
210
211         return eSendReply;
212 }
213
214 eNextState SMTPC_send_auth(SmtpOutMsg *Msg)
215 {
216         AsyncIO *IO = &Msg->IO;
217         char buf[SIZ];
218         char encoded[1024];
219
220         if ((Msg->pCurrRelay == NULL) ||
221             (Msg->pCurrRelay->User == NULL))
222                 READ_NEXT_STATE(eFROM); /* Skip auth, shouldn't even come here!... */
223         else {
224                 /* Do an AUTH command if necessary */
225                 if (Msg->SendLogin)
226                 {
227                         StrBufPlain(Msg->IO.SendBuf.Buf,
228                                     HKEY("AUTH LOGIN\r\n"));
229                 }
230                 else
231                 {
232                         sprintf(buf, "%s%c%s%c%s",
233                                 Msg->pCurrRelay->User, '\0',
234                                 Msg->pCurrRelay->User, '\0',
235                                 Msg->pCurrRelay->Pass);
236                         
237                         CtdlEncodeBase64(encoded, buf,
238                                          strlen(Msg->pCurrRelay->User) * 2 +
239                                          strlen(Msg->pCurrRelay->Pass) + 2, 0);
240                         
241                         StrBufPrintf(Msg->IO.SendBuf.Buf,
242                                      "AUTH PLAIN %s\r\n",
243                                      encoded);
244                 }
245         }
246         SMTP_DBG_SEND();
247         return eReadMessage;
248 }
249
250
251 eNextState SMTPC_read_auth_reply(SmtpOutMsg *Msg)
252 {
253         AsyncIO *IO = &Msg->IO;
254         /* Do an AUTH command if necessary */
255
256         SMTP_DBG_READ();
257
258         if (Msg->SendLogin)
259         {
260                 if (!SMTP_IS_STATE('3'))
261                         SMTP_VERROR(5);
262         }
263         else
264         {
265                 if (!SMTP_IS_STATE('2')) {
266                         if (SMTP_IS_STATE('4'))
267                                 SMTP_VERROR(4);
268                         else
269                                 SMTP_VERROR(5);
270                 }
271                 READ_NEXT_STATE(eFROM);
272         }
273         return eSendReply;
274 }
275
276
277 eNextState SMTPC_send_authplain_1(SmtpOutMsg *Msg)
278 {
279         AsyncIO *IO = &Msg->IO;
280         char buf[SIZ];
281         char encoded[1024];
282         long encodedlen;
283
284         sprintf(buf, "%s",
285                 Msg->pCurrRelay->User);
286         
287         encodedlen = CtdlEncodeBase64(
288                 encoded,
289                 Msg->pCurrRelay->User,
290                 strlen(Msg->pCurrRelay->User),
291                 0);
292
293         StrBufPlain(Msg->IO.SendBuf.Buf,
294                     encoded,
295                     encodedlen);
296
297         StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
298                              HKEY("\r\n"), 0);
299
300         SMTP_DBG_SEND();
301
302         return eReadMessage;
303 }
304 eNextState SMTPC_read_auth_plain_reply_1(SmtpOutMsg *Msg)
305 {
306         AsyncIO *IO = &Msg->IO;
307         /* Do an AUTH command if necessary */
308
309         SMTP_DBG_READ();
310
311         if (!SMTP_IS_STATE('3'))
312                 SMTP_VERROR(5);
313         return eSendReply;
314 }
315
316
317 eNextState SMTPC_send_authplain_2(SmtpOutMsg *Msg)
318 {
319         AsyncIO *IO = &Msg->IO;
320         char buf[SIZ];
321         char encoded[1024];
322         long encodedlen;
323
324         sprintf(buf, "%s",
325                 Msg->pCurrRelay->Pass);
326         
327         encodedlen = CtdlEncodeBase64(
328                 encoded,
329                 Msg->pCurrRelay->Pass,
330                 strlen(Msg->pCurrRelay->Pass),
331                 0);
332
333         StrBufPlain(Msg->IO.SendBuf.Buf,
334                     encoded,
335                     encodedlen);
336
337         StrBufAppendBufPlain(Msg->IO.SendBuf.Buf,
338                              HKEY("\r\n"), 0);
339
340         SMTP_DBG_SEND();
341
342         return eReadMessage;
343 }
344 eNextState SMTPC_read_auth_plain_reply_2(SmtpOutMsg *Msg)
345 {
346         AsyncIO *IO = &Msg->IO;
347         /* Do an AUTH command if necessary */
348
349         SMTP_DBG_READ();
350
351         if (!SMTP_IS_STATE('2')) {
352                 if (SMTP_IS_STATE('4'))
353                         SMTP_VERROR(4);
354                 else
355                         SMTP_VERROR(5);
356         }
357         return eSendReply;
358 }
359
360 eNextState SMTPC_send_FROM(SmtpOutMsg *Msg)
361 {
362         AsyncIO *IO = &Msg->IO;
363         /* previous command succeeded, now try the MAIL FROM: command */
364         StrBufPrintf(Msg->IO.SendBuf.Buf,
365                      "MAIL FROM:<%s>\r\n",
366                      Msg->envelope_from);
367
368         SMTP_DBG_SEND();
369         return eReadMessage;
370 }
371
372 eNextState SMTPC_read_FROM_reply(SmtpOutMsg *Msg)
373 {
374         AsyncIO *IO = &Msg->IO;
375         SMTP_DBG_READ();
376
377         if (!SMTP_IS_STATE('2')) {
378                 if (SMTP_IS_STATE('4'))
379                         SMTP_VERROR(4);
380                 else
381                         SMTP_VERROR(5);
382         }
383         return eSendReply;
384 }
385
386
387 eNextState SMTPC_send_RCPT(SmtpOutMsg *Msg)
388 {
389         AsyncIO *IO = &Msg->IO;
390         /* MAIL succeeded, now try the RCPT To: command */
391         StrBufPrintf(Msg->IO.SendBuf.Buf,
392                      "RCPT TO:<%s@%s>\r\n",
393                      Msg->user,
394                      Msg->node);
395
396         SMTP_DBG_SEND();
397         return eReadMessage;
398 }
399
400 eNextState SMTPC_read_RCPT_reply(SmtpOutMsg *Msg)
401 {
402         AsyncIO *IO = &Msg->IO;
403         SMTP_DBG_READ();
404
405         if (!SMTP_IS_STATE('2')) {
406                 if (SMTP_IS_STATE('4'))
407                         SMTP_VERROR(4);
408                 else
409                         SMTP_VERROR(5);
410         }
411         return eSendReply;
412 }
413
414 eNextState SMTPC_send_DATAcmd(SmtpOutMsg *Msg)
415 {
416         AsyncIO *IO = &Msg->IO;
417         /* RCPT succeeded, now try the DATA command */
418         StrBufPlain(Msg->IO.SendBuf.Buf,
419                     HKEY("DATA\r\n"));
420
421         SMTP_DBG_SEND();
422         return eReadMessage;
423 }
424
425 eNextState SMTPC_read_DATAcmd_reply(SmtpOutMsg *Msg)
426 {
427         AsyncIO *IO = &Msg->IO;
428         SMTP_DBG_READ();
429
430         if (!SMTP_IS_STATE('3')) {
431                 SetSMTPState(IO, eSTMPfailOne);
432                 if (SMTP_IS_STATE('4'))
433                         SMTP_VERROR(3);
434                 else
435                         SMTP_VERROR(5);
436         }
437         SetSMTPState(IO, eSTMPsmtpdata);
438         return eSendReply;
439 }
440
441 eNextState SMTPC_send_data_body(SmtpOutMsg *Msg)
442 {
443         StrBuf *Buf;
444         /* If we reach this point, the server is expecting data.*/
445
446         Buf = Msg->IO.SendBuf.Buf;
447         Msg->IO.SendBuf.Buf = Msg->msgtext;
448         Msg->msgtext = Buf;
449         /* 
450          * sending the message itself doesn't use this state machine.
451          * so we have to operate it here by ourselves.
452          */
453         Msg->State ++;
454
455         return eSendMore;
456 }
457
458 eNextState SMTPC_send_terminate_data_body(SmtpOutMsg *Msg)
459 {
460         StrBuf *Buf;
461
462         Buf = Msg->IO.SendBuf.Buf;
463         Msg->IO.SendBuf.Buf = Msg->msgtext;
464         Msg->msgtext = Buf;
465
466         StrBufPlain(Msg->IO.SendBuf.Buf,
467                     HKEY(".\r\n"));
468
469         return eReadMessage;
470
471 }
472
473 eNextState SMTPC_read_data_body_reply(SmtpOutMsg *Msg)
474 {
475         AsyncIO *IO = &Msg->IO;
476         SMTP_DBG_READ();
477
478         if (!SMTP_IS_STATE('2')) {
479                 if (SMTP_IS_STATE('4'))
480                         SMTP_VERROR(4);
481                 else
482                         SMTP_VERROR(5);
483         }
484
485         SetSMTPState(IO, eSTMPsmtpdone);
486         /* We did it! */
487         StrBufPlain(Msg->MyQEntry->StatusMessage,
488                     &ChrPtr(Msg->IO.RecvBuf.Buf)[4],
489                     StrLength(Msg->IO.RecvBuf.Buf) - 4);
490         StrBufTrim(Msg->MyQEntry->StatusMessage);
491         Msg->MyQEntry->Status = 2;
492         return eSendReply;
493 }
494
495 eNextState SMTPC_send_QUIT(SmtpOutMsg *Msg)
496 {
497         AsyncIO *IO = &Msg->IO;
498         StrBufPlain(Msg->IO.SendBuf.Buf,
499                     HKEY("QUIT\r\n"));
500
501         SMTP_DBG_SEND();
502         return eReadMessage;
503 }
504
505 eNextState SMTPC_read_QUIT_reply(SmtpOutMsg *Msg)
506 {
507         AsyncIO *IO = &Msg->IO;
508         SMTP_DBG_READ();
509
510         EVS_syslog(LOG_DEBUG,
511                    "delivery to <%s> @ <%s> (%s) succeeded\n",
512                    Msg->user,
513                    Msg->node,
514                    Msg->name);
515
516         return eTerminateConnection;
517 }
518
519 eNextState SMTPC_read_dummy(SmtpOutMsg *Msg)
520 {
521         return eSendReply;
522 }
523
524 eNextState SMTPC_send_dummy(SmtpOutMsg *Msg)
525 {
526         return eReadMessage;
527 }
528
529 /*****************************************************************************/
530 /*                     SMTP CLIENT DISPATCHER                                */
531 /*****************************************************************************/
532 SMTPReadHandler ReadHandlers[eMaxSMTPC] = {
533         SMTPC_read_greeting,
534         SMTPC_read_EHLO_reply,
535         SMTPC_read_HELO_reply,
536         SMTPC_read_auth_reply,
537         SMTPC_read_auth_plain_reply_1,
538         SMTPC_read_auth_plain_reply_2,
539         SMTPC_read_FROM_reply,
540         SMTPC_read_RCPT_reply,
541         SMTPC_read_DATAcmd_reply,
542         SMTPC_read_dummy,
543         SMTPC_read_data_body_reply,
544         SMTPC_read_QUIT_reply
545 };
546 SMTPSendHandler SendHandlers[eMaxSMTPC] = {
547         SMTPC_send_dummy, /* we don't send a greeting, the server does... */
548         SMTPC_send_EHLO,
549         STMPC_send_HELO,
550         SMTPC_send_auth,
551         SMTPC_send_authplain_1,
552         SMTPC_send_authplain_2,
553         SMTPC_send_FROM,
554         SMTPC_send_RCPT,
555         SMTPC_send_DATAcmd,
556         SMTPC_send_data_body,
557         SMTPC_send_terminate_data_body,
558         SMTPC_send_QUIT
559 };
560
561 const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
562
563 const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
564         300., /* Greeting... */
565         30., /* EHLO */
566         30., /* HELO */
567         30., /* Auth */
568         30., /* Auth */
569         30., /* Auth */
570         30., /* From */
571         90., /* RCPT */
572         30., /* DATA */
573         90., /* DATABody */
574         90., /* end of body... */
575         30.  /* QUIT */
576 };
577 const double SMTP_C_SendTimeouts[eMaxSMTPC] = {
578         90., /* Greeting... */
579         30., /* EHLO */
580         30., /* HELO */
581         30., /* Auth */
582         30., /* Auth */
583         30., /* Auth */
584         30., /* From */
585         30., /* RCPT */
586         30., /* DATA */
587         90., /* DATABody */
588         900., /* end of body... */
589         30.  /* QUIT */
590 };
591
592 const ConstStr ReadErrors[eMaxSMTPC + 1] = {
593         {HKEY("Connection broken during SMTP conversation")},
594         {HKEY("Connection broken during SMTP EHLO")},
595         {HKEY("Connection broken during SMTP HELO")},
596         {HKEY("Connection broken during SMTP AUTH")},
597         {HKEY("Connection broken during SMTP AUTH PLAIN I")},
598         {HKEY("Connection broken during SMTP AUTH PLAIN II")},
599         {HKEY("Connection broken during SMTP MAIL FROM")},
600         {HKEY("Connection broken during SMTP RCPT")},
601         {HKEY("Connection broken during SMTP DATA")},
602         {HKEY("Connection broken during SMTP message transmit")},
603         {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
604         {HKEY("Connection broken during SMTP message transmit")},/* quit reply, don't care. */
605         {HKEY("")}/* quit reply, don't care. */
606 };
607
608
609
610
611
612 int smtp_resolve_recipients(SmtpOutMsg *Msg)
613 {
614         AsyncIO *IO = &Msg->IO;
615         const char *ptr;
616         char buf[1024];
617         int scan_done;
618         int lp, rp;
619         int i;
620
621         EVNCS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
622
623         if ((Msg==NULL) ||
624             (Msg->MyQEntry == NULL) ||
625             (StrLength(Msg->MyQEntry->Recipient) == 0)) {
626                 return 0;
627         }
628
629         /* Parse out the host portion of the recipient address */
630         process_rfc822_addr(ChrPtr(Msg->MyQEntry->Recipient),
631                             Msg->user,
632                             Msg->node,
633                             Msg->name);
634
635         EVNCS_syslog(LOG_DEBUG,
636                      "Attempting delivery to <%s> @ <%s> (%s)\n",
637                      Msg->user,
638                      Msg->node,
639                      Msg->name);
640
641         /* If no envelope_from is supplied, extract one from the message */
642         Msg->envelope_from = ChrPtr(Msg->MyQItem->EnvelopeFrom);
643         if ( (Msg->envelope_from == NULL) ||
644              (IsEmptyStr(Msg->envelope_from)) ) {
645                 Msg->mailfrom[0] = '\0';
646                 scan_done = 0;
647                 ptr = ChrPtr(Msg->msgtext);
648                 do {
649                         if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0)
650                         {
651                                 scan_done = 1;
652                         }
653                         if (!strncasecmp(buf, "From:", 5))
654                         {
655                                 safestrncpy(Msg->mailfrom,
656                                             &buf[5],
657                                             sizeof Msg->mailfrom);
658
659                                 striplt(Msg->mailfrom);
660                                 for (i=0; Msg->mailfrom[i]; ++i) {
661                                         if (!isprint(Msg->mailfrom[i]))
662                                         {
663                                                 strcpy(&Msg->mailfrom[i],
664                                                        &Msg->mailfrom[i+1]);
665                                                 i=0;
666                                         }
667                                 }
668
669                                 /* Strip out parenthesized names */
670                                 lp = (-1);
671                                 rp = (-1);
672                                 for (i=0;
673                                      !IsEmptyStr(Msg->mailfrom + i);
674                                      ++i)
675                                 {
676                                         if (Msg->mailfrom[i] == '(') lp = i;
677                                         if (Msg->mailfrom[i] == ')') rp = i;
678                                 }
679                                 if ((lp>0)&&(rp>lp))
680                                 {
681                                         strcpy(&Msg->mailfrom[lp-1],
682                                                &Msg->mailfrom[rp+1]);
683                                 }
684
685                                 /* Prefer brokketized names */
686                                 lp = (-1);
687                                 rp = (-1);
688                                 for (i=0;
689                                      !IsEmptyStr(Msg->mailfrom + i);
690                                      ++i)
691                                 {
692                                         if (Msg->mailfrom[i] == '<') lp = i;
693                                         if (Msg->mailfrom[i] == '>') rp = i;
694                                 }
695                                 if ( (lp>=0) && (rp>lp) ) {
696                                         Msg->mailfrom[rp] = 0;
697                                         memmove(Msg->mailfrom,
698                                                 &Msg->mailfrom[lp + 1],
699                                                 rp - lp);
700                                 }
701
702                                 scan_done = 1;
703                         }
704                 } while (scan_done == 0);
705                 if (IsEmptyStr(Msg->mailfrom))
706                         strcpy(Msg->mailfrom, "someone@somewhere.org");
707
708                 stripallbut(Msg->mailfrom, '<', '>');
709                 Msg->envelope_from = Msg->mailfrom;
710         }
711
712         return 1;
713 }