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"
34 #define LA_INVISIBLE 10
36 #define LA_NOSYSLOG 12
39 #define LA_POSTMASTER 15
40 #define LA_NOBOUNCE 16
41 #define LA_PROTOCOL 17
46 #define LA_ENVELOPE 22
47 #define LA_QVIRTUAL 23
48 #define LA_USERNAME 24
53 #define LA_NOREWRITE 29
55 #define LA_WARNINGS 31
57 #define LA_SMTPHOST 33
58 #define LA_SMTPADDR 34
59 #define LA_ANTISPAM 35
60 #define LA_BATCHLIMIT 36
61 #define LA_FETCHLIMIT 37
69 #define LA_INTERFACE 45
71 #define LA_CONFIGDUMP 47
73 #define LA_SMTPNAME 49
79 #define LA_SSLPROTO 53
80 #define LA_SSLCERTCK 54
81 #define LA_SSLCERTPATH 55
82 #define LA_SSLFINGERPRINT 56
85 #define LA_SHOWDOTS 57
86 #define LA_PRINCIPAL 58
88 #define LA_TRACEPOLLS 59
90 /* options still left: CDgGhHjJoORwWxXYz */
91 static const char *shortoptions =
92 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:";
94 static const struct option longoptions[] = {
95 /* this can be const because all flag fields are 0 and will never get set */
96 {"help", no_argument, (int *) 0, LA_HELP },
97 {"version", no_argument, (int *) 0, LA_VERSION },
98 {"check", no_argument, (int *) 0, LA_CHECK },
99 {"silent", no_argument, (int *) 0, LA_SILENT },
100 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
101 {"daemon", required_argument, (int *) 0, LA_DAEMON },
102 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
103 {"quit", no_argument, (int *) 0, LA_QUIT },
104 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
105 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
106 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
107 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
108 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
109 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
110 {"idfile", required_argument, (int *) 0, LA_IDFILE },
111 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
112 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
114 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
115 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
116 {"uidl", no_argument, (int *) 0, LA_UIDL },
117 {"port", required_argument, (int *) 0, LA_PORT },
118 {"auth", required_argument, (int *) 0, LA_AUTH},
119 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
120 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
121 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
123 {"user", required_argument, (int *) 0, LA_USERNAME },
124 {"username", required_argument, (int *) 0, LA_USERNAME },
126 {"all", no_argument, (int *) 0, LA_ALL },
127 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
128 {"keep", no_argument, (int *) 0, LA_KEEP },
129 {"flush", no_argument, (int *) 0, LA_FLUSH },
130 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
131 {"limit", required_argument, (int *) 0, LA_LIMIT },
132 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
134 {"folder", required_argument, (int *) 0, LA_FOLDER },
135 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
136 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
137 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
138 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
140 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
141 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
142 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
143 {"mda", required_argument, (int *) 0, LA_MDA },
144 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
145 {"lmtp", no_argument, (int *) 0, LA_LMTP },
148 {"netsec", required_argument, (int *) 0, LA_NETSEC },
149 #endif /* INET6_ENABLE */
152 {"ssl", no_argument, (int *) 0, LA_SSL },
153 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
154 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
155 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
156 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
157 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
158 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
161 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
163 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
164 {"interface", required_argument, (int *) 0, LA_INTERFACE },
165 {"monitor", required_argument, (int *) 0, LA_MONITOR },
166 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
167 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
168 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
170 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
172 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
174 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
176 {(char *) 0, no_argument, (int *) 0, 0 }
179 static int xatoi(char *s, int *errflagptr)
180 /* do safe conversion from string to number */
182 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
183 /* parse and convert numbers, but also check for invalid characters in
192 value = strtol(s, &endptr, 0);
194 /* any invalid chars in string? */
195 if ( (endptr == s) || (*endptr != '\0') ) {
196 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
201 /* is the range valid? */
202 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
203 (value > INT_MAX) || (value < INT_MIN)) {
205 (void) fprintf(stderr, _("Value of string '%s' is %s than %d.\n"), s,
206 (value < 0) ? _("smaller"): _("larger"),
207 (value < 0) ? INT_MIN : INT_MAX);
212 return (int) value; /* shut up, I know what I'm doing */
216 # if defined (STDC_HEADERS)
222 /* We do only base 10 conversions here (atoi)! */
225 /* check for leading white spaces */
226 for (i = 0; (i < len) && isspace(s[i]); i++)
232 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
234 /* skip over digits */
235 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
238 /* check for trailing garbage */
240 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
245 /* atoi should be safe by now, except for number range over/underflow */
250 int parsecmdline (argc, argv, rctl, ctl)
251 /* parse and validate the command line options */
252 int argc; /* argument count */
253 char **argv; /* argument strings */
254 struct runctl *rctl; /* global run controls to modify */
255 struct query *ctl; /* option record to be initialized */
258 * return value: if positive, argv index of last parsed option + 1
259 * (presumes one or more server names follows). if zero, the
260 * command line switches are such that no server names are
261 * required (e.g. --version). if negative, the command line is
262 * has one or more syntax errors.
266 int ocount = 0; /* count of destinations specified */
267 int errflag = 0; /* TRUE when a syntax error is detected */
268 int helpflag = 0; /* TRUE when option help was explicitly requested */
272 rctl->poll_interval = -1;
274 memset(ctl, '\0', sizeof(struct query)); /* start clean */
275 ctl->smtp_socket = -1;
278 (c = getopt_long(argc,argv,shortoptions,
279 longoptions,&option_index)) != -1) {
296 if (outlevel == O_VERBOSE)
299 outlevel = O_VERBOSE;
303 rctl->poll_interval = xatoi(optarg, &errflag);
315 rctl->logfile = optarg;
318 rctl->invisible = TRUE;
321 rctl->showdots = TRUE;
325 rcfile = (char *) xstrdup(optarg);
329 rctl->idfile = (char *) xstrdup(optarg);
332 rctl->postmaster = (char *) xstrdup(optarg);
335 run.bouncemail = FALSE;
339 /* XXX -- should probably use a table lookup here */
340 if (strcasecmp(optarg,"auto") == 0)
341 ctl->server.protocol = P_AUTO;
342 else if (strcasecmp(optarg,"pop2") == 0)
343 ctl->server.protocol = P_POP2;
345 else if (strcasecmp(optarg,"sdps") == 0)
347 ctl->server.protocol = P_POP3;
348 ctl->server.sdps = TRUE;
350 #endif /* SDPS_ENABLE */
351 else if (strcasecmp(optarg,"pop3") == 0)
352 ctl->server.protocol = P_POP3;
353 else if (strcasecmp(optarg,"apop") == 0)
354 ctl->server.protocol = P_APOP;
355 else if (strcasecmp(optarg,"rpop") == 0)
356 ctl->server.protocol = P_RPOP;
357 else if (strcasecmp(optarg,"kpop") == 0)
359 ctl->server.protocol = P_POP3;
361 ctl->server.service = KPOP_PORT;
362 #else /* INET6_ENABLE */
363 ctl->server.port = KPOP_PORT;
364 #endif /* INET6_ENABLE */
366 ctl->server.authenticate = A_KERBEROS_V5;
368 ctl->server.authenticate = A_KERBEROS_V4;
369 #endif /* KERBEROS_V5 */
371 else if (strcasecmp(optarg,"imap") == 0)
372 ctl->server.protocol = P_IMAP;
373 else if (strcasecmp(optarg,"etrn") == 0)
374 ctl->server.protocol = P_ETRN;
375 else if (strcasecmp(optarg,"odmr") == 0)
376 ctl->server.protocol = P_ODMR;
378 fprintf(stderr,_("Invalid protocol `%s' specified.\n"), optarg);
384 ctl->server.uidl = FLAG_TRUE;
389 ctl->server.service = optarg;
390 #else /* INET6_ENABLE */
391 ctl->server.port = xatoi(optarg, &errflag);
392 #endif /* INET6_ENABLE */
395 if (strcmp(optarg, "password") == 0)
396 ctl->server.authenticate = A_PASSWORD;
397 else if (strcmp(optarg, "kerberos") == 0)
399 ctl->server.authenticate = A_KERBEROS_V5;
401 ctl->server.authenticate = A_KERBEROS_V4;
402 #endif /* KERBEROS_V5 */
403 else if (strcmp(optarg, "kerberos_v5") == 0)
404 ctl->server.authenticate = A_KERBEROS_V5;
405 else if (strcmp(optarg, "kerberos_v4") == 0)
406 ctl->server.authenticate = A_KERBEROS_V4;
407 else if (strcmp(optarg, "ssh") == 0)
408 ctl->server.authenticate = A_SSH;
409 else if (strcmp(optarg, "otp") == 0)
410 ctl->server.authenticate = A_OTP;
411 else if (strcmp(optarg, "ntlm") == 0)
412 ctl->server.authenticate = A_NTLM;
413 else if (strcmp(optarg, "cram") == 0)
414 ctl->server.authenticate = A_CRAM_MD5;
415 else if (strcmp(optarg, "cram-md5") == 0)
416 ctl->server.authenticate = A_CRAM_MD5;
417 else if (strcmp(optarg, "gssapi") == 0)
418 ctl->server.authenticate = A_GSSAPI;
419 else if (strcmp(optarg, "any") == 0)
420 ctl->server.authenticate = A_ANY;
422 fprintf(stderr,_("Invalid authentication `%s' specified.\n"), optarg);
428 ctl->server.timeout = xatoi(optarg, &errflag);
429 if (ctl->server.timeout == 0)
430 ctl->server.timeout = -1;
434 ctl->server.envelope = xstrdup(optarg);
438 ctl->server.qvirtual = xstrdup(optarg);
443 ctl->remotename = xstrdup(optarg);
447 ctl->fetchall = FLAG_TRUE;
451 ctl->keep = FLAG_FALSE;
455 ctl->keep = FLAG_TRUE;
459 ctl->flush = FLAG_TRUE;
463 ctl->rewrite = FLAG_FALSE;
467 c = xatoi(optarg, &errflag);
468 ctl->limit = NUM_VALUE_IN(c);
472 xalloca(buf, char *, strlen(optarg) + 1);
474 cp = strtok(buf, ",");
476 save_str(&ctl->mailboxes, cp, 0);
478 ((cp = strtok((char *)NULL, ",")));
482 xalloca(buf, char *, strlen(optarg) + 1);
484 cp = strtok(buf, ",");
486 save_str(&ctl->smtphunt, cp, TRUE);
488 ((cp = strtok((char *)NULL, ",")));
493 ctl->smtpaddress = xstrdup(optarg);
496 ctl->smtpname = xstrdup(optarg);
500 xalloca(buf, char *, strlen(optarg) + 1);
502 cp = strtok(buf, ",");
504 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
506 idp->val.status.num = xatoi(cp, &errflag);
508 ((cp = strtok((char *)NULL, ",")));
512 c = xatoi(optarg, &errflag);
513 ctl->batchlimit = NUM_VALUE_IN(c);
517 c = xatoi(optarg, &errflag);
518 ctl->fetchlimit = NUM_VALUE_IN(c);
522 c = xatoi(optarg, &errflag);
523 ctl->expunge = NUM_VALUE_IN(c);
527 ctl->mda = xstrdup(optarg);
531 ctl->bsmtp = xstrdup(optarg);
535 ctl->listener = LMTP_MODE;
541 ctl->server.netsec = (void *)optarg;
543 fprintf(stderr, _("fetchmail: network security support is disabled\n"));
545 #endif /* NET_SECURITY */
548 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
551 interface_parse(optarg, &ctl->server);
555 ctl->server.monitor = xstrdup(optarg);
557 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
559 ctl->server.plugin = xstrdup(optarg);
562 ctl->server.plugout = xstrdup(optarg);
567 ctl->use_ssl = FLAG_TRUE;
571 ctl->sslkey = xstrdup(optarg);
575 ctl->sslcert = xstrdup(optarg);
579 ctl->sslproto = xstrdup(optarg);
583 ctl->sslcertck = FLAG_TRUE;
587 ctl->sslcertpath = xstrdup(optarg);
590 case LA_SSLFINGERPRINT:
591 ctl->sslfingerprint = xstrdup(optarg);
596 ctl->server.principal = xstrdup(optarg);
606 c = xatoi(optarg, &errflag);
607 ctl->warnings = NUM_VALUE_IN(c);
615 rctl->use_syslog = FLAG_TRUE;
619 rctl->use_syslog = FLAG_FALSE;
623 ctl->tracepolls = FLAG_TRUE;
633 if (errflag || ocount > 1 || helpflag) {
634 /* squawk if syntax errors were detected */
635 #define P(s) fputs(s, helpflag ? stdout : stderr)
636 P(_("usage: fetchmail [options] [server ...]\n"));
637 P(_(" Options are as follows:\n"));
638 P(_(" -?, --help display this option help\n"));
639 P(_(" -V, --version display version info\n"));
641 P(_(" -c, --check check for messages without fetching\n"));
642 P(_(" -s, --silent work silently\n"));
643 P(_(" -v, --verbose work noisily (diagnostic output)\n"));
644 P(_(" -d, --daemon run as a daemon once per n seconds\n"));
645 P(_(" -N, --nodetach don't detach daemon process\n"));
646 P(_(" -q, --quit kill daemon process\n"));
647 P(_(" -L, --logfile specify logfile name\n"));
648 P(_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
649 P(_(" --invisible don't write Received & enable host spoofing\n"));
650 P(_(" -f, --fetchmailrc specify alternate run control file\n"));
651 P(_(" -i, --idfile specify alternate UIDs file\n"));
652 P(_(" --postmaster specify recipient of last resort\n"));
653 P(_(" --nobounce redirect bounces from user to postmaster.\n"));
654 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
655 P(_(" -I, --interface interface required specification\n"));
656 P(_(" -M, --monitor monitor interface for activity\n"));
658 #if defined( SSL_ENABLE )
659 P(_(" --ssl enable ssl encrypted session\n"));
660 P(_(" --sslkey ssl private key file\n"));
661 P(_(" --sslcert ssl client certificate\n"));
662 P(_(" --sslproto force ssl protocol (ssl2/ssl3/tls1)\n"));
664 P(_(" --plugin specify external command to open connection\n"));
665 P(_(" --plugout specify external command to open smtp connection\n"));
667 P(_(" -p, --protocol specify retrieval protocol (see man page)\n"));
668 P(_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
669 P(_(" -P, --port TCP/IP service port to connect to\n"));
670 P(_(" --auth authentication type (password/kerberos/ssh)\n"));
671 P(_(" -t, --timeout server nonresponse timeout\n"));
672 P(_(" -E, --envelope envelope address header\n"));
673 P(_(" -Q, --qvirtual prefix to remove from local user id\n"));
674 P(_(" --principal mail service principal\n"));
675 P(_(" --addaccthdr add an X-Fetchmail-Account header (\"label <user@host>\")\n"));
677 P(_(" -u, --username specify users's login on server\n"));
678 P(_(" -a, --all retrieve old and new messages\n"));
679 P(_(" -K, --nokeep delete new messages after retrieval\n"));
680 P(_(" -k, --keep save new messages after retrieval\n"));
681 P(_(" -F, --flush delete old messages from server\n"));
682 P(_(" -n, --norewrite don't rewrite header addresses\n"));
683 P(_(" -l, --limit don't fetch messages over given size\n"));
684 P(_(" -w, --warnings interval between warning mail notification\n"));
687 P(_(" -T, --netsec set IP security request\n"));
688 #endif /* NET_SECURITY */
689 P(_(" -S, --smtphost set SMTP forwarding host\n"));
690 P(_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
691 P(_(" --smtpname set SMTP full name username@domain\n"));
692 P(_(" -Z, --antispam, set antispam response values\n"));
693 P(_(" -b, --batchlimit set batch limit for SMTP connections\n"));
694 P(_(" -B, --fetchlimit set fetch limit for server connections\n"));
695 P(_(" -e, --expunge set max deletions between expunges\n"));
696 P(_(" -m, --mda set MDA to use for forwarding\n"));
697 P(_(" --bsmtp set output BSMTP file\n"));
698 P(_(" --lmtp use LMTP (RFC2033) for delivery\n"));
699 P(_(" -r, --folder specify remote folder name\n"));
700 P(_(" --showdots show progress dots even in logfiles\n"));
712 /* options.c ends here */