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