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
69 #define LA_INTERFACE 46
71 #define LA_CONFIGDUMP 48
73 #define LA_SMTPNAME 50
74 #define LA_SHOWDOTS 51
75 #define LA_PRINCIPAL 52
76 #define LA_TRACEPOLLS 53
82 #define LA_SSLPROTO 57
83 #define LA_SSLCERTCK 58
84 #define LA_SSLCERTPATH 59
85 #define LA_SSLFINGERPRINT 60
88 #define LA_FETCHSIZELIMIT 61
89 #define LA_FASTUIDL 62
91 /* options still left: CDgGhHjJoORwWxXYz */
92 static const char *shortoptions =
93 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:D:";
95 static const struct option longoptions[] = {
96 /* this can be const because all flag fields are 0 and will never get set */
97 {"help", no_argument, (int *) 0, LA_HELP },
98 {"version", no_argument, (int *) 0, LA_VERSION },
99 {"check", no_argument, (int *) 0, LA_CHECK },
100 {"silent", no_argument, (int *) 0, LA_SILENT },
101 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
102 {"daemon", required_argument, (int *) 0, LA_DAEMON },
103 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
104 {"quit", no_argument, (int *) 0, LA_QUIT },
105 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
106 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
107 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
108 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
109 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
110 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
111 {"idfile", required_argument, (int *) 0, LA_IDFILE },
112 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
113 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
115 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
116 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
117 {"uidl", no_argument, (int *) 0, LA_UIDL },
118 {"port", required_argument, (int *) 0, LA_PORT },
119 {"auth", required_argument, (int *) 0, LA_AUTH},
120 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
121 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
122 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
124 {"user", required_argument, (int *) 0, LA_USERNAME },
125 {"username", required_argument, (int *) 0, LA_USERNAME },
127 {"all", no_argument, (int *) 0, LA_ALL },
128 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
129 {"keep", no_argument, (int *) 0, LA_KEEP },
130 {"flush", no_argument, (int *) 0, LA_FLUSH },
131 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
132 {"limit", required_argument, (int *) 0, LA_LIMIT },
133 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
135 {"folder", required_argument, (int *) 0, LA_FOLDER },
136 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
137 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
138 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
139 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
140 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
142 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
143 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
144 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
145 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
146 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
147 {"mda", required_argument, (int *) 0, LA_MDA },
148 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
149 {"lmtp", no_argument, (int *) 0, LA_LMTP },
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) && !defined(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, GT_("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, GT_("Value of string '%s' is %s than %d.\n"), s,
206 (value < 0) ? GT_("smaller"): GT_("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, GT_("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 = prependdir (optarg, currentwd);
318 rctl->invisible = TRUE;
321 rctl->showdots = FLAG_TRUE;
325 rcfile = prependdir (optarg, currentwd);
329 rctl->idfile = prependdir (optarg, currentwd);
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,GT_("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, "opie") == 0)
412 ctl->server.authenticate = A_OTP;
413 else if (strcmp(optarg, "ntlm") == 0)
414 ctl->server.authenticate = A_NTLM;
415 else if (strcmp(optarg, "cram") == 0)
416 ctl->server.authenticate = A_CRAM_MD5;
417 else if (strcmp(optarg, "cram-md5") == 0)
418 ctl->server.authenticate = A_CRAM_MD5;
419 else if (strcmp(optarg, "gssapi") == 0)
420 ctl->server.authenticate = A_GSSAPI;
421 else if (strcmp(optarg, "any") == 0)
422 ctl->server.authenticate = A_ANY;
424 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
430 ctl->server.timeout = xatoi(optarg, &errflag);
431 if (ctl->server.timeout == 0)
432 ctl->server.timeout = -1;
436 ctl->server.envelope = xstrdup(optarg);
440 ctl->server.qvirtual = xstrdup(optarg);
445 ctl->remotename = xstrdup(optarg);
449 ctl->fetchall = FLAG_TRUE;
453 ctl->keep = FLAG_FALSE;
457 ctl->keep = FLAG_TRUE;
461 ctl->flush = FLAG_TRUE;
465 ctl->rewrite = FLAG_FALSE;
469 c = xatoi(optarg, &errflag);
470 ctl->limit = NUM_VALUE_IN(c);
474 xalloca(buf, char *, strlen(optarg) + 1);
476 cp = strtok(buf, ",");
478 save_str(&ctl->mailboxes, cp, 0);
480 ((cp = strtok((char *)NULL, ",")));
484 xalloca(buf, char *, strlen(optarg) + 1);
486 cp = strtok(buf, ",");
488 save_str(&ctl->smtphunt, cp, TRUE);
490 ((cp = strtok((char *)NULL, ",")));
493 case LA_FETCHDOMAINS:
494 xalloca(buf, char *, strlen(optarg) + 1);
496 cp = strtok(buf, ",");
498 save_str(&ctl->domainlist, cp, TRUE);
500 ((cp = strtok((char *)NULL, ",")));
504 ctl->smtpaddress = xstrdup(optarg);
507 ctl->smtpname = xstrdup(optarg);
511 xalloca(buf, char *, strlen(optarg) + 1);
513 cp = strtok(buf, ",");
515 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
517 idp->val.status.num = xatoi(cp, &errflag);
519 ((cp = strtok((char *)NULL, ",")));
523 c = xatoi(optarg, &errflag);
524 ctl->batchlimit = NUM_VALUE_IN(c);
528 c = xatoi(optarg, &errflag);
529 ctl->fetchlimit = NUM_VALUE_IN(c);
531 case LA_FETCHSIZELIMIT:
532 c = xatoi(optarg, &errflag);
533 ctl->fetchsizelimit = NUM_VALUE_IN(c);
536 c = xatoi(optarg, &errflag);
537 ctl->fastuidl = NUM_VALUE_IN(c);
541 c = xatoi(optarg, &errflag);
542 ctl->expunge = NUM_VALUE_IN(c);
546 ctl->mda = xstrdup(optarg);
550 ctl->bsmtp = prependdir (optarg, currentwd);
554 ctl->listener = LMTP_MODE;
557 #if (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__)
560 interface_parse(optarg, &ctl->server);
564 ctl->server.monitor = xstrdup(optarg);
566 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
568 ctl->server.plugin = xstrdup(optarg);
571 ctl->server.plugout = xstrdup(optarg);
576 ctl->use_ssl = FLAG_TRUE;
580 ctl->sslkey = prependdir (optarg, currentwd);
584 ctl->sslcert = prependdir (optarg, currentwd);
588 ctl->sslproto = xstrdup(optarg);
592 ctl->sslcertck = FLAG_TRUE;
596 ctl->sslcertpath = prependdir(optarg, currentwd);
599 case LA_SSLFINGERPRINT:
600 ctl->sslfingerprint = xstrdup(optarg);
605 ctl->server.principal = xstrdup(optarg);
615 c = xatoi(optarg, &errflag);
616 ctl->warnings = NUM_VALUE_IN(c);
624 rctl->use_syslog = FLAG_TRUE;
628 rctl->use_syslog = FLAG_FALSE;
632 ctl->server.tracepolls = FLAG_TRUE;
642 if (errflag || ocount > 1 || helpflag) {
643 /* squawk if syntax errors were detected */
644 #define P(s) fputs(s, helpflag ? stdout : stderr)
645 P(GT_("usage: fetchmail [options] [server ...]\n"));
646 P(GT_(" Options are as follows:\n"));
647 P(GT_(" -?, --help display this option help\n"));
648 P(GT_(" -V, --version display version info\n"));
650 P(GT_(" -c, --check check for messages without fetching\n"));
651 P(GT_(" -s, --silent work silently\n"));
652 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
653 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
654 P(GT_(" -N, --nodetach don't detach daemon process\n"));
655 P(GT_(" -q, --quit kill daemon process\n"));
656 P(GT_(" -L, --logfile specify logfile name\n"));
657 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
658 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
659 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
660 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
661 P(GT_(" --postmaster specify recipient of last resort\n"));
662 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
663 #if (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__)
664 P(GT_(" -I, --interface interface required specification\n"));
665 P(GT_(" -M, --monitor monitor interface for activity\n"));
667 #if defined( SSL_ENABLE )
668 P(GT_(" --ssl enable ssl encrypted session\n"));
669 P(GT_(" --sslkey ssl private key file\n"));
670 P(GT_(" --sslcert ssl client certificate\n"));
671 P(GT_(" --sslcertpath path to ssl certificates\n"));
672 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\n"));
673 P(GT_(" --sslproto force ssl protocol (ssl2/ssl3/tls1)\n"));
675 P(GT_(" --plugin specify external command to open connection\n"));
676 P(GT_(" --plugout specify external command to open smtp connection\n"));
678 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
679 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
680 P(GT_(" -P, --port TCP/IP service port to connect to\n"));
681 P(GT_(" --auth authentication type (password/kerberos/ssh/otp)\n"));
682 P(GT_(" -t, --timeout server nonresponse timeout\n"));
683 P(GT_(" -E, --envelope envelope address header\n"));
684 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
685 P(GT_(" --principal mail service principal\n"));
686 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
688 P(GT_(" -u, --username specify users's login on server\n"));
689 P(GT_(" -a, --all retrieve old and new messages\n"));
690 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
691 P(GT_(" -k, --keep save new messages after retrieval\n"));
692 P(GT_(" -F, --flush delete old messages from server\n"));
693 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
694 P(GT_(" -l, --limit don't fetch messages over given size\n"));
695 P(GT_(" -w, --warnings interval between warning mail notification\n"));
697 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
698 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
699 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
700 P(GT_(" --smtpname set SMTP full name username@domain\n"));
701 P(GT_(" -Z, --antispam, set antispam response values\n"));
702 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
703 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
704 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
705 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
706 P(GT_(" -e, --expunge set max deletions between expunges\n"));
707 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
708 P(GT_(" --bsmtp set output BSMTP file\n"));
709 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
710 P(GT_(" -r, --folder specify remote folder name\n"));
711 P(GT_(" --showdots show progress dots even in logfiles\n"));
723 /* options.c ends here */