]> Pileus Git - ~andy/fetchmail/blob - fetchmail.h
Suppress monitor check after signal wakeup.
[~andy/fetchmail] / fetchmail.h
1 /*
2  * For license terms, see the file COPYING in this directory.
3  */
4
5 /* We need this for HAVE_STDARG_H, etc */
6 #include "config.h"
7
8 /* constants designating the various supported protocols */
9 #define         P_AUTO          1
10 #define         P_POP2          2
11 #define         P_POP3          3
12 #define         P_APOP          4
13 #define         P_RPOP          5
14 #define         P_IMAP          6
15 #define         P_IMAP_K4       7
16 #define         P_IMAP_GSS      8
17 #define         P_IMAP_CRAM_MD5 9
18 #define         P_IMAP_LOGIN    10
19 #define         P_ETRN          11
20
21 #if INET6_ENABLE
22 #define         SMTP_PORT       "smtp"
23 #define         KPOP_PORT       "kpop"
24 #else /* INET6_ENABLE */
25 #define         SMTP_PORT       25
26 #define         KPOP_PORT       1109
27 #endif /* INET6_ENABLE */
28
29 #ifdef SSL_ENABLE
30 #define         SIMAP_PORT      993
31 #define         SPOP3_PORT      995
32 #endif
33
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 */
39
40 /*
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.
43  */
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) */
47
48 /* per RFC1939 this should be 40, but Microsoft Exchange ignores that limit */
49 #define         USERNAMELEN     128     /* max POP3 arg length */
50
51 /* clear a netBSD kernel parameter out of the way */ 
52 #undef          MSGBUFSIZE
53
54 /*
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
58  * lines.
59  */
60 #define         MSGBUFSIZE      8192
61
62 #define         NAMELEN         64      /* max username length */
63 #define         PASSWORDLEN     64      /* max password length */
64 #define         DIGESTLEN       33      /* length of MD5 digest */
65
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) */
87
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
94
95 #define         SIZETICKER      1024    /* print 1 dot per this many bytes */
96
97 /*
98  * We #ifdef this and use flag rather than bool
99  * to avoid a type clash with curses.h
100  */
101 #ifndef TRUE
102 #define FALSE   0
103 #define TRUE    1
104 #endif /* TRUE */
105 typedef char    flag;
106
107 /* we need to use zero as a flag-uninitialized value */
108 #define FLAG_TRUE       2
109 #define FLAG_FALSE      1
110
111 struct runctl
112 {
113     char        *logfile;
114     char        *idfile;
115     int         poll_interval;
116     char        *postmaster;
117     flag        bouncemail;
118     char        *properties;
119     flag        use_syslog;
120     flag        invisible;
121 };
122
123 struct idlist
124 {
125     char *id;
126     union
127     {
128         struct
129         {
130             short       num;
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 */ 
136         }
137         status;
138         char *id2;
139     } val;
140     struct idlist *next;
141 };
142
143 struct query;
144
145 struct method           /* describe methods for protocol state machine */
146 {
147     const char *name;           /* protocol name */
148 #if INET6_ENABLE
149     const char *service;
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);
176                                 /* delete method */
177     int (*logout_cmd)(int, struct query *);
178                                 /* logout command */
179     flag retry;                 /* can getrange poll for new messages? */
180 };
181
182 struct hostdata         /* shared among all user connections to given server */
183 {
184     /* rc file data */
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 */
190 #if INET6_ENABLE
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? */
205 #ifdef SDPS_ENABLE
206     flag sdps;                          /* use Demon Internet SDPS *ENV */
207 #endif /* SDPS_ENABLE */
208     flag checkalias;                    /* resolve aliases by comparing IPs? */
209
210
211 #if defined(linux) || defined(__FreeBSD__)
212     char *interface;
213     char *monitor;
214     int  monitor_io;
215     struct interface_pair_s *interface_pair;
216 #endif /* linux */
217
218     char *plugin,*plugout;
219
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     struct hostdata *lead_server;       /* ptr to lead query for this server */
226     int esmtp_options;
227 };
228
229 struct query
230 {
231     /* mailserver connection controls */
232     struct hostdata server;
233
234     /* per-user data */
235     struct idlist *localnames;  /* including calling user's name */
236     int wildcard;               /* should unmatched names be passed through */
237     char *remotename;           /* remote login name to use */
238     char *password;             /* remote password to use */
239     struct idlist *mailboxes;   /* list of mailboxes to check */
240
241     /* per-forwarding-target data */
242     struct idlist *smtphunt;    /* list of SMTP hosts to try forwarding to */
243     char *smtpaddress;          /* address to force in RCPT TO */ 
244     struct idlist *antispam;    /* list of listener's antispam response */
245     char *mda;                  /* local MDA to pass mail to */
246     char *bsmtp;                /* BSMTP output file */
247     char listener;              /* what's the listener's wire protocol? */
248 #define SMTP_MODE       'S'
249 #define LMTP_MODE       'L'
250     char *preconnect;           /* pre-connection command to execute */
251     char *postconnect;          /* post-connection command to execute */
252
253     /* per-user control flags */
254     flag keep;                  /* if TRUE, leave messages undeleted */
255     flag fetchall;              /* if TRUE, fetch all (not just unseen) */
256     flag flush;                 /* if TRUE, delete messages already seen */
257     flag rewrite;               /* if TRUE, canonicalize recipient addresses */
258     flag stripcr;               /* if TRUE, strip CRs in text */
259     flag forcecr;               /* if TRUE, force CRs before LFs in text */
260     flag pass8bits;             /* if TRUE, ignore Content-Transfer-Encoding */
261     flag dropstatus;            /* if TRUE, drop Status lines in mail */
262     flag mimedecode;            /* if TRUE, decode MIME-armored messages */
263     flag idle;                  /* if TRUE, idle after each poll */
264     int limit;                  /* limit size of retrieved messages */
265     int warnings;               /* size warning interval */
266     int fetchlimit;             /* max # msgs to get in single poll */
267     int batchlimit;             /* max # msgs to pass in single SMTP session */
268     int expunge;                /* max # msgs to pass between expunges */
269     flag use_ssl;               /* use SSL encrypted session */
270     char *sslkey;               /* optional SSL private key file */
271     char *sslcert;              /* optional SSL certificate file */
272     char *properties;           /* passthrough properties for extensions */
273
274     /* internal use -- per-poll state */
275     flag active;                /* should we actually poll this server? */
276     const char *destaddr;       /* destination host for this query */
277     int errcount;               /* count transient errors in last pass */
278     int authfailcount;          /* count of authorization failures */
279     int wedged;                 /* wedged by auth failures or timeouts? */
280     char *smtphost;             /* actual SMTP host we connected to */
281     int smtp_socket;            /* socket descriptor for SMTP connection */
282     unsigned int uid;           /* UID of user to deliver to */
283     struct idlist *skipped;     /* messages skipped on the mail server */
284     struct idlist *oldsaved, *newsaved;
285     char *lastid;               /* last Message-ID seen on this connection */
286
287     /* internal use -- per-message state */
288     int mimemsg;                /* bitmask indicating MIME body-type */
289     char digest [DIGESTLEN];    /* md5 digest buffer */
290
291     /* internal use -- housekeeping */
292     struct query *next;         /* next query control block in chain */
293 };
294
295 struct msgblk                   /* message header parsed for open_sink() */
296 {
297     char                *headers;       /* raw message headers */
298     struct idlist       *recipients;    /* addressees */
299     char                return_path[HOSTLEN + USERNAMELEN + 4]; 
300     int                 reallen;
301 };
302
303
304 /*
305  * Numeric option handling.  Numeric option value of zero actually means
306  * it's unspecified.  Value less than zero is zero.  The reason for this
307  * screwy encoding is so we can zero out an option block in order to set the
308  * numeric flags in it to unspecified.
309  */
310 #define NUM_VALUE_IN(n)         (((n) == 0) ? -1 : (n))
311 #define NUM_VALUE_OUT(n)        (((n) < 0) ? 0 : (n))
312 #define NUM_NONZERO(n)          ((n) > 0)
313 #define NUM_ZERO(n)             ((n) < 0)
314 #define NUM_SPECIFIED(n)        ((n) != 0)
315
316 #define MULTIDROP(ctl)  (ctl->wildcard || \
317                                 ((ctl)->localnames && (ctl)->localnames->next))
318
319 /*
320  * Note: tags are generated with an a%04d format from a 1-origin
321  * integer sequence number.  Length 4 permits transaction numbers
322  * up to 9999, so we force rollover with % 10000.  There's no special
323  * reason for this format other than to look like the exmples in the
324  * IMAP RFCs.
325  */
326 #define TAGLEN  6               /* 'a' + 4 digits + NUL */
327 extern char tag[TAGLEN];
328 #define TAGMOD  10000
329
330 /* list of hosts assembled from run control file and command line */
331 extern struct query cmd_opts, *querylist;
332
333 /* what's returned by envquery */
334 extern void envquery(int, char **);
335
336 /* controls the detail level of status/progress messages written to stderr */
337 extern int outlevel;            /* see the O_.* constants above */
338 extern int yydebug;             /* enable parse debugging */
339
340 /* these get computed */
341 extern int batchcount;          /* count of messages sent in current batch */
342 extern flag peek_capable;       /* can we read msgs without setting seen? */
343
344 /* miscellaneous global controls */
345 extern struct runctl run;       /* global controls for this run */
346 extern flag nodetach;           /* if TRUE, don't detach daemon process */
347 extern flag quitmode;           /* if --quit was set */
348 extern flag check_only;         /* if --check was set */
349 extern char *rcfile;            /* path name of rc file */
350 extern int linelimit;           /* limit # lines retrieved per site */
351 extern flag versioninfo;        /* emit only version info */
352 extern char *user;              /* name of invoking user */
353 extern char *home;              /* home directory of invoking user */
354 extern int pass;                /* number of re-polling pass */
355 extern flag configdump;         /* dump control blocks as Python dictionary */
356 extern const char *fetchmailhost;
357                                 /* either "localhost" or an FQDN */
358 #ifdef SDPS_ENABLE
359 extern char *sdps_envfrom;
360 extern char *sdps_envto;
361 #endif /* SDPS_ENABLE */
362
363 /* prototypes for globally callable functions */
364
365 /* error.c: Error reporting */
366 #if defined(HAVE_STDARG_H)
367 void report_init(int foreground);
368 void report (FILE *fp, const char *format, ...);
369 void report_build (FILE *fp, const char *format, ...);
370 void report_complete (FILE *fp, const char *format, ...);
371 void report_at_line (FILE *fp, int, const char *, unsigned int, const char *, ...);
372 #else
373 void report ();
374 void report_build ();
375 void report_complete ();
376 void report_at_line ();
377 #endif
378
379 /* driver.c: transaction support */
380 void set_timeout(int);
381 #if defined(HAVE_STDARG_H)
382 void gen_send (int sock, const char *, ... );
383 int gen_recv(int sock, char *buf, int size);
384 int gen_transact (int sock, const char *, ... );
385 #else
386 void gen_send ();
387 int gen_recv();
388 int gen_transact ();
389 #endif
390
391 /* use these to track what was happening when the nonresponse timer fired */
392 #define GENERAL_WAIT    0       /* unknown wait type */
393 #define OPEN_WAIT       1       /* waiting from mailserver open */
394 #define SERVER_WAIT     2       /* waiting for mailserver response */
395 #define LISTENER_WAIT   3       /* waiting for listener initialization */
396 #define FORWARDING_WAIT 4       /* waiting for listener response */
397 extern int phase;
398
399 /* response hooks can use this to identify the query stage */
400 #define STAGE_GETAUTH   0
401 #define STAGE_GETRANGE  1
402 #define STAGE_GETSIZES  2
403 #define STAGE_FETCH     3
404 #define STAGE_LOGOUT    4
405 extern int stage;
406
407 extern int mytimeout;
408
409 /* mark values for name lists */
410 #define XMIT_ACCEPT     1       /* accepted; matches local domain or name */
411 #define XMIT_REJECT     2       /* rejected; no match */
412 #define XMIT_RCPTBAD    3       /* SMTP listener rejected the name */ 
413
414 /* idle.c */
415 int interruptible_idle(int interval);
416
417 /* sink.c: forwarding */
418 int stuffline(struct query *, char *);
419 int open_sink(struct query*, struct msgblk *, int*, int*);
420 void release_sink(struct query *);
421 int close_sink(struct query *, struct msgblk *, flag);
422 int open_warning_by_mail(struct query *, struct msgblk *);
423 #if defined(HAVE_STDARG_H)
424 void stuff_warning(struct query *, const char *, ... );
425 #else
426 void stuff_warning();
427 #endif
428 void close_warning_by_mail(struct query *, struct msgblk *);
429
430 /* rfc822.c: RFC822 header parsing */
431 char *reply_hack(char *, const char *);
432 char *nxtaddr(const char *);
433
434 /* uid.c: UID support */
435 void initialize_saved_lists(struct query *, const char *);
436 struct idlist *save_str(struct idlist **, const char *, flag);
437 void free_str_list(struct idlist **);
438 struct idlist *copy_str_list(struct idlist *idl);
439 void save_str_pair(struct idlist **, const char *, const char *);
440 void free_str_pair_list(struct idlist **);
441 int delete_str(struct idlist **, int);
442 int str_in_list(struct idlist **, const char *, const flag);
443 int str_nr_in_list(struct idlist **, const char *);
444 int str_nr_last_in_list(struct idlist **, const char *);
445 void str_set_mark( struct idlist **, const char *, const flag);
446 int count_list( struct idlist **idl );
447 char *str_from_nr_list( struct idlist **idl, int number );
448 char *str_find(struct idlist **, int);
449 char *idpair_find(struct idlist **, const char *);
450 void append_str_list(struct idlist **, struct idlist **);
451 void expunge_uids(struct query *);
452 void uid_end_query(struct query *);
453 void write_saved_lists(struct query *, const char *);
454
455 /* rcfile_y.y */
456 int prc_parse_file(const char *, const flag);
457 int prc_filecheck(const char *, const flag);
458
459 /* base64.c */
460 void to64frombits(unsigned char *, const unsigned char *, int);
461 int from64tobits(char *, const char *);
462
463 /* unmime.c */
464 /* Bit-mask returned by MimeBodyType */
465 #define MSG_IS_7BIT       0x01
466 #define MSG_IS_8BIT       0x02
467 #define MSG_NEEDS_DECODE  0x80
468 extern void UnMimeHeader(unsigned char *buf);
469 extern int  MimeBodyType(unsigned char *hdrs, int WantDecode);
470 extern int  UnMimeBodyline(unsigned char **buf, flag delimited, flag issoftline);
471
472 /* interface.c */
473 void interface_init(void);
474 void interface_parse(char *, struct hostdata *);
475 void interface_note_activity(struct hostdata *);
476 int interface_approve(struct hostdata *, flag domonitor);
477
478 /* xmalloc.c */
479 #if defined(HAVE_VOIDPOINTER)
480 #define XMALLOCTYPE void
481 #else
482 #define XMALLOCTYPE char
483 #endif
484 XMALLOCTYPE *xmalloc(int);
485 XMALLOCTYPE *xrealloc(XMALLOCTYPE *, int);
486 char *xstrdup(const char *);
487 #if defined(HAVE_ALLOCA_H)
488 #include <alloca.h>
489 #else
490 #ifdef _AIX
491  #pragma alloca
492 #endif
493 #endif
494 #define xalloca(ptr, t, n)      if (!(ptr = (t) alloca(n))) {report(stderr, "alloca failed"); exit(PS_UNDEFINED);}
495
496 /* protocol driver and methods */
497 int do_protocol(struct query *, const struct method *);
498 int doPOP2 (struct query *); 
499 int doPOP3 (struct query *);
500 int doIMAP (struct query *);
501 int doETRN (struct query *);
502
503 /* miscellanea */
504 struct query *hostalloc(struct query *); 
505 int parsecmdline (int, char **, struct runctl *, struct query *);
506 char *MD5Digest (unsigned char *);
507 int POP3_auth_rpa(unsigned char *, unsigned char *, int socket);
508 int daemonize(const char *, void (*)(int));
509 char *getpassword(char *);
510 void escapes(const char *, char *);
511 char *visbuf(const char *);
512 const char *showproto(int);
513 void dump_config(struct runctl *runp, struct query *querylist);
514 int is_host_alias(const char *, struct query *);
515 char *host_fqdn(void);
516 char *rfc822timestamp(void);
517
518 void yyerror(const char *);
519 int yylex(void);
520
521 #ifdef __EMX__
522 void itimerthread(void*);
523 /* Have to include these first to avoid errors from redefining getcwd
524    and chdir.  They're re-include protected in EMX, so it's okay, I
525    guess.  */
526 #include <stdlib.h>
527 #include <unistd.h>
528 /* Redefine getcwd and chdir to get drive-letter support so we can
529    find all of our lock files and stuff. */
530 #define getcwd _getcwd2
531 #define chdir _chdir2
532 #endif /* _EMX_ */
533
534 # if HAVE_STRERROR
535 #  ifndef strerror              /* On some systems, strerror is a macro */
536 char *strerror ();
537 #  endif
538 # endif /* HAVE_STRERROR */
539
540 #define STRING_DISABLED (char *)-1
541 #define STRING_DUMMY    ""
542
543 #ifdef NeXT
544 #ifndef S_IXGRP
545 #define S_IXGRP 0000010
546 #endif
547 #endif
548
549 /* fetchmail.h ends here */