]> Pileus Git - ~andy/fetchmail/blob - fetchmail.h
Spambounce patch.
[~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     flag        spambounce;
119     char        *properties;
120     flag        use_syslog;
121     flag        invisible;
122     flag        showdots;
123 };
124
125 struct idlist
126 {
127     unsigned char *id;
128     union
129     {
130         struct
131         {
132             short       num;
133             flag        mark;           /* UID-index information */
134 #define UID_UNSEEN      0               /* hasn't been seen */
135 #define UID_SEEN        1               /* seen, but not deleted */
136 #define UID_DELETED     2               /* this message has been deleted */
137 #define UID_EXPUNGED    3               /* this message has been expunged */ 
138         }
139         status;
140         unsigned char *id2;
141     } val;
142     struct idlist *next;
143 };
144
145 struct query;
146
147 struct method           /* describe methods for protocol state machine */
148 {
149     const char *name;           /* protocol name */
150 #if INET6_ENABLE
151     const char *service;
152     const char *sslservice;
153 #else /* INET6_ENABLE */
154     int port;                   /* service port */
155     int sslport;                /* service port for ssl */
156 #endif /* INET6_ENABLE */
157     flag tagged;                /* if true, generate & expect command tags */
158     flag delimited;             /* if true, accept "." message delimiter */
159     int (*parse_response)(int, char *);
160                                 /* response_parsing function */
161     int (*password_canonify)(char *, char *, int);
162                                 /* canonicalize password */
163     int (*getauth)(int, struct query *, char *);
164                                 /* authorization fetcher */
165     int (*getrange)(int, struct query *, const char *, int *, int *, int *);
166                                 /* get message range to fetch */
167     int (*getsizes)(int, int, int *);
168                                 /* get sizes of messages */
169     int (*is_old)(int, struct query *, int);
170                                 /* check for old message */
171     int (*fetch_headers)(int, struct query *, int, int *);
172                                 /* fetch FROM headera given message */
173     int (*fetch_body)(int, struct query *, int, int *);
174                                 /* fetch a given message */
175     int (*trail)(int, struct query *, int);
176                                 /* eat trailer of a message */
177     int (*delete)(int, struct query *, int);
178                                 /* delete method */
179     int (*logout_cmd)(int, struct query *);
180                                 /* logout command */
181     flag retry;                 /* can getrange poll for new messages? */
182 };
183
184 struct hostdata         /* shared among all user connections to given server */
185 {
186     /* rc file data */
187     char *pollname;                     /* poll label of host */
188     char *via;                          /* "true" server name if non-NULL */
189     struct idlist *akalist;             /* server name first, then akas */
190     struct idlist *localdomains;        /* list of pass-through domains */
191     int protocol;                       /* protocol type */
192 #if INET6_ENABLE
193     char *service;                      /* IPv6 service name */
194     void *netsec;                       /* IPv6 security request */
195 #else /* INET6_ENABLE */
196     int port;                           /* TCP/IP service port number */
197 #endif /* INET6_ENABLE */
198     int interval;                       /* # cycles to skip between polls */
199     int preauthenticate;                /* preauthentication mode to try */
200     int timeout;                        /* inactivity timout in seconds */
201     char *envelope;                     /* envelope address list header */
202     int envskip;                        /* skip to numbered envelope header */
203     char *qvirtual;                     /* prefix removed from local user id */
204     flag skip;                          /* suppress poll in implicit mode? */
205     flag dns;                           /* do DNS lookup on multidrop? */
206     flag uidl;                          /* use RFC1725 UIDLs? */
207 #ifdef SDPS_ENABLE
208     flag sdps;                          /* use Demon Internet SDPS *ENV */
209 #endif /* SDPS_ENABLE */
210     flag checkalias;                    /* resolve aliases by comparing IPs? */
211     char *principal;                    /* Kerberos principal for mail service */
212
213
214 #if defined(linux) || defined(__FreeBSD__)
215     char *interface;
216     char *monitor;
217     int  monitor_io;
218     struct interface_pair_s *interface_pair;
219 #endif /* linux */
220
221     char *plugin,*plugout;
222
223     /* computed for internal use */
224     const struct method *base_protocol; /* relevant protocol method table */
225     int poll_count;                     /* count of polls so far */
226     char *queryname;                    /* name to attempt DNS lookup on */
227     char *truename;                     /* "true name" of server host */
228     char *trueaddr;                     /* IP address of truename, as char */
229     struct hostdata *lead_server;       /* ptr to lead query for this server */
230     int esmtp_options;
231 };
232
233 struct query
234 {
235     /* mailserver connection controls */
236     struct hostdata server;
237
238     /* per-user data */
239     struct idlist *localnames;  /* including calling user's name */
240     int wildcard;               /* should unmatched names be passed through */
241     char *remotename;           /* remote login name to use */
242     char *password;             /* remote password to use */
243     struct idlist *mailboxes;   /* list of mailboxes to check */
244
245     /* per-forwarding-target data */
246     struct idlist *smtphunt;    /* list of SMTP hosts to try forwarding to */
247     char *smtpaddress;          /* address to force in RCPT TO */ 
248     char *smtpname;             /* full RCPT TO name, including domain */
249     struct idlist *antispam;    /* list of listener's antispam response */
250     char *mda;                  /* local MDA to pass mail to */
251     char *bsmtp;                /* BSMTP output file */
252     char listener;              /* what's the listener's wire protocol? */
253 #define SMTP_MODE       'S'
254 #define LMTP_MODE       'L'
255     char *preconnect;           /* pre-connection command to execute */
256     char *postconnect;          /* post-connection command to execute */
257
258     /* per-user control flags */
259     flag keep;                  /* if TRUE, leave messages undeleted */
260     flag fetchall;              /* if TRUE, fetch all (not just unseen) */
261     flag flush;                 /* if TRUE, delete messages already seen */
262     flag rewrite;               /* if TRUE, canonicalize recipient addresses */
263     flag stripcr;               /* if TRUE, strip CRs in text */
264     flag forcecr;               /* if TRUE, force CRs before LFs in text */
265     flag pass8bits;             /* if TRUE, ignore Content-Transfer-Encoding */
266     flag dropstatus;            /* if TRUE, drop Status lines in mail */
267     flag dropdelivered;         /* if TRUE, drop Delivered-To lines in mail */
268     flag mimedecode;            /* if TRUE, decode MIME-armored messages */
269     flag idle;                  /* if TRUE, idle after each poll */
270     int limit;                  /* limit size of retrieved messages */
271     int warnings;               /* size warning interval */
272     int fetchlimit;             /* max # msgs to get in single poll */
273     int batchlimit;             /* max # msgs to pass in single SMTP session */
274     int expunge;                /* max # msgs to pass between expunges */
275     flag use_ssl;               /* use SSL encrypted session */
276     char *sslkey;               /* optional SSL private key file */
277     char *sslcert;              /* optional SSL certificate file */
278     char *properties;           /* passthrough properties for extensions */
279
280     /* internal use -- per-poll state */
281     flag active;                /* should we actually poll this server? */
282     const char *destaddr;       /* destination host for this query */
283     int errcount;               /* count transient errors in last pass */
284     int authfailcount;          /* count of authorization failures */
285     int wedged;                 /* wedged by auth failures or timeouts? */
286     char *smtphost;             /* actual SMTP host we connected to */
287     int smtp_socket;            /* socket descriptor for SMTP connection */
288     unsigned int uid;           /* UID of user to deliver to */
289     struct idlist *skipped;     /* messages skipped on the mail server */
290     struct idlist *oldsaved, *newsaved;
291     char *lastid;               /* last Message-ID seen on this connection */
292
293     /* internal use -- per-message state */
294     int mimemsg;                /* bitmask indicating MIME body-type */
295     char digest [DIGESTLEN];    /* md5 digest buffer */
296
297     /* internal use -- housekeeping */
298     struct query *next;         /* next query control block in chain */
299 };
300
301 struct msgblk                   /* message header parsed for open_sink() */
302 {
303     char                *headers;       /* raw message headers */
304     struct idlist       *recipients;    /* addressees */
305     char                return_path[HOSTLEN + USERNAMELEN + 4]; 
306     int                 reallen;
307 };
308
309
310 /*
311  * Numeric option handling.  Numeric option value of zero actually means
312  * it's unspecified.  Value less than zero is zero.  The reason for this
313  * screwy encoding is so we can zero out an option block in order to set the
314  * numeric flags in it to unspecified.
315  */
316 #define NUM_VALUE_IN(n)         (((n) == 0) ? -1 : (n))
317 #define NUM_VALUE_OUT(n)        (((n) < 0) ? 0 : (n))
318 #define NUM_NONZERO(n)          ((n) > 0)
319 #define NUM_ZERO(n)             ((n) < 0)
320 #define NUM_SPECIFIED(n)        ((n) != 0)
321
322 #define MULTIDROP(ctl)  (ctl->wildcard || \
323                                 ((ctl)->localnames && (ctl)->localnames->next))
324
325 /*
326  * Note: tags are generated with an a%04d format from a 1-origin
327  * integer sequence number.  Length 4 permits transaction numbers
328  * up to 9999, so we force rollover with % 10000.  There's no special
329  * reason for this format other than to look like the exmples in the
330  * IMAP RFCs.
331  */
332 #define TAGLEN  6               /* 'a' + 4 digits + NUL */
333 extern char tag[TAGLEN];
334 #define TAGMOD  10000
335
336 /* list of hosts assembled from run control file and command line */
337 extern struct query cmd_opts, *querylist;
338
339 /* what's returned by envquery */
340 extern void envquery(int, char **);
341
342 /* controls the detail level of status/progress messages written to stderr */
343 extern int outlevel;            /* see the O_.* constants above */
344 extern int yydebug;             /* enable parse debugging */
345
346 /* these get computed */
347 extern int batchcount;          /* count of messages sent in current batch */
348 extern flag peek_capable;       /* can we read msgs without setting seen? */
349
350 /* miscellaneous global controls */
351 extern struct runctl run;       /* global controls for this run */
352 extern flag nodetach;           /* if TRUE, don't detach daemon process */
353 extern flag quitmode;           /* if --quit was set */
354 extern flag check_only;         /* if --check was set */
355 extern char *rcfile;            /* path name of rc file */
356 extern int linelimit;           /* limit # lines retrieved per site */
357 extern flag versioninfo;        /* emit only version info */
358 extern char *user;              /* name of invoking user */
359 extern char *home;              /* home directory of invoking user */
360 extern char *fmhome;            /* fetchmail home directory */
361 extern int pass;                /* number of re-polling pass */
362 extern flag configdump;         /* dump control blocks as Python dictionary */
363 extern const char *fetchmailhost;
364                                 /* either "localhost" or an FQDN */
365 #ifdef SDPS_ENABLE
366 extern char *sdps_envfrom;
367 extern char *sdps_envto;
368 #endif /* SDPS_ENABLE */
369
370 /* prototypes for globally callable functions */
371
372 /* error.c: Error reporting */
373 #if defined(HAVE_STDARG_H)
374 void report_init(int foreground);
375 void report (FILE *fp, const char *format, ...);
376 void report_build (FILE *fp, const char *format, ...);
377 void report_complete (FILE *fp, const char *format, ...);
378 void report_at_line (FILE *fp, int, const char *, unsigned int, const char *, ...);
379 #else
380 void report ();
381 void report_build ();
382 void report_complete ();
383 void report_at_line ();
384 #endif
385
386 /* driver.c: transaction support */
387 void set_timeout(int);
388 #if defined(HAVE_STDARG_H)
389 void gen_send (int sock, const char *, ... );
390 int gen_recv(int sock, char *buf, int size);
391 int gen_transact (int sock, const char *, ... );
392 #else
393 void gen_send ();
394 int gen_recv();
395 int gen_transact ();
396 #endif
397
398 /* use these to track what was happening when the nonresponse timer fired */
399 #define GENERAL_WAIT    0       /* unknown wait type */
400 #define OPEN_WAIT       1       /* waiting from mailserver open */
401 #define SERVER_WAIT     2       /* waiting for mailserver response */
402 #define LISTENER_WAIT   3       /* waiting for listener initialization */
403 #define FORWARDING_WAIT 4       /* waiting for listener response */
404 extern int phase;
405
406 /* response hooks can use this to identify the query stage */
407 #define STAGE_GETAUTH   0
408 #define STAGE_GETRANGE  1
409 #define STAGE_GETSIZES  2
410 #define STAGE_FETCH     3
411 #define STAGE_IDLE      4
412 #define STAGE_LOGOUT    5
413 extern int stage;
414
415 extern int mytimeout;
416
417 /* mark values for name lists */
418 #define XMIT_ACCEPT     1       /* accepted; matches local domain or name */
419 #define XMIT_REJECT     2       /* rejected; no match */
420 #define XMIT_RCPTBAD    3       /* SMTP listener rejected the name */ 
421
422 /* idle.c */
423 int interruptible_idle(int interval);
424
425 /* sink.c: forwarding */
426 int stuffline(struct query *, char *);
427 int open_sink(struct query*, struct msgblk *, int*, int*);
428 void release_sink(struct query *);
429 int close_sink(struct query *, struct msgblk *, flag);
430 int open_warning_by_mail(struct query *, struct msgblk *);
431 #if defined(HAVE_STDARG_H)
432 void stuff_warning(struct query *, const char *, ... );
433 #else
434 void stuff_warning();
435 #endif
436 void close_warning_by_mail(struct query *, struct msgblk *);
437
438 /* rfc822.c: RFC822 header parsing */
439 unsigned char *reply_hack(unsigned char *, const unsigned char *);
440 unsigned char *nxtaddr(const unsigned char *);
441
442 /* uid.c: UID support */
443 void initialize_saved_lists(struct query *, const char *);
444 struct idlist *save_str(struct idlist **, const char *, flag);
445 void free_str_list(struct idlist **);
446 struct idlist *copy_str_list(struct idlist *idl);
447 void save_str_pair(struct idlist **, const char *, const char *);
448 void free_str_pair_list(struct idlist **);
449 int delete_str(struct idlist **, int);
450 int str_in_list(struct idlist **, const char *, const flag);
451 int str_nr_in_list(struct idlist **, const char *);
452 int str_nr_last_in_list(struct idlist **, const char *);
453 void str_set_mark( struct idlist **, const char *, const flag);
454 int count_list( struct idlist **idl );
455 char *str_from_nr_list( struct idlist **idl, int number );
456 char *str_find(struct idlist **, int);
457 char *idpair_find(struct idlist **, const char *);
458 void append_str_list(struct idlist **, struct idlist **);
459 void expunge_uids(struct query *);
460 void uid_swap_lists(struct query *);
461 void write_saved_lists(struct query *, const char *);
462
463 /* rcfile_y.y */
464 int prc_parse_file(const char *, const flag);
465 int prc_filecheck(const char *, const flag);
466
467 /* base64.c */
468 void to64frombits(unsigned char *, const unsigned char *, int);
469 int from64tobits(char *, const char *);
470
471 /* unmime.c */
472 /* Bit-mask returned by MimeBodyType */
473 #define MSG_IS_7BIT       0x01
474 #define MSG_IS_8BIT       0x02
475 #define MSG_NEEDS_DECODE  0x80
476 extern void UnMimeHeader(unsigned char *buf);
477 extern int  MimeBodyType(unsigned char *hdrs, int WantDecode);
478 extern int  UnMimeBodyline(unsigned char **buf, flag delimited, flag issoftline);
479
480 /* interface.c */
481 void interface_init(void);
482 void interface_parse(char *, struct hostdata *);
483 void interface_note_activity(struct hostdata *);
484 int interface_approve(struct hostdata *, flag domonitor);
485
486 /* xmalloc.c */
487 #if defined(HAVE_VOIDPOINTER)
488 #define XMALLOCTYPE void
489 #else
490 #define XMALLOCTYPE char
491 #endif
492 XMALLOCTYPE *xmalloc(int);
493 XMALLOCTYPE *xrealloc(XMALLOCTYPE *, int);
494 char *xstrdup(const char *);
495 #if defined(HAVE_ALLOCA_H)
496 #include <alloca.h>
497 #else
498 #ifdef _AIX
499  #pragma alloca
500 #endif
501 #endif
502 #define xalloca(ptr, t, n)      if (!(ptr = (t) alloca(n)))\
503        {report(stderr, _("alloca failed")); exit(PS_UNDEFINED);}
504 #if FALSE
505 /*
506  * This is a hack to help xgettext which cannot find strings in
507  * macro definitions like the one for xalloca above.
508  */
509 static char *dummy = gettext_noop("alloca failed");
510 #endif
511
512 /* protocol driver and methods */
513 int do_protocol(struct query *, const struct method *);
514 int doPOP2 (struct query *); 
515 int doPOP3 (struct query *);
516 int doIMAP (struct query *);
517 int doETRN (struct query *);
518
519 /* miscellanea */
520 struct query *hostalloc(struct query *); 
521 int parsecmdline (int, char **, struct runctl *, struct query *);
522 char *MD5Digest (unsigned char *);
523 int POP3_auth_rpa(unsigned char *, unsigned char *, int socket);
524 int daemonize(const char *, void (*)(int));
525 char *fm_getpassword(char *);
526 void escapes(const char *, char *);
527 char *visbuf(const char *);
528 const char *showproto(int);
529 void dump_config(struct runctl *runp, struct query *querylist);
530 int is_host_alias(const char *, struct query *);
531 char *host_fqdn(void);
532 char *rfc822timestamp(void);
533 flag isafile(int);
534
535 void yyerror(const char *);
536 int yylex(void);
537
538 #ifdef __EMX__
539 void itimerthread(void*);
540 /* Have to include these first to avoid errors from redefining getcwd
541    and chdir.  They're re-include protected in EMX, so it's okay, I
542    guess.  */
543 #include <stdlib.h>
544 #include <unistd.h>
545 /* Redefine getcwd and chdir to get drive-letter support so we can
546    find all of our lock files and stuff. */
547 #define getcwd _getcwd2
548 #define chdir _chdir2
549 #endif /* _EMX_ */
550
551 # if HAVE_STRERROR
552 #  ifndef strerror              /* On some systems, strerror is a macro */
553 char *strerror ();
554 #  endif
555 # endif /* HAVE_STRERROR */
556
557 #define STRING_DISABLED (char *)-1
558 #define STRING_DUMMY    ""
559
560 #ifdef NeXT
561 #ifndef S_IXGRP
562 #define S_IXGRP 0000010
563 #endif
564 #endif
565
566 #ifdef FETCHMAIL_DEBUG
567 #define exit(e) do { \
568        FILE *out; \
569        out = fopen("/tmp/fetchmail.log", "a"); \
570        fprintf(out, \
571                "Exiting fetchmail from file %s, line %d with status %d\n", \
572                __FILE__, __LINE__, e); \
573        fclose(out); \
574        _exit(e); \
575        } while(0)
576 #endif /* FETCHMAIL_DEBUG */
577
578 /* fetchmail.h ends here */