2 * options.c -- command-line option processing
4 * Copyright 1998 by Eric S. Raymond.
5 * For license terms, see the file COPYING in this directory.
14 #if defined(STDC_HEADERS)
22 #include "fetchmail.h"
55 /* options still left: CgGhHjJoORTWxXYz */
56 static const char *shortoptions =
57 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:I:M:yw:D:";
59 static const struct option longoptions[] = {
60 /* this can be const because all flag fields are 0 and will never get set */
61 {"help", no_argument, (int *) 0, '?' },
62 {"version", no_argument, (int *) 0, 'V' },
63 {"check", no_argument, (int *) 0, 'c' },
64 {"silent", no_argument, (int *) 0, 's' },
65 {"verbose", no_argument, (int *) 0, 'v' },
66 {"daemon", required_argument, (int *) 0, 'd' },
67 {"nodetach", no_argument, (int *) 0, 'N' },
68 {"quit", no_argument, (int *) 0, 'q' },
69 {"logfile", required_argument, (int *) 0, 'L' },
70 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
71 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
72 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
73 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
74 {"fetchmailrc",required_argument,(int *) 0, 'f' },
75 {"idfile", required_argument, (int *) 0, 'i' },
76 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
77 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
79 {"protocol", required_argument, (int *) 0, 'p' },
80 {"proto", required_argument, (int *) 0, 'p' },
81 {"uidl", no_argument, (int *) 0, 'U' },
82 {"idle", no_argument, (int *) 0, LA_IDLE},
83 {"port", required_argument, (int *) 0, 'P' },
84 {"service", required_argument, (int *) 0, 'P' },
85 {"auth", required_argument, (int *) 0, LA_AUTH},
86 {"timeout", required_argument, (int *) 0, 't' },
87 {"envelope", required_argument, (int *) 0, 'E' },
88 {"qvirtual", required_argument, (int *) 0, 'Q' },
90 {"user", required_argument, (int *) 0, 'u' },
91 {"username", required_argument, (int *) 0, 'u' },
93 {"all", no_argument, (int *) 0, 'a' },
94 {"nokeep", no_argument, (int *) 0, 'K' },
95 {"keep", no_argument, (int *) 0, 'k' },
96 {"flush", no_argument, (int *) 0, 'F' },
97 {"limitflush", no_argument, (int *) 0, LA_LIMITFLUSH },
98 {"norewrite", no_argument, (int *) 0, 'n' },
99 {"limit", required_argument, (int *) 0, 'l' },
100 {"warnings", required_argument, (int *) 0, 'w' },
102 {"folder", required_argument, (int *) 0, 'r' },
103 {"smtphost", required_argument, (int *) 0, 'S' },
104 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
105 {"smtpaddress", required_argument, (int *) 0, 'D' },
106 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
107 {"antispam", required_argument, (int *) 0, 'Z' },
109 {"batchlimit",required_argument, (int *) 0, 'b' },
110 {"fetchlimit",required_argument, (int *) 0, 'B' },
111 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
112 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
113 {"expunge", required_argument, (int *) 0, 'e' },
114 {"mda", required_argument, (int *) 0, 'm' },
115 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
116 {"lmtp", no_argument, (int *) 0, LA_LMTP },
119 {"ssl", no_argument, (int *) 0, LA_SSL },
120 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
121 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
122 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
123 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
124 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
125 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
128 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
131 {"interface", required_argument, (int *) 0, 'I' },
132 {"monitor", required_argument, (int *) 0, 'M' },
133 #endif /* CAN_MONITOR */
134 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
135 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
137 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
139 {"yydebug", no_argument, (int *) 0, 'y' },
141 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
143 {(char *) 0, no_argument, (int *) 0, 0 }
146 static int xatoi(char *s, int *errflagptr)
147 /* do safe conversion from string to number */
149 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
150 /* parse and convert numbers, but also check for invalid characters in
159 value = strtol(s, &endptr, 0);
161 /* any invalid chars in string? */
162 if ( (endptr == s) || (*endptr != '\0') ) {
163 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
168 /* is the range valid? */
169 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
170 (value > INT_MAX) || (value < INT_MIN)) {
172 (void) fprintf(stderr, GT_("Value of string '%s' is %s than %d.\n"), s,
173 (value < 0) ? GT_("smaller"): GT_("larger"),
174 (value < 0) ? INT_MIN : INT_MAX);
179 return (int) value; /* shut up, I know what I'm doing */
183 # if defined (STDC_HEADERS)
189 /* We do only base 10 conversions here (atoi)! */
192 /* check for leading white spaces */
193 for (i = 0; (i < len) && isspace((unsigned char)s[i]); i++)
199 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
201 /* skip over digits */
202 for ( /* no init */ ; (i < len) && isdigit((unsigned char)s[i]); i++)
205 /* check for trailing garbage */
207 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
212 /* atoi should be safe by now, except for number range over/underflow */
217 int parsecmdline (argc, argv, rctl, ctl)
218 /* parse and validate the command line options */
219 int argc; /* argument count */
220 char **argv; /* argument strings */
221 struct runctl *rctl; /* global run controls to modify */
222 struct query *ctl; /* option record to be initialized */
225 * return value: if positive, argv index of last parsed option + 1
226 * (presumes one or more server names follows). if zero, the
227 * command line switches are such that no server names are
228 * required (e.g. --version). if negative, the command line is
229 * has one or more syntax errors.
233 int ocount = 0; /* count of destinations specified */
234 int errflag = 0; /* TRUE when a syntax error is detected */
235 int helpflag = 0; /* TRUE when option help was explicitly requested */
239 rctl->poll_interval = -1;
241 memset(ctl, '\0', sizeof(struct query)); /* start clean */
242 ctl->smtp_socket = -1;
245 (c = getopt_long(argc,argv,shortoptions,
246 longoptions, &option_index)) != -1)
259 if (outlevel >= O_VERBOSE)
262 outlevel = O_VERBOSE;
265 rctl->poll_interval = xatoi(optarg, &errflag);
275 rctl->logfile = prependdir (optarg, currentwd);
278 rctl->invisible = TRUE;
281 rctl->showdots = FLAG_TRUE;
285 rcfile = prependdir (optarg, currentwd);
288 rctl->idfile = prependdir (optarg, currentwd);
291 rctl->postmaster = (char *) xstrdup(optarg);
294 run.bouncemail = FALSE;
297 /* XXX -- should probably use a table lookup here */
298 if (strcasecmp(optarg,"auto") == 0)
299 ctl->server.protocol = P_AUTO;
300 else if (strcasecmp(optarg,"pop2") == 0)
301 ctl->server.protocol = P_POP2;
303 else if (strcasecmp(optarg,"sdps") == 0)
305 ctl->server.protocol = P_POP3;
306 ctl->server.sdps = TRUE;
308 #endif /* SDPS_ENABLE */
309 else if (strcasecmp(optarg,"pop3") == 0)
310 ctl->server.protocol = P_POP3;
311 else if (strcasecmp(optarg,"apop") == 0)
312 ctl->server.protocol = P_APOP;
313 else if (strcasecmp(optarg,"rpop") == 0)
314 ctl->server.protocol = P_RPOP;
315 else if (strcasecmp(optarg,"kpop") == 0)
317 ctl->server.protocol = P_POP3;
318 ctl->server.service = KPOP_PORT;
320 ctl->server.authenticate = A_KERBEROS_V5;
322 ctl->server.authenticate = A_KERBEROS_V4;
323 #endif /* KERBEROS_V5 */
325 else if (strcasecmp(optarg,"imap") == 0)
326 ctl->server.protocol = P_IMAP;
327 else if (strcasecmp(optarg,"etrn") == 0)
328 ctl->server.protocol = P_ETRN;
329 else if (strcasecmp(optarg,"odmr") == 0)
330 ctl->server.protocol = P_ODMR;
332 fprintf(stderr,GT_("Invalid protocol `%s' specified.\n"), optarg);
337 ctl->server.uidl = FLAG_TRUE;
340 ctl->idle = FLAG_TRUE;
343 ctl->server.service = optarg;
346 if (strcmp(optarg, "password") == 0)
347 ctl->server.authenticate = A_PASSWORD;
348 else if (strcmp(optarg, "kerberos") == 0)
350 ctl->server.authenticate = A_KERBEROS_V5;
352 ctl->server.authenticate = A_KERBEROS_V4;
353 #endif /* KERBEROS_V5 */
354 else if (strcmp(optarg, "kerberos_v5") == 0)
355 ctl->server.authenticate = A_KERBEROS_V5;
356 else if (strcmp(optarg, "kerberos_v4") == 0)
357 ctl->server.authenticate = A_KERBEROS_V4;
358 else if (strcmp(optarg, "ssh") == 0)
359 ctl->server.authenticate = A_SSH;
360 else if (strcmp(optarg, "otp") == 0)
361 ctl->server.authenticate = A_OTP;
362 else if (strcmp(optarg, "opie") == 0)
363 ctl->server.authenticate = A_OTP;
364 else if (strcmp(optarg, "ntlm") == 0)
365 ctl->server.authenticate = A_NTLM;
366 else if (strcmp(optarg, "cram") == 0)
367 ctl->server.authenticate = A_CRAM_MD5;
368 else if (strcmp(optarg, "cram-md5") == 0)
369 ctl->server.authenticate = A_CRAM_MD5;
370 else if (strcmp(optarg, "gssapi") == 0)
371 ctl->server.authenticate = A_GSSAPI;
372 else if (strcmp(optarg, "any") == 0)
373 ctl->server.authenticate = A_ANY;
374 else if (strcmp(optarg, "msn") == 0)
375 ctl->server.authenticate = A_MSN;
377 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
382 ctl->server.timeout = xatoi(optarg, &errflag);
383 if (ctl->server.timeout == 0)
384 ctl->server.timeout = -1;
387 ctl->server.envelope = xstrdup(optarg);
390 ctl->server.qvirtual = xstrdup(optarg);
394 ctl->remotename = xstrdup(optarg);
397 ctl->fetchall = FLAG_TRUE;
400 ctl->keep = FLAG_FALSE;
403 ctl->keep = FLAG_TRUE;
406 ctl->flush = FLAG_TRUE;
409 ctl->limitflush = FLAG_TRUE;
412 ctl->rewrite = FLAG_FALSE;
415 c = xatoi(optarg, &errflag);
416 ctl->limit = NUM_VALUE_IN(c);
419 buf = xstrdup(optarg);
420 cp = strtok(buf, ",");
422 save_str(&ctl->mailboxes, cp, 0);
424 ((cp = strtok((char *)NULL, ",")));
428 buf = xstrdup(optarg);
429 cp = strtok(buf, ",");
431 save_str(&ctl->smtphunt, cp, TRUE);
433 ((cp = strtok((char *)NULL, ",")));
437 case LA_FETCHDOMAINS:
438 buf = xstrdup(optarg);
439 cp = strtok(buf, ",");
441 save_str(&ctl->domainlist, cp, TRUE);
443 ((cp = strtok((char *)NULL, ",")));
447 ctl->smtpaddress = xstrdup(optarg);
450 ctl->smtpname = xstrdup(optarg);
453 buf = xstrdup(optarg);
454 cp = strtok(buf, ",");
456 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
458 idp->val.status.num = xatoi(cp, &errflag);
460 ((cp = strtok((char *)NULL, ",")));
464 c = xatoi(optarg, &errflag);
465 ctl->batchlimit = NUM_VALUE_IN(c);
468 c = xatoi(optarg, &errflag);
469 ctl->fetchlimit = NUM_VALUE_IN(c);
471 case LA_FETCHSIZELIMIT:
472 c = xatoi(optarg, &errflag);
473 ctl->fetchsizelimit = NUM_VALUE_IN(c);
476 c = xatoi(optarg, &errflag);
477 ctl->fastuidl = NUM_VALUE_IN(c);
480 c = xatoi(optarg, &errflag);
481 ctl->expunge = NUM_VALUE_IN(c);
484 ctl->mda = xstrdup(optarg);
488 ctl->bsmtp = prependdir (optarg, currentwd);
492 ctl->listener = LMTP_MODE;
497 interface_parse(optarg, &ctl->server);
500 ctl->server.monitor = xstrdup(optarg);
502 #endif /* CAN_MONITOR */
504 ctl->server.plugin = xstrdup(optarg);
507 ctl->server.plugout = xstrdup(optarg);
512 ctl->use_ssl = FLAG_TRUE;
516 ctl->sslkey = prependdir (optarg, currentwd);
520 ctl->sslcert = prependdir (optarg, currentwd);
524 ctl->sslproto = xstrdup(optarg);
528 ctl->sslcertck = FLAG_TRUE;
532 ctl->sslcertpath = prependdir(optarg, currentwd);
535 case LA_SSLFINGERPRINT:
536 ctl->sslfingerprint = xstrdup(optarg);
541 ctl->server.principal = xstrdup(optarg);
549 c = xatoi(optarg, &errflag);
550 ctl->warnings = NUM_VALUE_IN(c);
558 rctl->use_syslog = FLAG_TRUE;
562 rctl->use_syslog = FLAG_FALSE;
566 ctl->server.tracepolls = FLAG_TRUE;
575 if (errflag || ocount > 1 || helpflag) {
576 /* squawk if syntax errors were detected */
577 #define P(s) fputs(s, helpflag ? stdout : stderr)
578 P(GT_("usage: fetchmail [options] [server ...]\n"));
579 P(GT_(" Options are as follows:\n"));
580 P(GT_(" -?, --help display this option help\n"));
581 P(GT_(" -V, --version display version info\n"));
583 P(GT_(" -c, --check check for messages without fetching\n"));
584 P(GT_(" -s, --silent work silently\n"));
585 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
586 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
587 P(GT_(" -N, --nodetach don't detach daemon process\n"));
588 P(GT_(" -q, --quit kill daemon process\n"));
589 P(GT_(" -L, --logfile specify logfile name\n"));
590 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
591 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
592 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
593 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
594 P(GT_(" --postmaster specify recipient of last resort\n"));
595 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
597 P(GT_(" -I, --interface interface required specification\n"));
598 P(GT_(" -M, --monitor monitor interface for activity\n"));
600 #if defined( SSL_ENABLE )
601 P(GT_(" --ssl enable ssl encrypted session\n"));
602 P(GT_(" --sslkey ssl private key file\n"));
603 P(GT_(" --sslcert ssl client certificate\n"));
604 P(GT_(" --sslcertck do strict server certificate check (recommended)\n"));
605 P(GT_(" --sslcertpath path to ssl certificates\n"));
606 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\n"));
607 P(GT_(" --sslproto force ssl protocol (ssl2/ssl3/tls1)\n"));
609 P(GT_(" --plugin specify external command to open connection\n"));
610 P(GT_(" --plugout specify external command to open smtp connection\n"));
612 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
613 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
614 P(GT_(" --port TCP port to connect to (obsolete, use --service)\n"));
615 P(GT_(" -P, --service TCP service to connect to (can be numeric TCP port)\n"));
616 P(GT_(" --auth authentication type (password/kerberos/ssh/otp)\n"));
617 P(GT_(" -t, --timeout server nonresponse timeout\n"));
618 P(GT_(" -E, --envelope envelope address header\n"));
619 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
620 P(GT_(" --principal mail service principal\n"));
621 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
623 P(GT_(" -u, --username specify users's login on server\n"));
624 P(GT_(" -a, --all retrieve old and new messages\n"));
625 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
626 P(GT_(" -k, --keep save new messages after retrieval\n"));
627 P(GT_(" -F, --flush delete old messages from server\n"));
628 P(GT_(" --limitflush delete oversized messages\n"));
629 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
630 P(GT_(" -l, --limit don't fetch messages over given size\n"));
631 P(GT_(" -w, --warnings interval between warning mail notification\n"));
633 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
634 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
635 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
636 P(GT_(" --smtpname set SMTP full name username@domain\n"));
637 P(GT_(" -Z, --antispam, set antispam response values\n"));
638 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
639 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
640 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
641 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
642 P(GT_(" -e, --expunge set max deletions between expunges\n"));
643 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
644 P(GT_(" --bsmtp set output BSMTP file\n"));
645 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
646 P(GT_(" -r, --folder specify remote folder name\n"));
647 P(GT_(" --showdots show progress dots even in logfiles\n"));
650 * --configdump (internal use by fetchmailconf, dumps
651 * configuration as Python source code)
652 * --yydebug (developer use, enables parser debugging) */
663 /* options.c ends here */