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_FETCHDOMAINS 34
59 #define LA_SMTPADDR 35
60 #define LA_ANTISPAM 36
61 #define LA_BATCHLIMIT 37
62 #define LA_FETCHLIMIT 38
70 #define LA_INTERFACE 46
72 #define LA_CONFIGDUMP 48
74 #define LA_SMTPNAME 50
75 #define LA_SHOWDOTS 51
76 #define LA_PRINCIPAL 52
77 #define LA_TRACEPOLLS 53
83 #define LA_SSLPROTO 57
84 #define LA_SSLCERTCK 58
85 #define LA_SSLCERTPATH 59
86 #define LA_SSLFINGERPRINT 60
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:D:";
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 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
137 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
138 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
139 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
141 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
142 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
143 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
144 {"mda", required_argument, (int *) 0, LA_MDA },
145 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
146 {"lmtp", no_argument, (int *) 0, LA_LMTP },
149 {"netsec", required_argument, (int *) 0, LA_NETSEC },
150 #endif /* INET6_ENABLE */
153 {"ssl", no_argument, (int *) 0, LA_SSL },
154 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
155 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
156 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
157 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
158 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
159 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
162 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
164 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
165 {"interface", required_argument, (int *) 0, LA_INTERFACE },
166 {"monitor", required_argument, (int *) 0, LA_MONITOR },
167 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
168 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
169 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
171 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
173 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
175 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
177 {(char *) 0, no_argument, (int *) 0, 0 }
180 static int xatoi(char *s, int *errflagptr)
181 /* do safe conversion from string to number */
183 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
184 /* parse and convert numbers, but also check for invalid characters in
193 value = strtol(s, &endptr, 0);
195 /* any invalid chars in string? */
196 if ( (endptr == s) || (*endptr != '\0') ) {
197 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
202 /* is the range valid? */
203 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
204 (value > INT_MAX) || (value < INT_MIN)) {
206 (void) fprintf(stderr, GT_("Value of string '%s' is %s than %d.\n"), s,
207 (value < 0) ? GT_("smaller"): GT_("larger"),
208 (value < 0) ? INT_MIN : INT_MAX);
213 return (int) value; /* shut up, I know what I'm doing */
217 # if defined (STDC_HEADERS)
223 /* We do only base 10 conversions here (atoi)! */
226 /* check for leading white spaces */
227 for (i = 0; (i < len) && isspace(s[i]); i++)
233 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
235 /* skip over digits */
236 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
239 /* check for trailing garbage */
241 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
246 /* atoi should be safe by now, except for number range over/underflow */
251 int parsecmdline (argc, argv, rctl, ctl)
252 /* parse and validate the command line options */
253 int argc; /* argument count */
254 char **argv; /* argument strings */
255 struct runctl *rctl; /* global run controls to modify */
256 struct query *ctl; /* option record to be initialized */
259 * return value: if positive, argv index of last parsed option + 1
260 * (presumes one or more server names follows). if zero, the
261 * command line switches are such that no server names are
262 * required (e.g. --version). if negative, the command line is
263 * has one or more syntax errors.
267 int ocount = 0; /* count of destinations specified */
268 int errflag = 0; /* TRUE when a syntax error is detected */
269 int helpflag = 0; /* TRUE when option help was explicitly requested */
273 rctl->poll_interval = -1;
275 memset(ctl, '\0', sizeof(struct query)); /* start clean */
276 ctl->smtp_socket = -1;
279 #ifdef HAVE_GETOPTLONG
280 (c = getopt_long(argc,argv,shortoptions,
281 longoptions, &option_index)) != -1)
283 (c = getopt(argc, argv, shortoptions)) != -1)
301 if (outlevel == O_VERBOSE)
304 outlevel = O_VERBOSE;
308 rctl->poll_interval = xatoi(optarg, &errflag);
320 rctl->logfile = optarg;
323 rctl->invisible = TRUE;
326 rctl->showdots = TRUE;
330 rcfile = (char *) xstrdup(optarg);
334 rctl->idfile = (char *) xstrdup(optarg);
337 rctl->postmaster = (char *) xstrdup(optarg);
340 run.bouncemail = FALSE;
344 /* XXX -- should probably use a table lookup here */
345 if (strcasecmp(optarg,"auto") == 0)
346 ctl->server.protocol = P_AUTO;
347 else if (strcasecmp(optarg,"pop2") == 0)
348 ctl->server.protocol = P_POP2;
350 else if (strcasecmp(optarg,"sdps") == 0)
352 ctl->server.protocol = P_POP3;
353 ctl->server.sdps = TRUE;
355 #endif /* SDPS_ENABLE */
356 else if (strcasecmp(optarg,"pop3") == 0)
357 ctl->server.protocol = P_POP3;
358 else if (strcasecmp(optarg,"apop") == 0)
359 ctl->server.protocol = P_APOP;
360 else if (strcasecmp(optarg,"rpop") == 0)
361 ctl->server.protocol = P_RPOP;
362 else if (strcasecmp(optarg,"kpop") == 0)
364 ctl->server.protocol = P_POP3;
366 ctl->server.service = KPOP_PORT;
367 #else /* INET6_ENABLE */
368 ctl->server.port = KPOP_PORT;
369 #endif /* INET6_ENABLE */
371 ctl->server.authenticate = A_KERBEROS_V5;
373 ctl->server.authenticate = A_KERBEROS_V4;
374 #endif /* KERBEROS_V5 */
376 else if (strcasecmp(optarg,"imap") == 0)
377 ctl->server.protocol = P_IMAP;
378 else if (strcasecmp(optarg,"etrn") == 0)
379 ctl->server.protocol = P_ETRN;
380 else if (strcasecmp(optarg,"odmr") == 0)
381 ctl->server.protocol = P_ODMR;
383 fprintf(stderr,GT_("Invalid protocol `%s' specified.\n"), optarg);
389 ctl->server.uidl = FLAG_TRUE;
394 ctl->server.service = optarg;
395 #else /* INET6_ENABLE */
396 ctl->server.port = xatoi(optarg, &errflag);
397 #endif /* INET6_ENABLE */
400 if (strcmp(optarg, "password") == 0)
401 ctl->server.authenticate = A_PASSWORD;
402 else if (strcmp(optarg, "kerberos") == 0)
404 ctl->server.authenticate = A_KERBEROS_V5;
406 ctl->server.authenticate = A_KERBEROS_V4;
407 #endif /* KERBEROS_V5 */
408 else if (strcmp(optarg, "kerberos_v5") == 0)
409 ctl->server.authenticate = A_KERBEROS_V5;
410 else if (strcmp(optarg, "kerberos_v4") == 0)
411 ctl->server.authenticate = A_KERBEROS_V4;
412 else if (strcmp(optarg, "ssh") == 0)
413 ctl->server.authenticate = A_SSH;
414 else if (strcmp(optarg, "otp") == 0)
415 ctl->server.authenticate = A_OTP;
416 else if (strcmp(optarg, "ntlm") == 0)
417 ctl->server.authenticate = A_NTLM;
418 else if (strcmp(optarg, "cram") == 0)
419 ctl->server.authenticate = A_CRAM_MD5;
420 else if (strcmp(optarg, "cram-md5") == 0)
421 ctl->server.authenticate = A_CRAM_MD5;
422 else if (strcmp(optarg, "gssapi") == 0)
423 ctl->server.authenticate = A_GSSAPI;
424 else if (strcmp(optarg, "any") == 0)
425 ctl->server.authenticate = A_ANY;
427 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
433 ctl->server.timeout = xatoi(optarg, &errflag);
434 if (ctl->server.timeout == 0)
435 ctl->server.timeout = -1;
439 ctl->server.envelope = xstrdup(optarg);
443 ctl->server.qvirtual = xstrdup(optarg);
448 ctl->remotename = xstrdup(optarg);
452 ctl->fetchall = FLAG_TRUE;
456 ctl->keep = FLAG_FALSE;
460 ctl->keep = FLAG_TRUE;
464 ctl->flush = FLAG_TRUE;
468 ctl->rewrite = FLAG_FALSE;
472 c = xatoi(optarg, &errflag);
473 ctl->limit = NUM_VALUE_IN(c);
477 xalloca(buf, char *, strlen(optarg) + 1);
479 cp = strtok(buf, ",");
481 save_str(&ctl->mailboxes, cp, 0);
483 ((cp = strtok((char *)NULL, ",")));
487 xalloca(buf, char *, strlen(optarg) + 1);
489 cp = strtok(buf, ",");
491 save_str(&ctl->smtphunt, cp, TRUE);
493 ((cp = strtok((char *)NULL, ",")));
496 case LA_FETCHDOMAINS:
497 xalloca(buf, char *, strlen(optarg) + 1);
499 cp = strtok(buf, ",");
501 save_str(&ctl->domainlist, cp, TRUE);
503 ((cp = strtok((char *)NULL, ",")));
507 ctl->smtpaddress = xstrdup(optarg);
510 ctl->smtpname = xstrdup(optarg);
514 xalloca(buf, char *, strlen(optarg) + 1);
516 cp = strtok(buf, ",");
518 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
520 idp->val.status.num = xatoi(cp, &errflag);
522 ((cp = strtok((char *)NULL, ",")));
526 c = xatoi(optarg, &errflag);
527 ctl->batchlimit = NUM_VALUE_IN(c);
531 c = xatoi(optarg, &errflag);
532 ctl->fetchlimit = NUM_VALUE_IN(c);
536 c = xatoi(optarg, &errflag);
537 ctl->expunge = NUM_VALUE_IN(c);
541 ctl->mda = xstrdup(optarg);
545 ctl->bsmtp = xstrdup(optarg);
549 ctl->listener = LMTP_MODE;
555 ctl->server.netsec = (void *)optarg;
557 fprintf(stderr, GT_("fetchmail: network security support is disabled\n"));
559 #endif /* NET_SECURITY */
562 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
565 interface_parse(optarg, &ctl->server);
569 ctl->server.monitor = xstrdup(optarg);
571 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
573 ctl->server.plugin = xstrdup(optarg);
576 ctl->server.plugout = xstrdup(optarg);
581 ctl->use_ssl = FLAG_TRUE;
585 ctl->sslkey = xstrdup(optarg);
589 ctl->sslcert = xstrdup(optarg);
593 ctl->sslproto = xstrdup(optarg);
597 ctl->sslcertck = FLAG_TRUE;
601 ctl->sslcertpath = xstrdup(optarg);
604 case LA_SSLFINGERPRINT:
605 ctl->sslfingerprint = xstrdup(optarg);
610 ctl->server.principal = xstrdup(optarg);
620 c = xatoi(optarg, &errflag);
621 ctl->warnings = NUM_VALUE_IN(c);
629 rctl->use_syslog = FLAG_TRUE;
633 rctl->use_syslog = FLAG_FALSE;
637 ctl->tracepolls = FLAG_TRUE;
647 if (errflag || ocount > 1 || helpflag) {
648 /* squawk if syntax errors were detected */
649 #define P(s) fputs(s, helpflag ? stdout : stderr)
650 P(GT_("usage: fetchmail [options] [server ...]\n"));
651 P(GT_(" Options are as follows:\n"));
652 P(GT_(" -?, --help display this option help\n"));
653 P(GT_(" -V, --version display version info\n"));
655 P(GT_(" -c, --check check for messages without fetching\n"));
656 P(GT_(" -s, --silent work silently\n"));
657 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
658 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
659 P(GT_(" -N, --nodetach don't detach daemon process\n"));
660 P(GT_(" -q, --quit kill daemon process\n"));
661 P(GT_(" -L, --logfile specify logfile name\n"));
662 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
663 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
664 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
665 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
666 P(GT_(" --postmaster specify recipient of last resort\n"));
667 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
668 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
669 P(GT_(" -I, --interface interface required specification\n"));
670 P(GT_(" -M, --monitor monitor interface for activity\n"));
672 #if defined( SSL_ENABLE )
673 P(GT_(" --ssl enable ssl encrypted session\n"));
674 P(GT_(" --sslkey ssl private key file\n"));
675 P(GT_(" --sslcert ssl client certificate\n"));
676 P(GT_(" --sslproto force ssl protocol (ssl2/ssl3/tls1)\n"));
678 P(GT_(" --plugin specify external command to open connection\n"));
679 P(GT_(" --plugout specify external command to open smtp connection\n"));
681 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
682 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
683 P(GT_(" -P, --port TCP/IP service port to connect to\n"));
684 P(GT_(" --auth authentication type (password/kerberos/ssh)\n"));
685 P(GT_(" -t, --timeout server nonresponse timeout\n"));
686 P(GT_(" -E, --envelope envelope address header\n"));
687 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
688 P(GT_(" --principal mail service principal\n"));
689 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
691 P(GT_(" -u, --username specify users's login on server\n"));
692 P(GT_(" -a, --all retrieve old and new messages\n"));
693 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
694 P(GT_(" -k, --keep save new messages after retrieval\n"));
695 P(GT_(" -F, --flush delete old messages from server\n"));
696 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
697 P(GT_(" -l, --limit don't fetch messages over given size\n"));
698 P(GT_(" -w, --warnings interval between warning mail notification\n"));
701 P(GT_(" -T, --netsec set IP security request\n"));
702 #endif /* NET_SECURITY */
703 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
704 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
705 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
706 P(GT_(" --smtpname set SMTP full name username@domain\n"));
707 P(GT_(" -Z, --antispam, set antispam response values\n"));
708 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
709 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
710 P(GT_(" -e, --expunge set max deletions between expunges\n"));
711 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
712 P(GT_(" --bsmtp set output BSMTP file\n"));
713 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
714 P(GT_(" -r, --folder specify remote folder name\n"));
715 P(GT_(" --showdots show progress dots even in logfiles\n"));
727 /* options.c ends here */