2 * For license terms, see the file COPYING in this directory.
5 /* We need this for HAVE_STDARG_H, etc */
8 /* We need this for size_t */
11 /* We need this for time_t */
12 #if TIME_WITH_SYS_TIME
13 # include <sys/time.h>
17 # include <sys/time.h>
25 /* We need this for strstr */
26 #if !defined(HAVE_STRSTR) && !defined(strstr)
27 char *strstr(const char *, const char *);
30 /* constants designating the various supported protocols */
41 #define SMTP_PORT "smtp"
42 #define KPOP_PORT "kpop"
43 #else /* INET6_ENABLE */
45 #define KPOP_PORT 1109
46 #endif /* INET6_ENABLE */
49 #define SIMAP_PORT 993
50 #define SPOP3_PORT 995
54 * We need to distinguish between mailbox and mailbag protocols.
55 * Under a mailbox protocol wwe're pulling mail for a speecific user.
56 * Under a mailbag protocol we're fetching mail for an entire domain.
58 #define MAILBOX_PROTOCOL(ctl) ((ctl)->server.protocol < P_ETRN)
60 /* authentication types */
61 #define A_ANY 0 /* use the first method that works */
62 #define A_PASSWORD 1 /* password authentication */
63 #define A_NTLM 2 /* Microsoft NTLM protocol */
64 #define A_CRAM_MD5 3 /* CRAM-MD5 shrouding (RFC2195) */
65 #define A_OTP 4 /* One-time password (RFC1508) */
66 #define A_KERBEROS_V4 5 /* authenticate w/ Kerberos V4 */
67 #define A_KERBEROS_V5 6 /* authenticate w/ Kerberos V5 */
68 #define A_GSSAPI 7 /* authenticate with GSSAPI */
69 #define A_SSH 8 /* authentication at session level */
71 /* some protocols (KERBEROS, GSSAPI, SSH) don't require a password */
72 #define NO_PASSWORD(ctl) ((ctl)->server.authenticate > A_OTP || (ctl)->server.protocol == P_ETRN)
75 * Definitions for buffer sizes. We get little help on setting maxima
76 * from IMAP RFCs up to 2060, so these are mostly from POP3.
78 #define HOSTLEN 635 /* max hostname length (RFC1123) */
79 #define POPBUFSIZE 512 /* max length of response (RFC1939) */
80 #define IDLEN 128 /* max length of UID (RFC1939) */
82 /* per RFC1939 this should be 40, but Microsoft Exchange ignores that limit */
83 #define USERNAMELEN 128 /* max POP3 arg length */
85 /* clear a netBSD kernel parameter out of the way */
89 * The RFC822 limit on message line size is just 998. But
90 * make this *way* oversized; idiot DOS-world mailers that
91 * don't line-wrap properly often ship entire paragraphs as
94 #define MSGBUFSIZE 8192
96 #define NAMELEN 64 /* max username length */
97 #define PASSWORDLEN 64 /* max password length */
98 #define DIGESTLEN 33 /* length of MD5 digest */
100 /* exit code values */
101 #define PS_SUCCESS 0 /* successful receipt of messages */
102 #define PS_NOMAIL 1 /* no mail available */
103 #define PS_SOCKET 2 /* socket I/O woes */
104 #define PS_AUTHFAIL 3 /* user authorization failed */
105 #define PS_PROTOCOL 4 /* protocol violation */
106 #define PS_SYNTAX 5 /* command-line syntax error */
107 #define PS_IOERR 6 /* bad permissions on rc file */
108 #define PS_ERROR 7 /* protocol error */
109 #define PS_EXCLUDE 8 /* client-side exclusion error */
110 #define PS_LOCKBUSY 9 /* server responded lock busy */
111 #define PS_SMTP 10 /* SMTP error */
112 #define PS_DNS 11 /* fatal DNS error */
113 #define PS_BSMTP 12 /* output batch could not be opened */
114 #define PS_MAXFETCH 13 /* poll ended by fetch limit */
115 #define PS_SERVBUSY 14 /* server is busy */
116 /* leave space for more codes */
117 #define PS_UNDEFINED 23 /* something I hadn't thought of */
118 #define PS_TRANSIENT 24 /* transient failure (internal use) */
119 #define PS_REFUSED 25 /* mail refused (internal use) */
120 #define PS_RETAINED 26 /* message retained (internal use) */
121 #define PS_TRUNCATED 27 /* headers incomplete (internal use) */
122 #define PS_REPOLL 28 /* repoll immediately with changed parameters (internal use) */
123 #define PS_IDLETIMEOUT 29 /* timeout on imap IDLE (internal use) */
125 /* output noise level */
126 #define O_SILENT 0 /* mute, max squelch, etc. */
127 #define O_NORMAL 1 /* user-friendly */
128 #define O_VERBOSE 2 /* chatty */
129 #define O_DEBUG 3 /* prolix */
130 #define O_MONITOR O_VERBOSE
132 #define SIZETICKER 1024 /* print 1 dot per this many bytes */
135 * We #ifdef this and use flag rather than bool
136 * to avoid a type clash with curses.h
144 /* we need to use zero as a flag-uninitialized value */
170 flag mark; /* UID-index information */
171 #define UID_UNSEEN 0 /* hasn't been seen */
172 #define UID_SEEN 1 /* seen, but not deleted */
173 #define UID_DELETED 2 /* this message has been deleted */
174 #define UID_EXPUNGED 3 /* this message has been expunged */
184 struct method /* describe methods for protocol state machine */
186 const char *name; /* protocol name */
189 const char *sslservice;
190 #else /* INET6_ENABLE */
191 int port; /* service port */
192 int sslport; /* service port for ssl */
193 #endif /* INET6_ENABLE */
194 flag tagged; /* if true, generate & expect command tags */
195 flag delimited; /* if true, accept "." message delimiter */
196 int (*parse_response)(int, char *);
197 /* response_parsing function */
198 int (*getauth)(int, struct query *, char *);
199 /* authorization fetcher */
200 int (*getrange)(int, struct query *, const char *, int *, int *, int *);
201 /* get message range to fetch */
202 int (*getsizes)(int, int, int *);
203 /* get sizes of messages */
204 int (*getpartialsizes)(int, int, int, int *);
205 /* get sizes of subset of messages */
206 int (*is_old)(int, struct query *, int);
207 /* check for old message */
208 int (*fetch_headers)(int, struct query *, int, int *);
209 /* fetch FROM headera given message */
210 int (*fetch_body)(int, struct query *, int, int *);
211 /* fetch a given message */
212 int (*trail)(int, struct query *, int);
213 /* eat trailer of a message */
214 int (*delete)(int, struct query *, int);
216 int (*mark_seen)(int, struct query *, int);
217 /* mark as seen method */
218 int (*logout_cmd)(int, struct query *);
220 flag retry; /* can getrange poll for new messages? */
223 struct hostdata /* shared among all user connections to given server */
226 char *pollname; /* poll label of host */
227 char *via; /* "true" server name if non-NULL */
228 struct idlist *akalist; /* server name first, then akas */
229 struct idlist *localdomains; /* list of pass-through domains */
230 int protocol; /* protocol type */
232 char *service; /* IPv6 service name */
233 void *netsec; /* IPv6 security request */
234 #else /* INET6_ENABLE */
235 int port; /* TCP/IP service port number */
236 #endif /* INET6_ENABLE */
237 int interval; /* # cycles to skip between polls */
238 int authenticate; /* authentication mode to try */
239 int timeout; /* inactivity timout in seconds */
240 char *envelope; /* envelope address list header */
241 int envskip; /* skip to numbered envelope header */
242 char *qvirtual; /* prefix removed from local user id */
243 flag skip; /* suppress poll in implicit mode? */
244 flag dns; /* do DNS lookup on multidrop? */
245 flag uidl; /* use RFC1725 UIDLs? */
247 flag sdps; /* use Demon Internet SDPS *ENV */
248 #endif /* SDPS_ENABLE */
249 flag checkalias; /* resolve aliases by comparing IPs? */
250 char *principal; /* Kerberos principal for mail service */
251 char *esmtp_name, *esmtp_password; /* ESMTP AUTH information */
253 #if defined(linux) || defined(__FreeBSD__)
257 struct interface_pair_s *interface_pair;
260 char *plugin,*plugout;
262 /* computed for internal use */
263 const struct method *base_protocol; /* relevant protocol method table */
264 int poll_count; /* count of polls so far */
265 char *queryname; /* name to attempt DNS lookup on */
266 char *truename; /* "true name" of server host */
267 char *trueaddr; /* IP address of truename, as char */
268 struct hostdata *lead_server; /* ptr to lead query for this server */
274 /* mailserver connection controls */
275 struct hostdata server;
278 struct idlist *localnames; /* including calling user's name */
279 int wildcard; /* should unmatched names be passed through */
280 char *remotename; /* remote login name to use */
281 char *password; /* remote password to use */
282 struct idlist *mailboxes; /* list of mailboxes to check */
284 /* per-forwarding-target data */
285 struct idlist *smtphunt; /* list of SMTP hosts to try forwarding to */
286 struct idlist *domainlist; /* domainlist to fetch from */
287 char *smtpaddress; /* address to force in RCPT TO */
288 char *smtpname; /* full RCPT TO name, including domain */
289 struct idlist *antispam; /* list of listener's antispam response */
290 char *mda; /* local MDA to pass mail to */
291 char *bsmtp; /* BSMTP output file */
292 char listener; /* what's the listener's wire protocol? */
293 #define SMTP_MODE 'S'
294 #define LMTP_MODE 'L'
295 char *preconnect; /* pre-connection command to execute */
296 char *postconnect; /* post-connection command to execute */
298 /* per-user control flags */
299 flag keep; /* if TRUE, leave messages undeleted */
300 flag fetchall; /* if TRUE, fetch all (not just unseen) */
301 flag flush; /* if TRUE, delete messages already seen */
302 flag rewrite; /* if TRUE, canonicalize recipient addresses */
303 flag stripcr; /* if TRUE, strip CRs in text */
304 flag forcecr; /* if TRUE, force CRs before LFs in text */
305 flag pass8bits; /* if TRUE, ignore Content-Transfer-Encoding */
306 flag dropstatus; /* if TRUE, drop Status lines in mail */
307 flag dropdelivered; /* if TRUE, drop Delivered-To lines in mail */
308 flag mimedecode; /* if TRUE, decode MIME-armored messages */
309 flag idle; /* if TRUE, idle after each poll */
310 int limit; /* limit size of retrieved messages */
311 int warnings; /* size warning interval */
312 int fetchlimit; /* max # msgs to get in single poll */
313 int fetchsizelimit; /* max # msg sizes to get in a request */
314 int fastuidl; /* do binary search for new UIDLs? */
315 int fastuidlcount; /* internal count for frequency of binary search */
316 int batchlimit; /* max # msgs to pass in single SMTP session */
317 int expunge; /* max # msgs to pass between expunges */
318 flag use_ssl; /* use SSL encrypted session */
319 char *sslkey; /* optional SSL private key file */
320 char *sslcert; /* optional SSL certificate file */
321 char *sslproto; /* force usage of protocol (ssl2|ssl3|tls1) - defaults to ssl23 */
322 char *sslcertpath; /* Trusted certificate directory for checking the server cert */
323 flag sslcertck; /* Strictly check the server cert. */
324 char *sslfingerprint; /* Fingerprint to check against */
325 char *properties; /* passthrough properties for extensions */
326 flag tracepolls; /* if TRUE, add poll trace info to Received */
328 /* internal use -- per-poll state */
329 flag active; /* should we actually poll this server? */
330 const char *destaddr; /* destination host for this query */
331 int errcount; /* count transient errors in last pass */
332 int authfailcount; /* count of authorization failures */
333 int wehaveauthed; /* We've managed to logon at least once! */
334 int wehavesentauthnote; /* We've sent an authorization failure note */
335 int wedged; /* wedged by auth failures or timeouts? */
336 char *smtphost; /* actual SMTP host we connected to */
337 int smtp_socket; /* socket descriptor for SMTP connection */
338 unsigned int uid; /* UID of user to deliver to */
339 struct idlist *skipped; /* messages skipped on the mail server */
340 struct idlist *oldsaved, *newsaved;
341 struct idlist **oldsavedend;
342 char lastdigest[DIGESTLEN]; /* last MD5 hash seen on this connection */
344 /* internal use -- per-message state */
345 int mimemsg; /* bitmask indicating MIME body-type */
346 char digest[DIGESTLEN]; /* md5 digest buffer */
348 /* internal use -- housekeeping */
349 struct query *next; /* next query control block in chain */
352 struct msgblk /* message header parsed for open_sink() */
354 char *headers; /* raw message headers */
355 struct idlist *recipients; /* addressees */
356 char return_path[HOSTLEN + USERNAMELEN + 4];
363 * Numeric option handling. Numeric option value of zero actually means
364 * it's unspecified. Value less than zero is zero. The reason for this
365 * screwy encoding is so we can zero out an option block in order to set the
366 * numeric flags in it to unspecified.
368 #define NUM_VALUE_IN(n) (((n) == 0) ? -1 : (n))
369 #define NUM_VALUE_OUT(n) (((n) < 0) ? 0 : (n))
370 #define NUM_NONZERO(n) ((n) > 0)
371 #define NUM_ZERO(n) ((n) < 0)
372 #define NUM_SPECIFIED(n) ((n) != 0)
374 #define MULTIDROP(ctl) (ctl->wildcard || \
375 ((ctl)->localnames && (ctl)->localnames->next))
378 * Note: tags are generated with an a%04d format from a 1-origin
379 * integer sequence number. Length 4 permits transaction numbers
380 * up to 9999, so we force rollover with % 10000. There's no special
381 * reason for this format other than to look like the exmples in the
384 #define TAGLEN 6 /* 'a' + 4 digits + NUL */
385 extern char tag[TAGLEN];
388 /* list of hosts assembled from run control file and command line */
389 extern struct query cmd_opts, *querylist;
391 /* what's returned by envquery */
392 extern void envquery(int, char **);
394 /* controls the detail level of status/progress messages written to stderr */
395 extern int outlevel; /* see the O_.* constants above */
396 extern int yydebug; /* enable parse debugging */
398 /* these get computed */
399 extern int batchcount; /* count of messages sent in current batch */
400 extern flag peek_capable; /* can we read msgs without setting seen? */
402 /* miscellaneous global controls */
403 extern struct runctl run; /* global controls for this run */
404 extern flag nodetach; /* if TRUE, don't detach daemon process */
405 extern flag quitmode; /* if --quit was set */
406 extern flag check_only; /* if --check was set */
407 extern char *rcfile; /* path name of rc file */
408 extern int linelimit; /* limit # lines retrieved per site */
409 extern flag versioninfo; /* emit only version info */
410 extern char *user; /* name of invoking user */
411 extern char *home; /* home directory of invoking user */
412 extern char *fmhome; /* fetchmail home directory */
413 extern int pass; /* number of re-polling pass */
414 extern flag configdump; /* dump control blocks as Python dictionary */
415 extern char *fetchmailhost; /* either "localhost" or an FQDN */
416 extern int suppress_tags; /* suppress tags in tagged protocols? */
417 extern char shroud[PASSWORDLEN*2+3]; /* string to shroud in debug output */
419 extern char *sdps_envfrom;
420 extern char *sdps_envto;
421 #endif /* SDPS_ENABLE */
423 /* prototypes for globally callable functions */
425 /* from /usr/include/sys/cdefs.h */
426 #if !defined __GNUC__ || __GNUC__ < 2
427 # define __attribute__(xyz) /* Ignore. */
430 /* error.c: Error reporting */
431 #if defined(HAVE_STDARG_H)
432 void report_init(int foreground);
433 void report (FILE *fp, const char *format, ...)
434 __attribute__ ((format (printf, 2, 3)))
436 void report_build (FILE *fp, const char *format, ...)
437 __attribute__ ((format (printf, 2, 3)))
439 void report_complete (FILE *fp, const char *format, ...)
440 __attribute__ ((format (printf, 2, 3)))
442 void report_at_line (FILE *fp, int, const char *, unsigned int, const char *, ...)
443 __attribute__ ((format (printf, 5, 6)))
447 void report_build ();
448 void report_complete ();
449 void report_at_line ();
452 /* driver.c -- main driver loop */
453 void set_timeout(int);
454 int isidletimeout(void);
455 void resetidletimeout(void);
456 int do_protocol(struct query *, const struct method *);
458 /* transact.c: transaction support */
459 void init_transact(const struct method *);
460 int readheaders(int sock,
465 flag *suppress_readbody);
466 int readbody(int sock, struct query *ctl, flag forward, int len);
467 #if defined(HAVE_STDARG_H)
468 void gen_send(int sock, const char *, ... )
469 __attribute__ ((format (printf, 2, 3)))
471 int gen_recv(int sock, char *buf, int size);
472 int gen_transact(int sock, const char *, ... )
473 __attribute__ ((format (printf, 2, 3)))
480 extern struct msgblk msgblk;
482 /* lock.c: concurrency locking */
483 void lock_setup(void), lock_assert(void);
484 void lock_or_die(void), lock_release(void);
485 int lock_state(void);
486 void lock_dispose(void);
488 /* use these to track what was happening when the nonresponse timer fired */
489 #define GENERAL_WAIT 0 /* unknown wait type */
490 #define OPEN_WAIT 1 /* waiting from mailserver open */
491 #define SERVER_WAIT 2 /* waiting for mailserver response */
492 #define LISTENER_WAIT 3 /* waiting for listener initialization */
493 #define FORWARDING_WAIT 4 /* waiting for listener response */
496 /* response hooks can use this to identify the query stage */
497 #define STAGE_GETAUTH 0
498 #define STAGE_GETRANGE 1
499 #define STAGE_GETSIZES 2
500 #define STAGE_FETCH 3
502 #define STAGE_LOGOUT 5
505 extern int mytimeout;
507 /* mark values for name lists */
508 #define XMIT_ACCEPT 1 /* accepted; matches local domain or name */
509 #define XMIT_REJECT 2 /* rejected; no match */
510 #define XMIT_RCPTBAD 3 /* SMTP listener rejected the name */
513 int interruptible_idle(int interval);
514 extern volatile int lastsig;
516 /* sink.c: forwarding */
517 void smtp_close(struct query *, int);
518 int smtp_open(struct query *);
519 char *rcpt_address(struct query *, const char *, int);
520 int stuffline(struct query *, char *);
521 int open_sink(struct query*, struct msgblk *, int*, int*);
522 void release_sink(struct query *);
523 int close_sink(struct query *, struct msgblk *, flag);
524 int open_warning_by_mail(struct query *, struct msgblk *);
525 #if defined(HAVE_STDARG_H)
526 void stuff_warning(const char *, struct query *, const char *, ... )
527 __attribute__ ((format (printf, 3, 4)))
530 void stuff_warning();
532 void close_warning_by_mail(struct query *, struct msgblk *);
534 /* rfc822.c: RFC822 header parsing */
535 unsigned char *reply_hack(unsigned char *, const unsigned char *, int *);
536 unsigned char *nxtaddr(const unsigned char *);
538 /* uid.c: UID support */
539 extern int dofastuidl;
541 void initialize_saved_lists(struct query *, const char *);
542 struct idlist *save_str(struct idlist **, const char *, flag);
543 void free_str_list(struct idlist **);
544 struct idlist *copy_str_list(struct idlist *idl);
545 void save_str_pair(struct idlist **, const char *, const char *);
546 void free_str_pair_list(struct idlist **);
547 int delete_str(struct idlist **, long);
548 struct idlist *str_in_list(struct idlist **, const char *, const flag);
549 int str_nr_in_list(struct idlist **, const char *);
550 int str_nr_last_in_list(struct idlist **, const char *);
551 void str_set_mark( struct idlist **, const char *, const flag);
552 int count_list( struct idlist **idl );
553 char *str_from_nr_list( struct idlist **idl, long number );
554 char *str_find(struct idlist **, long);
555 struct idlist *id_find(struct idlist **idl, long);
556 char *idpair_find(struct idlist **, const char *);
557 void append_str_list(struct idlist **, struct idlist **);
558 void expunge_uids(struct query *);
559 void uid_swap_lists(struct query *);
560 void uid_discard_new_list(struct query *ctl);
561 void uid_reset_num(struct query *ctl);
562 void write_saved_lists(struct query *, const char *);
565 int prc_parse_file(const char *, const flag);
566 int prc_filecheck(const char *, const flag);
569 void to64frombits(unsigned char *, const unsigned char *, int);
570 int from64tobits(char *, const char *, int maxlen);
573 /* Bit-mask returned by MimeBodyType */
574 #define MSG_IS_7BIT 0x01
575 #define MSG_IS_8BIT 0x02
576 #define MSG_NEEDS_DECODE 0x80
577 extern void UnMimeHeader(unsigned char *buf);
578 extern int MimeBodyType(unsigned char *hdrs, int WantDecode);
579 extern int UnMimeBodyline(unsigned char **buf, flag delimited, flag issoftline);
582 void interface_init(void);
583 void interface_parse(char *, struct hostdata *);
584 void interface_note_activity(struct hostdata *);
585 int interface_approve(struct hostdata *, flag domonitor);
588 #if defined(HAVE_VOIDPOINTER)
589 #define XMALLOCTYPE void
591 #define XMALLOCTYPE char
593 XMALLOCTYPE *xmalloc(size_t);
594 XMALLOCTYPE *xrealloc(/*@null@*/ XMALLOCTYPE *, size_t);
595 char *xstrdup(const char *);
596 #if defined(HAVE_ALLOCA_H)
603 #define xalloca(ptr, t, n) if (!(ptr = (t) alloca(n)))\
604 {report(stderr, GT_("alloca failed")); exit(PS_UNDEFINED);}
607 * This is a hack to help xgettext which cannot find strings in
608 * macro definitions like the one for xalloca above.
610 static char *dummy = gettext_noop("alloca failed");
613 /* protocol driver and methods */
614 int doPOP2 (struct query *);
615 int doPOP3 (struct query *);
616 int doIMAP (struct query *);
617 int doETRN (struct query *);
618 int doODMR (struct query *);
620 /* authentication functions */
621 int do_cram_md5(int sock, char *command, struct query *ctl, char *strip);
622 int do_rfc1731(int sock, char *command, char *truename);
623 int do_gssauth(int sock, char *command, char *service, char *hostname, char *username);
624 int do_otp(int sock, char *command, struct query *ctl);
628 /* these should be of size PATH_MAX */
629 extern char currentwd[1024], rcfiledir[1024];
631 struct query *hostalloc(struct query *);
632 int parsecmdline (int, char **, struct runctl *, struct query *);
633 char *prependdir (const char *, const char *);
634 char *MD5Digest (unsigned char *);
635 void hmac_md5 (unsigned char *, size_t, unsigned char *, size_t, unsigned char *, size_t);
636 int POP3_auth_rpa(unsigned char *, unsigned char *, int socket);
637 typedef RETSIGTYPE (*SIGHANDLERTYPE) (int);
638 void deal_with_sigchld(void);
639 RETSIGTYPE null_signal_handler(int sig);
640 SIGHANDLERTYPE set_signal_handler(int sig, SIGHANDLERTYPE handler);
641 int daemonize(const char *, void (*)(int));
642 char *fm_getpassword(char *);
643 void escapes(const char *, char *);
644 char *visbuf(const char *);
645 const char *showproto(int);
646 void dump_config(struct runctl *runp, struct query *querylist);
647 int is_host_alias(const char *, struct query *);
648 char *host_fqdn(void);
649 char *rfc822timestamp(void);
651 char *rfc2047e(const char*, const char *);
653 void yyerror(const char *);
657 void itimerthread(void*);
658 /* Have to include these first to avoid errors from redefining getcwd
659 and chdir. They're re-include protected in EMX, so it's okay, I
663 /* Redefine getcwd and chdir to get drive-letter support so we can
664 find all of our lock files and stuff. */
665 #define getcwd _getcwd2
666 #define chdir _chdir2
670 # ifndef strerror /* On some systems, strerror is a macro */
671 char *strerror (int);
673 # endif /* HAVE_STRERROR */
675 #define STRING_DISABLED (char *)-1
676 #define STRING_DUMMY ""
680 #define S_IXGRP 0000010
685 char *stpcpy(char *, const char*);
688 #ifdef FETCHMAIL_DEBUG
689 #define exit(e) do { \
691 out = fopen("/tmp/fetchmail.log", "a"); \
693 "Exiting fetchmail from file %s, line %d with status %d\n", \
694 __FILE__, __LINE__, e); \
698 #endif /* FETCHMAIL_DEBUG */
702 #else /* !__CYGWIN__ */
704 #endif /* __CYGWIN__ */
706 extern int mailserver_socket_temp;
707 extern char *program_name;
709 /* fetchmail.h ends here */