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"
57 static const char *shortoptions =
58 /* options still left: ghHjJoRTWxXYz */
62 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:I:M:yw:D:";
64 static const struct option longoptions[] = {
65 /* this can be const because all flag fields are 0 and will never get set */
67 {"pwmd-socket", required_argument, (int *) 0, 'C' },
68 {"pwmd-file", required_argument, (int *) 0, 'G' },
69 {"pinentry-timeout", required_argument, (int *) 0, 'O' },
71 {"help", no_argument, (int *) 0, '?' },
72 {"version", no_argument, (int *) 0, 'V' },
73 {"check", no_argument, (int *) 0, 'c' },
74 {"silent", no_argument, (int *) 0, 's' },
75 {"verbose", no_argument, (int *) 0, 'v' },
76 {"daemon", required_argument, (int *) 0, 'd' },
77 {"nodetach", no_argument, (int *) 0, 'N' },
78 {"quit", no_argument, (int *) 0, 'q' },
79 {"logfile", required_argument, (int *) 0, 'L' },
80 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
81 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
82 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
83 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
84 {"fetchmailrc",required_argument,(int *) 0, 'f' },
85 {"idfile", required_argument, (int *) 0, 'i' },
86 {"pidfile", required_argument, (int *) 0, LA_PIDFILE },
87 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
88 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
89 {"nosoftbounce", no_argument, (int *) 0, LA_NOSOFTBOUNCE },
90 {"softbounce", no_argument, (int *) 0, LA_SOFTBOUNCE },
92 {"protocol", required_argument, (int *) 0, 'p' },
93 {"proto", required_argument, (int *) 0, 'p' },
94 {"uidl", no_argument, (int *) 0, 'U' },
95 {"idle", no_argument, (int *) 0, LA_IDLE},
96 {"port", required_argument, (int *) 0, 'P' },
97 {"service", required_argument, (int *) 0, 'P' },
98 {"auth", required_argument, (int *) 0, LA_AUTH},
99 {"timeout", required_argument, (int *) 0, 't' },
100 {"envelope", required_argument, (int *) 0, 'E' },
101 {"qvirtual", required_argument, (int *) 0, 'Q' },
102 {"bad-header",required_argument, (int *) 0, LA_BADHEADER},
104 {"user", required_argument, (int *) 0, 'u' },
105 {"username", required_argument, (int *) 0, 'u' },
107 {"all", no_argument, (int *) 0, 'a' },
108 {"fetchall", no_argument, (int *) 0, 'a' },
109 {"nokeep", no_argument, (int *) 0, 'K' },
110 {"keep", no_argument, (int *) 0, 'k' },
111 {"flush", no_argument, (int *) 0, 'F' },
112 {"limitflush", no_argument, (int *) 0, LA_LIMITFLUSH },
113 {"norewrite", no_argument, (int *) 0, 'n' },
114 {"limit", required_argument, (int *) 0, 'l' },
115 {"warnings", required_argument, (int *) 0, 'w' },
117 {"folder", required_argument, (int *) 0, 'r' },
118 {"smtphost", required_argument, (int *) 0, 'S' },
119 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
120 {"smtpaddress", required_argument, (int *) 0, 'D' },
121 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
122 {"antispam", required_argument, (int *) 0, 'Z' },
124 {"batchlimit",required_argument, (int *) 0, 'b' },
125 {"fetchlimit",required_argument, (int *) 0, 'B' },
126 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
127 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
128 {"expunge", required_argument, (int *) 0, 'e' },
129 {"mda", required_argument, (int *) 0, 'm' },
130 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
131 {"lmtp", no_argument, (int *) 0, LA_LMTP },
134 {"ssl", no_argument, (int *) 0, LA_SSL },
135 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
136 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
137 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
138 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
139 {"sslcertfile", required_argument, (int *) 0, LA_SSLCERTFILE },
140 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
141 {"sslcommonname", required_argument, (int *) 0, LA_SSLCOMMONNAME },
142 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
145 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
148 {"interface", required_argument, (int *) 0, 'I' },
149 {"monitor", required_argument, (int *) 0, 'M' },
150 #endif /* CAN_MONITOR */
151 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
152 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
154 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
156 {"yydebug", no_argument, (int *) 0, 'y' },
158 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
160 {(char *) 0, no_argument, (int *) 0, 0 }
163 static int xatoi(char *s, int *errflagptr)
164 /* do safe conversion from string to number */
166 /* parse and convert numbers, but also check for invalid characters in
175 value = strtol(s, &endptr, 0);
177 /* any invalid chars in string? */
178 if ( (endptr == s) || (*endptr != '\0') ) {
179 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
184 /* is the range valid? */
185 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
186 (value > INT_MAX) || (value < INT_MIN)) {
188 (void) fprintf(stderr, GT_("Value of string '%s' is %s than %d.\n"), s,
189 (value < 0) ? GT_("smaller"): GT_("larger"),
190 (value < 0) ? INT_MIN : INT_MAX);
195 return (int) value; /* shut up, I know what I'm doing */
198 /** parse and validate the command line options */
199 int parsecmdline (int argc /** argument count */,
200 char **argv /** argument strings */,
201 struct runctl *rctl /** global run controls to modify */,
202 struct query *ctl /** option record to initialize */)
205 * return value: if positive, argv index of last parsed option + 1
206 * (presumes one or more server names follows). if zero, the
207 * command line switches are such that no server names are
208 * required (e.g. --version). if negative, the command line is
209 * has one or more syntax errors.
213 int ocount = 0; /* count of destinations specified */
214 int errflag = 0; /* TRUE when a syntax error is detected */
215 int helpflag = 0; /* TRUE when option help was explicitly requested */
219 rctl->poll_interval = -1;
221 memset(ctl, '\0', sizeof(struct query)); /* start clean */
222 ctl->smtp_socket = -1;
225 (c = getopt_long(argc,argv,shortoptions,
226 longoptions, &option_index)) != -1)
231 ctl->pwmd_socket = prependdir(optarg, currentwd);
234 ctl->pwmd_file = xstrdup(optarg);
237 rctl->pinentry_timeout = atoi(optarg);
250 if (outlevel >= O_VERBOSE)
253 outlevel = O_VERBOSE;
256 rctl->poll_interval = xatoi(optarg, &errflag);
266 rctl->logfile = prependdir (optarg, currentwd);
269 rctl->invisible = FLAG_TRUE;
272 rctl->showdots = FLAG_TRUE;
276 rcfile = prependdir (optarg, currentwd);
279 rctl->idfile = prependdir (optarg, currentwd);
282 rctl->pidfile = prependdir (optarg, currentwd);
285 rctl->postmaster = (char *) xstrdup(optarg);
288 rctl->bouncemail = FLAG_FALSE;
290 case LA_NOSOFTBOUNCE:
291 rctl->softbounce = FLAG_FALSE;
294 rctl->softbounce = FLAG_TRUE;
297 if (strcasecmp(optarg,"accept") == 0) {
298 ctl->server.badheader = BHACCEPT;
299 } else if (strcasecmp(optarg,"reject") == 0) {
300 ctl->server.badheader = BHREJECT;
302 fprintf(stderr,GT_("Invalid bad-header policy `%s' specified.\n"), optarg);
308 /* XXX -- should probably use a table lookup here */
309 if (strcasecmp(optarg,"auto") == 0)
310 ctl->server.protocol = P_AUTO;
312 else if (strcasecmp(optarg,"sdps") == 0)
314 ctl->server.protocol = P_POP3;
315 ctl->server.sdps = TRUE;
317 #endif /* SDPS_ENABLE */
318 else if (strcasecmp(optarg,"pop3") == 0)
319 ctl->server.protocol = P_POP3;
320 else if (strcasecmp(optarg,"apop") == 0)
321 ctl->server.protocol = P_APOP;
322 else if (strcasecmp(optarg,"kpop") == 0)
324 ctl->server.protocol = P_POP3;
325 ctl->server.service = KPOP_PORT;
327 ctl->server.authenticate = A_KERBEROS_V5;
328 #endif /* KERBEROS_V5 */
330 else if (strcasecmp(optarg,"imap") == 0)
331 ctl->server.protocol = P_IMAP;
332 else if (strcasecmp(optarg,"etrn") == 0)
333 ctl->server.protocol = P_ETRN;
334 else if (strcasecmp(optarg,"odmr") == 0)
335 ctl->server.protocol = P_ODMR;
337 fprintf(stderr,GT_("Invalid protocol `%s' specified.\n"), optarg);
342 ctl->server.uidl = FLAG_TRUE;
345 ctl->idle = FLAG_TRUE;
348 ctl->server.service = optarg;
351 if (strcmp(optarg, "password") == 0)
352 ctl->server.authenticate = A_PASSWORD;
354 else if (strcmp(optarg, "kerberos") == 0)
355 ctl->server.authenticate = A_KERBEROS_V5;
356 else if (strcmp(optarg, "kerberos_v5") == 0)
357 ctl->server.authenticate = A_KERBEROS_V5;
358 #endif /* KERBEROS_V5 */
359 else if (strcmp(optarg, "ssh") == 0)
360 ctl->server.authenticate = A_SSH;
361 else if (strcasecmp(optarg, "external") == 0)
362 ctl->server.authenticate = A_EXTERNAL;
363 else if (strcmp(optarg, "otp") == 0)
364 ctl->server.authenticate = A_OTP;
365 else if (strcmp(optarg, "opie") == 0)
366 ctl->server.authenticate = A_OTP;
367 else if (strcmp(optarg, "ntlm") == 0)
368 ctl->server.authenticate = A_NTLM;
369 else if (strcmp(optarg, "cram") == 0)
370 ctl->server.authenticate = A_CRAM_MD5;
371 else if (strcmp(optarg, "cram-md5") == 0)
372 ctl->server.authenticate = A_CRAM_MD5;
373 else if (strcmp(optarg, "gssapi") == 0)
374 ctl->server.authenticate = A_GSSAPI;
375 else if (strcmp(optarg, "any") == 0)
376 ctl->server.authenticate = A_ANY;
377 else if (strcmp(optarg, "msn") == 0)
378 ctl->server.authenticate = A_MSN;
380 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
385 ctl->server.timeout = xatoi(optarg, &errflag);
386 if (ctl->server.timeout == 0)
387 ctl->server.timeout = -1;
390 ctl->server.envelope = xstrdup(optarg);
393 ctl->server.qvirtual = xstrdup(optarg);
397 ctl->remotename = xstrdup(optarg);
400 ctl->fetchall = FLAG_TRUE;
403 ctl->keep = FLAG_FALSE;
406 ctl->keep = FLAG_TRUE;
409 ctl->flush = FLAG_TRUE;
412 ctl->limitflush = FLAG_TRUE;
415 ctl->rewrite = FLAG_FALSE;
418 c = xatoi(optarg, &errflag);
419 ctl->limit = NUM_VALUE_IN(c);
422 buf = xstrdup(optarg);
423 cp = strtok(buf, ",");
425 save_str(&ctl->mailboxes, cp, 0);
427 ((cp = strtok((char *)NULL, ",")));
431 buf = xstrdup(optarg);
432 cp = strtok(buf, ",");
434 save_str(&ctl->smtphunt, cp, TRUE);
436 ((cp = strtok((char *)NULL, ",")));
440 case LA_FETCHDOMAINS:
441 buf = xstrdup(optarg);
442 cp = strtok(buf, ",");
444 save_str(&ctl->domainlist, cp, TRUE);
446 ((cp = strtok((char *)NULL, ",")));
450 ctl->smtpaddress = xstrdup(optarg);
453 ctl->smtpname = xstrdup(optarg);
456 buf = xstrdup(optarg);
457 cp = strtok(buf, ",");
459 struct idlist *idp = save_str(&ctl->antispam, STRING_DUMMY, 0);
461 idp->val.status.num = xatoi(cp, &errflag);
463 ((cp = strtok((char *)NULL, ",")));
467 c = xatoi(optarg, &errflag);
468 ctl->batchlimit = NUM_VALUE_IN(c);
471 c = xatoi(optarg, &errflag);
472 ctl->fetchlimit = NUM_VALUE_IN(c);
474 case LA_FETCHSIZELIMIT:
475 c = xatoi(optarg, &errflag);
476 ctl->fetchsizelimit = NUM_VALUE_IN(c);
479 c = xatoi(optarg, &errflag);
480 ctl->fastuidl = NUM_VALUE_IN(c);
483 c = xatoi(optarg, &errflag);
484 ctl->expunge = NUM_VALUE_IN(c);
487 ctl->mda = xstrdup(optarg);
491 ctl->bsmtp = prependdir (optarg, currentwd);
495 ctl->listener = LMTP_MODE;
500 interface_parse(optarg, &ctl->server);
503 ctl->server.monitor = xstrdup(optarg);
505 #endif /* CAN_MONITOR */
507 ctl->server.plugin = xstrdup(optarg);
510 ctl->server.plugout = xstrdup(optarg);
515 ctl->use_ssl = FLAG_TRUE;
519 ctl->sslkey = prependdir (optarg, currentwd);
523 ctl->sslcert = prependdir (optarg, currentwd);
527 ctl->sslproto = xstrdup(optarg);
531 ctl->sslcertck = FLAG_TRUE;
535 ctl->sslcertfile = prependdir(optarg, currentwd);
539 ctl->sslcertpath = prependdir(optarg, currentwd);
542 case LA_SSLCOMMONNAME:
543 ctl->sslcommonname = xstrdup(optarg);
546 case LA_SSLFINGERPRINT:
547 ctl->sslfingerprint = xstrdup(optarg);
552 ctl->server.principal = xstrdup(optarg);
560 c = xatoi(optarg, &errflag);
561 ctl->warnings = NUM_VALUE_IN(c);
569 rctl->use_syslog = FLAG_TRUE;
573 rctl->use_syslog = FLAG_FALSE;
577 ctl->server.tracepolls = FLAG_TRUE;
586 if (errflag || ocount > 1 || helpflag) {
587 /* squawk if syntax errors were detected */
588 #define P(s) fputs(s, helpflag ? stdout : stderr)
589 P(GT_("usage: fetchmail [options] [server ...]\n"));
590 P(GT_(" Options are as follows:\n"));
591 P(GT_(" -?, --help display this option help\n"));
592 P(GT_(" -V, --version display version info\n"));
594 P(GT_(" -c, --check check for messages without fetching\n"));
595 P(GT_(" -s, --silent work silently\n"));
596 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
597 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
598 P(GT_(" -N, --nodetach don't detach daemon process\n"));
599 P(GT_(" -q, --quit kill daemon process\n"));
600 P(GT_(" -L, --logfile specify logfile name\n"));
601 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
602 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
603 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
604 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
605 P(GT_(" --pidfile specify alternate PID (lock) file\n"));
606 P(GT_(" --postmaster specify recipient of last resort\n"));
607 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
608 P(GT_(" --nosoftbounce fetchmail deletes permanently undeliverable messages.\n"));
609 P(GT_(" --softbounce keep permanently undeliverable messages on server (default).\n"));
611 P(GT_(" -I, --interface interface required specification\n"));
612 P(GT_(" -M, --monitor monitor interface for activity\n"));
614 #if defined( SSL_ENABLE )
615 P(GT_(" --ssl enable ssl encrypted session\n"));
616 P(GT_(" --sslkey ssl private key file\n"));
617 P(GT_(" --sslcert ssl client certificate\n"));
618 P(GT_(" --sslcertck do strict server certificate check (recommended)\n"));
619 P(GT_(" --sslcertfile path to trusted-CA ssl certificate file\n"));
620 P(GT_(" --sslcertpath path to trusted-CA ssl certificate directory\n"));
621 P(GT_(" --sslcommonname expect this CommonName from server (discouraged)\n"));
622 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\n"));
623 P(GT_(" --sslproto force ssl protocol (SSL2/SSL3/TLS1)\n"));
625 P(GT_(" --plugin specify external command to open connection\n"));
626 P(GT_(" --plugout specify external command to open smtp connection\n"));
627 P(GT_(" --bad-header {reject|accept}\n"
628 " specify policy for handling messages with bad headers\n"));
630 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
632 P(GT_(" -C, --pwmd-socket pwmd socket path (~/.pwmd/socket)\n"));
633 P(GT_(" -G, --pwmd-file filename to use on the pwmd server\n"));
634 P(GT_(" -O, --pinentry-timeout seconds until pinentry is canceled\n"));
637 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
638 P(GT_(" --port TCP port to connect to (obsolete, use --service)\n"));
639 P(GT_(" -P, --service TCP service to connect to (can be numeric TCP port)\n"));
640 P(GT_(" --auth authentication type (password/kerberos/ssh/otp)\n"));
641 P(GT_(" -t, --timeout server nonresponse timeout\n"));
642 P(GT_(" -E, --envelope envelope address header\n"));
643 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
644 P(GT_(" --principal mail service principal\n"));
645 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
647 P(GT_(" -u, --username specify users's login on server\n"));
648 P(GT_(" -a, --[fetch]all retrieve old and new messages\n"));
649 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
650 P(GT_(" -k, --keep save new messages after retrieval\n"));
651 P(GT_(" -F, --flush delete old messages from server\n"));
652 P(GT_(" --limitflush delete oversized messages\n"));
653 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
654 P(GT_(" -l, --limit don't fetch messages over given size\n"));
655 P(GT_(" -w, --warnings interval between warning mail notification\n"));
657 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
658 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
659 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
660 P(GT_(" --smtpname set SMTP full name username@domain\n"));
661 P(GT_(" -Z, --antispam, set antispam response values\n"));
662 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
663 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
664 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
665 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
666 P(GT_(" -e, --expunge set max deletions between expunges\n"));
667 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
668 P(GT_(" --bsmtp set output BSMTP file\n"));
669 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
670 P(GT_(" -r, --folder specify remote folder name\n"));
671 P(GT_(" --showdots show progress dots even in logfiles\n"));
674 * --configdump (internal use by fetchmailconf, dumps
675 * configuration as Python source code)
676 * --yydebug (developer use, enables parser debugging) */
687 /* options.c ends here */