2 * For license terms, see the file COPYING in this directory.
5 /* We need this for HAVE_STDARG_H, etc */
8 /* constants designating the various supported protocols */
19 * We need to distinguish between mailbox and mailbag protocols.
20 * Under a mailbox protocol wwe're pulling mail for a speecific user
21 * and the transaction must be authenticated. Under a mailbag protocol
22 * we're fetching mail for an entire domain; the client and server
23 * authenticate to each other but no per-user authentication is needed.
25 #define MAILBOX_PROTOCOL(ctl) ((ctl)->server.protocol < P_ETRN)
28 #define SMTP_PORT "smtp"
29 #define KPOP_PORT "kpop"
30 #else /* INET6_ENABLE */
32 #define KPOP_PORT 1109
33 #endif /* INET6_ENABLE */
36 #define SIMAP_PORT 993
37 #define SPOP3_PORT 995
40 /* authentication types */
41 #define A_ANY 0 /* use the first method that works */
42 #define A_PASSWORD 1 /* password authentication */
43 #define A_CRAM_MD5 2 /* CRAM-MD5 shrouding (RFC2195) */
44 #define A_OTP 3 /* One-time password (RFC1508) */
45 #define A_KERBEROS_V4 4 /* authenticate w/ Kerberos V4 */
46 #define A_KERBEROS_V5 5 /* authenticate w/ Kerberos V5 */
47 #define A_GSSAPI 6 /* authenticate with GSSAPI */
48 #define A_SSH 7 /* authentication at session level */
50 /* some protocols (KERBEROS, GSSAPI, SSH) don't require a password */
51 #define NO_PASSWORD(ctl) ((ctl)->server.authenticate > A_OTP || !MAILBOX_PROTOCOL(ctl))
54 * Definitions for buffer sizes. We get little help on setting maxima
55 * from IMAP RFCs up to 2060, so these are mostly from POP3.
57 #define HOSTLEN 635 /* max hostname length (RFC1123) */
58 #define POPBUFSIZE 512 /* max length of response (RFC1939) */
59 #define IDLEN 128 /* max length of UID (RFC1939) */
61 /* per RFC1939 this should be 40, but Microsoft Exchange ignores that limit */
62 #define USERNAMELEN 128 /* max POP3 arg length */
64 /* clear a netBSD kernel parameter out of the way */
68 * The RFC822 limit on message line size is just 998. But
69 * make this *way* oversized; idiot DOS-world mailers that
70 * don't line-wrap properly often ship entire paragraphs as
73 #define MSGBUFSIZE 8192
75 #define NAMELEN 64 /* max username length */
76 #define PASSWORDLEN 64 /* max password length */
77 #define DIGESTLEN 33 /* length of MD5 digest */
79 /* exit code values */
80 #define PS_SUCCESS 0 /* successful receipt of messages */
81 #define PS_NOMAIL 1 /* no mail available */
82 #define PS_SOCKET 2 /* socket I/O woes */
83 #define PS_AUTHFAIL 3 /* user authorization failed */
84 #define PS_PROTOCOL 4 /* protocol violation */
85 #define PS_SYNTAX 5 /* command-line syntax error */
86 #define PS_IOERR 6 /* bad permissions on rc file */
87 #define PS_ERROR 7 /* protocol error */
88 #define PS_EXCLUDE 8 /* client-side exclusion error */
89 #define PS_LOCKBUSY 9 /* server responded lock busy */
90 #define PS_SMTP 10 /* SMTP error */
91 #define PS_DNS 11 /* fatal DNS error */
92 #define PS_BSMTP 12 /* output batch could not be opened */
93 #define PS_MAXFETCH 13 /* poll ended by fetch limit */
94 /* leave space for more codes */
95 #define PS_UNDEFINED 23 /* something I hadn't thought of */
96 #define PS_TRANSIENT 24 /* transient failure (internal use) */
97 #define PS_REFUSED 25 /* mail refused (internal use) */
98 #define PS_RETAINED 26 /* message retained (internal use) */
99 #define PS_TRUNCATED 27 /* headers incomplete (internal use) */
101 /* output noise level */
102 #define O_SILENT 0 /* mute, max squelch, etc. */
103 #define O_NORMAL 1 /* user-friendly */
104 #define O_VERBOSE 2 /* chatty */
105 #define O_DEBUG 3 /* prolix */
106 #define O_MONITOR O_VERBOSE
108 #define SIZETICKER 1024 /* print 1 dot per this many bytes */
111 * We #ifdef this and use flag rather than bool
112 * to avoid a type clash with curses.h
120 /* we need to use zero as a flag-uninitialized value */
146 flag mark; /* UID-index information */
147 #define UID_UNSEEN 0 /* hasn't been seen */
148 #define UID_SEEN 1 /* seen, but not deleted */
149 #define UID_DELETED 2 /* this message has been deleted */
150 #define UID_EXPUNGED 3 /* this message has been expunged */
160 struct method /* describe methods for protocol state machine */
162 const char *name; /* protocol name */
165 const char *sslservice;
166 #else /* INET6_ENABLE */
167 int port; /* service port */
168 int sslport; /* service port for ssl */
169 #endif /* INET6_ENABLE */
170 flag tagged; /* if true, generate & expect command tags */
171 flag delimited; /* if true, accept "." message delimiter */
172 int (*parse_response)(int, char *);
173 /* response_parsing function */
174 int (*password_canonify)(char *, char *, int);
175 /* canonicalize password */
176 int (*getauth)(int, struct query *, char *);
177 /* authorization fetcher */
178 int (*getrange)(int, struct query *, const char *, int *, int *, int *);
179 /* get message range to fetch */
180 int (*getsizes)(int, int, int *);
181 /* get sizes of messages */
182 int (*is_old)(int, struct query *, int);
183 /* check for old message */
184 int (*fetch_headers)(int, struct query *, int, int *);
185 /* fetch FROM headera given message */
186 int (*fetch_body)(int, struct query *, int, int *);
187 /* fetch a given message */
188 int (*trail)(int, struct query *, int);
189 /* eat trailer of a message */
190 int (*delete)(int, struct query *, int);
192 int (*logout_cmd)(int, struct query *);
194 flag retry; /* can getrange poll for new messages? */
197 struct hostdata /* shared among all user connections to given server */
200 char *pollname; /* poll label of host */
201 char *via; /* "true" server name if non-NULL */
202 struct idlist *akalist; /* server name first, then akas */
203 struct idlist *localdomains; /* list of pass-through domains */
204 int protocol; /* protocol type */
206 char *service; /* IPv6 service name */
207 void *netsec; /* IPv6 security request */
208 #else /* INET6_ENABLE */
209 int port; /* TCP/IP service port number */
210 #endif /* INET6_ENABLE */
211 int interval; /* # cycles to skip between polls */
212 int authenticate; /* authentication mode to try */
213 int timeout; /* inactivity timout in seconds */
214 char *envelope; /* envelope address list header */
215 int envskip; /* skip to numbered envelope header */
216 char *qvirtual; /* prefix removed from local user id */
217 flag skip; /* suppress poll in implicit mode? */
218 flag dns; /* do DNS lookup on multidrop? */
219 flag uidl; /* use RFC1725 UIDLs? */
221 flag sdps; /* use Demon Internet SDPS *ENV */
222 #endif /* SDPS_ENABLE */
223 flag checkalias; /* resolve aliases by comparing IPs? */
224 char *principal; /* Kerberos principal for mail service */
227 #if defined(linux) || defined(__FreeBSD__)
231 struct interface_pair_s *interface_pair;
234 char *plugin,*plugout;
236 /* computed for internal use */
237 const struct method *base_protocol; /* relevant protocol method table */
238 int poll_count; /* count of polls so far */
239 char *queryname; /* name to attempt DNS lookup on */
240 char *truename; /* "true name" of server host */
241 char *trueaddr; /* IP address of truename, as char */
242 struct hostdata *lead_server; /* ptr to lead query for this server */
248 /* mailserver connection controls */
249 struct hostdata server;
252 struct idlist *localnames; /* including calling user's name */
253 int wildcard; /* should unmatched names be passed through */
254 char *remotename; /* remote login name to use */
255 char *password; /* remote password to use */
256 struct idlist *mailboxes; /* list of mailboxes to check */
258 /* per-forwarding-target data */
259 struct idlist *smtphunt; /* list of SMTP hosts to try forwarding to */
260 char *smtpaddress; /* address to force in RCPT TO */
261 char *smtpname; /* full RCPT TO name, including domain */
262 struct idlist *antispam; /* list of listener's antispam response */
263 char *mda; /* local MDA to pass mail to */
264 char *bsmtp; /* BSMTP output file */
265 char listener; /* what's the listener's wire protocol? */
266 #define SMTP_MODE 'S'
267 #define LMTP_MODE 'L'
268 char *preconnect; /* pre-connection command to execute */
269 char *postconnect; /* post-connection command to execute */
271 /* per-user control flags */
272 flag keep; /* if TRUE, leave messages undeleted */
273 flag fetchall; /* if TRUE, fetch all (not just unseen) */
274 flag flush; /* if TRUE, delete messages already seen */
275 flag rewrite; /* if TRUE, canonicalize recipient addresses */
276 flag stripcr; /* if TRUE, strip CRs in text */
277 flag forcecr; /* if TRUE, force CRs before LFs in text */
278 flag pass8bits; /* if TRUE, ignore Content-Transfer-Encoding */
279 flag dropstatus; /* if TRUE, drop Status lines in mail */
280 flag dropdelivered; /* if TRUE, drop Delivered-To lines in mail */
281 flag mimedecode; /* if TRUE, decode MIME-armored messages */
282 flag idle; /* if TRUE, idle after each poll */
283 int limit; /* limit size of retrieved messages */
284 int warnings; /* size warning interval */
285 int fetchlimit; /* max # msgs to get in single poll */
286 int batchlimit; /* max # msgs to pass in single SMTP session */
287 int expunge; /* max # msgs to pass between expunges */
288 flag use_ssl; /* use SSL encrypted session */
289 char *sslkey; /* optional SSL private key file */
290 char *sslcert; /* optional SSL certificate file */
291 char *properties; /* passthrough properties for extensions */
293 /* internal use -- per-poll state */
294 flag active; /* should we actually poll this server? */
295 const char *destaddr; /* destination host for this query */
296 int errcount; /* count transient errors in last pass */
297 int authfailcount; /* count of authorization failures */
298 int wedged; /* wedged by auth failures or timeouts? */
299 char *smtphost; /* actual SMTP host we connected to */
300 int smtp_socket; /* socket descriptor for SMTP connection */
301 unsigned int uid; /* UID of user to deliver to */
302 struct idlist *skipped; /* messages skipped on the mail server */
303 struct idlist *oldsaved, *newsaved;
304 char *lastid; /* last Message-ID seen on this connection */
306 /* internal use -- per-message state */
307 int mimemsg; /* bitmask indicating MIME body-type */
308 char digest [DIGESTLEN]; /* md5 digest buffer */
310 /* internal use -- housekeeping */
311 struct query *next; /* next query control block in chain */
314 struct msgblk /* message header parsed for open_sink() */
316 char *headers; /* raw message headers */
317 struct idlist *recipients; /* addressees */
318 char return_path[HOSTLEN + USERNAMELEN + 4];
324 * Numeric option handling. Numeric option value of zero actually means
325 * it's unspecified. Value less than zero is zero. The reason for this
326 * screwy encoding is so we can zero out an option block in order to set the
327 * numeric flags in it to unspecified.
329 #define NUM_VALUE_IN(n) (((n) == 0) ? -1 : (n))
330 #define NUM_VALUE_OUT(n) (((n) < 0) ? 0 : (n))
331 #define NUM_NONZERO(n) ((n) > 0)
332 #define NUM_ZERO(n) ((n) < 0)
333 #define NUM_SPECIFIED(n) ((n) != 0)
335 #define MULTIDROP(ctl) (ctl->wildcard || \
336 ((ctl)->localnames && (ctl)->localnames->next))
339 * Note: tags are generated with an a%04d format from a 1-origin
340 * integer sequence number. Length 4 permits transaction numbers
341 * up to 9999, so we force rollover with % 10000. There's no special
342 * reason for this format other than to look like the exmples in the
345 #define TAGLEN 6 /* 'a' + 4 digits + NUL */
346 extern char tag[TAGLEN];
349 /* list of hosts assembled from run control file and command line */
350 extern struct query cmd_opts, *querylist;
352 /* what's returned by envquery */
353 extern void envquery(int, char **);
355 /* controls the detail level of status/progress messages written to stderr */
356 extern int outlevel; /* see the O_.* constants above */
357 extern int yydebug; /* enable parse debugging */
359 /* these get computed */
360 extern int batchcount; /* count of messages sent in current batch */
361 extern flag peek_capable; /* can we read msgs without setting seen? */
363 /* miscellaneous global controls */
364 extern struct runctl run; /* global controls for this run */
365 extern flag nodetach; /* if TRUE, don't detach daemon process */
366 extern flag quitmode; /* if --quit was set */
367 extern flag check_only; /* if --check was set */
368 extern char *rcfile; /* path name of rc file */
369 extern int linelimit; /* limit # lines retrieved per site */
370 extern flag versioninfo; /* emit only version info */
371 extern char *user; /* name of invoking user */
372 extern char *home; /* home directory of invoking user */
373 extern char *fmhome; /* fetchmail home directory */
374 extern int pass; /* number of re-polling pass */
375 extern flag configdump; /* dump control blocks as Python dictionary */
376 extern const char *fetchmailhost;
377 /* either "localhost" or an FQDN */
378 extern int suppress_tags; /* suppress tags in tagged protocols? */
380 extern char *sdps_envfrom;
381 extern char *sdps_envto;
382 #endif /* SDPS_ENABLE */
384 /* prototypes for globally callable functions */
386 /* error.c: Error reporting */
387 #if defined(HAVE_STDARG_H)
388 void report_init(int foreground);
389 void report (FILE *fp, const char *format, ...);
390 void report_build (FILE *fp, const char *format, ...);
391 void report_complete (FILE *fp, const char *format, ...);
392 void report_at_line (FILE *fp, int, const char *, unsigned int, const char *, ...);
395 void report_build ();
396 void report_complete ();
397 void report_at_line ();
400 /* driver.c: transaction support */
401 void set_timeout(int);
402 #if defined(HAVE_STDARG_H)
403 void gen_send (int sock, const char *, ... );
404 int gen_recv(int sock, char *buf, int size);
405 int gen_transact (int sock, const char *, ... );
412 /* use these to track what was happening when the nonresponse timer fired */
413 #define GENERAL_WAIT 0 /* unknown wait type */
414 #define OPEN_WAIT 1 /* waiting from mailserver open */
415 #define SERVER_WAIT 2 /* waiting for mailserver response */
416 #define LISTENER_WAIT 3 /* waiting for listener initialization */
417 #define FORWARDING_WAIT 4 /* waiting for listener response */
420 /* response hooks can use this to identify the query stage */
421 #define STAGE_GETAUTH 0
422 #define STAGE_GETRANGE 1
423 #define STAGE_GETSIZES 2
424 #define STAGE_FETCH 3
426 #define STAGE_LOGOUT 5
429 extern int mytimeout;
431 /* mark values for name lists */
432 #define XMIT_ACCEPT 1 /* accepted; matches local domain or name */
433 #define XMIT_REJECT 2 /* rejected; no match */
434 #define XMIT_RCPTBAD 3 /* SMTP listener rejected the name */
437 int interruptible_idle(int interval);
439 /* sink.c: forwarding */
440 int stuffline(struct query *, char *);
441 int open_sink(struct query*, struct msgblk *, int*, int*);
442 void release_sink(struct query *);
443 int close_sink(struct query *, struct msgblk *, flag);
444 int open_warning_by_mail(struct query *, struct msgblk *);
445 #if defined(HAVE_STDARG_H)
446 void stuff_warning(struct query *, const char *, ... );
448 void stuff_warning();
450 void close_warning_by_mail(struct query *, struct msgblk *);
452 /* rfc822.c: RFC822 header parsing */
453 unsigned char *reply_hack(unsigned char *, const unsigned char *);
454 unsigned char *nxtaddr(const unsigned char *);
456 /* uid.c: UID support */
457 void initialize_saved_lists(struct query *, const char *);
458 struct idlist *save_str(struct idlist **, const char *, flag);
459 void free_str_list(struct idlist **);
460 struct idlist *copy_str_list(struct idlist *idl);
461 void save_str_pair(struct idlist **, const char *, const char *);
462 void free_str_pair_list(struct idlist **);
463 int delete_str(struct idlist **, int);
464 int str_in_list(struct idlist **, const char *, const flag);
465 int str_nr_in_list(struct idlist **, const char *);
466 int str_nr_last_in_list(struct idlist **, const char *);
467 void str_set_mark( struct idlist **, const char *, const flag);
468 int count_list( struct idlist **idl );
469 char *str_from_nr_list( struct idlist **idl, int number );
470 char *str_find(struct idlist **, int);
471 char *idpair_find(struct idlist **, const char *);
472 void append_str_list(struct idlist **, struct idlist **);
473 void expunge_uids(struct query *);
474 void uid_swap_lists(struct query *);
475 void write_saved_lists(struct query *, const char *);
478 int prc_parse_file(const char *, const flag);
479 int prc_filecheck(const char *, const flag);
482 void to64frombits(unsigned char *, const unsigned char *, int);
483 int from64tobits(char *, const char *);
486 /* Bit-mask returned by MimeBodyType */
487 #define MSG_IS_7BIT 0x01
488 #define MSG_IS_8BIT 0x02
489 #define MSG_NEEDS_DECODE 0x80
490 extern void UnMimeHeader(unsigned char *buf);
491 extern int MimeBodyType(unsigned char *hdrs, int WantDecode);
492 extern int UnMimeBodyline(unsigned char **buf, flag delimited, flag issoftline);
495 void interface_init(void);
496 void interface_parse(char *, struct hostdata *);
497 void interface_note_activity(struct hostdata *);
498 int interface_approve(struct hostdata *, flag domonitor);
501 #if defined(HAVE_VOIDPOINTER)
502 #define XMALLOCTYPE void
504 #define XMALLOCTYPE char
506 XMALLOCTYPE *xmalloc(int);
507 XMALLOCTYPE *xrealloc(XMALLOCTYPE *, int);
508 char *xstrdup(const char *);
509 #if defined(HAVE_ALLOCA_H)
516 #define xalloca(ptr, t, n) if (!(ptr = (t) alloca(n)))\
517 {report(stderr, _("alloca failed")); exit(PS_UNDEFINED);}
520 * This is a hack to help xgettext which cannot find strings in
521 * macro definitions like the one for xalloca above.
523 static char *dummy = gettext_noop("alloca failed");
526 /* protocol driver and methods */
527 int do_protocol(struct query *, const struct method *);
528 int doPOP2 (struct query *);
529 int doPOP3 (struct query *);
530 int doIMAP (struct query *);
531 int doETRN (struct query *);
533 /* authentication functions */
534 int do_cram_md5(int sock, char *command, struct query *ctl);
535 int do_rfc1731(int sock, char *command, char *truename);
536 int do_gssauth(int sock, char *command, char *hostname, char *username);
537 int do_otp(int sock, char *command, struct query *ctl);
540 struct query *hostalloc(struct query *);
541 int parsecmdline (int, char **, struct runctl *, struct query *);
542 char *MD5Digest (unsigned char *);
543 int POP3_auth_rpa(unsigned char *, unsigned char *, int socket);
544 void deal_with_sigchld(void);
545 int daemonize(const char *, void (*)(int));
546 char *fm_getpassword(char *);
547 void escapes(const char *, char *);
548 char *visbuf(const char *);
549 const char *showproto(int);
550 void dump_config(struct runctl *runp, struct query *querylist);
551 int is_host_alias(const char *, struct query *);
552 char *host_fqdn(void);
553 char *rfc822timestamp(void);
556 void yyerror(const char *);
560 void itimerthread(void*);
561 /* Have to include these first to avoid errors from redefining getcwd
562 and chdir. They're re-include protected in EMX, so it's okay, I
566 /* Redefine getcwd and chdir to get drive-letter support so we can
567 find all of our lock files and stuff. */
568 #define getcwd _getcwd2
569 #define chdir _chdir2
573 # ifndef strerror /* On some systems, strerror is a macro */
576 # endif /* HAVE_STRERROR */
578 #define STRING_DISABLED (char *)-1
579 #define STRING_DUMMY ""
583 #define S_IXGRP 0000010
587 #ifdef FETCHMAIL_DEBUG
588 #define exit(e) do { \
590 out = fopen("/tmp/fetchmail.log", "a"); \
592 "Exiting fetchmail from file %s, line %d with status %d\n", \
593 __FILE__, __LINE__, e); \
597 #endif /* FETCHMAIL_DEBUG */
599 /* fetchmail.h ends here */