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"
54 /* options still left: CgGhHjJoORTWxXYz */
55 static const char *shortoptions =
56 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:I:M:yw:D:";
58 static const struct option longoptions[] = {
59 /* this can be const because all flag fields are 0 and will never get set */
60 {"help", no_argument, (int *) 0, '?' },
61 {"version", no_argument, (int *) 0, 'V' },
62 {"check", no_argument, (int *) 0, 'c' },
63 {"silent", no_argument, (int *) 0, 's' },
64 {"verbose", no_argument, (int *) 0, 'v' },
65 {"daemon", required_argument, (int *) 0, 'd' },
66 {"nodetach", no_argument, (int *) 0, 'N' },
67 {"quit", no_argument, (int *) 0, 'q' },
68 {"logfile", required_argument, (int *) 0, 'L' },
69 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
70 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
71 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
72 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
73 {"fetchmailrc",required_argument,(int *) 0, 'f' },
74 {"idfile", required_argument, (int *) 0, 'i' },
75 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
76 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
78 {"protocol", required_argument, (int *) 0, 'p' },
79 {"proto", required_argument, (int *) 0, 'p' },
80 {"uidl", no_argument, (int *) 0, 'U' },
81 {"port", required_argument, (int *) 0, 'P' },
82 {"service", required_argument, (int *) 0, 'P' },
83 {"auth", required_argument, (int *) 0, LA_AUTH},
84 {"timeout", required_argument, (int *) 0, 't' },
85 {"envelope", required_argument, (int *) 0, 'E' },
86 {"qvirtual", required_argument, (int *) 0, 'Q' },
88 {"user", required_argument, (int *) 0, 'u' },
89 {"username", required_argument, (int *) 0, 'u' },
91 {"all", no_argument, (int *) 0, 'a' },
92 {"nokeep", no_argument, (int *) 0, 'K' },
93 {"keep", no_argument, (int *) 0, 'k' },
94 {"flush", no_argument, (int *) 0, 'F' },
95 {"limitflush", no_argument, (int *) 0, LA_LIMITFLUSH },
96 {"norewrite", no_argument, (int *) 0, 'n' },
97 {"limit", required_argument, (int *) 0, 'l' },
98 {"warnings", required_argument, (int *) 0, 'w' },
100 {"folder", required_argument, (int *) 0, 'r' },
101 {"smtphost", required_argument, (int *) 0, 'S' },
102 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
103 {"smtpaddress", required_argument, (int *) 0, 'D' },
104 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
105 {"antispam", required_argument, (int *) 0, 'Z' },
107 {"batchlimit",required_argument, (int *) 0, 'b' },
108 {"fetchlimit",required_argument, (int *) 0, 'B' },
109 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
110 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
111 {"expunge", required_argument, (int *) 0, 'e' },
112 {"mda", required_argument, (int *) 0, 'm' },
113 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
114 {"lmtp", no_argument, (int *) 0, LA_LMTP },
117 {"ssl", no_argument, (int *) 0, LA_SSL },
118 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
119 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
120 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
121 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
122 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
123 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
126 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
129 {"interface", required_argument, (int *) 0, 'I' },
130 {"monitor", required_argument, (int *) 0, 'M' },
131 #endif /* CAN_MONITOR */
132 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
133 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
135 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
137 {"yydebug", no_argument, (int *) 0, 'y' },
139 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
141 {(char *) 0, no_argument, (int *) 0, 0 }
144 static int xatoi(char *s, int *errflagptr)
145 /* do safe conversion from string to number */
147 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
148 /* parse and convert numbers, but also check for invalid characters in
157 value = strtol(s, &endptr, 0);
159 /* any invalid chars in string? */
160 if ( (endptr == s) || (*endptr != '\0') ) {
161 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
166 /* is the range valid? */
167 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
168 (value > INT_MAX) || (value < INT_MIN)) {
170 (void) fprintf(stderr, GT_("Value of string '%s' is %s than %d.\n"), s,
171 (value < 0) ? GT_("smaller"): GT_("larger"),
172 (value < 0) ? INT_MIN : INT_MAX);
177 return (int) value; /* shut up, I know what I'm doing */
181 # if defined (STDC_HEADERS)
187 /* We do only base 10 conversions here (atoi)! */
190 /* check for leading white spaces */
191 for (i = 0; (i < len) && isspace((unsigned char)s[i]); i++)
197 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
199 /* skip over digits */
200 for ( /* no init */ ; (i < len) && isdigit((unsigned char)s[i]); i++)
203 /* check for trailing garbage */
205 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
210 /* atoi should be safe by now, except for number range over/underflow */
215 int parsecmdline (argc, argv, rctl, ctl)
216 /* parse and validate the command line options */
217 int argc; /* argument count */
218 char **argv; /* argument strings */
219 struct runctl *rctl; /* global run controls to modify */
220 struct query *ctl; /* option record to be initialized */
223 * return value: if positive, argv index of last parsed option + 1
224 * (presumes one or more server names follows). if zero, the
225 * command line switches are such that no server names are
226 * required (e.g. --version). if negative, the command line is
227 * has one or more syntax errors.
231 int ocount = 0; /* count of destinations specified */
232 int errflag = 0; /* TRUE when a syntax error is detected */
233 int helpflag = 0; /* TRUE when option help was explicitly requested */
237 rctl->poll_interval = -1;
239 memset(ctl, '\0', sizeof(struct query)); /* start clean */
240 ctl->smtp_socket = -1;
243 (c = getopt_long(argc,argv,shortoptions,
244 longoptions, &option_index)) != -1)
257 if (outlevel >= O_VERBOSE)
260 outlevel = O_VERBOSE;
263 rctl->poll_interval = xatoi(optarg, &errflag);
273 rctl->logfile = prependdir (optarg, currentwd);
276 rctl->invisible = TRUE;
279 rctl->showdots = FLAG_TRUE;
283 rcfile = prependdir (optarg, currentwd);
286 rctl->idfile = prependdir (optarg, currentwd);
289 rctl->postmaster = (char *) xstrdup(optarg);
292 run.bouncemail = FALSE;
295 /* XXX -- should probably use a table lookup here */
296 if (strcasecmp(optarg,"auto") == 0)
297 ctl->server.protocol = P_AUTO;
298 else if (strcasecmp(optarg,"pop2") == 0)
299 ctl->server.protocol = P_POP2;
301 else if (strcasecmp(optarg,"sdps") == 0)
303 ctl->server.protocol = P_POP3;
304 ctl->server.sdps = TRUE;
306 #endif /* SDPS_ENABLE */
307 else if (strcasecmp(optarg,"pop3") == 0)
308 ctl->server.protocol = P_POP3;
309 else if (strcasecmp(optarg,"apop") == 0)
310 ctl->server.protocol = P_APOP;
311 else if (strcasecmp(optarg,"rpop") == 0)
312 ctl->server.protocol = P_RPOP;
313 else if (strcasecmp(optarg,"kpop") == 0)
315 ctl->server.protocol = P_POP3;
316 ctl->server.service = KPOP_PORT;
318 ctl->server.authenticate = A_KERBEROS_V5;
320 ctl->server.authenticate = A_KERBEROS_V4;
321 #endif /* KERBEROS_V5 */
323 else if (strcasecmp(optarg,"imap") == 0)
324 ctl->server.protocol = P_IMAP;
325 else if (strcasecmp(optarg,"etrn") == 0)
326 ctl->server.protocol = P_ETRN;
327 else if (strcasecmp(optarg,"odmr") == 0)
328 ctl->server.protocol = P_ODMR;
330 fprintf(stderr,GT_("Invalid protocol `%s' specified.\n"), optarg);
335 ctl->server.uidl = FLAG_TRUE;
338 ctl->server.service = optarg;
341 if (strcmp(optarg, "password") == 0)
342 ctl->server.authenticate = A_PASSWORD;
343 else if (strcmp(optarg, "kerberos") == 0)
345 ctl->server.authenticate = A_KERBEROS_V5;
347 ctl->server.authenticate = A_KERBEROS_V4;
348 #endif /* KERBEROS_V5 */
349 else if (strcmp(optarg, "kerberos_v5") == 0)
350 ctl->server.authenticate = A_KERBEROS_V5;
351 else if (strcmp(optarg, "kerberos_v4") == 0)
352 ctl->server.authenticate = A_KERBEROS_V4;
353 else if (strcmp(optarg, "ssh") == 0)
354 ctl->server.authenticate = A_SSH;
355 else if (strcmp(optarg, "otp") == 0)
356 ctl->server.authenticate = A_OTP;
357 else if (strcmp(optarg, "opie") == 0)
358 ctl->server.authenticate = A_OTP;
359 else if (strcmp(optarg, "ntlm") == 0)
360 ctl->server.authenticate = A_NTLM;
361 else if (strcmp(optarg, "cram") == 0)
362 ctl->server.authenticate = A_CRAM_MD5;
363 else if (strcmp(optarg, "cram-md5") == 0)
364 ctl->server.authenticate = A_CRAM_MD5;
365 else if (strcmp(optarg, "gssapi") == 0)
366 ctl->server.authenticate = A_GSSAPI;
367 else if (strcmp(optarg, "any") == 0)
368 ctl->server.authenticate = A_ANY;
369 else if (strcmp(optarg, "msn") == 0)
370 ctl->server.authenticate = A_MSN;
372 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
377 ctl->server.timeout = xatoi(optarg, &errflag);
378 if (ctl->server.timeout == 0)
379 ctl->server.timeout = -1;
382 ctl->server.envelope = xstrdup(optarg);
385 ctl->server.qvirtual = xstrdup(optarg);
389 ctl->remotename = xstrdup(optarg);
392 ctl->fetchall = FLAG_TRUE;
395 ctl->keep = FLAG_FALSE;
398 ctl->keep = FLAG_TRUE;
401 ctl->flush = FLAG_TRUE;
404 ctl->limitflush = FLAG_TRUE;
407 ctl->rewrite = FLAG_FALSE;
410 c = xatoi(optarg, &errflag);
411 ctl->limit = NUM_VALUE_IN(c);
414 buf = xstrdup(optarg);
415 cp = strtok(buf, ",");
417 save_str(&ctl->mailboxes, cp, 0);
419 ((cp = strtok((char *)NULL, ",")));
423 buf = xstrdup(optarg);
424 cp = strtok(buf, ",");
426 save_str(&ctl->smtphunt, cp, TRUE);
428 ((cp = strtok((char *)NULL, ",")));
432 case LA_FETCHDOMAINS:
433 buf = xstrdup(optarg);
434 cp = strtok(buf, ",");
436 save_str(&ctl->domainlist, cp, TRUE);
438 ((cp = strtok((char *)NULL, ",")));
442 ctl->smtpaddress = xstrdup(optarg);
445 ctl->smtpname = xstrdup(optarg);
448 buf = xstrdup(optarg);
449 cp = strtok(buf, ",");
451 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
453 idp->val.status.num = xatoi(cp, &errflag);
455 ((cp = strtok((char *)NULL, ",")));
459 c = xatoi(optarg, &errflag);
460 ctl->batchlimit = NUM_VALUE_IN(c);
463 c = xatoi(optarg, &errflag);
464 ctl->fetchlimit = NUM_VALUE_IN(c);
466 case LA_FETCHSIZELIMIT:
467 c = xatoi(optarg, &errflag);
468 ctl->fetchsizelimit = NUM_VALUE_IN(c);
471 c = xatoi(optarg, &errflag);
472 ctl->fastuidl = NUM_VALUE_IN(c);
475 c = xatoi(optarg, &errflag);
476 ctl->expunge = NUM_VALUE_IN(c);
479 ctl->mda = xstrdup(optarg);
483 ctl->bsmtp = prependdir (optarg, currentwd);
487 ctl->listener = LMTP_MODE;
492 interface_parse(optarg, &ctl->server);
495 ctl->server.monitor = xstrdup(optarg);
497 #endif /* CAN_MONITOR */
499 ctl->server.plugin = xstrdup(optarg);
502 ctl->server.plugout = xstrdup(optarg);
507 ctl->use_ssl = FLAG_TRUE;
511 ctl->sslkey = prependdir (optarg, currentwd);
515 ctl->sslcert = prependdir (optarg, currentwd);
519 ctl->sslproto = xstrdup(optarg);
523 ctl->sslcertck = FLAG_TRUE;
527 ctl->sslcertpath = prependdir(optarg, currentwd);
530 case LA_SSLFINGERPRINT:
531 ctl->sslfingerprint = xstrdup(optarg);
536 ctl->server.principal = xstrdup(optarg);
544 c = xatoi(optarg, &errflag);
545 ctl->warnings = NUM_VALUE_IN(c);
553 rctl->use_syslog = FLAG_TRUE;
557 rctl->use_syslog = FLAG_FALSE;
561 ctl->server.tracepolls = FLAG_TRUE;
570 if (errflag || ocount > 1 || helpflag) {
571 /* squawk if syntax errors were detected */
572 #define P(s) fputs(s, helpflag ? stdout : stderr)
573 P(GT_("usage: fetchmail [options] [server ...]\n"));
574 P(GT_(" Options are as follows:\n"));
575 P(GT_(" -?, --help display this option help\n"));
576 P(GT_(" -V, --version display version info\n"));
578 P(GT_(" -c, --check check for messages without fetching\n"));
579 P(GT_(" -s, --silent work silently\n"));
580 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
581 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
582 P(GT_(" -N, --nodetach don't detach daemon process\n"));
583 P(GT_(" -q, --quit kill daemon process\n"));
584 P(GT_(" -L, --logfile specify logfile name\n"));
585 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
586 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
587 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
588 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
589 P(GT_(" --postmaster specify recipient of last resort\n"));
590 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
592 P(GT_(" -I, --interface interface required specification\n"));
593 P(GT_(" -M, --monitor monitor interface for activity\n"));
595 #if defined( SSL_ENABLE )
596 P(GT_(" --ssl enable ssl encrypted session\n"));
597 P(GT_(" --sslkey ssl private key file\n"));
598 P(GT_(" --sslcert ssl client certificate\n"));
599 P(GT_(" --sslcertck do strict server certificate check (recommended)\n"));
600 P(GT_(" --sslcertpath path to ssl certificates\n"));
601 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\n"));
602 P(GT_(" --sslproto force ssl protocol (ssl2/ssl3/tls1)\n"));
604 P(GT_(" --plugin specify external command to open connection\n"));
605 P(GT_(" --plugout specify external command to open smtp connection\n"));
607 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
608 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
609 P(GT_(" --port TCP port to connect to (obsolete, use --service)\n"));
610 P(GT_(" -P, --service TCP service to connect to (can be numeric TCP port)\n"));
611 P(GT_(" --auth authentication type (password/kerberos/ssh/otp)\n"));
612 P(GT_(" -t, --timeout server nonresponse timeout\n"));
613 P(GT_(" -E, --envelope envelope address header\n"));
614 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
615 P(GT_(" --principal mail service principal\n"));
616 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
618 P(GT_(" -u, --username specify users's login on server\n"));
619 P(GT_(" -a, --all retrieve old and new messages\n"));
620 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
621 P(GT_(" -k, --keep save new messages after retrieval\n"));
622 P(GT_(" -F, --flush delete old messages from server\n"));
623 P(GT_(" --limitflush delete oversized messages\n"));
624 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
625 P(GT_(" -l, --limit don't fetch messages over given size\n"));
626 P(GT_(" -w, --warnings interval between warning mail notification\n"));
628 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
629 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
630 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
631 P(GT_(" --smtpname set SMTP full name username@domain\n"));
632 P(GT_(" -Z, --antispam, set antispam response values\n"));
633 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
634 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
635 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
636 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
637 P(GT_(" -e, --expunge set max deletions between expunges\n"));
638 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
639 P(GT_(" --bsmtp set output BSMTP file\n"));
640 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
641 P(GT_(" -r, --folder specify remote folder name\n"));
642 P(GT_(" --showdots show progress dots even in logfiles\n"));
645 * --configdump (internal use by fetchmailconf, dumps
646 * configuration as Python source code)
647 * --yydebug (developer use, enables parser debugging) */
658 /* options.c ends here */