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