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
62 #define LA_INTERFACE 40
64 #define LA_CONFIGDUMP 42
67 /* options still left: CgGhHjJoORwWxXYz */
68 static const char *shortoptions =
69 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:yw:";
71 static const struct option longoptions[] = {
72 /* this can be const because all flag fields are 0 and will never get set */
73 {"help", no_argument, (int *) 0, LA_HELP },
74 {"version", no_argument, (int *) 0, LA_VERSION },
75 {"check", no_argument, (int *) 0, LA_CHECK },
76 {"silent", no_argument, (int *) 0, LA_SILENT },
77 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
78 {"daemon", required_argument, (int *) 0, LA_DAEMON },
79 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
80 {"quit", no_argument, (int *) 0, LA_QUIT },
81 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
82 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
83 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
84 {"nosyslog", no_argument, (int *) 0, LA_NOSYSLOG },
85 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
86 {"idfile", required_argument, (int *) 0, LA_IDFILE },
87 {"postmaster",required_argument, (int *) 0, LA_POSTMASTER },
89 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
90 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
91 {"uidl", no_argument, (int *) 0, LA_UIDL },
92 {"port", required_argument, (int *) 0, LA_PORT },
93 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
94 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
95 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
96 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
98 {"user", required_argument, (int *) 0, LA_USERNAME },
99 {"username", required_argument, (int *) 0, LA_USERNAME },
101 {"all", no_argument, (int *) 0, LA_ALL },
102 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
103 {"keep", no_argument, (int *) 0, LA_KEEP },
104 {"flush", no_argument, (int *) 0, LA_FLUSH },
105 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
106 {"limit", required_argument, (int *) 0, LA_LIMIT },
107 {"warnings", required_argument, (int *) 0, LA_WARNINGS },
109 {"folder", required_argument, (int *) 0, LA_FOLDER },
110 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
111 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
112 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
114 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
115 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
116 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
117 {"mda", required_argument, (int *) 0, LA_MDA },
120 {"netsec", required_argument, (int *) 0, LA_NETSEC },
123 #if defined(linux) && !INET6
124 {"interface", required_argument, (int *) 0, LA_INTERFACE },
125 {"monitor", required_argument, (int *) 0, LA_MONITOR },
126 #endif /* defined(linux) && !INET6 */
128 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
130 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
132 {(char *) 0, no_argument, (int *) 0, 0 }
135 static int xatoi(char *s, int *errflagptr)
136 /* do safe conversion from string to number */
138 #if defined (STDC_HEADERS) && defined (LONG_MAX) && defined (INT_MAX)
139 /* parse and convert numbers, but also check for invalid characters in
148 value = strtol(s, &endptr, 0);
150 /* any invalid chars in string? */
151 if ( (endptr == s) || (*endptr != '\0') ) {
152 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
157 /* is the range valid? */
158 if ( (((value == LONG_MAX) || (value == LONG_MIN)) && (errno == ERANGE)) ||
159 (value > INT_MAX) || (value < INT_MIN)) {
161 (void) fprintf(stderr, "Value of string '%s' is %s than %d.\n", s,
162 (value < 0) ? "smaller": "larger",
163 (value < 0) ? INT_MIN : INT_MAX);
168 return (int) value; /* shut up, I know what I'm doing */
172 # if defined (STDC_HEADERS)
178 /* We do only base 10 conversions here (atoi)! */
181 /* check for leading white spaces */
182 for (i = 0; (i < len) && isspace(s[i]); i++)
188 if (i < len && (s[i] == '+' || s[i] == '-')) i++;
190 /* skip over digits */
191 for ( /* no init */ ; (i < len) && isdigit(s[i]); i++)
194 /* check for trailing garbage */
196 (void) fprintf(stderr, "String '%s' is not a valid number string.\n", s);
201 /* atoi should be safe by now, except for number range over/underflow */
206 int parsecmdline (argc, argv, rctl, ctl)
207 /* parse and validate the command line options */
208 int argc; /* argument count */
209 char **argv; /* argument strings */
210 struct runctl *rctl; /* global run controls to modify */
211 struct query *ctl; /* option record to be initialized */
214 * return value: if positive, argv index of last parsed option + 1
215 * (presumes one or more server names follows). if zero, the
216 * command line switches are such that no server names are
217 * required (e.g. --version). if negative, the command line is
218 * has one or more syntax errors.
222 int ocount = 0; /* count of destinations specified */
223 int errflag = 0; /* TRUE when a syntax error is detected */
227 rctl->poll_interval = -1;
229 memset(ctl, '\0', sizeof(struct query)); /* start clean */
230 ctl->smtp_socket = -1;
233 (c = getopt_long(argc,argv,shortoptions,
234 longoptions,&option_index)) != -1) {
251 if (outlevel == O_VERBOSE)
254 outlevel = O_VERBOSE;
258 rctl->poll_interval = xatoi(optarg, &errflag);
270 rctl->logfile = optarg;
273 rctl->invisible = TRUE;
277 rcfile = (char *) xmalloc(strlen(optarg)+1);
278 strcpy(rcfile,optarg);
282 rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
283 strcpy(rctl->idfile,optarg);
286 rctl->postmaster = (char *) xmalloc(strlen(optarg)+1);
290 /* XXX -- should probably use a table lookup here */
291 if (strcasecmp(optarg,"pop2") == 0)
292 ctl->server.protocol = P_POP2;
294 else if (strcasecmp(optarg,"sdps") == 0)
296 ctl->server.protocol = P_POP3;
297 ctl->server.sdps = TRUE;
299 #endif /* SDPS_ENABLE */
300 else if (strcasecmp(optarg,"pop3") == 0)
301 ctl->server.protocol = P_POP3;
302 else if (strcasecmp(optarg,"apop") == 0)
303 ctl->server.protocol = P_APOP;
304 else if (strcasecmp(optarg,"rpop") == 0)
305 ctl->server.protocol = P_RPOP;
306 else if (strcasecmp(optarg,"kpop") == 0)
308 ctl->server.protocol = P_POP3;
310 ctl->server.service = KPOP_PORT;
312 ctl->server.port = KPOP_PORT;
315 ctl->server.preauthenticate = A_KERBEROS_V5;
317 ctl->server.preauthenticate = A_KERBEROS_V4;
318 #endif /* KERBEROS_V5 */
320 else if (strcasecmp(optarg,"imap") == 0)
321 ctl->server.protocol = P_IMAP;
323 else if (strcasecmp(optarg,"imap-k4") == 0)
324 ctl->server.protocol = P_IMAP_K4;
325 #endif /* KERBEROS_V4 */
327 else if (strcasecmp(optarg, "imap-gss") == 0)
328 ctl->server.protocol = P_IMAP_GSS;
330 else if (strcasecmp(optarg,"etrn") == 0)
331 ctl->server.protocol = P_ETRN;
333 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
339 ctl->server.uidl = FLAG_TRUE;
344 ctl->server.service = optarg;
346 ctl->server.port = xatoi(optarg, &errflag);
350 case LA_AUTHENTICATE:
351 if (strcmp(optarg, "password") == 0)
352 ctl->server.preauthenticate = A_PASSWORD;
353 else if (strcmp(optarg, "kerberos") == 0)
355 ctl->server.preauthenticate = A_KERBEROS_V5;
356 else if (strcmp(optarg, "kerberos_v5") == 0)
357 ctl->server.preauthenticate = A_KERBEROS_V5;
359 ctl->server.preauthenticate = A_KERBEROS_V4;
360 else if (strcmp(optarg, "kerberos_v4") == 0)
361 ctl->server.preauthenticate = A_KERBEROS_V4;
362 #endif /* KERBEROS_V5 */
364 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
370 ctl->server.timeout = xatoi(optarg, &errflag);
371 if (ctl->server.timeout == 0)
372 ctl->server.timeout = -1;
376 ctl->server.envelope = xstrdup(optarg);
380 ctl->server.qvirtual = xstrdup(optarg);
385 ctl->remotename = xstrdup(optarg);
389 ctl->fetchall = FLAG_TRUE;
393 ctl->keep = FLAG_FALSE;
397 ctl->keep = FLAG_TRUE;
401 ctl->flush = FLAG_TRUE;
405 ctl->rewrite = FLAG_FALSE;
409 c = xatoi(optarg, &errflag);
410 ctl->limit = NUM_VALUE(c);
414 xalloca(buf, char *, strlen(optarg));
416 cp = strtok(buf, ",");
418 save_str(&ctl->mailboxes, cp, 0);
420 ((cp = strtok((char *)NULL, ",")));
424 xalloca(buf, char *, strlen(optarg));
426 cp = strtok(buf, ",");
428 save_str(&ctl->smtphunt, cp, TRUE);
430 ((cp = strtok((char *)NULL, ",")));
435 ctl->smtpaddress = xstrdup(optarg);
439 xalloca(buf, char *, strlen(optarg));
441 cp = strtok(buf, ",");
443 struct idlist *idp = save_str(&ctl->antispam, NULL, 0);;
445 idp->val.status.num = atoi(cp);
447 ((cp = strtok((char *)NULL, ",")));
451 c = xatoi(optarg, &errflag);
452 ctl->batchlimit = NUM_VALUE(c);
456 c = xatoi(optarg, &errflag);
457 ctl->fetchlimit = NUM_VALUE(c);
461 c = xatoi(optarg, &errflag);
462 ctl->expunge = NUM_VALUE(c);
466 ctl->mda = xstrdup(optarg);
473 ctl->server.netsec = (void *)optarg;
475 fprintf(stderr, "fetchmail: network security support is disabled\n");
477 #endif /* NET_SECURITY */
480 #if defined(linux) && !INET6
483 interface_parse(optarg, &ctl->server);
487 ctl->server.monitor = xstrdup(optarg);
489 #endif /* defined(linux) && !INET6 */
498 c = xatoi(optarg, &errflag);
499 ctl->warnings = NUM_VALUE(c);
507 rctl->use_syslog = FLAG_TRUE;
511 rctl->use_syslog = FLAG_FALSE;
521 if (errflag || ocount > 1) {
522 /* squawk if syntax errors were detected */
523 #define P(s) fputs(s, stderr)
524 P("usage: fetchmail [options] [server ...]\n");
525 P(" Options are as follows:\n");
526 P(" -?, --help display this option help\n");
527 P(" -V, --version display version info\n");
529 P(" -c, --check check for messages without fetching\n");
530 P(" -s, --silent work silently\n");
531 P(" -v, --verbose work noisily (diagnostic output)\n");
532 P(" -d, --daemon run as a daemon once per n seconds\n");
533 P(" -N, --nodetach don't detach daemon process\n");
534 P(" -q, --quit kill daemon process\n");
535 P(" -L, --logfile specify logfile name\n");
536 P(" --syslog use syslog(3) for most messages when running as a daemon\n");
537 P(" --invisible don't write Received & enable host spoofing\n");
538 P(" -f, --fetchmailrc specify alternate run control file\n");
539 P(" -i, --idfile specify alternate UIDs file\n");
540 P(" --postmaster specify recipient of last resort\n");
541 #if defined(linux) && !INET6
542 P(" -I, --interface interface required specification\n");
543 P(" -M, --monitor monitor interface for activity\n");
546 P(" -p, --protocol specify retrieval protocol (see man page)\n");
547 P(" -U, --uidl force the use of UIDLs (pop3 only)\n");
548 P(" -P, --port TCP/IP service port to connect to\n");
549 P(" -A, --auth authentication type (password or kerberos)\n");
550 P(" -t, --timeout server nonresponse timeout\n");
551 P(" -E, --envelope envelope address header\n");
552 P(" -Q, --qvirtual prefix to remove from local user id\n");
554 P(" -u, --username specify users's login on server\n");
555 P(" -a, --all retrieve old and new messages\n");
556 P(" -K, --nokeep delete new messages after retrieval\n");
557 P(" -k, --keep save new messages after retrieval\n");
558 P(" -F, --flush delete old messages from server\n");
559 P(" -n, --norewrite don't rewrite header addresses\n");
560 P(" -l, --limit don't fetch messages over given size\n");
561 P(" -w, --warnings interval between warning mail notification\n");
564 P(" -T, --netsec set IP security request\n");
565 #endif /* NET_SECURITY */
566 P(" -S, --smtphost set SMTP forwarding host\n");
567 P(" -D, --smtpaddress set SMTP delivery domain to use\n");
568 P(" -Z, --antispam, set antispam response values\n");
569 P(" -b, --batchlimit set batch limit for SMTP connections\n");
570 P(" -B, --fetchlimit set fetch limit for server connections\n");
571 P(" -e, --expunge set max deletions between expunges\n");
572 P(" -r, --folder specify remote folder name\n");
580 /* options.c ends here */