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