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
44 #define LA_AUTHENTICATE 20
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
74 /* options still left: CDgGhHjJoORwWxXYz */
75 static const char *shortoptions =
76 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:";
78 static const struct option longoptions[] = {
79 /* this can be const because all flag fields are 0 and will never get set */
80 {"help", no_argument, (int *) 0, LA_HELP },
81 {"version", no_argument, (int *) 0, LA_VERSION },
82 {"check", no_argument, (int *) 0, LA_CHECK },
83 {"silent", no_argument, (int *) 0, LA_SILENT },
84 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
85 {"daemon", required_argument, (int *) 0, LA_DAEMON },
86 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
87 {"quit", no_argument, (int *) 0, LA_QUIT },
88 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
89 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
90 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
91 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
92 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
93 {"idfile", required_argument, (int *) 0, LA_IDFILE },
94 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
95 {"nobounce", no_argument, (int *) 0, LA_NOBOUNCE },
97 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
98 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
99 {"uidl", no_argument, (int *) 0, LA_UIDL },
100 {"port", required_argument, (int *) 0, LA_PORT },
101 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
102 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
103 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
104 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
106 {"user", required_argument, (int *) 0, LA_USERNAME },
107 {"username", required_argument, (int *) 0, LA_USERNAME },
109 {"all", no_argument, (int *) 0, LA_ALL },
110 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
111 {"keep", no_argument, (int *) 0, LA_KEEP },
112 {"flush", no_argument, (int *) 0, LA_FLUSH },
113 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
114 {"limit", required_argument, (int *) 0, LA_LIMIT },
115 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
117 {"folder", required_argument, (int *) 0, LA_FOLDER },
118 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
119 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
120 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
122 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
123 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
124 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
125 {"mda", required_argument, (int *) 0, LA_MDA },
126 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
127 {"lmtp", no_argument, (int *) 0, LA_LMTP },
130 {"netsec", required_argument, (int *) 0, LA_NETSEC },
133 #if (defined(linux) && !INET6) || defined(__FreeBSD__)
134 {"interface", required_argument, (int *) 0, LA_INTERFACE },
135 {"monitor", required_argument, (int *) 0, LA_MONITOR },
136 #endif /* (defined(linux) && !INET6) || defined(__FreeBSD__) */
137 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
138 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
140 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
142 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
144 {(char *) 0, no_argument, (int *) 0, 0 }
147 static int xatoi(char *s, int *errflagptr)
148 /* do safe conversion from string to number */
150 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
151 /* parse and convert numbers, but also check for invalid characters in
160 value = strtol(s, &endptr, 0);
162 /* any invalid chars in string? */
163 if ( (endptr == s) || (*endptr != '\0') ) {
164 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
169 /* is the range valid? */
170 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
171 (value > INT_MAX) || (value < INT_MIN)) {
173 (void) fprintf(stderr, _("Value of string '%s' is %s than %d.\n"), s,
174 (value < 0) ? _("smaller"): _("larger"),
175 (value < 0) ? INT_MIN : INT_MAX);
180 return (int) value; /* shut up, I know what I'm doing */
184 # if defined (STDC_HEADERS)
190 /* We do only base 10 conversions here (atoi)! */
193 /* check for leading white spaces */
194 for (i = 0; (i < len) && isspace(s[i]); i++)
200 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
202 /* skip over digits */
203 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
206 /* check for trailing garbage */
208 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
213 /* atoi should be safe by now, except for number range over/underflow */
218 int parsecmdline (argc, argv, rctl, ctl)
219 /* parse and validate the command line options */
220 int argc; /* argument count */
221 char **argv; /* argument strings */
222 struct runctl *rctl; /* global run controls to modify */
223 struct query *ctl; /* option record to be initialized */
226 * return value: if positive, argv index of last parsed option + 1
227 * (presumes one or more server names follows). if zero, the
228 * command line switches are such that no server names are
229 * required (e.g. --version). if negative, the command line is
230 * has one or more syntax errors.
234 int ocount = 0; /* count of destinations specified */
235 int errflag = 0; /* TRUE when a syntax error is detected */
236 int helpflag = 0; /* TRUE when option help was explicitly requested */
240 rctl->poll_interval = -1;
242 memset(ctl, '\0', sizeof(struct query)); /* start clean */
243 ctl->smtp_socket = -1;
246 (c = getopt_long(argc,argv,shortoptions,
247 longoptions,&option_index)) != -1) {
264 if (outlevel == O_VERBOSE)
267 outlevel = O_VERBOSE;
271 rctl->poll_interval = xatoi(optarg, &errflag);
283 rctl->logfile = optarg;
286 rctl->invisible = TRUE;
290 rcfile = (char *) xmalloc(strlen(optarg)+1);
291 strcpy(rcfile,optarg);
295 rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
296 strcpy(rctl->idfile,optarg);
299 rctl->postmaster = (char *) xmalloc(strlen(optarg)+1);
300 strcpy(rctl->postmaster,optarg);
303 run.bouncemail = FALSE;
307 /* XXX -- should probably use a table lookup here */
308 if (strcasecmp(optarg,"auto") == 0)
309 ctl->server.protocol = P_AUTO;
310 else if (strcasecmp(optarg,"pop2") == 0)
311 ctl->server.protocol = P_POP2;
313 else if (strcasecmp(optarg,"sdps") == 0)
315 ctl->server.protocol = P_POP3;
316 ctl->server.sdps = TRUE;
318 #endif /* SDPS_ENABLE */
319 else if (strcasecmp(optarg,"pop3") == 0)
320 ctl->server.protocol = P_POP3;
321 else if (strcasecmp(optarg,"apop") == 0)
322 ctl->server.protocol = P_APOP;
323 else if (strcasecmp(optarg,"rpop") == 0)
324 ctl->server.protocol = P_RPOP;
325 else if (strcasecmp(optarg,"kpop") == 0)
327 ctl->server.protocol = P_POP3;
329 ctl->server.service = KPOP_PORT;
331 ctl->server.port = KPOP_PORT;
334 ctl->server.preauthenticate = A_KERBEROS_V5;
336 ctl->server.preauthenticate = A_KERBEROS_V4;
337 #endif /* KERBEROS_V5 */
339 else if (strcasecmp(optarg,"imap") == 0)
340 ctl->server.protocol = P_IMAP;
342 else if (strcasecmp(optarg,"imap-k4") == 0)
343 ctl->server.protocol = P_IMAP_K4;
344 #endif /* KERBEROS_V4 */
346 else if (strcasecmp(optarg, "imap-gss") == 0)
347 ctl->server.protocol = P_IMAP_GSS;
349 else if (strcasecmp(optarg,"etrn") == 0)
350 ctl->server.protocol = P_ETRN;
352 fprintf(stderr,_("Invalid protocol `%s' specified.\n"), optarg);
358 ctl->server.uidl = FLAG_TRUE;
363 ctl->server.service = optarg;
365 ctl->server.port = xatoi(optarg, &errflag);
369 case LA_AUTHENTICATE:
370 if (strcmp(optarg, "password") == 0)
371 ctl->server.preauthenticate = A_PASSWORD;
372 else if (strcmp(optarg, "kerberos") == 0)
374 ctl->server.preauthenticate = A_KERBEROS_V5;
376 ctl->server.preauthenticate = A_KERBEROS_V4;
377 #endif /* KERBEROS_V5 */
378 else if (strcmp(optarg, "kerberos_v5") == 0)
379 ctl->server.preauthenticate = A_KERBEROS_V5;
380 else if (strcmp(optarg, "kerberos_v4") == 0)
381 ctl->server.preauthenticate = A_KERBEROS_V4;
383 fprintf(stderr,_("Invalid preauthentication `%s' specified.\n"), optarg);
389 ctl->server.timeout = xatoi(optarg, &errflag);
390 if (ctl->server.timeout == 0)
391 ctl->server.timeout = -1;
395 ctl->server.envelope = xstrdup(optarg);
399 ctl->server.qvirtual = xstrdup(optarg);
404 ctl->remotename = xstrdup(optarg);
408 ctl->fetchall = FLAG_TRUE;
412 ctl->keep = FLAG_FALSE;
416 ctl->keep = FLAG_TRUE;
420 ctl->flush = FLAG_TRUE;
424 ctl->rewrite = FLAG_FALSE;
428 c = xatoi(optarg, &errflag);
429 ctl->limit = NUM_VALUE_IN(c);
433 xalloca(buf, char *, strlen(optarg) + 1);
435 cp = strtok(buf, ",");
437 save_str(&ctl->mailboxes, cp, 0);
439 ((cp = strtok((char *)NULL, ",")));
443 xalloca(buf, char *, strlen(optarg) + 1);
445 cp = strtok(buf, ",");
447 save_str(&ctl->smtphunt, cp, TRUE);
449 ((cp = strtok((char *)NULL, ",")));
454 ctl->smtpaddress = xstrdup(optarg);
458 xalloca(buf, char *, strlen(optarg) + 1);
460 cp = strtok(buf, ",");
462 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
464 idp->val.status.num = atoi(cp);
466 ((cp = strtok((char *)NULL, ",")));
470 c = xatoi(optarg, &errflag);
471 ctl->batchlimit = NUM_VALUE_IN(c);
475 c = xatoi(optarg, &errflag);
476 ctl->fetchlimit = NUM_VALUE_IN(c);
480 c = xatoi(optarg, &errflag);
481 ctl->expunge = NUM_VALUE_IN(c);
485 ctl->mda = xstrdup(optarg);
489 ctl->bsmtp = xstrdup(optarg);
493 ctl->listener = LMTP_MODE;
499 ctl->server.netsec = (void *)optarg;
501 fprintf(stderr, _("fetchmail: network security support is disabled\n"));
503 #endif /* NET_SECURITY */
506 #if (defined(linux) && !INET6) || defined(__FreeBSD__)
509 interface_parse(optarg, &ctl->server);
513 ctl->server.monitor = xstrdup(optarg);
515 #endif /* (defined(linux) && !INET6) || defined(__FreeBSD__) */
517 ctl->server.plugin = xstrdup(optarg);
520 ctl->server.plugout = xstrdup(optarg);
530 c = xatoi(optarg, &errflag);
531 ctl->warnings = NUM_VALUE_IN(c);
539 rctl->use_syslog = FLAG_TRUE;
543 rctl->use_syslog = FLAG_FALSE;
553 if (errflag || ocount > 1 || helpflag) {
554 /* squawk if syntax errors were detected */
555 #define P(s) fputs(s, helpflag ? stdout : stderr)
556 P(_("usage: fetchmail [options] [server ...]\n"));
557 P(_(" Options are as follows:\n"));
558 P(_(" -?, --help display this option help\n"));
559 P(_(" -V, --version display version info\n"));
561 P(_(" -c, --check check for messages without fetching\n"));
562 P(_(" -s, --silent work silently\n"));
563 P(_(" -v, --verbose work noisily (diagnostic output)\n"));
564 P(_(" -d, --daemon run as a daemon once per n seconds\n"));
565 P(_(" -N, --nodetach don't detach daemon process\n"));
566 P(_(" -q, --quit kill daemon process\n"));
567 P(_(" -L, --logfile specify logfile name\n"));
568 P(_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
569 P(_(" --invisible don't write Received & enable host spoofing\n"));
570 P(_(" -f, --fetchmailrc specify alternate run control file\n"));
571 P(_(" -i, --idfile specify alternate UIDs file\n"));
572 P(_(" --postmaster specify recipient of last resort\n"));
573 P(_(" --nobounce redirect bounces from user to postmaster.\n"));
574 #if (defined(linux) && !INET6) || defined(__FreeBSD__)
575 P(_(" -I, --interface interface required specification\n"));
576 P(_(" -M, --monitor monitor interface for activity\n"));
578 P(_(" --plugin specify external command to open connection\n"));
579 P(_(" --plugout specify external command to open smtp connection\n"));
581 P(_(" -p, --protocol specify retrieval protocol (see man page)\n"));
582 P(_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
583 P(_(" -P, --port TCP/IP service port to connect to\n"));
584 P(_(" -A, --auth authentication type (password or kerberos)\n"));
585 P(_(" -t, --timeout server nonresponse timeout\n"));
586 P(_(" -E, --envelope envelope address header\n"));
587 P(_(" -Q, --qvirtual prefix to remove from local user id\n"));
589 P(_(" -u, --username specify users's login on server\n"));
590 P(_(" -a, --all retrieve old and new messages\n"));
591 P(_(" -K, --nokeep delete new messages after retrieval\n"));
592 P(_(" -k, --keep save new messages after retrieval\n"));
593 P(_(" -F, --flush delete old messages from server\n"));
594 P(_(" -n, --norewrite don't rewrite header addresses\n"));
595 P(_(" -l, --limit don't fetch messages over given size\n"));
596 P(_(" -w, --warnings interval between warning mail notification\n"));
599 P(_(" -T, --netsec set IP security request\n"));
600 #endif /* NET_SECURITY */
601 P(_(" -S, --smtphost set SMTP forwarding host\n"));
602 P(_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
603 P(_(" -Z, --antispam, set antispam response values\n"));
604 P(_(" -b, --batchlimit set batch limit for SMTP connections\n"));
605 P(_(" -B, --fetchlimit set fetch limit for server connections\n"));
606 P(_(" -e, --expunge set max deletions between expunges\n"));
607 P(_(" --mda set MDA to use for forwarding\n"));
608 P(_(" --bsmtp set output BSMTP file\n"));
609 P(_(" --lmtp use LMTP (RFC2033) for delivery\n"));
610 P(_(" -r, --folder specify remote folder name\n"));
622 /* options.c ends here */