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"
59 /* options still left: CgGhHjJoORTWxXYz */
60 static const char *shortoptions =
61 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:I:M:yw:D:";
63 static const struct option longoptions[] = {
64 /* this can be const because all flag fields are 0 and will never get set */
65 {"help", no_argument, (int *) 0, '?' },
66 {"version", no_argument, (int *) 0, 'V' },
67 {"check", no_argument, (int *) 0, 'c' },
68 {"silent", no_argument, (int *) 0, 's' },
69 {"verbose", no_argument, (int *) 0, 'v' },
70 {"daemon", required_argument, (int *) 0, 'd' },
71 {"nodetach", no_argument, (int *) 0, 'N' },
72 {"quit", no_argument, (int *) 0, 'q' },
73 {"logfile", required_argument, (int *) 0, 'L' },
74 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
75 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
76 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
77 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
78 {"fetchmailrc",required_argument,(int *) 0, 'f' },
79 {"idfile", required_argument, (int *) 0, 'i' },
80 {"pidfile", required_argument, (int *) 0, LA_PIDFILE },
81 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
82 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
83 {"nosoftbounce", no_argument, (int *) 0, LA_NOSOFTBOUNCE },
84 {"softbounce", no_argument, (int *) 0, LA_SOFTBOUNCE },
86 {"protocol", required_argument, (int *) 0, 'p' },
87 {"proto", required_argument, (int *) 0, 'p' },
88 {"uidl", no_argument, (int *) 0, 'U' },
89 {"idle", no_argument, (int *) 0, LA_IDLE},
90 {"port", required_argument, (int *) 0, 'P' },
91 {"service", required_argument, (int *) 0, 'P' },
92 {"auth", required_argument, (int *) 0, LA_AUTH},
93 {"timeout", required_argument, (int *) 0, 't' },
94 {"envelope", required_argument, (int *) 0, 'E' },
95 {"qvirtual", required_argument, (int *) 0, 'Q' },
97 {"user", required_argument, (int *) 0, 'u' },
98 {"username", required_argument, (int *) 0, 'u' },
100 {"all", no_argument, (int *) 0, 'a' },
101 {"fetchall", no_argument, (int *) 0, 'a' },
102 {"nokeep", no_argument, (int *) 0, 'K' },
103 {"keep", no_argument, (int *) 0, 'k' },
104 {"flush", no_argument, (int *) 0, 'F' },
105 {"limitflush", no_argument, (int *) 0, LA_LIMITFLUSH },
106 {"norewrite", no_argument, (int *) 0, 'n' },
107 {"limit", required_argument, (int *) 0, 'l' },
108 {"warnings", required_argument, (int *) 0, 'w' },
110 {"folder", required_argument, (int *) 0, 'r' },
111 {"smtphost", required_argument, (int *) 0, 'S' },
112 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
113 {"smtpaddress", required_argument, (int *) 0, 'D' },
114 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
115 {"antispam", required_argument, (int *) 0, 'Z' },
117 {"batchlimit",required_argument, (int *) 0, 'b' },
118 {"fetchlimit",required_argument, (int *) 0, 'B' },
119 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
120 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
121 {"expunge", required_argument, (int *) 0, 'e' },
122 {"mda", required_argument, (int *) 0, 'm' },
123 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
124 {"lmtp", no_argument, (int *) 0, LA_LMTP },
127 {"ssl", no_argument, (int *) 0, LA_SSL },
128 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
129 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
130 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
131 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
132 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
133 {"sslcommonname", required_argument, (int *) 0, LA_SSLCOMMONNAME },
134 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
137 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
140 {"interface", required_argument, (int *) 0, 'I' },
141 {"monitor", required_argument, (int *) 0, 'M' },
142 #endif /* CAN_MONITOR */
143 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
144 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
146 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
148 {"yydebug", no_argument, (int *) 0, 'y' },
150 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
152 {(char *) 0, no_argument, (int *) 0, 0 }
155 static int xatoi(char *s, int *errflagptr)
156 /* do safe conversion from string to number */
158 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
159 /* parse and convert numbers, but also check for invalid characters in
168 value = strtol(s, &endptr, 0);
170 /* any invalid chars in string? */
171 if ( (endptr == s) || (*endptr != '\0') ) {
172 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
177 /* is the range valid? */
178 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
179 (value > INT_MAX) || (value < INT_MIN)) {
181 (void) fprintf(stderr, GT_("Value of string '%s' is %s than %d.\n"), s,
182 (value < 0) ? GT_("smaller"): GT_("larger"),
183 (value < 0) ? INT_MIN : INT_MAX);
188 return (int) value; /* shut up, I know what I'm doing */
192 # if defined (STDC_HEADERS)
198 /* We do only base 10 conversions here (atoi)! */
201 /* check for leading white spaces */
202 for (i = 0; (i < len) && isspace((unsigned char)s[i]); i++)
208 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
210 /* skip over digits */
211 for ( /* no init */ ; (i < len) && isdigit((unsigned char)s[i]); i++)
214 /* check for trailing garbage */
216 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
221 /* atoi should be safe by now, except for number range over/underflow */
226 /** parse and validate the command line options */
227 int parsecmdline (int argc /** argument count */,
228 char **argv /** argument strings */,
229 struct runctl *rctl /** global run controls to modify */,
230 struct query *ctl /** option record to initialize */)
233 * return value: if positive, argv index of last parsed option + 1
234 * (presumes one or more server names follows). if zero, the
235 * command line switches are such that no server names are
236 * required (e.g. --version). if negative, the command line is
237 * has one or more syntax errors.
241 int ocount = 0; /* count of destinations specified */
242 int errflag = 0; /* TRUE when a syntax error is detected */
243 int helpflag = 0; /* TRUE when option help was explicitly requested */
247 rctl->poll_interval = -1;
249 memset(ctl, '\0', sizeof(struct query)); /* start clean */
250 ctl->smtp_socket = -1;
253 (c = getopt_long(argc,argv,shortoptions,
254 longoptions, &option_index)) != -1)
267 if (outlevel >= O_VERBOSE)
270 outlevel = O_VERBOSE;
273 rctl->poll_interval = xatoi(optarg, &errflag);
283 rctl->logfile = prependdir (optarg, currentwd);
286 rctl->invisible = TRUE;
289 rctl->showdots = FLAG_TRUE;
293 rcfile = prependdir (optarg, currentwd);
296 rctl->idfile = prependdir (optarg, currentwd);
299 rctl->pidfile = prependdir (optarg, currentwd);
302 rctl->postmaster = (char *) xstrdup(optarg);
305 run.bouncemail = FALSE;
307 case LA_NOSOFTBOUNCE:
308 run.softbounce = FALSE;
311 run.softbounce = TRUE;
314 /* XXX -- should probably use a table lookup here */
315 if (strcasecmp(optarg,"auto") == 0)
316 ctl->server.protocol = P_AUTO;
317 else if (strcasecmp(optarg,"pop2") == 0)
318 ctl->server.protocol = P_POP2;
320 else if (strcasecmp(optarg,"sdps") == 0)
322 ctl->server.protocol = P_POP3;
323 ctl->server.sdps = TRUE;
325 #endif /* SDPS_ENABLE */
326 else if (strcasecmp(optarg,"pop3") == 0)
327 ctl->server.protocol = P_POP3;
328 else if (strcasecmp(optarg,"apop") == 0)
329 ctl->server.protocol = P_APOP;
330 else if (strcasecmp(optarg,"rpop") == 0)
331 ctl->server.protocol = P_RPOP;
332 else if (strcasecmp(optarg,"kpop") == 0)
334 ctl->server.protocol = P_POP3;
335 ctl->server.service = KPOP_PORT;
337 ctl->server.authenticate = A_KERBEROS_V5;
339 ctl->server.authenticate = A_KERBEROS_V4;
340 #endif /* KERBEROS_V5 */
342 else if (strcasecmp(optarg,"imap") == 0)
343 ctl->server.protocol = P_IMAP;
344 else if (strcasecmp(optarg,"etrn") == 0)
345 ctl->server.protocol = P_ETRN;
346 else if (strcasecmp(optarg,"odmr") == 0)
347 ctl->server.protocol = P_ODMR;
349 fprintf(stderr,GT_("Invalid protocol `%s' specified.\n"), optarg);
354 ctl->server.uidl = FLAG_TRUE;
357 ctl->idle = FLAG_TRUE;
360 ctl->server.service = optarg;
363 if (strcmp(optarg, "password") == 0)
364 ctl->server.authenticate = A_PASSWORD;
365 else if (strcmp(optarg, "kerberos") == 0)
367 ctl->server.authenticate = A_KERBEROS_V5;
369 ctl->server.authenticate = A_KERBEROS_V4;
370 #endif /* KERBEROS_V5 */
371 else if (strcmp(optarg, "kerberos_v5") == 0)
372 ctl->server.authenticate = A_KERBEROS_V5;
373 else if (strcmp(optarg, "kerberos_v4") == 0)
374 ctl->server.authenticate = A_KERBEROS_V4;
375 else if (strcmp(optarg, "ssh") == 0)
376 ctl->server.authenticate = A_SSH;
377 else if (strcasecmp(optarg, "external") == 0)
378 ctl->server.authenticate = A_EXTERNAL;
379 else if (strcmp(optarg, "otp") == 0)
380 ctl->server.authenticate = A_OTP;
381 else if (strcmp(optarg, "opie") == 0)
382 ctl->server.authenticate = A_OTP;
383 else if (strcmp(optarg, "ntlm") == 0)
384 ctl->server.authenticate = A_NTLM;
385 else if (strcmp(optarg, "cram") == 0)
386 ctl->server.authenticate = A_CRAM_MD5;
387 else if (strcmp(optarg, "cram-md5") == 0)
388 ctl->server.authenticate = A_CRAM_MD5;
389 else if (strcmp(optarg, "gssapi") == 0)
390 ctl->server.authenticate = A_GSSAPI;
391 else if (strcmp(optarg, "any") == 0)
392 ctl->server.authenticate = A_ANY;
393 else if (strcmp(optarg, "msn") == 0)
394 ctl->server.authenticate = A_MSN;
396 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
401 ctl->server.timeout = xatoi(optarg, &errflag);
402 if (ctl->server.timeout == 0)
403 ctl->server.timeout = -1;
406 ctl->server.envelope = xstrdup(optarg);
409 ctl->server.qvirtual = xstrdup(optarg);
413 ctl->remotename = xstrdup(optarg);
416 ctl->fetchall = FLAG_TRUE;
419 ctl->keep = FLAG_FALSE;
422 ctl->keep = FLAG_TRUE;
425 ctl->flush = FLAG_TRUE;
428 ctl->limitflush = FLAG_TRUE;
431 ctl->rewrite = FLAG_FALSE;
434 c = xatoi(optarg, &errflag);
435 ctl->limit = NUM_VALUE_IN(c);
438 buf = xstrdup(optarg);
439 cp = strtok(buf, ",");
441 save_str(&ctl->mailboxes, cp, 0);
443 ((cp = strtok((char *)NULL, ",")));
447 buf = xstrdup(optarg);
448 cp = strtok(buf, ",");
450 save_str(&ctl->smtphunt, cp, TRUE);
452 ((cp = strtok((char *)NULL, ",")));
456 case LA_FETCHDOMAINS:
457 buf = xstrdup(optarg);
458 cp = strtok(buf, ",");
460 save_str(&ctl->domainlist, cp, TRUE);
462 ((cp = strtok((char *)NULL, ",")));
466 ctl->smtpaddress = xstrdup(optarg);
469 ctl->smtpname = xstrdup(optarg);
472 buf = xstrdup(optarg);
473 cp = strtok(buf, ",");
475 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
477 idp->val.status.num = xatoi(cp, &errflag);
479 ((cp = strtok((char *)NULL, ",")));
483 c = xatoi(optarg, &errflag);
484 ctl->batchlimit = NUM_VALUE_IN(c);
487 c = xatoi(optarg, &errflag);
488 ctl->fetchlimit = NUM_VALUE_IN(c);
490 case LA_FETCHSIZELIMIT:
491 c = xatoi(optarg, &errflag);
492 ctl->fetchsizelimit = NUM_VALUE_IN(c);
495 c = xatoi(optarg, &errflag);
496 ctl->fastuidl = NUM_VALUE_IN(c);
499 c = xatoi(optarg, &errflag);
500 ctl->expunge = NUM_VALUE_IN(c);
503 ctl->mda = xstrdup(optarg);
507 ctl->bsmtp = prependdir (optarg, currentwd);
511 ctl->listener = LMTP_MODE;
516 interface_parse(optarg, &ctl->server);
519 ctl->server.monitor = xstrdup(optarg);
521 #endif /* CAN_MONITOR */
523 ctl->server.plugin = xstrdup(optarg);
526 ctl->server.plugout = xstrdup(optarg);
531 ctl->use_ssl = FLAG_TRUE;
535 ctl->sslkey = prependdir (optarg, currentwd);
539 ctl->sslcert = prependdir (optarg, currentwd);
543 ctl->sslproto = xstrdup(optarg);
547 ctl->sslcertck = FLAG_TRUE;
551 ctl->sslcertpath = prependdir(optarg, currentwd);
554 case LA_SSLCOMMONNAME:
555 ctl->sslcommonname = xstrdup(optarg);
558 case LA_SSLFINGERPRINT:
559 ctl->sslfingerprint = xstrdup(optarg);
564 ctl->server.principal = xstrdup(optarg);
572 c = xatoi(optarg, &errflag);
573 ctl->warnings = NUM_VALUE_IN(c);
581 rctl->use_syslog = FLAG_TRUE;
585 rctl->use_syslog = FLAG_FALSE;
589 ctl->server.tracepolls = FLAG_TRUE;
598 if (errflag || ocount > 1 || helpflag) {
599 /* squawk if syntax errors were detected */
600 #define P(s) fputs(s, helpflag ? stdout : stderr)
601 P(GT_("usage: fetchmail [options] [server ...]\n"));
602 P(GT_(" Options are as follows:\n"));
603 P(GT_(" -?, --help display this option help\n"));
604 P(GT_(" -V, --version display version info\n"));
606 P(GT_(" -c, --check check for messages without fetching\n"));
607 P(GT_(" -s, --silent work silently\n"));
608 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
609 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
610 P(GT_(" -N, --nodetach don't detach daemon process\n"));
611 P(GT_(" -q, --quit kill daemon process\n"));
612 P(GT_(" -L, --logfile specify logfile name\n"));
613 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
614 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
615 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
616 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
617 P(GT_(" --pidfile specify alternate PID (lock) file\n"));
618 P(GT_(" --postmaster specify recipient of last resort\n"));
619 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
620 P(GT_(" --nosoftbounce fetchmail deletes permanently undeliverable messages.\n"));
621 P(GT_(" --softbounce keep permanently undeliverable messages on server (default).\n"));
623 P(GT_(" -I, --interface interface required specification\n"));
624 P(GT_(" -M, --monitor monitor interface for activity\n"));
626 #if defined( SSL_ENABLE )
627 P(GT_(" --ssl enable ssl encrypted session\n"));
628 P(GT_(" --sslkey ssl private key file\n"));
629 P(GT_(" --sslcert ssl client certificate\n"));
630 P(GT_(" --sslcertck do strict server certificate check (recommended)\n"));
631 P(GT_(" --sslcertpath path to ssl certificates\n"));
632 P(GT_(" --sslcommonname expect this CommonName from server (discouraged)\n"));
633 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\n"));
634 P(GT_(" --sslproto force ssl protocol (SSL2/SSL3/TLS1)\n"));
636 P(GT_(" --plugin specify external command to open connection\n"));
637 P(GT_(" --plugout specify external command to open smtp connection\n"));
639 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
640 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
641 P(GT_(" --port TCP port to connect to (obsolete, use --service)\n"));
642 P(GT_(" -P, --service TCP service to connect to (can be numeric TCP port)\n"));
643 P(GT_(" --auth authentication type (password/kerberos/ssh/otp)\n"));
644 P(GT_(" -t, --timeout server nonresponse timeout\n"));
645 P(GT_(" -E, --envelope envelope address header\n"));
646 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
647 P(GT_(" --principal mail service principal\n"));
648 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
650 P(GT_(" -u, --username specify users's login on server\n"));
651 P(GT_(" -a, --[fetch]all retrieve old and new messages\n"));
652 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
653 P(GT_(" -k, --keep save new messages after retrieval\n"));
654 P(GT_(" -F, --flush delete old messages from server\n"));
655 P(GT_(" --limitflush delete oversized messages\n"));
656 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
657 P(GT_(" -l, --limit don't fetch messages over given size\n"));
658 P(GT_(" -w, --warnings interval between warning mail notification\n"));
660 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
661 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
662 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
663 P(GT_(" --smtpname set SMTP full name username@domain\n"));
664 P(GT_(" -Z, --antispam, set antispam response values\n"));
665 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
666 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
667 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
668 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
669 P(GT_(" -e, --expunge set max deletions between expunges\n"));
670 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
671 P(GT_(" --bsmtp set output BSMTP file\n"));
672 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
673 P(GT_(" -r, --folder specify remote folder name\n"));
674 P(GT_(" --showdots show progress dots even in logfiles\n"));
677 * --configdump (internal use by fetchmailconf, dumps
678 * configuration as Python source code)
679 * --yydebug (developer use, enables parser debugging) */
690 /* options.c ends here */