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