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>
19 #if defined(__CYGWIN__)
20 #include <sys/cygwin.h>
21 #endif /* __CYGWIN__ */
23 #include "fetchmail.h"
26 /* parser reads these */
27 char *rcfile; /* path name of rc file */
28 struct query cmd_opts; /* where to put command-line info */
30 /* parser sets these */
31 struct query *querylist; /* head of server list (globally visible) */
33 int yydebug; /* in case we didn't generate with -- debug */
35 static struct query current; /* current server record */
36 static int prc_errflag;
37 static struct hostdata *leadentry;
40 static void record_current(void);
41 static void user_reset(void);
42 static void reset_server(const char *name, int skip);
44 /* these should be of size PATH_MAX */
45 char currentwd[1024] = "", rcfiledir[1024] = "";
47 /* using Bison, this arranges that yydebug messages will show actual tokens */
49 #define YYPRINT(fp, type, val) fprintf(fp, " = \"%s\"", yytext)
58 %token DEFAULTS POLL SKIP VIA AKA LOCALDOMAINS PROTOCOL
59 %token AUTHENTICATE TIMEOUT KPOP SDPS ENVELOPE QVIRTUAL
60 %token USERNAME PASSWORD FOLDER SMTPHOST FETCHDOMAINS MDA BSMTP LMTP
61 %token SMTPADDRESS SMTPNAME SPAMRESPONSE PRECONNECT POSTCONNECT LIMIT WARNINGS
62 %token INTERFACE MONITOR PLUGIN PLUGOUT
63 %token IS HERE THERE TO MAP
64 %token BATCHLIMIT FETCHLIMIT FETCHSIZELIMIT FASTUIDL EXPUNGE PROPERTIES
65 %token SET LOGFILE DAEMON SYSLOG IDFILE PIDFILE INVISIBLE POSTMASTER BOUNCEMAIL
66 %token SPAMBOUNCE SOFTBOUNCE SHOWDOTS
67 %token BADHEADER ACCEPT REJECT_
68 %token RETRIEVEERROR ABORT CONTINUE MARKSEEN
69 %token <proto> PROTO AUTHTYPE
71 %token <number> NUMBER
72 %token NO KEEP FLUSH LIMITFLUSH FETCHALL REWRITE FORCECR STRIPCR PASS8BITS
73 %token DROPSTATUS DROPDELIVERED
74 %token DNS SERVICE PORT UIDL INTERVAL MIMEDECODE IDLE CHECKALIAS
75 %token SSL SSLKEY SSLCERT SSLPROTO SSLCERTCK SSLCERTFILE SSLCERTPATH SSLCOMMONNAME SSLFINGERPRINT
76 %token PRINCIPAL ESMTPNAME ESMTPPASSWORD
81 %destructor { free ($$); } STRING
89 statement_list : statement
90 | statement_list statement
93 optmap : MAP | /* EMPTY */;
95 /* future global options should also have the form SET <name> optmap <value> */
96 statement : SET LOGFILE optmap STRING {run.logfile = prependdir ($4, rcfiledir); free($4);}
97 | SET IDFILE optmap STRING {run.idfile = prependdir ($4, rcfiledir); free($4);}
98 | SET PIDFILE optmap STRING {run.pidfile = prependdir ($4, rcfiledir); free($4);}
99 | SET DAEMON optmap NUMBER {run.poll_interval = $4;}
100 | SET POSTMASTER optmap STRING {run.postmaster = $4;}
101 | SET BOUNCEMAIL {run.bouncemail = TRUE;}
102 | SET NO BOUNCEMAIL {run.bouncemail = FALSE;}
103 | SET SPAMBOUNCE {run.spambounce = TRUE;}
104 | SET NO SPAMBOUNCE {run.spambounce = FALSE;}
105 | SET SOFTBOUNCE {run.softbounce = TRUE;}
106 | SET NO SOFTBOUNCE {run.softbounce = FALSE;}
107 | SET PROPERTIES optmap STRING {run.properties = $4;}
108 | SET SYSLOG {run.use_syslog = TRUE;}
109 | SET NO SYSLOG {run.use_syslog = FALSE;}
110 | SET INVISIBLE {run.invisible = TRUE;}
111 | SET NO INVISIBLE {run.invisible = FALSE;}
112 | SET SHOWDOTS {run.showdots = FLAG_TRUE;}
113 | SET NO SHOWDOTS {run.showdots = FLAG_FALSE;}
116 * The way the next two productions are written depends on the fact that
117 * userspecs cannot be empty. It's a kluge to deal with files that set
118 * up a load of defaults and then have poll statements following with no
119 * user options at all.
121 | define_server serverspecs {record_current();}
122 | define_server serverspecs userspecs
124 /* detect and complain about the most common user error */
125 | define_server serverspecs userspecs serv_option
126 {yyerror(GT_("server option after user options"));}
129 define_server : POLL STRING {reset_server($2, FALSE); free($2);}
130 | SKIP STRING {reset_server($2, TRUE); free($2);}
131 | DEFAULTS {reset_server("defaults", FALSE);}
134 serverspecs : /* EMPTY */
135 | serverspecs serv_option
138 alias_list : STRING {save_str(¤t.server.akalist,$1,0); free($1);}
139 | alias_list STRING {save_str(¤t.server.akalist,$2,0); free($2);}
142 domain_list : STRING {save_str(¤t.server.localdomains,$1,0); free($1);}
143 | domain_list STRING {save_str(¤t.server.localdomains,$2,0); free($2);}
146 serv_option : AKA alias_list
147 | VIA STRING {current.server.via = $2;}
148 | LOCALDOMAINS domain_list
149 | PROTOCOL PROTO {current.server.protocol = $2;}
151 current.server.protocol = P_POP3;
153 if (current.server.authenticate == A_PASSWORD)
154 current.server.authenticate = A_KERBEROS_V5;
155 current.server.service = KPOP_PORT;
157 yyerror(GT_("Kerberos not enabled."));
160 | PRINCIPAL STRING {current.server.principal = $2;}
161 | ESMTPNAME STRING {current.server.esmtp_name = $2;}
162 | ESMTPPASSWORD STRING {current.server.esmtp_password = $2;}
165 current.server.protocol = P_POP3;
166 current.server.sdps = TRUE;
168 yyerror(GT_("SDPS not enabled."));
169 #endif /* SDPS_ENABLE */
171 | UIDL {current.server.uidl = FLAG_TRUE;}
172 | NO UIDL {current.server.uidl = FLAG_FALSE;}
173 | CHECKALIAS {current.server.checkalias = FLAG_TRUE;}
174 | NO CHECKALIAS {current.server.checkalias = FLAG_FALSE;}
176 current.server.service = $2;
181 snprintf(buf, sizeof buf, "%d", port);
182 current.server.service = xstrdup(buf);
187 snprintf(buf, sizeof buf, "%d", port);
188 current.server.service = xstrdup(buf);
191 {current.server.interval = $2;}
192 | AUTHENTICATE AUTHTYPE
193 {current.server.authenticate = $2;}
195 {current.server.timeout = $2;}
196 | ENVELOPE NUMBER STRING
198 current.server.envelope = $3;
199 current.server.envskip = $2;
203 current.server.envelope = $2;
204 current.server.envskip = 0;
207 | QVIRTUAL STRING {current.server.qvirtual = $2;}
210 interface_parse($2, ¤t.server);
212 fprintf(stderr, GT_("fetchmail: interface option is only supported under Linux (without IPv6) and FreeBSD\n"));
218 current.server.monitor = $2;
220 fprintf(stderr, GT_("fetchmail: monitor option is only supported under Linux (without IPv6) and FreeBSD\n"));
224 | PLUGIN STRING { current.server.plugin = $2; }
225 | PLUGOUT STRING { current.server.plugout = $2; }
226 | DNS {current.server.dns = FLAG_TRUE;}
227 | NO DNS {current.server.dns = FLAG_FALSE;}
228 | NO ENVELOPE {current.server.envelope = STRING_DISABLED;}
229 | TRACEPOLLS {current.server.tracepolls = FLAG_TRUE;}
230 | NO TRACEPOLLS {current.server.tracepolls = FLAG_FALSE;}
231 | BADHEADER ACCEPT {current.server.badheader = BHACCEPT;}
232 | BADHEADER REJECT_ {current.server.badheader = BHREJECT;}
233 | RETRIEVEERROR ABORT {current.server.retrieveerror = RE_ABORT;}
234 | RETRIEVEERROR CONTINUE {current.server.retrieveerror = RE_CONTINUE;}
235 | RETRIEVEERROR MARKSEEN {current.server.retrieveerror = RE_MARKSEEN;}
238 userspecs : user1opts {record_current(); user_reset();}
242 explicits : explicitdef {record_current(); user_reset();}
243 | explicits explicitdef {record_current(); user_reset();}
246 explicitdef : userdef user0opts
249 userdef : USERNAME STRING {current.remotename = $2;}
250 | USERNAME mapping_list HERE
251 | USERNAME STRING THERE {current.remotename = $2;}
254 user0opts : /* EMPTY */
255 | user0opts user_option
258 user1opts : user_option
259 | user1opts user_option
262 mapping_list : mapping
263 | mapping_list mapping
266 mapping : STRING {if (0 == strcmp($1, "*")) {
267 current.wildcard = TRUE;
269 save_str_pair(¤t.localnames, $1, NULL);
272 | STRING MAP STRING {save_str_pair(¤t.localnames, $1, $3); free($1); free($3);}
275 folder_list : STRING {save_str(¤t.mailboxes,$1,0); free($1);}
276 | folder_list STRING {save_str(¤t.mailboxes,$2,0); free($2);}
279 smtp_list : STRING {save_str(¤t.smtphunt, $1,TRUE); free($1);}
280 | smtp_list STRING {save_str(¤t.smtphunt, $2,TRUE); free($2);}
283 fetch_list : STRING {save_str(¤t.domainlist, $1,TRUE); free($1);}
284 | fetch_list STRING {save_str(¤t.domainlist, $2,TRUE); free($2);}
290 id = save_str(¤t.antispam,STRING_DUMMY,0);
291 id->val.status.num = $1;
296 id = save_str(¤t.antispam,STRING_DUMMY,0);
297 id->val.status.num = $2;
301 user_option : TO mapping_list HERE
303 | IS mapping_list HERE
306 | IS STRING THERE {current.remotename = $2;}
307 | PASSWORD STRING {current.password = $2;}
310 | FETCHDOMAINS fetch_list
311 | SMTPADDRESS STRING {current.smtpaddress = $2;}
312 | SMTPNAME STRING {current.smtpname = $2;}
313 | SPAMRESPONSE num_list
314 | MDA STRING {current.mda = $2;}
315 | BSMTP STRING {current.bsmtp = prependdir ($2, rcfiledir); free($2);}
316 | LMTP {current.listener = LMTP_MODE;}
317 | PRECONNECT STRING {current.preconnect = $2;}
318 | POSTCONNECT STRING {current.postconnect = $2;}
320 | KEEP {current.keep = FLAG_TRUE;}
321 | FLUSH {current.flush = FLAG_TRUE;}
322 | LIMITFLUSH {current.limitflush = FLAG_TRUE;}
323 | FETCHALL {current.fetchall = FLAG_TRUE;}
324 | REWRITE {current.rewrite = FLAG_TRUE;}
325 | FORCECR {current.forcecr = FLAG_TRUE;}
326 | STRIPCR {current.stripcr = FLAG_TRUE;}
327 | PASS8BITS {current.pass8bits = FLAG_TRUE;}
328 | DROPSTATUS {current.dropstatus = FLAG_TRUE;}
329 | DROPDELIVERED {current.dropdelivered = FLAG_TRUE;}
330 | MIMEDECODE {current.mimedecode = FLAG_TRUE;}
331 | IDLE {current.idle = FLAG_TRUE;}
335 current.use_ssl = FLAG_TRUE;
337 yyerror(GT_("SSL is not enabled"));
340 | SSLKEY STRING {current.sslkey = prependdir ($2, rcfiledir); free($2);}
341 | SSLCERT STRING {current.sslcert = prependdir ($2, rcfiledir); free($2);}
342 | SSLPROTO STRING {current.sslproto = $2;}
343 | SSLCERTCK {current.sslcertck = FLAG_TRUE;}
344 | SSLCERTFILE STRING {current.sslcertfile = prependdir($2, rcfiledir); free($2);}
345 | SSLCERTPATH STRING {current.sslcertpath = prependdir($2, rcfiledir); free($2);}
346 | SSLCOMMONNAME STRING {current.sslcommonname = $2;}
347 | SSLFINGERPRINT STRING {current.sslfingerprint = $2;}
349 | NO KEEP {current.keep = FLAG_FALSE;}
350 | NO FLUSH {current.flush = FLAG_FALSE;}
351 | NO LIMITFLUSH {current.limitflush = FLAG_FALSE;}
352 | NO FETCHALL {current.fetchall = FLAG_FALSE;}
353 | NO REWRITE {current.rewrite = FLAG_FALSE;}
354 | NO FORCECR {current.forcecr = FLAG_FALSE;}
355 | NO STRIPCR {current.stripcr = FLAG_FALSE;}
356 | NO PASS8BITS {current.pass8bits = FLAG_FALSE;}
357 | NO DROPSTATUS {current.dropstatus = FLAG_FALSE;}
358 | NO DROPDELIVERED {current.dropdelivered = FLAG_FALSE;}
359 | NO MIMEDECODE {current.mimedecode = FLAG_FALSE;}
360 | NO IDLE {current.idle = FLAG_FALSE;}
362 | NO SSL {current.use_ssl = FLAG_FALSE;}
364 | LIMIT NUMBER {current.limit = NUM_VALUE_IN($2);}
365 | WARNINGS NUMBER {current.warnings = NUM_VALUE_IN($2);}
366 | FETCHLIMIT NUMBER {current.fetchlimit = NUM_VALUE_IN($2);}
367 | FETCHSIZELIMIT NUMBER {current.fetchsizelimit = NUM_VALUE_IN($2);}
368 | FASTUIDL NUMBER {current.fastuidl = NUM_VALUE_IN($2);}
369 | BATCHLIMIT NUMBER {current.batchlimit = NUM_VALUE_IN($2);}
370 | EXPUNGE NUMBER {current.expunge = NUM_VALUE_IN($2);}
372 | PROPERTIES STRING {current.properties = $2;}
376 /* lexer interface */
378 extern int prc_lineno;
382 static struct query *hosttail; /* where to add new elements */
384 void yyerror (const char *s)
385 /* report a syntax error */
387 report_at_line(stderr, 0, rcfile, prc_lineno, GT_("%s at %s"), s,
388 (yytext && yytext[0]) ? yytext : GT_("end of input"));
392 /** check that a configuration file is secure, returns PS_* status codes */
393 int prc_filecheck(const char *pathname,
394 const flag securecheck /** shortcuts permission, filetype and uid tests if false */)
400 /* special case useful for debugging purposes */
401 if (strcmp("/dev/null", pathname) == 0)
404 /* pass through the special name for stdin */
405 if (strcmp("-", pathname) == 0)
408 /* the run control file must have the same uid as the REAL uid of this
409 process, it must have permissions no greater than 600, and it must not
410 be a symbolic link. We check these conditions here. */
412 if (stat(pathname, &statbuf) < 0) {
416 report(stderr, "lstat: %s: %s\n", pathname, strerror(errno));
421 if (!securecheck) return PS_SUCCESS;
423 if (!S_ISREG(statbuf.st_mode))
425 fprintf(stderr, GT_("File %s must be a regular file.\n"), pathname);
430 if (cygwin_internal(CW_CHECK_NTSEC, pathname))
431 #endif /* __CYGWIN__ */
432 if (statbuf.st_mode & (S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | S_IXOTH))
434 fprintf(stderr, GT_("File %s must have no more than -rwx------ (0700) permissions.\n"),
439 if (statbuf.st_uid != geteuid())
441 fprintf(stderr, GT_("File %s must be owned by you.\n"), pathname);
447 int prc_parse_file (const char *pathname, const flag securecheck)
448 /* digest the configuration into a linked list of host records */
451 querylist = hosttail = (struct query *)NULL;
455 /* Check that the file is secure */
456 if ( (prc_errflag = prc_filecheck(pathname, securecheck)) != 0 )
460 * Croak if the configuration directory does not exist.
461 * This probably means an NFS mount failed and we can't
462 * see a configuration file that ought to be there.
463 * Question: is this a portable check? It's not clear
464 * that all implementations of lstat() will return ENOTDIR
465 * rather than plain ENOENT in this case...
467 if (errno == ENOTDIR)
469 else if (errno == ENOENT)
472 /* Open the configuration file and feed it to the lexer. */
473 if (strcmp(pathname, "-") == 0)
475 else if ((yyin = fopen(pathname,"r")) == (FILE *)NULL) {
476 report(stderr, "open: %s: %s\n", pathname, strerror(errno));
480 yyparse(); /* parse entire file */
482 fclose(yyin); /* not checking this should be safe, file mode was r */
490 static void reset_server(const char *name, int skip)
491 /* clear the entire global record and initialize it with a new name */
494 memset(¤t,'\0',sizeof(current));
495 current.smtp_socket = -1;
496 current.server.pollname = xstrdup(name);
497 current.server.skip = skip;
498 current.server.principal = (char *)NULL;
502 static void user_reset(void)
503 /* clear the global current record (user parameters) used by the parser */
505 struct hostdata save;
508 * Purpose of this code is to initialize the new server block, but
509 * preserve whatever server name was previously set. Also
510 * preserve server options unless the command-line explicitly
513 save = current.server;
515 memset(¤t, '\0', sizeof(current));
516 current.smtp_socket = -1;
518 current.server = save;
521 /** append a host record to the host list */
522 struct query *hostalloc(struct query *init /** pointer to block containing
527 /* allocate new node */
528 node = (struct query *) xmalloc(sizeof(struct query));
532 memcpy(node, init, sizeof(struct query));
535 memset(node, '\0', sizeof(struct query));
536 node->smtp_socket = -1;
539 /* append to end of list */
540 if (hosttail != (struct query *) 0)
541 hosttail->next = node; /* list contains at least one element */
543 querylist = node; /* list is empty */
547 node->server.lead_server = leadentry;
550 node->server.lead_server = NULL;
551 leadentry = &node->server;
557 static void record_current(void)
558 /* register current parameters and append to the host list */
560 (void) hostalloc(¤t);
564 char *prependdir (const char *file, const char *dir)
565 /* if a filename is relative to dir, convert it to an absolute path */
568 if (!file[0] || /* null path */
569 file[0] == '/' || /* absolute path */
570 strcmp(file, "-") == 0 || /* stdin/stdout */
571 !dir[0]) /* we don't HAVE_GETCWD */
572 return xstrdup (file);
573 newfile = (char *)xmalloc (strlen (dir) + 1 + strlen (file) + 1);
574 if (dir[strlen(dir) - 1] != '/')
575 sprintf (newfile, "%s/%s", dir, file);
577 sprintf (newfile, "%s%s", dir, file);
581 /* rcfile_y.y ends here */