]> Pileus Git - ~andy/fetchmail/blob - options.c
63238ea535b87704e798b78c011897ef9cc85502
[~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 option_index;
235     char *buf, *cp;
236
237     rctl->poll_interval = -1;
238
239     memset(ctl, '\0', sizeof(struct query));    /* start clean */
240     ctl->smtp_socket = -1;
241
242     while (!errflag && 
243            (c = getopt_long(argc,argv,shortoptions,
244                             longoptions,&option_index)) != -1) {
245
246         switch (c) {
247         case 'V':
248         case LA_VERSION:
249             versioninfo = TRUE;
250             break;
251         case 'c':
252         case LA_CHECK:
253             check_only = TRUE;
254             break;
255         case 's':
256         case LA_SILENT:
257             outlevel = O_SILENT;
258             break;
259         case 'v':
260         case LA_VERBOSE:
261             if (outlevel == O_VERBOSE)
262                 outlevel = O_DEBUG;
263             else
264                 outlevel = O_VERBOSE;
265             break;
266         case 'd':
267         case LA_DAEMON:
268             rctl->poll_interval = xatoi(optarg, &errflag);
269             break;
270         case 'N':
271         case LA_NODETACH:
272             nodetach = TRUE;
273             break;
274         case 'q':
275         case LA_QUIT:
276             quitmode = TRUE;
277             break;
278         case 'L':
279         case LA_LOGFILE:
280             rctl->logfile = optarg;
281             break;
282         case LA_INVISIBLE:
283             rctl->invisible = TRUE;
284             break;
285         case 'f':
286         case LA_RCFILE:
287             rcfile = (char *) xmalloc(strlen(optarg)+1);
288             strcpy(rcfile,optarg);
289             break;
290         case 'i':
291         case LA_IDFILE:
292             rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
293             strcpy(rctl->idfile,optarg);
294             break;
295         case LA_POSTMASTER:
296             rctl->postmaster = (char *) xmalloc(strlen(optarg)+1);
297             strcpy(rctl->postmaster,optarg);
298             break;
299         case 'p':
300         case LA_PROTOCOL:
301             /* XXX -- should probably use a table lookup here */
302             if (strcasecmp(optarg,"auto") == 0)
303                 ctl->server.protocol = P_AUTO;
304             else if (strcasecmp(optarg,"pop2") == 0)
305                 ctl->server.protocol = P_POP2;
306 #ifdef SDPS_ENABLE
307             else if (strcasecmp(optarg,"sdps") == 0)
308             {
309                 ctl->server.protocol = P_POP3; 
310                 ctl->server.sdps = TRUE;
311             }
312 #endif /* SDPS_ENABLE */
313             else if (strcasecmp(optarg,"pop3") == 0)
314                 ctl->server.protocol = P_POP3;
315             else if (strcasecmp(optarg,"apop") == 0)
316                 ctl->server.protocol = P_APOP;
317             else if (strcasecmp(optarg,"rpop") == 0)
318                 ctl->server.protocol = P_RPOP;
319             else if (strcasecmp(optarg,"kpop") == 0)
320             {
321                 ctl->server.protocol = P_POP3;
322 #if INET6
323                 ctl->server.service = KPOP_PORT;
324 #else /* INET6 */
325                 ctl->server.port = KPOP_PORT;
326 #endif /* INET6 */
327 #ifdef KERBEROS_V5
328                 ctl->server.preauthenticate =  A_KERBEROS_V5;
329 #else
330                 ctl->server.preauthenticate =  A_KERBEROS_V4;
331 #endif /* KERBEROS_V5 */
332             }
333             else if (strcasecmp(optarg,"imap") == 0)
334                 ctl->server.protocol = P_IMAP;
335 #ifdef KERBEROS_V4
336             else if (strcasecmp(optarg,"imap-k4") == 0)
337                 ctl->server.protocol = P_IMAP_K4;
338 #endif /* KERBEROS_V4 */
339 #ifdef GSSAPI
340             else if (strcasecmp(optarg, "imap-gss") == 0)
341                 ctl->server.protocol = P_IMAP_GSS;
342 #endif /* GSSAPI */
343             else if (strcasecmp(optarg,"etrn") == 0)
344                 ctl->server.protocol = P_ETRN;
345             else {
346                 fprintf(stderr,_("Invalid protocol `%s' specified.\n"), optarg);
347                 errflag++;
348             }
349             break;
350         case 'U':
351         case LA_UIDL:
352             ctl->server.uidl = FLAG_TRUE;
353             break;
354         case 'P':
355         case LA_PORT:
356 #if INET6
357             ctl->server.service = optarg;
358 #else /* INET6 */
359             ctl->server.port = xatoi(optarg, &errflag);
360 #endif /* INET6 */
361             break;
362         case 'A':
363         case LA_AUTHENTICATE:
364             if (strcmp(optarg, "password") == 0)
365                 ctl->server.preauthenticate = A_PASSWORD;
366             else if (strcmp(optarg, "kerberos") == 0)
367 #ifdef KERBEROS_V5
368                 ctl->server.preauthenticate = A_KERBEROS_V5;
369 #else
370                 ctl->server.preauthenticate = A_KERBEROS_V4;
371 #endif /* KERBEROS_V5 */
372             else if (strcmp(optarg, "kerberos_v5") == 0)
373                 ctl->server.preauthenticate = A_KERBEROS_V5;
374             else if (strcmp(optarg, "kerberos_v4") == 0)
375                 ctl->server.preauthenticate = A_KERBEROS_V4;
376             else {
377                 fprintf(stderr,_("Invalid preauthentication `%s' specified.\n"), optarg);
378                 errflag++;
379             }
380             break;
381         case 't':
382         case LA_TIMEOUT:
383             ctl->server.timeout = xatoi(optarg, &errflag);
384             if (ctl->server.timeout == 0)
385                 ctl->server.timeout = -1;
386             break;
387         case 'E':
388         case LA_ENVELOPE:
389             ctl->server.envelope = xstrdup(optarg);
390             break;
391         case 'Q':    
392         case LA_QVIRTUAL:
393             ctl->server.qvirtual = xstrdup(optarg);
394             break;
395
396         case 'u':
397         case LA_USERNAME:
398             ctl->remotename = xstrdup(optarg);
399             break;
400         case 'a':
401         case LA_ALL:
402             ctl->fetchall = FLAG_TRUE;
403             break;
404         case 'K':
405         case LA_NOKEEP:
406             ctl->keep = FLAG_FALSE;
407             break;
408         case 'k':
409         case LA_KEEP:
410             ctl->keep = FLAG_TRUE;
411             break;
412         case 'F':
413         case LA_FLUSH:
414             ctl->flush = FLAG_TRUE;
415             break;
416         case 'n':
417         case LA_NOREWRITE:
418             ctl->rewrite = FLAG_FALSE;
419             break;
420         case 'l':
421         case LA_LIMIT:
422             c = xatoi(optarg, &errflag);
423             ctl->limit = NUM_VALUE(c);
424             break;
425         case 'r':
426         case LA_FOLDER:
427             xalloca(buf, char *, strlen(optarg));
428             strcpy(buf, optarg);
429             cp = strtok(buf, ",");
430             do {
431                 save_str(&ctl->mailboxes, cp, 0);
432             } while
433                 ((cp = strtok((char *)NULL, ",")));
434             break;
435         case 'S':
436         case LA_SMTPHOST:
437             xalloca(buf, char *, strlen(optarg));
438             strcpy(buf, optarg);
439             cp = strtok(buf, ",");
440             do {
441                 save_str(&ctl->smtphunt, cp, TRUE);
442             } while
443                 ((cp = strtok((char *)NULL, ",")));
444             ocount++;
445             break;
446         case 'D':
447         case LA_SMTPADDR:
448             ctl->smtpaddress = xstrdup(optarg);
449             break;
450         case 'Z':
451         case LA_ANTISPAM:
452             xalloca(buf, char *, strlen(optarg));
453             strcpy(buf, optarg);
454             cp = strtok(buf, ",");
455             do {
456                 struct idlist   *idp = save_str(&ctl->antispam, NULL, 0);;
457
458                 idp->val.status.num = atoi(cp);
459             } while
460                 ((cp = strtok((char *)NULL, ",")));
461             break;
462         case 'b':
463         case LA_BATCHLIMIT:
464             c = xatoi(optarg, &errflag);
465             ctl->batchlimit = NUM_VALUE(c);
466             break;
467         case 'B':
468         case LA_FETCHLIMIT:
469             c = xatoi(optarg, &errflag);
470             ctl->fetchlimit = NUM_VALUE(c);
471             break;
472         case 'e':
473         case LA_EXPUNGE:
474             c = xatoi(optarg, &errflag);
475             ctl->expunge = NUM_VALUE(c);
476             break;
477         case 'm':
478         case LA_MDA:
479             ctl->mda = xstrdup(optarg);
480             ocount++;
481             break;
482         case LA_BSMTP:
483             ctl->bsmtp = xstrdup(optarg);
484             ocount++;
485             break;
486         case LA_LMTP:
487             ctl->listener = LMTP_MODE;
488             break;
489
490         case 'T':
491         case LA_NETSEC:
492 #if NET_SECURITY
493             ctl->server.netsec = (void *)optarg;
494 #else
495             fprintf(stderr, _("fetchmail: network security support is disabled\n"));
496             errflag++;
497 #endif /* NET_SECURITY */
498             break;
499
500 #if defined(linux) && !INET6
501         case 'I':
502         case LA_INTERFACE:
503             interface_parse(optarg, &ctl->server);
504             break;
505         case 'M':
506         case LA_MONITOR:
507             ctl->server.monitor = xstrdup(optarg);
508             break;
509 #endif /* defined(linux) && !INET6 */
510         case LA_PLUGIN:
511             ctl->server.plugin = xstrdup(optarg);
512             break;
513         case LA_PLUGOUT:
514             ctl->server.plugout = xstrdup(optarg);
515             break;
516
517         case 'y':
518         case LA_YYDEBUG:
519             yydebug = TRUE;
520             break;
521
522         case 'w':
523         case LA_WARNINGS:
524             c = xatoi(optarg, &errflag);
525             ctl->warnings = NUM_VALUE(c);
526             break;
527
528         case LA_CONFIGDUMP:
529             configdump = TRUE;
530             break;
531
532         case LA_SYSLOG:
533             rctl->use_syslog = FLAG_TRUE;
534             break;
535
536         case LA_NOSYSLOG:
537             rctl->use_syslog = FLAG_FALSE;
538             break;
539
540         case '?':
541         case LA_HELP:
542         default:
543             errflag++;
544         }
545     }
546
547     if (errflag || ocount > 1) {
548         /* squawk if syntax errors were detected */
549 #define P(s)    fputs(s, stderr)
550         P(_("usage:  fetchmail [options] [server ...]\n"));
551         P(_("  Options are as follows:\n"));
552         P(_("  -?, --help        display this option help\n"));
553         P(_("  -V, --version     display version info\n"));
554
555         P(_("  -c, --check       check for messages without fetching\n"));
556         P(_("  -s, --silent      work silently\n"));
557         P(_("  -v, --verbose     work noisily (diagnostic output)\n"));
558         P(_("  -d, --daemon      run as a daemon once per n seconds\n"));
559         P(_("  -N, --nodetach    don't detach daemon process\n"));
560         P(_("  -q, --quit        kill daemon process\n"));
561         P(_("  -L, --logfile     specify logfile name\n"));
562         P(_("      --syslog      use syslog(3) for most messages when running as a daemon\n"));
563         P(_("      --invisible   don't write Received & enable host spoofing\n"));
564         P(_("  -f, --fetchmailrc specify alternate run control file\n"));
565         P(_("  -i, --idfile      specify alternate UIDs file\n"));
566         P(_("      --postmaster  specify recipient of last resort\n"));
567 #if defined(linux) && !INET6
568         P(_("  -I, --interface   interface required specification\n"));
569         P(_("  -M, --monitor     monitor interface for activity\n"));
570 #endif
571         P(_("      --plugin      specify external command to open connection\n"));
572         P(_("      --plugout     specify external command to open smtp connection\n"));
573
574         P(_("  -p, --protocol    specify retrieval protocol (see man page)\n"));
575         P(_("  -U, --uidl        force the use of UIDLs (pop3 only)\n"));
576         P(_("  -P, --port        TCP/IP service port to connect to\n"));
577         P(_("  -A, --auth        authentication type (password or kerberos)\n"));
578         P(_("  -t, --timeout     server nonresponse timeout\n"));
579         P(_("  -E, --envelope    envelope address header\n"));
580         P(_("  -Q, --qvirtual    prefix to remove from local user id\n"));
581
582         P(_("  -u, --username    specify users's login on server\n"));
583         P(_("  -a, --all         retrieve old and new messages\n"));
584         P(_("  -K, --nokeep      delete new messages after retrieval\n"));
585         P(_("  -k, --keep        save new messages after retrieval\n"));
586         P(_("  -F, --flush       delete old messages from server\n"));
587         P(_("  -n, --norewrite   don't rewrite header addresses\n"));
588         P(_("  -l, --limit       don't fetch messages over given size\n"));
589         P(_("  -w, --warnings    interval between warning mail notification\n"));
590
591 #if NET_SECURITY
592         P(_("  -T, --netsec      set IP security request\n"));
593 #endif /* NET_SECURITY */
594         P(_("  -S, --smtphost    set SMTP forwarding host\n"));
595         P(_("  -D, --smtpaddress set SMTP delivery domain to use\n"));
596         P(_("  -Z, --antispam,   set antispam response values\n"));
597         P(_("  -b, --batchlimit  set batch limit for SMTP connections\n"));
598         P(_("  -B, --fetchlimit  set fetch limit for server connections\n"));
599         P(_("  -e, --expunge     set max deletions between expunges\n"));
600         P(_("      --mda         set MDA to use for forwarding\n"));
601         P(_("      --bsmtp       set output BSMTP file\n"));
602         P(_("      --lmtp        use LMTP (RFC2033) for delivery\n"));
603         P(_("  -r, --folder      specify remote folder name\n"));
604 #undef P
605         return(-1);
606     }
607
608     return(optind);
609 }
610
611 /* options.c ends here */