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"
62 /* options still left: CgGhHjJoORTWxXYz */
63 static const char *shortoptions =
64 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:I:M:yw:D:";
66 static const struct option longoptions[] = {
67 /* this can be const because all flag fields are 0 and will never get set */
68 {"help", no_argument, (int *) 0, '?' },
69 {"version", no_argument, (int *) 0, 'V' },
70 {"check", no_argument, (int *) 0, 'c' },
71 {"silent", no_argument, (int *) 0, 's' },
72 {"verbose", no_argument, (int *) 0, 'v' },
73 {"daemon", required_argument, (int *) 0, 'd' },
74 {"nodetach", no_argument, (int *) 0, 'N' },
75 {"quit", no_argument, (int *) 0, 'q' },
76 {"logfile", required_argument, (int *) 0, 'L' },
77 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
78 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
79 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
80 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
81 {"fetchmailrc",required_argument,(int *) 0, 'f' },
82 {"idfile", required_argument, (int *) 0, 'i' },
83 {"pidfile", required_argument, (int *) 0, LA_PIDFILE },
84 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
85 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
86 {"nosoftbounce", no_argument, (int *) 0, LA_NOSOFTBOUNCE },
87 {"softbounce", no_argument, (int *) 0, LA_SOFTBOUNCE },
89 {"protocol", required_argument, (int *) 0, 'p' },
90 {"proto", required_argument, (int *) 0, 'p' },
91 {"uidl", no_argument, (int *) 0, 'U' },
92 {"idle", no_argument, (int *) 0, LA_IDLE},
93 {"port", required_argument, (int *) 0, 'P' },
94 {"service", required_argument, (int *) 0, 'P' },
95 {"auth", required_argument, (int *) 0, LA_AUTH},
96 {"timeout", required_argument, (int *) 0, 't' },
97 {"envelope", required_argument, (int *) 0, 'E' },
98 {"qvirtual", required_argument, (int *) 0, 'Q' },
99 {"bad-header",required_argument, (int *) 0, LA_BADHEADER},
101 {"user", required_argument, (int *) 0, 'u' },
102 {"username", required_argument, (int *) 0, 'u' },
104 {"all", no_argument, (int *) 0, 'a' },
105 {"fetchall", no_argument, (int *) 0, 'a' },
106 {"nokeep", no_argument, (int *) 0, 'K' },
107 {"keep", no_argument, (int *) 0, 'k' },
108 {"flush", no_argument, (int *) 0, 'F' },
109 {"limitflush", no_argument, (int *) 0, LA_LIMITFLUSH },
110 {"norewrite", no_argument, (int *) 0, 'n' },
111 {"limit", required_argument, (int *) 0, 'l' },
112 {"warnings", required_argument, (int *) 0, 'w' },
113 {"retrieve-error", required_argument, (int *) 0, LA_RETRIEVEERROR },
115 {"folder", required_argument, (int *) 0, 'r' },
116 {"smtphost", required_argument, (int *) 0, 'S' },
117 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
118 {"smtpaddress", required_argument, (int *) 0, 'D' },
119 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
120 {"antispam", required_argument, (int *) 0, 'Z' },
122 {"batchlimit",required_argument, (int *) 0, 'b' },
123 {"fetchlimit",required_argument, (int *) 0, 'B' },
124 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
125 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
126 {"expunge", required_argument, (int *) 0, 'e' },
127 {"mda", required_argument, (int *) 0, 'm' },
128 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
129 {"lmtp", no_argument, (int *) 0, LA_LMTP },
132 {"ssl", no_argument, (int *) 0, LA_SSL },
133 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
134 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
135 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
136 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
137 {"sslcertfile", required_argument, (int *) 0, LA_SSLCERTFILE },
138 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
139 {"sslcommonname", required_argument, (int *) 0, LA_SSLCOMMONNAME },
140 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
143 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
146 {"interface", required_argument, (int *) 0, 'I' },
147 {"monitor", required_argument, (int *) 0, 'M' },
148 #endif /* CAN_MONITOR */
149 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
150 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
152 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
154 {"yydebug", no_argument, (int *) 0, 'y' },
156 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
158 {(char *) 0, no_argument, (int *) 0, 0 }
161 static int xatoi(char *s, int *errflagptr)
162 /* do safe conversion from string to number */
164 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
165 /* parse and convert numbers, but also check for invalid characters in
174 value = strtol(s, &endptr, 0);
176 /* any invalid chars in string? */
177 if ( (endptr == s) || (*endptr != '\0') ) {
178 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
183 /* is the range valid? */
184 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
185 (value > INT_MAX) || (value < INT_MIN)) {
187 (void) fprintf(stderr, GT_("Value of string '%s' is %s than %d.\n"), s,
188 (value < 0) ? GT_("smaller"): GT_("larger"),
189 (value < 0) ? INT_MIN : INT_MAX);
194 return (int) value; /* shut up, I know what I'm doing */
198 # if defined (STDC_HEADERS)
204 /* We do only base 10 conversions here (atoi)! */
207 /* check for leading white spaces */
208 for (i = 0; (i < len) && isspace((unsigned char)s[i]); i++)
214 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
216 /* skip over digits */
217 for ( /* no init */ ; (i < len) && isdigit((unsigned char)s[i]); i++)
220 /* check for trailing garbage */
222 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
227 /* atoi should be safe by now, except for number range over/underflow */
232 /** parse and validate the command line options */
233 int parsecmdline (int argc /** argument count */,
234 char **argv /** argument strings */,
235 struct runctl *rctl /** global run controls to modify */,
236 struct query *ctl /** option record to initialize */)
239 * return value: if positive, argv index of last parsed option + 1
240 * (presumes one or more server names follows). if zero, the
241 * command line switches are such that no server names are
242 * required (e.g. --version). if negative, the command line is
243 * has one or more syntax errors.
247 int ocount = 0; /* count of destinations specified */
248 int errflag = 0; /* TRUE when a syntax error is detected */
249 int helpflag = 0; /* TRUE when option help was explicitly requested */
253 rctl->poll_interval = -1;
255 memset(ctl, '\0', sizeof(struct query)); /* start clean */
256 ctl->smtp_socket = -1;
259 (c = getopt_long(argc,argv,shortoptions,
260 longoptions, &option_index)) != -1)
273 if (outlevel >= O_VERBOSE)
276 outlevel = O_VERBOSE;
279 rctl->poll_interval = xatoi(optarg, &errflag);
289 rctl->logfile = prependdir (optarg, currentwd);
292 rctl->invisible = FLAG_TRUE;
295 rctl->showdots = FLAG_TRUE;
299 rcfile = prependdir (optarg, currentwd);
302 rctl->idfile = prependdir (optarg, currentwd);
305 rctl->pidfile = prependdir (optarg, currentwd);
308 rctl->postmaster = (char *) xstrdup(optarg);
311 rctl->bouncemail = FLAG_FALSE;
313 case LA_NOSOFTBOUNCE:
314 rctl->softbounce = FLAG_FALSE;
317 rctl->softbounce = FLAG_TRUE;
320 if (strcasecmp(optarg,"accept") == 0) {
321 ctl->server.badheader = BHACCEPT;
322 } else if (strcasecmp(optarg,"reject") == 0) {
323 ctl->server.badheader = BHREJECT;
325 fprintf(stderr,GT_("Invalid bad-header policy `%s' specified.\n"), optarg);
331 /* XXX -- should probably use a table lookup here */
332 if (strcasecmp(optarg,"auto") == 0)
333 ctl->server.protocol = P_AUTO;
334 else if (strcasecmp(optarg,"pop2") == 0)
335 ctl->server.protocol = P_POP2;
337 else if (strcasecmp(optarg,"sdps") == 0)
339 ctl->server.protocol = P_POP3;
340 ctl->server.sdps = TRUE;
342 #endif /* SDPS_ENABLE */
343 else if (strcasecmp(optarg,"pop3") == 0)
344 ctl->server.protocol = P_POP3;
345 else if (strcasecmp(optarg,"apop") == 0)
346 ctl->server.protocol = P_APOP;
347 else if (strcasecmp(optarg,"rpop") == 0)
348 ctl->server.protocol = P_RPOP;
349 else if (strcasecmp(optarg,"kpop") == 0)
351 ctl->server.protocol = P_POP3;
352 ctl->server.service = KPOP_PORT;
354 ctl->server.authenticate = A_KERBEROS_V5;
356 ctl->server.authenticate = A_KERBEROS_V4;
357 #endif /* KERBEROS_V5 */
359 else if (strcasecmp(optarg,"imap") == 0)
360 ctl->server.protocol = P_IMAP;
361 else if (strcasecmp(optarg,"etrn") == 0)
362 ctl->server.protocol = P_ETRN;
363 else if (strcasecmp(optarg,"odmr") == 0)
364 ctl->server.protocol = P_ODMR;
366 fprintf(stderr,GT_("Invalid protocol `%s' specified.\n"), optarg);
371 ctl->server.uidl = FLAG_TRUE;
374 ctl->idle = FLAG_TRUE;
377 ctl->server.service = optarg;
380 if (strcmp(optarg, "password") == 0)
381 ctl->server.authenticate = A_PASSWORD;
382 else if (strcmp(optarg, "kerberos") == 0)
384 ctl->server.authenticate = A_KERBEROS_V5;
386 ctl->server.authenticate = A_KERBEROS_V4;
387 #endif /* KERBEROS_V5 */
388 else if (strcmp(optarg, "kerberos_v5") == 0)
389 ctl->server.authenticate = A_KERBEROS_V5;
390 else if (strcmp(optarg, "kerberos_v4") == 0)
391 ctl->server.authenticate = A_KERBEROS_V4;
392 else if (strcmp(optarg, "ssh") == 0)
393 ctl->server.authenticate = A_SSH;
394 else if (strcasecmp(optarg, "external") == 0)
395 ctl->server.authenticate = A_EXTERNAL;
396 else if (strcmp(optarg, "otp") == 0)
397 ctl->server.authenticate = A_OTP;
398 else if (strcmp(optarg, "opie") == 0)
399 ctl->server.authenticate = A_OTP;
400 else if (strcmp(optarg, "ntlm") == 0)
401 ctl->server.authenticate = A_NTLM;
402 else if (strcmp(optarg, "cram") == 0)
403 ctl->server.authenticate = A_CRAM_MD5;
404 else if (strcmp(optarg, "cram-md5") == 0)
405 ctl->server.authenticate = A_CRAM_MD5;
406 else if (strcmp(optarg, "gssapi") == 0)
407 ctl->server.authenticate = A_GSSAPI;
408 else if (strcmp(optarg, "any") == 0)
409 ctl->server.authenticate = A_ANY;
410 else if (strcmp(optarg, "msn") == 0)
411 ctl->server.authenticate = A_MSN;
413 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
418 ctl->server.timeout = xatoi(optarg, &errflag);
419 if (ctl->server.timeout == 0)
420 ctl->server.timeout = -1;
423 ctl->server.envelope = xstrdup(optarg);
426 ctl->server.qvirtual = xstrdup(optarg);
430 ctl->remotename = xstrdup(optarg);
433 ctl->fetchall = FLAG_TRUE;
436 ctl->keep = FLAG_FALSE;
439 ctl->keep = FLAG_TRUE;
442 ctl->flush = FLAG_TRUE;
445 ctl->limitflush = FLAG_TRUE;
448 ctl->rewrite = FLAG_FALSE;
451 c = xatoi(optarg, &errflag);
452 ctl->limit = NUM_VALUE_IN(c);
455 buf = xstrdup(optarg);
456 cp = strtok(buf, ",");
458 save_str(&ctl->mailboxes, cp, 0);
460 ((cp = strtok((char *)NULL, ",")));
464 buf = xstrdup(optarg);
465 cp = strtok(buf, ",");
467 save_str(&ctl->smtphunt, cp, TRUE);
469 ((cp = strtok((char *)NULL, ",")));
473 case LA_FETCHDOMAINS:
474 buf = xstrdup(optarg);
475 cp = strtok(buf, ",");
477 save_str(&ctl->domainlist, cp, TRUE);
479 ((cp = strtok((char *)NULL, ",")));
483 ctl->smtpaddress = xstrdup(optarg);
486 ctl->smtpname = xstrdup(optarg);
489 buf = xstrdup(optarg);
490 cp = strtok(buf, ",");
492 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
494 idp->val.status.num = xatoi(cp, &errflag);
496 ((cp = strtok((char *)NULL, ",")));
500 c = xatoi(optarg, &errflag);
501 ctl->batchlimit = NUM_VALUE_IN(c);
504 c = xatoi(optarg, &errflag);
505 ctl->fetchlimit = NUM_VALUE_IN(c);
507 case LA_FETCHSIZELIMIT:
508 c = xatoi(optarg, &errflag);
509 ctl->fetchsizelimit = NUM_VALUE_IN(c);
512 c = xatoi(optarg, &errflag);
513 ctl->fastuidl = NUM_VALUE_IN(c);
516 c = xatoi(optarg, &errflag);
517 ctl->expunge = NUM_VALUE_IN(c);
520 ctl->mda = xstrdup(optarg);
524 ctl->bsmtp = prependdir (optarg, currentwd);
528 ctl->listener = LMTP_MODE;
533 interface_parse(optarg, &ctl->server);
536 ctl->server.monitor = xstrdup(optarg);
538 #endif /* CAN_MONITOR */
540 ctl->server.plugin = xstrdup(optarg);
543 ctl->server.plugout = xstrdup(optarg);
548 ctl->use_ssl = FLAG_TRUE;
552 ctl->sslkey = prependdir (optarg, currentwd);
556 ctl->sslcert = prependdir (optarg, currentwd);
560 ctl->sslproto = xstrdup(optarg);
564 ctl->sslcertck = FLAG_TRUE;
568 ctl->sslcertfile = prependdir(optarg, currentwd);
572 ctl->sslcertpath = prependdir(optarg, currentwd);
575 case LA_SSLCOMMONNAME:
576 ctl->sslcommonname = xstrdup(optarg);
579 case LA_SSLFINGERPRINT:
580 ctl->sslfingerprint = xstrdup(optarg);
585 ctl->server.principal = xstrdup(optarg);
593 c = xatoi(optarg, &errflag);
594 ctl->warnings = NUM_VALUE_IN(c);
602 rctl->use_syslog = FLAG_TRUE;
606 rctl->use_syslog = FLAG_FALSE;
610 ctl->server.tracepolls = FLAG_TRUE;
613 case LA_RETRIEVEERROR:
614 if (strcasecmp(optarg,"abort") == 0) {
615 ctl->server.retrieveerror = RE_ABORT;
616 } else if (strcasecmp(optarg,"continue") == 0) {
617 ctl->server.retrieveerror = RE_CONTINUE;
618 } else if (strcasecmp(optarg,"markseen") == 0) {
619 ctl->server.retrieveerror = RE_MARKSEEN;
621 fprintf(stderr,GT_("Invalid retrieve-error policy `%s' specified.\n"), optarg);
632 if (errflag || ocount > 1 || helpflag) {
633 /* squawk if syntax errors were detected */
634 #define P(s) fputs(s, helpflag ? stdout : stderr)
635 P(GT_("usage: fetchmail [options] [server ...]\n"));
636 P(GT_(" Options are as follows:\n"));
637 P(GT_(" -?, --help display this option help\n"));
638 P(GT_(" -V, --version display version info\n"));
640 P(GT_(" -c, --check check for messages without fetching\n"));
641 P(GT_(" -s, --silent work silently\n"));
642 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
643 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
644 P(GT_(" -N, --nodetach don't detach daemon process\n"));
645 P(GT_(" -q, --quit kill daemon process\n"));
646 P(GT_(" -L, --logfile specify logfile name\n"));
647 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
648 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
649 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
650 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
651 P(GT_(" --pidfile specify alternate PID (lock) file\n"));
652 P(GT_(" --postmaster specify recipient of last resort\n"));
653 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
654 P(GT_(" --nosoftbounce fetchmail deletes permanently undeliverable messages.\n"));
655 P(GT_(" --softbounce keep permanently undeliverable messages on server (default).\n"));
657 P(GT_(" -I, --interface interface required specification\n"));
658 P(GT_(" -M, --monitor monitor interface for activity\n"));
660 #if defined( SSL_ENABLE )
661 P(GT_(" --ssl enable ssl encrypted session\n"));
662 P(GT_(" --sslkey ssl private key file\n"));
663 P(GT_(" --sslcert ssl client certificate\n"));
664 P(GT_(" --sslcertck do strict server certificate check (recommended)\n"));
665 P(GT_(" --sslcertfile path to trusted-CA ssl certificate file\n"));
666 P(GT_(" --sslcertpath path to trusted-CA ssl certificate directory\n"));
667 P(GT_(" --sslcommonname expect this CommonName from server (discouraged)\n"));
668 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\n"));
669 P(GT_(" --sslproto force ssl protocol (SSL2/SSL3/TLS1)\n"));
671 P(GT_(" --plugin specify external command to open connection\n"));
672 P(GT_(" --plugout specify external command to open smtp connection\n"));
673 P(GT_(" --bad-header {reject|accept}\n"
674 " specify policy for handling messages with bad headers\n"));
675 P(GT_(" --retrieve-error {abort|continue|markseen}\n"
676 " specify policy for processing messages with retrieve errors\n"));
678 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
679 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
680 P(GT_(" --port TCP port to connect to (obsolete, use --service)\n"));
681 P(GT_(" -P, --service TCP service to connect to (can be numeric TCP port)\n"));
682 P(GT_(" --auth authentication type (password/kerberos/ssh/otp)\n"));
683 P(GT_(" -t, --timeout server nonresponse timeout\n"));
684 P(GT_(" -E, --envelope envelope address header\n"));
685 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
686 P(GT_(" --principal mail service principal\n"));
687 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
689 P(GT_(" -u, --username specify users's login on server\n"));
690 P(GT_(" -a, --[fetch]all retrieve old and new messages\n"));
691 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
692 P(GT_(" -k, --keep save new messages after retrieval\n"));
693 P(GT_(" -F, --flush delete old messages from server\n"));
694 P(GT_(" --limitflush delete oversized messages\n"));
695 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
696 P(GT_(" -l, --limit don't fetch messages over given size\n"));
697 P(GT_(" -w, --warnings interval between warning mail notification\n"));
699 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
700 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
701 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
702 P(GT_(" --smtpname set SMTP full name username@domain\n"));
703 P(GT_(" -Z, --antispam, set antispam response values\n"));
704 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
705 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
706 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
707 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
708 P(GT_(" -e, --expunge set max deletions between expunges\n"));
709 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
710 P(GT_(" --bsmtp set output BSMTP file\n"));
711 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
712 P(GT_(" -r, --folder specify remote folder name\n"));
713 P(GT_(" --showdots show progress dots even in logfiles\n"));
716 * --configdump (internal use by fetchmailconf, dumps
717 * configuration as Python source code)
718 * --yydebug (developer use, enables parser debugging) */
729 /* options.c ends here */