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>
15 #if defined(STDC_HEADERS)
18 #if defined(HAVE_UNISTD_H)
23 #include "fetchmail.h"
25 /* parser reads these */
26 char *rcfile; /* path name of rc file */
27 struct query cmd_opts; /* where to put command-line info */
29 /* parser sets these */
30 int poll_interval; /* poll interval in seconds */
31 char *logfile; /* log file for daemon mode */
32 flag use_syslog; /* if syslog was set */
33 struct query *querylist; /* head of server list (globally visible) */
35 int yydebug; /* in case we didn't generate with -- debug */
37 static struct query current; /* current server record */
38 static int prc_errflag;
39 static struct hostdata *leadentry;
42 static void record_current();
43 static void user_reset();
44 static void reset_server(char *name, int skip);
46 /* using Bison, this arranges that yydebug messages will show actual tokens */
48 #define YYPRINT(fp, type, val) fprintf(fp, " = \"%s\"", yytext)
57 %token DEFAULTS POLL SKIP VIA AKA LOCALDOMAINS PROTOCOL
58 %token AUTHENTICATE TIMEOUT KPOP KERBEROS4
59 %token ENVELOPE QVIRTUAL USERNAME PASSWORD FOLDER SMTPHOST MDA
60 %token PRECONNECT POSTCONNECT LIMIT
61 %token IS HERE THERE TO MAP WILDCARD
62 %token BATCHLIMIT FETCHLIMIT EXPUNGE
63 %token SET LOGFILE DAEMON SYSLOG INTERFACE MONITOR
66 %token <number> NUMBER
67 %token NO KEEP FLUSH FETCHALL REWRITE FORCECR STRIPCR PASS8BITS DROPSTATUS
68 %token DNS PORT UIDL INTERVAL
76 statement_list : statement
77 | statement_list statement
80 optmap : MAP | /* EMPTY */;
82 /* future global options should also have the form SET <name> optmap <value> */
83 statement : SET LOGFILE optmap STRING {logfile = xstrdup($4);}
84 | SET DAEMON optmap NUMBER {poll_interval = $4;}
85 | SET SYSLOG {use_syslog = TRUE;}
88 * The way the next two productions are written depends on the fact that
89 * userspecs cannot be empty. It's a kluge to deal with files that set
90 * up a load of defaults and then have poll statements following with no
91 * user options at all.
93 | define_server serverspecs {record_current();}
94 | define_server serverspecs userspecs
96 /* detect and complain about the most common user error */
97 | define_server serverspecs userspecs serv_option
98 {yyerror("server option after user options");}
101 define_server : POLL STRING {reset_server($2, FALSE);}
102 | SKIP STRING {reset_server($2, TRUE);}
103 | DEFAULTS {reset_server("defaults", FALSE);}
106 serverspecs : /* EMPTY */
107 | serverspecs serv_option
110 alias_list : STRING {save_str(¤t.server.akalist,-1,$1);}
111 | alias_list STRING {save_str(¤t.server.akalist,-1,$2);}
114 domain_list : STRING {save_str(¤t.server.localdomains,-1,$1);}
115 | domain_list STRING {save_str(¤t.server.localdomains,-1,$2);}
118 serv_option : AKA alias_list
119 | VIA STRING {current.server.via = xstrdup($2);}
120 | LOCALDOMAINS domain_list
121 | PROTOCOL PROTO {current.server.protocol = $2;}
123 current.server.protocol = P_POP3;
124 current.server.preauthenticate = A_KERBEROS_V4;
125 current.server.port = KPOP_PORT;
127 | UIDL {current.server.uidl = FLAG_TRUE;}
128 | NO UIDL {current.server.uidl = FLAG_FALSE;}
129 | PORT NUMBER {current.server.port = $2;}
130 | INTERVAL NUMBER {current.server.interval = $2;}
131 | AUTHENTICATE PASSWORD {current.server.preauthenticate = A_PASSWORD;}
132 | AUTHENTICATE KERBEROS4 {current.server.preauthenticate = A_KERBEROS_V4;}
133 | TIMEOUT NUMBER {current.server.timeout = $2;}
134 | ENVELOPE STRING {current.server.envelope = xstrdup($2);}
135 | QVIRTUAL STRING {current.server.qvirtual = xstrdup($2);}
138 interface_parse($2, ¤t.server);
140 fprintf(stderr, "fetchmail: interface option is only supported under Linux\n");
145 current.server.monitor = xstrdup($2);
147 fprintf(stderr, "fetchmail: monitor option is only supported under Linux\n");
150 | DNS {current.server.dns = FLAG_TRUE;}
151 | NO DNS {current.server.dns = FLAG_FALSE;}
152 | NO ENVELOPE {current.server.envelope = STRING_DISABLED;}
155 userspecs : user1opts {record_current(); user_reset();}
159 explicits : explicitdef {record_current(); user_reset();}
160 | explicits explicitdef {record_current(); user_reset();}
163 explicitdef : userdef user0opts
166 userdef : USERNAME STRING {current.remotename = xstrdup($2);}
167 | USERNAME mapping_list HERE
168 | USERNAME STRING THERE {current.remotename = xstrdup($2);}
171 user0opts : /* EMPTY */
172 | user0opts user_option
175 user1opts : user_option
176 | user1opts user_option
179 localnames : WILDCARD {current.wildcard = TRUE;}
180 | mapping_list {current.wildcard = FALSE;}
181 | mapping_list WILDCARD {current.wildcard = TRUE;}
184 mapping_list : mapping
185 | mapping_list mapping
189 {save_str_pair(¤t.localnames, $1, NULL);}
191 {save_str_pair(¤t.localnames, $1, $3);}
194 folder_list : STRING {save_str(¤t.mailboxes,-1,$1);}
195 | folder_list STRING {save_str(¤t.mailboxes,-1,$2);}
198 smtp_list : STRING {save_str(¤t.smtphunt, TRUE,$1);}
199 | smtp_list STRING {save_str(¤t.smtphunt, TRUE,$2);}
202 user_option : TO localnames HERE
207 | IS STRING THERE {current.remotename = xstrdup($2);}
208 | PASSWORD STRING {current.password = xstrdup($2);}
211 | MDA STRING {current.mda = xstrdup($2);}
212 | PRECONNECT STRING {current.preconnect = xstrdup($2);}
213 | POSTCONNECT STRING {current.postconnect = xstrdup($2);}
215 | KEEP {current.keep = FLAG_TRUE;}
216 | FLUSH {current.flush = FLAG_TRUE;}
217 | FETCHALL {current.fetchall = FLAG_TRUE;}
218 | REWRITE {current.rewrite = FLAG_TRUE;}
219 | FORCECR {current.forcecr = FLAG_TRUE;}
220 | STRIPCR {current.stripcr = FLAG_TRUE;}
221 | PASS8BITS {current.pass8bits = FLAG_TRUE;}
222 | DROPSTATUS {current.dropstatus = FLAG_TRUE;}
224 | NO KEEP {current.keep = FLAG_FALSE;}
225 | NO FLUSH {current.flush = FLAG_FALSE;}
226 | NO FETCHALL {current.fetchall = FLAG_FALSE;}
227 | NO REWRITE {current.rewrite = FLAG_FALSE;}
228 | NO FORCECR {current.forcecr = FLAG_FALSE;}
229 | NO STRIPCR {current.stripcr = FLAG_FALSE;}
230 | NO PASS8BITS {current.pass8bits = FLAG_FALSE;}
231 | NO DROPSTATUS {current.dropstatus = FLAG_FALSE;}
233 | LIMIT NUMBER {current.limit = $2;}
234 | FETCHLIMIT NUMBER {current.fetchlimit = $2;}
235 | BATCHLIMIT NUMBER {current.batchlimit = $2;}
236 | EXPUNGE NUMBER {current.expunge = $2;}
240 /* lexer interface */
242 extern int prc_lineno;
246 static struct query *hosttail; /* where to add new elements */
248 void yyerror (const char *s)
249 /* report a syntax error */
251 error_at_line( 0, 0, rcfile, prc_lineno, "%s at %s", s,
252 (yytext && yytext[0]) ? yytext : "end of input");
256 int prc_filecheck(pathname)
257 /* check that a configuration file is secure */
258 const char *pathname; /* pathname for the configuration file */
264 /* special cases useful for debugging purposes */
265 if (strcmp("/dev/null", pathname) == 0)
268 /* the run control file must have the same uid as the REAL uid of this
269 process, it must have permissions no greater than 600, and it must not
270 be a symbolic link. We check these conditions here. */
272 if (lstat(pathname, &statbuf) < 0) {
276 error(0, errno, "lstat: %s", pathname);
281 if ((statbuf.st_mode & S_IFLNK) == S_IFLNK) {
282 fprintf(stderr, "File %s must not be a symbolic link.\n", pathname);
286 if (statbuf.st_mode & ~(S_IFREG | S_IREAD | S_IWRITE)) {
287 fprintf(stderr, "File %s must have no more than -rw------ (0600) permissions.\n",
292 if (statbuf.st_uid != getuid()) {
293 fprintf(stderr, "File %s must be owned by you.\n", pathname);
300 int prc_parse_file (const char *pathname, const flag securecheck)
301 /* digest the configuration into a linked list of host records */
304 querylist = hosttail = (struct query *)NULL;
308 /* Check that the file is secure */
309 if (securecheck && (prc_errflag = prc_filecheck(pathname)) != 0)
315 /* Open the configuration and feed it to the lexer. */
316 if ((yyin = fopen(pathname,"r")) == (FILE *)NULL) {
317 error(0, errno, "open: %s", pathname);
321 yyparse(); /* parse entire file */
331 static void reset_server(char *name, int skip)
332 /* clear the entire global record and initialize it with a new name */
335 memset(¤t,'\0',sizeof(current));
336 current.smtp_socket = -1;
337 current.server.pollname = xstrdup(name);
338 current.server.skip = skip;
342 static void user_reset(void)
343 /* clear the global current record (user parameters) used by the parser */
345 struct hostdata save;
348 * Purpose of this code is to initialize the new server block, but
349 * preserve whatever server name was previously set. Also
350 * preserve server options unless the command-line explicitly
353 save = current.server;
355 memset(¤t, '\0', sizeof(current));
356 current.smtp_socket = -1;
358 current.server = save;
361 struct query *hostalloc(init)
362 /* append a host record to the host list */
363 struct query *init; /* pointer to block containing initial values */
367 /* allocate new node */
368 node = (struct query *) xmalloc(sizeof(struct query));
371 memcpy(node, init, sizeof(struct query));
373 /* append to end of list */
374 if (hosttail != (struct query *) 0)
375 hosttail->next = node; /* list contains at least one element */
377 querylist = node; /* list is empty */
381 node->server.lead_server = leadentry;
384 node->server.lead_server = NULL;
385 leadentry = &node->server;
391 static void record_current(void)
392 /* register current parameters and append to the host list */
394 #define FLAG_FORCE(fld) if (cmd_opts.fld) current.fld = cmd_opts.fld
395 FLAG_FORCE(server.via);
396 FLAG_FORCE(server.protocol);
397 FLAG_FORCE(server.port);
398 FLAG_FORCE(server.interval);
399 FLAG_FORCE(server.preauthenticate);
400 FLAG_FORCE(server.timeout);
401 FLAG_FORCE(server.envelope);
402 FLAG_FORCE(server.qvirtual);
403 FLAG_FORCE(server.skip);
404 FLAG_FORCE(server.dns);
405 FLAG_FORCE(server.uidl);
408 FLAG_FORCE(server.interface);
409 FLAG_FORCE(server.monitor);
410 FLAG_FORCE(server.interface_pair);
413 FLAG_FORCE(remotename);
414 FLAG_FORCE(password);
415 if (cmd_opts.mailboxes)
416 save_str(¤t.mailboxes, -1, cmd_opts.mailboxes->id);
417 if (cmd_opts.smtphunt)
418 save_str(¤t.smtphunt, -1, cmd_opts.smtphunt->id);
420 FLAG_FORCE(preconnect);
421 FLAG_FORCE(postconnect);
425 FLAG_FORCE(fetchall);
429 FLAG_FORCE(pass8bits);
430 FLAG_FORCE(dropstatus);
432 FLAG_FORCE(fetchlimit);
433 FLAG_FORCE(batchlimit);
438 (void) hostalloc(¤t);
443 void optmerge(struct query *h2, struct query *h1)
444 /* merge two options records; empty fields in h2 are filled in from h1 */
446 append_str_list(&h2->server.localdomains, &h1->server.localdomains);
447 append_str_list(&h2->localnames, &h1->localnames);
448 append_str_list(&h2->mailboxes, &h1->mailboxes);
449 append_str_list(&h2->smtphunt, &h1->smtphunt);
451 #define FLAG_MERGE(fld) if (!h2->fld) h2->fld = h1->fld
452 FLAG_MERGE(server.via);
453 FLAG_MERGE(server.protocol);
454 FLAG_MERGE(server.port);
455 FLAG_MERGE(server.interval);
456 FLAG_MERGE(server.preauthenticate);
457 FLAG_MERGE(server.timeout);
458 FLAG_MERGE(server.envelope);
459 FLAG_MERGE(server.qvirtual);
460 FLAG_MERGE(server.skip);
461 FLAG_MERGE(server.dns);
462 FLAG_MERGE(server.uidl);
465 FLAG_MERGE(server.interface);
466 FLAG_MERGE(server.monitor);
467 FLAG_MERGE(server.interface_pair);
470 FLAG_MERGE(remotename);
471 FLAG_MERGE(password);
473 FLAG_MERGE(preconnect);
477 FLAG_MERGE(fetchall);
481 FLAG_MERGE(pass8bits);
482 FLAG_MERGE(dropstatus);
484 FLAG_MERGE(fetchlimit);
485 FLAG_MERGE(batchlimit);
490 /* easier to do this than cope with variations in where the library lives */
491 int yywrap(void) {return 1;}
493 /* rcfile_y.y ends here */