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_PROTOCOL 15
40 #define LA_AUTHENTICATE 18
42 #define LA_ENVELOPE 20
43 #define LA_QVIRTUAL 21
44 #define LA_USERNAME 22
49 #define LA_NOREWRITE 27
52 #define LA_SMTPHOST 30
53 #define LA_SMTPADDR 31
54 #define LA_ANTISPAM 32
55 #define LA_BATCHLIMIT 33
56 #define LA_FETCHLIMIT 34
60 #define LA_INTERFACE 38
62 #define LA_CONFIGDUMP 40
65 /* options still left: CgGhHjJoORUwWxXYz */
66 static const char *shortoptions =
67 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:y";
69 static const struct option longoptions[] = {
70 /* this can be const because all flag fields are 0 and will never get set */
71 {"help", no_argument, (int *) 0, LA_HELP },
72 {"version", no_argument, (int *) 0, LA_VERSION },
73 {"check", no_argument, (int *) 0, LA_CHECK },
74 {"silent", no_argument, (int *) 0, LA_SILENT },
75 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
76 {"daemon", required_argument, (int *) 0, LA_DAEMON },
77 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
78 {"quit", no_argument, (int *) 0, LA_QUIT },
79 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
80 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
81 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
82 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
83 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
84 {"idfile", required_argument, (int *) 0, LA_IDFILE },
86 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
87 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
88 {"uidl", no_argument, (int *) 0, LA_UIDL },
89 {"port", required_argument, (int *) 0, LA_PORT },
90 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
91 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
92 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
93 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
95 {"user", required_argument, (int *) 0, LA_USERNAME },
96 {"username", required_argument, (int *) 0, LA_USERNAME },
98 {"all", no_argument, (int *) 0, LA_ALL },
99 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
100 {"keep", no_argument, (int *) 0, LA_KEEP },
101 {"flush", no_argument, (int *) 0, LA_FLUSH },
102 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
103 {"limit", required_argument, (int *) 0, LA_LIMIT },
105 {"folder", required_argument, (int *) 0, LA_FOLDER },
106 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
107 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
108 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
110 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
111 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
112 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
113 {"mda", required_argument, (int *) 0, LA_MDA },
116 {"netsec", required_argument, (int *) 0, LA_NETSEC },
119 #if defined(linux) && !INET6
120 {"interface", required_argument, (int *) 0, LA_INTERFACE },
121 {"monitor", required_argument, (int *) 0, LA_MONITOR },
122 #endif /* defined(linux) && !INET6 */
124 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
126 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
128 {(char *) 0, no_argument, (int *) 0, 0 }
131 static int xatoi(char *s, int *errflagptr)
132 /* do safe conversion from string to number */
134 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
135 /* parse and convert numbers, but also check for invalid characters in
144 value = strtol(s, &endptr, 0);
146 /* any invalid chars in string? */
147 if ( (endptr == s) || (*endptr != '\0') ) {
148 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
153 /* is the range valid? */
154 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
155 (value > INT_MAX) || (value < INT_MIN)) {
157 (void) fprintf(stderr, "Value of string '%s' is %s than %d.\n", s,
158 (value < 0) ? "smaller": "larger",
159 (value < 0) ? INT_MIN : INT_MAX);
164 return (int) value; /* shut up, I know what I'm doing */
168 # if defined (STDC_HEADERS)
174 /* We do only base 10 conversions here (atoi)! */
177 /* check for leading white spaces */
178 for (i = 0; (i < len) && isspace(s[i]); i++)
184 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
186 /* skip over digits */
187 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
190 /* check for trailing garbage */
192 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
197 /* atoi should be safe by now, except for number range over/underflow */
202 int parsecmdline (argc, argv, rctl, ctl)
203 /* parse and validate the command line options */
204 int argc; /* argument count */
205 char **argv; /* argument strings */
206 struct runctl *rctl; /* global run controls to modify */
207 struct query *ctl; /* option record to be initialized */
210 * return value: if positive, argv index of last parsed option + 1
211 * (presumes one or more server names follows). if zero, the
212 * command line switches are such that no server names are
213 * required (e.g. --version). if negative, the command line is
214 * has one or more syntax errors.
218 int ocount = 0; /* count of destinations specified */
219 int errflag = 0; /* TRUE when a syntax error is detected */
223 rctl->poll_interval = -1;
225 memset(ctl, '\0', sizeof(struct query)); /* start clean */
226 ctl->smtp_socket = -1;
229 (c = getopt_long(argc,argv,shortoptions,
230 longoptions,&option_index)) != -1) {
247 outlevel = O_VERBOSE;
251 rctl->poll_interval = xatoi(optarg, &errflag);
263 rctl->logfile = optarg;
266 rctl->invisible = TRUE;
270 rcfile = (char *) xmalloc(strlen(optarg)+1);
271 strcpy(rcfile,optarg);
275 rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
276 strcpy(rctl->idfile,optarg);
280 /* XXX -- should probably use a table lookup here */
281 if (strcasecmp(optarg,"pop2") == 0)
282 ctl->server.protocol = P_POP2;
283 else if (strcasecmp(optarg,"pop3") == 0)
284 ctl->server.protocol = P_POP3;
285 else if (strcasecmp(optarg,"apop") == 0)
286 ctl->server.protocol = P_APOP;
287 else if (strcasecmp(optarg,"rpop") == 0)
288 ctl->server.protocol = P_RPOP;
289 else if (strcasecmp(optarg,"kpop") == 0)
291 ctl->server.protocol = P_POP3;
293 ctl->server.service = KPOP_PORT;
295 ctl->server.port = KPOP_PORT;
298 ctl->server.preauthenticate = A_KERBEROS_V5;
300 ctl->server.preauthenticate = A_KERBEROS_V4;
301 #endif /* KERBEROS_V5 */
303 else if (strcasecmp(optarg,"imap") == 0)
304 ctl->server.protocol = P_IMAP;
306 else if (strcasecmp(optarg,"imap-k4") == 0)
307 ctl->server.protocol = P_IMAP_K4;
308 #endif /* KERBEROS_V4 */
310 else if (strcasecmp(optarg, "imap-gss") == 0)
311 ctl->server.protocol = P_IMAP_GSS;
313 else if (strcasecmp(optarg,"etrn") == 0)
314 ctl->server.protocol = P_ETRN;
316 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
322 ctl->server.uidl = FLAG_TRUE;
327 ctl->server.service = optarg;
329 ctl->server.port = xatoi(optarg, &errflag);
333 case LA_AUTHENTICATE:
334 if (strcmp(optarg, "password") == 0)
335 ctl->server.preauthenticate = A_PASSWORD;
336 else if (strcmp(optarg, "kerberos") == 0)
338 ctl->server.preauthenticate = A_KERBEROS_V5;
339 else if (strcmp(optarg, "kerberos_v5") == 0)
340 ctl->server.preauthenticate = A_KERBEROS_V5;
342 ctl->server.preauthenticate = A_KERBEROS_V4;
343 else if (strcmp(optarg, "kerberos_v4") == 0)
344 ctl->server.preauthenticate = A_KERBEROS_V4;
345 #endif /* KERBEROS_V5 */
347 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
353 ctl->server.timeout = xatoi(optarg, &errflag);
354 if (ctl->server.timeout == 0)
355 ctl->server.timeout = -1;
359 ctl->server.envelope = xstrdup(optarg);
363 ctl->server.qvirtual = xstrdup(optarg);
368 ctl->remotename = xstrdup(optarg);
372 ctl->fetchall = FLAG_TRUE;
376 ctl->keep = FLAG_FALSE;
380 ctl->keep = FLAG_TRUE;
384 ctl->flush = FLAG_TRUE;
388 ctl->rewrite = FLAG_FALSE;
392 c = xatoi(optarg, &errflag);
393 ctl->limit = NUM_VALUE(c);
397 buf = xmalloc(strlen(optarg));
399 cp = strtok(buf, ",");
401 save_str(&ctl->mailboxes, cp, 0);
403 ((cp = strtok((char *)NULL, ",")));
408 buf = xmalloc(strlen(optarg));
410 cp = strtok(buf, ",");
412 save_str(&ctl->smtphunt, cp, TRUE);
414 ((cp = strtok((char *)NULL, ",")));
420 ctl->smtpaddress = xstrdup(optarg);
424 c = xatoi(optarg, &errflag);
425 ctl->antispam = NUM_VALUE(c);
428 c = xatoi(optarg, &errflag);
429 ctl->batchlimit = NUM_VALUE(c);
433 c = xatoi(optarg, &errflag);
434 ctl->fetchlimit = NUM_VALUE(c);
438 c = xatoi(optarg, &errflag);
439 ctl->expunge = NUM_VALUE(c);
443 ctl->mda = xstrdup(optarg);
450 ctl->server.netsec = (void *)optarg;
452 fprintf(stderr, "fetchmail: network security support is disabled\n");
454 #endif /* NET_SECURITY */
457 #if defined(linux) && !INET6
460 interface_parse(optarg, &ctl->server);
464 ctl->server.monitor = xstrdup(optarg);
466 #endif /* defined(linux) && !INET6 */
478 rctl->use_syslog = FLAG_TRUE;
482 rctl->use_syslog = FLAG_FALSE;
492 if (errflag || ocount > 1) {
493 /* squawk if syntax errors were detected */
494 fputs("usage: fetchmail [options] [server ...]\n", stderr);
495 fputs(" Options are as follows:\n",stderr);
496 fputs(" -?, --help display this option help\n", stderr);
497 fputs(" -V, --version display version info\n", stderr);
499 fputs(" -c, --check check for messages without fetching\n", stderr);
500 fputs(" -s, --silent work silently\n", stderr);
501 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
502 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
503 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
504 fputs(" -q, --quit kill daemon process\n", stderr);
505 fputs(" -L, --logfile specify logfile name\n", stderr);
506 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
507 fputs(" --invisible suppress Received line & enable host spoofing\n", stderr);
508 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
509 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
510 #if defined(linux) && !INET6
511 fputs(" -I, --interface interface required specification\n",stderr);
512 fputs(" -M, --monitor monitor interface for activity\n",stderr);
516 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn, imap-k4\n", stderr);
518 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
519 #endif /* KERBEROS_V4 */
520 fputs(" -U, --uidl force the use of UIDLs (pop3 only)\n", stderr);
521 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
522 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
523 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
524 fputs(" -E, --envelope envelope address header\n",stderr);
525 fputs(" -Q, --qvirtual prefix to remove from local user id\n",stderr);
527 fputs(" -u, --username specify users's login on server\n", stderr);
528 fputs(" -a, --all retrieve old and new messages\n", stderr);
529 fputs(" -K, --nokeep delete new messages after retrieval\n", stderr);
530 fputs(" -k, --keep save new messages after retrieval\n", stderr);
531 fputs(" -F, --flush delete old messages from server\n", stderr);
532 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
533 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
536 fputs(" -T, --netsec set IP security request\n", stderr);
537 #endif /* NET_SECURITY */
538 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
539 fputs(" -D, --smtpaddress set SMTP delivery domain to use\n", stderr);
540 fputs(" -Z, --antispam, set antispam response value\n", stderr);
541 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
542 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
543 fputs(" -e, --expunge set max deletions between expunges\n", stderr);
544 fputs(" -r, --folder specify remote folder name\n", stderr);
551 /* options.c ends here */