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
80 /* options still left: CDgGhHjJoORwWxXYz */
81 static const char *shortoptions =
82 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:";
84 static const struct option longoptions[] = {
85 /* this can be const because all flag fields are 0 and will never get set */
86 {"help", no_argument, (int *) 0, LA_HELP },
87 {"version", no_argument, (int *) 0, LA_VERSION },
88 {"check", no_argument, (int *) 0, LA_CHECK },
89 {"silent", no_argument, (int *) 0, LA_SILENT },
90 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
91 {"daemon", required_argument, (int *) 0, LA_DAEMON },
92 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
93 {"quit", no_argument, (int *) 0, LA_QUIT },
94 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
95 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
96 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
97 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
98 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
99 {"idfile", required_argument, (int *) 0, LA_IDFILE },
100 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
101 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
103 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
104 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
105 {"uidl", no_argument, (int *) 0, LA_UIDL },
106 {"port", required_argument, (int *) 0, LA_PORT },
107 {"preauth", required_argument, (int *) 0, LA_PREAUTH},
108 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
109 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
110 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
112 {"user", required_argument, (int *) 0, LA_USERNAME },
113 {"username", required_argument, (int *) 0, LA_USERNAME },
115 {"all", no_argument, (int *) 0, LA_ALL },
116 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
117 {"keep", no_argument, (int *) 0, LA_KEEP },
118 {"flush", no_argument, (int *) 0, LA_FLUSH },
119 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
120 {"limit", required_argument, (int *) 0, LA_LIMIT },
121 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
123 {"folder", required_argument, (int *) 0, LA_FOLDER },
124 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
125 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
126 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
128 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
129 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
130 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
131 {"mda", required_argument, (int *) 0, LA_MDA },
132 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
133 {"lmtp", no_argument, (int *) 0, LA_LMTP },
136 {"netsec", required_argument, (int *) 0, LA_NETSEC },
137 #endif /* INET6_ENABLE */
140 {"ssl", no_argument, (int *) 0, LA_SSL },
141 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
142 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
145 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
146 {"interface", required_argument, (int *) 0, LA_INTERFACE },
147 {"monitor", required_argument, (int *) 0, LA_MONITOR },
148 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
149 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
150 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
152 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
154 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
156 {(char *) 0, no_argument, (int *) 0, 0 }
159 static int xatoi(char *s, int *errflagptr)
160 /* do safe conversion from string to number */
162 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
163 /* parse and convert numbers, but also check for invalid characters in
172 value = strtol(s, &endptr, 0);
174 /* any invalid chars in string? */
175 if ( (endptr == s) || (*endptr != '\0') ) {
176 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
181 /* is the range valid? */
182 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
183 (value > INT_MAX) || (value < INT_MIN)) {
185 (void) fprintf(stderr, _("Value of string '%s' is %s than %d.\n"), s,
186 (value < 0) ? _("smaller"): _("larger"),
187 (value < 0) ? INT_MIN : INT_MAX);
192 return (int) value; /* shut up, I know what I'm doing */
196 # if defined (STDC_HEADERS)
202 /* We do only base 10 conversions here (atoi)! */
205 /* check for leading white spaces */
206 for (i = 0; (i < len) && isspace(s[i]); i++)
212 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
214 /* skip over digits */
215 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
218 /* check for trailing garbage */
220 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
225 /* atoi should be safe by now, except for number range over/underflow */
230 int parsecmdline (argc, argv, rctl, ctl)
231 /* parse and validate the command line options */
232 int argc; /* argument count */
233 char **argv; /* argument strings */
234 struct runctl *rctl; /* global run controls to modify */
235 struct query *ctl; /* option record to be initialized */
238 * return value: if positive, argv index of last parsed option + 1
239 * (presumes one or more server names follows). if zero, the
240 * command line switches are such that no server names are
241 * required (e.g. --version). if negative, the command line is
242 * has one or more syntax errors.
246 int ocount = 0; /* count of destinations specified */
247 int errflag = 0; /* TRUE when a syntax error is detected */
248 int helpflag = 0; /* TRUE when option help was explicitly requested */
252 rctl->poll_interval = -1;
254 memset(ctl, '\0', sizeof(struct query)); /* start clean */
255 ctl->smtp_socket = -1;
258 (c = getopt_long(argc,argv,shortoptions,
259 longoptions,&option_index)) != -1) {
276 if (outlevel == O_VERBOSE)
279 outlevel = O_VERBOSE;
283 rctl->poll_interval = xatoi(optarg, &errflag);
295 rctl->logfile = optarg;
298 rctl->invisible = TRUE;
302 rcfile = (char *) xstrdup(optarg);
306 rctl->idfile = (char *) xstrdup(optarg);
309 rctl->postmaster = (char *) xstrdup(optarg);
312 run.bouncemail = FALSE;
316 /* XXX -- should probably use a table lookup here */
317 if (strcasecmp(optarg,"auto") == 0)
318 ctl->server.protocol = P_AUTO;
319 else if (strcasecmp(optarg,"pop2") == 0)
320 ctl->server.protocol = P_POP2;
322 else if (strcasecmp(optarg,"sdps") == 0)
324 ctl->server.protocol = P_POP3;
325 ctl->server.sdps = TRUE;
327 #endif /* SDPS_ENABLE */
328 else if (strcasecmp(optarg,"pop3") == 0)
329 ctl->server.protocol = P_POP3;
330 else if (strcasecmp(optarg,"apop") == 0)
331 ctl->server.protocol = P_APOP;
332 else if (strcasecmp(optarg,"rpop") == 0)
333 ctl->server.protocol = P_RPOP;
334 else if (strcasecmp(optarg,"kpop") == 0)
336 ctl->server.protocol = P_POP3;
338 ctl->server.service = KPOP_PORT;
339 #else /* INET6_ENABLE */
340 ctl->server.port = KPOP_PORT;
341 #endif /* INET6_ENABLE */
343 ctl->server.preauthenticate = A_KERBEROS_V5;
345 ctl->server.preauthenticate = A_KERBEROS_V4;
346 #endif /* KERBEROS_V5 */
348 else if (strcasecmp(optarg,"imap") == 0)
349 ctl->server.protocol = P_IMAP;
351 else if (strcasecmp(optarg,"imap-k4") == 0)
352 ctl->server.protocol = P_IMAP_K4;
353 #endif /* KERBEROS_V4 */
355 else if (strcasecmp(optarg, "imap-gss") == 0)
356 ctl->server.protocol = P_IMAP_GSS;
358 else if (strcasecmp(optarg, "imap-crammd5") == 0)
359 ctl->server.protocol = P_IMAP_CRAM_MD5;
360 else if (strcasecmp(optarg, "imap-login") == 0)
361 ctl->server.protocol = P_IMAP_LOGIN;
362 else if (strcasecmp(optarg,"etrn") == 0)
363 ctl->server.protocol = P_ETRN;
365 fprintf(stderr,_("Invalid protocol `%s' specified.\n"), optarg);
371 ctl->server.uidl = FLAG_TRUE;
376 ctl->server.service = optarg;
377 #else /* INET6_ENABLE */
378 ctl->server.port = xatoi(optarg, &errflag);
379 #endif /* INET6_ENABLE */
382 if (strcmp(optarg, "password") == 0)
383 ctl->server.preauthenticate = A_PASSWORD;
384 else if (strcmp(optarg, "kerberos") == 0)
386 ctl->server.preauthenticate = A_KERBEROS_V5;
388 ctl->server.preauthenticate = A_KERBEROS_V4;
389 #endif /* KERBEROS_V5 */
390 else if (strcmp(optarg, "kerberos_v5") == 0)
391 ctl->server.preauthenticate = A_KERBEROS_V5;
392 else if (strcmp(optarg, "kerberos_v4") == 0)
393 ctl->server.preauthenticate = A_KERBEROS_V4;
394 else if (strcmp(optarg, "ssh") == 0)
395 ctl->server.preauthenticate = A_SSH;
397 fprintf(stderr,_("Invalid preauthentication `%s' specified.\n"), optarg);
403 ctl->server.timeout = xatoi(optarg, &errflag);
404 if (ctl->server.timeout == 0)
405 ctl->server.timeout = -1;
409 ctl->server.envelope = xstrdup(optarg);
413 ctl->server.qvirtual = xstrdup(optarg);
418 ctl->remotename = xstrdup(optarg);
422 ctl->fetchall = FLAG_TRUE;
426 ctl->keep = FLAG_FALSE;
430 ctl->keep = FLAG_TRUE;
434 ctl->flush = FLAG_TRUE;
438 ctl->rewrite = FLAG_FALSE;
442 c = xatoi(optarg, &errflag);
443 ctl->limit = NUM_VALUE_IN(c);
447 xalloca(buf, char *, strlen(optarg) + 1);
449 cp = strtok(buf, ",");
451 save_str(&ctl->mailboxes, cp, 0);
453 ((cp = strtok((char *)NULL, ",")));
457 xalloca(buf, char *, strlen(optarg) + 1);
459 cp = strtok(buf, ",");
461 save_str(&ctl->smtphunt, cp, TRUE);
463 ((cp = strtok((char *)NULL, ",")));
468 ctl->smtpaddress = xstrdup(optarg);
472 xalloca(buf, char *, strlen(optarg) + 1);
474 cp = strtok(buf, ",");
476 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
478 idp->val.status.num = xatoi(cp, &errflag);
480 ((cp = strtok((char *)NULL, ",")));
484 c = xatoi(optarg, &errflag);
485 ctl->batchlimit = NUM_VALUE_IN(c);
489 c = xatoi(optarg, &errflag);
490 ctl->fetchlimit = NUM_VALUE_IN(c);
494 c = xatoi(optarg, &errflag);
495 ctl->expunge = NUM_VALUE_IN(c);
499 ctl->mda = xstrdup(optarg);
503 ctl->bsmtp = xstrdup(optarg);
507 ctl->listener = LMTP_MODE;
513 ctl->server.netsec = (void *)optarg;
515 fprintf(stderr, _("fetchmail: network security support is disabled\n"));
517 #endif /* NET_SECURITY */
520 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
523 interface_parse(optarg, &ctl->server);
527 ctl->server.monitor = xstrdup(optarg);
529 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
531 ctl->server.plugin = xstrdup(optarg);
534 ctl->server.plugout = xstrdup(optarg);
539 ctl->use_ssl = FLAG_TRUE;
543 ctl->sslkey = xstrdup(optarg);
547 ctl->sslcert = xstrdup(optarg);
558 c = xatoi(optarg, &errflag);
559 ctl->warnings = NUM_VALUE_IN(c);
567 rctl->use_syslog = FLAG_TRUE;
571 rctl->use_syslog = FLAG_FALSE;
581 if (errflag || ocount > 1 || helpflag) {
582 /* squawk if syntax errors were detected */
583 #define P(s) fputs(s, helpflag ? stdout : stderr)
584 P(_("usage: fetchmail [options] [server ...]\n"));
585 P(_(" Options are as follows:\n"));
586 P(_(" -?, --help display this option help\n"));
587 P(_(" -V, --version display version info\n"));
589 P(_(" -c, --check check for messages without fetching\n"));
590 P(_(" -s, --silent work silently\n"));
591 P(_(" -v, --verbose work noisily (diagnostic output)\n"));
592 P(_(" -d, --daemon run as a daemon once per n seconds\n"));
593 P(_(" -N, --nodetach don't detach daemon process\n"));
594 P(_(" -q, --quit kill daemon process\n"));
595 P(_(" -L, --logfile specify logfile name\n"));
596 P(_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
597 P(_(" --invisible don't write Received & enable host spoofing\n"));
598 P(_(" -f, --fetchmailrc specify alternate run control file\n"));
599 P(_(" -i, --idfile specify alternate UIDs file\n"));
600 P(_(" --postmaster specify recipient of last resort\n"));
601 P(_(" --nobounce redirect bounces from user to postmaster.\n"));
602 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
603 P(_(" -I, --interface interface required specification\n"));
604 P(_(" -M, --monitor monitor interface for activity\n"));
606 #if defined( SSL_ENABLE )
607 P(_(" --ssl enable ssl encrypted session\n"));
608 P(_(" --sslkey ssl private key file\n"));
609 P(_(" --sslcert ssl client certificate\n"));
611 P(_(" --plugin specify external command to open connection\n"));
612 P(_(" --plugout specify external command to open smtp connection\n"));
614 P(_(" -p, --protocol specify retrieval protocol (see man page)\n"));
615 P(_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
616 P(_(" -P, --port TCP/IP service port to connect to\n"));
617 P(_(" --preauth preauthentication type (password/kerberos/ssh)\n"));
618 P(_(" -t, --timeout server nonresponse timeout\n"));
619 P(_(" -E, --envelope envelope address header\n"));
620 P(_(" -Q, --qvirtual prefix to remove from local user id\n"));
622 P(_(" -u, --username specify users's login on server\n"));
623 P(_(" -a, --all retrieve old and new messages\n"));
624 P(_(" -K, --nokeep delete new messages after retrieval\n"));
625 P(_(" -k, --keep save new messages after retrieval\n"));
626 P(_(" -F, --flush delete old messages from server\n"));
627 P(_(" -n, --norewrite don't rewrite header addresses\n"));
628 P(_(" -l, --limit don't fetch messages over given size\n"));
629 P(_(" -w, --warnings interval between warning mail notification\n"));
632 P(_(" -T, --netsec set IP security request\n"));
633 #endif /* NET_SECURITY */
634 P(_(" -S, --smtphost set SMTP forwarding host\n"));
635 P(_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
636 P(_(" -Z, --antispam, set antispam response values\n"));
637 P(_(" -b, --batchlimit set batch limit for SMTP connections\n"));
638 P(_(" -B, --fetchlimit set fetch limit for server connections\n"));
639 P(_(" -e, --expunge set max deletions between expunges\n"));
640 P(_(" -m, --mda set MDA to use for forwarding\n"));
641 P(_(" --bsmtp set output BSMTP file\n"));
642 P(_(" --lmtp use LMTP (RFC2033) for delivery\n"));
643 P(_(" -r, --folder specify remote folder name\n"));
655 /* options.c ends here */