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 PINENTRY_TIMEOUT PWMD_SOCKET PWMD_FILE
61 %token USERNAME PASSWORD FOLDER SMTPHOST FETCHDOMAINS MDA BSMTP LMTP
62 %token SMTPADDRESS SMTPNAME SPAMRESPONSE PRECONNECT POSTCONNECT LIMIT WARNINGS
63 %token INTERFACE MONITOR PLUGIN PLUGOUT
64 %token IS HERE THERE TO MAP
65 %token BATCHLIMIT FETCHLIMIT FETCHSIZELIMIT FASTUIDL EXPUNGE PROPERTIES
66 %token SET LOGFILE DAEMON SYSLOG IDFILE PIDFILE INVISIBLE POSTMASTER BOUNCEMAIL
67 %token SPAMBOUNCE SOFTBOUNCE SHOWDOTS
68 %token BADHEADER ACCEPT REJECT_
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;}
114 | SET PINENTRY_TIMEOUT optmap NUMBER {
116 run.pinentry_timeout = $4;
118 yyerror(GT_("pwmd not enabled"));
123 * The way the next two productions are written depends on the fact that
124 * userspecs cannot be empty. It's a kluge to deal with files that set
125 * up a load of defaults and then have poll statements following with no
126 * user options at all.
128 | define_server serverspecs {record_current();}
129 | define_server serverspecs userspecs
131 /* detect and complain about the most common user error */
132 | define_server serverspecs userspecs serv_option
133 {yyerror(GT_("server option after user options"));}
136 define_server : POLL STRING {reset_server($2, FALSE); free($2);}
137 | SKIP STRING {reset_server($2, TRUE); free($2);}
138 | DEFAULTS {reset_server("defaults", FALSE);}
141 serverspecs : /* EMPTY */
142 | serverspecs serv_option
145 alias_list : STRING {save_str(¤t.server.akalist,$1,0); free($1);}
146 | alias_list STRING {save_str(¤t.server.akalist,$2,0); free($2);}
149 domain_list : STRING {save_str(¤t.server.localdomains,$1,0); free($1);}
150 | domain_list STRING {save_str(¤t.server.localdomains,$2,0); free($2);}
153 serv_option : AKA alias_list
154 | VIA STRING {current.server.via = $2;}
155 | LOCALDOMAINS domain_list
156 | PROTOCOL PROTO {current.server.protocol = $2;}
158 current.server.protocol = P_POP3;
160 if (current.server.authenticate == A_PASSWORD)
161 current.server.authenticate = A_KERBEROS_V5;
162 current.server.service = KPOP_PORT;
164 yyerror(GT_("Kerberos not enabled."));
167 | PRINCIPAL STRING {current.server.principal = $2;}
168 | ESMTPNAME STRING {current.server.esmtp_name = $2;}
169 | ESMTPPASSWORD STRING {current.server.esmtp_password = $2;}
172 current.server.protocol = P_POP3;
173 current.server.sdps = TRUE;
175 yyerror(GT_("SDPS not enabled."));
176 #endif /* SDPS_ENABLE */
178 | UIDL {current.server.uidl = FLAG_TRUE;}
179 | NO UIDL {current.server.uidl = FLAG_FALSE;}
180 | CHECKALIAS {current.server.checkalias = FLAG_TRUE;}
181 | NO CHECKALIAS {current.server.checkalias = FLAG_FALSE;}
183 current.server.service = $2;
188 snprintf(buf, sizeof buf, "%d", port);
189 current.server.service = xstrdup(buf);
194 snprintf(buf, sizeof buf, "%d", port);
195 current.server.service = xstrdup(buf);
198 {current.server.interval = $2;}
199 | AUTHENTICATE AUTHTYPE
200 {current.server.authenticate = $2;}
202 {current.server.timeout = $2;}
203 | ENVELOPE NUMBER STRING
205 current.server.envelope = $3;
206 current.server.envskip = $2;
210 current.server.envelope = $2;
211 current.server.envskip = 0;
214 | QVIRTUAL STRING {current.server.qvirtual = $2;}
217 interface_parse($2, ¤t.server);
219 fprintf(stderr, GT_("fetchmail: interface option is only supported under Linux (without IPv6) and FreeBSD\n"));
225 current.server.monitor = $2;
227 fprintf(stderr, GT_("fetchmail: monitor option is only supported under Linux (without IPv6) and FreeBSD\n"));
231 | PLUGIN STRING { current.server.plugin = $2; }
232 | PLUGOUT STRING { current.server.plugout = $2; }
233 | DNS {current.server.dns = FLAG_TRUE;}
234 | NO DNS {current.server.dns = FLAG_FALSE;}
235 | NO ENVELOPE {current.server.envelope = STRING_DISABLED;}
236 | TRACEPOLLS {current.server.tracepolls = FLAG_TRUE;}
237 | NO TRACEPOLLS {current.server.tracepolls = FLAG_FALSE;}
238 | BADHEADER ACCEPT {current.server.badheader = BHACCEPT;}
239 | BADHEADER REJECT_ {current.server.badheader = BHREJECT;}
242 userspecs : user1opts {record_current(); user_reset();}
246 explicits : explicitdef {record_current(); user_reset();}
247 | explicits explicitdef {record_current(); user_reset();}
250 explicitdef : userdef user0opts
253 userdef : USERNAME STRING {current.remotename = $2;}
254 | USERNAME mapping_list HERE
255 | USERNAME STRING THERE {current.remotename = $2;}
258 user0opts : /* EMPTY */
259 | user0opts user_option
262 user1opts : user_option
263 | user1opts user_option
266 mapping_list : mapping
267 | mapping_list mapping
270 mapping : STRING {if (0 == strcmp($1, "*")) {
271 current.wildcard = TRUE;
273 save_str_pair(¤t.localnames, $1, NULL);
276 | STRING MAP STRING {save_str_pair(¤t.localnames, $1, $3); free($1); free($3);}
279 folder_list : STRING {save_str(¤t.mailboxes,$1,0); free($1);}
280 | folder_list STRING {save_str(¤t.mailboxes,$2,0); free($2);}
283 smtp_list : STRING {save_str(¤t.smtphunt, $1,TRUE); free($1);}
284 | smtp_list STRING {save_str(¤t.smtphunt, $2,TRUE); free($2);}
287 fetch_list : STRING {save_str(¤t.domainlist, $1,TRUE); free($1);}
288 | fetch_list STRING {save_str(¤t.domainlist, $2,TRUE); free($2);}
294 id = save_str(¤t.antispam,STRING_DUMMY,0);
295 id->val.status.num = $1;
300 id = save_str(¤t.antispam,STRING_DUMMY,0);
301 id->val.status.num = $2;
305 user_option : TO mapping_list HERE
307 | IS mapping_list HERE
310 | IS STRING THERE {current.remotename = $2;}
311 | PASSWORD STRING {current.password = $2;}
314 | FETCHDOMAINS fetch_list
315 | SMTPADDRESS STRING {current.smtpaddress = $2;}
316 | SMTPNAME STRING {current.smtpname = $2;}
317 | SPAMRESPONSE num_list
318 | MDA STRING {current.mda = $2;}
319 | BSMTP STRING {current.bsmtp = prependdir ($2, rcfiledir); free($2);}
320 | LMTP {current.listener = LMTP_MODE;}
321 | PRECONNECT STRING {current.preconnect = $2;}
322 | POSTCONNECT STRING {current.postconnect = $2;}
324 | KEEP {current.keep = FLAG_TRUE;}
325 | FLUSH {current.flush = FLAG_TRUE;}
326 | LIMITFLUSH {current.limitflush = FLAG_TRUE;}
327 | FETCHALL {current.fetchall = FLAG_TRUE;}
328 | REWRITE {current.rewrite = FLAG_TRUE;}
329 | FORCECR {current.forcecr = FLAG_TRUE;}
330 | STRIPCR {current.stripcr = FLAG_TRUE;}
331 | PASS8BITS {current.pass8bits = FLAG_TRUE;}
332 | DROPSTATUS {current.dropstatus = FLAG_TRUE;}
333 | DROPDELIVERED {current.dropdelivered = FLAG_TRUE;}
334 | MIMEDECODE {current.mimedecode = FLAG_TRUE;}
335 | IDLE {current.idle = FLAG_TRUE;}
339 current.use_ssl = FLAG_TRUE;
341 yyerror(GT_("SSL is not enabled"));
344 | SSLKEY STRING {current.sslkey = prependdir ($2, rcfiledir); free($2);}
345 | SSLCERT STRING {current.sslcert = prependdir ($2, rcfiledir); free($2);}
346 | SSLPROTO STRING {current.sslproto = $2;}
347 | SSLCERTCK {current.sslcertck = FLAG_TRUE;}
348 | SSLCERTFILE STRING {current.sslcertfile = prependdir($2, rcfiledir); free($2);}
349 | SSLCERTPATH STRING {current.sslcertpath = prependdir($2, rcfiledir); free($2);}
350 | SSLCOMMONNAME STRING {current.sslcommonname = $2;}
351 | SSLFINGERPRINT STRING {current.sslfingerprint = $2;}
353 | NO KEEP {current.keep = FLAG_FALSE;}
354 | NO FLUSH {current.flush = FLAG_FALSE;}
355 | NO LIMITFLUSH {current.limitflush = FLAG_FALSE;}
356 | NO FETCHALL {current.fetchall = FLAG_FALSE;}
357 | NO REWRITE {current.rewrite = FLAG_FALSE;}
358 | NO FORCECR {current.forcecr = FLAG_FALSE;}
359 | NO STRIPCR {current.stripcr = FLAG_FALSE;}
360 | NO PASS8BITS {current.pass8bits = FLAG_FALSE;}
361 | NO DROPSTATUS {current.dropstatus = FLAG_FALSE;}
362 | NO DROPDELIVERED {current.dropdelivered = FLAG_FALSE;}
363 | NO MIMEDECODE {current.mimedecode = FLAG_FALSE;}
364 | NO IDLE {current.idle = FLAG_FALSE;}
366 | NO SSL {current.use_ssl = FLAG_FALSE;}
368 | LIMIT NUMBER {current.limit = NUM_VALUE_IN($2);}
369 | WARNINGS NUMBER {current.warnings = NUM_VALUE_IN($2);}
370 | FETCHLIMIT NUMBER {current.fetchlimit = NUM_VALUE_IN($2);}
371 | FETCHSIZELIMIT NUMBER {current.fetchsizelimit = NUM_VALUE_IN($2);}
372 | FASTUIDL NUMBER {current.fastuidl = NUM_VALUE_IN($2);}
373 | BATCHLIMIT NUMBER {current.batchlimit = NUM_VALUE_IN($2);}
374 | EXPUNGE NUMBER {current.expunge = NUM_VALUE_IN($2);}
376 | PROPERTIES STRING {current.properties = $2;}
378 | PWMD_SOCKET STRING {
380 current.pwmd_socket = xstrdup($2);
382 yyerror(GT_("pwmd not enabled"));
388 current.pwmd_file = xstrdup($2);
390 yyerror(GT_("pwmd not enabled"));
396 /* lexer interface */
398 extern int prc_lineno;
402 static struct query *hosttail; /* where to add new elements */
404 void yyerror (const char *s)
405 /* report a syntax error */
407 report_at_line(stderr, 0, rcfile, prc_lineno, GT_("%s at %s"), s,
408 (yytext && yytext[0]) ? yytext : GT_("end of input"));
412 /** check that a configuration file is secure, returns PS_* status codes */
413 int prc_filecheck(const char *pathname,
414 const flag securecheck /** shortcuts permission, filetype and uid tests if false */)
420 /* special case useful for debugging purposes */
421 if (strcmp("/dev/null", pathname) == 0)
424 /* pass through the special name for stdin */
425 if (strcmp("-", pathname) == 0)
428 /* the run control file must have the same uid as the REAL uid of this
429 process, it must have permissions no greater than 600, and it must not
430 be a symbolic link. We check these conditions here. */
432 if (stat(pathname, &statbuf) < 0) {
436 report(stderr, "lstat: %s: %s\n", pathname, strerror(errno));
441 if (!securecheck) return PS_SUCCESS;
443 if (!S_ISREG(statbuf.st_mode))
445 fprintf(stderr, GT_("File %s must be a regular file.\n"), pathname);
450 if (cygwin_internal(CW_CHECK_NTSEC, pathname))
451 #endif /* __CYGWIN__ */
452 if (statbuf.st_mode & (S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | S_IXOTH))
454 fprintf(stderr, GT_("File %s must have no more than -rwx------ (0700) permissions.\n"),
459 if (statbuf.st_uid != geteuid())
461 fprintf(stderr, GT_("File %s must be owned by you.\n"), pathname);
467 int prc_parse_file (const char *pathname, const flag securecheck)
468 /* digest the configuration into a linked list of host records */
471 querylist = hosttail = (struct query *)NULL;
475 /* Check that the file is secure */
476 if ( (prc_errflag = prc_filecheck(pathname, securecheck)) != 0 )
480 * Croak if the configuration directory does not exist.
481 * This probably means an NFS mount failed and we can't
482 * see a configuration file that ought to be there.
483 * Question: is this a portable check? It's not clear
484 * that all implementations of lstat() will return ENOTDIR
485 * rather than plain ENOENT in this case...
487 if (errno == ENOTDIR)
489 else if (errno == ENOENT)
492 /* Open the configuration file and feed it to the lexer. */
493 if (strcmp(pathname, "-") == 0)
495 else if ((yyin = fopen(pathname,"r")) == (FILE *)NULL) {
496 report(stderr, "open: %s: %s\n", pathname, strerror(errno));
500 yyparse(); /* parse entire file */
502 fclose(yyin); /* not checking this should be safe, file mode was r */
510 static void reset_server(const char *name, int skip)
511 /* clear the entire global record and initialize it with a new name */
514 memset(¤t,'\0',sizeof(current));
515 current.smtp_socket = -1;
516 current.server.pollname = xstrdup(name);
517 current.server.skip = skip;
518 current.server.principal = (char *)NULL;
522 static void user_reset(void)
523 /* clear the global current record (user parameters) used by the parser */
525 struct hostdata save;
528 * Purpose of this code is to initialize the new server block, but
529 * preserve whatever server name was previously set. Also
530 * preserve server options unless the command-line explicitly
533 save = current.server;
535 memset(¤t, '\0', sizeof(current));
536 current.smtp_socket = -1;
538 current.server = save;
541 /** append a host record to the host list */
542 struct query *hostalloc(struct query *init /** pointer to block containing
547 /* allocate new node */
548 node = (struct query *) xmalloc(sizeof(struct query));
552 memcpy(node, init, sizeof(struct query));
555 memset(node, '\0', sizeof(struct query));
556 node->smtp_socket = -1;
559 /* append to end of list */
560 if (hosttail != (struct query *) 0)
561 hosttail->next = node; /* list contains at least one element */
563 querylist = node; /* list is empty */
567 node->server.lead_server = leadentry;
570 node->server.lead_server = NULL;
571 leadentry = &node->server;
577 static void record_current(void)
578 /* register current parameters and append to the host list */
580 (void) hostalloc(¤t);
584 char *prependdir (const char *file, const char *dir)
585 /* if a filename is relative to dir, convert it to an absolute path */
588 if (!file[0] || /* null path */
589 file[0] == '/' || /* absolute path */
590 strcmp(file, "-") == 0 || /* stdin/stdout */
591 !dir[0]) /* we don't HAVE_GETCWD */
592 return xstrdup (file);
593 newfile = (char *)xmalloc (strlen (dir) + 1 + strlen (file) + 1);
594 if (dir[strlen(dir) - 1] != '/')
595 sprintf (newfile, "%s/%s", dir, file);
597 sprintf (newfile, "%s%s", dir, file);
601 /* rcfile_y.y ends here */