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
83 /* options still left: CDgGhHjJoORwWxXYz */
84 static const char *shortoptions =
85 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:";
87 static const struct option longoptions[] = {
88 /* this can be const because all flag fields are 0 and will never get set */
89 {"help", no_argument, (int *) 0, LA_HELP },
90 {"version", no_argument, (int *) 0, LA_VERSION },
91 {"check", no_argument, (int *) 0, LA_CHECK },
92 {"silent", no_argument, (int *) 0, LA_SILENT },
93 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
94 {"daemon", required_argument, (int *) 0, LA_DAEMON },
95 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
96 {"quit", no_argument, (int *) 0, LA_QUIT },
97 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
98 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
99 {"showdots", no_argument, (int *) 0, LA_SHOWDOTS },
100 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
101 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
102 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
103 {"idfile", required_argument, (int *) 0, LA_IDFILE },
104 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
105 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
107 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
108 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
109 {"uidl", no_argument, (int *) 0, LA_UIDL },
110 {"port", required_argument, (int *) 0, LA_PORT },
111 {"preauth", required_argument, (int *) 0, LA_PREAUTH},
112 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
113 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
114 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
116 {"user", required_argument, (int *) 0, LA_USERNAME },
117 {"username", required_argument, (int *) 0, LA_USERNAME },
119 {"all", no_argument, (int *) 0, LA_ALL },
120 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
121 {"keep", no_argument, (int *) 0, LA_KEEP },
122 {"flush", no_argument, (int *) 0, LA_FLUSH },
123 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
124 {"limit", required_argument, (int *) 0, LA_LIMIT },
125 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
127 {"folder", required_argument, (int *) 0, LA_FOLDER },
128 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
129 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
130 {"smtpname", required_argument, (int *) 0, LA_SMTPNAME },
131 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
133 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
134 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
135 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
136 {"mda", required_argument, (int *) 0, LA_MDA },
137 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
138 {"lmtp", no_argument, (int *) 0, LA_LMTP },
141 {"netsec", required_argument, (int *) 0, LA_NETSEC },
142 #endif /* INET6_ENABLE */
145 {"ssl", no_argument, (int *) 0, LA_SSL },
146 {"sslkey", required_argument, (int *) 0, LA_SSLKEY },
147 {"sslcert", required_argument, (int *) 0, LA_SSLCERT },
150 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
151 {"interface", required_argument, (int *) 0, LA_INTERFACE },
152 {"monitor", required_argument, (int *) 0, LA_MONITOR },
153 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
154 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
155 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
157 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
159 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
161 {(char *) 0, no_argument, (int *) 0, 0 }
164 static int xatoi(char *s, int *errflagptr)
165 /* do safe conversion from string to number */
167 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
168 /* parse and convert numbers, but also check for invalid characters in
177 value = strtol(s, &endptr, 0);
179 /* any invalid chars in string? */
180 if ( (endptr == s) || (*endptr != '\0') ) {
181 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
186 /* is the range valid? */
187 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
188 (value > INT_MAX) || (value < INT_MIN)) {
190 (void) fprintf(stderr, _("Value of string '%s' is %s than %d.\n"), s,
191 (value < 0) ? _("smaller"): _("larger"),
192 (value < 0) ? INT_MIN : INT_MAX);
197 return (int) value; /* shut up, I know what I'm doing */
201 # if defined (STDC_HEADERS)
207 /* We do only base 10 conversions here (atoi)! */
210 /* check for leading white spaces */
211 for (i = 0; (i < len) && isspace(s[i]); i++)
217 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
219 /* skip over digits */
220 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
223 /* check for trailing garbage */
225 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
230 /* atoi should be safe by now, except for number range over/underflow */
235 int parsecmdline (argc, argv, rctl, ctl)
236 /* parse and validate the command line options */
237 int argc; /* argument count */
238 char **argv; /* argument strings */
239 struct runctl *rctl; /* global run controls to modify */
240 struct query *ctl; /* option record to be initialized */
243 * return value: if positive, argv index of last parsed option + 1
244 * (presumes one or more server names follows). if zero, the
245 * command line switches are such that no server names are
246 * required (e.g. --version). if negative, the command line is
247 * has one or more syntax errors.
251 int ocount = 0; /* count of destinations specified */
252 int errflag = 0; /* TRUE when a syntax error is detected */
253 int helpflag = 0; /* TRUE when option help was explicitly requested */
257 rctl->poll_interval = -1;
259 memset(ctl, '\0', sizeof(struct query)); /* start clean */
260 ctl->smtp_socket = -1;
263 (c = getopt_long(argc,argv,shortoptions,
264 longoptions,&option_index)) != -1) {
281 if (outlevel == O_VERBOSE)
284 outlevel = O_VERBOSE;
288 rctl->poll_interval = xatoi(optarg, &errflag);
300 rctl->logfile = optarg;
303 rctl->invisible = TRUE;
306 rctl->showdots = TRUE;
310 rcfile = (char *) xstrdup(optarg);
314 rctl->idfile = (char *) xstrdup(optarg);
317 rctl->postmaster = (char *) xstrdup(optarg);
320 run.bouncemail = FALSE;
324 /* XXX -- should probably use a table lookup here */
325 if (strcasecmp(optarg,"auto") == 0)
326 ctl->server.protocol = P_AUTO;
327 else if (strcasecmp(optarg,"pop2") == 0)
328 ctl->server.protocol = P_POP2;
330 else if (strcasecmp(optarg,"sdps") == 0)
332 ctl->server.protocol = P_POP3;
333 ctl->server.sdps = TRUE;
335 #endif /* SDPS_ENABLE */
336 else if (strcasecmp(optarg,"pop3") == 0)
337 ctl->server.protocol = P_POP3;
338 else if (strcasecmp(optarg,"apop") == 0)
339 ctl->server.protocol = P_APOP;
340 else if (strcasecmp(optarg,"rpop") == 0)
341 ctl->server.protocol = P_RPOP;
342 else if (strcasecmp(optarg,"kpop") == 0)
344 ctl->server.protocol = P_POP3;
346 ctl->server.service = KPOP_PORT;
347 #else /* INET6_ENABLE */
348 ctl->server.port = KPOP_PORT;
349 #endif /* INET6_ENABLE */
351 ctl->server.preauthenticate = A_KERBEROS_V5;
353 ctl->server.preauthenticate = A_KERBEROS_V4;
354 #endif /* KERBEROS_V5 */
356 else if (strcasecmp(optarg,"imap") == 0)
357 ctl->server.protocol = P_IMAP;
359 else if (strcasecmp(optarg,"imap-k4") == 0)
360 ctl->server.protocol = P_IMAP_K4;
361 #endif /* KERBEROS_V4 */
363 else if (strcasecmp(optarg, "imap-gss") == 0)
364 ctl->server.protocol = P_IMAP_GSS;
366 else if (strcasecmp(optarg, "imap-crammd5") == 0)
367 ctl->server.protocol = P_IMAP_CRAM_MD5;
368 else if (strcasecmp(optarg, "imap-login") == 0)
369 ctl->server.protocol = P_IMAP_LOGIN;
370 else if (strcasecmp(optarg,"etrn") == 0)
371 ctl->server.protocol = P_ETRN;
373 fprintf(stderr,_("Invalid protocol `%s' specified.\n"), optarg);
379 ctl->server.uidl = FLAG_TRUE;
384 ctl->server.service = optarg;
385 #else /* INET6_ENABLE */
386 ctl->server.port = xatoi(optarg, &errflag);
387 #endif /* INET6_ENABLE */
390 if (strcmp(optarg, "password") == 0)
391 ctl->server.preauthenticate = A_PASSWORD;
392 else if (strcmp(optarg, "kerberos") == 0)
394 ctl->server.preauthenticate = A_KERBEROS_V5;
396 ctl->server.preauthenticate = A_KERBEROS_V4;
397 #endif /* KERBEROS_V5 */
398 else if (strcmp(optarg, "kerberos_v5") == 0)
399 ctl->server.preauthenticate = A_KERBEROS_V5;
400 else if (strcmp(optarg, "kerberos_v4") == 0)
401 ctl->server.preauthenticate = A_KERBEROS_V4;
402 else if (strcmp(optarg, "ssh") == 0)
403 ctl->server.preauthenticate = A_SSH;
405 fprintf(stderr,_("Invalid preauthentication `%s' specified.\n"), optarg);
411 ctl->server.timeout = xatoi(optarg, &errflag);
412 if (ctl->server.timeout == 0)
413 ctl->server.timeout = -1;
417 ctl->server.envelope = xstrdup(optarg);
421 ctl->server.qvirtual = xstrdup(optarg);
426 ctl->remotename = xstrdup(optarg);
430 ctl->fetchall = FLAG_TRUE;
434 ctl->keep = FLAG_FALSE;
438 ctl->keep = FLAG_TRUE;
442 ctl->flush = FLAG_TRUE;
446 ctl->rewrite = FLAG_FALSE;
450 c = xatoi(optarg, &errflag);
451 ctl->limit = NUM_VALUE_IN(c);
455 xalloca(buf, char *, strlen(optarg) + 1);
457 cp = strtok(buf, ",");
459 save_str(&ctl->mailboxes, cp, 0);
461 ((cp = strtok((char *)NULL, ",")));
465 xalloca(buf, char *, strlen(optarg) + 1);
467 cp = strtok(buf, ",");
469 save_str(&ctl->smtphunt, cp, TRUE);
471 ((cp = strtok((char *)NULL, ",")));
476 ctl->smtpaddress = xstrdup(optarg);
479 ctl->smtpname = xstrdup(optarg);
483 xalloca(buf, char *, strlen(optarg) + 1);
485 cp = strtok(buf, ",");
487 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
489 idp->val.status.num = xatoi(cp, &errflag);
491 ((cp = strtok((char *)NULL, ",")));
495 c = xatoi(optarg, &errflag);
496 ctl->batchlimit = NUM_VALUE_IN(c);
500 c = xatoi(optarg, &errflag);
501 ctl->fetchlimit = NUM_VALUE_IN(c);
505 c = xatoi(optarg, &errflag);
506 ctl->expunge = NUM_VALUE_IN(c);
510 ctl->mda = xstrdup(optarg);
514 ctl->bsmtp = xstrdup(optarg);
518 ctl->listener = LMTP_MODE;
524 ctl->server.netsec = (void *)optarg;
526 fprintf(stderr, _("fetchmail: network security support is disabled\n"));
528 #endif /* NET_SECURITY */
531 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
534 interface_parse(optarg, &ctl->server);
538 ctl->server.monitor = xstrdup(optarg);
540 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
542 ctl->server.plugin = xstrdup(optarg);
545 ctl->server.plugout = xstrdup(optarg);
550 ctl->use_ssl = FLAG_TRUE;
554 ctl->sslkey = xstrdup(optarg);
558 ctl->sslcert = xstrdup(optarg);
569 c = xatoi(optarg, &errflag);
570 ctl->warnings = NUM_VALUE_IN(c);
578 rctl->use_syslog = FLAG_TRUE;
582 rctl->use_syslog = FLAG_FALSE;
592 if (errflag || ocount > 1 || helpflag) {
593 /* squawk if syntax errors were detected */
594 #define P(s) fputs(s, helpflag ? stdout : stderr)
595 P(_("usage: fetchmail [options] [server ...]\n"));
596 P(_(" Options are as follows:\n"));
597 P(_(" -?, --help display this option help\n"));
598 P(_(" -V, --version display version info\n"));
600 P(_(" -c, --check check for messages without fetching\n"));
601 P(_(" -s, --silent work silently\n"));
602 P(_(" -v, --verbose work noisily (diagnostic output)\n"));
603 P(_(" -d, --daemon run as a daemon once per n seconds\n"));
604 P(_(" -N, --nodetach don't detach daemon process\n"));
605 P(_(" -q, --quit kill daemon process\n"));
606 P(_(" -L, --logfile specify logfile name\n"));
607 P(_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
608 P(_(" --invisible don't write Received & enable host spoofing\n"));
609 P(_(" -f, --fetchmailrc specify alternate run control file\n"));
610 P(_(" -i, --idfile specify alternate UIDs file\n"));
611 P(_(" --postmaster specify recipient of last resort\n"));
612 P(_(" --nobounce redirect bounces from user to postmaster.\n"));
613 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
614 P(_(" -I, --interface interface required specification\n"));
615 P(_(" -M, --monitor monitor interface for activity\n"));
617 #if defined( SSL_ENABLE )
618 P(_(" --ssl enable ssl encrypted session\n"));
619 P(_(" --sslkey ssl private key file\n"));
620 P(_(" --sslcert ssl client certificate\n"));
622 P(_(" --plugin specify external command to open connection\n"));
623 P(_(" --plugout specify external command to open smtp connection\n"));
625 P(_(" -p, --protocol specify retrieval protocol (see man page)\n"));
626 P(_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
627 P(_(" -P, --port TCP/IP service port to connect to\n"));
628 P(_(" --preauth preauthentication type (password/kerberos/ssh)\n"));
629 P(_(" -t, --timeout server nonresponse timeout\n"));
630 P(_(" -E, --envelope envelope address header\n"));
631 P(_(" -Q, --qvirtual prefix to remove from local user id\n"));
633 P(_(" -u, --username specify users's login on server\n"));
634 P(_(" -a, --all retrieve old and new messages\n"));
635 P(_(" -K, --nokeep delete new messages after retrieval\n"));
636 P(_(" -k, --keep save new messages after retrieval\n"));
637 P(_(" -F, --flush delete old messages from server\n"));
638 P(_(" -n, --norewrite don't rewrite header addresses\n"));
639 P(_(" -l, --limit don't fetch messages over given size\n"));
640 P(_(" -w, --warnings interval between warning mail notification\n"));
643 P(_(" -T, --netsec set IP security request\n"));
644 #endif /* NET_SECURITY */
645 P(_(" -S, --smtphost set SMTP forwarding host\n"));
646 P(_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
647 P(_(" --smtpname set SMTP full name username@domain\n"));
648 P(_(" -Z, --antispam, set antispam response values\n"));
649 P(_(" -b, --batchlimit set batch limit for SMTP connections\n"));
650 P(_(" -B, --fetchlimit set fetch limit for server connections\n"));
651 P(_(" -e, --expunge set max deletions between expunges\n"));
652 P(_(" -m, --mda set MDA to use for forwarding\n"));
653 P(_(" --bsmtp set output BSMTP file\n"));
654 P(_(" --lmtp use LMTP (RFC2033) for delivery\n"));
655 P(_(" -r, --folder specify remote folder name\n"));
656 P(_(" --showdots show progress dots even in logfiles\n"));
668 /* options.c ends here */