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 */)
207 * return value: if positive, argv index of last parsed option + 1
208 * (presumes one or more server names follows). if zero, the
209 * command line switches are such that no server names are
210 * required (e.g. --version). if negative, the command line is
211 * has one or more syntax errors.
215 int ocount = 0; /* count of destinations specified */
216 int errflag = 0; /* TRUE when a syntax error is detected */
217 int helpflag = 0; /* TRUE when option help was explicitly requested */
221 rctl->poll_interval = -1;
223 memset(ctl, '\0', sizeof(struct query)); /* start clean */
224 ctl->smtp_socket = -1;
227 (c = getopt_long(argc,argv,shortoptions,
228 longoptions, &option_index)) != -1)
233 ctl->pwmd_socket = prependdir(optarg, currentwd);
236 ctl->pwmd_file = xstrdup(optarg);
239 rctl->pinentry_timeout = atoi(optarg);
252 if (outlevel >= O_VERBOSE)
255 outlevel = O_VERBOSE;
258 rctl->poll_interval = xatoi(optarg, &errflag);
268 rctl->logfile = prependdir (optarg, currentwd);
271 rctl->invisible = FLAG_TRUE;
274 rctl->showdots = FLAG_TRUE;
278 rcfile = prependdir (optarg, currentwd);
281 rctl->idfile = prependdir (optarg, currentwd);
284 rctl->pidfile = prependdir (optarg, currentwd);
287 rctl->postmaster = (char *) xstrdup(optarg);
290 rctl->bouncemail = FLAG_FALSE;
292 case LA_NOSOFTBOUNCE:
293 rctl->softbounce = FLAG_FALSE;
296 rctl->softbounce = FLAG_TRUE;
299 if (strcasecmp(optarg,"accept") == 0) {
300 ctl->server.badheader = BHACCEPT;
301 } else if (strcasecmp(optarg,"reject") == 0) {
302 ctl->server.badheader = BHREJECT;
304 fprintf(stderr,GT_("Invalid bad-header policy `%s' specified.\n"), optarg);
310 /* XXX -- should probably use a table lookup here */
311 if (strcasecmp(optarg,"auto") == 0)
312 ctl->server.protocol = P_AUTO;
314 else if (strcasecmp(optarg,"sdps") == 0)
316 ctl->server.protocol = P_POP3;
317 ctl->server.sdps = TRUE;
319 #endif /* SDPS_ENABLE */
320 else if (strcasecmp(optarg,"pop3") == 0)
321 ctl->server.protocol = P_POP3;
322 else if (strcasecmp(optarg,"apop") == 0)
323 ctl->server.protocol = P_APOP;
324 else if (strcasecmp(optarg,"kpop") == 0)
326 ctl->server.protocol = P_POP3;
327 ctl->server.service = KPOP_PORT;
329 ctl->server.authenticate = A_KERBEROS_V5;
330 #endif /* KERBEROS_V5 */
332 else if (strcasecmp(optarg,"imap") == 0)
333 ctl->server.protocol = P_IMAP;
334 else if (strcasecmp(optarg,"etrn") == 0)
335 ctl->server.protocol = P_ETRN;
336 else if (strcasecmp(optarg,"odmr") == 0)
337 ctl->server.protocol = P_ODMR;
339 fprintf(stderr,GT_("Invalid protocol `%s' specified.\n"), optarg);
344 ctl->server.uidl = FLAG_TRUE;
347 ctl->idle = FLAG_TRUE;
350 ctl->server.service = optarg;
353 if (strcmp(optarg, "password") == 0)
354 ctl->server.authenticate = A_PASSWORD;
356 else if (strcmp(optarg, "kerberos") == 0)
357 ctl->server.authenticate = A_KERBEROS_V5;
358 else if (strcmp(optarg, "kerberos_v5") == 0)
359 ctl->server.authenticate = A_KERBEROS_V5;
360 #endif /* KERBEROS_V5 */
361 else if (strcmp(optarg, "ssh") == 0)
362 ctl->server.authenticate = A_SSH;
363 else if (strcasecmp(optarg, "external") == 0)
364 ctl->server.authenticate = A_EXTERNAL;
365 else if (strcmp(optarg, "otp") == 0)
366 ctl->server.authenticate = A_OTP;
367 else if (strcmp(optarg, "opie") == 0)
368 ctl->server.authenticate = A_OTP;
369 else if (strcmp(optarg, "ntlm") == 0)
370 ctl->server.authenticate = A_NTLM;
371 else if (strcmp(optarg, "cram") == 0)
372 ctl->server.authenticate = A_CRAM_MD5;
373 else if (strcmp(optarg, "cram-md5") == 0)
374 ctl->server.authenticate = A_CRAM_MD5;
375 else if (strcmp(optarg, "gssapi") == 0)
376 ctl->server.authenticate = A_GSSAPI;
377 else if (strcmp(optarg, "any") == 0)
378 ctl->server.authenticate = A_ANY;
379 else if (strcmp(optarg, "msn") == 0)
380 ctl->server.authenticate = A_MSN;
382 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
387 ctl->server.timeout = xatoi(optarg, &errflag);
388 if (ctl->server.timeout == 0)
389 ctl->server.timeout = -1;
392 ctl->server.envelope = xstrdup(optarg);
395 ctl->server.qvirtual = xstrdup(optarg);
399 ctl->remotename = xstrdup(optarg);
402 ctl->fetchall = FLAG_TRUE;
405 ctl->keep = FLAG_FALSE;
408 ctl->keep = FLAG_TRUE;
411 ctl->flush = FLAG_TRUE;
414 ctl->limitflush = FLAG_TRUE;
417 ctl->rewrite = FLAG_FALSE;
420 c = xatoi(optarg, &errflag);
421 ctl->limit = NUM_VALUE_IN(c);
424 buf = xstrdup(optarg);
425 cp = strtok(buf, ",");
427 save_str(&ctl->mailboxes, cp, 0);
429 ((cp = strtok((char *)NULL, ",")));
433 buf = xstrdup(optarg);
434 cp = strtok(buf, ",");
436 save_str(&ctl->smtphunt, cp, TRUE);
438 ((cp = strtok((char *)NULL, ",")));
442 case LA_FETCHDOMAINS:
443 buf = xstrdup(optarg);
444 cp = strtok(buf, ",");
446 save_str(&ctl->domainlist, cp, TRUE);
448 ((cp = strtok((char *)NULL, ",")));
452 ctl->smtpaddress = xstrdup(optarg);
455 ctl->smtpname = xstrdup(optarg);
458 buf = xstrdup(optarg);
459 cp = strtok(buf, ",");
461 struct idlist *idp = save_str(&ctl->antispam, STRING_DUMMY, 0);
463 idp->val.status.num = xatoi(cp, &errflag);
465 ((cp = strtok((char *)NULL, ",")));
469 c = xatoi(optarg, &errflag);
470 ctl->batchlimit = NUM_VALUE_IN(c);
473 c = xatoi(optarg, &errflag);
474 ctl->fetchlimit = NUM_VALUE_IN(c);
476 case LA_FETCHSIZELIMIT:
477 c = xatoi(optarg, &errflag);
478 ctl->fetchsizelimit = NUM_VALUE_IN(c);
481 c = xatoi(optarg, &errflag);
482 ctl->fastuidl = NUM_VALUE_IN(c);
485 c = xatoi(optarg, &errflag);
486 ctl->expunge = NUM_VALUE_IN(c);
489 ctl->mda = xstrdup(optarg);
493 ctl->bsmtp = prependdir (optarg, currentwd);
497 ctl->listener = LMTP_MODE;
502 interface_parse(optarg, &ctl->server);
505 ctl->server.monitor = xstrdup(optarg);
507 #endif /* CAN_MONITOR */
509 ctl->server.plugin = xstrdup(optarg);
512 ctl->server.plugout = xstrdup(optarg);
517 ctl->use_ssl = FLAG_TRUE;
521 ctl->sslkey = prependdir (optarg, currentwd);
525 ctl->sslcert = prependdir (optarg, currentwd);
529 ctl->sslproto = xstrdup(optarg);
533 ctl->sslcertck = FLAG_TRUE;
537 ctl->sslcertfile = prependdir(optarg, currentwd);
541 ctl->sslcertpath = prependdir(optarg, currentwd);
544 case LA_SSLCOMMONNAME:
545 ctl->sslcommonname = xstrdup(optarg);
548 case LA_SSLFINGERPRINT:
549 ctl->sslfingerprint = xstrdup(optarg);
554 ctl->server.principal = xstrdup(optarg);
562 c = xatoi(optarg, &errflag);
563 ctl->warnings = NUM_VALUE_IN(c);
571 rctl->use_syslog = FLAG_TRUE;
575 rctl->use_syslog = FLAG_FALSE;
579 ctl->server.tracepolls = FLAG_TRUE;
582 case LA_RETRIEVEERROR:
583 if (strcasecmp(optarg,"abort") == 0) {
584 ctl->server.retrieveerror = RE_ABORT;
585 } else if (strcasecmp(optarg,"continue") == 0) {
586 ctl->server.retrieveerror = RE_CONTINUE;
587 } else if (strcasecmp(optarg,"markseen") == 0) {
588 ctl->server.retrieveerror = RE_MARKSEEN;
590 fprintf(stderr,GT_("Invalid retrieve-error policy `%s' specified.\n"), optarg);
601 if (errflag || ocount > 1 || helpflag) {
602 /* squawk if syntax errors were detected */
603 #define P(s) fputs(s, helpflag ? stdout : stderr)
604 P(GT_("usage: fetchmail [options] [server ...]\n"));
605 P(GT_(" Options are as follows:\n"));
606 P(GT_(" -?, --help display this option help\n"));
607 P(GT_(" -V, --version display version info\n"));
609 P(GT_(" -c, --check check for messages without fetching\n"));
610 P(GT_(" -s, --silent work silently\n"));
611 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
612 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
613 P(GT_(" -N, --nodetach don't detach daemon process\n"));
614 P(GT_(" -q, --quit kill daemon process\n"));
615 P(GT_(" -L, --logfile specify logfile name\n"));
616 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
617 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
618 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
619 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
620 P(GT_(" --pidfile specify alternate PID (lock) file\n"));
621 P(GT_(" --postmaster specify recipient of last resort\n"));
622 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
623 P(GT_(" --nosoftbounce fetchmail deletes permanently undeliverable messages.\n"));
624 P(GT_(" --softbounce keep permanently undeliverable messages on server (default).\n"));
626 P(GT_(" -I, --interface interface required specification\n"));
627 P(GT_(" -M, --monitor monitor interface for activity\n"));
629 #if defined( SSL_ENABLE )
630 P(GT_(" --ssl enable ssl encrypted session\n"));
631 P(GT_(" --sslkey ssl private key file\n"));
632 P(GT_(" --sslcert ssl client certificate\n"));
633 P(GT_(" --sslcertck do strict server certificate check (recommended)\n"));
634 P(GT_(" --sslcertfile path to trusted-CA ssl certificate file\n"));
635 P(GT_(" --sslcertpath path to trusted-CA ssl certificate directory\n"));
636 P(GT_(" --sslcommonname expect this CommonName from server (discouraged)\n"));
637 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\n"));
638 P(GT_(" --sslproto force ssl protocol (SSL2/SSL3/TLS1)\n"));
640 P(GT_(" --plugin specify external command to open connection\n"));
641 P(GT_(" --plugout specify external command to open smtp connection\n"));
642 P(GT_(" --bad-header {reject|accept}\n"
643 " specify policy for handling messages with bad headers\n"));
644 P(GT_(" --retrieve-error {abort|continue|markseen}\n"
645 " specify policy for processing messages with retrieve errors\n"));
647 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
649 P(GT_(" -C, --pwmd-socket pwmd socket path (~/.pwmd/socket)\n"));
650 P(GT_(" -G, --pwmd-file filename to use on the pwmd server\n"));
651 P(GT_(" -O, --pinentry-timeout seconds until pinentry is canceled\n"));
654 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
655 P(GT_(" --port TCP port to connect to (obsolete, use --service)\n"));
656 P(GT_(" -P, --service TCP service to connect to (can be numeric TCP port)\n"));
657 P(GT_(" --auth authentication type (password/kerberos/ssh/otp)\n"));
658 P(GT_(" -t, --timeout server nonresponse timeout\n"));
659 P(GT_(" -E, --envelope envelope address header\n"));
660 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
661 P(GT_(" --principal mail service principal\n"));
662 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
664 P(GT_(" -u, --username specify users's login on server\n"));
665 P(GT_(" -a, --[fetch]all retrieve old and new messages\n"));
666 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
667 P(GT_(" -k, --keep save new messages after retrieval\n"));
668 P(GT_(" -F, --flush delete old messages from server\n"));
669 P(GT_(" --limitflush delete oversized messages\n"));
670 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
671 P(GT_(" -l, --limit don't fetch messages over given size\n"));
672 P(GT_(" -w, --warnings interval between warning mail notification\n"));
674 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
675 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
676 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
677 P(GT_(" --smtpname set SMTP full name username@domain\n"));
678 P(GT_(" -Z, --antispam, set antispam response values\n"));
679 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
680 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
681 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
682 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
683 P(GT_(" -e, --expunge set max deletions between expunges\n"));
684 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
685 P(GT_(" --bsmtp set output BSMTP file\n"));
686 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
687 P(GT_(" -r, --folder specify remote folder name\n"));
688 P(GT_(" --showdots show progress dots even in logfiles\n"));
691 * --configdump (internal use by fetchmailconf, dumps
692 * configuration as Python source code)
693 * --yydebug (developer use, enables parser debugging) */
704 /* options.c ends here */