]> Pileus Git - ~andy/fetchmail/blob - fetchmail.h
Try to fix a nasty MIME-decoding bug.
[~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_IMAP          4
13 #define         P_IMAP_K4       5
14 #define         P_IMAP_GSS      6
15 #define         P_APOP          7
16 #define         P_RPOP          8
17 #define         P_ETRN          9
18
19 #if INET6
20 #define         SMTP_PORT       "smtp"
21 #define         KPOP_PORT       "kpop"
22 #else /* INET6 */
23 #define         SMTP_PORT       25
24 #define         KPOP_PORT       1109
25 #endif /* INET6 */
26
27 /* preauthentication types */
28 #define         A_PASSWORD      0       /* password or inline authentication */
29 #define         A_KERBEROS_V4   1       /* preauthenticate w/ Kerberos V4 */
30 #define         A_KERBEROS_V5   2       /* preauthenticate w/ Kerberos V5 */
31
32 /*
33  * Definitions for buffer sizes.  We get little help on setting maxima
34  * from IMAP RFCs up to 2060, so these are mostly from POP3.
35  */
36 #define         HOSTLEN         635     /* max hostname length (RFC1123) */
37 #define         POPBUFSIZE      512     /* max length of response (RFC1939) */
38 #define         IDLEN           128     /* max length of UID (RFC1939) */
39
40 /* per RFC1939 this should be 40, but Microsoft Exchange ignores that limit */
41 #define         USERNAMELEN     128     /* max POP3 arg length */
42
43 /* clear a netBSD kernel parameter out of the way */ 
44 #undef          MSGBUFSIZE
45
46 /*
47  * The RFC822 limit on message line size is just 998.  But
48  * make this *way* oversized; idiot DOS-world mailers that
49  * don't line-wrap properly often ship entire paragraphs as
50  * lines.
51  */
52 #define         MSGBUFSIZE      8192
53
54 #define         PASSWORDLEN     64      /* max password length */
55 #define         DIGESTLEN       33      /* length of MD5 digest */
56
57 /* exit code values */
58 #define         PS_SUCCESS      0       /* successful receipt of messages */
59 #define         PS_NOMAIL       1       /* no mail available */
60 #define         PS_SOCKET       2       /* socket I/O woes */
61 #define         PS_AUTHFAIL     3       /* user authorization failed */
62 #define         PS_PROTOCOL     4       /* protocol violation */
63 #define         PS_SYNTAX       5       /* command-line syntax error */
64 #define         PS_IOERR        6       /* bad permissions on rc file */
65 #define         PS_ERROR        7       /* protocol error */
66 #define         PS_EXCLUDE      8       /* client-side exclusion error */
67 #define         PS_LOCKBUSY     9       /* server responded lock busy */
68 #define         PS_SMTP         10      /* SMTP error */
69 #define         PS_DNS          11      /* fatal DNS error */
70 #define         PS_BSMTP        12      /* output batch could not be opened */
71 /* leave space for more codes */
72 #define         PS_UNDEFINED    23      /* something I hadn't thought of */
73 #define         PS_TRANSIENT    24      /* transient failure (internal use) */
74 #define         PS_REFUSED      25      /* mail refused (internal use) */
75 #define         PS_RETAINED     26      /* message retained (internal use) */
76 #define         PS_TRUNCATED    27      /* headers incomplete (internal use) */
77
78 /* output noise level */
79 #define         O_SILENT        0       /* mute, max squelch, etc. */
80 #define         O_NORMAL        1       /* user-friendly */
81 #define         O_VERBOSE       2       /* chatty */
82 #define         O_DEBUG         3       /* prolix */
83 #define         O_MONITOR       O_VERBOSE
84
85 #define         SIZETICKER      1024    /* print 1 dot per this many bytes */
86
87 /*
88  * We #ifdef this and use flag rather than bool
89  * to avoid a type clash with curses.h
90  */
91 #ifndef TRUE
92 #define FALSE   0
93 #define TRUE    1
94 #endif /* TRUE */
95 typedef char    flag;
96
97 /* we need to use zero as a flag-uninitialized value */
98 #define FLAG_TRUE       2
99 #define FLAG_FALSE      1
100
101 struct runctl
102 {
103     char        *logfile;
104     char        *idfile;
105     int         poll_interval;
106     char        *postmaster;
107     flag        bouncemail;
108     char        *properties;
109     flag        use_syslog;
110     flag        invisible;
111 };
112
113 struct idlist
114 {
115     char *id;
116     union
117     {
118         struct
119         {
120             short       num;
121             flag        mark;           /* UID-index information */
122 #define UID_UNSEEN      0               /* hasn't been seen */
123 #define UID_SEEN        1               /* seen, but not deleted */
124 #define UID_DELETED     2               /* this message has been deleted */
125 #define UID_EXPUNGED    3               /* this message has been expunged */ 
126         }
127         status;
128         char *id2;
129     } val;
130     struct idlist *next;
131 };
132
133 struct query;
134
135 struct method           /* describe methods for protocol state machine */
136 {
137     const char *name;           /* protocol name */
138 #if INET6
139     const char *service;
140 #else /* INET6 */
141     int port;                   /* service port */
142 #endif /* INET6 */
143     flag tagged;                /* if true, generate & expect command tags */
144     flag delimited;             /* if true, accept "." message delimiter */
145     int (*parse_response)(int, char *);
146                                 /* response_parsing function */
147     int (*password_canonify)(char *, char *);
148                                 /* canonicalize password */
149     int (*getauth)(int, struct query *, char *);
150                                 /* authorization fetcher */
151     int (*getrange)(int, struct query *, const char *, int *, int *, int *);
152                                 /* get message range to fetch */
153     int (*getsizes)(int, int, int *);
154                                 /* get sizes of messages */
155     int (*is_old)(int, struct query *, int);
156                                 /* check for old message */
157     int (*fetch_headers)(int, struct query *, int, int *);
158                                 /* fetch FROM headera given message */
159     int (*fetch_body)(int, struct query *, int, int *);
160                                 /* fetch a given message */
161     int (*trail)(int, struct query *, int);
162                                 /* eat trailer of a message */
163     int (*delete)(int, struct query *, int);
164                                 /* delete method */
165     int (*logout_cmd)(int, struct query *);
166                                 /* logout command */
167     flag retry;                 /* can getrange poll for new messages? */
168 };
169
170 struct hostdata         /* shared among all user connections to given server */
171 {
172     /* rc file data */
173     char *pollname;                     /* poll label of host */
174     char *via;                          /* "true" server name if non-NULL */
175     struct idlist *akalist;             /* server name first, then akas */
176     struct idlist *localdomains;        /* list of pass-through domains */
177     int protocol;                       /* protocol type */
178 #if INET6
179     char *service;                      /* IPv6 service name */
180     void *netsec;                       /* IPv6 security request */
181 #else /* INET6 */
182     int port;                           /* TCP/IP service port number */
183 #endif /* INET6 */
184     int interval;                       /* # cycles to skip between polls */
185     int preauthenticate;                /* preauthentication mode to try */
186     int timeout;                        /* inactivity timout in seconds */
187     char *envelope;                     /* envelope address list header */
188     int envskip;                        /* skip to numbered envelope header */
189     char *qvirtual;                     /* prefix removed from local user id */
190     flag skip;                          /* suppress poll in implicit mode? */
191     flag dns;                           /* do DNS lookup on multidrop? */
192     flag uidl;                          /* use RFC1725 UIDLs? */
193 #ifdef SDPS_ENABLE
194     flag sdps;                          /* use Demon Internet SDPS *ENV */
195 #endif /* SDPS_ENABLE */
196     flag checkalias;                    /* resolve aliases by comparing IPs? */
197
198
199 #if defined(linux) || defined(__FreeBSD__)
200     char *interface;
201     char *monitor;
202     int  monitor_io;
203     struct interface_pair_s *interface_pair;
204 #endif /* linux */
205
206     char *plugin,*plugout;
207
208     /* computed for internal use */
209     const struct method *base_protocol; /* relevant protocol method table */
210     int poll_count;                     /* count of polls so far */
211     char *queryname;                    /* name to attempt DNS lookup on */
212     char *truename;                     /* "true name" of server host */
213     struct hostdata *lead_server;       /* ptr to lead query for this server */
214     int esmtp_options;
215 };
216
217 struct query
218 {
219     /* mailserver connection controls */
220     struct hostdata server;
221
222     /* per-user data */
223     struct idlist *localnames;  /* including calling user's name */
224     int wildcard;               /* should unmatched names be passed through */
225     char *remotename;           /* remote login name to use */
226     char *password;             /* remote password to use */
227     struct idlist *mailboxes;   /* list of mailboxes to check */
228
229     /* per-forwarding-target data */
230     struct idlist *smtphunt;    /* list of SMTP hosts to try forwarding to */
231     char *smtpaddress;          /* address to force in RCPT TO */ 
232     struct idlist *antispam;    /* list of listener's antispam response */
233     char *mda;                  /* local MDA to pass mail to */
234     char *bsmtp;                /* BSMTP output file */
235     char listener;              /* what's the listener's wire protocol? */
236 #define SMTP_MODE       'S'
237 #define LMTP_MODE       'L'
238     char *preconnect;           /* pre-connection command to execute */
239     char *postconnect;          /* post-connection command to execute */
240
241     /* per-user control flags */
242     flag keep;                  /* if TRUE, leave messages undeleted */
243     flag fetchall;              /* if TRUE, fetch all (not just unseen) */
244     flag flush;                 /* if TRUE, delete messages already seen */
245     flag rewrite;               /* if TRUE, canonicalize recipient addresses */
246     flag stripcr;               /* if TRUE, strip CRs in text */
247     flag forcecr;               /* if TRUE, force CRs before LFs in text */
248     flag pass8bits;             /* if TRUE, ignore Content-Transfer-Encoding */
249     flag dropstatus;            /* if TRUE, drop Status lines in mail */
250     flag mimedecode;            /* if TRUE, decode MIME-armored messages */
251     int limit;                  /* limit size of retrieved messages */
252     int warnings;               /* size warning interval */
253     int fetchlimit;             /* max # msgs to get in single poll */
254     int batchlimit;             /* max # msgs to pass in single SMTP session */
255     int expunge;                /* max # msgs to pass between expunges */
256     char *properties;           /* passthrough properties for extensions */
257
258     /* internal use -- per-poll state */
259     flag active;                /* should we actually poll this server? */
260     const char *destaddr;       /* destination host for this query */
261     int errcount;               /* count transient errors in last pass */
262     int wedged;                 /* wedged by auth failures or timeouts? */
263     char *smtphost;             /* actual SMTP host we connected to */
264     int smtp_socket;            /* socket descriptor for SMTP connection */
265     unsigned int uid;           /* UID of user to deliver to */
266     struct idlist *skipped;     /* messages skipped on the mail server */
267     struct idlist *oldsaved, *newsaved;
268
269     /* internal use -- per-message state */
270     int mimemsg;                /* bitmask indicating MIME body-type */
271     char digest [DIGESTLEN];    /* md5 digest buffer */
272
273     /* internal use -- housekeeping */
274     struct query *next;         /* next query control block in chain */
275 };
276
277 struct msgblk                   /* message header parsed for open_sink() */
278 {
279     char                *headers;       /* raw message headers */
280     struct idlist       *recipients;    /* addressees */
281     char                return_path[HOSTLEN + USERNAMELEN + 4]; 
282     int                 reallen;
283 };
284
285
286 /*
287  * Numeric option handling.  Numeric option value of zero actually means
288  * it's unspecified.  Value less than zero is zero.
289  */
290 #define NUM_VALUE(n)            (((n) == 0) ? -1 : (n))
291 #define NUM_NONZERO(n)          ((n) > 0)
292 #define NUM_ZERO(n)             ((n) < 0)
293 #define NUM_SPECIFIED(n)        ((n) != 0)
294
295 #define MULTIDROP(ctl)  (ctl->wildcard || \
296                                 ((ctl)->localnames && (ctl)->localnames->next))
297
298 /*
299  * Note: tags are generated with an a%04d format from a 1-origin
300  * integer sequence number.  Length 4 permits transaction numbers
301  * up to 9999, so we force rollover with % 10000.  There's no special
302  * reason for this format other than to look like the exmples in the
303  * IMAP RFCs.
304  */
305 #define TAGLEN  6               /* 'a' + 4 digits + NUL */
306 extern char tag[TAGLEN];
307 #define TAGMOD  10000
308
309 /* list of hosts assembled from run control file and command line */
310 extern struct query cmd_opts, *querylist;
311
312 /* what's returned by envquery */
313 extern void envquery(int, char **);
314
315 /* controls the detail level of status/progress messages written to stderr */
316 extern int outlevel;            /* see the O_.* constants above */
317 extern int yydebug;             /* enable parse debugging */
318
319 /* these get computed */
320 extern int batchcount;          /* count of messages sent in current batch */
321 extern flag peek_capable;       /* can we read msgs without setting seen? */
322
323 /* miscellaneous global controls */
324 extern struct runctl run;       /* global controls for this run */
325 extern flag nodetach;           /* if TRUE, don't detach daemon process */
326 extern flag quitmode;           /* if --quit was set */
327 extern flag check_only;         /* if --check was set */
328 extern char *rcfile;            /* path name of rc file */
329 extern int linelimit;           /* limit # lines retrieved per site */
330 extern flag versioninfo;        /* emit only version info */
331 extern char *user;              /* name of invoking user */
332 extern char *home;              /* home directory of invoking user */
333 extern int pass;                /* number of re-polling pass */
334 extern flag configdump;         /* dump control blocks as Python dictionary */
335 extern const char *fetchmailhost;
336                                 /* either "localhost" or an FQDN */
337 #ifdef SDPS_ENABLE
338 extern char *sdps_envfrom;
339 extern char *sdps_envto;
340 #endif /* SDPS_ENABLE */
341
342 /* prototypes for globally callable functions */
343
344 /* error.c: Error reporting */
345 #if defined(HAVE_STDARG_H)
346 void report_init(int foreground);
347 void report (FILE *fp, const char *format, ...);
348 void report_build (FILE *fp, const char *format, ...);
349 void report_complete (FILE *fp, const char *format, ...);
350 void report_at_line (FILE *fp, int, const char *, unsigned int, const char *, ...);
351 #else
352 void report ();
353 void report_build ();
354 void report_complete ();
355 void report_at_line ();
356 #endif
357
358 /* driver.c: transaction support */
359 void set_timeout(int);
360 #if defined(HAVE_STDARG_H)
361 void gen_send (int sock, const char *, ... );
362 int gen_recv(int sock, char *buf, int size);
363 int gen_transact (int sock, const char *, ... );
364 #else
365 void gen_send ();
366 int gen_recv();
367 int gen_transact ();
368 #endif
369
370 /* use these to track what was happening when the nonresponse timer fired */
371 #define GENERAL_WAIT    0       /* unknown wait type */
372 #define OPEN_WAIT       1       /* waiting from mailserver open */
373 #define SERVER_WAIT     2       /* waiting for mailserver response */
374 #define LISTENER_WAIT   3       /* waiting for listener initialization */
375 #define FORWARDING_WAIT 4       /* waiting for listener response */
376 extern int phase;
377
378 /* mark values for name lists */
379 #define XMIT_ACCEPT     1       /* accepted; matches local domain or name */
380 #define XMIT_REJECT     2       /* rejected; no match */
381 #define XMIT_RCPTBAD    3       /* SMTP listener rejected the name */ 
382
383 /* sink.c: forwarding */
384 int stuffline(struct query *, char *);
385 int open_sink(struct query*, struct msgblk *, int*, int*);
386 void release_sink(struct query *);
387 int close_sink(struct query *, struct msgblk *, flag);
388 int open_warning_by_mail(struct query *, struct msgblk *);
389 #if defined(HAVE_STDARG_H)
390 void stuff_warning(struct query *, const char *, ... );
391 #else
392 void stuff_warning();
393 #endif
394 void close_warning_by_mail(struct query *, struct msgblk *);
395
396 /* rfc822.c: RFC822 header parsing */
397 char *reply_hack(char *, const char *);
398 char *nxtaddr(const char *);
399
400 /* uid.c: UID support */
401 void initialize_saved_lists(struct query *, const char *);
402 struct idlist *save_str(struct idlist **, const char *, flag);
403 void free_str_list(struct idlist **);
404 void save_str_pair(struct idlist **, const char *, const char *);
405 void free_str_pair_list(struct idlist **);
406 int delete_str(struct idlist **, int);
407 int str_in_list(struct idlist **, const char *, const flag);
408 int str_nr_in_list(struct idlist **, const char *);
409 int str_nr_last_in_list(struct idlist **, const char *);
410 void str_set_mark( struct idlist **, const char *, const flag);
411 int count_list( struct idlist **idl );
412 char *str_from_nr_list( struct idlist **idl, int number );
413 char *str_find(struct idlist **, int);
414 char *idpair_find(struct idlist **, const char *);
415 void append_str_list(struct idlist **, struct idlist **);
416 void expunge_uids(struct query *);
417 void update_str_lists(struct query *);
418 void write_saved_lists(struct query *, const char *);
419
420 /* rcfile_y.y */
421 int prc_parse_file(const char *, const flag);
422 int prc_filecheck(const char *, const flag);
423
424 /* base64.c */
425 void to64frombits(unsigned char *, const unsigned char *, int);
426 int from64tobits(char *, const char *);
427
428 /* unmime.c */
429 /* Bit-mask returned by MimeBodyType */
430 #define MSG_IS_7BIT       0x01
431 #define MSG_IS_8BIT       0x02
432 #define MSG_NEEDS_DECODE  0x80
433 extern void UnMimeHeader(unsigned char *buf);
434 extern int  MimeBodyType(unsigned char *hdrs, int WantDecode);
435 extern int  UnMimeBodyline(unsigned char **buf, flag delimited, flag issoftline);
436
437 /* interface.c */
438 void interface_init(void);
439 void interface_parse(char *, struct hostdata *);
440 void interface_note_activity(struct hostdata *);
441 int interface_approve(struct hostdata *);
442
443 /* xmalloc.c */
444 #if defined(HAVE_VOIDPOINTER)
445 #define XMALLOCTYPE void
446 #else
447 #define XMALLOCTYPE char
448 #endif
449 XMALLOCTYPE *xmalloc(int);
450 XMALLOCTYPE *xrealloc(XMALLOCTYPE *, int);
451 char *xstrdup(const char *);
452 #if defined(HAVE_ALLOCA_H)
453 #include <alloca.h>
454 #else
455 #ifdef _AIX
456  #pragma alloca
457 #endif
458 #endif
459 #define xalloca(ptr, t, n)      if (!(ptr = (t) alloca(n))) {report(stderr, "alloca failed"); exit(PS_UNDEFINED);}
460
461 /* protocol driver and methods */
462 int do_protocol(struct query *, const struct method *);
463 int doPOP2 (struct query *); 
464 int doPOP3 (struct query *);
465 int doIMAP (struct query *);
466 int doETRN (struct query *);
467
468 /* miscellanea */
469 struct query *hostalloc(struct query *); 
470 int parsecmdline (int, char **, struct runctl *, struct query *);
471 char *MD5Digest (unsigned char *);
472 int POP3_auth_rpa(unsigned char *, unsigned char *, int socket);
473 int daemonize(const char *, void (*)(int));
474 char *getpassword(char *);
475 void escapes(const char *, char *);
476 char *visbuf(const char *);
477 const char *showproto(int);
478 void dump_config(struct runctl *runp, struct query *querylist);
479 int is_host_alias(const char *, struct query *);
480 char *host_fqdn(void);
481 char *rfc822timestamp(void);
482
483 void yyerror(const char *);
484 int yylex(void);
485
486 #ifdef __EMX__
487 void itimerthread(void*);
488 /* Have to include these first to avoid errors from redefining getcwd
489    and chdir.  They're re-include protected in EMX, so it's okay, I
490    guess.  */
491 #include <stdlib.h>
492 #include <unistd.h>
493 /* Redefine getcwd and chdir to get drive-letter support so we can
494    find all of our lock files and stuff. */
495 #define getcwd _getcwd2
496 #define chdir _chdir2
497 #endif /* _EMX_ */
498
499 # if HAVE_STRERROR
500 #  ifndef strerror              /* On some systems, strerror is a macro */
501 char *strerror ();
502 #  endif
503 # endif /* HAVE_STRERROR */
504
505 #define STRING_DISABLED (char *)-1
506 #define STRING_DUMMY    ""
507
508 #ifdef NeXT
509 #ifndef S_IXGRP
510 #define S_IXGRP 0000010
511 #endif
512 #endif
513
514 /* fetchmail.h ends here */