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