2 * options.c -- command-line option processing
4 * For license terms, see the file COPYING in this directory.
13 #if defined(STDC_HEADERS)
21 #include "fetchmail.h"
32 #define LA_INVISIBLE 10
34 #define LA_NOSYSLOG 12
37 #define LA_POSTMASTER 15
38 #define LA_PROTOCOL 16
41 #define LA_AUTHENTICATE 19
43 #define LA_ENVELOPE 21
44 #define LA_QVIRTUAL 22
45 #define LA_USERNAME 23
50 #define LA_NOREWRITE 28
53 #define LA_SMTPHOST 31
54 #define LA_SMTPADDR 32
55 #define LA_ANTISPAM 33
56 #define LA_BATCHLIMIT 34
57 #define LA_FETCHLIMIT 35
61 #define LA_INTERFACE 39
63 #define LA_CONFIGDUMP 42
66 /* options still left: CgGhHjJoORwWxXYz */
67 static const char *shortoptions =
68 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:y";
70 static const struct option longoptions[] = {
71 /* this can be const because all flag fields are 0 and will never get set */
72 {"help", no_argument, (int *) 0, LA_HELP },
73 {"version", no_argument, (int *) 0, LA_VERSION },
74 {"check", no_argument, (int *) 0, LA_CHECK },
75 {"silent", no_argument, (int *) 0, LA_SILENT },
76 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
77 {"daemon", required_argument, (int *) 0, LA_DAEMON },
78 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
79 {"quit", no_argument, (int *) 0, LA_QUIT },
80 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
81 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
82 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
83 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
84 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
85 {"idfile", required_argument, (int *) 0, LA_IDFILE },
86 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
88 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
89 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
90 {"uidl", no_argument, (int *) 0, LA_UIDL },
91 {"port", required_argument, (int *) 0, LA_PORT },
92 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
93 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
94 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
95 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
97 {"user", required_argument, (int *) 0, LA_USERNAME },
98 {"username", required_argument, (int *) 0, LA_USERNAME },
100 {"all", no_argument, (int *) 0, LA_ALL },
101 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
102 {"keep", no_argument, (int *) 0, LA_KEEP },
103 {"flush", no_argument, (int *) 0, LA_FLUSH },
104 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
105 {"limit", required_argument, (int *) 0, LA_LIMIT },
107 {"folder", required_argument, (int *) 0, LA_FOLDER },
108 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
109 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
110 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
112 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
113 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
114 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
115 {"mda", required_argument, (int *) 0, LA_MDA },
118 {"netsec", required_argument, (int *) 0, LA_NETSEC },
121 #if defined(linux) && !INET6
122 {"interface", required_argument, (int *) 0, LA_INTERFACE },
123 {"monitor", required_argument, (int *) 0, LA_MONITOR },
124 #endif /* defined(linux) && !INET6 */
126 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
128 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
130 {(char *) 0, no_argument, (int *) 0, 0 }
133 static int xatoi(char *s, int *errflagptr)
134 /* do safe conversion from string to number */
136 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
137 /* parse and convert numbers, but also check for invalid characters in
146 value = strtol(s, &endptr, 0);
148 /* any invalid chars in string? */
149 if ( (endptr == s) || (*endptr != '\0') ) {
150 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
155 /* is the range valid? */
156 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
157 (value > INT_MAX) || (value < INT_MIN)) {
159 (void) fprintf(stderr, "Value of string '%s' is %s than %d.\n", s,
160 (value < 0) ? "smaller": "larger",
161 (value < 0) ? INT_MIN : INT_MAX);
166 return (int) value; /* shut up, I know what I'm doing */
170 # if defined (STDC_HEADERS)
176 /* We do only base 10 conversions here (atoi)! */
179 /* check for leading white spaces */
180 for (i = 0; (i < len) && isspace(s[i]); i++)
186 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
188 /* skip over digits */
189 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
192 /* check for trailing garbage */
194 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
199 /* atoi should be safe by now, except for number range over/underflow */
204 int parsecmdline (argc, argv, rctl, ctl)
205 /* parse and validate the command line options */
206 int argc; /* argument count */
207 char **argv; /* argument strings */
208 struct runctl *rctl; /* global run controls to modify */
209 struct query *ctl; /* option record to be initialized */
212 * return value: if positive, argv index of last parsed option + 1
213 * (presumes one or more server names follows). if zero, the
214 * command line switches are such that no server names are
215 * required (e.g. --version). if negative, the command line is
216 * has one or more syntax errors.
220 int ocount = 0; /* count of destinations specified */
221 int errflag = 0; /* TRUE when a syntax error is detected */
225 rctl->poll_interval = -1;
227 memset(ctl, '\0', sizeof(struct query)); /* start clean */
228 ctl->smtp_socket = -1;
231 (c = getopt_long(argc,argv,shortoptions,
232 longoptions,&option_index)) != -1) {
249 outlevel = O_VERBOSE;
253 rctl->poll_interval = xatoi(optarg, &errflag);
265 rctl->logfile = optarg;
268 rctl->invisible = TRUE;
272 rcfile = (char *) xmalloc(strlen(optarg)+1);
273 strcpy(rcfile,optarg);
277 rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
278 strcpy(rctl->idfile,optarg);
281 rctl->postmaster = (char *) xmalloc(strlen(optarg)+1);
285 /* XXX -- should probably use a table lookup here */
286 if (strcasecmp(optarg,"pop2") == 0)
287 ctl->server.protocol = P_POP2;
288 else if (strcasecmp(optarg,"pop3") == 0)
289 ctl->server.protocol = P_POP3;
290 else if (strcasecmp(optarg,"apop") == 0)
291 ctl->server.protocol = P_APOP;
292 else if (strcasecmp(optarg,"rpop") == 0)
293 ctl->server.protocol = P_RPOP;
294 else if (strcasecmp(optarg,"kpop") == 0)
296 ctl->server.protocol = P_POP3;
298 ctl->server.service = KPOP_PORT;
300 ctl->server.port = KPOP_PORT;
303 ctl->server.preauthenticate = A_KERBEROS_V5;
305 ctl->server.preauthenticate = A_KERBEROS_V4;
306 #endif /* KERBEROS_V5 */
308 else if (strcasecmp(optarg,"imap") == 0)
309 ctl->server.protocol = P_IMAP;
311 else if (strcasecmp(optarg,"imap-k4") == 0)
312 ctl->server.protocol = P_IMAP_K4;
313 #endif /* KERBEROS_V4 */
315 else if (strcasecmp(optarg, "imap-gss") == 0)
316 ctl->server.protocol = P_IMAP_GSS;
318 else if (strcasecmp(optarg,"etrn") == 0)
319 ctl->server.protocol = P_ETRN;
321 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
327 ctl->server.uidl = FLAG_TRUE;
332 ctl->server.service = optarg;
334 ctl->server.port = xatoi(optarg, &errflag);
338 case LA_AUTHENTICATE:
339 if (strcmp(optarg, "password") == 0)
340 ctl->server.preauthenticate = A_PASSWORD;
341 else if (strcmp(optarg, "kerberos") == 0)
343 ctl->server.preauthenticate = A_KERBEROS_V5;
344 else if (strcmp(optarg, "kerberos_v5") == 0)
345 ctl->server.preauthenticate = A_KERBEROS_V5;
347 ctl->server.preauthenticate = A_KERBEROS_V4;
348 else if (strcmp(optarg, "kerberos_v4") == 0)
349 ctl->server.preauthenticate = A_KERBEROS_V4;
350 #endif /* KERBEROS_V5 */
352 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
358 ctl->server.timeout = xatoi(optarg, &errflag);
359 if (ctl->server.timeout == 0)
360 ctl->server.timeout = -1;
364 ctl->server.envelope = xstrdup(optarg);
368 ctl->server.qvirtual = xstrdup(optarg);
373 ctl->remotename = xstrdup(optarg);
377 ctl->fetchall = FLAG_TRUE;
381 ctl->keep = FLAG_FALSE;
385 ctl->keep = FLAG_TRUE;
389 ctl->flush = FLAG_TRUE;
393 ctl->rewrite = FLAG_FALSE;
397 c = xatoi(optarg, &errflag);
398 ctl->limit = NUM_VALUE(c);
402 buf = xmalloc(strlen(optarg));
404 cp = strtok(buf, ",");
406 save_str(&ctl->mailboxes, cp, 0);
408 ((cp = strtok((char *)NULL, ",")));
413 buf = xmalloc(strlen(optarg));
415 cp = strtok(buf, ",");
417 save_str(&ctl->smtphunt, cp, TRUE);
419 ((cp = strtok((char *)NULL, ",")));
425 ctl->smtpaddress = xstrdup(optarg);
429 buf = xmalloc(strlen(optarg));
431 cp = strtok(buf, ",");
433 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
435 idp->val.status.num = atoi(cp);
437 ((cp = strtok((char *)NULL, ",")));
442 c = xatoi(optarg, &errflag);
443 ctl->batchlimit = NUM_VALUE(c);
447 c = xatoi(optarg, &errflag);
448 ctl->fetchlimit = NUM_VALUE(c);
452 c = xatoi(optarg, &errflag);
453 ctl->expunge = NUM_VALUE(c);
457 ctl->mda = xstrdup(optarg);
464 ctl->server.netsec = (void *)optarg;
466 fprintf(stderr, "fetchmail: network security support is disabled\n");
468 #endif /* NET_SECURITY */
471 #if defined(linux) && !INET6
474 interface_parse(optarg, &ctl->server);
478 ctl->server.monitor = xstrdup(optarg);
480 #endif /* defined(linux) && !INET6 */
492 rctl->use_syslog = FLAG_TRUE;
496 rctl->use_syslog = FLAG_FALSE;
506 if (errflag || ocount > 1) {
507 /* squawk if syntax errors were detected */
508 fputs("usage: fetchmail [options] [server ...]\n", stderr);
509 fputs(" Options are as follows:\n",stderr);
510 fputs(" -?, --help display this option help\n", stderr);
511 fputs(" -V, --version display version info\n", stderr);
513 fputs(" -c, --check check for messages without fetching\n", stderr);
514 fputs(" -s, --silent work silently\n", stderr);
515 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
516 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
517 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
518 fputs(" -q, --quit kill daemon process\n", stderr);
519 fputs(" -L, --logfile specify logfile name\n", stderr);
520 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
521 fputs(" --invisible suppress Received line & enable host spoofing\n", stderr);
522 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
523 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
524 fputs(" --postmaster specify recipient of last resort\n",stderr);
525 #if defined(linux) && !INET6
526 fputs(" -I, --interface interface required specification\n",stderr);
527 fputs(" -M, --monitor monitor interface for activity\n",stderr);
531 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn, imap-k4\n", stderr);
533 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
534 #endif /* KERBEROS_V4 */
535 fputs(" -U, --uidl force the use of UIDLs (pop3 only)\n", stderr);
536 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
537 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
538 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
539 fputs(" -E, --envelope envelope address header\n",stderr);
540 fputs(" -Q, --qvirtual prefix to remove from local user id\n",stderr);
542 fputs(" -u, --username specify users's login on server\n", stderr);
543 fputs(" -a, --all retrieve old and new messages\n", stderr);
544 fputs(" -K, --nokeep delete new messages after retrieval\n", stderr);
545 fputs(" -k, --keep save new messages after retrieval\n", stderr);
546 fputs(" -F, --flush delete old messages from server\n", stderr);
547 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
548 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
551 fputs(" -T, --netsec set IP security request\n", stderr);
552 #endif /* NET_SECURITY */
553 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
554 fputs(" -D, --smtpaddress set SMTP delivery domain to use\n", stderr);
555 fputs(" -Z, --antispam, set antispam response values\n", stderr);
556 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
557 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
558 fputs(" -e, --expunge set max deletions between expunges\n", stderr);
559 fputs(" -r, --folder specify remote folder name\n", stderr);
566 /* options.c ends here */