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 /* don't use 63-122: they could clash with short options */
93 #define LA_LIMITFLUSH 128
95 /* options still left: CDgGhHjJoORwWxXYz */
96 static const char *shortoptions =
97 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:D:";
99 static const struct option longoptions[] = {
100 /* this can be const because all flag fields are 0 and will never get set */
101 {"help", no_argument, (int *) 0, LA_HELP },
102 {"version", no_argument, (int *) 0, LA_VERSION },
103 {"check", no_argument, (int *) 0, LA_CHECK },
104 {"silent", no_argument, (int *) 0, LA_SILENT },
105 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
106 {"daemon", required_argument, (int *) 0, LA_DAEMON },
107 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
108 {"quit", no_argument, (int *) 0, LA_QUIT },
109 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
110 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
111 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
112 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
113 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
114 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
115 {"idfile", required_argument, (int *) 0, LA_IDFILE },
116 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
117 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
119 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
120 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
121 {"uidl", no_argument, (int *) 0, LA_UIDL },
122 {"port", required_argument, (int *) 0, LA_PORT },
123 {"auth", required_argument, (int *) 0, LA_AUTH},
124 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
125 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
126 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
128 {"user", required_argument, (int *) 0, LA_USERNAME },
129 {"username", required_argument, (int *) 0, LA_USERNAME },
131 {"all", no_argument, (int *) 0, LA_ALL },
132 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
133 {"keep", no_argument, (int *) 0, LA_KEEP },
134 {"flush", no_argument, (int *) 0, LA_FLUSH },
135 {"limitflush", no_argument, (int *) 0, LA_LIMITFLUSH },
136 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
137 {"limit", required_argument, (int *) 0, LA_LIMIT },
138 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
140 {"folder", required_argument, (int *) 0, LA_FOLDER },
141 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
142 {"fetchdomains", required_argument, (int *) 0, LA_FETCHDOMAINS },
143 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
144 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
145 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
147 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
148 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
149 {"fetchsizelimit",required_argument, (int *) 0, LA_FETCHSIZELIMIT },
150 {"fastuidl", required_argument, (int *) 0, LA_FASTUIDL },
151 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
152 {"mda", required_argument, (int *) 0, LA_MDA },
153 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
154 {"lmtp", no_argument, (int *) 0, LA_LMTP },
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 },
169 {"interface", required_argument, (int *) 0, LA_INTERFACE },
170 {"monitor", required_argument, (int *) 0, LA_MONITOR },
171 #endif /* CAN_MONITOR */
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;
331 rcfile = prependdir (optarg, currentwd);
335 rctl->idfile = prependdir (optarg, currentwd);
338 rctl->postmaster = (char *) xstrdup(optarg);
341 run.bouncemail = FALSE;
345 /* XXX -- should probably use a table lookup here */
346 if (strcasecmp(optarg,"auto") == 0)
347 ctl->server.protocol = P_AUTO;
348 else if (strcasecmp(optarg,"pop2") == 0)
349 ctl->server.protocol = P_POP2;
351 else if (strcasecmp(optarg,"sdps") == 0)
353 ctl->server.protocol = P_POP3;
354 ctl->server.sdps = TRUE;
356 #endif /* SDPS_ENABLE */
357 else if (strcasecmp(optarg,"pop3") == 0)
358 ctl->server.protocol = P_POP3;
359 else if (strcasecmp(optarg,"apop") == 0)
360 ctl->server.protocol = P_APOP;
361 else if (strcasecmp(optarg,"rpop") == 0)
362 ctl->server.protocol = P_RPOP;
363 else if (strcasecmp(optarg,"kpop") == 0)
365 ctl->server.protocol = P_POP3;
366 ctl->server.service = KPOP_PORT;
368 ctl->server.authenticate = A_KERBEROS_V5;
370 ctl->server.authenticate = A_KERBEROS_V4;
371 #endif /* KERBEROS_V5 */
373 else if (strcasecmp(optarg,"imap") == 0)
374 ctl->server.protocol = P_IMAP;
375 else if (strcasecmp(optarg,"etrn") == 0)
376 ctl->server.protocol = P_ETRN;
377 else if (strcasecmp(optarg,"odmr") == 0)
378 ctl->server.protocol = P_ODMR;
380 fprintf(stderr,GT_("Invalid protocol `%s' specified.\n"), optarg);
386 ctl->server.uidl = FLAG_TRUE;
390 ctl->server.service = optarg;
393 if (strcmp(optarg, "password") == 0)
394 ctl->server.authenticate = A_PASSWORD;
395 else if (strcmp(optarg, "kerberos") == 0)
397 ctl->server.authenticate = A_KERBEROS_V5;
399 ctl->server.authenticate = A_KERBEROS_V4;
400 #endif /* KERBEROS_V5 */
401 else if (strcmp(optarg, "kerberos_v5") == 0)
402 ctl->server.authenticate = A_KERBEROS_V5;
403 else if (strcmp(optarg, "kerberos_v4") == 0)
404 ctl->server.authenticate = A_KERBEROS_V4;
405 else if (strcmp(optarg, "ssh") == 0)
406 ctl->server.authenticate = A_SSH;
407 else if (strcmp(optarg, "otp") == 0)
408 ctl->server.authenticate = A_OTP;
409 else if (strcmp(optarg, "opie") == 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;
421 else if (strcmp(optarg, "msn") == 0)
422 ctl->server.authenticate = A_MSN;
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;
464 ctl->limitflush = FLAG_TRUE;
468 ctl->rewrite = FLAG_FALSE;
472 c = xatoi(optarg, &errflag);
473 ctl->limit = NUM_VALUE_IN(c);
477 buf = xstrdup(optarg);
478 cp = strtok(buf, ",");
480 save_str(&ctl->mailboxes, cp, 0);
482 ((cp = strtok((char *)NULL, ",")));
487 buf = xstrdup(optarg);
488 cp = strtok(buf, ",");
490 save_str(&ctl->smtphunt, cp, TRUE);
492 ((cp = strtok((char *)NULL, ",")));
496 case LA_FETCHDOMAINS:
497 buf = xstrdup(optarg);
498 cp = strtok(buf, ",");
500 save_str(&ctl->domainlist, cp, TRUE);
502 ((cp = strtok((char *)NULL, ",")));
507 ctl->smtpaddress = xstrdup(optarg);
510 ctl->smtpname = xstrdup(optarg);
514 buf = xstrdup(optarg);
515 cp = strtok(buf, ",");
517 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
519 idp->val.status.num = xatoi(cp, &errflag);
521 ((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);
534 case LA_FETCHSIZELIMIT:
535 c = xatoi(optarg, &errflag);
536 ctl->fetchsizelimit = NUM_VALUE_IN(c);
539 c = xatoi(optarg, &errflag);
540 ctl->fastuidl = NUM_VALUE_IN(c);
544 c = xatoi(optarg, &errflag);
545 ctl->expunge = NUM_VALUE_IN(c);
549 ctl->mda = xstrdup(optarg);
553 ctl->bsmtp = prependdir (optarg, currentwd);
557 ctl->listener = LMTP_MODE;
563 interface_parse(optarg, &ctl->server);
567 ctl->server.monitor = xstrdup(optarg);
569 #endif /* CAN_MONITOR */
571 ctl->server.plugin = xstrdup(optarg);
574 ctl->server.plugout = xstrdup(optarg);
579 ctl->use_ssl = FLAG_TRUE;
583 ctl->sslkey = prependdir (optarg, currentwd);
587 ctl->sslcert = prependdir (optarg, currentwd);
591 ctl->sslproto = xstrdup(optarg);
595 ctl->sslcertck = FLAG_TRUE;
599 ctl->sslcertpath = prependdir(optarg, currentwd);
602 case LA_SSLFINGERPRINT:
603 ctl->sslfingerprint = xstrdup(optarg);
608 ctl->server.principal = xstrdup(optarg);
618 c = xatoi(optarg, &errflag);
619 ctl->warnings = NUM_VALUE_IN(c);
627 rctl->use_syslog = FLAG_TRUE;
631 rctl->use_syslog = FLAG_FALSE;
635 ctl->server.tracepolls = FLAG_TRUE;
645 if (errflag || ocount > 1 || helpflag) {
646 /* squawk if syntax errors were detected */
647 #define P(s) fputs(s, helpflag ? stdout : stderr)
648 P(GT_("usage: fetchmail [options] [server ...]\n"));
649 P(GT_(" Options are as follows:\n"));
650 P(GT_(" -?, --help display this option help\n"));
651 P(GT_(" -V, --version display version info\n"));
653 P(GT_(" -c, --check check for messages without fetching\n"));
654 P(GT_(" -s, --silent work silently\n"));
655 P(GT_(" -v, --verbose work noisily (diagnostic output)\n"));
656 P(GT_(" -d, --daemon run as a daemon once per n seconds\n"));
657 P(GT_(" -N, --nodetach don't detach daemon process\n"));
658 P(GT_(" -q, --quit kill daemon process\n"));
659 P(GT_(" -L, --logfile specify logfile name\n"));
660 P(GT_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
661 P(GT_(" --invisible don't write Received & enable host spoofing\n"));
662 P(GT_(" -f, --fetchmailrc specify alternate run control file\n"));
663 P(GT_(" -i, --idfile specify alternate UIDs file\n"));
664 P(GT_(" --postmaster specify recipient of last resort\n"));
665 P(GT_(" --nobounce redirect bounces from user to postmaster.\n"));
667 P(GT_(" -I, --interface interface required specification\n"));
668 P(GT_(" -M, --monitor monitor interface for activity\n"));
670 #if defined( SSL_ENABLE )
671 P(GT_(" --ssl enable ssl encrypted session\n"));
672 P(GT_(" --sslkey ssl private key file\n"));
673 P(GT_(" --sslcert ssl client certificate\n"));
674 P(GT_(" --sslcertpath path to ssl certificates\n"));
675 P(GT_(" --sslfingerprint fingerprint that must match that of the server's cert.\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/otp)\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_(" --limitflush delete oversized messages\n"));
697 P(GT_(" -n, --norewrite don't rewrite header addresses\n"));
698 P(GT_(" -l, --limit don't fetch messages over given size\n"));
699 P(GT_(" -w, --warnings interval between warning mail notification\n"));
701 P(GT_(" -S, --smtphost set SMTP forwarding host\n"));
702 P(GT_(" --fetchdomains fetch mail for specified domains\n"));
703 P(GT_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
704 P(GT_(" --smtpname set SMTP full name username@domain\n"));
705 P(GT_(" -Z, --antispam, set antispam response values\n"));
706 P(GT_(" -b, --batchlimit set batch limit for SMTP connections\n"));
707 P(GT_(" -B, --fetchlimit set fetch limit for server connections\n"));
708 P(GT_(" --fetchsizelimit set fetch message size limit\n"));
709 P(GT_(" --fastuidl do a binary search for UIDLs\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 */