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.
59 /* options still left: ACgGhHjJoORTWxXYz */
60 static const char *shortoptions =
61 "?Vcsvd:NqL:f:i:p:UP: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' },
96 {"bad-header",required_argument, (int *) 0, LA_BADHEADER},
98 {"user", required_argument, (int *) 0, 'u' },
99 {"username", required_argument, (int *) 0, 'u' },
101 {"all", no_argument, (int *) 0, 'a' },
102 {"fetchall", no_argument, (int *) 0, 'a' },
103 {"nokeep", no_argument, (int *) 0, 'K' },
104 {"keep", no_argument, (int *) 0, 'k' },
105 {"flush", no_argument, (int *) 0, 'F' },
106 {"limitflush", no_argument, (int *) 0, LA_LIMITFLUSH },
107 {"norewrite", no_argument, (int *) 0, 'n' },
108 {"limit", required_argument, (int *) 0, 'l' },
109 {"warnings", required_argument, (int *) 0, 'w' },
111 {"folder", required_argument, (int *) 0, 'r' },
112 {"smtphost", required_argument, (int *) 0, 'S' },
113 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
114 {"smtpaddress", required_argument, (int *) 0, 'D' },
115 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
116 {"antispam", required_argument, (int *) 0, 'Z' },
118 {"batchlimit",required_argument, (int *) 0, 'b' },
119 {"fetchlimit",required_argument, (int *) 0, 'B' },
120 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
121 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
122 {"expunge", required_argument, (int *) 0, 'e' },
123 {"mda", required_argument, (int *) 0, 'm' },
124 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
125 {"lmtp", no_argument, (int *) 0, LA_LMTP },
128 {"ssl", no_argument, (int *) 0, LA_SSL },
129 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
130 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
131 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
132 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
133 {"nosslcertck", no_argument, (int *) 0, LA_NOSSLCERTCK },
134 {"sslcertfile", required_argument, (int *) 0, LA_SSLCERTFILE },
135 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
136 {"sslcommonname", required_argument, (int *) 0, LA_SSLCOMMONNAME },
137 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
140 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
143 {"interface", required_argument, (int *) 0, 'I' },
144 {"monitor", required_argument, (int *) 0, 'M' },
145 #endif /* CAN_MONITOR */
146 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
147 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
149 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
151 {"yydebug", no_argument, (int *) 0, 'y' },
153 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
155 {(char *) 0, no_argument, (int *) 0, 0 }
158 static int xatoi(char *s, int *errflagptr)
159 /* do safe conversion from string to number */
161 /* parse and convert numbers, but also check for invalid characters in
170 value = strtol(s, &endptr, 0);
172 /* any invalid chars in string? */
173 if ( (endptr == s) || (*endptr != '\0') ) {
174 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
179 /* is the range valid? */
180 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
181 (value > INT_MAX) || (value < INT_MIN)) {
183 (void) fprintf(stderr, GT_("Value of string '%s' is %s than %d.\n"), s,
184 (value < 0) ? GT_("smaller"): GT_("larger"),
185 (value < 0) ? INT_MIN : INT_MAX);
190 return (int) value; /* shut up, I know what I'm doing */
193 /** parse and validate the command line options */
194 int parsecmdline (int argc /** argument count */,
195 char **argv /** argument strings */,
196 struct runctl *rctl /** global run controls to modify */,
197 struct query *ctl /** option record to initialize */,
198 flag *safewithbg /** set to whether options are
199 compatible with another copy
200 running in the background */)
203 * return value: if positive, argv index of last parsed option + 1
204 * (presumes one or more server names follows). if zero, the
205 * command line switches are such that no server names are
206 * required (e.g. --version). if negative, the command line is
207 * has one or more syntax errors.
211 int ocount = 0; /* count of destinations specified */
212 int errflag = 0; /* TRUE when a syntax error is detected */
213 int helpflag = 0; /* TRUE when option help was explicitly requested */
215 int option_safe; /* to track if option currently parsed is safe
216 with a background copy */
219 rctl->poll_interval = -1;
222 memset(ctl, '\0', sizeof(struct query)); /* start clean */
223 ctl->smtp_socket = -1;
226 (c = getopt_long(argc,argv,shortoptions,
227 longoptions, &option_index)) != -1)
244 if (outlevel >= O_VERBOSE)
247 outlevel = O_VERBOSE;
251 rctl->poll_interval = xatoi(optarg, &errflag);
261 rctl->logfile = prependdir (optarg, currentwd);
264 rctl->invisible = FLAG_TRUE;
267 rctl->showdots = FLAG_TRUE;
271 rcfile = prependdir (optarg, currentwd);
274 rctl->idfile = prependdir (optarg, currentwd);
277 rctl->pidfile = prependdir (optarg, currentwd);
280 rctl->postmaster = (char *) xstrdup(optarg);
283 rctl->bouncemail = FLAG_FALSE;
285 case LA_NOSOFTBOUNCE:
286 rctl->softbounce = FLAG_FALSE;
289 rctl->softbounce = FLAG_TRUE;
292 if (strcasecmp(optarg,"accept") == 0) {
293 ctl->server.badheader = BHACCEPT;
294 } else if (strcasecmp(optarg,"reject") == 0) {
295 ctl->server.badheader = BHREJECT;
297 fprintf(stderr,GT_("Invalid bad-header policy `%s' specified.\n"), optarg);
303 /* XXX -- should probably use a table lookup here */
304 if (strcasecmp(optarg,"auto") == 0)
305 ctl->server.protocol = P_AUTO;
306 else if (strcasecmp(optarg,"pop2") == 0)
307 ctl->server.protocol = P_POP2;
309 else if (strcasecmp(optarg,"sdps") == 0)
311 ctl->server.protocol = P_POP3;
312 ctl->server.sdps = TRUE;
314 #endif /* SDPS_ENABLE */
315 else if (strcasecmp(optarg,"pop3") == 0)
316 ctl->server.protocol = P_POP3;
317 else if (strcasecmp(optarg,"apop") == 0)
318 ctl->server.protocol = P_APOP;
319 else if (strcasecmp(optarg,"rpop") == 0)
320 ctl->server.protocol = P_RPOP;
321 else if (strcasecmp(optarg,"kpop") == 0)
323 ctl->server.protocol = P_POP3;
324 ctl->server.service = xstrdup(KPOP_PORT);
326 ctl->server.authenticate = A_KERBEROS_V5;
328 ctl->server.authenticate = A_KERBEROS_V4;
329 #endif /* KERBEROS_V5 */
331 else if (strcasecmp(optarg,"imap") == 0)
332 ctl->server.protocol = P_IMAP;
333 else if (strcasecmp(optarg,"etrn") == 0)
334 ctl->server.protocol = P_ETRN;
335 else if (strcasecmp(optarg,"odmr") == 0)
336 ctl->server.protocol = P_ODMR;
338 fprintf(stderr,GT_("Invalid protocol `%s' specified.\n"), optarg);
343 ctl->server.uidl = FLAG_TRUE;
346 ctl->idle = FLAG_TRUE;
349 ctl->server.service = optarg;
352 if (strcmp(optarg, "password") == 0)
353 ctl->server.authenticate = A_PASSWORD;
354 else if (strcmp(optarg, "kerberos") == 0)
356 ctl->server.authenticate = A_KERBEROS_V5;
358 ctl->server.authenticate = A_KERBEROS_V4;
359 #endif /* KERBEROS_V5 */
360 else if (strcmp(optarg, "kerberos_v5") == 0)
361 ctl->server.authenticate = A_KERBEROS_V5;
362 else if (strcmp(optarg, "kerberos_v4") == 0)
363 ctl->server.authenticate = A_KERBEROS_V4;
364 else if (strcmp(optarg, "ssh") == 0)
365 ctl->server.authenticate = A_SSH;
366 else if (strcasecmp(optarg, "external") == 0)
367 ctl->server.authenticate = A_EXTERNAL;
368 else if (strcmp(optarg, "otp") == 0)
369 ctl->server.authenticate = A_OTP;
370 else if (strcmp(optarg, "opie") == 0)
371 ctl->server.authenticate = A_OTP;
372 else if (strcmp(optarg, "ntlm") == 0)
373 ctl->server.authenticate = A_NTLM;
374 else if (strcmp(optarg, "cram") == 0)
375 ctl->server.authenticate = A_CRAM_MD5;
376 else if (strcmp(optarg, "cram-md5") == 0)
377 ctl->server.authenticate = A_CRAM_MD5;
378 else if (strcmp(optarg, "gssapi") == 0)
379 ctl->server.authenticate = A_GSSAPI;
380 else if (strcmp(optarg, "any") == 0)
381 ctl->server.authenticate = A_ANY;
382 else if (strcmp(optarg, "msn") == 0)
383 ctl->server.authenticate = A_MSN;
385 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
390 ctl->server.timeout = xatoi(optarg, &errflag);
391 if (ctl->server.timeout == 0)
392 ctl->server.timeout = -1;
395 ctl->server.envelope = xstrdup(optarg);
398 ctl->server.qvirtual = xstrdup(optarg);
402 ctl->remotename = xstrdup(optarg);
405 ctl->fetchall = FLAG_TRUE;
408 ctl->keep = FLAG_FALSE;
411 ctl->keep = FLAG_TRUE;
414 ctl->flush = FLAG_TRUE;
417 ctl->limitflush = FLAG_TRUE;
420 ctl->rewrite = FLAG_FALSE;
423 c = xatoi(optarg, &errflag);
424 ctl->limit = NUM_VALUE_IN(c);
427 buf = xstrdup(optarg);
428 cp = strtok(buf, ",");
430 save_str(&ctl->mailboxes, cp, 0);
432 ((cp = strtok((char *)NULL, ",")));
436 buf = xstrdup(optarg);
437 cp = strtok(buf, ",");
439 save_str(&ctl->smtphunt, cp, TRUE);
441 ((cp = strtok((char *)NULL, ",")));
445 case LA_FETCHDOMAINS:
446 buf = xstrdup(optarg);
447 cp = strtok(buf, ",");
449 save_str(&ctl->domainlist, cp, TRUE);
451 ((cp = strtok((char *)NULL, ",")));
455 ctl->smtpaddress = xstrdup(optarg);
458 ctl->smtpname = xstrdup(optarg);
461 buf = xstrdup(optarg);
462 cp = strtok(buf, ",");
464 struct idlist *idp = save_str(&ctl->antispam, STRING_DUMMY, 0);
466 idp->val.status.num = xatoi(cp, &errflag);
468 ((cp = strtok((char *)NULL, ",")));
472 c = xatoi(optarg, &errflag);
473 ctl->batchlimit = NUM_VALUE_IN(c);
476 c = xatoi(optarg, &errflag);
477 ctl->fetchlimit = NUM_VALUE_IN(c);
479 case LA_FETCHSIZELIMIT:
480 c = xatoi(optarg, &errflag);
481 ctl->fetchsizelimit = NUM_VALUE_IN(c);
484 c = xatoi(optarg, &errflag);
485 ctl->fastuidl = NUM_VALUE_IN(c);
488 c = xatoi(optarg, &errflag);
489 ctl->expunge = NUM_VALUE_IN(c);
492 ctl->mda = xstrdup(optarg);
496 ctl->bsmtp = prependdir (optarg, currentwd);
500 ctl->listener = LMTP_MODE;
505 interface_parse(optarg, &ctl->server);
508 ctl->server.monitor = xstrdup(optarg);
510 #endif /* CAN_MONITOR */
512 ctl->server.plugin = xstrdup(optarg);
515 ctl->server.plugout = xstrdup(optarg);
520 ctl->use_ssl = FLAG_TRUE;
524 ctl->sslkey = prependdir (optarg, currentwd);
528 ctl->sslcert = prependdir (optarg, currentwd);
532 ctl->sslproto = xstrdup(optarg);
536 ctl->sslcertck = FLAG_TRUE;
540 ctl->sslcertck = FLAG_FALSE;
544 ctl->sslcertfile = prependdir(optarg, currentwd);
548 ctl->sslcertpath = prependdir(optarg, currentwd);
551 case LA_SSLCOMMONNAME:
552 ctl->sslcommonname = xstrdup(optarg);
555 case LA_SSLFINGERPRINT:
556 ctl->sslfingerprint = xstrdup(optarg);
561 ctl->server.principal = xstrdup(optarg);
569 c = xatoi(optarg, &errflag);
570 ctl->warnings = NUM_VALUE_IN(c);
579 rctl->use_syslog = FLAG_TRUE;
583 rctl->use_syslog = FLAG_FALSE;
587 ctl->server.tracepolls = FLAG_TRUE;
595 *safewithbg &= option_safe;
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_(" --nosyslog turns off use of syslog(3)\n"));
615 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
616 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
617 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
618 P(GT_(" --pidfile specify alternate PID (lock) file\n"));
619 P(GT_(" --postmaster specify recipient of last resort\n"));
620 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
621 P(GT_(" --nosoftbounce fetchmail deletes permanently undeliverable messages.\n"));
622 P(GT_(" --softbounce keep permanently undeliverable messages on server (default).\n"));
624 P(GT_(" -I, --interface interface required specification\n"));
625 P(GT_(" -M, --monitor monitor interface for activity\n"));
627 #if defined( SSL_ENABLE )
628 P(GT_(" --ssl enable ssl encrypted session\n"));
629 P(GT_(" --sslkey ssl private key file\n"));
630 P(GT_(" --sslcert ssl client certificate\n"));
631 P(GT_(" --sslcertck do strict server certificate check (recommended)\n"));
632 P(GT_(" --nosslcertck skip strict server certificate check (insecure)\n"));
633 P(GT_(" --sslcertfile path to trusted-CA ssl certificate file\n"));
634 P(GT_(" --sslcertpath path to trusted-CA ssl certificate directory\n"));
635 P(GT_(" --sslcommonname expect this CommonName from server (discouraged)\n"));
636 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\n"));
637 P(GT_(" --sslproto force ssl protocol (see manual)\n"));
639 P(GT_(" --plugin specify external command to open connection\n"));
640 P(GT_(" --plugout specify external command to open smtp connection\n"));
641 P(GT_(" --bad-header {reject|accept}\n"
642 " specify policy for handling messages with bad headers\n"));
644 P(GT_(" -p, --proto[col] specify retrieval protocol (see man page)\n"));
645 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
646 P(GT_(" --idle tells the IMAP server to send notice of new messages\n"));
647 P(GT_(" --port TCP port to connect to (obsolete, use --service)\n"));
648 P(GT_(" -P, --service TCP service to connect to (can be numeric TCP port)\n"));
649 P(GT_(" --auth authentication type (see manual)\n"));
650 P(GT_(" -t, --timeout server nonresponse timeout\n"));
651 P(GT_(" -E, --envelope envelope address header\n"));
652 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
653 P(GT_(" --principal mail service principal\n"));
654 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
656 P(GT_(" -u, --user[name] specify users's login on server\n"));
657 P(GT_(" -a, --[fetch]all retrieve old and new messages\n"));
658 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
659 P(GT_(" -k, --keep save new messages after retrieval\n"));
660 P(GT_(" -F, --flush delete old messages from server\n"));
661 P(GT_(" --limitflush delete oversized messages\n"));
662 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
663 P(GT_(" -l, --limit don't fetch messages over given size\n"));
664 P(GT_(" -w, --warnings interval between warning mail notification\n"));
666 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
667 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
668 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
669 P(GT_(" --smtpname set SMTP full name username@domain\n"));
670 P(GT_(" -Z, --antispam, set antispam response values\n"));
671 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
672 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
673 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
674 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
675 P(GT_(" -e, --expunge set max deletions between expunges\n"));
676 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
677 P(GT_(" --bsmtp set output BSMTP file\n"));
678 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
679 P(GT_(" -r, --folder specify remote folder name\n"));
680 P(GT_(" --showdots show progress dots even in logfiles\n"));
683 * --configdump (internal use by fetchmailconf, dumps
684 * configuration as Python source code)
685 * --yydebug (developer use, enables parser debugging) */
696 /* options.c ends here */