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 net_security_operation request[NET_SECURITY_OPERATION_MAX];
50 static int requestlen = NET_SECURITY_OPERATION_MAX;
51 #endif /* NET_SECURITY */
53 static void record_current();
54 static void user_reset();
55 static void reset_server(char *name, int skip);
57 /* using Bison, this arranges that yydebug messages will show actual tokens */
59 #define YYPRINT(fp, type, val) fprintf(fp, " = \"%s\"", yytext)
68 %token DEFAULTS POLL SKIP VIA AKA LOCALDOMAINS PROTOCOL
69 %token AUTHENTICATE TIMEOUT KPOP KERBEROS4
70 %token ENVELOPE QVIRTUAL USERNAME PASSWORD FOLDER SMTPHOST MDA SMTPADDRESS
71 %token PRECONNECT POSTCONNECT LIMIT
72 %token IS HERE THERE TO MAP WILDCARD
73 %token BATCHLIMIT FETCHLIMIT EXPUNGE
74 %token SET LOGFILE DAEMON SYSLOG INVISIBLE NETSEC INTERFACE MONITOR
77 %token <number> NUMBER
78 %token NO KEEP FLUSH FETCHALL REWRITE FORCECR STRIPCR PASS8BITS DROPSTATUS
79 %token DNS SERVICE PORT UIDL INTERVAL
87 statement_list : statement
88 | statement_list statement
91 optmap : MAP | /* EMPTY */;
93 /* future global options should also have the form SET <name> optmap <value> */
94 statement : SET LOGFILE optmap STRING {logfile = xstrdup($4);}
95 | SET DAEMON optmap NUMBER {poll_interval = $4;}
96 | SET SYSLOG {errors_to_syslog = TRUE;}
97 | SET INVISIBLE {use_invisible = TRUE;}
100 * The way the next two productions are written depends on the fact that
101 * userspecs cannot be empty. It's a kluge to deal with files that set
102 * up a load of defaults and then have poll statements following with no
103 * user options at all.
105 | define_server serverspecs {record_current();}
106 | define_server serverspecs userspecs
108 /* detect and complain about the most common user error */
109 | define_server serverspecs userspecs serv_option
110 {yyerror("server option after user options");}
113 define_server : POLL STRING {reset_server($2, FALSE);}
114 | SKIP STRING {reset_server($2, TRUE);}
115 | DEFAULTS {reset_server("defaults", FALSE);}
118 serverspecs : /* EMPTY */
119 | serverspecs serv_option
122 alias_list : STRING {save_str(¤t.server.akalist,-1,$1);}
123 | alias_list STRING {save_str(¤t.server.akalist,-1,$2);}
126 domain_list : STRING {save_str(¤t.server.localdomains,-1,$1);}
127 | domain_list STRING {save_str(¤t.server.localdomains,-1,$2);}
130 serv_option : AKA alias_list
131 | VIA STRING {current.server.via = xstrdup($2);}
132 | LOCALDOMAINS domain_list
133 | PROTOCOL PROTO {current.server.protocol = $2;}
135 current.server.protocol = P_POP3;
136 current.server.preauthenticate = A_KERBEROS_V4;
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 | TIMEOUT NUMBER {current.server.timeout = $2;}
160 | ENVELOPE NUMBER STRING
162 current.server.envelope =
164 current.server.envskip = $2;
168 current.server.envelope =
170 current.server.envskip = 0;
173 | QVIRTUAL STRING {current.server.qvirtual=xstrdup($2);}
176 if (net_security_strtorequest(options, request, &requestlen))
177 yyerror("invalid security request");
179 current.server.netsec =
182 yyerror("network-security support disabled")
183 #endif /* NET_SECURITY */
186 #if defined(linux) && !defined(INET6)
187 interface_parse($2, ¤t.server);
188 #else /* defined(linux) && !defined(INET6) */
189 fprintf(stderr, "fetchmail: interface option is only supported under Linux\n");
190 #endif /* defined(linux) && !defined(INET6) */
193 #if defined(linux) && !defined(INET6)
194 current.server.monitor = xstrdup($2);
195 #else /* defined(linux) && !defined(INET6) */
196 fprintf(stderr, "fetchmail: monitor option is only supported under Linux\n");
197 #endif /* defined(linux) && !defined(INET6) */
199 | DNS {current.server.dns = FLAG_TRUE;}
200 | NO DNS {current.server.dns = FLAG_FALSE;}
201 | NO ENVELOPE {current.server.envelope = STRING_DISABLED;}
204 userspecs : user1opts {record_current(); user_reset();}
208 explicits : explicitdef {record_current(); user_reset();}
209 | explicits explicitdef {record_current(); user_reset();}
212 explicitdef : userdef user0opts
215 userdef : USERNAME STRING {current.remotename = xstrdup($2);}
216 | USERNAME mapping_list HERE
217 | USERNAME STRING THERE {current.remotename = xstrdup($2);}
220 user0opts : /* EMPTY */
221 | user0opts user_option
224 user1opts : user_option
225 | user1opts user_option
228 localnames : WILDCARD {current.wildcard = TRUE;}
229 | mapping_list {current.wildcard = FALSE;}
230 | mapping_list WILDCARD {current.wildcard = TRUE;}
233 mapping_list : mapping
234 | mapping_list mapping
238 {save_str_pair(¤t.localnames, $1, NULL);}
240 {save_str_pair(¤t.localnames, $1, $3);}
243 folder_list : STRING {save_str(¤t.mailboxes,-1,$1);}
244 | folder_list STRING {save_str(¤t.mailboxes,-1,$2);}
247 smtp_list : STRING {save_str(¤t.smtphunt, TRUE,$1);}
248 | smtp_list STRING {save_str(¤t.smtphunt, TRUE,$2);}
251 user_option : TO localnames HERE
256 | IS STRING THERE {current.remotename = xstrdup($2);}
257 | PASSWORD STRING {current.password = xstrdup($2);}
260 | SMTPADDRESS STRING {current.smtpaddress = xstrdup($2);}
261 | MDA STRING {current.mda = xstrdup($2);}
262 | PRECONNECT STRING {current.preconnect = xstrdup($2);}
263 | POSTCONNECT STRING {current.postconnect = xstrdup($2);}
265 | KEEP {current.keep = FLAG_TRUE;}
266 | FLUSH {current.flush = FLAG_TRUE;}
267 | FETCHALL {current.fetchall = FLAG_TRUE;}
268 | REWRITE {current.rewrite = FLAG_TRUE;}
269 | FORCECR {current.forcecr = FLAG_TRUE;}
270 | STRIPCR {current.stripcr = FLAG_TRUE;}
271 | PASS8BITS {current.pass8bits = FLAG_TRUE;}
272 | DROPSTATUS {current.dropstatus = FLAG_TRUE;}
274 | NO KEEP {current.keep = FLAG_FALSE;}
275 | NO FLUSH {current.flush = FLAG_FALSE;}
276 | NO FETCHALL {current.fetchall = FLAG_FALSE;}
277 | NO REWRITE {current.rewrite = FLAG_FALSE;}
278 | NO FORCECR {current.forcecr = FLAG_FALSE;}
279 | NO STRIPCR {current.stripcr = FLAG_FALSE;}
280 | NO PASS8BITS {current.pass8bits = FLAG_FALSE;}
281 | NO DROPSTATUS {current.dropstatus = FLAG_FALSE;}
283 | LIMIT NUMBER {current.limit = NUM_VALUE($2);}
284 | FETCHLIMIT NUMBER {current.fetchlimit = NUM_VALUE($2);}
285 | BATCHLIMIT NUMBER {current.batchlimit = NUM_VALUE($2);}
286 | EXPUNGE NUMBER {current.expunge = NUM_VALUE($2);}
290 /* lexer interface */
292 extern int prc_lineno;
296 static struct query *hosttail; /* where to add new elements */
298 void yyerror (const char *s)
299 /* report a syntax error */
301 error_at_line( 0, 0, rcfile, prc_lineno, "%s at %s", s,
302 (yytext && yytext[0]) ? yytext : "end of input");
306 int prc_filecheck(pathname)
307 /* check that a configuration file is secure */
308 const char *pathname; /* pathname for the configuration file */
314 /* special cases useful for debugging purposes */
315 if (strcmp("/dev/null", pathname) == 0)
318 /* the run control file must have the same uid as the REAL uid of this
319 process, it must have permissions no greater than 600, and it must not
320 be a symbolic link. We check these conditions here. */
322 if (lstat(pathname, &statbuf) < 0) {
326 error(0, errno, "lstat: %s", pathname);
331 if ((statbuf.st_mode & S_IFLNK) == S_IFLNK) {
332 fprintf(stderr, "File %s must not be a symbolic link.\n", pathname);
336 if (statbuf.st_mode & ~(S_IFREG | S_IREAD | S_IWRITE)) {
337 fprintf(stderr, "File %s must have no more than -rw------ (0600) permissions.\n",
342 if (statbuf.st_uid != getuid()) {
343 fprintf(stderr, "File %s must be owned by you.\n", pathname);
350 int prc_parse_file (const char *pathname, const flag securecheck)
351 /* digest the configuration into a linked list of host records */
354 querylist = hosttail = (struct query *)NULL;
358 /* Check that the file is secure */
359 if (securecheck && (prc_errflag = prc_filecheck(pathname)) != 0)
365 /* Open the configuration and feed it to the lexer. */
366 if ((yyin = fopen(pathname,"r")) == (FILE *)NULL) {
367 error(0, errno, "open: %s", pathname);
371 yyparse(); /* parse entire file */
381 static void reset_server(char *name, int skip)
382 /* clear the entire global record and initialize it with a new name */
385 memset(¤t,'\0',sizeof(current));
386 current.smtp_socket = -1;
387 current.server.pollname = xstrdup(name);
388 current.server.skip = skip;
392 static void user_reset(void)
393 /* clear the global current record (user parameters) used by the parser */
395 struct hostdata save;
398 * Purpose of this code is to initialize the new server block, but
399 * preserve whatever server name was previously set. Also
400 * preserve server options unless the command-line explicitly
403 save = current.server;
405 memset(¤t, '\0', sizeof(current));
406 current.smtp_socket = -1;
408 current.server = save;
411 struct query *hostalloc(init)
412 /* append a host record to the host list */
413 struct query *init; /* pointer to block containing initial values */
417 /* allocate new node */
418 node = (struct query *) xmalloc(sizeof(struct query));
421 memcpy(node, init, sizeof(struct query));
423 /* append to end of list */
424 if (hosttail != (struct query *) 0)
425 hosttail->next = node; /* list contains at least one element */
427 querylist = node; /* list is empty */
431 node->server.lead_server = leadentry;
434 node->server.lead_server = NULL;
435 leadentry = &node->server;
441 static void record_current(void)
442 /* register current parameters and append to the host list */
444 #define FLAG_FORCE(fld) if (cmd_opts.fld) current.fld = cmd_opts.fld
445 FLAG_FORCE(server.via);
446 FLAG_FORCE(server.protocol);
448 FLAG_FORCE(server.service);
449 FLAG_FORCE(server.netsec);
451 FLAG_FORCE(server.port);
453 FLAG_FORCE(server.interval);
454 FLAG_FORCE(server.preauthenticate);
455 FLAG_FORCE(server.timeout);
456 FLAG_FORCE(server.envelope);
457 FLAG_FORCE(server.envskip);
458 FLAG_FORCE(server.qvirtual);
459 FLAG_FORCE(server.skip);
460 FLAG_FORCE(server.dns);
461 FLAG_FORCE(server.uidl);
464 FLAG_FORCE(server.interface);
465 FLAG_FORCE(server.monitor);
466 FLAG_FORCE(server.interface_pair);
469 FLAG_FORCE(remotename);
470 FLAG_FORCE(password);
471 if (cmd_opts.mailboxes)
472 current.mailboxes = cmd_opts.mailboxes;
473 if (cmd_opts.smtphunt)
474 current.smtphunt = cmd_opts.smtphunt;
476 FLAG_FORCE(smtpaddress);
477 FLAG_FORCE(preconnect);
478 FLAG_FORCE(postconnect);
482 FLAG_FORCE(fetchall);
486 FLAG_FORCE(pass8bits);
487 FLAG_FORCE(dropstatus);
489 FLAG_FORCE(fetchlimit);
490 FLAG_FORCE(batchlimit);
495 (void) hostalloc(¤t);
500 void optmerge(struct query *h2, struct query *h1)
501 /* merge two options records; empty fields in h2 are filled in from h1 */
503 append_str_list(&h2->server.localdomains, &h1->server.localdomains);
504 append_str_list(&h2->localnames, &h1->localnames);
505 append_str_list(&h2->mailboxes, &h1->mailboxes);
506 append_str_list(&h2->smtphunt, &h1->smtphunt);
508 #define FLAG_MERGE(fld) if (!h2->fld) h2->fld = h1->fld
509 FLAG_MERGE(server.via);
510 FLAG_MERGE(server.protocol);
512 FLAG_MERGE(server.service);
513 FLAG_MERGE(server.netsec);
515 FLAG_MERGE(server.port);
517 FLAG_MERGE(server.interval);
518 FLAG_MERGE(server.preauthenticate);
519 FLAG_MERGE(server.timeout);
520 FLAG_MERGE(server.envelope);
521 FLAG_MERGE(server.envskip);
522 FLAG_MERGE(server.qvirtual);
523 FLAG_MERGE(server.skip);
524 FLAG_MERGE(server.dns);
525 FLAG_MERGE(server.uidl);
528 FLAG_MERGE(server.interface);
529 FLAG_MERGE(server.monitor);
530 FLAG_MERGE(server.interface_pair);
533 FLAG_MERGE(remotename);
534 FLAG_MERGE(password);
536 FLAG_MERGE(smtpaddress);
537 FLAG_MERGE(preconnect);
541 FLAG_MERGE(fetchall);
545 FLAG_MERGE(pass8bits);
546 FLAG_MERGE(dropstatus);
548 FLAG_MERGE(fetchlimit);
549 FLAG_MERGE(batchlimit);
554 /* easier to do this than cope with variations in where the library lives */
555 int yywrap(void) {return 1;}
557 /* rcfile_y.y ends here */