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"
61 /* options still left: CgGhHjJoORTWxXYz */
62 static const char *shortoptions =
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 */
67 {"help", no_argument, (int *) 0, '?' },
68 {"version", no_argument, (int *) 0, 'V' },
69 {"check", no_argument, (int *) 0, 'c' },
70 {"silent", no_argument, (int *) 0, 's' },
71 {"verbose", no_argument, (int *) 0, 'v' },
72 {"daemon", required_argument, (int *) 0, 'd' },
73 {"nodetach", no_argument, (int *) 0, 'N' },
74 {"quit", no_argument, (int *) 0, 'q' },
75 {"logfile", required_argument, (int *) 0, 'L' },
76 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
77 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
78 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
79 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
80 {"fetchmailrc",required_argument,(int *) 0, 'f' },
81 {"idfile", required_argument, (int *) 0, 'i' },
82 {"pidfile", required_argument, (int *) 0, LA_PIDFILE },
83 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
84 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
85 {"nosoftbounce", no_argument, (int *) 0, LA_NOSOFTBOUNCE },
86 {"softbounce", no_argument, (int *) 0, LA_SOFTBOUNCE },
88 {"protocol", required_argument, (int *) 0, 'p' },
89 {"proto", required_argument, (int *) 0, 'p' },
90 {"uidl", no_argument, (int *) 0, 'U' },
91 {"idle", no_argument, (int *) 0, LA_IDLE},
92 {"port", required_argument, (int *) 0, 'P' },
93 {"service", required_argument, (int *) 0, 'P' },
94 {"auth", required_argument, (int *) 0, LA_AUTH},
95 {"timeout", required_argument, (int *) 0, 't' },
96 {"envelope", required_argument, (int *) 0, 'E' },
97 {"qvirtual", required_argument, (int *) 0, 'Q' },
98 {"bad-header",required_argument, (int *) 0, LA_BADHEADER},
100 {"user", required_argument, (int *) 0, 'u' },
101 {"username", required_argument, (int *) 0, 'u' },
103 {"all", no_argument, (int *) 0, 'a' },
104 {"fetchall", no_argument, (int *) 0, 'a' },
105 {"nokeep", no_argument, (int *) 0, 'K' },
106 {"keep", no_argument, (int *) 0, 'k' },
107 {"flush", no_argument, (int *) 0, 'F' },
108 {"limitflush", no_argument, (int *) 0, LA_LIMITFLUSH },
109 {"norewrite", no_argument, (int *) 0, 'n' },
110 {"limit", required_argument, (int *) 0, 'l' },
111 {"warnings", required_argument, (int *) 0, 'w' },
113 {"folder", required_argument, (int *) 0, 'r' },
114 {"smtphost", required_argument, (int *) 0, 'S' },
115 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
116 {"smtpaddress", required_argument, (int *) 0, 'D' },
117 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
118 {"antispam", required_argument, (int *) 0, 'Z' },
120 {"batchlimit",required_argument, (int *) 0, 'b' },
121 {"fetchlimit",required_argument, (int *) 0, 'B' },
122 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
123 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
124 {"expunge", required_argument, (int *) 0, 'e' },
125 {"mda", required_argument, (int *) 0, 'm' },
126 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
127 {"lmtp", no_argument, (int *) 0, LA_LMTP },
130 {"ssl", no_argument, (int *) 0, LA_SSL },
131 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
132 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
133 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
134 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
135 {"sslcertfile", required_argument, (int *) 0, LA_SSLCERTFILE },
136 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
137 {"sslcommonname", required_argument, (int *) 0, LA_SSLCOMMONNAME },
138 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
141 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
144 {"interface", required_argument, (int *) 0, 'I' },
145 {"monitor", required_argument, (int *) 0, 'M' },
146 #endif /* CAN_MONITOR */
147 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
148 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
150 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
152 {"yydebug", no_argument, (int *) 0, 'y' },
154 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
156 {(char *) 0, no_argument, (int *) 0, 0 }
159 static int xatoi(char *s, int *errflagptr)
160 /* do safe conversion from string to number */
162 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
163 /* parse and convert numbers, but also check for invalid characters in
172 value = strtol(s, &endptr, 0);
174 /* any invalid chars in string? */
175 if ( (endptr == s) || (*endptr != '\0') ) {
176 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
181 /* is the range valid? */
182 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
183 (value > INT_MAX) || (value < INT_MIN)) {
185 (void) fprintf(stderr, GT_("Value of string '%s' is %s than %d.\n"), s,
186 (value < 0) ? GT_("smaller"): GT_("larger"),
187 (value < 0) ? INT_MIN : INT_MAX);
192 return (int) value; /* shut up, I know what I'm doing */
196 # if defined (STDC_HEADERS)
202 /* We do only base 10 conversions here (atoi)! */
205 /* check for leading white spaces */
206 for (i = 0; (i < len) && isspace((unsigned char)s[i]); i++)
212 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
214 /* skip over digits */
215 for ( /* no init */ ; (i < len) && isdigit((unsigned char)s[i]); i++)
218 /* check for trailing garbage */
220 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
225 /* atoi should be safe by now, except for number range over/underflow */
230 /** parse and validate the command line options */
231 int parsecmdline (int argc /** argument count */,
232 char **argv /** argument strings */,
233 struct runctl *rctl /** global run controls to modify */,
234 struct query *ctl /** option record to initialize */)
237 * return value: if positive, argv index of last parsed option + 1
238 * (presumes one or more server names follows). if zero, the
239 * command line switches are such that no server names are
240 * required (e.g. --version). if negative, the command line is
241 * has one or more syntax errors.
245 int ocount = 0; /* count of destinations specified */
246 int errflag = 0; /* TRUE when a syntax error is detected */
247 int helpflag = 0; /* TRUE when option help was explicitly requested */
251 rctl->poll_interval = -1;
253 memset(ctl, '\0', sizeof(struct query)); /* start clean */
254 ctl->smtp_socket = -1;
257 (c = getopt_long(argc,argv,shortoptions,
258 longoptions, &option_index)) != -1)
271 if (outlevel >= O_VERBOSE)
274 outlevel = O_VERBOSE;
277 rctl->poll_interval = xatoi(optarg, &errflag);
287 rctl->logfile = prependdir (optarg, currentwd);
290 rctl->invisible = FLAG_TRUE;
293 rctl->showdots = FLAG_TRUE;
297 rcfile = prependdir (optarg, currentwd);
300 rctl->idfile = prependdir (optarg, currentwd);
303 rctl->pidfile = prependdir (optarg, currentwd);
306 rctl->postmaster = (char *) xstrdup(optarg);
309 rctl->bouncemail = FLAG_FALSE;
311 case LA_NOSOFTBOUNCE:
312 rctl->softbounce = FLAG_FALSE;
315 rctl->softbounce = FLAG_TRUE;
318 if (strcasecmp(optarg,"accept") == 0) {
319 ctl->server.badheader = BHACCEPT;
320 } else if (strcasecmp(optarg,"reject") == 0) {
321 ctl->server.badheader = BHREJECT;
323 fprintf(stderr,GT_("Invalid bad-header policy `%s' specified.\n"), optarg);
329 /* XXX -- should probably use a table lookup here */
330 if (strcasecmp(optarg,"auto") == 0)
331 ctl->server.protocol = P_AUTO;
332 else if (strcasecmp(optarg,"pop2") == 0)
333 ctl->server.protocol = P_POP2;
335 else if (strcasecmp(optarg,"sdps") == 0)
337 ctl->server.protocol = P_POP3;
338 ctl->server.sdps = TRUE;
340 #endif /* SDPS_ENABLE */
341 else if (strcasecmp(optarg,"pop3") == 0)
342 ctl->server.protocol = P_POP3;
343 else if (strcasecmp(optarg,"apop") == 0)
344 ctl->server.protocol = P_APOP;
345 else if (strcasecmp(optarg,"rpop") == 0)
346 ctl->server.protocol = P_RPOP;
347 else if (strcasecmp(optarg,"kpop") == 0)
349 ctl->server.protocol = P_POP3;
350 ctl->server.service = KPOP_PORT;
352 ctl->server.authenticate = A_KERBEROS_V5;
354 ctl->server.authenticate = A_KERBEROS_V4;
355 #endif /* KERBEROS_V5 */
357 else if (strcasecmp(optarg,"imap") == 0)
358 ctl->server.protocol = P_IMAP;
359 else if (strcasecmp(optarg,"etrn") == 0)
360 ctl->server.protocol = P_ETRN;
361 else if (strcasecmp(optarg,"odmr") == 0)
362 ctl->server.protocol = P_ODMR;
364 fprintf(stderr,GT_("Invalid protocol `%s' specified.\n"), optarg);
369 ctl->server.uidl = FLAG_TRUE;
372 ctl->idle = FLAG_TRUE;
375 ctl->server.service = optarg;
378 if (strcmp(optarg, "password") == 0)
379 ctl->server.authenticate = A_PASSWORD;
380 else if (strcmp(optarg, "kerberos") == 0)
382 ctl->server.authenticate = A_KERBEROS_V5;
384 ctl->server.authenticate = A_KERBEROS_V4;
385 #endif /* KERBEROS_V5 */
386 else if (strcmp(optarg, "kerberos_v5") == 0)
387 ctl->server.authenticate = A_KERBEROS_V5;
388 else if (strcmp(optarg, "kerberos_v4") == 0)
389 ctl->server.authenticate = A_KERBEROS_V4;
390 else if (strcmp(optarg, "ssh") == 0)
391 ctl->server.authenticate = A_SSH;
392 else if (strcasecmp(optarg, "external") == 0)
393 ctl->server.authenticate = A_EXTERNAL;
394 else if (strcmp(optarg, "otp") == 0)
395 ctl->server.authenticate = A_OTP;
396 else if (strcmp(optarg, "opie") == 0)
397 ctl->server.authenticate = A_OTP;
398 else if (strcmp(optarg, "ntlm") == 0)
399 ctl->server.authenticate = A_NTLM;
400 else if (strcmp(optarg, "cram") == 0)
401 ctl->server.authenticate = A_CRAM_MD5;
402 else if (strcmp(optarg, "cram-md5") == 0)
403 ctl->server.authenticate = A_CRAM_MD5;
404 else if (strcmp(optarg, "gssapi") == 0)
405 ctl->server.authenticate = A_GSSAPI;
406 else if (strcmp(optarg, "any") == 0)
407 ctl->server.authenticate = A_ANY;
408 else if (strcmp(optarg, "msn") == 0)
409 ctl->server.authenticate = A_MSN;
411 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
416 ctl->server.timeout = xatoi(optarg, &errflag);
417 if (ctl->server.timeout == 0)
418 ctl->server.timeout = -1;
421 ctl->server.envelope = xstrdup(optarg);
424 ctl->server.qvirtual = xstrdup(optarg);
428 ctl->remotename = xstrdup(optarg);
431 ctl->fetchall = FLAG_TRUE;
434 ctl->keep = FLAG_FALSE;
437 ctl->keep = FLAG_TRUE;
440 ctl->flush = FLAG_TRUE;
443 ctl->limitflush = FLAG_TRUE;
446 ctl->rewrite = FLAG_FALSE;
449 c = xatoi(optarg, &errflag);
450 ctl->limit = NUM_VALUE_IN(c);
453 buf = xstrdup(optarg);
454 cp = strtok(buf, ",");
456 save_str(&ctl->mailboxes, cp, 0);
458 ((cp = strtok((char *)NULL, ",")));
462 buf = xstrdup(optarg);
463 cp = strtok(buf, ",");
465 save_str(&ctl->smtphunt, cp, TRUE);
467 ((cp = strtok((char *)NULL, ",")));
471 case LA_FETCHDOMAINS:
472 buf = xstrdup(optarg);
473 cp = strtok(buf, ",");
475 save_str(&ctl->domainlist, cp, TRUE);
477 ((cp = strtok((char *)NULL, ",")));
481 ctl->smtpaddress = xstrdup(optarg);
484 ctl->smtpname = xstrdup(optarg);
487 buf = xstrdup(optarg);
488 cp = strtok(buf, ",");
490 struct idlist *idp = save_str(&ctl->antispam, STRING_DUMMY, 0);
492 idp->val.status.num = xatoi(cp, &errflag);
494 ((cp = strtok((char *)NULL, ",")));
498 c = xatoi(optarg, &errflag);
499 ctl->batchlimit = NUM_VALUE_IN(c);
502 c = xatoi(optarg, &errflag);
503 ctl->fetchlimit = NUM_VALUE_IN(c);
505 case LA_FETCHSIZELIMIT:
506 c = xatoi(optarg, &errflag);
507 ctl->fetchsizelimit = NUM_VALUE_IN(c);
510 c = xatoi(optarg, &errflag);
511 ctl->fastuidl = NUM_VALUE_IN(c);
514 c = xatoi(optarg, &errflag);
515 ctl->expunge = NUM_VALUE_IN(c);
518 ctl->mda = xstrdup(optarg);
522 ctl->bsmtp = prependdir (optarg, currentwd);
526 ctl->listener = LMTP_MODE;
531 interface_parse(optarg, &ctl->server);
534 ctl->server.monitor = xstrdup(optarg);
536 #endif /* CAN_MONITOR */
538 ctl->server.plugin = xstrdup(optarg);
541 ctl->server.plugout = xstrdup(optarg);
546 ctl->use_ssl = FLAG_TRUE;
550 ctl->sslkey = prependdir (optarg, currentwd);
554 ctl->sslcert = prependdir (optarg, currentwd);
558 ctl->sslproto = xstrdup(optarg);
562 ctl->sslcertck = FLAG_TRUE;
566 ctl->sslcertfile = prependdir(optarg, currentwd);
570 ctl->sslcertpath = prependdir(optarg, currentwd);
573 case LA_SSLCOMMONNAME:
574 ctl->sslcommonname = xstrdup(optarg);
577 case LA_SSLFINGERPRINT:
578 ctl->sslfingerprint = xstrdup(optarg);
583 ctl->server.principal = xstrdup(optarg);
591 c = xatoi(optarg, &errflag);
592 ctl->warnings = NUM_VALUE_IN(c);
600 rctl->use_syslog = FLAG_TRUE;
604 rctl->use_syslog = FLAG_FALSE;
608 ctl->server.tracepolls = FLAG_TRUE;
617 if (errflag || ocount > 1 || helpflag) {
618 /* squawk if syntax errors were detected */
619 #define P(s) fputs(s, helpflag ? stdout : stderr)
620 P(GT_("usage: fetchmail [options] [server ...]\n"));
621 P(GT_(" Options are as follows:\n"));
622 P(GT_(" -?, --help display this option help\n"));
623 P(GT_(" -V, --version display version info\n"));
625 P(GT_(" -c, --check check for messages without fetching\n"));
626 P(GT_(" -s, --silent work silently\n"));
627 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
628 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
629 P(GT_(" -N, --nodetach don't detach daemon process\n"));
630 P(GT_(" -q, --quit kill daemon process\n"));
631 P(GT_(" -L, --logfile specify logfile name\n"));
632 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
633 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
634 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
635 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
636 P(GT_(" --pidfile specify alternate PID (lock) file\n"));
637 P(GT_(" --postmaster specify recipient of last resort\n"));
638 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
639 P(GT_(" --nosoftbounce fetchmail deletes permanently undeliverable messages.\n"));
640 P(GT_(" --softbounce keep permanently undeliverable messages on server (default).\n"));
642 P(GT_(" -I, --interface interface required specification\n"));
643 P(GT_(" -M, --monitor monitor interface for activity\n"));
645 #if defined( SSL_ENABLE )
646 P(GT_(" --ssl enable ssl encrypted session\n"));
647 P(GT_(" --sslkey ssl private key file\n"));
648 P(GT_(" --sslcert ssl client certificate\n"));
649 P(GT_(" --sslcertck do strict server certificate check (recommended)\n"));
650 P(GT_(" --sslcertfile path to trusted-CA ssl certificate file\n"));
651 P(GT_(" --sslcertpath path to trusted-CA ssl certificate directory\n"));
652 P(GT_(" --sslcommonname expect this CommonName from server (discouraged)\n"));
653 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\n"));
654 P(GT_(" --sslproto force ssl protocol (SSL2/SSL3/TLS1)\n"));
656 P(GT_(" --plugin specify external command to open connection\n"));
657 P(GT_(" --plugout specify external command to open smtp connection\n"));
658 P(GT_(" --bad-header {reject|accept}\n"
659 " specify policy for handling messages with bad headers\n"));
661 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
662 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
663 P(GT_(" --port TCP port to connect to (obsolete, use --service)\n"));
664 P(GT_(" -P, --service TCP service to connect to (can be numeric TCP port)\n"));
665 P(GT_(" --auth authentication type (password/kerberos/ssh/otp)\n"));
666 P(GT_(" -t, --timeout server nonresponse timeout\n"));
667 P(GT_(" -E, --envelope envelope address header\n"));
668 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
669 P(GT_(" --principal mail service principal\n"));
670 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
672 P(GT_(" -u, --username specify users's login on server\n"));
673 P(GT_(" -a, --[fetch]all retrieve old and new messages\n"));
674 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
675 P(GT_(" -k, --keep save new messages after retrieval\n"));
676 P(GT_(" -F, --flush delete old messages from server\n"));
677 P(GT_(" --limitflush delete oversized messages\n"));
678 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
679 P(GT_(" -l, --limit don't fetch messages over given size\n"));
680 P(GT_(" -w, --warnings interval between warning mail notification\n"));
682 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
683 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
684 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
685 P(GT_(" --smtpname set SMTP full name username@domain\n"));
686 P(GT_(" -Z, --antispam, set antispam response values\n"));
687 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
688 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
689 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
690 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
691 P(GT_(" -e, --expunge set max deletions between expunges\n"));
692 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
693 P(GT_(" --bsmtp set output BSMTP file\n"));
694 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
695 P(GT_(" -r, --folder specify remote folder name\n"));
696 P(GT_(" --showdots show progress dots even in logfiles\n"));
699 * --configdump (internal use by fetchmailconf, dumps
700 * configuration as Python source code)
701 * --yydebug (developer use, enables parser debugging) */
712 /* options.c ends here */