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
73 #define LA_SMTPNAME 49
81 #define LA_SHOWDOTS 53
82 #define LA_PRINCIPAL 54
84 /* options still left: CDgGhHjJoORwWxXYz */
85 static const char *shortoptions =
86 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:";
88 static const struct option longoptions[] = {
89 /* this can be const because all flag fields are 0 and will never get set */
90 {"help", no_argument, (int *) 0, LA_HELP },
91 {"version", no_argument, (int *) 0, LA_VERSION },
92 {"check", no_argument, (int *) 0, LA_CHECK },
93 {"silent", no_argument, (int *) 0, LA_SILENT },
94 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
95 {"daemon", required_argument, (int *) 0, LA_DAEMON },
96 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
97 {"quit", no_argument, (int *) 0, LA_QUIT },
98 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
99 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
100 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
101 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
102 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
103 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
104 {"idfile", required_argument, (int *) 0, LA_IDFILE },
105 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
106 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
108 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
109 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
110 {"uidl", no_argument, (int *) 0, LA_UIDL },
111 {"port", required_argument, (int *) 0, LA_PORT },
112 {"preauth", required_argument, (int *) 0, LA_PREAUTH},
113 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
114 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
115 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
117 {"user", required_argument, (int *) 0, LA_USERNAME },
118 {"username", required_argument, (int *) 0, LA_USERNAME },
120 {"all", no_argument, (int *) 0, LA_ALL },
121 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
122 {"keep", no_argument, (int *) 0, LA_KEEP },
123 {"flush", no_argument, (int *) 0, LA_FLUSH },
124 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
125 {"limit", required_argument, (int *) 0, LA_LIMIT },
126 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
128 {"folder", required_argument, (int *) 0, LA_FOLDER },
129 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
130 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
131 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
132 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
134 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
135 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
136 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
137 {"mda", required_argument, (int *) 0, LA_MDA },
138 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
139 {"lmtp", no_argument, (int *) 0, LA_LMTP },
142 {"netsec", required_argument, (int *) 0, LA_NETSEC },
143 #endif /* INET6_ENABLE */
146 {"ssl", no_argument, (int *) 0, LA_SSL },
147 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
148 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
151 {"principal", required_argument, (int *) 0, LA_PRINCIPAL },
153 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
154 {"interface", required_argument, (int *) 0, LA_INTERFACE },
155 {"monitor", required_argument, (int *) 0, LA_MONITOR },
156 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
157 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
158 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
160 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
162 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
164 {(char *) 0, no_argument, (int *) 0, 0 }
167 static int xatoi(char *s, int *errflagptr)
168 /* do safe conversion from string to number */
170 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
171 /* parse and convert numbers, but also check for invalid characters in
180 value = strtol(s, &endptr, 0);
182 /* any invalid chars in string? */
183 if ( (endptr == s) || (*endptr != '\0') ) {
184 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
189 /* is the range valid? */
190 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
191 (value > INT_MAX) || (value < INT_MIN)) {
193 (void) fprintf(stderr, _("Value of string '%s' is %s than %d.\n"), s,
194 (value < 0) ? _("smaller"): _("larger"),
195 (value < 0) ? INT_MIN : INT_MAX);
200 return (int) value; /* shut up, I know what I'm doing */
204 # if defined (STDC_HEADERS)
210 /* We do only base 10 conversions here (atoi)! */
213 /* check for leading white spaces */
214 for (i = 0; (i < len) && isspace(s[i]); i++)
220 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
222 /* skip over digits */
223 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
226 /* check for trailing garbage */
228 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
233 /* atoi should be safe by now, except for number range over/underflow */
238 int parsecmdline (argc, argv, rctl, ctl)
239 /* parse and validate the command line options */
240 int argc; /* argument count */
241 char **argv; /* argument strings */
242 struct runctl *rctl; /* global run controls to modify */
243 struct query *ctl; /* option record to be initialized */
246 * return value: if positive, argv index of last parsed option + 1
247 * (presumes one or more server names follows). if zero, the
248 * command line switches are such that no server names are
249 * required (e.g. --version). if negative, the command line is
250 * has one or more syntax errors.
254 int ocount = 0; /* count of destinations specified */
255 int errflag = 0; /* TRUE when a syntax error is detected */
256 int helpflag = 0; /* TRUE when option help was explicitly requested */
260 rctl->poll_interval = -1;
262 memset(ctl, '\0', sizeof(struct query)); /* start clean */
263 ctl->smtp_socket = -1;
266 (c = getopt_long(argc,argv,shortoptions,
267 longoptions,&option_index)) != -1) {
284 if (outlevel == O_VERBOSE)
287 outlevel = O_VERBOSE;
291 rctl->poll_interval = xatoi(optarg, &errflag);
303 rctl->logfile = optarg;
306 rctl->invisible = TRUE;
309 rctl->showdots = TRUE;
313 rcfile = (char *) xstrdup(optarg);
317 rctl->idfile = (char *) xstrdup(optarg);
320 rctl->postmaster = (char *) xstrdup(optarg);
323 run.bouncemail = FALSE;
327 /* XXX -- should probably use a table lookup here */
328 if (strcasecmp(optarg,"auto") == 0)
329 ctl->server.protocol = P_AUTO;
330 else if (strcasecmp(optarg,"pop2") == 0)
331 ctl->server.protocol = P_POP2;
333 else if (strcasecmp(optarg,"sdps") == 0)
335 ctl->server.protocol = P_POP3;
336 ctl->server.sdps = TRUE;
338 #endif /* SDPS_ENABLE */
339 else if (strcasecmp(optarg,"pop3") == 0)
340 ctl->server.protocol = P_POP3;
341 else if (strcasecmp(optarg,"apop") == 0)
342 ctl->server.protocol = P_APOP;
343 else if (strcasecmp(optarg,"rpop") == 0)
344 ctl->server.protocol = P_RPOP;
345 else if (strcasecmp(optarg,"kpop") == 0)
347 ctl->server.protocol = P_POP3;
349 ctl->server.service = KPOP_PORT;
350 #else /* INET6_ENABLE */
351 ctl->server.port = KPOP_PORT;
352 #endif /* INET6_ENABLE */
354 ctl->server.preauthenticate = A_KERBEROS_V5;
356 ctl->server.preauthenticate = A_KERBEROS_V4;
357 #endif /* KERBEROS_V5 */
359 else if (strcasecmp(optarg,"imap") == 0)
360 ctl->server.protocol = P_IMAP;
362 else if (strcasecmp(optarg,"imap-k4") == 0)
363 ctl->server.protocol = P_IMAP_K4;
364 #endif /* KERBEROS_V4 */
366 else if (strcasecmp(optarg, "imap-gss") == 0)
367 ctl->server.protocol = P_IMAP_GSS;
369 else if (strcasecmp(optarg, "imap-crammd5") == 0)
370 ctl->server.protocol = P_IMAP_CRAM_MD5;
371 else if (strcasecmp(optarg, "imap-login") == 0)
372 ctl->server.protocol = P_IMAP_LOGIN;
373 else if (strcasecmp(optarg,"etrn") == 0)
374 ctl->server.protocol = P_ETRN;
376 fprintf(stderr,_("Invalid protocol `%s' specified.\n"), optarg);
382 ctl->server.uidl = FLAG_TRUE;
387 ctl->server.service = optarg;
388 #else /* INET6_ENABLE */
389 ctl->server.port = xatoi(optarg, &errflag);
390 #endif /* INET6_ENABLE */
393 if (strcmp(optarg, "password") == 0)
394 ctl->server.preauthenticate = A_PASSWORD;
395 else if (strcmp(optarg, "kerberos") == 0)
397 ctl->server.preauthenticate = A_KERBEROS_V5;
399 ctl->server.preauthenticate = A_KERBEROS_V4;
400 #endif /* KERBEROS_V5 */
401 else if (strcmp(optarg, "kerberos_v5") == 0)
402 ctl->server.preauthenticate = A_KERBEROS_V5;
403 else if (strcmp(optarg, "kerberos_v4") == 0)
404 ctl->server.preauthenticate = A_KERBEROS_V4;
405 else if (strcmp(optarg, "ssh") == 0)
406 ctl->server.preauthenticate = A_SSH;
408 fprintf(stderr,_("Invalid preauthentication `%s' specified.\n"), optarg);
414 ctl->server.timeout = xatoi(optarg, &errflag);
415 if (ctl->server.timeout == 0)
416 ctl->server.timeout = -1;
420 ctl->server.envelope = xstrdup(optarg);
424 ctl->server.qvirtual = xstrdup(optarg);
429 ctl->remotename = xstrdup(optarg);
433 ctl->fetchall = FLAG_TRUE;
437 ctl->keep = FLAG_FALSE;
441 ctl->keep = FLAG_TRUE;
445 ctl->flush = FLAG_TRUE;
449 ctl->rewrite = FLAG_FALSE;
453 c = xatoi(optarg, &errflag);
454 ctl->limit = NUM_VALUE_IN(c);
458 xalloca(buf, char *, strlen(optarg) + 1);
460 cp = strtok(buf, ",");
462 save_str(&ctl->mailboxes, cp, 0);
464 ((cp = strtok((char *)NULL, ",")));
468 xalloca(buf, char *, strlen(optarg) + 1);
470 cp = strtok(buf, ",");
472 save_str(&ctl->smtphunt, cp, TRUE);
474 ((cp = strtok((char *)NULL, ",")));
479 ctl->smtpaddress = xstrdup(optarg);
482 ctl->smtpname = xstrdup(optarg);
486 xalloca(buf, char *, strlen(optarg) + 1);
488 cp = strtok(buf, ",");
490 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
492 idp->val.status.num = xatoi(cp, &errflag);
494 ((cp = strtok((char *)NULL, ",")));
498 c = xatoi(optarg, &errflag);
499 ctl->batchlimit = NUM_VALUE_IN(c);
503 c = xatoi(optarg, &errflag);
504 ctl->fetchlimit = NUM_VALUE_IN(c);
508 c = xatoi(optarg, &errflag);
509 ctl->expunge = NUM_VALUE_IN(c);
513 ctl->mda = xstrdup(optarg);
517 ctl->bsmtp = xstrdup(optarg);
521 ctl->listener = LMTP_MODE;
527 ctl->server.netsec = (void *)optarg;
529 fprintf(stderr, _("fetchmail: network security support is disabled\n"));
531 #endif /* NET_SECURITY */
534 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
537 interface_parse(optarg, &ctl->server);
541 ctl->server.monitor = xstrdup(optarg);
543 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
545 ctl->server.plugin = xstrdup(optarg);
548 ctl->server.plugout = xstrdup(optarg);
553 ctl->use_ssl = FLAG_TRUE;
557 ctl->sslkey = xstrdup(optarg);
561 ctl->sslcert = xstrdup(optarg);
566 ctl->server.principal = xstrdup(optarg);
576 c = xatoi(optarg, &errflag);
577 ctl->warnings = NUM_VALUE_IN(c);
585 rctl->use_syslog = FLAG_TRUE;
589 rctl->use_syslog = FLAG_FALSE;
599 if (errflag || ocount > 1 || helpflag) {
600 /* squawk if syntax errors were detected */
601 #define P(s) fputs(s, helpflag ? stdout : stderr)
602 P(_("usage: fetchmail [options] [server ...]\n"));
603 P(_(" Options are as follows:\n"));
604 P(_(" -?, --help display this option help\n"));
605 P(_(" -V, --version display version info\n"));
607 P(_(" -c, --check check for messages without fetching\n"));
608 P(_(" -s, --silent work silently\n"));
609 P(_(" -v, --verbose work noisily (diagnostic output)\n"));
610 P(_(" -d, --daemon run as a daemon once per n seconds\n"));
611 P(_(" -N, --nodetach don't detach daemon process\n"));
612 P(_(" -q, --quit kill daemon process\n"));
613 P(_(" -L, --logfile specify logfile name\n"));
614 P(_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
615 P(_(" --invisible don't write Received & enable host spoofing\n"));
616 P(_(" -f, --fetchmailrc specify alternate run control file\n"));
617 P(_(" -i, --idfile specify alternate UIDs file\n"));
618 P(_(" --postmaster specify recipient of last resort\n"));
619 P(_(" --nobounce redirect bounces from user to postmaster.\n"));
620 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
621 P(_(" -I, --interface interface required specification\n"));
622 P(_(" -M, --monitor monitor interface for activity\n"));
624 #if defined( SSL_ENABLE )
625 P(_(" --ssl enable ssl encrypted session\n"));
626 P(_(" --sslkey ssl private key file\n"));
627 P(_(" --sslcert ssl client certificate\n"));
629 P(_(" --plugin specify external command to open connection\n"));
630 P(_(" --plugout specify external command to open smtp connection\n"));
632 P(_(" -p, --protocol specify retrieval protocol (see man page)\n"));
633 P(_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
634 P(_(" -P, --port TCP/IP service port to connect to\n"));
635 P(_(" --preauth preauthentication type (password/kerberos/ssh)\n"));
636 P(_(" -t, --timeout server nonresponse timeout\n"));
637 P(_(" -E, --envelope envelope address header\n"));
638 P(_(" -Q, --qvirtual prefix to remove from local user id\n"));
639 P(_(" --principal mail service principal\n"));
641 P(_(" -u, --username specify users's login on server\n"));
642 P(_(" -a, --all retrieve old and new messages\n"));
643 P(_(" -K, --nokeep delete new messages after retrieval\n"));
644 P(_(" -k, --keep save new messages after retrieval\n"));
645 P(_(" -F, --flush delete old messages from server\n"));
646 P(_(" -n, --norewrite don't rewrite header addresses\n"));
647 P(_(" -l, --limit don't fetch messages over given size\n"));
648 P(_(" -w, --warnings interval between warning mail notification\n"));
651 P(_(" -T, --netsec set IP security request\n"));
652 #endif /* NET_SECURITY */
653 P(_(" -S, --smtphost set SMTP forwarding host\n"));
654 P(_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
655 P(_(" --smtpname set SMTP full name username@domain\n"));
656 P(_(" -Z, --antispam, set antispam response values\n"));
657 P(_(" -b, --batchlimit set batch limit for SMTP connections\n"));
658 P(_(" -B, --fetchlimit set fetch limit for server connections\n"));
659 P(_(" -e, --expunge set max deletions between expunges\n"));
660 P(_(" -m, --mda set MDA to use for forwarding\n"));
661 P(_(" --bsmtp set output BSMTP file\n"));
662 P(_(" --lmtp use LMTP (RFC2033) for delivery\n"));
663 P(_(" -r, --folder specify remote folder name\n"));
664 P(_(" --showdots show progress dots even in logfiles\n"));
676 /* options.c ends here */