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 /* options still left: CDgGhHjJoORwWxXYz */
82 static const char *shortoptions =
83 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:";
85 static const struct option longoptions[] = {
86 /* this can be const because all flag fields are 0 and will never get set */
87 {"help", no_argument, (int *) 0, LA_HELP },
88 {"version", no_argument, (int *) 0, LA_VERSION },
89 {"check", no_argument, (int *) 0, LA_CHECK },
90 {"silent", no_argument, (int *) 0, LA_SILENT },
91 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
92 {"daemon", required_argument, (int *) 0, LA_DAEMON },
93 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
94 {"quit", no_argument, (int *) 0, LA_QUIT },
95 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
96 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
97 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
98 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
99 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
100 {"idfile", required_argument, (int *) 0, LA_IDFILE },
101 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
102 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
104 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
105 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
106 {"uidl", no_argument, (int *) 0, LA_UIDL },
107 {"port", required_argument, (int *) 0, LA_PORT },
108 {"preauth", required_argument, (int *) 0, LA_PREAUTH},
109 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
110 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
111 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
113 {"user", required_argument, (int *) 0, LA_USERNAME },
114 {"username", required_argument, (int *) 0, LA_USERNAME },
116 {"all", no_argument, (int *) 0, LA_ALL },
117 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
118 {"keep", no_argument, (int *) 0, LA_KEEP },
119 {"flush", no_argument, (int *) 0, LA_FLUSH },
120 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
121 {"limit", required_argument, (int *) 0, LA_LIMIT },
122 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
124 {"folder", required_argument, (int *) 0, LA_FOLDER },
125 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
126 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
127 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
128 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
130 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
131 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
132 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
133 {"mda", required_argument, (int *) 0, LA_MDA },
134 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
135 {"lmtp", no_argument, (int *) 0, LA_LMTP },
138 {"netsec", required_argument, (int *) 0, LA_NETSEC },
139 #endif /* INET6_ENABLE */
142 {"ssl", no_argument, (int *) 0, LA_SSL },
143 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
144 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
147 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
148 {"interface", required_argument, (int *) 0, LA_INTERFACE },
149 {"monitor", required_argument, (int *) 0, LA_MONITOR },
150 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
151 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
152 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
154 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
156 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
158 {(char *) 0, no_argument, (int *) 0, 0 }
161 static int xatoi(char *s, int *errflagptr)
162 /* do safe conversion from string to number */
164 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
165 /* parse and convert numbers, but also check for invalid characters in
174 value = strtol(s, &endptr, 0);
176 /* any invalid chars in string? */
177 if ( (endptr == s) || (*endptr != '\0') ) {
178 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
183 /* is the range valid? */
184 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
185 (value > INT_MAX) || (value < INT_MIN)) {
187 (void) fprintf(stderr, _("Value of string '%s' is %s than %d.\n"), s,
188 (value < 0) ? _("smaller"): _("larger"),
189 (value < 0) ? INT_MIN : INT_MAX);
194 return (int) value; /* shut up, I know what I'm doing */
198 # if defined (STDC_HEADERS)
204 /* We do only base 10 conversions here (atoi)! */
207 /* check for leading white spaces */
208 for (i = 0; (i < len) && isspace(s[i]); i++)
214 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
216 /* skip over digits */
217 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
220 /* check for trailing garbage */
222 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
227 /* atoi should be safe by now, except for number range over/underflow */
232 int parsecmdline (argc, argv, rctl, ctl)
233 /* parse and validate the command line options */
234 int argc; /* argument count */
235 char **argv; /* argument strings */
236 struct runctl *rctl; /* global run controls to modify */
237 struct query *ctl; /* option record to be initialized */
240 * return value: if positive, argv index of last parsed option + 1
241 * (presumes one or more server names follows). if zero, the
242 * command line switches are such that no server names are
243 * required (e.g. --version). if negative, the command line is
244 * has one or more syntax errors.
248 int ocount = 0; /* count of destinations specified */
249 int errflag = 0; /* TRUE when a syntax error is detected */
250 int helpflag = 0; /* TRUE when option help was explicitly requested */
254 rctl->poll_interval = -1;
256 memset(ctl, '\0', sizeof(struct query)); /* start clean */
257 ctl->smtp_socket = -1;
260 (c = getopt_long(argc,argv,shortoptions,
261 longoptions,&option_index)) != -1) {
278 if (outlevel == O_VERBOSE)
281 outlevel = O_VERBOSE;
285 rctl->poll_interval = xatoi(optarg, &errflag);
297 rctl->logfile = optarg;
300 rctl->invisible = TRUE;
304 rcfile = (char *) xstrdup(optarg);
308 rctl->idfile = (char *) xstrdup(optarg);
311 rctl->postmaster = (char *) xstrdup(optarg);
314 run.bouncemail = FALSE;
318 /* XXX -- should probably use a table lookup here */
319 if (strcasecmp(optarg,"auto") == 0)
320 ctl->server.protocol = P_AUTO;
321 else if (strcasecmp(optarg,"pop2") == 0)
322 ctl->server.protocol = P_POP2;
324 else if (strcasecmp(optarg,"sdps") == 0)
326 ctl->server.protocol = P_POP3;
327 ctl->server.sdps = TRUE;
329 #endif /* SDPS_ENABLE */
330 else if (strcasecmp(optarg,"pop3") == 0)
331 ctl->server.protocol = P_POP3;
332 else if (strcasecmp(optarg,"apop") == 0)
333 ctl->server.protocol = P_APOP;
334 else if (strcasecmp(optarg,"rpop") == 0)
335 ctl->server.protocol = P_RPOP;
336 else if (strcasecmp(optarg,"kpop") == 0)
338 ctl->server.protocol = P_POP3;
340 ctl->server.service = KPOP_PORT;
341 #else /* INET6_ENABLE */
342 ctl->server.port = KPOP_PORT;
343 #endif /* INET6_ENABLE */
345 ctl->server.preauthenticate = A_KERBEROS_V5;
347 ctl->server.preauthenticate = A_KERBEROS_V4;
348 #endif /* KERBEROS_V5 */
350 else if (strcasecmp(optarg,"imap") == 0)
351 ctl->server.protocol = P_IMAP;
353 else if (strcasecmp(optarg,"imap-k4") == 0)
354 ctl->server.protocol = P_IMAP_K4;
355 #endif /* KERBEROS_V4 */
357 else if (strcasecmp(optarg, "imap-gss") == 0)
358 ctl->server.protocol = P_IMAP_GSS;
360 else if (strcasecmp(optarg, "imap-crammd5") == 0)
361 ctl->server.protocol = P_IMAP_CRAM_MD5;
362 else if (strcasecmp(optarg, "imap-login") == 0)
363 ctl->server.protocol = P_IMAP_LOGIN;
364 else if (strcasecmp(optarg,"etrn") == 0)
365 ctl->server.protocol = P_ETRN;
367 fprintf(stderr,_("Invalid protocol `%s' specified.\n"), optarg);
373 ctl->server.uidl = FLAG_TRUE;
378 ctl->server.service = optarg;
379 #else /* INET6_ENABLE */
380 ctl->server.port = xatoi(optarg, &errflag);
381 #endif /* INET6_ENABLE */
384 if (strcmp(optarg, "password") == 0)
385 ctl->server.preauthenticate = A_PASSWORD;
386 else if (strcmp(optarg, "kerberos") == 0)
388 ctl->server.preauthenticate = A_KERBEROS_V5;
390 ctl->server.preauthenticate = A_KERBEROS_V4;
391 #endif /* KERBEROS_V5 */
392 else if (strcmp(optarg, "kerberos_v5") == 0)
393 ctl->server.preauthenticate = A_KERBEROS_V5;
394 else if (strcmp(optarg, "kerberos_v4") == 0)
395 ctl->server.preauthenticate = A_KERBEROS_V4;
396 else if (strcmp(optarg, "ssh") == 0)
397 ctl->server.preauthenticate = A_SSH;
399 fprintf(stderr,_("Invalid preauthentication `%s' specified.\n"), optarg);
405 ctl->server.timeout = xatoi(optarg, &errflag);
406 if (ctl->server.timeout == 0)
407 ctl->server.timeout = -1;
411 ctl->server.envelope = xstrdup(optarg);
415 ctl->server.qvirtual = xstrdup(optarg);
420 ctl->remotename = xstrdup(optarg);
424 ctl->fetchall = FLAG_TRUE;
428 ctl->keep = FLAG_FALSE;
432 ctl->keep = FLAG_TRUE;
436 ctl->flush = FLAG_TRUE;
440 ctl->rewrite = FLAG_FALSE;
444 c = xatoi(optarg, &errflag);
445 ctl->limit = NUM_VALUE_IN(c);
449 xalloca(buf, char *, strlen(optarg) + 1);
451 cp = strtok(buf, ",");
453 save_str(&ctl->mailboxes, cp, 0);
455 ((cp = strtok((char *)NULL, ",")));
459 xalloca(buf, char *, strlen(optarg) + 1);
461 cp = strtok(buf, ",");
463 save_str(&ctl->smtphunt, cp, TRUE);
465 ((cp = strtok((char *)NULL, ",")));
470 ctl->smtpaddress = xstrdup(optarg);
473 ctl->smtpname = xstrdup(optarg);
477 xalloca(buf, char *, strlen(optarg) + 1);
479 cp = strtok(buf, ",");
481 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
483 idp->val.status.num = xatoi(cp, &errflag);
485 ((cp = strtok((char *)NULL, ",")));
489 c = xatoi(optarg, &errflag);
490 ctl->batchlimit = NUM_VALUE_IN(c);
494 c = xatoi(optarg, &errflag);
495 ctl->fetchlimit = NUM_VALUE_IN(c);
499 c = xatoi(optarg, &errflag);
500 ctl->expunge = NUM_VALUE_IN(c);
504 ctl->mda = xstrdup(optarg);
508 ctl->bsmtp = xstrdup(optarg);
512 ctl->listener = LMTP_MODE;
518 ctl->server.netsec = (void *)optarg;
520 fprintf(stderr, _("fetchmail: network security support is disabled\n"));
522 #endif /* NET_SECURITY */
525 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
528 interface_parse(optarg, &ctl->server);
532 ctl->server.monitor = xstrdup(optarg);
534 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
536 ctl->server.plugin = xstrdup(optarg);
539 ctl->server.plugout = xstrdup(optarg);
544 ctl->use_ssl = FLAG_TRUE;
548 ctl->sslkey = xstrdup(optarg);
552 ctl->sslcert = xstrdup(optarg);
563 c = xatoi(optarg, &errflag);
564 ctl->warnings = NUM_VALUE_IN(c);
572 rctl->use_syslog = FLAG_TRUE;
576 rctl->use_syslog = FLAG_FALSE;
586 if (errflag || ocount > 1 || helpflag) {
587 /* squawk if syntax errors were detected */
588 #define P(s) fputs(s, helpflag ? stdout : stderr)
589 P(_("usage: fetchmail [options] [server ...]\n"));
590 P(_(" Options are as follows:\n"));
591 P(_(" -?, --help display this option help\n"));
592 P(_(" -V, --version display version info\n"));
594 P(_(" -c, --check check for messages without fetching\n"));
595 P(_(" -s, --silent work silently\n"));
596 P(_(" -v, --verbose work noisily (diagnostic output)\n"));
597 P(_(" -d, --daemon run as a daemon once per n seconds\n"));
598 P(_(" -N, --nodetach don't detach daemon process\n"));
599 P(_(" -q, --quit kill daemon process\n"));
600 P(_(" -L, --logfile specify logfile name\n"));
601 P(_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
602 P(_(" --invisible don't write Received & enable host spoofing\n"));
603 P(_(" -f, --fetchmailrc specify alternate run control file\n"));
604 P(_(" -i, --idfile specify alternate UIDs file\n"));
605 P(_(" --postmaster specify recipient of last resort\n"));
606 P(_(" --nobounce redirect bounces from user to postmaster.\n"));
607 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
608 P(_(" -I, --interface interface required specification\n"));
609 P(_(" -M, --monitor monitor interface for activity\n"));
611 #if defined( SSL_ENABLE )
612 P(_(" --ssl enable ssl encrypted session\n"));
613 P(_(" --sslkey ssl private key file\n"));
614 P(_(" --sslcert ssl client certificate\n"));
616 P(_(" --plugin specify external command to open connection\n"));
617 P(_(" --plugout specify external command to open smtp connection\n"));
619 P(_(" -p, --protocol specify retrieval protocol (see man page)\n"));
620 P(_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
621 P(_(" -P, --port TCP/IP service port to connect to\n"));
622 P(_(" --preauth preauthentication type (password/kerberos/ssh)\n"));
623 P(_(" -t, --timeout server nonresponse timeout\n"));
624 P(_(" -E, --envelope envelope address header\n"));
625 P(_(" -Q, --qvirtual prefix to remove from local user id\n"));
627 P(_(" -u, --username specify users's login on server\n"));
628 P(_(" -a, --all retrieve old and new messages\n"));
629 P(_(" -K, --nokeep delete new messages after retrieval\n"));
630 P(_(" -k, --keep save new messages after retrieval\n"));
631 P(_(" -F, --flush delete old messages from server\n"));
632 P(_(" -n, --norewrite don't rewrite header addresses\n"));
633 P(_(" -l, --limit don't fetch messages over given size\n"));
634 P(_(" -w, --warnings interval between warning mail notification\n"));
637 P(_(" -T, --netsec set IP security request\n"));
638 #endif /* NET_SECURITY */
639 P(_(" -S, --smtphost set SMTP forwarding host\n"));
640 P(_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
641 P(_(" --smtpname set SMTP full name username@domain\n"));
642 P(_(" -Z, --antispam, set antispam response values\n"));
643 P(_(" -b, --batchlimit set batch limit for SMTP connections\n"));
644 P(_(" -B, --fetchlimit set fetch limit for server connections\n"));
645 P(_(" -e, --expunge set max deletions between expunges\n"));
646 P(_(" -m, --mda set MDA to use for forwarding\n"));
647 P(_(" --bsmtp set output BSMTP file\n"));
648 P(_(" --lmtp use LMTP (RFC2033) for delivery\n"));
649 P(_(" -r, --folder specify remote folder name\n"));
661 /* options.c ends here */