2 * options.c -- command-line option processing
4 * For license terms, see the file COPYING in this directory.
13 #if defined(STDC_HEADERS)
21 #include "fetchmail.h"
32 #define LA_INVISIBLE 10
34 #define LA_NOSYSLOG 12
37 #define LA_POSTMASTER 15
38 #define LA_PROTOCOL 16
41 #define LA_AUTHENTICATE 19
43 #define LA_ENVELOPE 21
44 #define LA_QVIRTUAL 22
45 #define LA_USERNAME 23
50 #define LA_NOREWRITE 28
52 #define LA_WARNINGS 30
54 #define LA_SMTPHOST 32
55 #define LA_SMTPADDR 33
56 #define LA_ANTISPAM 34
57 #define LA_BATCHLIMIT 35
58 #define LA_FETCHLIMIT 36
66 #define LA_INTERFACE 44
68 #define LA_CONFIGDUMP 46
71 /* options still left: CDgGhHjJoORwWxXYz */
72 static const char *shortoptions =
73 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:";
75 static const struct option longoptions[] = {
76 /* this can be const because all flag fields are 0 and will never get set */
77 {"help", no_argument, (int *) 0, LA_HELP },
78 {"version", no_argument, (int *) 0, LA_VERSION },
79 {"check", no_argument, (int *) 0, LA_CHECK },
80 {"silent", no_argument, (int *) 0, LA_SILENT },
81 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
82 {"daemon", required_argument, (int *) 0, LA_DAEMON },
83 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
84 {"quit", no_argument, (int *) 0, LA_QUIT },
85 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
86 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
87 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
88 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
89 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
90 {"idfile", required_argument, (int *) 0, LA_IDFILE },
91 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
93 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
94 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
95 {"uidl", no_argument, (int *) 0, LA_UIDL },
96 {"port", required_argument, (int *) 0, LA_PORT },
97 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
98 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
99 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
100 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
102 {"user", required_argument, (int *) 0, LA_USERNAME },
103 {"username", required_argument, (int *) 0, LA_USERNAME },
105 {"all", no_argument, (int *) 0, LA_ALL },
106 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
107 {"keep", no_argument, (int *) 0, LA_KEEP },
108 {"flush", no_argument, (int *) 0, LA_FLUSH },
109 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
110 {"limit", required_argument, (int *) 0, LA_LIMIT },
111 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
113 {"folder", required_argument, (int *) 0, LA_FOLDER },
114 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
115 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
116 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
118 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
119 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
120 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
121 {"mda", required_argument, (int *) 0, LA_MDA },
122 {"bsmtp", required_argument, (int *) 0, LA_BSMTP },
123 {"lmtp", no_argument, (int *) 0, LA_LMTP },
126 {"netsec", required_argument, (int *) 0, LA_NETSEC },
129 #if defined(linux) && !INET6
130 {"interface", required_argument, (int *) 0, LA_INTERFACE },
131 {"monitor", required_argument, (int *) 0, LA_MONITOR },
132 #endif /* defined(linux) && !INET6 */
133 {"plugin", required_argument, (int *) 0, LA_PLUGIN },
134 {"plugout", required_argument, (int *) 0, LA_PLUGOUT },
136 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
138 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
140 {(char *) 0, no_argument, (int *) 0, 0 }
143 static int xatoi(char *s, int *errflagptr)
144 /* do safe conversion from string to number */
146 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
147 /* parse and convert numbers, but also check for invalid characters in
156 value = strtol(s, &endptr, 0);
158 /* any invalid chars in string? */
159 if ( (endptr == s) || (*endptr != '\0') ) {
160 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
165 /* is the range valid? */
166 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
167 (value > INT_MAX) || (value < INT_MIN)) {
169 (void) fprintf(stderr, "Value of string '%s' is %s than %d.\n", s,
170 (value < 0) ? "smaller": "larger",
171 (value < 0) ? INT_MIN : INT_MAX);
176 return (int) value; /* shut up, I know what I'm doing */
180 # if defined (STDC_HEADERS)
186 /* We do only base 10 conversions here (atoi)! */
189 /* check for leading white spaces */
190 for (i = 0; (i < len) && isspace(s[i]); i++)
196 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
198 /* skip over digits */
199 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
202 /* check for trailing garbage */
204 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
209 /* atoi should be safe by now, except for number range over/underflow */
214 int parsecmdline (argc, argv, rctl, ctl)
215 /* parse and validate the command line options */
216 int argc; /* argument count */
217 char **argv; /* argument strings */
218 struct runctl *rctl; /* global run controls to modify */
219 struct query *ctl; /* option record to be initialized */
222 * return value: if positive, argv index of last parsed option + 1
223 * (presumes one or more server names follows). if zero, the
224 * command line switches are such that no server names are
225 * required (e.g. --version). if negative, the command line is
226 * has one or more syntax errors.
230 int ocount = 0; /* count of destinations specified */
231 int errflag = 0; /* TRUE when a syntax error is detected */
235 rctl->poll_interval = -1;
237 memset(ctl, '\0', sizeof(struct query)); /* start clean */
238 ctl->smtp_socket = -1;
241 (c = getopt_long(argc,argv,shortoptions,
242 longoptions,&option_index)) != -1) {
259 if (outlevel == O_VERBOSE)
262 outlevel = O_VERBOSE;
266 rctl->poll_interval = xatoi(optarg, &errflag);
278 rctl->logfile = optarg;
281 rctl->invisible = TRUE;
285 rcfile = (char *) xmalloc(strlen(optarg)+1);
286 strcpy(rcfile,optarg);
290 rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
291 strcpy(rctl->idfile,optarg);
294 rctl->postmaster = (char *) xmalloc(strlen(optarg)+1);
295 strcpy(rctl->postmaster,optarg);
299 /* XXX -- should probably use a table lookup here */
300 if (strcasecmp(optarg,"auto") == 0)
301 ctl->server.protocol = P_AUTO;
302 else if (strcasecmp(optarg,"pop2") == 0)
303 ctl->server.protocol = P_POP2;
305 else if (strcasecmp(optarg,"sdps") == 0)
307 ctl->server.protocol = P_POP3;
308 ctl->server.sdps = TRUE;
310 #endif /* SDPS_ENABLE */
311 else if (strcasecmp(optarg,"pop3") == 0)
312 ctl->server.protocol = P_POP3;
313 else if (strcasecmp(optarg,"apop") == 0)
314 ctl->server.protocol = P_APOP;
315 else if (strcasecmp(optarg,"rpop") == 0)
316 ctl->server.protocol = P_RPOP;
317 else if (strcasecmp(optarg,"kpop") == 0)
319 ctl->server.protocol = P_POP3;
321 ctl->server.service = KPOP_PORT;
323 ctl->server.port = KPOP_PORT;
326 ctl->server.preauthenticate = A_KERBEROS_V5;
328 ctl->server.preauthenticate = A_KERBEROS_V4;
329 #endif /* KERBEROS_V5 */
331 else if (strcasecmp(optarg,"imap") == 0)
332 ctl->server.protocol = P_IMAP;
334 else if (strcasecmp(optarg,"imap-k4") == 0)
335 ctl->server.protocol = P_IMAP_K4;
336 #endif /* KERBEROS_V4 */
338 else if (strcasecmp(optarg, "imap-gss") == 0)
339 ctl->server.protocol = P_IMAP_GSS;
341 else if (strcasecmp(optarg,"etrn") == 0)
342 ctl->server.protocol = P_ETRN;
344 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
350 ctl->server.uidl = FLAG_TRUE;
355 ctl->server.service = optarg;
357 ctl->server.port = xatoi(optarg, &errflag);
361 case LA_AUTHENTICATE:
362 if (strcmp(optarg, "password") == 0)
363 ctl->server.preauthenticate = A_PASSWORD;
364 else if (strcmp(optarg, "kerberos") == 0)
366 ctl->server.preauthenticate = A_KERBEROS_V5;
368 ctl->server.preauthenticate = A_KERBEROS_V4;
369 #endif /* KERBEROS_V5 */
370 else if (strcmp(optarg, "kerberos_v5") == 0)
371 ctl->server.preauthenticate = A_KERBEROS_V5;
372 else if (strcmp(optarg, "kerberos_v4") == 0)
373 ctl->server.preauthenticate = A_KERBEROS_V4;
375 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
381 ctl->server.timeout = xatoi(optarg, &errflag);
382 if (ctl->server.timeout == 0)
383 ctl->server.timeout = -1;
387 ctl->server.envelope = xstrdup(optarg);
391 ctl->server.qvirtual = xstrdup(optarg);
396 ctl->remotename = xstrdup(optarg);
400 ctl->fetchall = FLAG_TRUE;
404 ctl->keep = FLAG_FALSE;
408 ctl->keep = FLAG_TRUE;
412 ctl->flush = FLAG_TRUE;
416 ctl->rewrite = FLAG_FALSE;
420 c = xatoi(optarg, &errflag);
421 ctl->limit = NUM_VALUE(c);
425 xalloca(buf, char *, strlen(optarg));
427 cp = strtok(buf, ",");
429 save_str(&ctl->mailboxes, cp, 0);
431 ((cp = strtok((char *)NULL, ",")));
435 xalloca(buf, char *, strlen(optarg));
437 cp = strtok(buf, ",");
439 save_str(&ctl->smtphunt, cp, TRUE);
441 ((cp = strtok((char *)NULL, ",")));
446 ctl->smtpaddress = xstrdup(optarg);
450 xalloca(buf, char *, strlen(optarg));
452 cp = strtok(buf, ",");
454 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
456 idp->val.status.num = atoi(cp);
458 ((cp = strtok((char *)NULL, ",")));
462 c = xatoi(optarg, &errflag);
463 ctl->batchlimit = NUM_VALUE(c);
467 c = xatoi(optarg, &errflag);
468 ctl->fetchlimit = NUM_VALUE(c);
472 c = xatoi(optarg, &errflag);
473 ctl->expunge = NUM_VALUE(c);
477 ctl->mda = xstrdup(optarg);
481 ctl->bsmtp = xstrdup(optarg);
485 ctl->listener = LMTP_MODE;
491 ctl->server.netsec = (void *)optarg;
493 fprintf(stderr, "fetchmail: network security support is disabled\n");
495 #endif /* NET_SECURITY */
498 #if defined(linux) && !INET6
501 interface_parse(optarg, &ctl->server);
505 ctl->server.monitor = xstrdup(optarg);
507 #endif /* defined(linux) && !INET6 */
509 ctl->server.plugin = xstrdup(optarg);
512 ctl->server.plugout = xstrdup(optarg);
522 c = xatoi(optarg, &errflag);
523 ctl->warnings = NUM_VALUE(c);
531 rctl->use_syslog = FLAG_TRUE;
535 rctl->use_syslog = FLAG_FALSE;
545 if (errflag || ocount > 1) {
546 /* squawk if syntax errors were detected */
547 #define P(s) fputs(s, stderr)
548 P("usage: fetchmail [options] [server ...]\n");
549 P(" Options are as follows:\n");
550 P(" -?, --help display this option help\n");
551 P(" -V, --version display version info\n");
553 P(" -c, --check check for messages without fetching\n");
554 P(" -s, --silent work silently\n");
555 P(" -v, --verbose work noisily (diagnostic output)\n");
556 P(" -d, --daemon run as a daemon once per n seconds\n");
557 P(" -N, --nodetach don't detach daemon process\n");
558 P(" -q, --quit kill daemon process\n");
559 P(" -L, --logfile specify logfile name\n");
560 P(" --syslog use syslog(3) for most messages when running as a daemon\n");
561 P(" --invisible don't write Received & enable host spoofing\n");
562 P(" -f, --fetchmailrc specify alternate run control file\n");
563 P(" -i, --idfile specify alternate UIDs file\n");
564 P(" --postmaster specify recipient of last resort\n");
565 #if defined(linux) && !INET6
566 P(" -I, --interface interface required specification\n");
567 P(" -M, --monitor monitor interface for activity\n");
569 P(" --plugin specify external command to open connection\n");
570 P(" --plugout specify external command to open smtp connection\n");
572 P(" -p, --protocol specify retrieval protocol (see man page)\n");
573 P(" -U, --uidl force the use of UIDLs (pop3 only)\n");
574 P(" -P, --port TCP/IP service port to connect to\n");
575 P(" -A, --auth authentication type (password or kerberos)\n");
576 P(" -t, --timeout server nonresponse timeout\n");
577 P(" -E, --envelope envelope address header\n");
578 P(" -Q, --qvirtual prefix to remove from local user id\n");
580 P(" -u, --username specify users's login on server\n");
581 P(" -a, --all retrieve old and new messages\n");
582 P(" -K, --nokeep delete new messages after retrieval\n");
583 P(" -k, --keep save new messages after retrieval\n");
584 P(" -F, --flush delete old messages from server\n");
585 P(" -n, --norewrite don't rewrite header addresses\n");
586 P(" -l, --limit don't fetch messages over given size\n");
587 P(" -w, --warnings interval between warning mail notification\n");
590 P(" -T, --netsec set IP security request\n");
591 #endif /* NET_SECURITY */
592 P(" -S, --smtphost set SMTP forwarding host\n");
593 P(" -D, --smtpaddress set SMTP delivery domain to use\n");
594 P(" -Z, --antispam, set antispam response values\n");
595 P(" -b, --batchlimit set batch limit for SMTP connections\n");
596 P(" -B, --fetchlimit set fetch limit for server connections\n");
597 P(" -e, --expunge set max deletions between expunges\n");
598 P(" --mda set MDA to use for forwarding\n");
599 P(" --bsmtp set output BSMTP file\n");
600 P(" --lmtp use LMTP (RFC2033) for delivery\n");
601 P(" -r, --folder specify remote folder name\n");
609 /* options.c ends here */