]> Pileus Git - ~andy/fetchmail/blob - options.c
Added configuration dumper.
[~andy/fetchmail] / options.c
1 /*
2  * options.c -- command-line option processing
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include "config.h"
8
9 #include <stdio.h>
10 #include <pwd.h>
11 #include <string.h>
12 #if defined(STDC_HEADERS)
13 #include  <stdlib.h>
14 #endif
15
16 #include "getopt.h"
17 #include "fetchmail.h"
18
19 #define LA_HELP         1
20 #define LA_VERSION      2 
21 #define LA_CHECK        3
22 #define LA_SILENT       4 
23 #define LA_VERBOSE      5 
24 #define LA_DAEMON       6
25 #define LA_NODETACH     7
26 #define LA_QUIT         8
27 #define LA_LOGFILE      9
28 #define LA_INVISIBLE    10
29 #define LA_SYSLOG       11
30 #define LA_RCFILE       12
31 #define LA_IDFILE       13
32 #define LA_PROTOCOL     14
33 #define LA_UIDL         15
34 #define LA_PORT         16
35 #define LA_AUTHENTICATE 17
36 #define LA_TIMEOUT      18
37 #define LA_ENVELOPE     19
38 #define LA_QVIRTUAL     20
39 #define LA_USERNAME     21
40 #define LA_ALL          22
41 #define LA_NOKEEP       23
42 #define LA_KEEP         24
43 #define LA_FLUSH        25
44 #define LA_NOREWRITE    26
45 #define LA_LIMIT        27
46 #define LA_FOLDER       28
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
52 #define LA_EXPUNGE      34
53 #define LA_MDA          35
54 #define LA_NETSEC       36
55 #define LA_INTERFACE    37
56 #define LA_MONITOR      38
57 #define LA_CONFIGDUMP   39
58 #define LA_YYDEBUG      40
59
60 /* options still left: CgGhHjJoORUwWxXYz */
61 static const char *shortoptions = 
62         "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:y";
63
64 static const struct option longoptions[] = {
65 /* this can be const because all flag fields are 0 and will never get set */
66   {"help",      no_argument,       (int *) 0, LA_HELP        },
67   {"version",   no_argument,       (int *) 0, LA_VERSION     },
68   {"check",     no_argument,       (int *) 0, LA_CHECK       },
69   {"silent",    no_argument,       (int *) 0, LA_SILENT      },
70   {"verbose",   no_argument,       (int *) 0, LA_VERBOSE     },
71   {"daemon",    required_argument, (int *) 0, LA_DAEMON      },
72   {"nodetach",  no_argument,       (int *) 0, LA_NODETACH    },
73   {"quit",      no_argument,       (int *) 0, LA_QUIT        },
74   {"logfile",   required_argument, (int *) 0, LA_LOGFILE     },
75   {"invisible", no_argument,       (int *) 0, LA_INVISIBLE   },
76   {"syslog",    no_argument,       (int *) 0, LA_SYSLOG      },
77   {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE      },
78   {"idfile",    required_argument, (int *) 0, LA_IDFILE      },
79
80   {"protocol",  required_argument, (int *) 0, LA_PROTOCOL    },
81   {"proto",     required_argument, (int *) 0, LA_PROTOCOL    },
82   {"uidl",      no_argument,       (int *) 0, LA_UIDL        },
83   {"port",      required_argument, (int *) 0, LA_PORT        },
84   {"auth",      required_argument, (int *) 0, LA_AUTHENTICATE},
85   {"timeout",   required_argument, (int *) 0, LA_TIMEOUT     },
86   {"envelope",  required_argument, (int *) 0, LA_ENVELOPE    },
87   {"qvirtual",  required_argument, (int *) 0, LA_QVIRTUAL    },
88
89   {"user",      required_argument, (int *) 0, LA_USERNAME    },
90   {"username",  required_argument, (int *) 0, LA_USERNAME    },
91
92   {"all",       no_argument,       (int *) 0, LA_ALL         },
93   {"nokeep",    no_argument,       (int *) 0, LA_NOKEEP      },
94   {"keep",      no_argument,       (int *) 0, LA_KEEP        },
95   {"flush",     no_argument,       (int *) 0, LA_FLUSH       },
96   {"norewrite", no_argument,       (int *) 0, LA_NOREWRITE   },
97   {"limit",     required_argument, (int *) 0, LA_LIMIT       },
98
99   {"folder",    required_argument, (int *) 0, LA_FOLDER      },
100   {"smtphost",  required_argument, (int *) 0, LA_SMTPHOST    },
101   {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR  },
102   {"antispam",  required_argument, (int *) 0, LA_ANTISPAM    },
103   
104   {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT  },
105   {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT  },
106   {"expunge",   required_argument, (int *) 0, LA_EXPUNGE     },
107   {"mda",       required_argument, (int *) 0, LA_MDA         },
108
109 #ifdef INET6
110   {"netsec",    required_argument, (int *) 0, LA_NETSEC    },
111 #endif /* INET6 */
112
113 #if defined(linux) && !INET6
114   {"interface", required_argument, (int *) 0, LA_INTERFACE   },
115   {"monitor",   required_argument, (int *) 0, LA_MONITOR     },
116 #endif /* defined(linux) && !INET6 */
117
118   {"configdump",no_argument,       (int *) 0, LA_CONFIGDUMP  },
119
120   {"yydebug",   no_argument,       (int *) 0, LA_YYDEBUG     },
121
122   {(char *) 0,  no_argument,       (int *) 0, 0              }
123 };
124
125 int parsecmdline (argc, argv, rctl, ctl)
126 /* parse and validate the command line options */
127 int argc;               /* argument count */
128 char **argv;            /* argument strings */
129 struct runctl *rctl;    /* global run controls to modify */
130 struct query *ctl;      /* option record to be initialized */
131 {
132     /*
133      * return value: if positive, argv index of last parsed option + 1
134      * (presumes one or more server names follows).  if zero, the
135      * command line switches are such that no server names are
136      * required (e.g. --version).  if negative, the command line is
137      * has one or more syntax errors.
138      */
139
140     int c;
141     int ocount = 0;     /* count of destinations specified */
142     int errflag = 0;   /* TRUE when a syntax error is detected */
143     int option_index;
144     char buf[BUFSIZ], *cp;
145
146     rctl->poll_interval = -1;
147
148     memset(ctl, '\0', sizeof(struct query));    /* start clean */
149     ctl->smtp_socket = -1;
150
151     while (!errflag && 
152            (c = getopt_long(argc,argv,shortoptions,
153                             longoptions,&option_index)) != -1) {
154
155         switch (c) {
156         case 'V':
157         case LA_VERSION:
158             versioninfo = TRUE;
159             break;
160         case 'c':
161         case LA_CHECK:
162             check_only = TRUE;
163             break;
164         case 's':
165         case LA_SILENT:
166             outlevel = O_SILENT;
167             break;
168         case 'v':
169         case LA_VERBOSE:
170             outlevel = O_VERBOSE;
171             break;
172         case 'd':
173         case LA_DAEMON:
174             rctl->poll_interval = atoi(optarg);
175             break;
176         case 'N':
177         case LA_NODETACH:
178             nodetach = TRUE;
179             break;
180         case 'q':
181         case LA_QUIT:
182             quitmode = TRUE;
183             break;
184         case 'L':
185         case LA_LOGFILE:
186             rctl->logfile = optarg;
187             break;
188         case LA_INVISIBLE:
189             rctl->invisible = TRUE;
190             break;
191         case 'f':
192         case LA_RCFILE:
193             rcfile = (char *) xmalloc(strlen(optarg)+1);
194             strcpy(rcfile,optarg);
195             break;
196         case 'i':
197         case LA_IDFILE:
198             rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
199             strcpy(rctl->idfile,optarg);
200             break;
201         case 'p':
202         case LA_PROTOCOL:
203             /* XXX -- should probably use a table lookup here */
204             if (strcasecmp(optarg,"pop2") == 0)
205                 ctl->server.protocol = P_POP2;
206             else if (strcasecmp(optarg,"pop3") == 0)
207                 ctl->server.protocol = P_POP3;
208             else if (strcasecmp(optarg,"apop") == 0)
209                 ctl->server.protocol = P_APOP;
210             else if (strcasecmp(optarg,"rpop") == 0)
211                 ctl->server.protocol = P_RPOP;
212             else if (strcasecmp(optarg,"kpop") == 0)
213             {
214                 ctl->server.protocol = P_POP3;
215 #if INET6
216                 ctl->server.service = KPOP_PORT;
217 #else /* INET6 */
218                 ctl->server.port = KPOP_PORT;
219 #endif /* INET6 */
220 #ifdef KERBEROS_V5
221                 ctl->server.preauthenticate =  A_KERBEROS_V5;
222 #else
223                 ctl->server.preauthenticate =  A_KERBEROS_V4;
224 #endif /* KERBEROS_V5 */
225             }
226             else if (strcasecmp(optarg,"imap") == 0)
227                 ctl->server.protocol = P_IMAP;
228 #ifdef KERBEROS_V4
229             else if (strcasecmp(optarg,"imap-k4") == 0)
230                 ctl->server.protocol = P_IMAP_K4;
231 #endif /* KERBEROS_V4 */
232             else if (strcasecmp(optarg,"etrn") == 0)
233                 ctl->server.protocol = P_ETRN;
234             else {
235                 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
236                 errflag++;
237             }
238             break;
239         case 'U':
240         case LA_UIDL:
241             ctl->server.uidl = FLAG_TRUE;
242             break;
243         case 'P':
244         case LA_PORT:
245 #if INET6
246             ctl->server.service = optarg;
247 #else /* INET6 */
248             ctl->server.port = atoi(optarg);
249 #endif /* INET6 */
250             break;
251         case 'A':
252         case LA_AUTHENTICATE:
253             if (strcmp(optarg, "password") == 0)
254                 ctl->server.preauthenticate = A_PASSWORD;
255             else if (strcmp(optarg, "kerberos") == 0)
256 #ifdef KERBEROS_V5
257                 ctl->server.preauthenticate = A_KERBEROS_V5;
258             else if (strcmp(optarg, "kerberos_v5") == 0)
259                 ctl->server.preauthenticate = A_KERBEROS_V5;
260 #else
261                 ctl->server.preauthenticate = A_KERBEROS_V4;
262             else if (strcmp(optarg, "kerberos_v4") == 0)
263                 ctl->server.preauthenticate = A_KERBEROS_V4;
264 #endif /* KERBEROS_V5 */
265             else {
266                 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
267                 errflag++;
268             }
269             break;
270         case 't':
271         case LA_TIMEOUT:
272             ctl->server.timeout = atoi(optarg);
273             if (ctl->server.timeout == 0)
274                 ctl->server.timeout = -1;
275             break;
276         case 'E':
277         case LA_ENVELOPE:
278             ctl->server.envelope = xstrdup(optarg);
279             break;
280         case 'Q':    
281         case LA_QVIRTUAL:
282             ctl->server.qvirtual = xstrdup(optarg);
283             break;
284
285         case 'u':
286         case LA_USERNAME:
287             ctl->remotename = xstrdup(optarg);
288             break;
289         case 'a':
290         case LA_ALL:
291             ctl->fetchall = FLAG_TRUE;
292             break;
293         case 'K':
294         case LA_NOKEEP:
295             ctl->keep = FLAG_FALSE;
296             break;
297         case 'k':
298         case LA_KEEP:
299             ctl->keep = FLAG_TRUE;
300             break;
301         case 'F':
302         case LA_FLUSH:
303             ctl->flush = FLAG_TRUE;
304             break;
305         case 'n':
306         case LA_NOREWRITE:
307             ctl->rewrite = FLAG_FALSE;
308             break;
309         case 'l':
310         case LA_LIMIT:
311             c = atoi(optarg);
312             ctl->limit = NUM_VALUE(c);
313             break;
314         case 'r':
315         case LA_FOLDER:
316             strcpy(buf, optarg);
317             cp = strtok(buf, ",");
318             do {
319                 save_str(&ctl->mailboxes, cp, 0);
320             } while
321                 ((cp = strtok((char *)NULL, ",")));
322             break;
323         case 'S':
324         case LA_SMTPHOST:
325             strcpy(buf, optarg);
326             cp = strtok(buf, ",");
327             do {
328                 save_str(&ctl->smtphunt, cp, TRUE);
329             } while
330                 ((cp = strtok((char *)NULL, ",")));
331             ocount++;
332             break;
333         case 'D':
334         case LA_SMTPADDR:
335             ctl->smtpaddress = xstrdup(optarg);
336             break;
337         case 'Z':
338         case LA_ANTISPAM:
339             c = atoi(optarg);
340             ctl->antispam = NUM_VALUE(c);
341         case 'b':
342         case LA_BATCHLIMIT:
343             c = atoi(optarg);
344             ctl->batchlimit = NUM_VALUE(c);
345             break;
346         case 'B':
347         case LA_FETCHLIMIT:
348             c = atoi(optarg);
349             ctl->fetchlimit = NUM_VALUE(c);
350             break;
351         case 'e':
352         case LA_EXPUNGE:
353             c = atoi(optarg);
354             ctl->expunge = NUM_VALUE(c);
355             break;
356         case 'm':
357         case LA_MDA:
358             ctl->mda = xstrdup(optarg);
359             ocount++;
360             break;
361
362         case 'T':
363         case LA_NETSEC:
364 #if NET_SECURITY
365             ctl->server.netsec = (void *)optarg;
366 #else
367             fprintf(stderr, "fetchmail: network security support is disabled\n");
368             errflag++;
369 #endif /* NET_SECURITY */
370             break;
371
372 #if defined(linux) && !INET6
373         case 'I':
374         case LA_INTERFACE:
375             interface_parse(optarg, &ctl->server);
376             break;
377         case 'M':
378         case LA_MONITOR:
379             ctl->server.monitor = xstrdup(optarg);
380             break;
381 #endif /* defined(linux) && !INET6 */
382
383         case 'y':
384         case LA_YYDEBUG:
385             yydebug = TRUE;
386             break;
387
388         case LA_CONFIGDUMP:
389             configdump = TRUE;
390             break;
391
392         case LA_SYSLOG:
393             rctl->use_syslog = TRUE;
394             break;
395
396         case '?':
397         case LA_HELP:
398         default:
399             errflag++;
400         }
401     }
402
403     if (errflag || ocount > 1) {
404         /* squawk if syntax errors were detected */
405         fputs("usage:  fetchmail [options] [server ...]\n", stderr);
406         fputs("  Options are as follows:\n",stderr);
407         fputs("  -?, --help        display this option help\n", stderr);
408         fputs("  -V, --version     display version info\n", stderr);
409
410         fputs("  -c, --check       check for messages without fetching\n", stderr);
411         fputs("  -s, --silent      work silently\n", stderr);
412         fputs("  -v, --verbose     work noisily (diagnostic output)\n", stderr);
413         fputs("  -d, --daemon      run as a daemon once per n seconds\n", stderr);
414         fputs("  -N, --nodetach    don't detach daemon process\n", stderr);
415         fputs("  -q, --quit        kill daemon process\n", stderr);
416         fputs("  -L, --logfile     specify logfile name\n", stderr);
417         fputs("      --syslog      use syslog(3) for most messages when running as a daemon\n", stderr);
418         fputs("      --invisible   suppress Received line & enable host spoofing\n", stderr);
419         fputs("  -f, --fetchmailrc specify alternate run control file\n", stderr);
420         fputs("  -i, --idfile      specify alternate UIDs file\n", stderr);
421 #if defined(linux) && !INET6
422         fputs("  -I, --interface   interface required specification\n",stderr);
423         fputs("  -M, --monitor     monitor interface for activity\n",stderr);
424 #endif
425
426 #ifdef KERBEROS_V4
427         fputs("  -p, --protocol    specify pop2, pop3, imap, apop, rpop, kpop, etrn, imap-k4\n", stderr);
428 #else
429         fputs("  -p, --protocol    specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
430 #endif /* KERBEROS_V4 */
431         fputs("  -U, --uidl        force the use of UIDLs (pop3 only)\n", stderr);
432         fputs("  -P, --port        TCP/IP service port to connect to\n",stderr);
433         fputs("  -A, --auth        authentication type (password or kerberos)\n",stderr);
434         fputs("  -t, --timeout     server nonresponse timeout\n",stderr);
435         fputs("  -E, --envelope    envelope address header\n",stderr);
436         fputs("  -Q, --qvirtual    prefix to remove from local user id\n",stderr);
437
438         fputs("  -u, --username    specify users's login on server\n", stderr);
439         fputs("  -a, --all         retrieve old and new messages\n", stderr);
440         fputs("  -K, --nokeep      delete new messages after retrieval\n", stderr);
441         fputs("  -k, --keep        save new messages after retrieval\n", stderr);
442         fputs("  -F, --flush       delete old messages from server\n", stderr);
443         fputs("  -n, --norewrite   don't rewrite header addresses\n", stderr);
444         fputs("  -l, --limit       don't fetch messages over given size\n", stderr);
445
446 #if NET_SECURITY
447         fputs("  -T, --netsec      set IP security request\n", stderr);
448 #endif /* NET_SECURITY */
449         fputs("  -S, --smtphost    set SMTP forwarding host\n", stderr);
450         fputs("  -D, --smtpaddress set SMTP delivery domain to use\n", stderr);
451         fputs("  -Z, --antispam,   set antispam response value\n", stderr);
452         fputs("  -b, --batchlimit  set batch limit for SMTP connections\n", stderr);
453         fputs("  -B, --fetchlimit  set fetch limit for server connections\n", stderr);
454         fputs("  -e, --expunge     set max deletions between expunges\n", stderr);
455         fputs("  -r, --folder      specify remote folder name\n", stderr);
456         return(-1);
457     }
458
459     return(optind);
460 }
461
462 /* options.c ends here */