]> Pileus Git - ~andy/fetchmail/blob - driver.c
Another step towards cleaner code.
[~andy/fetchmail] / driver.c
1 /*
2  * driver.c -- generic driver for mail fetch method protocols
3  *
4  * Copyright 1996 by Eric S. Raymond
5  * All rights reserved.
6  * For license terms, see the file COPYING in this directory.
7  */
8
9 #include  <config.h>
10 #include  <stdio.h>
11 #include  <setjmp.h>
12 #include  <errno.h>
13 #include  <ctype.h>
14 #include  <string.h>
15 #if defined(STDC_HEADERS)
16 #include  <stdlib.h>
17 #endif
18 #if defined(HAVE_UNISTD_H)
19 #include <unistd.h>
20 #endif
21 #if defined(HAVE_STDARG_H)
22 #include  <stdarg.h>
23 #else
24 #include  <varargs.h>
25 #endif
26 #if defined(HAVE_ALLOCA_H)
27 #include <alloca.h>
28 #endif
29 #if defined(HAVE_SYS_ITIMER_H)
30 #include <sys/itimer.h>
31 #endif
32 #include  <sys/time.h>
33 #include  <signal.h>
34
35 #ifdef HAVE_GETHOSTBYNAME
36 #include <netdb.h>
37 #include "mx.h"
38 #endif /* HAVE_GETHOSTBYNAME */
39
40 #ifdef SUNOS
41 #include <memory.h>
42 #endif
43
44 #ifdef KERBEROS_V4
45 #include <krb.h>
46 #include <des.h>
47 #include <netinet/in.h>
48 #include <netdb.h>
49 #endif /* KERBEROS_V4 */
50 #include  "socket.h"
51 #include  "fetchmail.h"
52 #include  "socket.h"
53 #include  "smtp.h"
54
55 /* BSD portability hack...I know, this is an ugly place to put it */
56 #if !defined(SIGCHLD) && defined(SIGCLD)
57 #define SIGCHLD SIGCLD
58 #endif
59
60 #define SMTP_PORT       25      /* standard SMTP service port */
61
62 extern char *strstr();  /* needed on sysV68 R3V7.1. */
63
64 int fetchlimit;         /* how often to tear down the server connection */
65 int batchcount;         /* count of messages sent in current batch */
66 int peek_capable;       /* can we peek for better error recovery? */
67
68 static const struct method *protocol;
69 static jmp_buf  restart;
70
71 char tag[TAGLEN];
72 static int tagnum;
73 #define GENSYM  (sprintf(tag, "a%04d", ++tagnum), tag)
74
75 static char *shroud;    /* string to shroud in debug output, if  non-NULL */
76 static int mytimeout;   /* value of nonreponse timeout */
77
78 static void set_timeout(int timeleft)
79 /* reset the nonresponse-timeout */
80 {
81     struct itimerval ntimeout;
82
83     ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
84     ntimeout.it_value.tv_sec  = timeleft;
85     ntimeout.it_value.tv_usec = 0;
86     setitimer(ITIMER_REAL, &ntimeout, (struct itimerval *)NULL);
87 }
88
89 static void timeout_handler (int signal)
90 /* handle server-timeout SIGALRM signal */
91 {
92     longjmp(restart, 1);
93 }
94
95 #define XMIT_ACCEPT             1
96 #define XMIT_REJECT             2
97 #define XMIT_ANTISPAM           3       
98 static int accept_count, reject_count;
99
100 #ifdef HAVE_RES_SEARCH
101 #define MX_RETRIES      3
102
103 static int is_host_alias(const char *name, struct query *ctl)
104 /* determine whether name is a DNS alias of the hostname */
105 {
106     struct hostent      *he;
107     struct mxentry      *mxp, *mxrecords;
108
109     /*
110      * The first two checks are optimizations that will catch a good
111      * many cases.  (1) check against the hostname the user
112      * specified.  Odds are good this will either be the mailserver's
113      * FQDN or a suffix of it with the mailserver's domain's default
114      * host name omitted.  Then check the rest of the `also known as'
115      * cache accumulated by previous DNS checks.  This cache is primed
116      * by the aka list option.
117      *
118      * (2) check against the mailserver's FQDN, in case
119      * it's not the same as the declared hostname.
120      *
121      * Either of these on a mail address is definitive.  Only if the
122      * name doesn't match either is it time to call the bind library.
123      * If this happens odds are good we're looking at an MX name.
124      */
125     if (str_in_list(&ctl->server.lead_server->names, name))
126         return(TRUE);
127     else if (strcmp(name, ctl->server.canonical_name) == 0)
128         return(TRUE);
129     else if (!ctl->server.dns)
130         return(FALSE);
131
132     /*
133      * We know DNS service was up at the beginning of this poll cycle.
134      * If it's down, our nameserver has crashed.  We don't want to try
135      * delivering the current message or anything else from this
136      * mailbox until it's back up.
137      */
138     else if ((he = gethostbyname(name)) != (struct hostent *)NULL)
139     {
140         if (strcmp(ctl->server.canonical_name, he->h_name) == 0)
141             goto match;
142         else
143             return(FALSE);
144     }
145     else
146         switch (h_errno)
147         {
148         case HOST_NOT_FOUND:    /* specified host is unknown */
149         case NO_ADDRESS:        /* valid, but does not have an IP address */
150             break;
151
152         case NO_RECOVERY:       /* non-recoverable name server error */
153         case TRY_AGAIN:         /* temporary error on authoritative server */
154         default:
155             if (outlevel != O_SILENT)
156                 putchar('\n');  /* terminate the progress message */
157             error(0, 0,
158                 "nameserver failure while looking for `%s' during poll of %s.",
159                 name, ctl->server.names->id);
160             ctl->errcount++;
161             break;
162         }
163
164     /*
165      * We're only here if DNS was OK but the gethostbyname() failed
166      * with a HOST_NOT_FOUND or NO_ADDRESS error.
167      * Search for a name match on MX records pointing to the server.
168      */
169     h_errno = 0;
170     if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
171     {
172         switch (h_errno)
173         {
174         case HOST_NOT_FOUND:    /* specified host is unknown */
175         case NO_ADDRESS:        /* valid, but does not have an IP address */
176             return(FALSE);
177             break;
178
179         case NO_RECOVERY:       /* non-recoverable name server error */
180         case TRY_AGAIN:         /* temporary error on authoritative server */
181         default:
182             error(0, 0,
183                 "nameserver failure while looking for `%s' during poll of %s.",
184                 name, ctl->server.names->id);
185             ctl->errcount++;
186             break;
187         }
188     }
189     else
190     {
191         for (mxp = mxrecords; mxp->name; mxp++)
192             if (strcmp(ctl->server.canonical_name, mxp->name) == 0)
193                 goto match;
194         return(FALSE);
195     match:;
196     }
197
198     /* add this name to relevant server's `also known as' list */
199     save_str(&ctl->server.lead_server->names, -1, name);
200     return(TRUE);
201 }
202
203 static void map_name(name, ctl, xmit_names)
204 /* add given name to xmit_names if it matches declared localnames */
205 const char *name;               /* name to map */
206 struct query *ctl;              /* list of permissible aliases */
207 struct idlist **xmit_names;     /* list of recipient names parsed out */
208 {
209     const char  *lname;
210
211     lname = idpair_find(&ctl->localnames, name);
212     if (!lname && ctl->wildcard)
213         lname = name;
214
215     if (lname != (char *)NULL)
216     {
217         if (outlevel == O_VERBOSE)
218             error(0, 0, "mapped %s to local %s", name, lname);
219         save_str(xmit_names, XMIT_ACCEPT, lname);
220         accept_count++;
221     }
222 }
223
224 void find_server_names(hdr, ctl, xmit_names)
225 /* parse names out of a RFC822 header into an ID list */
226 const char *hdr;                /* RFC822 header in question */
227 struct query *ctl;              /* list of permissible aliases */
228 struct idlist **xmit_names;     /* list of recipient names parsed out */
229 {
230     if (hdr == (char *)NULL)
231         return;
232     else
233     {
234         char    *cp;
235
236         if ((cp = nxtaddr(hdr)) != (char *)NULL)
237             do {
238                 char    *atsign;
239
240                 if ((atsign = strchr(cp, '@')))
241                 {
242                     struct idlist       *idp;
243
244                     /*
245                      * Does a trailing segment of the hostname match something
246                      * on the localdomains list?  If so, save the whole name
247                      * and keep going.
248                      */
249                     for (idp = ctl->server.localdomains; idp; idp = idp->next)
250                     {
251                         char    *rhs;
252
253                         rhs = atsign + (strlen(atsign) - strlen(idp->id));
254                         if ((rhs[-1] == '.' || rhs[-1] == '@')
255                                         && strcasecmp(rhs, idp->id) == 0)
256                         {
257                             if (outlevel == O_VERBOSE)
258                                 error(0, 0, "passed through %s matching %s", 
259                                       cp, idp->id);
260                             save_str(xmit_names, XMIT_ACCEPT, cp);
261                             accept_count++;
262                             continue;
263                         }
264                     }
265
266                     /*
267                      * Check to see if the right-hand part is an alias
268                      * or MX equivalent of the mailserver.  If it's
269                      * not, skip this name.  If it is, we'll keep
270                      * going and try to find a mapping to a client name.
271                      */
272                     if (!is_host_alias(atsign+1, ctl))
273                     {
274                         save_str(xmit_names, XMIT_REJECT, cp);
275                         reject_count++;
276                         continue;
277                     }
278                     atsign[0] = '\0';
279                 }
280
281                 map_name(cp, ctl, xmit_names);
282             } while
283                 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
284     }
285 }
286
287 char *parse_received(struct query *ctl, char *bufp)
288 /* try to extract real addressee from the Received line */
289 {
290     char *ok;
291     static char rbuf[HOSTLEN + USERNAMELEN + 4]; 
292
293     /*
294      * Try to extract the real envelope addressee.  We look here
295      * specifically for the mailserver's Received line.
296      * Note: this will only work for sendmail, or an MTA that
297      * shares sendmail's convention for embedding the envelope
298      * address in the Received line.  Sendmail itself only
299      * does this when the mail has a single recipient.
300      */
301     if ((ok = strstr(bufp, "by ")) == (char *)NULL)
302         ok = (char *)NULL;
303     else
304     {
305         char    *sp, *tp;
306
307         /* extract space-delimited token after "by " */
308         for (sp = ok + 3; isspace(*sp); sp++)
309             continue;
310         tp = rbuf;
311         for (; !isspace(*sp); sp++)
312             *tp++ = *sp;
313         *tp = '\0';
314
315         /*
316          * If it's a DNS name of the mail server, look for the
317          * recipient name after a following "for".  Otherwise
318          * punt.
319          */
320         if (is_host_alias(rbuf, ctl))
321             ok = strstr(sp, "for ");
322         else
323             ok = (char *)NULL;
324     }
325
326     if (ok != 0)
327     {
328         char    *sp, *tp;
329
330         tp = rbuf;
331         sp = ok + 4;
332         if (*sp == '<')
333             sp++;
334         while (*sp && *sp != '>' && *sp != '@' && *sp != ';')
335             if (!isspace(*sp))
336                 *tp++ = *sp++;
337             else
338             {
339                 /* uh oh -- whitespace here can't be right! */
340                 ok = (char *)NULL;
341                 break;
342             }
343         *tp = '\0';
344     }
345
346     if (!ok)
347         return(NULL);
348     else
349     {
350         if (outlevel == O_VERBOSE)
351             error(0, 0, "found Received address `%s'", rbuf);
352         return(rbuf);
353     }
354 }
355 #endif /* HAVE_RES_SEARCH */
356
357 int smtp_open(struct query *ctl)
358 /* try to open a socket to the appropriate SMTP server for this query */ 
359 {
360     struct idlist       *idp;
361
362     /* maybe it's time to close the socket in order to force delivery */
363     if (ctl->batchlimit && (ctl->smtp_socket != -1) && batchcount++ == ctl->batchlimit)
364     {
365         close(ctl->smtp_socket);
366         ctl->smtp_socket = -1;
367         batchcount = 0;
368     }
369
370     /* run down the SMTP hunt list looking for a server that's up */
371     for (idp = ctl->smtphunt; idp; idp = idp->next)
372     {
373         /* 
374          * RFC 1123 requires that the domain name in HELO address is a
375          * "valid principal domain name" for the client host.  We
376          * violate this with malice aforethought in order to make the
377          * Received headers and logging look right.
378          *
379          * In fact this code relies on the RFC1123 requirement that the
380          * SMTP listener must accept messages even if verification of the
381          * HELO name fails (RFC1123 section 5.2.5, paragraph 2).
382          */
383
384         /* if no socket to this host is already set up, try to open ESMTP */
385         if (ctl->smtp_socket == -1)
386         {
387             if ((ctl->smtp_socket = SockOpen(idp->id,SMTP_PORT)) == -1)
388                 continue;
389             else if (SMTP_ok(ctl->smtp_socket) != SM_OK
390                      || SMTP_ehlo(ctl->smtp_socket, 
391                                   ctl->server.names->id,
392                                   &ctl->server.esmtp_options) != SM_OK)
393             {
394                 /*
395                  * RFC 1869 warns that some listeners hang up on a failed EHLO,
396                  * so it's safest not to assume the socket will still be good.
397                  */
398                 close(ctl->smtp_socket);
399                 ctl->smtp_socket = -1;
400             }
401             else
402             {
403                 ctl->smtphost = idp->id;
404                 break;
405             }
406         }
407
408         /* if opening for ESMTP failed, try SMTP */
409         if (ctl->smtp_socket == -1)
410         {
411             if ((ctl->smtp_socket = SockOpen(idp->id,SMTP_PORT)) == -1)
412                 continue;
413             else if (SMTP_ok(ctl->smtp_socket) != SM_OK
414                      || SMTP_helo(ctl->smtp_socket, ctl->server.names->id) != SM_OK)
415             {
416                 close(ctl->smtp_socket);
417                 ctl->smtp_socket = -1;
418             }
419             else
420             {
421                 ctl->smtphost = idp->id;
422                 break;
423             }
424         }
425     }
426
427     return(ctl->smtp_socket);
428 }
429
430 static int gen_readmsg(sock, len, delimited, num, ctl, realname)
431 /* read message content and ship to SMTP or MDA */
432 int sock;               /* to which the server is connected */
433 long len;               /* length of message */
434 int delimited;          /* does the protocol use a message delimiter? */
435 int num;                /* message number we're fetching */
436 struct query *ctl;      /* query control record */
437 char *realname;         /* real name of host */
438 {
439     char buf[MSGBUFSIZE+1], return_path[MSGBUFSIZE+1]; 
440     int from_offs, to_offs, cc_offs, bcc_offs, ctt_offs, env_offs;
441     char *headers, *received_for;
442     int n, linelen, oldlen, ch, sizeticker, delete_ok, forward_ok, remaining;
443     FILE *sinkfp;
444     RETSIGTYPE (*sigchld)();
445     char                *cp;
446     struct idlist       *idp, *xmit_names;
447     int                 good_addresses, bad_addresses, has_nuls;
448 #ifdef HAVE_RES_SEARCH
449     int                 no_local_matches = FALSE;
450 #endif /* HAVE_RES_SEARCH */
451     int                 olderrs;
452
453     sizeticker = 0;
454     forward_ok = TRUE;
455     delete_ok = TRUE;
456     has_nuls = FALSE;
457     return_path[0] = '\0';
458     remaining = len;
459     olderrs = ctl->errcount;
460
461     /* read message headers */
462     headers = received_for = NULL;
463     from_offs = to_offs = cc_offs = bcc_offs = ctt_offs = env_offs = -1;
464     oldlen = 0;
465     for (;;)
466     {
467         char *line;
468
469         line = xmalloc(sizeof(buf));
470         linelen = 0;
471         line[0] = '\0';
472         do {
473             if ((n = SockRead(sock, buf, sizeof(buf)-1)) == -1)
474                 return(PS_SOCKET);
475             linelen += n;
476
477             /* lines may not be properly CRLF terminated; fix this for qmail */
478             if (ctl->forcecr)
479             {
480                 cp = buf + strlen(buf) - 1;
481                 if (*cp == '\n' && (cp == buf || cp[-1] != '\r'))
482                 {
483                     *cp++ = '\r';
484                     *cp++ = '\n';
485                     *cp++ = '\0';
486                 }
487             }
488
489             set_timeout(ctl->server.timeout);
490             /* leave extra room for reply_hack to play with */
491             line = (char *) realloc(line, strlen(line) + strlen(buf) + HOSTLEN + 1);
492             strcat(line, buf);
493             if (line[0] == '\r' && line[1] == '\n')
494                 break;
495         } while
496             /* we may need to grab RFC822 continuations */
497             ((ch = SockPeek(sock)) == ' ' || ch == '\t');
498
499         /* write the message size dots */
500         if ((outlevel > O_SILENT && outlevel < O_VERBOSE) && linelen > 0)
501         {
502             sizeticker += linelen;
503             while (sizeticker >= SIZETICKER)
504             {
505                 error_build(".");
506                 sizeticker -= SIZETICKER;
507             }
508         }
509
510         if (linelen != strlen(line))
511             has_nuls = TRUE;
512
513         remaining -= linelen;
514
515         /* check for end of headers; don't save terminating line */
516         if (line[0] == '\r' && line[1] == '\n')
517         {
518             free(line);
519             break;
520         }
521      
522         /*
523          * This code prevents fetchmail from becoming an accessory after
524          * the fact to upstream sendmails with the `E' option on.  This
525          * can result in an escaped Unix From_ line at the beginning of
526          * the headers.  If fetchmail just passes it through, the client
527          * listener may think the message has *no* headers (since the first)
528          * line it sees doesn't look RFC822-conformant) and fake up a set.
529          *
530          * What the user would see in this case is bogus (synthesized)
531          * headers, followed by a blank line, followed by the >From, 
532          * followed by the real headers, followed by a blank line,
533          * followed by text.
534          *
535          * We forestall this lossage by tossing anything that looks
536          * like an escaped From_ line in headers.  These aren't RFC822
537          * so our conscience is clear...
538          */
539         if (!strncasecmp(line, ">From ", 6))
540         {
541             free(line);
542             continue;
543         }
544
545         /*
546          * OK, this is messy.  If we're forwarding by SMTP, it's the
547          * SMTP-receiver's job (according to RFC821, page 22, section
548          * 4.1.1) to generate a Return-Path line on final delivery.
549          * The trouble is, we've already got one because the
550          * mailserver's SMTP thought *it* was responsible for final
551          * delivery.
552          *
553          * Stash away the contents of Return-Path for use in generating
554          * MAIL FROM later on, then prevent the header from being saved
555          * with the others.  In effect, we strip it off here.
556          *
557          * If the SMTP server conforms to the standards, and fetchmail gets the
558          * envelope sender from the Return-Path, the new Return-Path should be
559          * exactly the same as the original one.
560          */
561         if (!ctl->mda && !strncasecmp("Return-Path:", line, 12))
562         {
563             strcpy(return_path, nxtaddr(line));
564             free(line);
565             continue;
566         }
567
568         if (ctl->rewrite)
569             reply_hack(line, realname);
570
571         if (!headers)
572         {
573             oldlen = strlen(line);
574             headers = xmalloc(oldlen + 1);
575             (void) strcpy(headers, line);
576             free(line);
577             line = headers;
578         }
579         else
580         {
581             int newlen;
582
583             newlen = oldlen + strlen(line);
584             headers = (char *) realloc(headers, newlen + 1);
585             if (headers == NULL)
586                 return(PS_IOERR);
587             strcpy(headers + oldlen, line);
588             free(line);
589             line = headers + oldlen;
590             oldlen = newlen;
591         }
592
593         if (from_offs == -1 && !strncasecmp("From:", line, 5))
594             from_offs = (line - headers);
595         else if (from_offs == -1 && !strncasecmp("Resent-From:", line, 12))
596             from_offs = (line - headers);
597         else if (from_offs == -1 && !strncasecmp("Apparently-From:", line, 16))
598             from_offs = (line - headers);
599
600         else if (!strncasecmp("To:", line, 3))
601             to_offs = (line - headers);
602
603         else if (ctl->server.envelope != STRING_DISABLED && env_offs == -1
604                  && !strncasecmp(ctl->server.envelope,
605                                                 line,
606                                                 strlen(ctl->server.envelope)))
607             env_offs = (line - headers);
608
609         else if (!strncasecmp("Cc:", line, 3))
610             cc_offs = (line - headers);
611
612         else if (!strncasecmp("Bcc:", line, 4))
613             bcc_offs = (line - headers);
614
615         else if (!strncasecmp("Content-Transfer-Encoding:", line, 26))
616             ctt_offs = (line - headers);
617
618 #ifdef HAVE_RES_SEARCH
619         else if (ctl->server.envelope != STRING_DISABLED && MULTIDROP(ctl) && !received_for && !strncasecmp("Received:", line, 9))
620             received_for = parse_received(ctl, line);
621 #endif /* HAVE_RES_SEARCH */
622     }
623
624     /*
625      * Hack time.  If the first line of the message was blank, with no headers
626      * (this happens occasionally due to bad gatewaying software) cons up
627      * a set of fake headers.  
628      *
629      * If you modify the fake header template below, be sure you don't
630      * make either From or To address @-less, otherwise the reply_hack
631      * logic will do bad things.
632      */
633     if (headers == (char *)NULL)
634     {
635         sprintf(buf, 
636         "From: <FETCHMAIL-DAEMON@%s>\r\nTo: %s@localhost\r\nSubject: Headerless mail from %s's mailbox on %s\r\n",
637                 fetchmailhost, user, ctl->remotename, realname);
638         headers = xstrdup(buf);
639     }
640
641     /*
642      * We can now process message headers before reading the text.
643      * In fact we have to, as this will tell us where to forward to.
644      */
645
646     /* cons up a list of local recipients */
647     xmit_names = (struct idlist *)NULL;
648     bad_addresses = good_addresses = accept_count = reject_count = 0;
649 #ifdef HAVE_RES_SEARCH
650     /* is this a multidrop box? */
651     if (MULTIDROP(ctl))
652     {
653         if (env_offs > -1)          /* We have the actual envelope addressee */
654             find_server_names(headers + env_offs, ctl, &xmit_names);
655         else if (received_for)
656             /*
657              * We have the Received for addressee.  
658              * It has to be a mailserver address, or we
659              * wouldn't have got here.
660              */
661             map_name(received_for, ctl, &xmit_names);
662         else
663         {
664             /*
665              * We haven't extracted the envelope address.
666              * So check all the header addresses.
667              */
668             if (to_offs > -1)
669                 find_server_names(headers + to_offs,  ctl, &xmit_names);
670             if (cc_offs > -1)
671                 find_server_names(headers + cc_offs,  ctl, &xmit_names);
672             if (bcc_offs > -1)
673                 find_server_names(headers + bcc_offs, ctl, &xmit_names);
674         }
675         if (!accept_count)
676         {
677             no_local_matches = TRUE;
678             save_str(&xmit_names, XMIT_ACCEPT, user);
679             if (outlevel == O_VERBOSE)
680                 error(0, 0, 
681                       "no local matches, forwarding to %s",
682                       user);
683         }
684     }
685     else        /* it's a single-drop box, use first localname */
686 #endif /* HAVE_RES_SEARCH */
687         save_str(&xmit_names, XMIT_ACCEPT, ctl->localnames->id);
688
689
690     /*
691      * Time to either address the message or decide we can't deliver it yet.
692      */
693     if (ctl->errcount > olderrs)        /* there were DNS errors above */
694     {
695         delete_ok = FALSE;
696         sinkfp = (FILE *)NULL;
697         if (outlevel == O_VERBOSE)
698             error(0,0, "forwarding and deletion suppressed due to DNS errors");
699     }
700     else if (ctl->mda)          /* we have a declared MDA */
701     {
702         int     length = 0;
703         char    *names, *cmd;
704
705         /*
706          * We go through this in order to be able to handle very
707          * long lists of users and (re)implement %s.
708          */
709         for (idp = xmit_names; idp; idp = idp->next)
710             if (idp->val.num == XMIT_ACCEPT)
711                 length += (strlen(idp->id) + 1);
712         names = (char *)alloca(length);
713         names[0] = '\0';
714         for (idp = xmit_names; idp; idp = idp->next)
715             if (idp->val.num == XMIT_ACCEPT)
716             {
717                 strcat(names, idp->id);
718                 strcat(names, " ");
719             }
720         cmd = (char *)alloca(strlen(ctl->mda) + length);
721         sprintf(cmd, ctl->mda, names);
722         if (outlevel == O_VERBOSE)
723             error(0, 0, "about to deliver with: %s", cmd);
724
725 #ifdef HAVE_SETEUID
726         /*
727          * Arrange to run with user's permissions if we're root.
728          * This will initialize the ownership of any files the
729          * MDA creates properly.  (The seteuid call is available
730          * under all BSDs and Linux)
731          */
732         seteuid(ctl->uid);
733 #endif /* HAVE_SETEUID */
734
735         sinkfp = popen(cmd, "w");
736
737 #ifdef HAVE_SETEUID
738         /* this will fail quietly if we didn't start as root */
739         seteuid(0);
740 #endif /* HAVE_SETEUID */
741
742         if (!sinkfp)
743         {
744             error(0, -1, "MDA open failed");
745             return(PS_IOERR);
746         }
747
748         sigchld = signal(SIGCHLD, SIG_DFL);
749     }
750     else
751     {
752         char    *ap, *ctt, options[MSGBUFSIZE];
753
754         /* build a connection to the SMTP listener */
755         if ((smtp_open(ctl) == -1))
756         {
757             free_str_list(&xmit_names);
758             error(0, -1, "SMTP connect to %s failed",
759                   ctl->smtphost ? ctl->smtphost : "localhost");
760             return(PS_SMTP);
761         }
762
763         /*
764          * Compute ESMTP options.  It's a kluge to use nxtaddr()
765          * here because the contents of the Content-Transfer-Encoding
766          * headers isn't semantically an address.  But it has the
767          * desired tokenizing effect.
768          */
769         options[0] = '\0';
770         if ((ctl->server.esmtp_options & ESMTP_8BITMIME)
771             && (ctt_offs >= 0)
772             && (ctt = nxtaddr(headers + ctt_offs)))
773             if (!strcasecmp(ctt,"7BIT"))
774                 sprintf(options, " BODY=7BIT");
775             else if (!strcasecmp(ctt,"8BIT"))
776                 sprintf(options, " BODY=8BITMIME");
777         if ((ctl->server.esmtp_options & ESMTP_SIZE) && !delimited)
778             sprintf(options + strlen(options), " SIZE=%ld", len);
779
780         /*
781          * If there is a Return-Path address on the message, this was
782          * almost certainly the MAIL FROM address given the originating
783          * sendmail.  This is the best thing to use for logging the
784          * message origin (it sets up the right behavior for bounces and
785          * mailing lists).  Otherwise, take the From address.
786          *
787          * Try to get the SMTP listener to take the Return-Path or
788          * From address as MAIL FROM .  If it won't, fall back on the
789          * calling-user ID.  This won't affect replies, which use the
790          * header From address anyway.
791          *
792          * RFC 1123 requires that the domain name part of the
793          * MAIL FROM address be "canonicalized", that is a
794          * FQDN or MX but not a CNAME.  We'll assume the From
795          * header is already in this form here (it certainly
796          * is if rewrite is on).  RFC 1123 is silent on whether
797          * a nonexistent hostname part is considered canonical.
798          *
799          * This is a potential problem if the MTAs further upstream
800          * didn't pass canonicalized From/Return-Path lines, *and* the
801          * local SMTP listener insists on them.
802          */
803         ap = (char *)NULL;
804         if (return_path[0])
805             ap = return_path;
806         else if (from_offs == -1 || !(ap = nxtaddr(headers + from_offs)))
807             ap = user;
808         if (SMTP_from(ctl->smtp_socket, ap, options) != SM_OK)
809         {
810             int smtperr = atoi(smtp_response);
811
812             if (smtperr >= 400)
813                 error(0, -1, "SMTP error: %s", smtp_response);
814
815             /*
816              * There's one problem with this flow of control;
817              * there's no way to avoid reading the whole message
818              * off the server, even if the MAIL FROM response 
819              * tells us that it's just to be discarded.  We could
820              * fix this under IMAP by reading headers first, then
821              * trying to issue the MAIL FROM, and *then* reading
822              * the body...but POP3 can't do this.
823              */
824
825             switch (smtperr)
826             {
827             case 571: /* unsolicited email refused */
828                 /*
829                  * SMTP listener explicitly refuses to deliver
830                  * mail coming from this address, probably due
831                  * to an anti-spam domain exclusion.  Respect
832                  * this.  Don't try to ship the message, and
833                  * don't prevent it from being deleted.
834                  */
835                 forward_ok = FALSE;
836                 goto skiptext;
837
838             case 452: /* insufficient system storage */
839                 /*
840                  * Temporary out-of-queue-space condition on the
841                  * ESMTP server.  Don't try to ship the message, 
842                  * and suppress deletion so it can be retried on
843                  * a future retrieval cycle.
844                  */
845                 forward_ok = FALSE;
846                 delete_ok = FALSE;
847                 SMTP_rset(ctl->smtp_socket);    /* required by RFC1870 */
848                 goto skiptext;
849
850             case 552: /* message exceeds fixed maximum message size */
851                 /*
852                  * Permanent no-go condition on the
853                  * ESMTP server.  Don't try to ship the message, 
854                  * and allow it to be deleted.
855                  */
856                 forward_ok = FALSE;
857                 SMTP_rset(ctl->smtp_socket);    /* required by RFC1870 */
858                 goto skiptext;
859
860             default:    /* retry with invoking user's address */
861                 if (SMTP_from(ctl->smtp_socket, user, options) != SM_OK)
862                 {
863                     error(0, -1, "SMTP error: %s", smtp_response);
864                     free(headers);
865                     return(PS_SMTP);    /* should never happen */
866                 }
867             }
868         }
869
870         /*
871          * Now list the recipient addressees
872          *
873          * RFC 1123 requires that the domain name part of the
874          * RCPT TO address be "canonicalized", that is a FQDN
875          * or MX but not a CNAME.  RFC1123 doesn't say whether
876          * the FQDN part can be null (as it frequently will be
877          * here), but it's hard to see how this could cause a
878          * problem.
879          */
880         for (idp = xmit_names; idp; idp = idp->next)
881             if (idp->val.num == XMIT_ACCEPT)
882                 if (SMTP_rcpt(ctl->smtp_socket, idp->id) == SM_OK)
883                     good_addresses++;
884                 else
885                 {
886                     bad_addresses++;
887                     idp->val.num = XMIT_ANTISPAM;
888                     error(0, 0, 
889                           "SMTP listener doesn't like recipient address `%s'", idp->id);
890                 }
891         if (!good_addresses && SMTP_rcpt(ctl->smtp_socket, user) != SM_OK)
892         {
893             error(0, 0, 
894                   "can't even send to calling user!");
895             free(headers);
896             return(PS_SMTP);
897         }
898
899         /* tell it we're ready to send data */
900         SMTP_data(ctl->smtp_socket);
901     }
902
903     /* we may need to strip carriage returns */
904     if (ctl->stripcr)
905     {
906         char    *sp, *tp;
907
908         for (sp = tp = headers; *sp; sp++)
909             if (*sp != '\r')
910                 *tp++ =  *sp;
911         *tp = '\0';
912
913     }
914
915     /* write all the headers */
916     n = 0;
917     if (ctl->mda && sinkfp)
918         n = fwrite(headers, 1, strlen(headers), sinkfp);
919     else if (ctl->smtp_socket != -1)
920         n = SockWrite(ctl->smtp_socket, headers, strlen(headers));
921     free(headers);
922
923     if (n < 0)
924     {
925         error(0, errno, "writing RFC822 headers");
926         if (ctl->mda)
927         {
928             pclose(sinkfp);
929             signal(SIGCHLD, sigchld);
930         }
931         return(PS_IOERR);
932     }
933     else if (outlevel == O_VERBOSE)
934         fputs("#", stderr);
935
936     /* write error notifications */
937 #ifdef HAVE_RES_SEARCH
938     if (no_local_matches || has_nuls || bad_addresses)
939 #else
940     if (has_nuls || bad_addresses)
941 #endif /* HAVE_RES_SEARCH */
942     {
943         int     errlen = 0;
944         char    errhd[USERNAMELEN + POPBUFSIZE], *errmsg;
945
946         errmsg = errhd;
947         (void) strcpy(errhd, "X-Fetchmail-Warning: ");
948 #ifdef HAVE_RES_SEARCH
949         if (no_local_matches)
950         {
951             if (reject_count != 1)
952                 strcat(errhd, "no recipient addresses matched declared local names");
953             else
954             {
955                 for (idp = xmit_names; idp; idp = idp->next)
956                     if (idp->val.num == XMIT_REJECT)
957                         break;
958                 sprintf(errhd+strlen(errhd), "recipient address %s didn't match any local name", idp->id);
959             }
960         }
961 #endif /* HAVE_RES_SEARCH */
962
963         if (has_nuls)
964         {
965             if (errhd[sizeof("X-Fetchmail-Warning: ")])
966                 strcat(errhd, "; ");
967             strcat(errhd, "message has embedded NULs");
968         }
969
970         if (bad_addresses)
971         {
972             if (errhd[sizeof("X-Fetchmail-Warning: ")])
973                 strcat(errhd, "; ");
974             strcat(errhd, "SMTP listener rejected local recipient addresses: ");
975             errlen = strlen(errhd);
976             for (idp = xmit_names; idp; idp = idp->next)
977                 if (idp->val.num == XMIT_ANTISPAM)
978                     errlen += strlen(idp->id) + 2;
979
980             errmsg = alloca(errlen+3);
981             (void) strcpy(errmsg, errhd);
982             for (idp = xmit_names; idp; idp = idp->next)
983                 if (idp->val.num == XMIT_ANTISPAM)
984                 {
985                     strcat(errmsg, idp->id);
986                     if (idp->next)
987                         strcat(errmsg, ", ");
988                 }
989
990         }
991
992         if (ctl->mda && !ctl->forcecr)
993             strcat(errmsg, "\n");
994         else
995             strcat(errmsg, "\r\n");
996
997         /* we may need to strip carriage returns */
998         if (ctl->stripcr)
999         {
1000             char        *sp, *tp;
1001
1002             for (sp = tp = errmsg; *sp; sp++)
1003                 if (*sp != '\r')
1004                     *tp++ =  *sp;
1005             *tp = '\0';
1006         }
1007
1008         /* ship out the error line */
1009         if (sinkfp)
1010         {
1011             if (ctl->mda)
1012                 fwrite(errmsg, sizeof(char), strlen(errmsg), sinkfp);
1013             else
1014                 SockWrite(ctl->smtp_socket, errmsg, strlen(errmsg));
1015         }
1016     }
1017
1018     free_str_list(&xmit_names);
1019
1020     /* issue the delimiter line */
1021     if (sinkfp && ctl->mda)
1022         fputc('\n', sinkfp);
1023     else if (ctl->smtp_socket != -1)
1024     {
1025         if (ctl->stripcr)
1026             SockWrite(ctl->smtp_socket, "\n", 1);
1027         else
1028             SockWrite(ctl->smtp_socket, "\r\n", 2);
1029     }
1030
1031     /*
1032      * If we're using IMAP4 or something else that can fetch headers
1033      * separately from bodies, it's time to grab the body now.  This
1034      * fetch may be skipped if we got an anti-spam or other error
1035      * response from SMTP above.
1036      *
1037      * The protocol methods should never fail here.  Just in case...
1038      * we return PS_TRANSIENT because failure could not be due 
1039      * to desynchronization or permanent request failure.
1040      */
1041     if (protocol->fetch_body)
1042     {
1043         int     ok;
1044
1045         if ((ok = (protocol->trail)(sock, ctl, num)))
1046             return(PS_TRANSIENT);
1047         if ((ok = (protocol->fetch_body)(sock, ctl, num, &remaining)))
1048             return(PS_TRANSIENT);
1049     }
1050
1051     /*
1052      *  Body processing starts here
1053      */
1054
1055 skiptext:
1056     /* pass through the text lines */
1057     while (delimited || remaining > 0)
1058     {
1059         if ((linelen = SockRead(sock, buf, sizeof(buf)-1)) == -1)
1060             return(PS_SOCKET);
1061         set_timeout(ctl->server.timeout);
1062
1063         /* write the message size dots */
1064         if (linelen > 0)
1065         {
1066             sizeticker += linelen;
1067             while (sizeticker >= SIZETICKER)
1068             {
1069                 if (outlevel > O_SILENT)
1070                     error_build(".");
1071                 sizeticker -= SIZETICKER;
1072             }
1073         }
1074         remaining -= linelen;
1075
1076         /* fix messages that have only \n line-termination (for qmail) */
1077         if (ctl->forcecr)
1078         {
1079             cp = buf + strlen(buf) - 1;
1080             if (*cp == '\n' && (cp == buf || cp[-1] != '\r'))
1081             {
1082                 *cp++ = '\r';
1083                 *cp++ = '\n';
1084                 *cp++ = '\0';
1085             }
1086         }
1087
1088         /* check for end of message */
1089         if (delimited && *buf == '.')
1090             if (buf[1] == '\r' && buf[2] == '\n')
1091                 break;
1092
1093         /* ship out the text line */
1094         if (forward_ok)
1095         {
1096             /* SMTP byte-stuffing */
1097             if (*buf == '.')
1098                 if (sinkfp && ctl->mda)
1099                     fputs(".", sinkfp);
1100                 else if (forward_ok && ctl->smtp_socket != -1)
1101                     SockWrite(ctl->smtp_socket, buf, 1);
1102
1103             /* we may need to strip carriage returns */
1104             if (ctl->stripcr)
1105             {
1106                 char    *sp, *tp;
1107
1108                 for (sp = tp = buf; *sp; sp++)
1109                     if (*sp != '\r')
1110                         *tp++ =  *sp;
1111                 *tp = '\0';
1112             }
1113
1114             n = 0;
1115             if (ctl->mda)
1116                 n = fwrite(buf, 1, strlen(buf), sinkfp);
1117             else if (ctl->smtp_socket != -1)
1118                 n = SockWrite(ctl->smtp_socket, buf, strlen(buf));
1119
1120             if (n < 0)
1121             {
1122                 error(0, errno, "writing message text");
1123                 if (ctl->mda)
1124                 {
1125                     pclose(sinkfp);
1126                     signal(SIGCHLD, sigchld);
1127                 }
1128                 return(PS_IOERR);
1129             }
1130             else if (outlevel == O_VERBOSE)
1131                 fputc('*', stderr);
1132         }
1133     }
1134
1135     /*
1136      * End-of-message processing starts here
1137      */
1138
1139     if (outlevel == O_VERBOSE)
1140         fputc('\n', stderr);
1141
1142     if (ctl->mda)
1143     {
1144         int rc;
1145
1146         /* close the delivery pipe, we'll reopen before next message */
1147         rc = pclose(sinkfp);
1148         signal(SIGCHLD, sigchld);
1149         if (rc)
1150         {
1151             error(0, -1, "MDA exited abnormally or returned nonzero status");
1152             return(PS_IOERR);
1153         }
1154     }
1155     else if (ctl->smtp_socket != -1)
1156     {
1157         /* write message terminator */
1158         if (SMTP_eom(ctl->smtp_socket) != SM_OK)
1159         {
1160             error(0, -1, "SMTP listener refused delivery");
1161             ctl->errcount++;
1162             return(PS_TRANSIENT);
1163         }
1164     }
1165
1166     return(delete_ok ? PS_SUCCESS : PS_TRANSIENT);
1167 }
1168
1169 #ifdef KERBEROS_V4
1170 int
1171 kerberos_auth (socket, canonical) 
1172 /* authenticate to the server host using Kerberos V4 */
1173 int socket;             /* socket to server host */
1174 const char *canonical;  /* server name */
1175 {
1176     char * host_primary;
1177     KTEXT ticket;
1178     MSG_DAT msg_data;
1179     CREDENTIALS cred;
1180     Key_schedule schedule;
1181     int rem;
1182   
1183     ticket = ((KTEXT) (malloc (sizeof (KTEXT_ST))));
1184     rem = (krb_sendauth (0L, socket, ticket, "pop",
1185                          canonical,
1186                          ((char *) (krb_realmofhost (canonical))),
1187                          ((unsigned long) 0),
1188                          (&msg_data),
1189                          (&cred),
1190                          (schedule),
1191                          ((struct sockaddr_in *) 0),
1192                          ((struct sockaddr_in *) 0),
1193                          "KPOPV0.1"));
1194     free (ticket);
1195     if (rem != KSUCCESS)
1196     {
1197         error(0, -1, "kerberos error %s", (krb_get_err_text (rem)));
1198         return (PS_ERROR);
1199     }
1200     return (0);
1201 }
1202 #endif /* KERBEROS_V4 */
1203
1204 int do_protocol(ctl, proto)
1205 /* retrieve messages from server using given protocol method table */
1206 struct query *ctl;              /* parsed options with merged-in defaults */
1207 const struct method *proto;     /* protocol method table */
1208 {
1209     int ok, js, pst;
1210     char *msg, *sp, *cp, realname[HOSTLEN];
1211     void (*sigsave)();
1212
1213 #ifndef KERBEROS_V4
1214     if (ctl->server.authenticate == A_KERBEROS_V4)
1215     {
1216         error(0, -1, "Kerberos V4 support not linked.");
1217         return(PS_ERROR);
1218     }
1219 #endif /* KERBEROS_V4 */
1220
1221     /* lacking methods, there are some options that may fail */
1222     if (!proto->is_old)
1223     {
1224         /* check for unsupported options */
1225         if (ctl->flush) {
1226             error(0, 0,
1227                     "Option --flush is not supported with %s",
1228                     proto->name);
1229             return(PS_SYNTAX);
1230         }
1231         else if (ctl->fetchall) {
1232             error(0, 0,
1233                     "Option --all is not supported with %s",
1234                     proto->name);
1235             return(PS_SYNTAX);
1236         }
1237     }
1238     if (!proto->getsizes && ctl->limit)
1239     {
1240         error(0, 0,
1241                 "Option --limit is not supported with %s",
1242                 proto->name);
1243         return(PS_SYNTAX);
1244     }
1245
1246     protocol = proto;
1247     tagnum = 0;
1248     tag[0] = '\0';      /* nuke any tag hanging out from previous query */
1249     ok = 0;
1250     error_init(poll_interval == 0 && !logfile);
1251
1252     /* set up the server-nonresponse timeout */
1253     sigsave = signal(SIGALRM, timeout_handler);
1254     set_timeout(mytimeout = ctl->server.timeout);
1255
1256     if ((js = setjmp(restart)) == 1)
1257     {
1258         error(0, 0,
1259                 "timeout after %d seconds waiting for %s.",
1260                 ctl->server.timeout, ctl->server.names->id);
1261         ok = PS_ERROR;
1262     }
1263     else
1264     {
1265         char buf [POPBUFSIZE+1];
1266         int *msgsizes, len, num, count, new, deletions = 0;
1267         int sock, port, fetches;
1268         struct idlist *idp;
1269
1270         /* execute pre-initialization command, if any */
1271         if (ctl->preconnect && (ok = system(ctl->preconnect)))
1272         {
1273             sprintf(buf, "pre-connection command failed with status %d", ok);
1274             error(0, 0, buf);
1275             ok = PS_SYNTAX;
1276             goto closeUp;
1277         }
1278
1279         /* open a socket to the mail server */
1280         port = ctl->server.port ? ctl->server.port : protocol->port;
1281         if ((sock = SockOpen(ctl->server.names->id, port)) == -1)
1282         {
1283 #ifndef EHOSTUNREACH
1284 #define EHOSTUNREACH (-1)
1285 #endif
1286             if (outlevel == O_VERBOSE || errno != EHOSTUNREACH)
1287                 error(0, errno, "connecting to host");
1288             ok = PS_SOCKET;
1289             goto closeUp;
1290         }
1291
1292 #ifdef KERBEROS_V4
1293         if (ctl->server.authenticate == A_KERBEROS_V4)
1294         {
1295             ok = kerberos_auth(sock, ctl->server.canonical_name);
1296             if (ok != 0)
1297                 goto cleanUp;
1298             set_timeout(ctl->server.timeout);
1299         }
1300 #endif /* KERBEROS_V4 */
1301
1302         /* accept greeting message from mail server */
1303         ok = (protocol->parse_response)(sock, buf);
1304         if (ok != 0)
1305             goto cleanUp;
1306         set_timeout(ctl->server.timeout);
1307
1308         /*
1309          * Try to parse the host's actual name out of the greeting
1310          * message.  We do this so that the progress messages will
1311          * make sense even if the connection is indirected through
1312          * ssh. *Do* use this for hacking reply headers, but *don't*
1313          * use it for error logging, as the names in the log should
1314          * correlate directly back to rc file entries.
1315          *
1316          * This assumes that the first space-delimited token found
1317          * that contains at least two dots (with the characters on
1318          * each side of the dot alphanumeric to exclude version
1319          * numbers) is the hostname.  The hostname candidate may not
1320          * contain @ -- if it does it's probably a mailserver
1321          * maintainer's name.  If no such token is found, fall back on
1322          * the .fetchmailrc id.
1323          */
1324         pst = 0;
1325         for (cp = buf; *cp; cp++)
1326         {
1327             switch (pst)
1328             {
1329             case 0:             /* skip to end of current token */
1330                 if (*cp == ' ')
1331                     pst = 1;
1332                 break;
1333
1334             case 1:             /* look for blank-delimited token */
1335                 if (*cp != ' ')
1336                 {
1337                     sp = cp;
1338                     pst = 2;
1339                 }
1340                 break;
1341
1342             case 2:             /* look for first dot */
1343                 if (*cp == '@')
1344                     pst = 0;
1345                 else if (*cp == ' ')
1346                     pst = 1;
1347                 else if (*cp == '.' && isalpha(cp[1]) && isalpha(cp[-1]))
1348                     pst = 3;
1349                 break;
1350
1351             case 3:             /* look for second dot */
1352                 if (*cp == '@')
1353                     pst = 0;
1354                 else if (*cp == ' ')
1355                     pst = 1;
1356                 else if (*cp == '.' && isalpha(cp[1]) && isalpha(cp[-1]))
1357                     pst = 4;
1358                 break;
1359
1360             case 4:             /* look for trailing space */
1361                 if (*cp == '@')
1362                     pst = 0;
1363                 else if (*cp == ' ')
1364                 {
1365                     pst = 5;
1366                     goto done;
1367                 }
1368                 break;
1369             }
1370         }
1371     done:
1372         if (pst == 5)
1373         {
1374             char        *tp = realname;
1375
1376             while (sp < cp)
1377                 *tp++ = *sp++;
1378             *tp = '\0';
1379         }
1380         else
1381             strcpy(realname, ctl->server.names->id);
1382
1383         /* try to get authorized to fetch mail */
1384         if (protocol->getauth)
1385         {
1386             shroud = ctl->password;
1387             ok = (protocol->getauth)(sock, ctl, buf);
1388             shroud = (char *)NULL;
1389             if (ok == PS_ERROR)
1390                 ok = PS_AUTHFAIL;
1391             if (ok != 0)
1392             {
1393                 error(0, -1, "Authorization failure on %s@%s", 
1394                       ctl->remotename,
1395                       realname);
1396                 goto cleanUp;
1397             }
1398             set_timeout(ctl->server.timeout);
1399         }
1400
1401         ctl->errcount = fetches = 0;
1402
1403         /* now iterate over each folder selected */
1404         for (idp = ctl->mailboxes; idp; idp = idp->next)
1405         {
1406             if (outlevel >= O_VERBOSE)
1407                 if (idp->next)
1408                     error(0, 0, "selecting folder %s");
1409                 else
1410                     error(0, 0, "selecting default folder");
1411
1412             /* compute number of messages and number of new messages waiting */
1413             ok = (protocol->getrange)(sock, ctl, idp->id, &count, &new);
1414             if (ok != 0)
1415                 goto cleanUp;
1416             set_timeout(ctl->server.timeout);
1417
1418             /* show user how many messages we downloaded */
1419             if (idp->id)
1420                 (void) sprintf(buf, "%s@%s:%s",
1421                                ctl->remotename, realname, idp->id);
1422             else
1423                 (void) sprintf(buf, "%s@%s", ctl->remotename, realname);
1424             if (outlevel > O_SILENT)
1425                 if (count == -1)                /* only used for ETRN */
1426                     error(0, 0, "Polling %s", buf);
1427                 else if (count == 0)
1428                     error(0, 0, "No mail at %s", buf); 
1429                 else
1430                 {
1431                     if (new != -1 && (count - new) > 0)
1432                         error(0, 0, "%d message%s (%d seen) at %s.",
1433                               count, count > 1 ? "s" : "", count-new, buf);
1434                     else
1435                         error(0, 0, "%d message%s at %s.", 
1436                               count, count > 1 ? "s" : "", buf);
1437                 }
1438
1439             /* we may need to get sizes in order to check message limits */
1440             msgsizes = (int *)NULL;
1441             if (!ctl->fetchall && proto->getsizes && ctl->limit)
1442             {
1443                 msgsizes = (int *)alloca(sizeof(int) * count);
1444
1445                 ok = (proto->getsizes)(sock, count, msgsizes);
1446                 if (ok != 0)
1447                     goto cleanUp;
1448                 set_timeout(ctl->server.timeout);
1449             }
1450
1451             if (check_only)
1452             {
1453                 if (new == -1 || ctl->fetchall)
1454                     new = count;
1455                 ok = ((new > 0) ? PS_SUCCESS : PS_NOMAIL);
1456                 goto cleanUp;
1457             }
1458             else if (count > 0)
1459             {    
1460                 int     force_retrieval;
1461
1462                 /*
1463                  * What forces this code is that in POP3 and IMAP2BIS you can't
1464                  * fetch a message without having it marked `seen'.  In IMAP4,
1465                  * on the other hand, you can (peek_capable is set to convey
1466                  * this).
1467                  *
1468                  * The result of being unable to peek is that if there's
1469                  * any kind of transient error (DNS lookup failure, or
1470                  * sendmail refusing delivery due to process-table limits)
1471                  * the message will be marked "seen" on the server without
1472                  * having been delivered.  This is not a big problem if
1473                  * fetchmail is running in foreground, because the user
1474                  * will see a "skipped" message when it next runs and get
1475                  * clued in.
1476                  *
1477                  * But in daemon mode this leads to the message being silently
1478                  * ignored forever.  This is not acceptable.
1479                  *
1480                  * We compensate for this by checking the error count from the 
1481                  * previous pass and forcing all messages to be considered new
1482                  * if it's nonzero.
1483                  */
1484                 force_retrieval = !peek_capable && (ctl->errcount > 0);
1485
1486                 /* read, forward, and delete messages */
1487                 for (num = 1; num <= count; num++)
1488                 {
1489                     int toolarge = msgsizes && (msgsizes[num-1] > ctl->limit);
1490                     int fetch_it = ctl->fetchall ||
1491                         (!toolarge && (force_retrieval || !(protocol->is_old && (protocol->is_old)(sock,ctl,num))));
1492                     int suppress_delete = FALSE;
1493
1494                     /* we may want to reject this message if it's old */
1495                     if (!fetch_it)
1496                     {
1497                         if (outlevel > O_SILENT)
1498                         {
1499                             error_build("skipping message %d", num);
1500                             if (toolarge)
1501                                 error_build(" (oversized, %d bytes)", msgsizes[num-1]);
1502                         }
1503                     }
1504                     else
1505                     {
1506                         /* request a message */
1507                         ok = (protocol->fetch_headers)(sock, ctl, num, &len);
1508                         if (ok != 0)
1509                             goto cleanUp;
1510                         set_timeout(ctl->server.timeout);
1511
1512                         if (outlevel > O_SILENT)
1513                         {
1514                             error_build("reading message %d", num);
1515                             if (len > 0)
1516                                 error_build(" (%d bytes)", len);
1517                             if (outlevel == O_VERBOSE)
1518                                 error_complete(0, 0, "");
1519                             else
1520                                 error_build(" ");
1521                         }
1522
1523                         /* read the message and ship it to the output sink */
1524                         ok = gen_readmsg(sock,
1525                                          len, 
1526                                          protocol->delimited,
1527                                          num,
1528                                          ctl,
1529                                          realname);
1530                         if (ok == PS_TRANSIENT)
1531                             suppress_delete = TRUE;
1532                         else if (ok)
1533                             goto cleanUp;
1534                         set_timeout(ctl->server.timeout);
1535
1536                         /* tell the server we got it OK and resynchronize */
1537                         if (protocol->trail)
1538                         {
1539                             ok = (protocol->trail)(sock, ctl, num);
1540                             if (ok != 0)
1541                                 goto cleanUp;
1542                             set_timeout(ctl->server.timeout);
1543                         }
1544
1545                         fetches++;
1546                     }
1547
1548                     /*
1549                      * At this point in flow of control, either we've bombed
1550                      * on a protocol error or had delivery refused by the SMTP
1551                      * server (unlikely -- I've never seen it) or we've seen
1552                      * `accepted for delivery' and the message is shipped.
1553                      * It's safe to mark the message seen and delete it on the
1554                      * server now.
1555                      */
1556
1557                     /* maybe we delete this message now? */
1558                     if (protocol->delete
1559                         && !suppress_delete
1560                         && (fetch_it ? !ctl->keep : ctl->flush))
1561                     {
1562                         deletions++;
1563                         if (outlevel > O_SILENT) 
1564                             error_complete(0, 0, " flushed");
1565                         ok = (protocol->delete)(sock, ctl, num);
1566                         if (ok != 0)
1567                             goto cleanUp;
1568                         set_timeout(ctl->server.timeout);
1569                         delete_str(&ctl->newsaved, num);
1570                     }
1571                     else if (outlevel > O_SILENT) 
1572                         error_complete(0, 0, " not flushed");
1573
1574                     /* perhaps this as many as we're ready to handle */
1575                     if (ctl->fetchlimit && ctl->fetchlimit <= fetches)
1576                         goto no_error;
1577                 }
1578             }
1579         }
1580
1581    no_error:
1582         set_timeout(ctl->server.timeout);
1583         ok = gen_transact(sock, protocol->exit_cmd);
1584         if (ok == 0)
1585             ok = (fetches > 0) ? PS_SUCCESS : PS_NOMAIL;
1586         set_timeout(0);
1587         close(sock);
1588         goto closeUp;
1589
1590     cleanUp:
1591         set_timeout(ctl->server.timeout);
1592         if (ok != 0 && ok != PS_SOCKET)
1593             gen_transact(sock, protocol->exit_cmd);
1594         set_timeout(0);
1595         close(sock);
1596     }
1597
1598     switch (ok)
1599     {
1600     case PS_SOCKET:
1601         msg = "socket";
1602         break;
1603     case PS_AUTHFAIL:
1604         msg = "authorization";
1605         break;
1606     case PS_SYNTAX:
1607         msg = "missing or bad RFC822 header";
1608         break;
1609     case PS_IOERR:
1610         msg = "MDA";
1611         break;
1612     case PS_ERROR:
1613         msg = "client/server synchronization";
1614         break;
1615     case PS_PROTOCOL:
1616         msg = "client/server protocol";
1617         break;
1618     case PS_SMTP:
1619         msg = "SMTP transaction";
1620         break;
1621     case PS_UNDEFINED:
1622         error(0, 0, "undefined");
1623         break;
1624     }
1625     if (ok==PS_SOCKET || ok==PS_AUTHFAIL || ok==PS_SYNTAX || ok==PS_IOERR
1626                 || ok==PS_ERROR || ok==PS_PROTOCOL || ok==PS_SMTP)
1627         error(0, -1, "%s error while fetching from %s", msg, ctl->server.names->id);
1628
1629 closeUp:
1630     signal(SIGALRM, sigsave);
1631     return(ok);
1632 }
1633
1634 #if defined(HAVE_STDARG_H)
1635 void gen_send(int sock, char *fmt, ... )
1636 /* assemble command in printf(3) style and send to the server */
1637 #else
1638 void gen_send(sock, fmt, va_alist)
1639 /* assemble command in printf(3) style and send to the server */
1640 int sock;               /* socket to which server is connected */
1641 const char *fmt;        /* printf-style format */
1642 va_dcl
1643 #endif
1644 {
1645     char buf [POPBUFSIZE+1];
1646     va_list ap;
1647
1648     if (protocol->tagged)
1649         (void) sprintf(buf, "%s ", GENSYM);
1650     else
1651         buf[0] = '\0';
1652
1653 #if defined(HAVE_STDARG_H)
1654     va_start(ap, fmt) ;
1655 #else
1656     va_start(ap);
1657 #endif
1658     vsprintf(buf + strlen(buf), fmt, ap);
1659     va_end(ap);
1660
1661     strcat(buf, "\r\n");
1662     SockWrite(sock, buf, strlen(buf));
1663
1664     if (outlevel == O_VERBOSE)
1665     {
1666         char *cp;
1667
1668         if (shroud && shroud[0] && (cp = strstr(buf, shroud)))
1669         {
1670             char        *sp;
1671
1672             sp = cp + strlen(shroud);
1673             *cp++ = '*';
1674             while (*sp)
1675                 *cp++ = *sp++;
1676             *cp = '\0';
1677         }
1678         buf[strlen(buf)-2] = '\0';
1679         error(0, 0, "%s> %s", protocol->name, buf);
1680     }
1681 }
1682
1683 int gen_recv(sock, buf, size)
1684 /* get one line of input from the server */
1685 int sock;       /* socket to which server is connected */
1686 char *buf;      /* buffer to receive input */
1687 int size;       /* length of buffer */
1688 {
1689     if (SockRead(sock, buf, size) == -1)
1690         return(PS_SOCKET);
1691     else
1692     {
1693         if (buf[strlen(buf)-1] == '\n')
1694             buf[strlen(buf)-1] = '\0';
1695         if (buf[strlen(buf)-1] == '\r')
1696             buf[strlen(buf)-1] = '\r';
1697         if (outlevel == O_VERBOSE)
1698             error(0, 0, "%s< %s", protocol->name, buf);
1699         return(PS_SUCCESS);
1700     }
1701 }
1702
1703 #if defined(HAVE_STDARG_H)
1704 int gen_transact(int sock, char *fmt, ... )
1705 /* assemble command in printf(3) style, send to server, accept a response */
1706 #else
1707 int gen_transact(int sock, fmt, va_alist)
1708 /* assemble command in printf(3) style, send to server, accept a response */
1709 int sock;               /* socket to which server is connected */
1710 const char *fmt;        /* printf-style format */
1711 va_dcl
1712 #endif
1713 {
1714     int ok;
1715     char buf [POPBUFSIZE+1];
1716     va_list ap;
1717
1718     if (protocol->tagged)
1719         (void) sprintf(buf, "%s ", GENSYM);
1720     else
1721         buf[0] = '\0';
1722
1723 #if defined(HAVE_STDARG_H)
1724     va_start(ap, fmt) ;
1725 #else
1726     va_start(ap);
1727 #endif
1728     vsprintf(buf + strlen(buf), fmt, ap);
1729     va_end(ap);
1730
1731     strcat(buf, "\r\n");
1732     SockWrite(sock, buf, strlen(buf));
1733
1734     if (outlevel == O_VERBOSE)
1735     {
1736         char *cp;
1737
1738         if (shroud && shroud[0] && (cp = strstr(buf, shroud)))
1739         {
1740             char        *sp;
1741
1742             sp = cp + strlen(shroud);
1743             *cp++ = '*';
1744             while (*sp)
1745                 *cp++ = *sp++;
1746             *cp = '\0';
1747         }
1748         buf[strlen(buf)-1] = '\0';
1749         error(0, 0, "%s> %s", protocol->name, buf);
1750     }
1751
1752     /* we presume this does its own response echoing */
1753     ok = (protocol->parse_response)(sock, buf);
1754     set_timeout(mytimeout);
1755
1756     return(ok);
1757 }
1758
1759 /* driver.c ends here */