]> Pileus Git - ~andy/fetchmail/blob - rcfile_y.y
Drop Trio, and drop checks for various POSIX functions...
[~andy/fetchmail] / rcfile_y.y
1 %{
2 /*
3  * rcfile_y.y -- Run control file parser for fetchmail
4  *
5  * For license terms, see the file COPYING in this directory.
6  */
7
8 #include "config.h"
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/file.h>
12 #if defined(HAVE_SYS_WAIT_H)
13 #include <sys/wait.h>
14 #endif
15 #include <sys/stat.h>
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <string.h>
20
21 #if defined(__CYGWIN__)
22 #include <sys/cygwin.h>
23 #endif /* __CYGWIN__ */
24
25 #include "fetchmail.h"
26 #include "i18n.h"
27   
28 /* parser reads these */
29 char *rcfile;                   /* path name of rc file */
30 struct query cmd_opts;          /* where to put command-line info */
31
32 /* parser sets these */
33 struct query *querylist;        /* head of server list (globally visible) */
34
35 int yydebug;                    /* in case we didn't generate with -- debug */
36
37 static struct query current;    /* current server record */
38 static int prc_errflag;
39 static struct hostdata *leadentry;
40 static flag trailer;
41
42 static void record_current(void);
43 static void user_reset(void);
44 static void reset_server(const char *name, int skip);
45
46 /* these should be of size PATH_MAX */
47 char currentwd[1024] = "", rcfiledir[1024] = "";
48
49 /* using Bison, this arranges that yydebug messages will show actual tokens */
50 extern char * yytext;
51 #define YYPRINT(fp, type, val)  fprintf(fp, " = \"%s\"", yytext)
52 %}
53
54 %union {
55   int proto;
56   int number;
57   char *sval;
58 }
59
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
71 %token <sval>  STRING
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
78 %token TRACEPOLLS
79
80 %expect 2
81
82 %destructor { free ($$); } STRING
83
84 %%
85
86 rcfile          : /* empty */
87                 | statement_list
88                 ;
89
90 statement_list  : statement
91                 | statement_list statement
92                 ;
93
94 optmap          : MAP | /* EMPTY */;
95
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;}
115
116 /* 
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. 
121  */
122                 | define_server serverspecs             {record_current();}
123                 | define_server serverspecs userspecs
124
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"));}
128                 ;
129
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);}
133                 ;
134
135 serverspecs     : /* EMPTY */
136                 | serverspecs serv_option
137                 ;
138
139 alias_list      : STRING                {save_str(&current.server.akalist,$1,0); free($1);}
140                 | alias_list STRING     {save_str(&current.server.akalist,$2,0); free($2);}
141                 ;
142
143 domain_list     : STRING                {save_str(&current.server.localdomains,$1,0); free($1);}
144                 | domain_list STRING    {save_str(&current.server.localdomains,$2,0); free($2);}
145                 ;
146
147 serv_option     : AKA alias_list
148                 | VIA STRING            {current.server.via = $2;}
149                 | LOCALDOMAINS domain_list
150                 | PROTOCOL PROTO        {current.server.protocol = $2;}
151                 | PROTOCOL KPOP         {
152                                             current.server.protocol = P_POP3;
153
154                                             if (current.server.authenticate == A_PASSWORD)
155 #ifdef KERBEROS_V5
156                                                 current.server.authenticate = A_KERBEROS_V5;
157 #else
158                                                 current.server.authenticate = A_KERBEROS_V4;
159 #endif /* KERBEROS_V5 */
160                                             current.server.service = KPOP_PORT;
161                                         }
162                 | PRINCIPAL STRING      {current.server.principal = $2;}
163                 | ESMTPNAME STRING      {current.server.esmtp_name = $2;}
164                 | ESMTPPASSWORD STRING  {current.server.esmtp_password = $2;}
165                 | PROTOCOL SDPS         {
166 #ifdef SDPS_ENABLE
167                                             current.server.protocol = P_POP3;
168                                             current.server.sdps = TRUE;
169 #else
170                                             yyerror(GT_("SDPS not enabled."));
171 #endif /* SDPS_ENABLE */
172                                         }
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;}
177                 | SERVICE STRING        {
178                                         current.server.service = $2;
179                                         }
180                 | SERVICE NUMBER        {
181                                         int port = $2;
182                                         char buf[10];
183                                         snprintf(buf, sizeof buf, "%d", port);
184                                         current.server.service = xstrdup(buf);
185                 }
186                 | PORT NUMBER           {
187                                         int port = $2;
188                                         char buf[10];
189                                         snprintf(buf, sizeof buf, "%d", port);
190                                         current.server.service = xstrdup(buf);
191                 }
192                 | INTERVAL NUMBER
193                         {current.server.interval = $2;}
194                 | AUTHENTICATE AUTHTYPE
195                         {current.server.authenticate = $2;}
196                 | TIMEOUT NUMBER
197                         {current.server.timeout = $2;}
198                 | ENVELOPE NUMBER STRING
199                                         {
200                                             current.server.envelope = $3;
201                                             current.server.envskip = $2;
202                                         }
203                 | ENVELOPE STRING
204                                         {
205                                             current.server.envelope = $2;
206                                             current.server.envskip = 0;
207                                         }
208
209                 | QVIRTUAL STRING       {current.server.qvirtual = $2;}
210                 | INTERFACE STRING      {
211 #ifdef CAN_MONITOR
212                                         interface_parse($2, &current.server);
213 #else
214                                         fprintf(stderr, GT_("fetchmail: interface option is only supported under Linux (without IPv6) and FreeBSD\n"));
215 #endif
216                                         free($2);
217                                         }
218                 | MONITOR STRING        {
219 #ifdef CAN_MONITOR
220                                         current.server.monitor = $2;
221 #else
222                                         fprintf(stderr, GT_("fetchmail: monitor option is only supported under Linux (without IPv6) and FreeBSD\n"));
223                                         free($2);
224 #endif
225                                         }
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;}
235                 ;
236
237 userspecs       : user1opts             {record_current(); user_reset();}
238                 | explicits
239                 ;
240
241 explicits       : explicitdef           {record_current(); user_reset();}
242                 | explicits explicitdef {record_current(); user_reset();}
243                 ;
244
245 explicitdef     : userdef user0opts
246                 ;
247
248 userdef         : USERNAME STRING       {current.remotename = $2;}
249                 | USERNAME mapping_list HERE
250                 | USERNAME STRING THERE {current.remotename = $2;}
251                 ;
252
253 user0opts       : /* EMPTY */
254                 | user0opts user_option
255                 ;
256
257 user1opts       : user_option
258                 | user1opts user_option
259                 ;
260
261 mapping_list    : mapping               
262                 | mapping_list mapping
263                 ;
264
265 mapping         : STRING                {if (0 == strcmp($1, "*")) {
266                                               current.wildcard = TRUE;
267                                           } else {
268                                             save_str_pair(&current.localnames, $1, NULL);
269                                           }
270                                          free($1);}
271                 | STRING MAP STRING     {save_str_pair(&current.localnames, $1, $3); free($1); free($3);}
272                 ;
273
274 folder_list     : STRING                {save_str(&current.mailboxes,$1,0); free($1);}
275                 | folder_list STRING    {save_str(&current.mailboxes,$2,0); free($2);}
276                 ;
277
278 smtp_list       : STRING                {save_str(&current.smtphunt, $1,TRUE); free($1);}
279                 | smtp_list STRING      {save_str(&current.smtphunt, $2,TRUE); free($2);}
280                 ;
281
282 fetch_list      : STRING                {save_str(&current.domainlist, $1,TRUE); free($1);}
283                 | fetch_list STRING     {save_str(&current.domainlist, $2,TRUE); free($2);}
284                 ;
285
286 num_list        : NUMBER
287                         {
288                             struct idlist *id;
289                             id = save_str(&current.antispam,STRING_DUMMY,0);
290                             id->val.status.num = $1;
291                         }
292                 | num_list NUMBER
293                         {
294                             struct idlist *id;
295                             id = save_str(&current.antispam,STRING_DUMMY,0);
296                             id->val.status.num = $2;
297                         }
298                 ;
299
300 user_option     : TO mapping_list HERE
301                 | TO mapping_list
302                 | IS mapping_list HERE
303                 | IS mapping_list
304
305                 | IS STRING THERE       {current.remotename  = $2;}
306                 | PASSWORD STRING       {current.password    = $2;}
307                 | FOLDER folder_list
308                 | SMTPHOST smtp_list
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;}
318
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;}
331
332                 | SSL                   {
333 #ifdef SSL_ENABLE
334                     current.use_ssl = FLAG_TRUE;
335 #else
336                     yyerror(GT_("SSL is not enabled"));
337 #endif 
338                 }
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;}
347
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;}
360
361                 | NO SSL                {current.use_ssl     = FLAG_FALSE;}
362
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);}
370
371                 | PROPERTIES STRING     {current.properties  = $2;}
372                 ;
373 %%
374
375 /* lexer interface */
376 extern char *rcfile;
377 extern int prc_lineno;
378 extern char *yytext;
379 extern FILE *yyin;
380
381 static struct query *hosttail;  /* where to add new elements */
382
383 void yyerror (const char *s)
384 /* report a syntax error */
385 {
386     report_at_line(stderr, 0, rcfile, prc_lineno, GT_("%s at %s"), s, 
387                    (yytext && yytext[0]) ? yytext : GT_("end of input"));
388     prc_errflag++;
389 }
390
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 */)
394 {
395     struct stat statbuf;
396
397     errno = 0;
398
399     /* special case useful for debugging purposes */
400     if (strcmp("/dev/null", pathname) == 0)
401         return(PS_SUCCESS);
402
403     /* pass through the special name for stdin */
404     if (strcmp("-", pathname) == 0)
405         return(PS_SUCCESS);
406
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. */
410
411     if (stat(pathname, &statbuf) < 0) {
412         if (errno == ENOENT) 
413             return(PS_SUCCESS);
414         else {
415             report(stderr, "lstat: %s: %s\n", pathname, strerror(errno));
416             return(PS_IOERR);
417         }
418     }
419
420     if (!securecheck)   return PS_SUCCESS;
421
422     if (!S_ISREG(statbuf.st_mode))
423     {
424         fprintf(stderr, GT_("File %s must be a regular file.\n"), pathname);
425         return(PS_IOERR);
426     }
427
428 #ifdef __CYGWIN__
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))
432     {
433         fprintf(stderr, GT_("File %s must have no more than -rwx------ (0700) permissions.\n"), 
434                 pathname);
435         return(PS_IOERR);
436     }
437
438     if (statbuf.st_uid != geteuid())
439     {
440         fprintf(stderr, GT_("File %s must be owned by you.\n"), pathname);
441         return(PS_IOERR);
442     }
443     return(PS_SUCCESS);
444 }
445
446 int prc_parse_file (const char *pathname, const flag securecheck)
447 /* digest the configuration into a linked list of host records */
448 {
449     prc_errflag = 0;
450     querylist = hosttail = (struct query *)NULL;
451
452     errno = 0;
453
454     /* Check that the file is secure */
455     if ( (prc_errflag = prc_filecheck(pathname, securecheck)) != 0 )
456         return(prc_errflag);
457
458     /*
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...
465      */
466     if (errno == ENOTDIR)
467         return(PS_IOERR);
468     else if (errno == ENOENT)
469         return(PS_SUCCESS);
470
471     /* Open the configuration file and feed it to the lexer. */
472     if (strcmp(pathname, "-") == 0)
473         yyin = stdin;
474     else if ((yyin = fopen(pathname,"r")) == (FILE *)NULL) {
475         report(stderr, "open: %s: %s\n", pathname, strerror(errno));
476         return(PS_IOERR);
477     }
478
479     yyparse();          /* parse entire file */
480
481     fclose(yyin);       /* not checking this should be safe, file mode was r */
482
483     if (prc_errflag) 
484         return(PS_SYNTAX);
485     else
486         return(PS_SUCCESS);
487 }
488
489 static void reset_server(const char *name, int skip)
490 /* clear the entire global record and initialize it with a new name */
491 {
492     trailer = FALSE;
493     memset(&current,'\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;
498 }
499
500
501 static void user_reset(void)
502 /* clear the global current record (user parameters) used by the parser */
503 {
504     struct hostdata save;
505
506     /*
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
510      * overrides them.
511      */
512     save = current.server;
513
514     memset(&current, '\0', sizeof(current));
515     current.smtp_socket = -1;
516
517     current.server = save;
518 }
519
520 /** append a host record to the host list */
521 struct query *hostalloc(struct query *init /** pointer to block containing
522                                                initial values */)
523 {
524     struct query *node;
525
526     /* allocate new node */
527     node = (struct query *) xmalloc(sizeof(struct query));
528
529     /* initialize it */
530     if (init)
531         memcpy(node, init, sizeof(struct query));
532     else
533     {
534         memset(node, '\0', sizeof(struct query));
535         node->smtp_socket = -1;
536     }
537
538     /* append to end of list */
539     if (hosttail != (struct query *) 0)
540         hosttail->next = node;  /* list contains at least one element */
541     else
542         querylist = node;       /* list is empty */
543     hosttail = node;
544
545     if (trailer)
546         node->server.lead_server = leadentry;
547     else
548     {
549         node->server.lead_server = NULL;
550         leadentry = &node->server;
551     }
552
553     return(node);
554 }
555
556 static void record_current(void)
557 /* register current parameters and append to the host list */
558 {
559     (void) hostalloc(&current);
560     trailer = TRUE;
561 }
562
563 char *prependdir (const char *file, const char *dir)
564 /* if a filename is relative to dir, convert it to an absolute path */
565 {
566     char *newfile;
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);
575     else
576         sprintf (newfile, "%s%s", dir, file);
577     return newfile;
578 }
579
580 /* rcfile_y.y ends here */