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"
56 /* options still left: CgGhHjJoORTWxXYz */
57 static const char *shortoptions =
58 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:I:M:yw:D:";
60 static const struct option longoptions[] = {
61 /* this can be const because all flag fields are 0 and will never get set */
62 {"help", no_argument, (int *) 0, '?' },
63 {"version", no_argument, (int *) 0, 'V' },
64 {"check", no_argument, (int *) 0, 'c' },
65 {"silent", no_argument, (int *) 0, 's' },
66 {"verbose", no_argument, (int *) 0, 'v' },
67 {"daemon", required_argument, (int *) 0, 'd' },
68 {"nodetach", no_argument, (int *) 0, 'N' },
69 {"quit", no_argument, (int *) 0, 'q' },
70 {"logfile", required_argument, (int *) 0, 'L' },
71 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
72 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
73 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
74 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
75 {"fetchmailrc",required_argument,(int *) 0, 'f' },
76 {"idfile", required_argument, (int *) 0, 'i' },
77 {"pidfile", required_argument, (int *) 0, LA_PIDFILE },
78 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
79 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
81 {"protocol", required_argument, (int *) 0, 'p' },
82 {"proto", required_argument, (int *) 0, 'p' },
83 {"uidl", no_argument, (int *) 0, 'U' },
84 {"idle", no_argument, (int *) 0, LA_IDLE},
85 {"port", required_argument, (int *) 0, 'P' },
86 {"service", required_argument, (int *) 0, 'P' },
87 {"auth", required_argument, (int *) 0, LA_AUTH},
88 {"timeout", required_argument, (int *) 0, 't' },
89 {"envelope", required_argument, (int *) 0, 'E' },
90 {"qvirtual", required_argument, (int *) 0, 'Q' },
92 {"user", required_argument, (int *) 0, 'u' },
93 {"username", required_argument, (int *) 0, 'u' },
95 {"all", no_argument, (int *) 0, 'a' },
96 {"fetchall", no_argument, (int *) 0, 'a' },
97 {"nokeep", no_argument, (int *) 0, 'K' },
98 {"keep", no_argument, (int *) 0, 'k' },
99 {"flush", no_argument, (int *) 0, 'F' },
100 {"limitflush", no_argument, (int *) 0, LA_LIMITFLUSH },
101 {"norewrite", no_argument, (int *) 0, 'n' },
102 {"limit", required_argument, (int *) 0, 'l' },
103 {"warnings", required_argument, (int *) 0, 'w' },
105 {"folder", required_argument, (int *) 0, 'r' },
106 {"smtphost", required_argument, (int *) 0, 'S' },
107 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
108 {"smtpaddress", required_argument, (int *) 0, 'D' },
109 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
110 {"antispam", required_argument, (int *) 0, 'Z' },
112 {"batchlimit",required_argument, (int *) 0, 'b' },
113 {"fetchlimit",required_argument, (int *) 0, 'B' },
114 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
115 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
116 {"expunge", required_argument, (int *) 0, 'e' },
117 {"mda", required_argument, (int *) 0, 'm' },
118 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
119 {"lmtp", no_argument, (int *) 0, LA_LMTP },
122 {"ssl", no_argument, (int *) 0, LA_SSL },
123 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
124 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
125 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
126 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
127 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
128 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
131 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
134 {"interface", required_argument, (int *) 0, 'I' },
135 {"monitor", required_argument, (int *) 0, 'M' },
136 #endif /* CAN_MONITOR */
137 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
138 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
140 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
142 {"yydebug", no_argument, (int *) 0, 'y' },
144 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
146 {(char *) 0, no_argument, (int *) 0, 0 }
149 static int xatoi(char *s, int *errflagptr)
150 /* do safe conversion from string to number */
152 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
153 /* parse and convert numbers, but also check for invalid characters in
162 value = strtol(s, &endptr, 0);
164 /* any invalid chars in string? */
165 if ( (endptr == s) || (*endptr != '\0') ) {
166 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
171 /* is the range valid? */
172 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
173 (value > INT_MAX) || (value < INT_MIN)) {
175 (void) fprintf(stderr, GT_("Value of string '%s' is %s than %d.\n"), s,
176 (value < 0) ? GT_("smaller"): GT_("larger"),
177 (value < 0) ? INT_MIN : INT_MAX);
182 return (int) value; /* shut up, I know what I'm doing */
186 # if defined (STDC_HEADERS)
192 /* We do only base 10 conversions here (atoi)! */
195 /* check for leading white spaces */
196 for (i = 0; (i < len) && isspace((unsigned char)s[i]); i++)
202 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
204 /* skip over digits */
205 for ( /* no init */ ; (i < len) && isdigit((unsigned char)s[i]); i++)
208 /* check for trailing garbage */
210 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
215 /* atoi should be safe by now, except for number range over/underflow */
220 /** parse and validate the command line options */
221 int parsecmdline (int argc /** argument count */,
222 char **argv /** argument strings */,
223 struct runctl *rctl /** global run controls to modify */,
224 struct query *ctl /** option record to initialize */)
227 * return value: if positive, argv index of last parsed option + 1
228 * (presumes one or more server names follows). if zero, the
229 * command line switches are such that no server names are
230 * required (e.g. --version). if negative, the command line is
231 * has one or more syntax errors.
235 int ocount = 0; /* count of destinations specified */
236 int errflag = 0; /* TRUE when a syntax error is detected */
237 int helpflag = 0; /* TRUE when option help was explicitly requested */
241 rctl->poll_interval = -1;
243 memset(ctl, '\0', sizeof(struct query)); /* start clean */
244 ctl->smtp_socket = -1;
247 (c = getopt_long(argc,argv,shortoptions,
248 longoptions, &option_index)) != -1)
261 if (outlevel >= O_VERBOSE)
264 outlevel = O_VERBOSE;
267 rctl->poll_interval = xatoi(optarg, &errflag);
277 rctl->logfile = prependdir (optarg, currentwd);
280 rctl->invisible = TRUE;
283 rctl->showdots = FLAG_TRUE;
287 rcfile = prependdir (optarg, currentwd);
290 rctl->idfile = prependdir (optarg, currentwd);
293 rctl->pidfile = prependdir (optarg, currentwd);
296 rctl->postmaster = (char *) xstrdup(optarg);
299 run.bouncemail = FALSE;
302 /* XXX -- should probably use a table lookup here */
303 if (strcasecmp(optarg,"auto") == 0)
304 ctl->server.protocol = P_AUTO;
305 else if (strcasecmp(optarg,"pop2") == 0)
306 ctl->server.protocol = P_POP2;
308 else if (strcasecmp(optarg,"sdps") == 0)
310 ctl->server.protocol = P_POP3;
311 ctl->server.sdps = TRUE;
313 #endif /* SDPS_ENABLE */
314 else if (strcasecmp(optarg,"pop3") == 0)
315 ctl->server.protocol = P_POP3;
316 else if (strcasecmp(optarg,"apop") == 0)
317 ctl->server.protocol = P_APOP;
318 else if (strcasecmp(optarg,"rpop") == 0)
319 ctl->server.protocol = P_RPOP;
320 else if (strcasecmp(optarg,"kpop") == 0)
322 ctl->server.protocol = P_POP3;
323 ctl->server.service = KPOP_PORT;
325 ctl->server.authenticate = A_KERBEROS_V5;
327 ctl->server.authenticate = A_KERBEROS_V4;
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;
353 else if (strcmp(optarg, "kerberos") == 0)
355 ctl->server.authenticate = A_KERBEROS_V5;
357 ctl->server.authenticate = A_KERBEROS_V4;
358 #endif /* KERBEROS_V5 */
359 else if (strcmp(optarg, "kerberos_v5") == 0)
360 ctl->server.authenticate = A_KERBEROS_V5;
361 else if (strcmp(optarg, "kerberos_v4") == 0)
362 ctl->server.authenticate = A_KERBEROS_V4;
363 else if (strcmp(optarg, "ssh") == 0)
364 ctl->server.authenticate = A_SSH;
365 else if (strcasecmp(optarg, "external") == 0)
366 ctl->server.authenticate = A_EXTERNAL;
367 else if (strcmp(optarg, "otp") == 0)
368 ctl->server.authenticate = A_OTP;
369 else if (strcmp(optarg, "opie") == 0)
370 ctl->server.authenticate = A_OTP;
371 else if (strcmp(optarg, "ntlm") == 0)
372 ctl->server.authenticate = A_NTLM;
373 else if (strcmp(optarg, "cram") == 0)
374 ctl->server.authenticate = A_CRAM_MD5;
375 else if (strcmp(optarg, "cram-md5") == 0)
376 ctl->server.authenticate = A_CRAM_MD5;
377 else if (strcmp(optarg, "gssapi") == 0)
378 ctl->server.authenticate = A_GSSAPI;
379 else if (strcmp(optarg, "any") == 0)
380 ctl->server.authenticate = A_ANY;
381 else if (strcmp(optarg, "msn") == 0)
382 ctl->server.authenticate = A_MSN;
384 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
389 ctl->server.timeout = xatoi(optarg, &errflag);
390 if (ctl->server.timeout == 0)
391 ctl->server.timeout = -1;
394 ctl->server.envelope = xstrdup(optarg);
397 ctl->server.qvirtual = xstrdup(optarg);
401 ctl->remotename = xstrdup(optarg);
404 ctl->fetchall = FLAG_TRUE;
407 ctl->keep = FLAG_FALSE;
410 ctl->keep = FLAG_TRUE;
413 ctl->flush = FLAG_TRUE;
416 ctl->limitflush = FLAG_TRUE;
419 ctl->rewrite = FLAG_FALSE;
422 c = xatoi(optarg, &errflag);
423 ctl->limit = NUM_VALUE_IN(c);
426 buf = xstrdup(optarg);
427 cp = strtok(buf, ",");
429 save_str(&ctl->mailboxes, cp, 0);
431 ((cp = strtok((char *)NULL, ",")));
435 buf = xstrdup(optarg);
436 cp = strtok(buf, ",");
438 save_str(&ctl->smtphunt, cp, TRUE);
440 ((cp = strtok((char *)NULL, ",")));
444 case LA_FETCHDOMAINS:
445 buf = xstrdup(optarg);
446 cp = strtok(buf, ",");
448 save_str(&ctl->domainlist, cp, TRUE);
450 ((cp = strtok((char *)NULL, ",")));
454 ctl->smtpaddress = xstrdup(optarg);
457 ctl->smtpname = xstrdup(optarg);
460 buf = xstrdup(optarg);
461 cp = strtok(buf, ",");
463 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
465 idp->val.status.num = xatoi(cp, &errflag);
467 ((cp = strtok((char *)NULL, ",")));
471 c = xatoi(optarg, &errflag);
472 ctl->batchlimit = NUM_VALUE_IN(c);
475 c = xatoi(optarg, &errflag);
476 ctl->fetchlimit = NUM_VALUE_IN(c);
478 case LA_FETCHSIZELIMIT:
479 c = xatoi(optarg, &errflag);
480 ctl->fetchsizelimit = NUM_VALUE_IN(c);
483 c = xatoi(optarg, &errflag);
484 ctl->fastuidl = NUM_VALUE_IN(c);
487 c = xatoi(optarg, &errflag);
488 ctl->expunge = NUM_VALUE_IN(c);
491 ctl->mda = xstrdup(optarg);
495 ctl->bsmtp = prependdir (optarg, currentwd);
499 ctl->listener = LMTP_MODE;
504 interface_parse(optarg, &ctl->server);
507 ctl->server.monitor = xstrdup(optarg);
509 #endif /* CAN_MONITOR */
511 ctl->server.plugin = xstrdup(optarg);
514 ctl->server.plugout = xstrdup(optarg);
519 ctl->use_ssl = FLAG_TRUE;
523 ctl->sslkey = prependdir (optarg, currentwd);
527 ctl->sslcert = prependdir (optarg, currentwd);
531 ctl->sslproto = xstrdup(optarg);
535 ctl->sslcertck = FLAG_TRUE;
539 ctl->sslcertpath = prependdir(optarg, currentwd);
542 case LA_SSLFINGERPRINT:
543 ctl->sslfingerprint = xstrdup(optarg);
548 ctl->server.principal = xstrdup(optarg);
556 c = xatoi(optarg, &errflag);
557 ctl->warnings = NUM_VALUE_IN(c);
565 rctl->use_syslog = FLAG_TRUE;
569 rctl->use_syslog = FLAG_FALSE;
573 ctl->server.tracepolls = FLAG_TRUE;
582 if (errflag || ocount > 1 || helpflag) {
583 /* squawk if syntax errors were detected */
584 #define P(s) fputs(s, helpflag ? stdout : stderr)
585 P(GT_("usage: fetchmail [options] [server ...]\n"));
586 P(GT_(" Options are as follows:\n"));
587 P(GT_(" -?, --help display this option help\n"));
588 P(GT_(" -V, --version display version info\n"));
590 P(GT_(" -c, --check check for messages without fetching\n"));
591 P(GT_(" -s, --silent work silently\n"));
592 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
593 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
594 P(GT_(" -N, --nodetach don't detach daemon process\n"));
595 P(GT_(" -q, --quit kill daemon process\n"));
596 P(GT_(" -L, --logfile specify logfile name\n"));
597 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
598 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
599 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
600 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
601 P(GT_(" --pidfile specify alternate PID (lock) file\n"));
602 P(GT_(" --postmaster specify recipient of last resort\n"));
603 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
605 P(GT_(" -I, --interface interface required specification\n"));
606 P(GT_(" -M, --monitor monitor interface for activity\n"));
608 #if defined( SSL_ENABLE )
609 P(GT_(" --ssl enable ssl encrypted session\n"));
610 P(GT_(" --sslkey ssl private key file\n"));
611 P(GT_(" --sslcert ssl client certificate\n"));
612 P(GT_(" --sslcertck do strict server certificate check (recommended)\n"));
613 P(GT_(" --sslcertpath path to ssl certificates\n"));
614 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\n"));
615 P(GT_(" --sslproto force ssl protocol (SSL2/SSL3/TLS1)\n"));
617 P(GT_(" --plugin specify external command to open connection\n"));
618 P(GT_(" --plugout specify external command to open smtp connection\n"));
620 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
621 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
622 P(GT_(" --port TCP port to connect to (obsolete, use --service)\n"));
623 P(GT_(" -P, --service TCP service to connect to (can be numeric TCP port)\n"));
624 P(GT_(" --auth authentication type (password/kerberos/ssh/otp)\n"));
625 P(GT_(" -t, --timeout server nonresponse timeout\n"));
626 P(GT_(" -E, --envelope envelope address header\n"));
627 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
628 P(GT_(" --principal mail service principal\n"));
629 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
631 P(GT_(" -u, --username specify users's login on server\n"));
632 P(GT_(" -a, --[fetch]all retrieve old and new messages\n"));
633 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
634 P(GT_(" -k, --keep save new messages after retrieval\n"));
635 P(GT_(" -F, --flush delete old messages from server\n"));
636 P(GT_(" --limitflush delete oversized messages\n"));
637 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
638 P(GT_(" -l, --limit don't fetch messages over given size\n"));
639 P(GT_(" -w, --warnings interval between warning mail notification\n"));
641 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
642 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
643 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
644 P(GT_(" --smtpname set SMTP full name username@domain\n"));
645 P(GT_(" -Z, --antispam, set antispam response values\n"));
646 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
647 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
648 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
649 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
650 P(GT_(" -e, --expunge set max deletions between expunges\n"));
651 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
652 P(GT_(" --bsmtp set output BSMTP file\n"));
653 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
654 P(GT_(" -r, --folder specify remote folder name\n"));
655 P(GT_(" --showdots show progress dots even in logfiles\n"));
658 * --configdump (internal use by fetchmailconf, dumps
659 * configuration as Python source code)
660 * --yydebug (developer use, enables parser debugging) */
671 /* options.c ends here */