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 */
17 #define P_IMAP_CRAM_MD5 9
18 #define P_IMAP_LOGIN 10
22 #define SMTP_PORT "smtp"
23 #define KPOP_PORT "kpop"
24 #else /* INET6_ENABLE */
26 #define KPOP_PORT 1109
27 #endif /* INET6_ENABLE */
30 #define SIMAP_PORT 993
31 #define SPOP3_PORT 995
34 /* preauthentication types */
35 #define A_PASSWORD 0 /* password or inline authentication */
36 #define A_KERBEROS_V4 1 /* preauthenticate w/ Kerberos V4 */
37 #define A_KERBEROS_V5 2 /* preauthenticate w/ Kerberos V5 */
38 #define A_SSH 3 /* preauthentication at session level */
41 * Definitions for buffer sizes. We get little help on setting maxima
42 * from IMAP RFCs up to 2060, so these are mostly from POP3.
44 #define HOSTLEN 635 /* max hostname length (RFC1123) */
45 #define POPBUFSIZE 512 /* max length of response (RFC1939) */
46 #define IDLEN 128 /* max length of UID (RFC1939) */
48 /* per RFC1939 this should be 40, but Microsoft Exchange ignores that limit */
49 #define USERNAMELEN 128 /* max POP3 arg length */
51 /* clear a netBSD kernel parameter out of the way */
55 * The RFC822 limit on message line size is just 998. But
56 * make this *way* oversized; idiot DOS-world mailers that
57 * don't line-wrap properly often ship entire paragraphs as
60 #define MSGBUFSIZE 8192
62 #define NAMELEN 64 /* max username length */
63 #define PASSWORDLEN 64 /* max password length */
64 #define DIGESTLEN 33 /* length of MD5 digest */
66 /* exit code values */
67 #define PS_SUCCESS 0 /* successful receipt of messages */
68 #define PS_NOMAIL 1 /* no mail available */
69 #define PS_SOCKET 2 /* socket I/O woes */
70 #define PS_AUTHFAIL 3 /* user authorization failed */
71 #define PS_PROTOCOL 4 /* protocol violation */
72 #define PS_SYNTAX 5 /* command-line syntax error */
73 #define PS_IOERR 6 /* bad permissions on rc file */
74 #define PS_ERROR 7 /* protocol error */
75 #define PS_EXCLUDE 8 /* client-side exclusion error */
76 #define PS_LOCKBUSY 9 /* server responded lock busy */
77 #define PS_SMTP 10 /* SMTP error */
78 #define PS_DNS 11 /* fatal DNS error */
79 #define PS_BSMTP 12 /* output batch could not be opened */
80 #define PS_MAXFETCH 13 /* poll ended by fetch limit */
81 /* leave space for more codes */
82 #define PS_UNDEFINED 23 /* something I hadn't thought of */
83 #define PS_TRANSIENT 24 /* transient failure (internal use) */
84 #define PS_REFUSED 25 /* mail refused (internal use) */
85 #define PS_RETAINED 26 /* message retained (internal use) */
86 #define PS_TRUNCATED 27 /* headers incomplete (internal use) */
88 /* output noise level */
89 #define O_SILENT 0 /* mute, max squelch, etc. */
90 #define O_NORMAL 1 /* user-friendly */
91 #define O_VERBOSE 2 /* chatty */
92 #define O_DEBUG 3 /* prolix */
93 #define O_MONITOR O_VERBOSE
95 #define SIZETICKER 1024 /* print 1 dot per this many bytes */
98 * We #ifdef this and use flag rather than bool
99 * to avoid a type clash with curses.h
107 /* we need to use zero as a flag-uninitialized value */
131 flag mark; /* UID-index information */
132 #define UID_UNSEEN 0 /* hasn't been seen */
133 #define UID_SEEN 1 /* seen, but not deleted */
134 #define UID_DELETED 2 /* this message has been deleted */
135 #define UID_EXPUNGED 3 /* this message has been expunged */
145 struct method /* describe methods for protocol state machine */
147 const char *name; /* protocol name */
150 const char *sslservice;
151 #else /* INET6_ENABLE */
152 int port; /* service port */
153 int sslport; /* service port for ssl */
154 #endif /* INET6_ENABLE */
155 flag tagged; /* if true, generate & expect command tags */
156 flag delimited; /* if true, accept "." message delimiter */
157 int (*parse_response)(int, char *);
158 /* response_parsing function */
159 int (*password_canonify)(char *, char *, int);
160 /* canonicalize password */
161 int (*getauth)(int, struct query *, char *);
162 /* authorization fetcher */
163 int (*getrange)(int, struct query *, const char *, int *, int *, int *);
164 /* get message range to fetch */
165 int (*getsizes)(int, int, int *);
166 /* get sizes of messages */
167 int (*is_old)(int, struct query *, int);
168 /* check for old message */
169 int (*fetch_headers)(int, struct query *, int, int *);
170 /* fetch FROM headera given message */
171 int (*fetch_body)(int, struct query *, int, int *);
172 /* fetch a given message */
173 int (*trail)(int, struct query *, int);
174 /* eat trailer of a message */
175 int (*delete)(int, struct query *, int);
177 int (*logout_cmd)(int, struct query *);
179 flag retry; /* can getrange poll for new messages? */
182 struct hostdata /* shared among all user connections to given server */
185 char *pollname; /* poll label of host */
186 char *via; /* "true" server name if non-NULL */
187 struct idlist *akalist; /* server name first, then akas */
188 struct idlist *localdomains; /* list of pass-through domains */
189 int protocol; /* protocol type */
191 char *service; /* IPv6 service name */
192 void *netsec; /* IPv6 security request */
193 #else /* INET6_ENABLE */
194 int port; /* TCP/IP service port number */
195 #endif /* INET6_ENABLE */
196 int interval; /* # cycles to skip between polls */
197 int preauthenticate; /* preauthentication mode to try */
198 int timeout; /* inactivity timout in seconds */
199 char *envelope; /* envelope address list header */
200 int envskip; /* skip to numbered envelope header */
201 char *qvirtual; /* prefix removed from local user id */
202 flag skip; /* suppress poll in implicit mode? */
203 flag dns; /* do DNS lookup on multidrop? */
204 flag uidl; /* use RFC1725 UIDLs? */
206 flag sdps; /* use Demon Internet SDPS *ENV */
207 #endif /* SDPS_ENABLE */
208 flag checkalias; /* resolve aliases by comparing IPs? */
211 #if defined(linux) || defined(__FreeBSD__)
215 struct interface_pair_s *interface_pair;
218 char *plugin,*plugout;
220 /* computed for internal use */
221 const struct method *base_protocol; /* relevant protocol method table */
222 int poll_count; /* count of polls so far */
223 char *queryname; /* name to attempt DNS lookup on */
224 char *truename; /* "true name" of server host */
225 char *trueaddr; /* IP address of truename, as char */
226 struct hostdata *lead_server; /* ptr to lead query for this server */
232 /* mailserver connection controls */
233 struct hostdata server;
236 struct idlist *localnames; /* including calling user's name */
237 int wildcard; /* should unmatched names be passed through */
238 char *remotename; /* remote login name to use */
239 char *password; /* remote password to use */
240 struct idlist *mailboxes; /* list of mailboxes to check */
242 /* per-forwarding-target data */
243 struct idlist *smtphunt; /* list of SMTP hosts to try forwarding to */
244 char *smtpaddress; /* address to force in RCPT TO */
245 char *smtpname; /* full RCPT TO name, including domain */
246 struct idlist *antispam; /* list of listener's antispam response */
247 char *mda; /* local MDA to pass mail to */
248 char *bsmtp; /* BSMTP output file */
249 char listener; /* what's the listener's wire protocol? */
250 #define SMTP_MODE 'S'
251 #define LMTP_MODE 'L'
252 char *preconnect; /* pre-connection command to execute */
253 char *postconnect; /* post-connection command to execute */
255 /* per-user control flags */
256 flag keep; /* if TRUE, leave messages undeleted */
257 flag fetchall; /* if TRUE, fetch all (not just unseen) */
258 flag flush; /* if TRUE, delete messages already seen */
259 flag rewrite; /* if TRUE, canonicalize recipient addresses */
260 flag stripcr; /* if TRUE, strip CRs in text */
261 flag forcecr; /* if TRUE, force CRs before LFs in text */
262 flag pass8bits; /* if TRUE, ignore Content-Transfer-Encoding */
263 flag dropstatus; /* if TRUE, drop Status lines in mail */
264 flag mimedecode; /* if TRUE, decode MIME-armored messages */
265 flag idle; /* if TRUE, idle after each poll */
266 int limit; /* limit size of retrieved messages */
267 int warnings; /* size warning interval */
268 int fetchlimit; /* max # msgs to get in single poll */
269 int batchlimit; /* max # msgs to pass in single SMTP session */
270 int expunge; /* max # msgs to pass between expunges */
271 flag use_ssl; /* use SSL encrypted session */
272 char *sslkey; /* optional SSL private key file */
273 char *sslcert; /* optional SSL certificate file */
274 char *properties; /* passthrough properties for extensions */
276 /* internal use -- per-poll state */
277 flag active; /* should we actually poll this server? */
278 const char *destaddr; /* destination host for this query */
279 int errcount; /* count transient errors in last pass */
280 int authfailcount; /* count of authorization failures */
281 int wedged; /* wedged by auth failures or timeouts? */
282 char *smtphost; /* actual SMTP host we connected to */
283 int smtp_socket; /* socket descriptor for SMTP connection */
284 unsigned int uid; /* UID of user to deliver to */
285 struct idlist *skipped; /* messages skipped on the mail server */
286 struct idlist *oldsaved, *newsaved;
287 char *lastid; /* last Message-ID seen on this connection */
289 /* internal use -- per-message state */
290 int mimemsg; /* bitmask indicating MIME body-type */
291 char digest [DIGESTLEN]; /* md5 digest buffer */
293 /* internal use -- housekeeping */
294 struct query *next; /* next query control block in chain */
297 struct msgblk /* message header parsed for open_sink() */
299 char *headers; /* raw message headers */
300 struct idlist *recipients; /* addressees */
301 char return_path[HOSTLEN + USERNAMELEN + 4];
307 * Numeric option handling. Numeric option value of zero actually means
308 * it's unspecified. Value less than zero is zero. The reason for this
309 * screwy encoding is so we can zero out an option block in order to set the
310 * numeric flags in it to unspecified.
312 #define NUM_VALUE_IN(n) (((n) == 0) ? -1 : (n))
313 #define NUM_VALUE_OUT(n) (((n) < 0) ? 0 : (n))
314 #define NUM_NONZERO(n) ((n) > 0)
315 #define NUM_ZERO(n) ((n) < 0)
316 #define NUM_SPECIFIED(n) ((n) != 0)
318 #define MULTIDROP(ctl) (ctl->wildcard || \
319 ((ctl)->localnames && (ctl)->localnames->next))
322 * Note: tags are generated with an a%04d format from a 1-origin
323 * integer sequence number. Length 4 permits transaction numbers
324 * up to 9999, so we force rollover with % 10000. There's no special
325 * reason for this format other than to look like the exmples in the
328 #define TAGLEN 6 /* 'a' + 4 digits + NUL */
329 extern char tag[TAGLEN];
332 /* list of hosts assembled from run control file and command line */
333 extern struct query cmd_opts, *querylist;
335 /* what's returned by envquery */
336 extern void envquery(int, char **);
338 /* controls the detail level of status/progress messages written to stderr */
339 extern int outlevel; /* see the O_.* constants above */
340 extern int yydebug; /* enable parse debugging */
342 /* these get computed */
343 extern int batchcount; /* count of messages sent in current batch */
344 extern flag peek_capable; /* can we read msgs without setting seen? */
346 /* miscellaneous global controls */
347 extern struct runctl run; /* global controls for this run */
348 extern flag nodetach; /* if TRUE, don't detach daemon process */
349 extern flag quitmode; /* if --quit was set */
350 extern flag check_only; /* if --check was set */
351 extern char *rcfile; /* path name of rc file */
352 extern int linelimit; /* limit # lines retrieved per site */
353 extern flag versioninfo; /* emit only version info */
354 extern char *user; /* name of invoking user */
355 extern char *home; /* home directory of invoking user */
356 extern char *fmhome; /* fetchmail home directory */
357 extern int pass; /* number of re-polling pass */
358 extern flag configdump; /* dump control blocks as Python dictionary */
359 extern const char *fetchmailhost;
360 /* either "localhost" or an FQDN */
362 extern char *sdps_envfrom;
363 extern char *sdps_envto;
364 #endif /* SDPS_ENABLE */
366 /* prototypes for globally callable functions */
368 /* error.c: Error reporting */
369 #if defined(HAVE_STDARG_H)
370 void report_init(int foreground);
371 void report (FILE *fp, const char *format, ...);
372 void report_build (FILE *fp, const char *format, ...);
373 void report_complete (FILE *fp, const char *format, ...);
374 void report_at_line (FILE *fp, int, const char *, unsigned int, const char *, ...);
377 void report_build ();
378 void report_complete ();
379 void report_at_line ();
382 /* driver.c: transaction support */
383 void set_timeout(int);
384 #if defined(HAVE_STDARG_H)
385 void gen_send (int sock, const char *, ... );
386 int gen_recv(int sock, char *buf, int size);
387 int gen_transact (int sock, const char *, ... );
394 /* use these to track what was happening when the nonresponse timer fired */
395 #define GENERAL_WAIT 0 /* unknown wait type */
396 #define OPEN_WAIT 1 /* waiting from mailserver open */
397 #define SERVER_WAIT 2 /* waiting for mailserver response */
398 #define LISTENER_WAIT 3 /* waiting for listener initialization */
399 #define FORWARDING_WAIT 4 /* waiting for listener response */
402 /* response hooks can use this to identify the query stage */
403 #define STAGE_GETAUTH 0
404 #define STAGE_GETRANGE 1
405 #define STAGE_GETSIZES 2
406 #define STAGE_FETCH 3
408 #define STAGE_LOGOUT 5
411 extern int mytimeout;
413 /* mark values for name lists */
414 #define XMIT_ACCEPT 1 /* accepted; matches local domain or name */
415 #define XMIT_REJECT 2 /* rejected; no match */
416 #define XMIT_RCPTBAD 3 /* SMTP listener rejected the name */
419 int interruptible_idle(int interval);
421 /* sink.c: forwarding */
422 int stuffline(struct query *, char *);
423 int open_sink(struct query*, struct msgblk *, int*, int*);
424 void release_sink(struct query *);
425 int close_sink(struct query *, struct msgblk *, flag);
426 int open_warning_by_mail(struct query *, struct msgblk *);
427 #if defined(HAVE_STDARG_H)
428 void stuff_warning(struct query *, const char *, ... );
430 void stuff_warning();
432 void close_warning_by_mail(struct query *, struct msgblk *);
434 /* rfc822.c: RFC822 header parsing */
435 char *reply_hack(char *, const char *);
436 char *nxtaddr(const char *);
438 /* uid.c: UID support */
439 void initialize_saved_lists(struct query *, const char *);
440 struct idlist *save_str(struct idlist **, const char *, flag);
441 void free_str_list(struct idlist **);
442 struct idlist *copy_str_list(struct idlist *idl);
443 void save_str_pair(struct idlist **, const char *, const char *);
444 void free_str_pair_list(struct idlist **);
445 int delete_str(struct idlist **, int);
446 int str_in_list(struct idlist **, const char *, const flag);
447 int str_nr_in_list(struct idlist **, const char *);
448 int str_nr_last_in_list(struct idlist **, const char *);
449 void str_set_mark( struct idlist **, const char *, const flag);
450 int count_list( struct idlist **idl );
451 char *str_from_nr_list( struct idlist **idl, int number );
452 char *str_find(struct idlist **, int);
453 char *idpair_find(struct idlist **, const char *);
454 void append_str_list(struct idlist **, struct idlist **);
455 void expunge_uids(struct query *);
456 void uid_end_query(struct query *);
457 void write_saved_lists(struct query *, const char *);
460 int prc_parse_file(const char *, const flag);
461 int prc_filecheck(const char *, const flag);
464 void to64frombits(unsigned char *, const unsigned char *, int);
465 int from64tobits(char *, const char *);
468 /* Bit-mask returned by MimeBodyType */
469 #define MSG_IS_7BIT 0x01
470 #define MSG_IS_8BIT 0x02
471 #define MSG_NEEDS_DECODE 0x80
472 extern void UnMimeHeader(unsigned char *buf);
473 extern int MimeBodyType(unsigned char *hdrs, int WantDecode);
474 extern int UnMimeBodyline(unsigned char **buf, flag delimited, flag issoftline);
477 void interface_init(void);
478 void interface_parse(char *, struct hostdata *);
479 void interface_note_activity(struct hostdata *);
480 int interface_approve(struct hostdata *, flag domonitor);
483 #if defined(HAVE_VOIDPOINTER)
484 #define XMALLOCTYPE void
486 #define XMALLOCTYPE char
488 XMALLOCTYPE *xmalloc(int);
489 XMALLOCTYPE *xrealloc(XMALLOCTYPE *, int);
490 char *xstrdup(const char *);
491 #if defined(HAVE_ALLOCA_H)
498 #define xalloca(ptr, t, n) if (!(ptr = (t) alloca(n))) {report(stderr, "alloca failed"); exit(PS_UNDEFINED);}
500 /* protocol driver and methods */
501 int do_protocol(struct query *, const struct method *);
502 int doPOP2 (struct query *);
503 int doPOP3 (struct query *);
504 int doIMAP (struct query *);
505 int doETRN (struct query *);
508 struct query *hostalloc(struct query *);
509 int parsecmdline (int, char **, struct runctl *, struct query *);
510 char *MD5Digest (unsigned char *);
511 int POP3_auth_rpa(unsigned char *, unsigned char *, int socket);
512 int daemonize(const char *, void (*)(int));
513 char *fm_getpassword(char *);
514 void escapes(const char *, char *);
515 char *visbuf(const char *);
516 const char *showproto(int);
517 void dump_config(struct runctl *runp, struct query *querylist);
518 int is_host_alias(const char *, struct query *);
519 char *host_fqdn(void);
520 char *rfc822timestamp(void);
523 void yyerror(const char *);
527 void itimerthread(void*);
528 /* Have to include these first to avoid errors from redefining getcwd
529 and chdir. They're re-include protected in EMX, so it's okay, I
533 /* Redefine getcwd and chdir to get drive-letter support so we can
534 find all of our lock files and stuff. */
535 #define getcwd _getcwd2
536 #define chdir _chdir2
540 # ifndef strerror /* On some systems, strerror is a macro */
543 # endif /* HAVE_STRERROR */
545 #define STRING_DISABLED (char *)-1
546 #define STRING_DUMMY ""
550 #define S_IXGRP 0000010
554 /* fetchmail.h ends here */