3 * rcfile_y.y -- Run control file parser for fetchmail
5 * For license terms, see the file COPYING in this directory.
10 #include <sys/types.h>
12 #if defined(HAVE_SYS_WAIT_H)
17 #if defined(STDC_HEADERS)
20 #if defined(HAVE_UNISTD_H)
26 #include <net/security.h>
27 #endif /* NET_SECURITY */
29 #include "fetchmail.h"
31 /* parser reads these */
32 char *rcfile; /* path name of rc file */
33 struct query cmd_opts; /* where to put command-line info */
35 /* parser sets these */
36 int poll_interval; /* poll interval in seconds */
37 char *logfile; /* log file for daemon mode */
38 flag errors_to_syslog; /* if syslog was set */
39 flag use_invisible; /* if invisible was set */
40 struct query *querylist; /* head of server list (globally visible) */
42 int yydebug; /* in case we didn't generate with -- debug */
44 static struct query current; /* current server record */
45 static int prc_errflag;
46 static struct hostdata *leadentry;
49 static void record_current();
50 static void user_reset();
51 static void reset_server(char *name, int skip);
53 /* using Bison, this arranges that yydebug messages will show actual tokens */
55 #define YYPRINT(fp, type, val) fprintf(fp, " = \"%s\"", yytext)
64 %token DEFAULTS POLL SKIP VIA AKA LOCALDOMAINS PROTOCOL
65 %token AUTHENTICATE TIMEOUT KPOP KERBEROS4 KERBEROS5 KERBEROS
66 %token ENVELOPE QVIRTUAL USERNAME PASSWORD FOLDER SMTPHOST MDA SMTPADDRESS
67 %token SPAMRESPONSE PRECONNECT POSTCONNECT LIMIT
68 %token IS HERE THERE TO MAP WILDCARD
69 %token BATCHLIMIT FETCHLIMIT EXPUNGE
70 %token SET LOGFILE DAEMON SYSLOG INVISIBLE NETSEC INTERFACE MONITOR
73 %token <number> NUMBER
74 %token NO KEEP FLUSH FETCHALL REWRITE FORCECR STRIPCR PASS8BITS DROPSTATUS
75 %token DNS SERVICE PORT UIDL INTERVAL
83 statement_list : statement
84 | statement_list statement
87 optmap : MAP | /* EMPTY */;
89 /* future global options should also have the form SET <name> optmap <value> */
90 statement : SET LOGFILE optmap STRING {logfile = xstrdup($4);}
91 | SET DAEMON optmap NUMBER {poll_interval = $4;}
92 | SET SYSLOG {errors_to_syslog = TRUE;}
93 | SET INVISIBLE {use_invisible = TRUE;}
96 * The way the next two productions are written depends on the fact that
97 * userspecs cannot be empty. It's a kluge to deal with files that set
98 * up a load of defaults and then have poll statements following with no
99 * user options at all.
101 | define_server serverspecs {record_current();}
102 | define_server serverspecs userspecs
104 /* detect and complain about the most common user error */
105 | define_server serverspecs userspecs serv_option
106 {yyerror("server option after user options");}
109 define_server : POLL STRING {reset_server($2, FALSE);}
110 | SKIP STRING {reset_server($2, TRUE);}
111 | DEFAULTS {reset_server("defaults", FALSE);}
114 serverspecs : /* EMPTY */
115 | serverspecs serv_option
118 alias_list : STRING {save_str(¤t.server.akalist,$1,0);}
119 | alias_list STRING {save_str(¤t.server.akalist,$2,0);}
122 domain_list : STRING {save_str(¤t.server.localdomains,$1,0);}
123 | domain_list STRING {save_str(¤t.server.localdomains,$2,0);}
126 serv_option : AKA alias_list
127 | VIA STRING {current.server.via = xstrdup($2);}
128 | LOCALDOMAINS domain_list
129 | PROTOCOL PROTO {current.server.protocol = $2;}
131 current.server.protocol = P_POP3;
133 current.server.preauthenticate = A_KERBEROS_V5;
135 current.server.preauthenticate = A_KERBEROS_V4;
136 #endif /* KERBEROS_V5 */
138 current.server.service = KPOP_PORT;
140 current.server.port = KPOP_PORT;
143 | UIDL {current.server.uidl = FLAG_TRUE;}
144 | NO UIDL {current.server.uidl = FLAG_FALSE;}
147 current.server.service = $2;
152 current.server.port = $2;
155 | INTERVAL NUMBER {current.server.interval = $2;}
156 | AUTHENTICATE PASSWORD {current.server.preauthenticate = A_PASSWORD;}
157 | AUTHENTICATE KERBEROS4 {current.server.preauthenticate = A_KERBEROS_V4;}
158 | AUTHENTICATE KERBEROS5 {current.server.preauthenticate = A_KERBEROS_V5;}
159 | AUTHENTICATE KERBEROS {
161 current.server.preauthenticate = A_KERBEROS_V5;
163 current.server.preauthenticate = A_KERBEROS_V4;
164 #endif /* KERBEROS_V5 */
166 | TIMEOUT NUMBER {current.server.timeout = $2;}
168 | ENVELOPE NUMBER STRING
170 current.server.envelope =
172 current.server.envskip = $2;
176 current.server.envelope =
178 current.server.envskip = 0;
181 | QVIRTUAL STRING {current.server.qvirtual=xstrdup($2);}
187 if (net_security_strtorequest($2, &request, &requestlen))
188 yyerror("invalid security request");
190 current.server.netsec = xstrdup($2);
194 yyerror("network-security support disabled");
195 #endif /* NET_SECURITY */
198 #if defined(linux) && !defined(INET6)
199 interface_parse($2, ¤t.server);
200 #else /* defined(linux) && !defined(INET6) */
201 fprintf(stderr, "fetchmail: interface option is only supported under Linux\n");
202 #endif /* defined(linux) && !defined(INET6) */
205 #if defined(linux) && !defined(INET6)
206 current.server.monitor = xstrdup($2);
207 #else /* defined(linux) && !defined(INET6) */
208 fprintf(stderr, "fetchmail: monitor option is only supported under Linux\n");
209 #endif /* defined(linux) && !defined(INET6) */
211 | DNS {current.server.dns = FLAG_TRUE;}
212 | NO DNS {current.server.dns = FLAG_FALSE;}
213 | NO ENVELOPE {current.server.envelope = STRING_DISABLED;}
216 userspecs : user1opts {record_current(); user_reset();}
220 explicits : explicitdef {record_current(); user_reset();}
221 | explicits explicitdef {record_current(); user_reset();}
224 explicitdef : userdef user0opts
227 userdef : USERNAME STRING {current.remotename = xstrdup($2);}
228 | USERNAME mapping_list HERE
229 | USERNAME STRING THERE {current.remotename = xstrdup($2);}
232 user0opts : /* EMPTY */
233 | user0opts user_option
236 user1opts : user_option
237 | user1opts user_option
240 localnames : WILDCARD {current.wildcard = TRUE;}
241 | mapping_list {current.wildcard = FALSE;}
242 | mapping_list WILDCARD {current.wildcard = TRUE;}
245 mapping_list : mapping
246 | mapping_list mapping
250 {save_str_pair(¤t.localnames, $1, NULL);}
252 {save_str_pair(¤t.localnames, $1, $3);}
255 folder_list : STRING {save_str(¤t.mailboxes,$1,0);}
256 | folder_list STRING {save_str(¤t.mailboxes,$2,0);}
259 smtp_list : STRING {save_str(¤t.smtphunt, $1,TRUE);}
260 | smtp_list STRING {save_str(¤t.smtphunt, $2,TRUE);}
263 user_option : TO localnames HERE
268 | IS STRING THERE {current.remotename = xstrdup($2);}
269 | PASSWORD STRING {current.password = xstrdup($2);}
272 | SMTPADDRESS STRING {current.smtpaddress = xstrdup($2);}
273 | SPAMRESPONSE NUMBER {current.antispam = $2;}
274 | MDA STRING {current.mda = xstrdup($2);}
275 | PRECONNECT STRING {current.preconnect = xstrdup($2);}
276 | POSTCONNECT STRING {current.postconnect = xstrdup($2);}
278 | KEEP {current.keep = FLAG_TRUE;}
279 | FLUSH {current.flush = FLAG_TRUE;}
280 | FETCHALL {current.fetchall = FLAG_TRUE;}
281 | REWRITE {current.rewrite = FLAG_TRUE;}
282 | FORCECR {current.forcecr = FLAG_TRUE;}
283 | STRIPCR {current.stripcr = FLAG_TRUE;}
284 | PASS8BITS {current.pass8bits = FLAG_TRUE;}
285 | DROPSTATUS {current.dropstatus = FLAG_TRUE;}
287 | NO KEEP {current.keep = FLAG_FALSE;}
288 | NO FLUSH {current.flush = FLAG_FALSE;}
289 | NO FETCHALL {current.fetchall = FLAG_FALSE;}
290 | NO REWRITE {current.rewrite = FLAG_FALSE;}
291 | NO FORCECR {current.forcecr = FLAG_FALSE;}
292 | NO STRIPCR {current.stripcr = FLAG_FALSE;}
293 | NO PASS8BITS {current.pass8bits = FLAG_FALSE;}
294 | NO DROPSTATUS {current.dropstatus = FLAG_FALSE;}
296 | LIMIT NUMBER {current.limit = NUM_VALUE($2);}
297 | FETCHLIMIT NUMBER {current.fetchlimit = NUM_VALUE($2);}
298 | BATCHLIMIT NUMBER {current.batchlimit = NUM_VALUE($2);}
299 | EXPUNGE NUMBER {current.expunge = NUM_VALUE($2);}
303 /* lexer interface */
305 extern int prc_lineno;
309 static struct query *hosttail; /* where to add new elements */
311 void yyerror (const char *s)
312 /* report a syntax error */
314 error_at_line( 0, 0, rcfile, prc_lineno, "%s at %s", s,
315 (yytext && yytext[0]) ? yytext : "end of input");
319 int prc_filecheck(pathname)
320 /* check that a configuration file is secure */
321 const char *pathname; /* pathname for the configuration file */
328 /* special cases useful for debugging purposes */
329 if (strcmp("/dev/null", pathname) == 0)
332 /* the run control file must have the same uid as the REAL uid of this
333 process, it must have permissions no greater than 600, and it must not
334 be a symbolic link. We check these conditions here. */
336 if (lstat(pathname, &statbuf) < 0) {
340 error(0, errno, "lstat: %s", pathname);
345 if ((statbuf.st_mode & S_IFLNK) == S_IFLNK) {
346 fprintf(stderr, "File %s must not be a symbolic link.\n", pathname);
350 if (statbuf.st_mode & ~(S_IFREG | S_IREAD | S_IWRITE)) {
351 fprintf(stderr, "File %s must have no more than -rw------ (0600) permissions.\n",
356 if (statbuf.st_uid != getuid()) {
357 fprintf(stderr, "File %s must be owned by you.\n", pathname);
364 int prc_parse_file (const char *pathname, const flag securecheck)
365 /* digest the configuration into a linked list of host records */
368 querylist = hosttail = (struct query *)NULL;
372 /* Check that the file is secure */
373 if (securecheck && (prc_errflag = prc_filecheck(pathname)) != 0)
379 /* Open the configuration and feed it to the lexer. */
380 if ((yyin = fopen(pathname,"r")) == (FILE *)NULL) {
381 error(0, errno, "open: %s", pathname);
385 yyparse(); /* parse entire file */
395 static void reset_server(char *name, int skip)
396 /* clear the entire global record and initialize it with a new name */
399 memset(¤t,'\0',sizeof(current));
400 current.smtp_socket = -1;
401 current.server.pollname = xstrdup(name);
402 current.server.skip = skip;
406 static void user_reset(void)
407 /* clear the global current record (user parameters) used by the parser */
409 struct hostdata save;
412 * Purpose of this code is to initialize the new server block, but
413 * preserve whatever server name was previously set. Also
414 * preserve server options unless the command-line explicitly
417 save = current.server;
419 memset(¤t, '\0', sizeof(current));
420 current.smtp_socket = -1;
422 current.server = save;
425 struct query *hostalloc(init)
426 /* append a host record to the host list */
427 struct query *init; /* pointer to block containing initial values */
431 /* allocate new node */
432 node = (struct query *) xmalloc(sizeof(struct query));
435 memcpy(node, init, sizeof(struct query));
437 /* append to end of list */
438 if (hosttail != (struct query *) 0)
439 hosttail->next = node; /* list contains at least one element */
441 querylist = node; /* list is empty */
445 node->server.lead_server = leadentry;
448 node->server.lead_server = NULL;
449 leadentry = &node->server;
455 static void record_current(void)
456 /* register current parameters and append to the host list */
458 #define FLAG_FORCE(fld) if (cmd_opts.fld) current.fld = cmd_opts.fld
459 FLAG_FORCE(server.via);
460 FLAG_FORCE(server.protocol);
462 FLAG_FORCE(server.service);
463 FLAG_FORCE(server.netsec);
465 FLAG_FORCE(server.port);
467 FLAG_FORCE(server.interval);
468 FLAG_FORCE(server.preauthenticate);
469 FLAG_FORCE(server.timeout);
470 FLAG_FORCE(server.envelope);
471 FLAG_FORCE(server.envskip);
472 FLAG_FORCE(server.qvirtual);
473 FLAG_FORCE(server.skip);
474 FLAG_FORCE(server.dns);
475 FLAG_FORCE(server.uidl);
478 FLAG_FORCE(server.interface);
479 FLAG_FORCE(server.monitor);
480 FLAG_FORCE(server.interface_pair);
483 FLAG_FORCE(remotename);
484 FLAG_FORCE(password);
485 if (cmd_opts.mailboxes)
486 current.mailboxes = cmd_opts.mailboxes;
487 if (cmd_opts.smtphunt)
488 current.smtphunt = cmd_opts.smtphunt;
490 FLAG_FORCE(smtpaddress);
491 FLAG_FORCE(antispam);
492 FLAG_FORCE(preconnect);
493 FLAG_FORCE(postconnect);
497 FLAG_FORCE(fetchall);
501 FLAG_FORCE(pass8bits);
502 FLAG_FORCE(dropstatus);
504 FLAG_FORCE(fetchlimit);
505 FLAG_FORCE(batchlimit);
510 (void) hostalloc(¤t);
515 void optmerge(struct query *h2, struct query *h1)
516 /* merge two options records; empty fields in h2 are filled in from h1 */
518 append_str_list(&h2->server.localdomains, &h1->server.localdomains);
519 append_str_list(&h2->localnames, &h1->localnames);
520 append_str_list(&h2->mailboxes, &h1->mailboxes);
521 append_str_list(&h2->smtphunt, &h1->smtphunt);
523 #define FLAG_MERGE(fld) if (!h2->fld) h2->fld = h1->fld
524 FLAG_MERGE(server.via);
525 FLAG_MERGE(server.protocol);
527 FLAG_MERGE(server.service);
528 FLAG_MERGE(server.netsec);
530 FLAG_MERGE(server.port);
532 FLAG_MERGE(server.interval);
533 FLAG_MERGE(server.preauthenticate);
534 FLAG_MERGE(server.timeout);
535 FLAG_MERGE(server.envelope);
536 FLAG_MERGE(server.envskip);
537 FLAG_MERGE(server.qvirtual);
538 FLAG_MERGE(server.skip);
539 FLAG_MERGE(server.dns);
540 FLAG_MERGE(server.uidl);
543 FLAG_MERGE(server.interface);
544 FLAG_MERGE(server.monitor);
545 FLAG_MERGE(server.interface_pair);
548 FLAG_MERGE(remotename);
549 FLAG_MERGE(password);
551 FLAG_MERGE(smtpaddress);
552 FLAG_MERGE(antispam);
553 FLAG_MERGE(preconnect);
557 FLAG_MERGE(fetchall);
561 FLAG_MERGE(pass8bits);
562 FLAG_MERGE(dropstatus);
564 FLAG_MERGE(fetchlimit);
565 FLAG_MERGE(batchlimit);
570 /* easier to do this than cope with variations in where the library lives */
571 int yywrap(void) {return 1;}
573 /* rcfile_y.y ends here */