]> Pileus Git - ~andy/fetchmail/blob - options.c
5172657dc7c59b98f52a45bb5aabfacf5714fb12
[~andy/fetchmail] / options.c
1 /*
2  * options.c -- command-line option processing
3  *
4  * Copyright 1998 by Eric S. Raymond.
5  * For license terms, see the file COPYING in this directory.
6  */
7
8 #include "config.h"
9
10 #include <stdio.h>
11 #include <pwd.h>
12 #include <string.h>
13 #include <errno.h>
14 #if defined(STDC_HEADERS)
15 #include  <stdlib.h>
16 #include  <limits.h>
17 #else
18 #include  <ctype.h>
19 #endif
20
21 #include "getopt.h"
22 #include "fetchmail.h"
23 #include "i18n.h"
24
25 #define LA_HELP         1
26 #define LA_VERSION      2 
27 #define LA_CHECK        3
28 #define LA_SILENT       4 
29 #define LA_VERBOSE      5 
30 #define LA_DAEMON       6
31 #define LA_NODETACH     7
32 #define LA_QUIT         8
33 #define LA_LOGFILE      9
34 #define LA_INVISIBLE    10
35 #define LA_SYSLOG       11
36 #define LA_NOSYSLOG     12
37 #define LA_RCFILE       13
38 #define LA_IDFILE       14
39 #define LA_POSTMASTER   15
40 #define LA_NOBOUNCE     16
41 #define LA_PROTOCOL     17
42 #define LA_UIDL         18
43 #define LA_PORT         19
44 #define LA_AUTHENTICATE 20
45 #define LA_TIMEOUT      21
46 #define LA_ENVELOPE     22
47 #define LA_QVIRTUAL     23
48 #define LA_USERNAME     24
49 #define LA_ALL          25
50 #define LA_NOKEEP       26
51 #define LA_KEEP         27
52 #define LA_FLUSH        28
53 #define LA_NOREWRITE    29
54 #define LA_LIMIT        30
55 #define LA_WARNINGS     31
56 #define LA_FOLDER       32
57 #define LA_SMTPHOST     33
58 #define LA_SMTPADDR     34
59 #define LA_ANTISPAM     35
60 #define LA_BATCHLIMIT   36
61 #define LA_FETCHLIMIT   37
62 #define LA_EXPUNGE      38
63 #define LA_MDA          39
64 #define LA_BSMTP        40
65 #define LA_LMTP         41
66 #define LA_PLUGIN       42
67 #define LA_PLUGOUT      43
68 #define LA_NETSEC       44
69 #define LA_INTERFACE    45
70 #define LA_MONITOR      46
71 #define LA_CONFIGDUMP   47
72 #define LA_YYDEBUG      48
73
74 /* options still left: CDgGhHjJoORwWxXYz */
75 static const char *shortoptions = 
76         "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:";
77
78 static const struct option longoptions[] = {
79 /* this can be const because all flag fields are 0 and will never get set */
80   {"help",      no_argument,       (int *) 0, LA_HELP        },
81   {"version",   no_argument,       (int *) 0, LA_VERSION     },
82   {"check",     no_argument,       (int *) 0, LA_CHECK       },
83   {"silent",    no_argument,       (int *) 0, LA_SILENT      },
84   {"verbose",   no_argument,       (int *) 0, LA_VERBOSE     },
85   {"daemon",    required_argument, (int *) 0, LA_DAEMON      },
86   {"nodetach",  no_argument,       (int *) 0, LA_NODETACH    },
87   {"quit",      no_argument,       (int *) 0, LA_QUIT        },
88   {"logfile",   required_argument, (int *) 0, LA_LOGFILE     },
89   {"invisible", no_argument,       (int *) 0, LA_INVISIBLE   },
90   {"syslog",    no_argument,       (int *) 0, LA_SYSLOG      },
91   {"nosyslog",  no_argument,       (int *) 0, LA_NOSYSLOG    },
92   {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE      },
93   {"idfile",    required_argument, (int *) 0, LA_IDFILE      },
94   {"postmaster",required_argument, (int *) 0, LA_POSTMASTER  },
95   {"nobounce",  no_argument,       (int *) 0, LA_NOBOUNCE    },
96
97   {"protocol",  required_argument, (int *) 0, LA_PROTOCOL    },
98   {"proto",     required_argument, (int *) 0, LA_PROTOCOL    },
99   {"uidl",      no_argument,       (int *) 0, LA_UIDL        },
100   {"port",      required_argument, (int *) 0, LA_PORT        },
101   {"auth",      required_argument, (int *) 0, LA_AUTHENTICATE},
102   {"timeout",   required_argument, (int *) 0, LA_TIMEOUT     },
103   {"envelope",  required_argument, (int *) 0, LA_ENVELOPE    },
104   {"qvirtual",  required_argument, (int *) 0, LA_QVIRTUAL    },
105
106   {"user",      required_argument, (int *) 0, LA_USERNAME    },
107   {"username",  required_argument, (int *) 0, LA_USERNAME    },
108
109   {"all",       no_argument,       (int *) 0, LA_ALL         },
110   {"nokeep",    no_argument,       (int *) 0, LA_NOKEEP      },
111   {"keep",      no_argument,       (int *) 0, LA_KEEP        },
112   {"flush",     no_argument,       (int *) 0, LA_FLUSH       },
113   {"norewrite", no_argument,       (int *) 0, LA_NOREWRITE   },
114   {"limit",     required_argument, (int *) 0, LA_LIMIT       },
115   {"warnings",  required_argument, (int *) 0, LA_WARNINGS    },
116
117   {"folder",    required_argument, (int *) 0, LA_FOLDER      },
118   {"smtphost",  required_argument, (int *) 0, LA_SMTPHOST    },
119   {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR  },
120   {"antispam",  required_argument, (int *) 0, LA_ANTISPAM    },
121   
122   {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT  },
123   {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT  },
124   {"expunge",   required_argument, (int *) 0, LA_EXPUNGE     },
125   {"mda",       required_argument, (int *) 0, LA_MDA         },
126   {"bsmtp",     required_argument, (int *) 0, LA_BSMTP       },
127   {"lmtp",      no_argument,       (int *) 0, LA_LMTP        },
128
129 #ifdef INET6
130   {"netsec",    required_argument, (int *) 0, LA_NETSEC      },
131 #endif /* INET6 */
132
133 #if (defined(linux) && !INET6) || defined(__FreeBSD__)
134   {"interface", required_argument, (int *) 0, LA_INTERFACE   },
135   {"monitor",   required_argument, (int *) 0, LA_MONITOR     },
136 #endif /* (defined(linux) && !INET6) || defined(__FreeBSD__) */
137   {"plugin",    required_argument, (int *) 0, LA_PLUGIN      },
138   {"plugout",   required_argument, (int *) 0, LA_PLUGOUT     },
139
140   {"configdump",no_argument,       (int *) 0, LA_CONFIGDUMP  },
141
142   {"yydebug",   no_argument,       (int *) 0, LA_YYDEBUG     },
143
144   {(char *) 0,  no_argument,       (int *) 0, 0              }
145 };
146
147 static int xatoi(char *s, int *errflagptr)
148 /* do safe conversion from string to number */
149 {
150 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
151     /* parse and convert numbers, but also check for invalid characters in
152      * numbers
153      */
154
155     char *endptr;
156     long value;
157
158     errno = 0;
159
160     value = strtol(s, &endptr, 0);
161
162     /* any invalid chars in string? */
163     if ( (endptr == s) || (*endptr != '\0') ) {
164         (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
165         (*errflagptr)++;
166         return 0;
167     }
168
169     /* is the range valid? */
170     if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
171                                 (value > INT_MAX) || (value < INT_MIN)) {
172
173         (void) fprintf(stderr, _("Value of string '%s' is %s than %d.\n"), s,
174                                         (value < 0) ? _("smaller"): _("larger"),
175                                         (value < 0) ? INT_MIN : INT_MAX);
176         (*errflagptr)++;
177         return 0;
178     }
179
180     return (int) value;  /* shut up, I know what I'm doing */
181 #else
182     int i;
183     char *dp;
184 # if defined (STDC_HEADERS)
185     size_t      len;
186 # else
187     int         len;
188 # endif
189
190     /* We do only base 10 conversions here (atoi)! */
191
192     len = strlen(s);
193     /* check for leading white spaces */
194     for (i = 0; (i < len) && isspace(s[i]); i++)
195         ;
196
197     dp = &s[i];
198
199     /* check for +/- */
200     if (i < len && (s[i] == '+' || s[i] == '-'))        i++;
201
202     /* skip over digits */
203     for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
204         ;
205
206     /* check for trailing garbage */
207     if (i != len) {
208         (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
209         (*errflagptr)++;
210         return 0;
211     }
212
213     /* atoi should be safe by now, except for number range over/underflow */
214     return atoi(dp);
215 #endif
216 }
217
218 int parsecmdline (argc, argv, rctl, ctl)
219 /* parse and validate the command line options */
220 int argc;               /* argument count */
221 char **argv;            /* argument strings */
222 struct runctl *rctl;    /* global run controls to modify */
223 struct query *ctl;      /* option record to be initialized */
224 {
225     /*
226      * return value: if positive, argv index of last parsed option + 1
227      * (presumes one or more server names follows).  if zero, the
228      * command line switches are such that no server names are
229      * required (e.g. --version).  if negative, the command line is
230      * has one or more syntax errors.
231      */
232
233     int c;
234     int ocount = 0;     /* count of destinations specified */
235     int errflag = 0;    /* TRUE when a syntax error is detected */
236     int helpflag = 0;   /* TRUE when option help was explicitly requested */
237     int option_index;
238     char *buf, *cp;
239
240     rctl->poll_interval = -1;
241
242     memset(ctl, '\0', sizeof(struct query));    /* start clean */
243     ctl->smtp_socket = -1;
244
245     while (!errflag && 
246            (c = getopt_long(argc,argv,shortoptions,
247                             longoptions,&option_index)) != -1) {
248
249         switch (c) {
250         case 'V':
251         case LA_VERSION:
252             versioninfo = TRUE;
253             break;
254         case 'c':
255         case LA_CHECK:
256             check_only = TRUE;
257             break;
258         case 's':
259         case LA_SILENT:
260             outlevel = O_SILENT;
261             break;
262         case 'v':
263         case LA_VERBOSE:
264             if (outlevel == O_VERBOSE)
265                 outlevel = O_DEBUG;
266             else
267                 outlevel = O_VERBOSE;
268             break;
269         case 'd':
270         case LA_DAEMON:
271             rctl->poll_interval = xatoi(optarg, &errflag);
272             break;
273         case 'N':
274         case LA_NODETACH:
275             nodetach = TRUE;
276             break;
277         case 'q':
278         case LA_QUIT:
279             quitmode = TRUE;
280             break;
281         case 'L':
282         case LA_LOGFILE:
283             rctl->logfile = optarg;
284             break;
285         case LA_INVISIBLE:
286             rctl->invisible = TRUE;
287             break;
288         case 'f':
289         case LA_RCFILE:
290             rcfile = (char *) xmalloc(strlen(optarg)+1);
291             strcpy(rcfile,optarg);
292             break;
293         case 'i':
294         case LA_IDFILE:
295             rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
296             strcpy(rctl->idfile,optarg);
297             break;
298         case LA_POSTMASTER:
299             rctl->postmaster = (char *) xmalloc(strlen(optarg)+1);
300             strcpy(rctl->postmaster,optarg);
301             break;
302         case LA_NOBOUNCE:
303             run.bouncemail = FALSE;
304             break;
305         case 'p':
306         case LA_PROTOCOL:
307             /* XXX -- should probably use a table lookup here */
308             if (strcasecmp(optarg,"auto") == 0)
309                 ctl->server.protocol = P_AUTO;
310             else if (strcasecmp(optarg,"pop2") == 0)
311                 ctl->server.protocol = P_POP2;
312 #ifdef SDPS_ENABLE
313             else if (strcasecmp(optarg,"sdps") == 0)
314             {
315                 ctl->server.protocol = P_POP3; 
316                 ctl->server.sdps = TRUE;
317             }
318 #endif /* SDPS_ENABLE */
319             else if (strcasecmp(optarg,"pop3") == 0)
320                 ctl->server.protocol = P_POP3;
321             else if (strcasecmp(optarg,"apop") == 0)
322                 ctl->server.protocol = P_APOP;
323             else if (strcasecmp(optarg,"rpop") == 0)
324                 ctl->server.protocol = P_RPOP;
325             else if (strcasecmp(optarg,"kpop") == 0)
326             {
327                 ctl->server.protocol = P_POP3;
328 #if INET6
329                 ctl->server.service = KPOP_PORT;
330 #else /* INET6 */
331                 ctl->server.port = KPOP_PORT;
332 #endif /* INET6 */
333 #ifdef KERBEROS_V5
334                 ctl->server.preauthenticate =  A_KERBEROS_V5;
335 #else
336                 ctl->server.preauthenticate =  A_KERBEROS_V4;
337 #endif /* KERBEROS_V5 */
338             }
339             else if (strcasecmp(optarg,"imap") == 0)
340                 ctl->server.protocol = P_IMAP;
341 #ifdef KERBEROS_V4
342             else if (strcasecmp(optarg,"imap-k4") == 0)
343                 ctl->server.protocol = P_IMAP_K4;
344 #endif /* KERBEROS_V4 */
345 #ifdef GSSAPI
346             else if (strcasecmp(optarg, "imap-gss") == 0)
347                 ctl->server.protocol = P_IMAP_GSS;
348 #endif /* GSSAPI */
349             else if (strcasecmp(optarg,"etrn") == 0)
350                 ctl->server.protocol = P_ETRN;
351             else {
352                 fprintf(stderr,_("Invalid protocol `%s' specified.\n"), optarg);
353                 errflag++;
354             }
355             break;
356         case 'U':
357         case LA_UIDL:
358             ctl->server.uidl = FLAG_TRUE;
359             break;
360         case 'P':
361         case LA_PORT:
362 #if INET6
363             ctl->server.service = optarg;
364 #else /* INET6 */
365             ctl->server.port = xatoi(optarg, &errflag);
366 #endif /* INET6 */
367             break;
368         case 'A':
369         case LA_AUTHENTICATE:
370             if (strcmp(optarg, "password") == 0)
371                 ctl->server.preauthenticate = A_PASSWORD;
372             else if (strcmp(optarg, "kerberos") == 0)
373 #ifdef KERBEROS_V5
374                 ctl->server.preauthenticate = A_KERBEROS_V5;
375 #else
376                 ctl->server.preauthenticate = A_KERBEROS_V4;
377 #endif /* KERBEROS_V5 */
378             else if (strcmp(optarg, "kerberos_v5") == 0)
379                 ctl->server.preauthenticate = A_KERBEROS_V5;
380             else if (strcmp(optarg, "kerberos_v4") == 0)
381                 ctl->server.preauthenticate = A_KERBEROS_V4;
382             else {
383                 fprintf(stderr,_("Invalid preauthentication `%s' specified.\n"), optarg);
384                 errflag++;
385             }
386             break;
387         case 't':
388         case LA_TIMEOUT:
389             ctl->server.timeout = xatoi(optarg, &errflag);
390             if (ctl->server.timeout == 0)
391                 ctl->server.timeout = -1;
392             break;
393         case 'E':
394         case LA_ENVELOPE:
395             ctl->server.envelope = xstrdup(optarg);
396             break;
397         case 'Q':    
398         case LA_QVIRTUAL:
399             ctl->server.qvirtual = xstrdup(optarg);
400             break;
401
402         case 'u':
403         case LA_USERNAME:
404             ctl->remotename = xstrdup(optarg);
405             break;
406         case 'a':
407         case LA_ALL:
408             ctl->fetchall = FLAG_TRUE;
409             break;
410         case 'K':
411         case LA_NOKEEP:
412             ctl->keep = FLAG_FALSE;
413             break;
414         case 'k':
415         case LA_KEEP:
416             ctl->keep = FLAG_TRUE;
417             break;
418         case 'F':
419         case LA_FLUSH:
420             ctl->flush = FLAG_TRUE;
421             break;
422         case 'n':
423         case LA_NOREWRITE:
424             ctl->rewrite = FLAG_FALSE;
425             break;
426         case 'l':
427         case LA_LIMIT:
428             c = xatoi(optarg, &errflag);
429             ctl->limit = NUM_VALUE_IN(c);
430             break;
431         case 'r':
432         case LA_FOLDER:
433             xalloca(buf, char *, strlen(optarg) + 1);
434             strcpy(buf, optarg);
435             cp = strtok(buf, ",");
436             do {
437                 save_str(&ctl->mailboxes, cp, 0);
438             } while
439                 ((cp = strtok((char *)NULL, ",")));
440             break;
441         case 'S':
442         case LA_SMTPHOST:
443             xalloca(buf, char *, strlen(optarg) + 1);
444             strcpy(buf, optarg);
445             cp = strtok(buf, ",");
446             do {
447                 save_str(&ctl->smtphunt, cp, TRUE);
448             } while
449                 ((cp = strtok((char *)NULL, ",")));
450             ocount++;
451             break;
452         case 'D':
453         case LA_SMTPADDR:
454             ctl->smtpaddress = xstrdup(optarg);
455             break;
456         case 'Z':
457         case LA_ANTISPAM:
458             xalloca(buf, char *, strlen(optarg) + 1);
459             strcpy(buf, optarg);
460             cp = strtok(buf, ",");
461             do {
462                 struct idlist   *idp = save_str(&ctl->antispam, NULL, 0);;
463
464                 idp->val.status.num = atoi(cp);
465             } while
466                 ((cp = strtok((char *)NULL, ",")));
467             break;
468         case 'b':
469         case LA_BATCHLIMIT:
470             c = xatoi(optarg, &errflag);
471             ctl->batchlimit = NUM_VALUE_IN(c);
472             break;
473         case 'B':
474         case LA_FETCHLIMIT:
475             c = xatoi(optarg, &errflag);
476             ctl->fetchlimit = NUM_VALUE_IN(c);
477             break;
478         case 'e':
479         case LA_EXPUNGE:
480             c = xatoi(optarg, &errflag);
481             ctl->expunge = NUM_VALUE_IN(c);
482             break;
483         case 'm':
484         case LA_MDA:
485             ctl->mda = xstrdup(optarg);
486             ocount++;
487             break;
488         case LA_BSMTP:
489             ctl->bsmtp = xstrdup(optarg);
490             ocount++;
491             break;
492         case LA_LMTP:
493             ctl->listener = LMTP_MODE;
494             break;
495
496         case 'T':
497         case LA_NETSEC:
498 #if NET_SECURITY
499             ctl->server.netsec = (void *)optarg;
500 #else
501             fprintf(stderr, _("fetchmail: network security support is disabled\n"));
502             errflag++;
503 #endif /* NET_SECURITY */
504             break;
505
506 #if (defined(linux) && !INET6) || defined(__FreeBSD__)
507         case 'I':
508         case LA_INTERFACE:
509             interface_parse(optarg, &ctl->server);
510             break;
511         case 'M':
512         case LA_MONITOR:
513             ctl->server.monitor = xstrdup(optarg);
514             break;
515 #endif /* (defined(linux) && !INET6) || defined(__FreeBSD__) */
516         case LA_PLUGIN:
517             ctl->server.plugin = xstrdup(optarg);
518             break;
519         case LA_PLUGOUT:
520             ctl->server.plugout = xstrdup(optarg);
521             break;
522
523         case 'y':
524         case LA_YYDEBUG:
525             yydebug = TRUE;
526             break;
527
528         case 'w':
529         case LA_WARNINGS:
530             c = xatoi(optarg, &errflag);
531             ctl->warnings = NUM_VALUE_IN(c);
532             break;
533
534         case LA_CONFIGDUMP:
535             configdump = TRUE;
536             break;
537
538         case LA_SYSLOG:
539             rctl->use_syslog = FLAG_TRUE;
540             break;
541
542         case LA_NOSYSLOG:
543             rctl->use_syslog = FLAG_FALSE;
544             break;
545
546         case '?':
547         case LA_HELP:
548         default:
549             helpflag++;
550         }
551     }
552
553     if (errflag || ocount > 1 || helpflag) {
554         /* squawk if syntax errors were detected */
555 #define P(s)    fputs(s, helpflag ? stdout : stderr)
556         P(_("usage:  fetchmail [options] [server ...]\n"));
557         P(_("  Options are as follows:\n"));
558         P(_("  -?, --help        display this option help\n"));
559         P(_("  -V, --version     display version info\n"));
560
561         P(_("  -c, --check       check for messages without fetching\n"));
562         P(_("  -s, --silent      work silently\n"));
563         P(_("  -v, --verbose     work noisily (diagnostic output)\n"));
564         P(_("  -d, --daemon      run as a daemon once per n seconds\n"));
565         P(_("  -N, --nodetach    don't detach daemon process\n"));
566         P(_("  -q, --quit        kill daemon process\n"));
567         P(_("  -L, --logfile     specify logfile name\n"));
568         P(_("      --syslog      use syslog(3) for most messages when running as a daemon\n"));
569         P(_("      --invisible   don't write Received & enable host spoofing\n"));
570         P(_("  -f, --fetchmailrc specify alternate run control file\n"));
571         P(_("  -i, --idfile      specify alternate UIDs file\n"));
572         P(_("      --postmaster  specify recipient of last resort\n"));
573         P(_("      --nobounce    redirect bounces from user to postmaster.\n"));
574 #if (defined(linux) && !INET6) || defined(__FreeBSD__)
575         P(_("  -I, --interface   interface required specification\n"));
576         P(_("  -M, --monitor     monitor interface for activity\n"));
577 #endif
578         P(_("      --plugin      specify external command to open connection\n"));
579         P(_("      --plugout     specify external command to open smtp connection\n"));
580
581         P(_("  -p, --protocol    specify retrieval protocol (see man page)\n"));
582         P(_("  -U, --uidl        force the use of UIDLs (pop3 only)\n"));
583         P(_("  -P, --port        TCP/IP service port to connect to\n"));
584         P(_("  -A, --auth        authentication type (password or kerberos)\n"));
585         P(_("  -t, --timeout     server nonresponse timeout\n"));
586         P(_("  -E, --envelope    envelope address header\n"));
587         P(_("  -Q, --qvirtual    prefix to remove from local user id\n"));
588
589         P(_("  -u, --username    specify users's login on server\n"));
590         P(_("  -a, --all         retrieve old and new messages\n"));
591         P(_("  -K, --nokeep      delete new messages after retrieval\n"));
592         P(_("  -k, --keep        save new messages after retrieval\n"));
593         P(_("  -F, --flush       delete old messages from server\n"));
594         P(_("  -n, --norewrite   don't rewrite header addresses\n"));
595         P(_("  -l, --limit       don't fetch messages over given size\n"));
596         P(_("  -w, --warnings    interval between warning mail notification\n"));
597
598 #if NET_SECURITY
599         P(_("  -T, --netsec      set IP security request\n"));
600 #endif /* NET_SECURITY */
601         P(_("  -S, --smtphost    set SMTP forwarding host\n"));
602         P(_("  -D, --smtpaddress set SMTP delivery domain to use\n"));
603         P(_("  -Z, --antispam,   set antispam response values\n"));
604         P(_("  -b, --batchlimit  set batch limit for SMTP connections\n"));
605         P(_("  -B, --fetchlimit  set fetch limit for server connections\n"));
606         P(_("  -e, --expunge     set max deletions between expunges\n"));
607         P(_("      --mda         set MDA to use for forwarding\n"));
608         P(_("      --bsmtp       set output BSMTP file\n"));
609         P(_("      --lmtp        use LMTP (RFC2033) for delivery\n"));
610         P(_("  -r, --folder      specify remote folder name\n"));
611 #undef P
612
613         if (helpflag)
614             exit(PS_SUCCESS);
615         else
616             exit(PS_SYNTAX);
617     }
618
619     return(optind);
620 }
621
622 /* options.c ends here */