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
82 #define LA_SHOWDOTS 54
83 #define LA_PRINCIPAL 55
85 /* options still left: CDgGhHjJoORwWxXYz */
86 static const char *shortoptions =
87 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:";
89 static const struct option longoptions[] = {
90 /* this can be const because all flag fields are 0 and will never get set */
91 {"help", no_argument, (int *) 0, LA_HELP },
92 {"version", no_argument, (int *) 0, LA_VERSION },
93 {"check", no_argument, (int *) 0, LA_CHECK },
94 {"silent", no_argument, (int *) 0, LA_SILENT },
95 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
96 {"daemon", required_argument, (int *) 0, LA_DAEMON },
97 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
98 {"quit", no_argument, (int *) 0, LA_QUIT },
99 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
100 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
101 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
102 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
103 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
104 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
105 {"idfile", required_argument, (int *) 0, LA_IDFILE },
106 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
107 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
109 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
110 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
111 {"uidl", no_argument, (int *) 0, LA_UIDL },
112 {"port", required_argument, (int *) 0, LA_PORT },
113 {"auth", required_argument, (int *) 0, LA_AUTH},
114 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
115 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
116 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
118 {"user", required_argument, (int *) 0, LA_USERNAME },
119 {"username", required_argument, (int *) 0, LA_USERNAME },
121 {"all", no_argument, (int *) 0, LA_ALL },
122 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
123 {"keep", no_argument, (int *) 0, LA_KEEP },
124 {"flush", no_argument, (int *) 0, LA_FLUSH },
125 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
126 {"limit", required_argument, (int *) 0, LA_LIMIT },
127 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
129 {"folder", required_argument, (int *) 0, LA_FOLDER },
130 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
131 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
132 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
133 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
135 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
136 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
137 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
138 {"mda", required_argument, (int *) 0, LA_MDA },
139 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
140 {"lmtp", no_argument, (int *) 0, LA_LMTP },
143 {"netsec", required_argument, (int *) 0, LA_NETSEC },
144 #endif /* INET6_ENABLE */
147 {"ssl", no_argument, (int *) 0, LA_SSL },
148 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
149 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
150 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
153 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
155 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
156 {"interface", required_argument, (int *) 0, LA_INTERFACE },
157 {"monitor", required_argument, (int *) 0, LA_MONITOR },
158 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
159 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
160 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
162 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
164 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
166 {(char *) 0, no_argument, (int *) 0, 0 }
169 static int xatoi(char *s, int *errflagptr)
170 /* do safe conversion from string to number */
172 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
173 /* parse and convert numbers, but also check for invalid characters in
182 value = strtol(s, &endptr, 0);
184 /* any invalid chars in string? */
185 if ( (endptr == s) || (*endptr != '\0') ) {
186 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
191 /* is the range valid? */
192 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
193 (value > INT_MAX) || (value < INT_MIN)) {
195 (void) fprintf(stderr, _("Value of string '%s' is %s than %d.\n"), s,
196 (value < 0) ? _("smaller"): _("larger"),
197 (value < 0) ? INT_MIN : INT_MAX);
202 return (int) value; /* shut up, I know what I'm doing */
206 # if defined (STDC_HEADERS)
212 /* We do only base 10 conversions here (atoi)! */
215 /* check for leading white spaces */
216 for (i = 0; (i < len) && isspace(s[i]); i++)
222 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
224 /* skip over digits */
225 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
228 /* check for trailing garbage */
230 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
235 /* atoi should be safe by now, except for number range over/underflow */
240 int parsecmdline (argc, argv, rctl, ctl)
241 /* parse and validate the command line options */
242 int argc; /* argument count */
243 char **argv; /* argument strings */
244 struct runctl *rctl; /* global run controls to modify */
245 struct query *ctl; /* option record to be initialized */
248 * return value: if positive, argv index of last parsed option + 1
249 * (presumes one or more server names follows). if zero, the
250 * command line switches are such that no server names are
251 * required (e.g. --version). if negative, the command line is
252 * has one or more syntax errors.
256 int ocount = 0; /* count of destinations specified */
257 int errflag = 0; /* TRUE when a syntax error is detected */
258 int helpflag = 0; /* TRUE when option help was explicitly requested */
262 rctl->poll_interval = -1;
264 memset(ctl, '\0', sizeof(struct query)); /* start clean */
265 ctl->smtp_socket = -1;
268 (c = getopt_long(argc,argv,shortoptions,
269 longoptions,&option_index)) != -1) {
286 if (outlevel == O_VERBOSE)
289 outlevel = O_VERBOSE;
293 rctl->poll_interval = xatoi(optarg, &errflag);
305 rctl->logfile = optarg;
308 rctl->invisible = TRUE;
311 rctl->showdots = TRUE;
315 rcfile = (char *) xstrdup(optarg);
319 rctl->idfile = (char *) xstrdup(optarg);
322 rctl->postmaster = (char *) xstrdup(optarg);
325 run.bouncemail = FALSE;
329 /* XXX -- should probably use a table lookup here */
330 if (strcasecmp(optarg,"auto") == 0)
331 ctl->server.protocol = P_AUTO;
332 else if (strcasecmp(optarg,"pop2") == 0)
333 ctl->server.protocol = P_POP2;
335 else if (strcasecmp(optarg,"sdps") == 0)
337 ctl->server.protocol = P_POP3;
338 ctl->server.sdps = TRUE;
340 #endif /* SDPS_ENABLE */
341 else if (strcasecmp(optarg,"pop3") == 0)
342 ctl->server.protocol = P_POP3;
343 else if (strcasecmp(optarg,"apop") == 0)
344 ctl->server.protocol = P_APOP;
345 else if (strcasecmp(optarg,"rpop") == 0)
346 ctl->server.protocol = P_RPOP;
347 else if (strcasecmp(optarg,"kpop") == 0)
349 ctl->server.protocol = P_POP3;
351 ctl->server.service = KPOP_PORT;
352 #else /* INET6_ENABLE */
353 ctl->server.port = KPOP_PORT;
354 #endif /* INET6_ENABLE */
356 ctl->server.authenticate = A_KERBEROS_V5;
358 ctl->server.authenticate = A_KERBEROS_V4;
359 #endif /* KERBEROS_V5 */
361 else if (strcasecmp(optarg,"imap") == 0)
362 ctl->server.protocol = P_IMAP;
363 else if (strcasecmp(optarg,"etrn") == 0)
364 ctl->server.protocol = P_ETRN;
366 fprintf(stderr,_("Invalid protocol `%s' specified.\n"), optarg);
372 ctl->server.uidl = FLAG_TRUE;
377 ctl->server.service = optarg;
378 #else /* INET6_ENABLE */
379 ctl->server.port = xatoi(optarg, &errflag);
380 #endif /* INET6_ENABLE */
383 if (strcmp(optarg, "password") == 0)
384 ctl->server.authenticate = A_PASSWORD;
385 else if (strcmp(optarg, "kerberos") == 0)
387 ctl->server.authenticate = A_KERBEROS_V5;
389 ctl->server.authenticate = A_KERBEROS_V4;
390 #endif /* KERBEROS_V5 */
391 else if (strcmp(optarg, "kerberos_v5") == 0)
392 ctl->server.authenticate = A_KERBEROS_V5;
393 else if (strcmp(optarg, "kerberos_v4") == 0)
394 ctl->server.authenticate = A_KERBEROS_V4;
395 else if (strcmp(optarg, "ssh") == 0)
396 ctl->server.authenticate = A_SSH;
398 fprintf(stderr,_("Invalid authentication `%s' specified.\n"), optarg);
404 ctl->server.timeout = xatoi(optarg, &errflag);
405 if (ctl->server.timeout == 0)
406 ctl->server.timeout = -1;
410 ctl->server.envelope = xstrdup(optarg);
414 ctl->server.qvirtual = xstrdup(optarg);
419 ctl->remotename = xstrdup(optarg);
423 ctl->fetchall = FLAG_TRUE;
427 ctl->keep = FLAG_FALSE;
431 ctl->keep = FLAG_TRUE;
435 ctl->flush = FLAG_TRUE;
439 ctl->rewrite = FLAG_FALSE;
443 c = xatoi(optarg, &errflag);
444 ctl->limit = NUM_VALUE_IN(c);
448 xalloca(buf, char *, strlen(optarg) + 1);
450 cp = strtok(buf, ",");
452 save_str(&ctl->mailboxes, cp, 0);
454 ((cp = strtok((char *)NULL, ",")));
458 xalloca(buf, char *, strlen(optarg) + 1);
460 cp = strtok(buf, ",");
462 save_str(&ctl->smtphunt, cp, TRUE);
464 ((cp = strtok((char *)NULL, ",")));
469 ctl->smtpaddress = xstrdup(optarg);
472 ctl->smtpname = xstrdup(optarg);
476 xalloca(buf, char *, strlen(optarg) + 1);
478 cp = strtok(buf, ",");
480 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
482 idp->val.status.num = xatoi(cp, &errflag);
484 ((cp = strtok((char *)NULL, ",")));
488 c = xatoi(optarg, &errflag);
489 ctl->batchlimit = NUM_VALUE_IN(c);
493 c = xatoi(optarg, &errflag);
494 ctl->fetchlimit = NUM_VALUE_IN(c);
498 c = xatoi(optarg, &errflag);
499 ctl->expunge = NUM_VALUE_IN(c);
503 ctl->mda = xstrdup(optarg);
507 ctl->bsmtp = xstrdup(optarg);
511 ctl->listener = LMTP_MODE;
517 ctl->server.netsec = (void *)optarg;
519 fprintf(stderr, _("fetchmail: network security support is disabled\n"));
521 #endif /* NET_SECURITY */
524 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
527 interface_parse(optarg, &ctl->server);
531 ctl->server.monitor = xstrdup(optarg);
533 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
535 ctl->server.plugin = xstrdup(optarg);
538 ctl->server.plugout = xstrdup(optarg);
543 ctl->use_ssl = FLAG_TRUE;
547 ctl->sslkey = xstrdup(optarg);
551 ctl->sslcert = xstrdup(optarg);
555 ctl->sslproto = xstrdup(optarg);
560 ctl->server.principal = xstrdup(optarg);
570 c = xatoi(optarg, &errflag);
571 ctl->warnings = NUM_VALUE_IN(c);
579 rctl->use_syslog = FLAG_TRUE;
583 rctl->use_syslog = FLAG_FALSE;
593 if (errflag || ocount > 1 || helpflag) {
594 /* squawk if syntax errors were detected */
595 #define P(s) fputs(s, helpflag ? stdout : stderr)
596 P(_("usage: fetchmail [options] [server ...]\n"));
597 P(_(" Options are as follows:\n"));
598 P(_(" -?, --help display this option help\n"));
599 P(_(" -V, --version display version info\n"));
601 P(_(" -c, --check check for messages without fetching\n"));
602 P(_(" -s, --silent work silently\n"));
603 P(_(" -v, --verbose work noisily (diagnostic output)\n"));
604 P(_(" -d, --daemon run as a daemon once per n seconds\n"));
605 P(_(" -N, --nodetach don't detach daemon process\n"));
606 P(_(" -q, --quit kill daemon process\n"));
607 P(_(" -L, --logfile specify logfile name\n"));
608 P(_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
609 P(_(" --invisible don't write Received & enable host spoofing\n"));
610 P(_(" -f, --fetchmailrc specify alternate run control file\n"));
611 P(_(" -i, --idfile specify alternate UIDs file\n"));
612 P(_(" --postmaster specify recipient of last resort\n"));
613 P(_(" --nobounce redirect bounces from user to postmaster.\n"));
614 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
615 P(_(" -I, --interface interface required specification\n"));
616 P(_(" -M, --monitor monitor interface for activity\n"));
618 #if defined( SSL_ENABLE )
619 P(_(" --ssl enable ssl encrypted session\n"));
620 P(_(" --sslkey ssl private key file\n"));
621 P(_(" --sslcert ssl client certificate\n"));
622 P(_(" --sslproto force ssl protocol (ssl2/ssl3/tls1)\n"));
624 P(_(" --plugin specify external command to open connection\n"));
625 P(_(" --plugout specify external command to open smtp connection\n"));
627 P(_(" -p, --protocol specify retrieval protocol (see man page)\n"));
628 P(_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
629 P(_(" -P, --port TCP/IP service port to connect to\n"));
630 P(_(" --auth authentication type (password/kerberos/ssh)\n"));
631 P(_(" -t, --timeout server nonresponse timeout\n"));
632 P(_(" -E, --envelope envelope address header\n"));
633 P(_(" -Q, --qvirtual prefix to remove from local user id\n"));
634 P(_(" --principal mail service principal\n"));
636 P(_(" -u, --username specify users's login on server\n"));
637 P(_(" -a, --all retrieve old and new messages\n"));
638 P(_(" -K, --nokeep delete new messages after retrieval\n"));
639 P(_(" -k, --keep save new messages after retrieval\n"));
640 P(_(" -F, --flush delete old messages from server\n"));
641 P(_(" -n, --norewrite don't rewrite header addresses\n"));
642 P(_(" -l, --limit don't fetch messages over given size\n"));
643 P(_(" -w, --warnings interval between warning mail notification\n"));
646 P(_(" -T, --netsec set IP security request\n"));
647 #endif /* NET_SECURITY */
648 P(_(" -S, --smtphost set SMTP forwarding host\n"));
649 P(_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
650 P(_(" --smtpname set SMTP full name username@domain\n"));
651 P(_(" -Z, --antispam, set antispam response values\n"));
652 P(_(" -b, --batchlimit set batch limit for SMTP connections\n"));
653 P(_(" -B, --fetchlimit set fetch limit for server connections\n"));
654 P(_(" -e, --expunge set max deletions between expunges\n"));
655 P(_(" -m, --mda set MDA to use for forwarding\n"));
656 P(_(" --bsmtp set output BSMTP file\n"));
657 P(_(" --lmtp use LMTP (RFC2033) for delivery\n"));
658 P(_(" -r, --folder specify remote folder name\n"));
659 P(_(" --showdots show progress dots even in logfiles\n"));
671 /* options.c ends here */