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
36 #define LA_PROTOCOL 14
39 #define LA_AUTHENTICATE 17
41 #define LA_ENVELOPE 19
42 #define LA_QVIRTUAL 20
43 #define LA_USERNAME 21
48 #define LA_NOREWRITE 26
51 #define LA_SMTPHOST 29
52 #define LA_SMTPADDR 30
53 #define LA_ANTISPAM 31
54 #define LA_BATCHLIMIT 32
55 #define LA_FETCHLIMIT 33
59 #define LA_INTERFACE 37
61 #define LA_CONFIGDUMP 39
64 /* options still left: CgGhHjJoORUwWxXYz */
65 static const char *shortoptions =
66 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:y";
68 static const struct option longoptions[] = {
69 /* this can be const because all flag fields are 0 and will never get set */
70 {"help", no_argument, (int *) 0, LA_HELP },
71 {"version", no_argument, (int *) 0, LA_VERSION },
72 {"check", no_argument, (int *) 0, LA_CHECK },
73 {"silent", no_argument, (int *) 0, LA_SILENT },
74 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
75 {"daemon", required_argument, (int *) 0, LA_DAEMON },
76 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
77 {"quit", no_argument, (int *) 0, LA_QUIT },
78 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
79 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
80 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
81 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
82 {"idfile", required_argument, (int *) 0, LA_IDFILE },
84 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
85 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
86 {"uidl", no_argument, (int *) 0, LA_UIDL },
87 {"port", required_argument, (int *) 0, LA_PORT },
88 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
89 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
90 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
91 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
93 {"user", required_argument, (int *) 0, LA_USERNAME },
94 {"username", required_argument, (int *) 0, LA_USERNAME },
96 {"all", no_argument, (int *) 0, LA_ALL },
97 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
98 {"keep", no_argument, (int *) 0, LA_KEEP },
99 {"flush", no_argument, (int *) 0, LA_FLUSH },
100 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
101 {"limit", required_argument, (int *) 0, LA_LIMIT },
103 {"folder", required_argument, (int *) 0, LA_FOLDER },
104 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
105 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
106 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
108 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
109 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
110 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
111 {"mda", required_argument, (int *) 0, LA_MDA },
114 {"netsec", required_argument, (int *) 0, LA_NETSEC },
117 #if defined(linux) && !INET6
118 {"interface", required_argument, (int *) 0, LA_INTERFACE },
119 {"monitor", required_argument, (int *) 0, LA_MONITOR },
120 #endif /* defined(linux) && !INET6 */
122 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
124 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
126 {(char *) 0, no_argument, (int *) 0, 0 }
129 static int xatoi(char *s, int *errflagptr)
130 /* do safe conversion from string to number */
132 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
133 /* parse and convert numbers, but also check for invalid characters in
142 value = strtol(s, &endptr, 0);
144 /* any invalid chars in string? */
145 if ( (endptr == s) || (*endptr != '\0') ) {
146 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
151 /* is the range valid? */
152 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
153 (value > INT_MAX) || (value < INT_MIN)) {
155 (void) fprintf(stderr, "Value of string '%s' is %s than %d.\n", s,
156 (value < 0) ? "smaller": "larger",
157 (value < 0) ? INT_MIN : INT_MAX);
162 return (int) value; /* shut up, I know what I'm doing */
166 # if defined (STDC_HEADERS)
172 /* We do only base 10 conversions here (atoi)! */
175 /* check for leading white spaces */
176 for (i = 0; (i < len) && isspace(s[i]); i++)
182 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
184 /* skip over digits */
185 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
188 /* check for trailing garbage */
190 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
195 /* atoi should be safe by now, except for number range over/underflow */
200 int parsecmdline (argc, argv, rctl, ctl)
201 /* parse and validate the command line options */
202 int argc; /* argument count */
203 char **argv; /* argument strings */
204 struct runctl *rctl; /* global run controls to modify */
205 struct query *ctl; /* option record to be initialized */
208 * return value: if positive, argv index of last parsed option + 1
209 * (presumes one or more server names follows). if zero, the
210 * command line switches are such that no server names are
211 * required (e.g. --version). if negative, the command line is
212 * has one or more syntax errors.
216 int ocount = 0; /* count of destinations specified */
217 int errflag = 0; /* TRUE when a syntax error is detected */
221 rctl->poll_interval = -1;
223 memset(ctl, '\0', sizeof(struct query)); /* start clean */
224 ctl->smtp_socket = -1;
227 (c = getopt_long(argc,argv,shortoptions,
228 longoptions,&option_index)) != -1) {
245 outlevel = O_VERBOSE;
249 rctl->poll_interval = xatoi(optarg, &errflag);
261 rctl->logfile = optarg;
264 rctl->invisible = TRUE;
268 rcfile = (char *) xmalloc(strlen(optarg)+1);
269 strcpy(rcfile,optarg);
273 rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
274 strcpy(rctl->idfile,optarg);
278 /* XXX -- should probably use a table lookup here */
279 if (strcasecmp(optarg,"pop2") == 0)
280 ctl->server.protocol = P_POP2;
281 else if (strcasecmp(optarg,"pop3") == 0)
282 ctl->server.protocol = P_POP3;
283 else if (strcasecmp(optarg,"apop") == 0)
284 ctl->server.protocol = P_APOP;
285 else if (strcasecmp(optarg,"rpop") == 0)
286 ctl->server.protocol = P_RPOP;
287 else if (strcasecmp(optarg,"kpop") == 0)
289 ctl->server.protocol = P_POP3;
291 ctl->server.service = KPOP_PORT;
293 ctl->server.port = KPOP_PORT;
296 ctl->server.preauthenticate = A_KERBEROS_V5;
298 ctl->server.preauthenticate = A_KERBEROS_V4;
299 #endif /* KERBEROS_V5 */
301 else if (strcasecmp(optarg,"imap") == 0)
302 ctl->server.protocol = P_IMAP;
304 else if (strcasecmp(optarg,"imap-k4") == 0)
305 ctl->server.protocol = P_IMAP_K4;
306 #endif /* KERBEROS_V4 */
307 else if (strcasecmp(optarg,"etrn") == 0)
308 ctl->server.protocol = P_ETRN;
310 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
316 ctl->server.uidl = FLAG_TRUE;
321 ctl->server.service = optarg;
323 ctl->server.port = xatoi(optarg, &errflag);
327 case LA_AUTHENTICATE:
328 if (strcmp(optarg, "password") == 0)
329 ctl->server.preauthenticate = A_PASSWORD;
330 else if (strcmp(optarg, "kerberos") == 0)
332 ctl->server.preauthenticate = A_KERBEROS_V5;
333 else if (strcmp(optarg, "kerberos_v5") == 0)
334 ctl->server.preauthenticate = A_KERBEROS_V5;
336 ctl->server.preauthenticate = A_KERBEROS_V4;
337 else if (strcmp(optarg, "kerberos_v4") == 0)
338 ctl->server.preauthenticate = A_KERBEROS_V4;
339 #endif /* KERBEROS_V5 */
341 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
347 ctl->server.timeout = xatoi(optarg, &errflag);
348 if (ctl->server.timeout == 0)
349 ctl->server.timeout = -1;
353 ctl->server.envelope = xstrdup(optarg);
357 ctl->server.qvirtual = xstrdup(optarg);
362 ctl->remotename = xstrdup(optarg);
366 ctl->fetchall = FLAG_TRUE;
370 ctl->keep = FLAG_FALSE;
374 ctl->keep = FLAG_TRUE;
378 ctl->flush = FLAG_TRUE;
382 ctl->rewrite = FLAG_FALSE;
386 c = xatoi(optarg, &errflag);
387 ctl->limit = NUM_VALUE(c);
391 buf = xmalloc(strlen(optarg));
393 cp = strtok(buf, ",");
395 save_str(&ctl->mailboxes, cp, 0);
397 ((cp = strtok((char *)NULL, ",")));
402 buf = xmalloc(strlen(optarg));
404 cp = strtok(buf, ",");
406 save_str(&ctl->smtphunt, cp, TRUE);
408 ((cp = strtok((char *)NULL, ",")));
414 ctl->smtpaddress = xstrdup(optarg);
418 c = xatoi(optarg, &errflag);
419 ctl->antispam = NUM_VALUE(c);
422 c = xatoi(optarg, &errflag);
423 ctl->batchlimit = NUM_VALUE(c);
427 c = xatoi(optarg, &errflag);
428 ctl->fetchlimit = NUM_VALUE(c);
432 c = xatoi(optarg, &errflag);
433 ctl->expunge = NUM_VALUE(c);
437 ctl->mda = xstrdup(optarg);
444 ctl->server.netsec = (void *)optarg;
446 fprintf(stderr, "fetchmail: network security support is disabled\n");
448 #endif /* NET_SECURITY */
451 #if defined(linux) && !INET6
454 interface_parse(optarg, &ctl->server);
458 ctl->server.monitor = xstrdup(optarg);
460 #endif /* defined(linux) && !INET6 */
472 rctl->use_syslog = TRUE;
482 if (errflag || ocount > 1) {
483 /* squawk if syntax errors were detected */
484 fputs("usage: fetchmail [options] [server ...]\n", stderr);
485 fputs(" Options are as follows:\n",stderr);
486 fputs(" -?, --help display this option help\n", stderr);
487 fputs(" -V, --version display version info\n", stderr);
489 fputs(" -c, --check check for messages without fetching\n", stderr);
490 fputs(" -s, --silent work silently\n", stderr);
491 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
492 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
493 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
494 fputs(" -q, --quit kill daemon process\n", stderr);
495 fputs(" -L, --logfile specify logfile name\n", stderr);
496 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
497 fputs(" --invisible suppress Received line & enable host spoofing\n", stderr);
498 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
499 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
500 #if defined(linux) && !INET6
501 fputs(" -I, --interface interface required specification\n",stderr);
502 fputs(" -M, --monitor monitor interface for activity\n",stderr);
506 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn, imap-k4\n", stderr);
508 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
509 #endif /* KERBEROS_V4 */
510 fputs(" -U, --uidl force the use of UIDLs (pop3 only)\n", stderr);
511 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
512 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
513 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
514 fputs(" -E, --envelope envelope address header\n",stderr);
515 fputs(" -Q, --qvirtual prefix to remove from local user id\n",stderr);
517 fputs(" -u, --username specify users's login on server\n", stderr);
518 fputs(" -a, --all retrieve old and new messages\n", stderr);
519 fputs(" -K, --nokeep delete new messages after retrieval\n", stderr);
520 fputs(" -k, --keep save new messages after retrieval\n", stderr);
521 fputs(" -F, --flush delete old messages from server\n", stderr);
522 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
523 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
526 fputs(" -T, --netsec set IP security request\n", stderr);
527 #endif /* NET_SECURITY */
528 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
529 fputs(" -D, --smtpaddress set SMTP delivery domain to use\n", stderr);
530 fputs(" -Z, --antispam, set antispam response value\n", stderr);
531 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
532 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
533 fputs(" -e, --expunge set max deletions between expunges\n", stderr);
534 fputs(" -r, --folder specify remote folder name\n", stderr);
541 /* options.c ends here */