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
89 #define LA_FETCHSIZELIMIT 61
90 #define LA_FASTUIDL 62
92 /* options still left: CDgGhHjJoORwWxXYz */
93 static const char *shortoptions =
94 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:D:";
96 static const struct option longoptions[] = {
97 /* this can be const because all flag fields are 0 and will never get set */
98 {"help", no_argument, (int *) 0, LA_HELP },
99 {"version", no_argument, (int *) 0, LA_VERSION },
100 {"check", no_argument, (int *) 0, LA_CHECK },
101 {"silent", no_argument, (int *) 0, LA_SILENT },
102 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
103 {"daemon", required_argument, (int *) 0, LA_DAEMON },
104 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
105 {"quit", no_argument, (int *) 0, LA_QUIT },
106 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
107 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
108 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
109 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
110 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
111 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
112 {"idfile", required_argument, (int *) 0, LA_IDFILE },
113 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
114 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
116 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
117 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
118 {"uidl", no_argument, (int *) 0, LA_UIDL },
119 {"port", required_argument, (int *) 0, LA_PORT },
120 {"auth", required_argument, (int *) 0, LA_AUTH},
121 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
122 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
123 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
125 {"user", required_argument, (int *) 0, LA_USERNAME },
126 {"username", required_argument, (int *) 0, LA_USERNAME },
128 {"all", no_argument, (int *) 0, LA_ALL },
129 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
130 {"keep", no_argument, (int *) 0, LA_KEEP },
131 {"flush", no_argument, (int *) 0, LA_FLUSH },
132 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
133 {"limit", required_argument, (int *) 0, LA_LIMIT },
134 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
136 {"folder", required_argument, (int *) 0, LA_FOLDER },
137 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
138 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
139 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
140 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
141 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
143 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
144 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
145 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
146 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
147 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
148 {"mda", required_argument, (int *) 0, LA_MDA },
149 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
150 {"lmtp", no_argument, (int *) 0, LA_LMTP },
153 {"netsec", required_argument, (int *) 0, LA_NETSEC },
154 #endif /* INET6_ENABLE */
157 {"ssl", no_argument, (int *) 0, LA_SSL },
158 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
159 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
160 {"sslproto", required_argument, (int *) 0, LA_SSLPROTO },
161 {"sslcertck", no_argument, (int *) 0, LA_SSLCERTCK },
162 {"sslcertpath", required_argument, (int *) 0, LA_SSLCERTPATH },
163 {"sslfingerprint", required_argument, (int *) 0, LA_SSLFINGERPRINT },
166 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
168 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
169 {"interface", required_argument, (int *) 0, LA_INTERFACE },
170 {"monitor", required_argument, (int *) 0, LA_MONITOR },
171 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
172 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
173 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
175 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
177 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
179 {"tracepolls",no_argument, (int *) 0, LA_TRACEPOLLS },
181 {(char *) 0, no_argument, (int *) 0, 0 }
184 static int xatoi(char *s, int *errflagptr)
185 /* do safe conversion from string to number */
187 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
188 /* parse and convert numbers, but also check for invalid characters in
197 value = strtol(s, &endptr, 0);
199 /* any invalid chars in string? */
200 if ( (endptr == s) || (*endptr != '\0') ) {
201 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
206 /* is the range valid? */
207 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
208 (value > INT_MAX) || (value < INT_MIN)) {
210 (void) fprintf(stderr, GT_("Value of string '%s' is %s than %d.\n"), s,
211 (value < 0) ? GT_("smaller"): GT_("larger"),
212 (value < 0) ? INT_MIN : INT_MAX);
217 return (int) value; /* shut up, I know what I'm doing */
221 # if defined (STDC_HEADERS)
227 /* We do only base 10 conversions here (atoi)! */
230 /* check for leading white spaces */
231 for (i = 0; (i < len) && isspace(s[i]); i++)
237 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
239 /* skip over digits */
240 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
243 /* check for trailing garbage */
245 (void) fprintf(stderr, GT_("String '%s' is not a valid number string.\n"), s);
250 /* atoi should be safe by now, except for number range over/underflow */
255 int parsecmdline (argc, argv, rctl, ctl)
256 /* parse and validate the command line options */
257 int argc; /* argument count */
258 char **argv; /* argument strings */
259 struct runctl *rctl; /* global run controls to modify */
260 struct query *ctl; /* option record to be initialized */
263 * return value: if positive, argv index of last parsed option + 1
264 * (presumes one or more server names follows). if zero, the
265 * command line switches are such that no server names are
266 * required (e.g. --version). if negative, the command line is
267 * has one or more syntax errors.
271 int ocount = 0; /* count of destinations specified */
272 int errflag = 0; /* TRUE when a syntax error is detected */
273 int helpflag = 0; /* TRUE when option help was explicitly requested */
277 rctl->poll_interval = -1;
279 memset(ctl, '\0', sizeof(struct query)); /* start clean */
280 ctl->smtp_socket = -1;
283 (c = getopt_long(argc,argv,shortoptions,
284 longoptions, &option_index)) != -1)
301 if (outlevel == O_VERBOSE)
304 outlevel = O_VERBOSE;
308 rctl->poll_interval = xatoi(optarg, &errflag);
320 rctl->logfile = prependdir (optarg, currentwd);
323 rctl->invisible = TRUE;
326 rctl->showdots = FLAG_TRUE;
330 rcfile = prependdir (optarg, currentwd);
334 rctl->idfile = prependdir (optarg, currentwd);
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, "opie") == 0)
417 ctl->server.authenticate = A_OTP;
418 else if (strcmp(optarg, "ntlm") == 0)
419 ctl->server.authenticate = A_NTLM;
420 else if (strcmp(optarg, "cram") == 0)
421 ctl->server.authenticate = A_CRAM_MD5;
422 else if (strcmp(optarg, "cram-md5") == 0)
423 ctl->server.authenticate = A_CRAM_MD5;
424 else if (strcmp(optarg, "gssapi") == 0)
425 ctl->server.authenticate = A_GSSAPI;
426 else if (strcmp(optarg, "any") == 0)
427 ctl->server.authenticate = A_ANY;
429 fprintf(stderr,GT_("Invalid authentication `%s' specified.\n"), optarg);
435 ctl->server.timeout = xatoi(optarg, &errflag);
436 if (ctl->server.timeout == 0)
437 ctl->server.timeout = -1;
441 ctl->server.envelope = xstrdup(optarg);
445 ctl->server.qvirtual = xstrdup(optarg);
450 ctl->remotename = xstrdup(optarg);
454 ctl->fetchall = FLAG_TRUE;
458 ctl->keep = FLAG_FALSE;
462 ctl->keep = FLAG_TRUE;
466 ctl->flush = FLAG_TRUE;
470 ctl->rewrite = FLAG_FALSE;
474 c = xatoi(optarg, &errflag);
475 ctl->limit = NUM_VALUE_IN(c);
479 xalloca(buf, char *, strlen(optarg) + 1);
481 cp = strtok(buf, ",");
483 save_str(&ctl->mailboxes, cp, 0);
485 ((cp = strtok((char *)NULL, ",")));
489 xalloca(buf, char *, strlen(optarg) + 1);
491 cp = strtok(buf, ",");
493 save_str(&ctl->smtphunt, cp, TRUE);
495 ((cp = strtok((char *)NULL, ",")));
498 case LA_FETCHDOMAINS:
499 xalloca(buf, char *, strlen(optarg) + 1);
501 cp = strtok(buf, ",");
503 save_str(&ctl->domainlist, cp, TRUE);
505 ((cp = strtok((char *)NULL, ",")));
509 ctl->smtpaddress = xstrdup(optarg);
512 ctl->smtpname = xstrdup(optarg);
516 xalloca(buf, char *, strlen(optarg) + 1);
518 cp = strtok(buf, ",");
520 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
522 idp->val.status.num = xatoi(cp, &errflag);
524 ((cp = strtok((char *)NULL, ",")));
528 c = xatoi(optarg, &errflag);
529 ctl->batchlimit = NUM_VALUE_IN(c);
533 c = xatoi(optarg, &errflag);
534 ctl->fetchlimit = NUM_VALUE_IN(c);
536 case LA_FETCHSIZELIMIT:
537 c = xatoi(optarg, &errflag);
538 ctl->fetchsizelimit = NUM_VALUE_IN(c);
541 c = xatoi(optarg, &errflag);
542 ctl->fastuidl = NUM_VALUE_IN(c);
546 c = xatoi(optarg, &errflag);
547 ctl->expunge = NUM_VALUE_IN(c);
551 ctl->mda = xstrdup(optarg);
555 ctl->bsmtp = prependdir (optarg, currentwd);
559 ctl->listener = LMTP_MODE;
565 ctl->server.netsec = (void *)optarg;
567 fprintf(stderr, GT_("fetchmail: network security support is disabled\n"));
569 #endif /* NET_SECURITY */
572 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
575 interface_parse(optarg, &ctl->server);
579 ctl->server.monitor = xstrdup(optarg);
581 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
583 ctl->server.plugin = xstrdup(optarg);
586 ctl->server.plugout = xstrdup(optarg);
591 ctl->use_ssl = FLAG_TRUE;
595 ctl->sslkey = prependdir (optarg, currentwd);
599 ctl->sslcert = prependdir (optarg, currentwd);
603 ctl->sslproto = xstrdup(optarg);
607 ctl->sslcertck = FLAG_TRUE;
611 ctl->sslcertpath = prependdir(optarg, currentwd);
614 case LA_SSLFINGERPRINT:
615 ctl->sslfingerprint = xstrdup(optarg);
620 ctl->server.principal = xstrdup(optarg);
630 c = xatoi(optarg, &errflag);
631 ctl->warnings = NUM_VALUE_IN(c);
639 rctl->use_syslog = FLAG_TRUE;
643 rctl->use_syslog = FLAG_FALSE;
647 ctl->tracepolls = FLAG_TRUE;
657 if (errflag || ocount > 1 || helpflag) {
658 /* squawk if syntax errors were detected */
659 #define P(s) fputs(s, helpflag ? stdout : stderr)
660 P(GT_("usage: fetchmail [options] [server ...]\n"));
661 P(GT_(" Options are as follows:\n"));
662 P(GT_(" -?, --help display this option help\n"));
663 P(GT_(" -V, --version display version info\n"));
665 P(GT_(" -c, --check check for messages without fetching\n"));
666 P(GT_(" -s, --silent work silently\n"));
667 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
668 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
669 P(GT_(" -N, --nodetach don't detach daemon process\n"));
670 P(GT_(" -q, --quit kill daemon process\n"));
671 P(GT_(" -L, --logfile specify logfile name\n"));
672 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
673 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
674 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
675 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
676 P(GT_(" --postmaster specify recipient of last resort\n"));
677 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
678 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
679 P(GT_(" -I, --interface interface required specification\n"));
680 P(GT_(" -M, --monitor monitor interface for activity\n"));
682 #if defined( SSL_ENABLE )
683 P(GT_(" --ssl enable ssl encrypted session\n"));
684 P(GT_(" --sslkey ssl private key file\n"));
685 P(GT_(" --sslcert ssl client certificate\n"));
686 P(GT_(" --sslproto force ssl protocol (ssl2/ssl3/tls1)\n"));
688 P(GT_(" --plugin specify external command to open connection\n"));
689 P(GT_(" --plugout specify external command to open smtp connection\n"));
691 P(GT_(" -p, --protocol specify retrieval protocol (see man page)\n"));
692 P(GT_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
693 P(GT_(" -P, --port TCP/IP service port to connect to\n"));
694 P(GT_(" --auth authentication type (password/kerberos/ssh/otp)\n"));
695 P(GT_(" -t, --timeout server nonresponse timeout\n"));
696 P(GT_(" -E, --envelope envelope address header\n"));
697 P(GT_(" -Q, --qvirtual prefix to remove from local user id\n"));
698 P(GT_(" --principal mail service principal\n"));
699 P(GT_(" --tracepolls add poll-tracing information to Received header\n"));
701 P(GT_(" -u, --username specify users's login on server\n"));
702 P(GT_(" -a, --all retrieve old and new messages\n"));
703 P(GT_(" -K, --nokeep delete new messages after retrieval\n"));
704 P(GT_(" -k, --keep save new messages after retrieval\n"));
705 P(GT_(" -F, --flush delete old messages from server\n"));
706 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
707 P(GT_(" -l, --limit don't fetch messages over given size\n"));
708 P(GT_(" -w, --warnings interval between warning mail notification\n"));
711 P(GT_(" -T, --netsec set IP security request\n"));
712 #endif /* NET_SECURITY */
713 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
714 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
715 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
716 P(GT_(" --smtpname set SMTP full name username@domain\n"));
717 P(GT_(" -Z, --antispam, set antispam response values\n"));
718 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
719 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
720 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
721 P(GT_(" --fastuidl do a binary search for UIDLs\n"));
722 P(GT_(" -e, --expunge set max deletions between expunges\n"));
723 P(GT_(" -m, --mda set MDA to use for forwarding\n"));
724 P(GT_(" --bsmtp set output BSMTP file\n"));
725 P(GT_(" --lmtp use LMTP (RFC2033) for delivery\n"));
726 P(GT_(" -r, --folder specify remote folder name\n"));
727 P(GT_(" --showdots show progress dots even in logfiles\n"));
739 /* options.c ends here */