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.
18 #include "fetchmail.h"
58 static const char *shortoptions =
59 /* options still left: ghHjJoRTWxXYz */
63 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:I:M:yw:D:";
65 static const struct option longoptions[] = {
66 /* this can be const because all flag fields are 0 and will never get set */
68 {"pwmd-socket", required_argument, (int *) 0, 'C' },
69 {"pwmd-file", required_argument, (int *) 0, 'G' },
70 {"pinentry-timeout", required_argument, (int *) 0, 'O' },
72 {"help", no_argument, (int *) 0, '?' },
73 {"version", no_argument, (int *) 0, 'V' },
74 {"check", no_argument, (int *) 0, 'c' },
75 {"silent", no_argument, (int *) 0, 's' },
76 {"verbose", no_argument, (int *) 0, 'v' },
77 {"daemon", required_argument, (int *) 0, 'd' },
78 {"nodetach", no_argument, (int *) 0, 'N' },
79 {"quit", no_argument, (int *) 0, 'q' },
80 {"logfile", required_argument, (int *) 0, 'L' },
81 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
82 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
83 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
84 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
85 {"fetchmailrc",required_argument,(int *) 0, 'f' },
86 {"idfile", required_argument, (int *) 0, 'i' },
87 {"pidfile", required_argument, (int *) 0, LA_PIDFILE },
88 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
89 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
90 {"nosoftbounce", no_argument, (int *) 0, LA_NOSOFTBOUNCE },
91 {"softbounce", no_argument, (int *) 0, LA_SOFTBOUNCE },
93 {"protocol", required_argument, (int *) 0, 'p' },
94 {"proto", required_argument, (int *) 0, 'p' },
95 {"uidl", no_argument, (int *) 0, 'U' },
96 {"idle", no_argument, (int *) 0, LA_IDLE},
97 {"port", required_argument, (int *) 0, 'P' },
98 {"service", required_argument, (int *) 0, 'P' },
99 {"auth", required_argument, (int *) 0, LA_AUTH},
100 {"timeout", required_argument, (int *) 0, 't' },
101 {"envelope", required_argument, (int *) 0, 'E' },
102 {"qvirtual", required_argument, (int *) 0, 'Q' },
103 {"bad-header",required_argument, (int *) 0, LA_BADHEADER},
105 {"user", required_argument, (int *) 0, 'u' },
106 {"username", required_argument, (int *) 0, 'u' },
108 {"all", no_argument, (int *) 0, 'a' },
109 {"fetchall", no_argument, (int *) 0, 'a' },
110 {"nokeep", no_argument, (int *) 0, 'K' },
111 {"keep", no_argument, (int *) 0, 'k' },
112 {"flush", no_argument, (int *) 0, 'F' },
113 {"limitflush", no_argument, (int *) 0, LA_LIMITFLUSH },
114 {"norewrite", no_argument, (int *) 0, 'n' },
115 {"limit", required_argument, (int *) 0, 'l' },
116 {"warnings", required_argument, (int *) 0, 'w' },
117 {"retrieve-error", required_argument, (int *) 0, LA_RETRIEVEERROR },
119 {"folder", required_argument, (int *) 0, 'r' },
120 {"smtphost", required_argument, (int *) 0, 'S' },
121 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
122 {"smtpaddress", required_argument, (int *) 0, 'D' },
123 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
124 {"antispam", required_argument, (int *) 0, 'Z' },
126 {"batchlimit",required_argument, (int *) 0, 'b' },
127 {"fetchlimit",required_argument, (int *) 0, 'B' },
128 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
129 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
130 {"expunge", required_argument, (int *) 0, 'e' },
131 {"mda", required_argument, (int *) 0, 'm' },
132 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
133 {"lmtp", no_argument, (int *) 0, LA_LMTP },
136 {"ssl", no_argument, (int *) 0, LA_SSL },
137 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
138 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
139 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
140 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
141 {"sslcertfile", required_argument, (int *) 0, LA_SSLCERTFILE },
142 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
143 {"sslcommonname", required_argument, (int *) 0, LA_SSLCOMMONNAME },
144 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
147 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
150 {"interface", required_argument, (int *) 0, 'I' },
151 {"monitor", required_argument, (int *) 0, 'M' },
152 #endif /* CAN_MONITOR */
153 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
154 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
156 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
158 {"yydebug", no_argument, (int *) 0, 'y' },
160 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
162 {(char *) 0, no_argument, (int *) 0, 0 }
165 static int xatoi(char *s, int *errflagptr)
166 /* do safe conversion from string to number */
168 /* parse and convert numbers, but also check for invalid characters in
177 value = strtol(s, &endptr, 0);
179 /* any invalid chars in string? */
180 if ( (endptr == s) || (*endptr != '\0') ) {
181 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
186 /* is the range valid? */
187 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
188 (value > INT_MAX) || (value < INT_MIN)) {
190 (void) fprintf(stderr, GT_("Value of string '%s' is %s than %d.\n"), s,
191 (value < 0) ? GT_("smaller"): GT_("larger"),
192 (value < 0) ? INT_MIN : INT_MAX);
197 return (int) value; /* shut up, I know what I'm doing */
200 /** parse and validate the command line options */
201 int parsecmdline (int argc /** argument count */,
202 char **argv /** argument strings */,
203 struct runctl *rctl /** global run controls to modify */,
204 struct query *ctl /** option record to initialize */,
205 flag *safewithbg /** set to whether options are
206 compatible with another copy
207 running in the background */)
210 * return value: if positive, argv index of last parsed option + 1
211 * (presumes one or more server names follows). if zero, the
212 * command line switches are such that no server names are
213 * required (e.g. --version). if negative, the command line is
214 * has one or more syntax errors.
218 int ocount = 0; /* count of destinations specified */
219 int errflag = 0; /* TRUE when a syntax error is detected */
220 int helpflag = 0; /* TRUE when option help was explicitly requested */
222 int option_safe; /* to track if option currently parsed is safe
223 with a background copy */
226 rctl->poll_interval = -1;
229 memset(ctl, '\0', sizeof(struct query)); /* start clean */
230 ctl->smtp_socket = -1;
233 (c = getopt_long(argc,argv,shortoptions,
234 longoptions, &option_index)) != -1)
241 ctl->pwmd_socket = prependdir(optarg, currentwd);
244 ctl->pwmd_file = xstrdup(optarg);
247 rctl->pinentry_timeout = atoi(optarg);
262 if (outlevel >= O_VERBOSE)
265 outlevel = O_VERBOSE;
269 rctl->poll_interval = xatoi(optarg, &errflag);
279 rctl->logfile = prependdir (optarg, currentwd);
282 rctl->invisible = FLAG_TRUE;
285 rctl->showdots = FLAG_TRUE;
289 rcfile = prependdir (optarg, currentwd);
292 rctl->idfile = prependdir (optarg, currentwd);
295 rctl->pidfile = prependdir (optarg, currentwd);
298 rctl->postmaster = (char *) xstrdup(optarg);
301 rctl->bouncemail = FLAG_FALSE;
303 case LA_NOSOFTBOUNCE:
304 rctl->softbounce = FLAG_FALSE;
307 rctl->softbounce = FLAG_TRUE;
310 if (strcasecmp(optarg,"accept") == 0) {
311 ctl->server.badheader = BHACCEPT;
312 } else if (strcasecmp(optarg,"reject") == 0) {
313 ctl->server.badheader = BHREJECT;
315 fprintf(stderr,GT_("Invalid bad-header policy `%s' specified.\n"), optarg);
321 /* XXX -- should probably use a table lookup here */
322 if (strcasecmp(optarg,"auto") == 0)
323 ctl->server.protocol = P_AUTO;
325 else if (strcasecmp(optarg,"sdps") == 0)
327 ctl->server.protocol = P_POP3;
328 ctl->server.sdps = TRUE;
330 #endif /* SDPS_ENABLE */
331 else if (strcasecmp(optarg,"pop3") == 0)
332 ctl->server.protocol = P_POP3;
333 else if (strcasecmp(optarg,"kpop") == 0)
335 ctl->server.protocol = P_POP3;
336 ctl->server.service = xstrdup(KPOP_PORT);
338 ctl->server.authenticate = A_KERBEROS_V5;
339 #endif /* KERBEROS_V5 */
341 else if (strcasecmp(optarg,"imap") == 0)
342 ctl->server.protocol = P_IMAP;
343 else if (strcasecmp(optarg,"etrn") == 0)
344 ctl->server.protocol = P_ETRN;
345 else if (strcasecmp(optarg,"odmr") == 0)
346 ctl->server.protocol = P_ODMR;
348 fprintf(stderr,GT_("Invalid protocol `%s' specified.\n"), optarg);
353 /* EMPTY - removed in 7.0.0 */
356 ctl->idle = FLAG_TRUE;
359 ctl->server.service = optarg;
362 if (strcmp(optarg, "password") == 0)
363 ctl->server.authenticate = A_PASSWORD;
365 else if (strcmp(optarg, "kerberos") == 0)
366 ctl->server.authenticate = A_KERBEROS_V5;
367 else if (strcmp(optarg, "kerberos_v5") == 0)
368 ctl->server.authenticate = A_KERBEROS_V5;
369 #endif /* KERBEROS_V5 */
370 else if (strcmp(optarg, "ssh") == 0)
371 ctl->server.authenticate = A_SSH;
372 else if (strcasecmp(optarg, "external") == 0)
373 ctl->server.authenticate = A_EXTERNAL;
374 else if (strcmp(optarg, "otp") == 0)
375 ctl->server.authenticate = A_OTP;
376 else if (strcmp(optarg, "opie") == 0)
377 ctl->server.authenticate = A_OTP;
378 else if (strcmp(optarg, "ntlm") == 0)
379 ctl->server.authenticate = A_NTLM;
380 else if (strcmp(optarg, "cram") == 0)
381 ctl->server.authenticate = A_CRAM_MD5;
382 else if (strcmp(optarg, "cram-md5") == 0)
383 ctl->server.authenticate = A_CRAM_MD5;
384 else if (strcmp(optarg, "gssapi") == 0)
385 ctl->server.authenticate = A_GSSAPI;
386 else if (strcmp(optarg, "any") == 0)
387 ctl->server.authenticate = A_ANY;
388 else if (strcmp(optarg, "msn") == 0)
389 ctl->server.authenticate = A_MSN;
390 else if (strcmp(optarg, "apop") == 0)
391 ctl->server.authenticate = A_APOP;
393 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
398 ctl->server.timeout = xatoi(optarg, &errflag);
399 if (ctl->server.timeout == 0)
400 ctl->server.timeout = -1;
403 ctl->server.envelope = xstrdup(optarg);
406 ctl->server.qvirtual = xstrdup(optarg);
410 ctl->remotename = xstrdup(optarg);
413 ctl->fetchall = FLAG_TRUE;
416 ctl->keep = FLAG_FALSE;
419 ctl->keep = FLAG_TRUE;
422 ctl->flush = FLAG_TRUE;
425 ctl->limitflush = FLAG_TRUE;
428 ctl->rewrite = FLAG_FALSE;
431 c = xatoi(optarg, &errflag);
432 ctl->limit = NUM_VALUE_IN(c);
435 buf = xstrdup(optarg);
436 cp = strtok(buf, ",");
438 save_str(&ctl->mailboxes, cp, 0);
440 ((cp = strtok((char *)NULL, ",")));
444 buf = xstrdup(optarg);
445 cp = strtok(buf, ",");
447 save_str(&ctl->smtphunt, cp, TRUE);
449 ((cp = strtok((char *)NULL, ",")));
453 case LA_FETCHDOMAINS:
454 buf = xstrdup(optarg);
455 cp = strtok(buf, ",");
457 save_str(&ctl->domainlist, cp, TRUE);
459 ((cp = strtok((char *)NULL, ",")));
463 ctl->smtpaddress = xstrdup(optarg);
466 ctl->smtpname = xstrdup(optarg);
469 buf = xstrdup(optarg);
470 cp = strtok(buf, ",");
472 struct idlist *idp = save_str(&ctl->antispam, STRING_DUMMY, 0);
474 idp->val.status.num = xatoi(cp, &errflag);
476 ((cp = strtok((char *)NULL, ",")));
480 c = xatoi(optarg, &errflag);
481 ctl->batchlimit = NUM_VALUE_IN(c);
484 c = xatoi(optarg, &errflag);
485 ctl->fetchlimit = NUM_VALUE_IN(c);
487 case LA_FETCHSIZELIMIT:
488 c = xatoi(optarg, &errflag);
489 ctl->fetchsizelimit = NUM_VALUE_IN(c);
492 c = xatoi(optarg, &errflag);
493 ctl->fastuidl = NUM_VALUE_IN(c);
496 c = xatoi(optarg, &errflag);
497 ctl->expunge = NUM_VALUE_IN(c);
500 ctl->mda = xstrdup(optarg);
504 ctl->bsmtp = prependdir (optarg, currentwd);
508 ctl->listener = LMTP_MODE;
513 interface_parse(optarg, &ctl->server);
516 ctl->server.monitor = xstrdup(optarg);
518 #endif /* CAN_MONITOR */
520 ctl->server.plugin = xstrdup(optarg);
523 ctl->server.plugout = xstrdup(optarg);
528 ctl->use_ssl = FLAG_TRUE;
532 ctl->sslkey = prependdir (optarg, currentwd);
536 ctl->sslcert = prependdir (optarg, currentwd);
540 ctl->sslproto = xstrdup(optarg);
544 ctl->sslcertck = FLAG_TRUE;
548 ctl->sslcertfile = prependdir(optarg, currentwd);
552 ctl->sslcertpath = prependdir(optarg, currentwd);
555 case LA_SSLCOMMONNAME:
556 ctl->sslcommonname = xstrdup(optarg);
559 case LA_SSLFINGERPRINT:
560 ctl->sslfingerprint = xstrdup(optarg);
565 ctl->server.principal = xstrdup(optarg);
573 c = xatoi(optarg, &errflag);
574 ctl->warnings = NUM_VALUE_IN(c);
583 rctl->use_syslog = FLAG_TRUE;
587 rctl->use_syslog = FLAG_FALSE;
591 ctl->server.tracepolls = FLAG_TRUE;
594 case LA_RETRIEVEERROR:
595 if (strcasecmp(optarg,"abort") == 0) {
596 ctl->server.retrieveerror = RE_ABORT;
597 } else if (strcasecmp(optarg,"continue") == 0) {
598 ctl->server.retrieveerror = RE_CONTINUE;
599 } else if (strcasecmp(optarg,"markseen") == 0) {
600 ctl->server.retrieveerror = RE_MARKSEEN;
602 fprintf(stderr,GT_("Invalid retrieve-error policy `%s' specified.\n"), optarg);
612 *safewithbg &= option_safe;
615 if (errflag || ocount > 1 || helpflag) {
616 /* squawk if syntax errors were detected */
617 #define P(s) fputs(s, helpflag ? stdout : stderr)
618 P(GT_("usage: fetchmail [options] [server ...]\n"));
619 P(GT_(" Options are as follows:\n"));
620 P(GT_(" -?, --help display this option help\n"));
621 P(GT_(" -V, --version display version info\n"));
623 P(GT_(" -c, --check check for messages without fetching\n"));
624 P(GT_(" -s, --silent work silently\n"));
625 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
626 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
627 P(GT_(" -N, --nodetach don't detach daemon process\n"));
628 P(GT_(" -q, --quit kill daemon process\n"));
629 P(GT_(" -L, --logfile specify logfile name\n"));
630 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
631 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
632 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
633 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
634 P(GT_(" --pidfile specify alternate PID (lock) file\n"));
635 P(GT_(" --postmaster specify recipient of last resort\n"));
636 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
637 P(GT_(" --nosoftbounce fetchmail deletes permanently undeliverable messages.\n"));
638 P(GT_(" --softbounce keep permanently undeliverable messages on server (default).\n"));
640 P(GT_(" -I, --interface interface required specification\n"));
641 P(GT_(" -M, --monitor monitor interface for activity\n"));
643 #if defined( SSL_ENABLE )
644 P(GT_(" --ssl enable ssl encrypted session\n"));
645 P(GT_(" --sslkey ssl private key file\n"));
646 P(GT_(" --sslcert ssl client certificate\n"));
647 P(GT_(" --sslcertck do strict server certificate check (recommended)\n"));
648 P(GT_(" --sslcertfile path to trusted-CA ssl certificate file\n"));
649 P(GT_(" --sslcertpath path to trusted-CA ssl certificate directory\n"));
650 P(GT_(" --sslcommonname expect this CommonName from server (discouraged)\n"));
651 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\n"));
652 P(GT_(" --sslproto force ssl protocol (SSL23/SSL3/TLS1)\n"));
654 P(GT_(" --plugin specify external command to open connection\n"));
655 P(GT_(" --plugout specify external command to open smtp connection\n"));
656 P(GT_(" --bad-header {reject|accept}\n"
657 " specify policy for handling messages with bad headers\n"));
658 P(GT_(" --retrieve-error {abort|continue|markseen}\n"
659 " specify policy for processing messages with retrieve errors\n"));
661 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
663 P(GT_(" -C, --pwmd-socket pwmd socket path (~/.pwmd/socket)\n"));
664 P(GT_(" -G, --pwmd-file filename to use on the pwmd server\n"));
665 P(GT_(" -O, --pinentry-timeout seconds until pinentry is canceled\n"));
668 P(GT_(" --port TCP port to connect to (obsolete, use --service)\n"));
669 P(GT_(" -P, --service TCP service to connect to (can be numeric TCP port)\n"));
670 P(GT_(" --auth authentication type (password/kerberos/ssh/otp)\n"));
671 P(GT_(" -t, --timeout server nonresponse timeout\n"));
672 P(GT_(" -E, --envelope envelope address header\n"));
673 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
674 P(GT_(" --principal mail service principal\n"));
675 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
677 P(GT_(" -u, --username specify users's login on server\n"));
678 P(GT_(" -a, --[fetch]all retrieve old and new messages\n"));
679 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
680 P(GT_(" -k, --keep save new messages after retrieval\n"));
681 P(GT_(" -F, --flush delete old messages from server\n"));
682 P(GT_(" --limitflush delete oversized messages\n"));
683 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
684 P(GT_(" -l, --limit don't fetch messages over given size\n"));
685 P(GT_(" -w, --warnings interval between warning mail notification\n"));
687 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
688 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
689 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
690 P(GT_(" --smtpname set SMTP full name username@domain\n"));
691 P(GT_(" -Z, --antispam, set antispam response values\n"));
692 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
693 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
694 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
695 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
696 P(GT_(" -e, --expunge set max deletions between expunges\n"));
697 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
698 P(GT_(" --bsmtp set output BSMTP file\n"));
699 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
700 P(GT_(" -r, --folder specify remote folder name\n"));
701 P(GT_(" --showdots show progress dots even in logfiles\n"));
704 * --configdump (internal use by fetchmailconf, dumps
705 * configuration as Python source code)
706 * --yydebug (developer use, enables parser debugging) */
717 /* options.c ends here */