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)
21 #if defined(__CYGWIN__)
22 #include <sys/cygwin.h>
23 #endif /* __CYGWIN__ */
25 #include "fetchmail.h"
28 /* parser reads these */
29 char *rcfile; /* path name of rc file */
30 struct query cmd_opts; /* where to put command-line info */
32 /* parser sets these */
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(void);
43 static void user_reset(void);
44 static void reset_server(const char *name, int skip);
46 /* these should be of size PATH_MAX */
47 char currentwd[1024] = "", rcfiledir[1024] = "";
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 ENVELOPE QVIRTUAL
62 %token USERNAME PASSWORD FOLDER SMTPHOST FETCHDOMAINS MDA BSMTP LMTP
63 %token SMTPADDRESS SMTPNAME SPAMRESPONSE PRECONNECT POSTCONNECT LIMIT WARNINGS
64 %token INTERFACE MONITOR PLUGIN PLUGOUT
65 %token IS HERE THERE TO MAP
66 %token BATCHLIMIT FETCHLIMIT FETCHSIZELIMIT FASTUIDL EXPUNGE PROPERTIES
67 %token SET LOGFILE DAEMON SYSLOG IDFILE PIDFILE INVISIBLE POSTMASTER BOUNCEMAIL
68 %token SPAMBOUNCE SOFTBOUNCE SHOWDOTS
69 %token BADHEADER ACCEPT REJECT_
70 %token <proto> PROTO AUTHTYPE
72 %token <number> NUMBER
73 %token NO KEEP FLUSH LIMITFLUSH FETCHALL REWRITE FORCECR STRIPCR PASS8BITS
74 %token DROPSTATUS DROPDELIVERED
75 %token DNS SERVICE PORT UIDL INTERVAL MIMEDECODE IDLE CHECKALIAS
76 %token SSL SSLKEY SSLCERT SSLPROTO SSLCERTCK SSLCERTFILE SSLCERTPATH SSLCOMMONNAME SSLFINGERPRINT
77 %token PRINCIPAL ESMTPNAME ESMTPPASSWORD
82 %destructor { free ($$); } STRING
90 statement_list : statement
91 | statement_list statement
94 optmap : MAP | /* EMPTY */;
96 /* future global options should also have the form SET <name> optmap <value> */
97 statement : SET LOGFILE optmap STRING {run.logfile = prependdir ($4, rcfiledir); free($4);}
98 | SET IDFILE optmap STRING {run.idfile = prependdir ($4, rcfiledir); free($4);}
99 | SET PIDFILE optmap STRING {run.pidfile = prependdir ($4, rcfiledir); free($4);}
100 | SET DAEMON optmap NUMBER {run.poll_interval = $4;}
101 | SET POSTMASTER optmap STRING {run.postmaster = $4;}
102 | SET BOUNCEMAIL {run.bouncemail = TRUE;}
103 | SET NO BOUNCEMAIL {run.bouncemail = FALSE;}
104 | SET SPAMBOUNCE {run.spambounce = TRUE;}
105 | SET NO SPAMBOUNCE {run.spambounce = FALSE;}
106 | SET SOFTBOUNCE {run.softbounce = TRUE;}
107 | SET NO SOFTBOUNCE {run.softbounce = FALSE;}
108 | SET PROPERTIES optmap STRING {run.properties = $4;}
109 | SET SYSLOG {run.use_syslog = TRUE;}
110 | SET NO SYSLOG {run.use_syslog = FALSE;}
111 | SET INVISIBLE {run.invisible = TRUE;}
112 | SET NO INVISIBLE {run.invisible = FALSE;}
113 | SET SHOWDOTS {run.showdots = FLAG_TRUE;}
114 | SET NO SHOWDOTS {run.showdots = FLAG_FALSE;}
117 * The way the next two productions are written depends on the fact that
118 * userspecs cannot be empty. It's a kluge to deal with files that set
119 * up a load of defaults and then have poll statements following with no
120 * user options at all.
122 | define_server serverspecs {record_current();}
123 | define_server serverspecs userspecs
125 /* detect and complain about the most common user error */
126 | define_server serverspecs userspecs serv_option
127 {yyerror(GT_("server option after user options"));}
130 define_server : POLL STRING {reset_server($2, FALSE); free($2);}
131 | SKIP STRING {reset_server($2, TRUE); free($2);}
132 | DEFAULTS {reset_server("defaults", FALSE);}
135 serverspecs : /* EMPTY */
136 | serverspecs serv_option
139 alias_list : STRING {save_str(¤t.server.akalist,$1,0); free($1);}
140 | alias_list STRING {save_str(¤t.server.akalist,$2,0); free($2);}
143 domain_list : STRING {save_str(¤t.server.localdomains,$1,0); free($1);}
144 | domain_list STRING {save_str(¤t.server.localdomains,$2,0); free($2);}
147 serv_option : AKA alias_list
148 | VIA STRING {current.server.via = $2;}
149 | LOCALDOMAINS domain_list
150 | PROTOCOL PROTO {current.server.protocol = $2;}
152 current.server.protocol = P_POP3;
154 if (current.server.authenticate == A_PASSWORD)
156 current.server.authenticate = A_KERBEROS_V5;
158 current.server.authenticate = A_KERBEROS_V4;
159 #endif /* KERBEROS_V5 */
160 current.server.service = KPOP_PORT;
162 | PRINCIPAL STRING {current.server.principal = $2;}
163 | ESMTPNAME STRING {current.server.esmtp_name = $2;}
164 | ESMTPPASSWORD STRING {current.server.esmtp_password = $2;}
167 current.server.protocol = P_POP3;
168 current.server.sdps = TRUE;
170 yyerror(GT_("SDPS not enabled."));
171 #endif /* SDPS_ENABLE */
173 | UIDL {current.server.uidl = FLAG_TRUE;}
174 | NO UIDL {current.server.uidl = FLAG_FALSE;}
175 | CHECKALIAS {current.server.checkalias = FLAG_TRUE;}
176 | NO CHECKALIAS {current.server.checkalias = FLAG_FALSE;}
178 current.server.service = $2;
183 snprintf(buf, sizeof buf, "%d", port);
184 current.server.service = xstrdup(buf);
189 snprintf(buf, sizeof buf, "%d", port);
190 current.server.service = xstrdup(buf);
193 {current.server.interval = $2;}
194 | AUTHENTICATE AUTHTYPE
195 {current.server.authenticate = $2;}
197 {current.server.timeout = $2;}
198 | ENVELOPE NUMBER STRING
200 current.server.envelope = $3;
201 current.server.envskip = $2;
205 current.server.envelope = $2;
206 current.server.envskip = 0;
209 | QVIRTUAL STRING {current.server.qvirtual = $2;}
212 interface_parse($2, ¤t.server);
214 fprintf(stderr, GT_("fetchmail: interface option is only supported under Linux (without IPv6) and FreeBSD\n"));
220 current.server.monitor = $2;
222 fprintf(stderr, GT_("fetchmail: monitor option is only supported under Linux (without IPv6) and FreeBSD\n"));
226 | PLUGIN STRING { current.server.plugin = $2; }
227 | PLUGOUT STRING { current.server.plugout = $2; }
228 | DNS {current.server.dns = FLAG_TRUE;}
229 | NO DNS {current.server.dns = FLAG_FALSE;}
230 | NO ENVELOPE {current.server.envelope = STRING_DISABLED;}
231 | TRACEPOLLS {current.server.tracepolls = FLAG_TRUE;}
232 | NO TRACEPOLLS {current.server.tracepolls = FLAG_FALSE;}
233 | BADHEADER ACCEPT {current.server.badheader = BHACCEPT;}
234 | BADHEADER REJECT_ {current.server.badheader = BHREJECT;}
237 userspecs : user1opts {record_current(); user_reset();}
241 explicits : explicitdef {record_current(); user_reset();}
242 | explicits explicitdef {record_current(); user_reset();}
245 explicitdef : userdef user0opts
248 userdef : USERNAME STRING {current.remotename = $2;}
249 | USERNAME mapping_list HERE
250 | USERNAME STRING THERE {current.remotename = $2;}
253 user0opts : /* EMPTY */
254 | user0opts user_option
257 user1opts : user_option
258 | user1opts user_option
261 mapping_list : mapping
262 | mapping_list mapping
265 mapping : STRING {if (0 == strcmp($1, "*")) {
266 current.wildcard = TRUE;
268 save_str_pair(¤t.localnames, $1, NULL);
271 | STRING MAP STRING {save_str_pair(¤t.localnames, $1, $3); free($1); free($3);}
274 folder_list : STRING {save_str(¤t.mailboxes,$1,0); free($1);}
275 | folder_list STRING {save_str(¤t.mailboxes,$2,0); free($2);}
278 smtp_list : STRING {save_str(¤t.smtphunt, $1,TRUE); free($1);}
279 | smtp_list STRING {save_str(¤t.smtphunt, $2,TRUE); free($2);}
282 fetch_list : STRING {save_str(¤t.domainlist, $1,TRUE); free($1);}
283 | fetch_list STRING {save_str(¤t.domainlist, $2,TRUE); free($2);}
289 id = save_str(¤t.antispam,STRING_DUMMY,0);
290 id->val.status.num = $1;
295 id = save_str(¤t.antispam,STRING_DUMMY,0);
296 id->val.status.num = $2;
300 user_option : TO mapping_list HERE
302 | IS mapping_list HERE
305 | IS STRING THERE {current.remotename = $2;}
306 | PASSWORD STRING {current.password = $2;}
309 | FETCHDOMAINS fetch_list
310 | SMTPADDRESS STRING {current.smtpaddress = $2;}
311 | SMTPNAME STRING {current.smtpname = $2;}
312 | SPAMRESPONSE num_list
313 | MDA STRING {current.mda = $2;}
314 | BSMTP STRING {current.bsmtp = prependdir ($2, rcfiledir); free($2);}
315 | LMTP {current.listener = LMTP_MODE;}
316 | PRECONNECT STRING {current.preconnect = $2;}
317 | POSTCONNECT STRING {current.postconnect = $2;}
319 | KEEP {current.keep = FLAG_TRUE;}
320 | FLUSH {current.flush = FLAG_TRUE;}
321 | LIMITFLUSH {current.limitflush = FLAG_TRUE;}
322 | FETCHALL {current.fetchall = FLAG_TRUE;}
323 | REWRITE {current.rewrite = FLAG_TRUE;}
324 | FORCECR {current.forcecr = FLAG_TRUE;}
325 | STRIPCR {current.stripcr = FLAG_TRUE;}
326 | PASS8BITS {current.pass8bits = FLAG_TRUE;}
327 | DROPSTATUS {current.dropstatus = FLAG_TRUE;}
328 | DROPDELIVERED {current.dropdelivered = FLAG_TRUE;}
329 | MIMEDECODE {current.mimedecode = FLAG_TRUE;}
330 | IDLE {current.idle = FLAG_TRUE;}
334 current.use_ssl = FLAG_TRUE;
336 yyerror(GT_("SSL is not enabled"));
339 | SSLKEY STRING {current.sslkey = prependdir ($2, rcfiledir); free($2);}
340 | SSLCERT STRING {current.sslcert = prependdir ($2, rcfiledir); free($2);}
341 | SSLPROTO STRING {current.sslproto = $2;}
342 | SSLCERTCK {current.sslcertck = FLAG_TRUE;}
343 | SSLCERTFILE STRING {current.sslcertfile = prependdir($2, rcfiledir); free($2);}
344 | SSLCERTPATH STRING {current.sslcertpath = prependdir($2, rcfiledir); free($2);}
345 | SSLCOMMONNAME STRING {current.sslcommonname = $2;}
346 | SSLFINGERPRINT STRING {current.sslfingerprint = $2;}
348 | NO KEEP {current.keep = FLAG_FALSE;}
349 | NO FLUSH {current.flush = FLAG_FALSE;}
350 | NO LIMITFLUSH {current.limitflush = FLAG_FALSE;}
351 | NO FETCHALL {current.fetchall = FLAG_FALSE;}
352 | NO REWRITE {current.rewrite = FLAG_FALSE;}
353 | NO FORCECR {current.forcecr = FLAG_FALSE;}
354 | NO STRIPCR {current.stripcr = FLAG_FALSE;}
355 | NO PASS8BITS {current.pass8bits = FLAG_FALSE;}
356 | NO DROPSTATUS {current.dropstatus = FLAG_FALSE;}
357 | NO DROPDELIVERED {current.dropdelivered = FLAG_FALSE;}
358 | NO MIMEDECODE {current.mimedecode = FLAG_FALSE;}
359 | NO IDLE {current.idle = FLAG_FALSE;}
361 | NO SSL {current.use_ssl = FLAG_FALSE;}
363 | LIMIT NUMBER {current.limit = NUM_VALUE_IN($2);}
364 | WARNINGS NUMBER {current.warnings = NUM_VALUE_IN($2);}
365 | FETCHLIMIT NUMBER {current.fetchlimit = NUM_VALUE_IN($2);}
366 | FETCHSIZELIMIT NUMBER {current.fetchsizelimit = NUM_VALUE_IN($2);}
367 | FASTUIDL NUMBER {current.fastuidl = NUM_VALUE_IN($2);}
368 | BATCHLIMIT NUMBER {current.batchlimit = NUM_VALUE_IN($2);}
369 | EXPUNGE NUMBER {current.expunge = NUM_VALUE_IN($2);}
371 | PROPERTIES STRING {current.properties = $2;}
375 /* lexer interface */
377 extern int prc_lineno;
381 static struct query *hosttail; /* where to add new elements */
383 void yyerror (const char *s)
384 /* report a syntax error */
386 report_at_line(stderr, 0, rcfile, prc_lineno, GT_("%s at %s"), s,
387 (yytext && yytext[0]) ? yytext : GT_("end of input"));
391 /** check that a configuration file is secure, returns PS_* status codes */
392 int prc_filecheck(const char *pathname,
393 const flag securecheck /** shortcuts permission, filetype and uid tests if false */)
399 /* special case useful for debugging purposes */
400 if (strcmp("/dev/null", pathname) == 0)
403 /* pass through the special name for stdin */
404 if (strcmp("-", pathname) == 0)
407 /* the run control file must have the same uid as the REAL uid of this
408 process, it must have permissions no greater than 600, and it must not
409 be a symbolic link. We check these conditions here. */
411 if (stat(pathname, &statbuf) < 0) {
415 report(stderr, "lstat: %s: %s\n", pathname, strerror(errno));
420 if (!securecheck) return PS_SUCCESS;
422 if (!S_ISREG(statbuf.st_mode))
424 fprintf(stderr, GT_("File %s must be a regular file.\n"), pathname);
429 if (cygwin_internal(CW_CHECK_NTSEC, pathname))
430 #endif /* __CYGWIN__ */
431 if (statbuf.st_mode & (S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | S_IXOTH))
433 fprintf(stderr, GT_("File %s must have no more than -rwx------ (0700) permissions.\n"),
438 if (statbuf.st_uid != geteuid())
440 fprintf(stderr, GT_("File %s must be owned by you.\n"), pathname);
446 int prc_parse_file (const char *pathname, const flag securecheck)
447 /* digest the configuration into a linked list of host records */
450 querylist = hosttail = (struct query *)NULL;
454 /* Check that the file is secure */
455 if ( (prc_errflag = prc_filecheck(pathname, securecheck)) != 0 )
459 * Croak if the configuration directory does not exist.
460 * This probably means an NFS mount failed and we can't
461 * see a configuration file that ought to be there.
462 * Question: is this a portable check? It's not clear
463 * that all implementations of lstat() will return ENOTDIR
464 * rather than plain ENOENT in this case...
466 if (errno == ENOTDIR)
468 else if (errno == ENOENT)
471 /* Open the configuration file and feed it to the lexer. */
472 if (strcmp(pathname, "-") == 0)
474 else if ((yyin = fopen(pathname,"r")) == (FILE *)NULL) {
475 report(stderr, "open: %s: %s\n", pathname, strerror(errno));
479 yyparse(); /* parse entire file */
481 fclose(yyin); /* not checking this should be safe, file mode was r */
489 static void reset_server(const char *name, int skip)
490 /* clear the entire global record and initialize it with a new name */
493 memset(¤t,'\0',sizeof(current));
494 current.smtp_socket = -1;
495 current.server.pollname = xstrdup(name);
496 current.server.skip = skip;
497 current.server.principal = (char *)NULL;
501 static void user_reset(void)
502 /* clear the global current record (user parameters) used by the parser */
504 struct hostdata save;
507 * Purpose of this code is to initialize the new server block, but
508 * preserve whatever server name was previously set. Also
509 * preserve server options unless the command-line explicitly
512 save = current.server;
514 memset(¤t, '\0', sizeof(current));
515 current.smtp_socket = -1;
517 current.server = save;
520 /** append a host record to the host list */
521 struct query *hostalloc(struct query *init /** pointer to block containing
526 /* allocate new node */
527 node = (struct query *) xmalloc(sizeof(struct query));
531 memcpy(node, init, sizeof(struct query));
534 memset(node, '\0', sizeof(struct query));
535 node->smtp_socket = -1;
538 /* append to end of list */
539 if (hosttail != (struct query *) 0)
540 hosttail->next = node; /* list contains at least one element */
542 querylist = node; /* list is empty */
546 node->server.lead_server = leadentry;
549 node->server.lead_server = NULL;
550 leadentry = &node->server;
556 static void record_current(void)
557 /* register current parameters and append to the host list */
559 (void) hostalloc(¤t);
563 char *prependdir (const char *file, const char *dir)
564 /* if a filename is relative to dir, convert it to an absolute path */
567 if (!file[0] || /* null path */
568 file[0] == '/' || /* absolute path */
569 strcmp(file, "-") == 0 || /* stdin/stdout */
570 !dir[0]) /* we don't HAVE_GETCWD */
571 return xstrdup (file);
572 newfile = (char *)xmalloc (strlen (dir) + 1 + strlen (file) + 1);
573 if (dir[strlen(dir) - 1] != '/')
574 sprintf (newfile, "%s/%s", dir, file);
576 sprintf (newfile, "%s%s", dir, file);
580 /* rcfile_y.y ends here */