2 * options.c -- command-line option processing
4 * For license terms, see the file COPYING in this directory.
10 #if defined(HAVE_ALLOCA_H)
20 #if defined(STDC_HEADERS)
28 #include "fetchmail.h"
39 #define LA_INVISIBLE 10
41 #define LA_NOSYSLOG 12
44 #define LA_POSTMASTER 15
45 #define LA_PROTOCOL 16
48 #define LA_AUTHENTICATE 19
50 #define LA_ENVELOPE 21
51 #define LA_QVIRTUAL 22
52 #define LA_USERNAME 23
57 #define LA_NOREWRITE 28
59 #define LA_WARNINGS 30
61 #define LA_SMTPHOST 32
62 #define LA_SMTPADDR 33
63 #define LA_ANTISPAM 34
64 #define LA_BATCHLIMIT 35
65 #define LA_FETCHLIMIT 36
69 #define LA_INTERFACE 40
71 #define LA_CONFIGDUMP 42
74 /* options still left: CgGhHjJoORwWxXYz */
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 },
96 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
97 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
98 {"uidl", no_argument, (int *) 0, LA_UIDL },
99 {"port", required_argument, (int *) 0, LA_PORT },
100 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
101 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
102 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
103 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
105 {"user", required_argument, (int *) 0, LA_USERNAME },
106 {"username", required_argument, (int *) 0, LA_USERNAME },
108 {"all", no_argument, (int *) 0, LA_ALL },
109 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
110 {"keep", no_argument, (int *) 0, LA_KEEP },
111 {"flush", no_argument, (int *) 0, LA_FLUSH },
112 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
113 {"limit", required_argument, (int *) 0, LA_LIMIT },
114 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
116 {"folder", required_argument, (int *) 0, LA_FOLDER },
117 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
118 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
119 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
121 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
122 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
123 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
124 {"mda", required_argument, (int *) 0, LA_MDA },
127 {"netsec", required_argument, (int *) 0, LA_NETSEC },
130 #if defined(linux) && !INET6
131 {"interface", required_argument, (int *) 0, LA_INTERFACE },
132 {"monitor", required_argument, (int *) 0, LA_MONITOR },
133 #endif /* defined(linux) && !INET6 */
135 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
137 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
139 {(char *) 0, no_argument, (int *) 0, 0 }
142 static int xatoi(char *s, int *errflagptr)
143 /* do safe conversion from string to number */
145 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
146 /* parse and convert numbers, but also check for invalid characters in
155 value = strtol(s, &endptr, 0);
157 /* any invalid chars in string? */
158 if ( (endptr == s) || (*endptr != '\0') ) {
159 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
164 /* is the range valid? */
165 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
166 (value > INT_MAX) || (value < INT_MIN)) {
168 (void) fprintf(stderr, "Value of string '%s' is %s than %d.\n", s,
169 (value < 0) ? "smaller": "larger",
170 (value < 0) ? INT_MIN : INT_MAX);
175 return (int) value; /* shut up, I know what I'm doing */
179 # if defined (STDC_HEADERS)
185 /* We do only base 10 conversions here (atoi)! */
188 /* check for leading white spaces */
189 for (i = 0; (i < len) && isspace(s[i]); i++)
195 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
197 /* skip over digits */
198 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
201 /* check for trailing garbage */
203 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
208 /* atoi should be safe by now, except for number range over/underflow */
213 int parsecmdline (argc, argv, rctl, ctl)
214 /* parse and validate the command line options */
215 int argc; /* argument count */
216 char **argv; /* argument strings */
217 struct runctl *rctl; /* global run controls to modify */
218 struct query *ctl; /* option record to be initialized */
221 * return value: if positive, argv index of last parsed option + 1
222 * (presumes one or more server names follows). if zero, the
223 * command line switches are such that no server names are
224 * required (e.g. --version). if negative, the command line is
225 * has one or more syntax errors.
229 int ocount = 0; /* count of destinations specified */
230 int errflag = 0; /* TRUE when a syntax error is detected */
234 rctl->poll_interval = -1;
236 memset(ctl, '\0', sizeof(struct query)); /* start clean */
237 ctl->smtp_socket = -1;
240 (c = getopt_long(argc,argv,shortoptions,
241 longoptions,&option_index)) != -1) {
258 if (outlevel == O_VERBOSE)
261 outlevel = O_VERBOSE;
265 rctl->poll_interval = xatoi(optarg, &errflag);
277 rctl->logfile = optarg;
280 rctl->invisible = TRUE;
284 rcfile = (char *) xmalloc(strlen(optarg)+1);
285 strcpy(rcfile,optarg);
289 rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
290 strcpy(rctl->idfile,optarg);
293 rctl->postmaster = (char *) xmalloc(strlen(optarg)+1);
297 /* XXX -- should probably use a table lookup here */
298 if (strcasecmp(optarg,"pop2") == 0)
299 ctl->server.protocol = P_POP2;
301 else if (strcasecmp(optarg,"sdps") == 0)
303 ctl->server.protocol = P_POP3;
304 ctl->server.sdps = TRUE;
306 #endif /* SDPS_ENABLE */
307 else if (strcasecmp(optarg,"pop3") == 0)
308 ctl->server.protocol = P_POP3;
309 else if (strcasecmp(optarg,"apop") == 0)
310 ctl->server.protocol = P_APOP;
311 else if (strcasecmp(optarg,"rpop") == 0)
312 ctl->server.protocol = P_RPOP;
313 else if (strcasecmp(optarg,"kpop") == 0)
315 ctl->server.protocol = P_POP3;
317 ctl->server.service = KPOP_PORT;
319 ctl->server.port = KPOP_PORT;
322 ctl->server.preauthenticate = A_KERBEROS_V5;
324 ctl->server.preauthenticate = A_KERBEROS_V4;
325 #endif /* KERBEROS_V5 */
327 else if (strcasecmp(optarg,"imap") == 0)
328 ctl->server.protocol = P_IMAP;
330 else if (strcasecmp(optarg,"imap-k4") == 0)
331 ctl->server.protocol = P_IMAP_K4;
332 #endif /* KERBEROS_V4 */
334 else if (strcasecmp(optarg, "imap-gss") == 0)
335 ctl->server.protocol = P_IMAP_GSS;
337 else if (strcasecmp(optarg,"etrn") == 0)
338 ctl->server.protocol = P_ETRN;
340 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
346 ctl->server.uidl = FLAG_TRUE;
351 ctl->server.service = optarg;
353 ctl->server.port = xatoi(optarg, &errflag);
357 case LA_AUTHENTICATE:
358 if (strcmp(optarg, "password") == 0)
359 ctl->server.preauthenticate = A_PASSWORD;
360 else if (strcmp(optarg, "kerberos") == 0)
362 ctl->server.preauthenticate = A_KERBEROS_V5;
363 else if (strcmp(optarg, "kerberos_v5") == 0)
364 ctl->server.preauthenticate = A_KERBEROS_V5;
366 ctl->server.preauthenticate = A_KERBEROS_V4;
367 else if (strcmp(optarg, "kerberos_v4") == 0)
368 ctl->server.preauthenticate = A_KERBEROS_V4;
369 #endif /* KERBEROS_V5 */
371 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
377 ctl->server.timeout = xatoi(optarg, &errflag);
378 if (ctl->server.timeout == 0)
379 ctl->server.timeout = -1;
383 ctl->server.envelope = xstrdup(optarg);
387 ctl->server.qvirtual = xstrdup(optarg);
392 ctl->remotename = xstrdup(optarg);
396 ctl->fetchall = FLAG_TRUE;
400 ctl->keep = FLAG_FALSE;
404 ctl->keep = FLAG_TRUE;
408 ctl->flush = FLAG_TRUE;
412 ctl->rewrite = FLAG_FALSE;
416 c = xatoi(optarg, &errflag);
417 ctl->limit = NUM_VALUE(c);
421 buf = alloca(strlen(optarg));
423 cp = strtok(buf, ",");
425 save_str(&ctl->mailboxes, cp, 0);
427 ((cp = strtok((char *)NULL, ",")));
431 buf = alloca(strlen(optarg));
433 cp = strtok(buf, ",");
435 save_str(&ctl->smtphunt, cp, TRUE);
437 ((cp = strtok((char *)NULL, ",")));
442 ctl->smtpaddress = xstrdup(optarg);
446 buf = alloca(strlen(optarg));
448 cp = strtok(buf, ",");
450 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
452 idp->val.status.num = atoi(cp);
454 ((cp = strtok((char *)NULL, ",")));
458 c = xatoi(optarg, &errflag);
459 ctl->batchlimit = NUM_VALUE(c);
463 c = xatoi(optarg, &errflag);
464 ctl->fetchlimit = NUM_VALUE(c);
468 c = xatoi(optarg, &errflag);
469 ctl->expunge = NUM_VALUE(c);
473 ctl->mda = xstrdup(optarg);
480 ctl->server.netsec = (void *)optarg;
482 fprintf(stderr, "fetchmail: network security support is disabled\n");
484 #endif /* NET_SECURITY */
487 #if defined(linux) && !INET6
490 interface_parse(optarg, &ctl->server);
494 ctl->server.monitor = xstrdup(optarg);
496 #endif /* defined(linux) && !INET6 */
505 c = xatoi(optarg, &errflag);
506 ctl->warnings = NUM_VALUE(c);
514 rctl->use_syslog = FLAG_TRUE;
518 rctl->use_syslog = FLAG_FALSE;
528 if (errflag || ocount > 1) {
529 /* squawk if syntax errors were detected */
530 #define P(s) fputs(s, stderr)
531 P("usage: fetchmail [options] [server ...]\n");
532 P(" Options are as follows:\n");
533 P(" -?, --help display this option help\n");
534 P(" -V, --version display version info\n");
536 P(" -c, --check check for messages without fetching\n");
537 P(" -s, --silent work silently\n");
538 P(" -v, --verbose work noisily (diagnostic output)\n");
539 P(" -d, --daemon run as a daemon once per n seconds\n");
540 P(" -N, --nodetach don't detach daemon process\n");
541 P(" -q, --quit kill daemon process\n");
542 P(" -L, --logfile specify logfile name\n");
543 P(" --syslog use syslog(3) for most messages when running as a daemon\n");
544 P(" --invisible don't write Received & enable host spoofing\n");
545 P(" -f, --fetchmailrc specify alternate run control file\n");
546 P(" -i, --idfile specify alternate UIDs file\n");
547 P(" --postmaster specify recipient of last resort\n");
548 #if defined(linux) && !INET6
549 P(" -I, --interface interface required specification\n");
550 P(" -M, --monitor monitor interface for activity\n");
553 P(" -p, --protocol specify retrieval protocol (see man page)\n");
554 P(" -U, --uidl force the use of UIDLs (pop3 only)\n");
555 P(" -P, --port TCP/IP service port to connect to\n");
556 P(" -A, --auth authentication type (password or kerberos)\n");
557 P(" -t, --timeout server nonresponse timeout\n");
558 P(" -E, --envelope envelope address header\n");
559 P(" -Q, --qvirtual prefix to remove from local user id\n");
561 P(" -u, --username specify users's login on server\n");
562 P(" -a, --all retrieve old and new messages\n");
563 P(" -K, --nokeep delete new messages after retrieval\n");
564 P(" -k, --keep save new messages after retrieval\n");
565 P(" -F, --flush delete old messages from server\n");
566 P(" -n, --norewrite don't rewrite header addresses\n");
567 P(" -l, --limit don't fetch messages over given size\n");
568 P(" -w, --warnings interval between warning mail notification\n");
571 P(" -T, --netsec set IP security request\n");
572 #endif /* NET_SECURITY */
573 P(" -S, --smtphost set SMTP forwarding host\n");
574 P(" -D, --smtpaddress set SMTP delivery domain to use\n");
575 P(" -Z, --antispam, set antispam response values\n");
576 P(" -b, --batchlimit set batch limit for SMTP connections\n");
577 P(" -B, --fetchlimit set fetch limit for server connections\n");
578 P(" -e, --expunge set max deletions between expunges\n");
579 P(" -r, --folder specify remote folder name\n");
587 /* options.c ends here */