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_PROTOCOL 16
43 #define LA_AUTHENTICATE 19
45 #define LA_ENVELOPE 21
46 #define LA_QVIRTUAL 22
47 #define LA_USERNAME 23
52 #define LA_NOREWRITE 28
54 #define LA_WARNINGS 30
56 #define LA_SMTPHOST 32
57 #define LA_SMTPADDR 33
58 #define LA_ANTISPAM 34
59 #define LA_BATCHLIMIT 35
60 #define LA_FETCHLIMIT 36
68 #define LA_INTERFACE 44
70 #define LA_CONFIGDUMP 46
73 /* options still left: CDgGhHjJoORwWxXYz */
74 static const char *shortoptions =
75 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:";
77 static const struct option longoptions[] = {
78 /* this can be const because all flag fields are 0 and will never get set */
79 {"help", no_argument, (int *) 0, LA_HELP },
80 {"version", no_argument, (int *) 0, LA_VERSION },
81 {"check", no_argument, (int *) 0, LA_CHECK },
82 {"silent", no_argument, (int *) 0, LA_SILENT },
83 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
84 {"daemon", required_argument, (int *) 0, LA_DAEMON },
85 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
86 {"quit", no_argument, (int *) 0, LA_QUIT },
87 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
88 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
89 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
90 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
91 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
92 {"idfile", required_argument, (int *) 0, LA_IDFILE },
93 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
95 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
96 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
97 {"uidl", no_argument, (int *) 0, LA_UIDL },
98 {"port", required_argument, (int *) 0, LA_PORT },
99 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
100 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
101 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
102 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
104 {"user", required_argument, (int *) 0, LA_USERNAME },
105 {"username", required_argument, (int *) 0, LA_USERNAME },
107 {"all", no_argument, (int *) 0, LA_ALL },
108 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
109 {"keep", no_argument, (int *) 0, LA_KEEP },
110 {"flush", no_argument, (int *) 0, LA_FLUSH },
111 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
112 {"limit", required_argument, (int *) 0, LA_LIMIT },
113 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
115 {"folder", required_argument, (int *) 0, LA_FOLDER },
116 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
117 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
118 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
120 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
121 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
122 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
123 {"mda", required_argument, (int *) 0, LA_MDA },
124 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
125 {"lmtp", no_argument, (int *) 0, LA_LMTP },
128 {"netsec", required_argument, (int *) 0, LA_NETSEC },
131 #if (defined(linux) && !INET6) || defined(__FreeBSD__)
132 {"interface", required_argument, (int *) 0, LA_INTERFACE },
133 {"monitor", required_argument, (int *) 0, LA_MONITOR },
134 #endif /* (defined(linux) && !INET6) || defined(__FreeBSD__) */
135 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
136 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
138 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
140 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
142 {(char *) 0, no_argument, (int *) 0, 0 }
145 static int xatoi(char *s, int *errflagptr)
146 /* do safe conversion from string to number */
148 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
149 /* parse and convert numbers, but also check for invalid characters in
158 value = strtol(s, &endptr, 0);
160 /* any invalid chars in string? */
161 if ( (endptr == s) || (*endptr != '\0') ) {
162 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
167 /* is the range valid? */
168 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
169 (value > INT_MAX) || (value < INT_MIN)) {
171 (void) fprintf(stderr, _("Value of string '%s' is %s than %d.\n"), s,
172 (value < 0) ? _("smaller"): _("larger"),
173 (value < 0) ? INT_MIN : INT_MAX);
178 return (int) value; /* shut up, I know what I'm doing */
182 # if defined (STDC_HEADERS)
188 /* We do only base 10 conversions here (atoi)! */
191 /* check for leading white spaces */
192 for (i = 0; (i < len) && isspace(s[i]); i++)
198 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
200 /* skip over digits */
201 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
204 /* check for trailing garbage */
206 (void) fprintf(stderr, _("String '%s' is not a valid number string.\n"), s);
211 /* atoi should be safe by now, except for number range over/underflow */
216 int parsecmdline (argc, argv, rctl, ctl)
217 /* parse and validate the command line options */
218 int argc; /* argument count */
219 char **argv; /* argument strings */
220 struct runctl *rctl; /* global run controls to modify */
221 struct query *ctl; /* option record to be initialized */
224 * return value: if positive, argv index of last parsed option + 1
225 * (presumes one or more server names follows). if zero, the
226 * command line switches are such that no server names are
227 * required (e.g. --version). if negative, the command line is
228 * has one or more syntax errors.
232 int ocount = 0; /* count of destinations specified */
233 int errflag = 0; /* TRUE when a syntax error is detected */
234 int helpflag = 0; /* TRUE when option help was explicitly requested */
238 rctl->poll_interval = -1;
240 memset(ctl, '\0', sizeof(struct query)); /* start clean */
241 ctl->smtp_socket = -1;
244 (c = getopt_long(argc,argv,shortoptions,
245 longoptions,&option_index)) != -1) {
262 if (outlevel == O_VERBOSE)
265 outlevel = O_VERBOSE;
269 rctl->poll_interval = xatoi(optarg, &errflag);
281 rctl->logfile = optarg;
284 rctl->invisible = TRUE;
288 rcfile = (char *) xmalloc(strlen(optarg)+1);
289 strcpy(rcfile,optarg);
293 rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
294 strcpy(rctl->idfile,optarg);
297 rctl->postmaster = (char *) xmalloc(strlen(optarg)+1);
298 strcpy(rctl->postmaster,optarg);
302 /* XXX -- should probably use a table lookup here */
303 if (strcasecmp(optarg,"auto") == 0)
304 ctl->server.protocol = P_AUTO;
305 else if (strcasecmp(optarg,"pop2") == 0)
306 ctl->server.protocol = P_POP2;
308 else if (strcasecmp(optarg,"sdps") == 0)
310 ctl->server.protocol = P_POP3;
311 ctl->server.sdps = TRUE;
313 #endif /* SDPS_ENABLE */
314 else if (strcasecmp(optarg,"pop3") == 0)
315 ctl->server.protocol = P_POP3;
316 else if (strcasecmp(optarg,"apop") == 0)
317 ctl->server.protocol = P_APOP;
318 else if (strcasecmp(optarg,"rpop") == 0)
319 ctl->server.protocol = P_RPOP;
320 else if (strcasecmp(optarg,"kpop") == 0)
322 ctl->server.protocol = P_POP3;
324 ctl->server.service = KPOP_PORT;
326 ctl->server.port = KPOP_PORT;
329 ctl->server.preauthenticate = A_KERBEROS_V5;
331 ctl->server.preauthenticate = A_KERBEROS_V4;
332 #endif /* KERBEROS_V5 */
334 else if (strcasecmp(optarg,"imap") == 0)
335 ctl->server.protocol = P_IMAP;
337 else if (strcasecmp(optarg,"imap-k4") == 0)
338 ctl->server.protocol = P_IMAP_K4;
339 #endif /* KERBEROS_V4 */
341 else if (strcasecmp(optarg, "imap-gss") == 0)
342 ctl->server.protocol = P_IMAP_GSS;
344 else if (strcasecmp(optarg,"etrn") == 0)
345 ctl->server.protocol = P_ETRN;
347 fprintf(stderr,_("Invalid protocol `%s' specified.\n"), optarg);
353 ctl->server.uidl = FLAG_TRUE;
358 ctl->server.service = optarg;
360 ctl->server.port = xatoi(optarg, &errflag);
364 case LA_AUTHENTICATE:
365 if (strcmp(optarg, "password") == 0)
366 ctl->server.preauthenticate = A_PASSWORD;
367 else if (strcmp(optarg, "kerberos") == 0)
369 ctl->server.preauthenticate = A_KERBEROS_V5;
371 ctl->server.preauthenticate = A_KERBEROS_V4;
372 #endif /* KERBEROS_V5 */
373 else if (strcmp(optarg, "kerberos_v5") == 0)
374 ctl->server.preauthenticate = A_KERBEROS_V5;
375 else if (strcmp(optarg, "kerberos_v4") == 0)
376 ctl->server.preauthenticate = A_KERBEROS_V4;
378 fprintf(stderr,_("Invalid preauthentication `%s' specified.\n"), optarg);
384 ctl->server.timeout = xatoi(optarg, &errflag);
385 if (ctl->server.timeout == 0)
386 ctl->server.timeout = -1;
390 ctl->server.envelope = xstrdup(optarg);
394 ctl->server.qvirtual = xstrdup(optarg);
399 ctl->remotename = xstrdup(optarg);
403 ctl->fetchall = FLAG_TRUE;
407 ctl->keep = FLAG_FALSE;
411 ctl->keep = FLAG_TRUE;
415 ctl->flush = FLAG_TRUE;
419 ctl->rewrite = FLAG_FALSE;
423 c = xatoi(optarg, &errflag);
424 ctl->limit = NUM_VALUE(c);
428 xalloca(buf, char *, strlen(optarg));
430 cp = strtok(buf, ",");
432 save_str(&ctl->mailboxes, cp, 0);
434 ((cp = strtok((char *)NULL, ",")));
438 xalloca(buf, char *, strlen(optarg));
440 cp = strtok(buf, ",");
442 save_str(&ctl->smtphunt, cp, TRUE);
444 ((cp = strtok((char *)NULL, ",")));
449 ctl->smtpaddress = xstrdup(optarg);
453 xalloca(buf, char *, strlen(optarg));
455 cp = strtok(buf, ",");
457 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
459 idp->val.status.num = atoi(cp);
461 ((cp = strtok((char *)NULL, ",")));
465 c = xatoi(optarg, &errflag);
466 ctl->batchlimit = NUM_VALUE(c);
470 c = xatoi(optarg, &errflag);
471 ctl->fetchlimit = NUM_VALUE(c);
475 c = xatoi(optarg, &errflag);
476 ctl->expunge = NUM_VALUE(c);
480 ctl->mda = xstrdup(optarg);
484 ctl->bsmtp = xstrdup(optarg);
488 ctl->listener = LMTP_MODE;
494 ctl->server.netsec = (void *)optarg;
496 fprintf(stderr, _("fetchmail: network security support is disabled\n"));
498 #endif /* NET_SECURITY */
501 #if (defined(linux) && !INET6) || defined(__FreeBSD__)
504 interface_parse(optarg, &ctl->server);
508 ctl->server.monitor = xstrdup(optarg);
510 #endif /* (defined(linux) && !INET6) || defined(__FreeBSD__) */
512 ctl->server.plugin = xstrdup(optarg);
515 ctl->server.plugout = xstrdup(optarg);
525 c = xatoi(optarg, &errflag);
526 ctl->warnings = NUM_VALUE(c);
534 rctl->use_syslog = FLAG_TRUE;
538 rctl->use_syslog = FLAG_FALSE;
548 if (errflag || ocount > 1 || helpflag) {
549 /* squawk if syntax errors were detected */
550 #define P(s) fputs(s, helpflag ? stdout : stderr)
551 P(_("usage: fetchmail [options] [server ...]\n"));
552 P(_(" Options are as follows:\n"));
553 P(_(" -?, --help display this option help\n"));
554 P(_(" -V, --version display version info\n"));
556 P(_(" -c, --check check for messages without fetching\n"));
557 P(_(" -s, --silent work silently\n"));
558 P(_(" -v, --verbose work noisily (diagnostic output)\n"));
559 P(_(" -d, --daemon run as a daemon once per n seconds\n"));
560 P(_(" -N, --nodetach don't detach daemon process\n"));
561 P(_(" -q, --quit kill daemon process\n"));
562 P(_(" -L, --logfile specify logfile name\n"));
563 P(_(" --syslog use syslog(3) for most messages when running as a daemon\n"));
564 P(_(" --invisible don't write Received & enable host spoofing\n"));
565 P(_(" -f, --fetchmailrc specify alternate run control file\n"));
566 P(_(" -i, --idfile specify alternate UIDs file\n"));
567 P(_(" --postmaster specify recipient of last resort\n"));
568 #if (defined(linux) && !INET6) || defined(__FreeBSD__)
569 P(_(" -I, --interface interface required specification\n"));
570 P(_(" -M, --monitor monitor interface for activity\n"));
572 P(_(" --plugin specify external command to open connection\n"));
573 P(_(" --plugout specify external command to open smtp connection\n"));
575 P(_(" -p, --protocol specify retrieval protocol (see man page)\n"));
576 P(_(" -U, --uidl force the use of UIDLs (pop3 only)\n"));
577 P(_(" -P, --port TCP/IP service port to connect to\n"));
578 P(_(" -A, --auth authentication type (password or kerberos)\n"));
579 P(_(" -t, --timeout server nonresponse timeout\n"));
580 P(_(" -E, --envelope envelope address header\n"));
581 P(_(" -Q, --qvirtual prefix to remove from local user id\n"));
583 P(_(" -u, --username specify users's login on server\n"));
584 P(_(" -a, --all retrieve old and new messages\n"));
585 P(_(" -K, --nokeep delete new messages after retrieval\n"));
586 P(_(" -k, --keep save new messages after retrieval\n"));
587 P(_(" -F, --flush delete old messages from server\n"));
588 P(_(" -n, --norewrite don't rewrite header addresses\n"));
589 P(_(" -l, --limit don't fetch messages over given size\n"));
590 P(_(" -w, --warnings interval between warning mail notification\n"));
593 P(_(" -T, --netsec set IP security request\n"));
594 #endif /* NET_SECURITY */
595 P(_(" -S, --smtphost set SMTP forwarding host\n"));
596 P(_(" -D, --smtpaddress set SMTP delivery domain to use\n"));
597 P(_(" -Z, --antispam, set antispam response values\n"));
598 P(_(" -b, --batchlimit set batch limit for SMTP connections\n"));
599 P(_(" -B, --fetchlimit set fetch limit for server connections\n"));
600 P(_(" -e, --expunge set max deletions between expunges\n"));
601 P(_(" --mda set MDA to use for forwarding\n"));
602 P(_(" --bsmtp set output BSMTP file\n"));
603 P(_(" --lmtp use LMTP (RFC2033) for delivery\n"));
604 P(_(" -r, --folder specify remote folder name\n"));
616 /* options.c ends here */