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 struct query *querylist; /* head of server list (globally visible) */
38 int yydebug; /* in case we didn't generate with -- debug */
40 static struct query current; /* current server record */
41 static int prc_errflag;
42 static struct hostdata *leadentry;
45 static void record_current(void);
46 static void user_reset(void);
47 static void reset_server(const char *name, int skip);
49 /* using Bison, this arranges that yydebug messages will show actual tokens */
51 #define YYPRINT(fp, type, val) fprintf(fp, " = \"%s\"", yytext)
60 %token DEFAULTS POLL SKIP VIA AKA LOCALDOMAINS PROTOCOL
61 %token AUTHENTICATE TIMEOUT KPOP SDPS KERBEROS4 KERBEROS5 KERBEROS
62 %token ENVELOPE QVIRTUAL USERNAME PASSWORD FOLDER SMTPHOST MDA BSMTP
63 %token SMTPADDRESS SPAMRESPONSE PRECONNECT POSTCONNECT LIMIT
64 %token NETSEC INTERFACE MONITOR PLUGIN PLUGOUT
65 %token IS HERE THERE TO MAP WILDCARD
66 %token BATCHLIMIT FETCHLIMIT EXPUNGE PROPERTIES
67 %token SET LOGFILE DAEMON SYSLOG IDFILE INVISIBLE POSTMASTER WARNINGS
70 %token <number> NUMBER
71 %token NO KEEP FLUSH FETCHALL REWRITE FORCECR STRIPCR PASS8BITS DROPSTATUS
72 %token DNS SERVICE PORT UIDL INTERVAL MIMEDECODE CHECKALIAS
80 statement_list : statement
81 | statement_list statement
84 optmap : MAP | /* EMPTY */;
86 /* future global options should also have the form SET <name> optmap <value> */
87 statement : SET LOGFILE optmap STRING {run.logfile = xstrdup($4);}
88 | SET IDFILE optmap STRING {run.idfile = xstrdup($4);}
89 | SET DAEMON optmap NUMBER {run.poll_interval = $4;}
90 | SET POSTMASTER optmap STRING {run.postmaster = xstrdup($4);}
91 | SET SYSLOG {run.use_syslog = TRUE;}
92 | SET INVISIBLE {run.invisible = TRUE;}
95 * The way the next two productions are written depends on the fact that
96 * userspecs cannot be empty. It's a kluge to deal with files that set
97 * up a load of defaults and then have poll statements following with no
98 * user options at all.
100 | define_server serverspecs {record_current();}
101 | define_server serverspecs userspecs
103 /* detect and complain about the most common user error */
104 | define_server serverspecs userspecs serv_option
105 {yyerror("server option after user options");}
108 define_server : POLL STRING {reset_server($2, FALSE);}
109 | SKIP STRING {reset_server($2, TRUE);}
110 | DEFAULTS {reset_server("defaults", FALSE);}
113 serverspecs : /* EMPTY */
114 | serverspecs serv_option
117 alias_list : STRING {save_str(¤t.server.akalist,$1,0);}
118 | alias_list STRING {save_str(¤t.server.akalist,$2,0);}
121 domain_list : STRING {save_str(¤t.server.localdomains,$1,0);}
122 | domain_list STRING {save_str(¤t.server.localdomains,$2,0);}
125 serv_option : AKA alias_list
126 | VIA STRING {current.server.via = xstrdup($2);}
127 | LOCALDOMAINS domain_list
128 | PROTOCOL PROTO {current.server.protocol = $2;}
130 current.server.protocol = P_POP3;
132 if (current.server.preauthenticate == A_PASSWORD)
134 current.server.preauthenticate = A_KERBEROS_V5;
136 current.server.preauthenticate = A_KERBEROS_V4;
137 #endif /* KERBEROS_V5 */
139 current.server.service = KPOP_PORT;
141 current.server.port = KPOP_PORT;
146 current.server.protocol = P_POP3;
147 current.server.sdps = TRUE;
149 yyerror("SDPS not enabled.");
150 #endif /* SDPS_ENABLE */
152 | UIDL {current.server.uidl = FLAG_TRUE;}
153 | NO UIDL {current.server.uidl = FLAG_FALSE;}
154 | CHECKALIAS {current.server.checkalias = FLAG_TRUE;}
155 | NO CHECKALIAS {current.server.checkalias = FLAG_FALSE;}
158 current.server.service = $2;
163 current.server.port = $2;
166 | INTERVAL NUMBER {current.server.interval = $2;}
167 | AUTHENTICATE PASSWORD {current.server.preauthenticate = A_PASSWORD;}
168 | AUTHENTICATE KERBEROS4 {current.server.preauthenticate = A_KERBEROS_V4;}
169 | AUTHENTICATE KERBEROS5 {current.server.preauthenticate = A_KERBEROS_V5;}
170 | AUTHENTICATE KERBEROS {
172 current.server.preauthenticate = A_KERBEROS_V5;
174 current.server.preauthenticate = A_KERBEROS_V4;
175 #endif /* KERBEROS_V5 */
177 | TIMEOUT NUMBER {current.server.timeout = $2;}
179 | ENVELOPE NUMBER STRING
181 current.server.envelope =
183 current.server.envskip = $2;
187 current.server.envelope =
189 current.server.envskip = 0;
192 | QVIRTUAL STRING {current.server.qvirtual=xstrdup($2);}
198 if (net_security_strtorequest($2, &request, &requestlen))
199 yyerror("invalid security request");
201 current.server.netsec = xstrdup($2);
205 yyerror("network-security support disabled");
206 #endif /* NET_SECURITY */
209 #if defined(linux) && !defined(INET6)
210 interface_parse($2, ¤t.server);
211 #else /* defined(linux) && !defined(INET6) */
212 fprintf(stderr, "fetchmail: interface option is only supported under Linux\n");
213 #endif /* defined(linux) && !defined(INET6) */
216 #if defined(linux) && !defined(INET6)
217 current.server.monitor = xstrdup($2);
218 #else /* defined(linux) && !defined(INET6) */
219 fprintf(stderr, "fetchmail: monitor option is only supported under Linux\n");
220 #endif /* defined(linux) && !defined(INET6) */
222 | PLUGIN STRING { current.server.plugin = xstrdup($2); }
223 | PLUGOUT STRING { current.server.plugout = xstrdup($2); }
224 | DNS {current.server.dns = FLAG_TRUE;}
225 | NO DNS {current.server.dns = FLAG_FALSE;}
226 | NO ENVELOPE {current.server.envelope = STRING_DISABLED;}
229 userspecs : user1opts {record_current(); user_reset();}
233 explicits : explicitdef {record_current(); user_reset();}
234 | explicits explicitdef {record_current(); user_reset();}
237 explicitdef : userdef user0opts
240 userdef : USERNAME STRING {current.remotename = xstrdup($2);}
241 | USERNAME mapping_list HERE
242 | USERNAME STRING THERE {current.remotename = xstrdup($2);}
245 user0opts : /* EMPTY */
246 | user0opts user_option
249 user1opts : user_option
250 | user1opts user_option
253 localnames : WILDCARD {current.wildcard = TRUE;}
254 | mapping_list {current.wildcard = FALSE;}
255 | mapping_list WILDCARD {current.wildcard = TRUE;}
258 mapping_list : mapping
259 | mapping_list mapping
263 {save_str_pair(¤t.localnames, $1, NULL);}
265 {save_str_pair(¤t.localnames, $1, $3);}
268 folder_list : STRING {save_str(¤t.mailboxes,$1,0);}
269 | folder_list STRING {save_str(¤t.mailboxes,$2,0);}
272 smtp_list : STRING {save_str(¤t.smtphunt, $1,TRUE);}
273 | smtp_list STRING {save_str(¤t.smtphunt, $2,TRUE);}
279 id=save_str(¤t.antispam,STRING_DUMMY,0);
280 id->val.status.num = $1;
285 id=save_str(¤t.antispam,STRING_DUMMY,0);
286 id->val.status.num = $2;
290 user_option : TO localnames HERE
295 | IS STRING THERE {current.remotename = xstrdup($2);}
296 | PASSWORD STRING {current.password = xstrdup($2);}
299 | SMTPADDRESS STRING {current.smtpaddress = xstrdup($2);}
300 | SPAMRESPONSE num_list
301 | MDA STRING {current.mda = xstrdup($2);}
302 | BSMTP STRING {current.bsmtp = xstrdup($2);}
303 | PRECONNECT STRING {current.preconnect = xstrdup($2);}
304 | POSTCONNECT STRING {current.postconnect = xstrdup($2);}
306 | KEEP {current.keep = FLAG_TRUE;}
307 | FLUSH {current.flush = FLAG_TRUE;}
308 | FETCHALL {current.fetchall = FLAG_TRUE;}
309 | REWRITE {current.rewrite = FLAG_TRUE;}
310 | FORCECR {current.forcecr = FLAG_TRUE;}
311 | STRIPCR {current.stripcr = FLAG_TRUE;}
312 | PASS8BITS {current.pass8bits = FLAG_TRUE;}
313 | DROPSTATUS {current.dropstatus = FLAG_TRUE;}
314 | MIMEDECODE {current.mimedecode = FLAG_TRUE;}
316 | NO KEEP {current.keep = FLAG_FALSE;}
317 | NO FLUSH {current.flush = FLAG_FALSE;}
318 | NO FETCHALL {current.fetchall = FLAG_FALSE;}
319 | NO REWRITE {current.rewrite = FLAG_FALSE;}
320 | NO FORCECR {current.forcecr = FLAG_FALSE;}
321 | NO STRIPCR {current.stripcr = FLAG_FALSE;}
322 | NO PASS8BITS {current.pass8bits = FLAG_FALSE;}
323 | NO DROPSTATUS {current.dropstatus = FLAG_FALSE;}
324 | NO MIMEDECODE {current.mimedecode = FLAG_FALSE;}
326 | LIMIT NUMBER {current.limit = NUM_VALUE($2);}
327 | WARNINGS NUMBER {current.warnings = NUM_VALUE($2);}
328 | FETCHLIMIT NUMBER {current.fetchlimit = NUM_VALUE($2);}
329 | BATCHLIMIT NUMBER {current.batchlimit = NUM_VALUE($2);}
330 | EXPUNGE NUMBER {current.expunge = NUM_VALUE($2);}
332 | PROPERTIES STRING {current.properties = xstrdup($2);}
336 /* lexer interface */
338 extern int prc_lineno;
342 static struct query *hosttail; /* where to add new elements */
344 void yyerror (const char *s)
345 /* report a syntax error */
347 error_at_line( 0, 0, rcfile, prc_lineno, "%s at %s", s,
348 (yytext && yytext[0]) ? yytext : "end of input");
352 int prc_filecheck(const char *pathname, const flag securecheck)
353 /* check that a configuration file is secure */
360 /* special cases useful for debugging purposes */
361 if (strcmp("/dev/null", pathname) == 0)
364 /* the run control file must have the same uid as the REAL uid of this
365 process, it must have permissions no greater than 600, and it must not
366 be a symbolic link. We check these conditions here. */
368 if (lstat(pathname, &statbuf) < 0) {
372 error(0, errno, "lstat: %s", pathname);
377 if (!securecheck) return 0;
379 if ((statbuf.st_mode & S_IFLNK) == S_IFLNK)
381 fprintf(stderr, "File %s must not be a symbolic link.\n", pathname);
385 if (statbuf.st_mode & ~(S_IFREG | S_IREAD | S_IWRITE | S_IEXEC | S_IXGRP))
387 fprintf(stderr, "File %s must have no more than -rwx--x--- (0710) permissions.\n",
392 if (statbuf.st_uid != getuid())
394 fprintf(stderr, "File %s must be owned by you.\n", pathname);
401 int prc_parse_file (const char *pathname, const flag securecheck)
402 /* digest the configuration into a linked list of host records */
405 querylist = hosttail = (struct query *)NULL;
409 /* Check that the file is secure */
410 if ( (prc_errflag = prc_filecheck(pathname, securecheck)) != 0 )
416 /* Open the configuration and feed it to the lexer. */
417 if ((yyin = fopen(pathname,"r")) == (FILE *)NULL) {
418 error(0, errno, "open: %s", pathname);
422 yyparse(); /* parse entire file */
432 static void reset_server(const char *name, int skip)
433 /* clear the entire global record and initialize it with a new name */
436 memset(¤t,'\0',sizeof(current));
437 current.smtp_socket = -1;
438 current.server.pollname = xstrdup(name);
439 current.server.skip = skip;
443 static void user_reset(void)
444 /* clear the global current record (user parameters) used by the parser */
446 struct hostdata save;
449 * Purpose of this code is to initialize the new server block, but
450 * preserve whatever server name was previously set. Also
451 * preserve server options unless the command-line explicitly
454 save = current.server;
456 memset(¤t, '\0', sizeof(current));
457 current.smtp_socket = -1;
459 current.server = save;
462 struct query *hostalloc(init)
463 /* append a host record to the host list */
464 struct query *init; /* pointer to block containing initial values */
468 /* allocate new node */
469 node = (struct query *) xmalloc(sizeof(struct query));
473 memcpy(node, init, sizeof(struct query));
476 memset(node, '\0', sizeof(struct query));
477 node->smtp_socket = -1;
480 /* append to end of list */
481 if (hosttail != (struct query *) 0)
482 hosttail->next = node; /* list contains at least one element */
484 querylist = node; /* list is empty */
488 node->server.lead_server = leadentry;
491 node->server.lead_server = NULL;
492 leadentry = &node->server;
498 static void record_current(void)
499 /* register current parameters and append to the host list */
501 (void) hostalloc(¤t);
505 /* easier to do this than cope with variations in where the library lives */
506 int yywrap(void) {return 1;}
508 /* rcfile_y.y ends here */