2 * options.c -- command-line option processing
4 * For license terms, see the file COPYING in this directory.
12 #if defined(STDC_HEADERS)
17 #include "fetchmail.h"
28 #define LA_INVISIBLE 10
32 #define LA_PROTOCOL 14
35 #define LA_AUTHENTICATE 17
37 #define LA_ENVELOPE 19
38 #define LA_QVIRTUAL 20
39 #define LA_USERNAME 21
44 #define LA_NOREWRITE 26
47 #define LA_SMTPHOST 29
48 #define LA_SMTPADDR 30
49 #define LA_ANTISPAM 31
50 #define LA_BATCHLIMIT 32
51 #define LA_FETCHLIMIT 33
55 #define LA_INTERFACE 37
59 /* options still left: CgGhHjJoORUwWxXYzZ */
60 static const char *shortoptions =
61 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:b:B:e:m:I:M:y";
63 static const struct option longoptions[] = {
64 /* this can be const because all flag fields are 0 and will never get set */
65 {"help", no_argument, (int *) 0, LA_HELP },
66 {"version", no_argument, (int *) 0, LA_VERSION },
67 {"check", no_argument, (int *) 0, LA_CHECK },
68 {"silent", no_argument, (int *) 0, LA_SILENT },
69 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
70 {"daemon", required_argument, (int *) 0, LA_DAEMON },
71 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
72 {"quit", no_argument, (int *) 0, LA_QUIT },
73 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
74 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
75 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
76 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
77 {"idfile", required_argument, (int *) 0, LA_IDFILE },
79 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
80 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
81 {"uidl", no_argument, (int *) 0, LA_UIDL },
82 {"port", required_argument, (int *) 0, LA_PORT },
83 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
84 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
85 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
86 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
88 {"user", required_argument, (int *) 0, LA_USERNAME },
89 {"username", required_argument, (int *) 0, LA_USERNAME },
91 {"all", no_argument, (int *) 0, LA_ALL },
92 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
93 {"keep", no_argument, (int *) 0, LA_KEEP },
94 {"flush", no_argument, (int *) 0, LA_FLUSH },
95 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
96 {"limit", required_argument, (int *) 0, LA_LIMIT },
98 {"folder", required_argument, (int *) 0, LA_FOLDER },
99 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
100 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
101 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
103 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
104 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
105 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
106 {"mda", required_argument, (int *) 0, LA_MDA },
109 {"netsec", required_argument, (int *) 0, LA_NETSEC },
112 #if defined(linux) && !INET6
113 {"interface", required_argument, (int *) 0, LA_INTERFACE },
114 {"monitor", required_argument, (int *) 0, LA_MONITOR },
115 #endif /* defined(linux) && !INET6 */
117 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
119 {(char *) 0, no_argument, (int *) 0, 0 }
122 int parsecmdline (argc, argv, ctl)
123 /* parse and validate the command line options */
124 int argc; /* argument count */
125 char **argv; /* argument strings */
126 struct query *ctl; /* option record to be initialized */
129 * return value: if positive, argv index of last parsed option + 1
130 * (presumes one or more server names follows). if zero, the
131 * command line switches are such that no server names are
132 * required (e.g. --version). if negative, the command line is
133 * has one or more syntax errors.
137 int ocount = 0; /* count of destinations specified */
138 int errflag = 0; /* TRUE when a syntax error is detected */
140 char buf[BUFSIZ], *cp;
144 memset(ctl, '\0', sizeof(struct query)); /* start clean */
145 ctl->smtp_socket = -1;
148 (c = getopt_long(argc,argv,shortoptions,
149 longoptions,&option_index)) != -1) {
166 outlevel = O_VERBOSE;
170 cmd_daemon = atoi(optarg);
182 cmd_logfile = optarg;
185 use_invisible = TRUE;
189 rcfile = (char *) xmalloc(strlen(optarg)+1);
190 strcpy(rcfile,optarg);
194 idfile = (char *) xmalloc(strlen(optarg)+1);
195 strcpy(idfile,optarg);
199 /* XXX -- should probably use a table lookup here */
200 if (strcasecmp(optarg,"pop2") == 0)
201 ctl->server.protocol = P_POP2;
202 else if (strcasecmp(optarg,"pop3") == 0)
203 ctl->server.protocol = P_POP3;
204 else if (strcasecmp(optarg,"apop") == 0)
205 ctl->server.protocol = P_APOP;
206 else if (strcasecmp(optarg,"rpop") == 0)
207 ctl->server.protocol = P_RPOP;
208 else if (strcasecmp(optarg,"kpop") == 0)
210 ctl->server.protocol = P_POP3;
212 ctl->server.service = KPOP_PORT;
214 ctl->server.port = KPOP_PORT;
217 ctl->server.preauthenticate = A_KERBEROS_V5;
219 ctl->server.preauthenticate = A_KERBEROS_V4;
220 #endif /* KERBEROS_V5 */
222 else if (strcasecmp(optarg,"imap") == 0)
223 ctl->server.protocol = P_IMAP;
225 else if (strcasecmp(optarg,"imap-k4") == 0)
226 ctl->server.protocol = P_IMAP_K4;
227 #endif /* KERBEROS_V4 */
228 else if (strcasecmp(optarg,"etrn") == 0)
229 ctl->server.protocol = P_ETRN;
231 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
237 ctl->server.uidl = FLAG_TRUE;
242 ctl->server.service = optarg;
244 ctl->server.port = atoi(optarg);
248 case LA_AUTHENTICATE:
249 if (strcmp(optarg, "password") == 0)
250 ctl->server.preauthenticate = A_PASSWORD;
251 else if (strcmp(optarg, "kerberos") == 0)
253 ctl->server.preauthenticate = A_KERBEROS_V5;
254 else if (strcmp(optarg, "kerberos_v5") == 0)
255 ctl->server.preauthenticate = A_KERBEROS_V5;
257 ctl->server.preauthenticate = A_KERBEROS_V4;
258 else if (strcmp(optarg, "kerberos_v4") == 0)
259 ctl->server.preauthenticate = A_KERBEROS_V4;
260 #endif /* KERBEROS_V5 */
262 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
268 ctl->server.timeout = atoi(optarg);
269 if (ctl->server.timeout == 0)
270 ctl->server.timeout = -1;
274 ctl->server.envelope = xstrdup(optarg);
278 ctl->server.qvirtual = xstrdup(optarg);
283 ctl->remotename = xstrdup(optarg);
287 ctl->fetchall = FLAG_TRUE;
291 ctl->keep = FLAG_FALSE;
295 ctl->keep = FLAG_TRUE;
299 ctl->flush = FLAG_TRUE;
303 ctl->rewrite = FLAG_FALSE;
308 ctl->limit = NUM_VALUE(c);
313 cp = strtok(buf, ",");
315 save_str(&ctl->mailboxes, cp, 0);
317 ((cp = strtok((char *)NULL, ",")));
322 cp = strtok(buf, ",");
324 save_str(&ctl->smtphunt, cp, TRUE);
326 ((cp = strtok((char *)NULL, ",")));
331 ctl->smtpaddress = xstrdup(optarg);
336 ctl->antispam = NUM_VALUE(c);
340 ctl->batchlimit = NUM_VALUE(c);
345 ctl->fetchlimit = NUM_VALUE(c);
350 ctl->expunge = NUM_VALUE(c);
354 ctl->mda = xstrdup(optarg);
361 ctl->server.netsec = (void *)optarg;
363 fprintf(stderr, "fetchmail: network security support is disabled\n");
365 #endif /* NET_SECURITY */
368 #if defined(linux) && !INET6
371 interface_parse(optarg, &ctl->server);
375 ctl->server.monitor = xstrdup(optarg);
377 #endif /* defined(linux) && !INET6 */
385 errors_to_syslog = TRUE;
395 if (errflag || ocount > 1) {
396 /* squawk if syntax errors were detected */
397 fputs("usage: fetchmail [options] [server ...]\n", stderr);
398 fputs(" Options are as follows:\n",stderr);
399 fputs(" -?, --help display this option help\n", stderr);
400 fputs(" -V, --version display version info\n", stderr);
402 fputs(" -c, --check check for messages without fetching\n", stderr);
403 fputs(" -s, --silent work silently\n", stderr);
404 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
405 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
406 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
407 fputs(" -q, --quit kill daemon process\n", stderr);
408 fputs(" -L, --logfile specify logfile name\n", stderr);
409 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
410 fputs(" --invisible suppress Received line & enable host spoofing\n", stderr);
411 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
412 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
413 #if defined(linux) && !INET6
414 fputs(" -I, --interface interface required specification\n",stderr);
415 fputs(" -M, --monitor monitor interface for activity\n",stderr);
419 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn, imap-k4\n", stderr);
421 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
422 #endif /* KERBEROS_V4 */
423 fputs(" -U, --uidl force the use of UIDLs (pop3 only)\n", stderr);
424 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
425 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
426 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
427 fputs(" -E, --envelope envelope address header\n",stderr);
428 fputs(" -Q, --qvirtual prefix to remove from local user id\n",stderr);
430 fputs(" -u, --username specify users's login on server\n", stderr);
431 fputs(" -a, --all retrieve old and new messages\n", stderr);
432 fputs(" -K, --nokeep delete new messages after retrieval\n", stderr);
433 fputs(" -k, --keep save new messages after retrieval\n", stderr);
434 fputs(" -F, --flush delete old messages from server\n", stderr);
435 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
436 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
439 fputs(" -T, --netsec set IP security request\n", stderr);
440 #endif /* NET_SECURITY */
441 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
442 fputs(" -D, --smtpaddress set SMTP delivery domain to use\n", stderr);
443 fputs(" -Z, --antispam, set antispam response value\n", stderr);
444 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
445 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
446 fputs(" -e, --expunge set max deletions between expunges\n", stderr);
447 fputs(" -r, --folder specify remote folder name\n", stderr);
454 /* options.c ends here */