]> Pileus Git - ~andy/fetchmail/blob - rcfile_y.y
Various minor bug fixes.
[~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 #if defined(STDC_HEADERS)
18 #include <stdlib.h>
19 #endif
20 #if defined(HAVE_UNISTD_H)
21 #include <unistd.h>
22 #endif
23 #include <string.h>
24
25 #if NET_SECURITY
26 #include <net/security.h>
27 #endif /* NET_SECURITY */
28
29 #include "fetchmail.h"
30
31 /* parser reads these */
32 char *rcfile;                   /* path name of rc file */
33 struct query cmd_opts;          /* where to put command-line info */
34
35 /* parser sets these */
36 struct query *querylist;        /* head of server list (globally visible) */
37
38 int yydebug;                    /* in case we didn't generate with -- debug */
39
40 static struct query current;    /* current server record */
41 static int prc_errflag;
42 static struct hostdata *leadentry;
43 static flag trailer;
44
45 static void record_current(void);
46 static void user_reset(void);
47 static void reset_server(const char *name, int skip);
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 PREAUTHENTICATE TIMEOUT KPOP SDPS KERBEROS4 KERBEROS5 KERBEROS SSH
62 %token ENVELOPE QVIRTUAL USERNAME PASSWORD FOLDER SMTPHOST MDA BSMTP LMTP
63 %token SMTPADDRESS SPAMRESPONSE PRECONNECT POSTCONNECT LIMIT WARNINGS
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 BOUNCEMAIL
68 %token <proto> PROTO
69 %token <sval>  STRING
70 %token <number> NUMBER
71 %token NO KEEP FLUSH FETCHALL REWRITE FORCECR STRIPCR PASS8BITS DROPSTATUS
72 %token DNS SERVICE PORT UIDL INTERVAL MIMEDECODE CHECKALIAS SSL SSLKEY SSLCERT
73
74 %%
75
76 rcfile          : /* empty */
77                 | statement_list
78                 ;
79
80 statement_list  : statement
81                 | statement_list statement
82                 ;
83
84 optmap          : MAP | /* EMPTY */;
85
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 BOUNCEMAIL                {run.bouncemail = TRUE;}
92                 | SET NO BOUNCEMAIL             {run.bouncemail = FALSE;}
93                 | SET PROPERTIES optmap STRING  {run.properties =xstrdup($4);}
94                 | SET SYSLOG                    {run.use_syslog = TRUE;}
95                 | SET INVISIBLE                 {run.invisible = TRUE;}
96
97 /* 
98  * The way the next two productions are written depends on the fact that
99  * userspecs cannot be empty.  It's a kluge to deal with files that set
100  * up a load of defaults and then have poll statements following with no
101  * user options at all. 
102  */
103                 | define_server serverspecs             {record_current();}
104                 | define_server serverspecs userspecs
105
106 /* detect and complain about the most common user error */
107                 | define_server serverspecs userspecs serv_option
108                         {yyerror("server option after user options");}
109                 ;
110
111 define_server   : POLL STRING           {reset_server($2, FALSE);}
112                 | SKIP STRING           {reset_server($2, TRUE);}
113                 | DEFAULTS              {reset_server("defaults", FALSE);}
114                 ;
115
116 serverspecs     : /* EMPTY */
117                 | serverspecs serv_option
118                 ;
119
120 alias_list      : STRING                {save_str(&current.server.akalist,$1,0);}
121                 | alias_list STRING     {save_str(&current.server.akalist,$2,0);}
122                 ;
123
124 domain_list     : STRING                {save_str(&current.server.localdomains,$1,0);}
125                 | domain_list STRING    {save_str(&current.server.localdomains,$2,0);}
126                 ;
127
128 serv_option     : AKA alias_list
129                 | VIA STRING            {current.server.via = xstrdup($2);}
130                 | LOCALDOMAINS domain_list
131                 | PROTOCOL PROTO        {current.server.protocol = $2;}
132                 | PROTOCOL KPOP         {
133                                             current.server.protocol = P_POP3;
134
135                                             if (current.server.preauthenticate == A_PASSWORD)
136 #ifdef KERBEROS_V5
137                                                 current.server.preauthenticate = A_KERBEROS_V5;
138 #else
139                                                 current.server.preauthenticate = A_KERBEROS_V4;
140 #endif /* KERBEROS_V5 */
141 #if INET6_ENABLE
142                                             current.server.service = KPOP_PORT;
143 #else /* INET6_ENABLE */
144                                             current.server.port = KPOP_PORT;
145 #endif /* INET6_ENABLE */
146                                         }
147                 | PROTOCOL SDPS         {
148 #ifdef SDPS_ENABLE
149                                             current.server.protocol = P_POP3;
150                                             current.server.sdps = TRUE;
151 #else
152                                             yyerror("SDPS not enabled.");
153 #endif /* SDPS_ENABLE */
154                                         }
155                 | UIDL                  {current.server.uidl = FLAG_TRUE;}
156                 | NO UIDL               {current.server.uidl  = FLAG_FALSE;}
157                 | CHECKALIAS            {current.server.checkalias = FLAG_TRUE;}
158                 | NO CHECKALIAS         {current.server.checkalias  = FLAG_FALSE;}
159                 | SERVICE STRING        {
160 #if INET6_ENABLE
161                                         current.server.service = $2;
162 #endif /* INET6_ENABLE */
163                                         }
164                 | PORT NUMBER           {
165 #if INET6_ENABLE
166                                         int port = $2;
167                                         char buf[10];
168                                         sprintf(buf, "%d", port);
169                                         current.server.service = xstrdup(buf);
170 #else
171                                         current.server.port = $2;
172 #endif /* INET6_ENABLE */
173                 }
174                 | INTERVAL NUMBER               {current.server.interval = $2;}
175                 | PREAUTHENTICATE PASSWORD      {current.server.preauthenticate = A_PASSWORD;}
176                 | PREAUTHENTICATE KERBEROS4     {current.server.preauthenticate = A_KERBEROS_V4;}
177                 | PREAUTHENTICATE KERBEROS5     {current.server.preauthenticate = A_KERBEROS_V5;}
178                 | PREAUTHENTICATE KERBEROS         {
179 #ifdef KERBEROS_V5
180                     current.server.preauthenticate = A_KERBEROS_V5;
181 #else
182                     current.server.preauthenticate = A_KERBEROS_V4;
183 #endif /* KERBEROS_V5 */
184                 }
185                 | PREAUTHENTICATE SSH   {current.server.preauthenticate = A_SSH;}
186                 | TIMEOUT NUMBER        {current.server.timeout = $2;}
187
188                 | ENVELOPE NUMBER STRING 
189                                         {
190                                             current.server.envelope = 
191                                                 xstrdup($3);
192                                             current.server.envskip = $2;
193                                         }
194                 | ENVELOPE STRING
195                                         {
196                                             current.server.envelope = 
197                                                 xstrdup($2);
198                                             current.server.envskip = 0;
199                                         }
200
201                 | QVIRTUAL STRING       {current.server.qvirtual=xstrdup($2);}
202                 | NETSEC STRING         {
203 #ifdef NET_SECURITY
204                                             void *request;
205                                             int requestlen;
206
207                                             if (net_security_strtorequest($2, &request, &requestlen))
208                                                 yyerror("invalid security request");
209                                             else {
210                                                 current.server.netsec = xstrdup($2);
211                                                 free(request);
212                                             }
213 #else
214                                             yyerror("network-security support disabled");
215 #endif /* NET_SECURITY */
216                                         }
217                 | INTERFACE STRING      {
218 #if (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__)
219                                         interface_parse($2, &current.server);
220 #else /* (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__) */
221                                         fprintf(stderr, "fetchmail: interface option is only supported under Linux and FreeBSD\n");
222 #endif /* (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__) */
223                                         }
224                 | MONITOR STRING        {
225 #if (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__)
226                                         current.server.monitor = xstrdup($2);
227 #else /* (defined(linux) && !defined(INET6_ENABLE)) || defined(__FreeBSD__) */
228                                         fprintf(stderr, "fetchmail: monitor option is only supported under Linux\n");
229 #endif /* (defined(linux) && !defined(INET6_ENABLE) || defined(__FreeBSD__)) */
230                                         }
231                 | PLUGIN STRING         { current.server.plugin = xstrdup($2); }
232                 | PLUGOUT STRING        { current.server.plugout = xstrdup($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                 ;
237
238 userspecs       : user1opts             {record_current(); user_reset();}
239                 | explicits
240                 ;
241
242 explicits       : explicitdef           {record_current(); user_reset();}
243                 | explicits explicitdef {record_current(); user_reset();}
244                 ;
245
246 explicitdef     : userdef user0opts
247                 ;
248
249 userdef         : USERNAME STRING       {current.remotename = xstrdup($2);}
250                 | USERNAME mapping_list HERE
251                 | USERNAME STRING THERE {current.remotename = xstrdup($2);}
252                 ;
253
254 user0opts       : /* EMPTY */
255                 | user0opts user_option
256                 ;
257
258 user1opts       : user_option
259                 | user1opts user_option
260                 ;
261
262 localnames      : WILDCARD              {current.wildcard =  TRUE;}
263                 | mapping_list          {current.wildcard =  FALSE;}
264                 | mapping_list WILDCARD {current.wildcard =  TRUE;}
265                 ;
266
267 mapping_list    : mapping               
268                 | mapping_list mapping
269                 ;
270
271 mapping         : STRING        
272                                 {save_str_pair(&current.localnames, $1, NULL);}
273                 | STRING MAP STRING
274                                 {save_str_pair(&current.localnames, $1, $3);}
275                 ;
276
277 folder_list     : STRING                {save_str(&current.mailboxes,$1,0);}
278                 | folder_list STRING    {save_str(&current.mailboxes,$2,0);}
279                 ;
280
281 smtp_list       : STRING                {save_str(&current.smtphunt, $1,TRUE);}
282                 | smtp_list STRING      {save_str(&current.smtphunt, $2,TRUE);}
283                 ;
284
285 num_list        : NUMBER
286                         {
287                             struct idlist *id;
288                             id=save_str(&current.antispam,STRING_DUMMY,0);
289                             id->val.status.num = $1;
290                         }
291                 | num_list NUMBER
292                         {
293                             struct idlist *id;
294                             id=save_str(&current.antispam,STRING_DUMMY,0);
295                             id->val.status.num = $2;
296                         }
297                 ;
298
299 user_option     : TO localnames HERE
300                 | TO localnames
301                 | IS localnames HERE
302                 | IS localnames
303
304                 | IS STRING THERE       {current.remotename  = xstrdup($2);}
305                 | PASSWORD STRING       {current.password    = xstrdup($2);}
306                 | FOLDER folder_list
307                 | SMTPHOST smtp_list
308                 | SMTPADDRESS STRING    {current.smtpaddress = xstrdup($2);}
309                 | SPAMRESPONSE num_list
310                 | MDA STRING            {current.mda         = xstrdup($2);}
311                 | BSMTP STRING          {current.bsmtp       = xstrdup($2);}
312                 | LMTP                  {current.listener    = LMTP_MODE;}
313                 | PRECONNECT STRING     {current.preconnect  = xstrdup($2);}
314                 | POSTCONNECT STRING    {current.postconnect = xstrdup($2);}
315
316                 | KEEP                  {current.keep        = FLAG_TRUE;}
317                 | FLUSH                 {current.flush       = FLAG_TRUE;}
318                 | FETCHALL              {current.fetchall    = FLAG_TRUE;}
319                 | REWRITE               {current.rewrite     = FLAG_TRUE;}
320                 | FORCECR               {current.forcecr     = FLAG_TRUE;}
321                 | STRIPCR               {current.stripcr     = FLAG_TRUE;}
322                 | PASS8BITS             {current.pass8bits   = FLAG_TRUE;}
323                 | DROPSTATUS            {current.dropstatus  = FLAG_TRUE;}
324                 | MIMEDECODE            {current.mimedecode  = FLAG_TRUE;}
325
326                 | SSL                   {current.use_ssl = FLAG_TRUE;}
327                 | SSLKEY STRING         {current.sslkey = xstrdup($2);}
328                 | SSLCERT STRING        {current.sslcert = xstrdup($2);}
329
330                 | NO KEEP               {current.keep        = FLAG_FALSE;}
331                 | NO FLUSH              {current.flush       = FLAG_FALSE;}
332                 | NO FETCHALL           {current.fetchall    = FLAG_FALSE;}
333                 | NO REWRITE            {current.rewrite     = FLAG_FALSE;}
334                 | NO FORCECR            {current.forcecr     = FLAG_FALSE;}
335                 | NO STRIPCR            {current.stripcr     = FLAG_FALSE;}
336                 | NO PASS8BITS          {current.pass8bits   = FLAG_FALSE;}
337                 | NO DROPSTATUS         {current.dropstatus  = FLAG_FALSE;}
338                 | NO MIMEDECODE         {current.mimedecode  = FLAG_FALSE;}
339
340                 | NO SSL                {current.use_ssl = FLAG_FALSE;}
341
342                 | LIMIT NUMBER          {current.limit       = NUM_VALUE_IN($2);}
343                 | WARNINGS NUMBER       {current.warnings    = NUM_VALUE_IN($2);}
344                 | FETCHLIMIT NUMBER     {current.fetchlimit  = NUM_VALUE_IN($2);}
345                 | BATCHLIMIT NUMBER     {current.batchlimit  = NUM_VALUE_IN($2);}
346                 | EXPUNGE NUMBER        {current.expunge     = NUM_VALUE_IN($2);}
347
348                 | PROPERTIES STRING     {current.properties  = xstrdup($2);}
349                 ;
350 %%
351
352 /* lexer interface */
353 extern char *rcfile;
354 extern int prc_lineno;
355 extern char *yytext;
356 extern FILE *yyin;
357
358 static struct query *hosttail;  /* where to add new elements */
359
360 void yyerror (const char *s)
361 /* report a syntax error */
362 {
363     report_at_line(stderr, 0, rcfile, prc_lineno, "%s at %s", s, 
364                    (yytext && yytext[0]) ? yytext : "end of input");
365     prc_errflag++;
366 }
367
368 int prc_filecheck(const char *pathname, const flag securecheck)
369 /* check that a configuration file is secure */
370 {
371 #ifndef __EMX__
372     struct stat statbuf;
373
374     errno = 0;
375
376     /* special case useful for debugging purposes */
377     if (strcmp("/dev/null", pathname) == 0)
378         return(PS_SUCCESS);
379
380     /* pass through the special name for stdin */
381     if (strcmp("-", pathname) == 0)
382         return(PS_SUCCESS);
383
384     /* the run control file must have the same uid as the REAL uid of this 
385        process, it must have permissions no greater than 600, and it must not 
386        be a symbolic link.  We check these conditions here. */
387
388     if (lstat(pathname, &statbuf) < 0) {
389         if (errno == ENOENT) 
390             return(PS_SUCCESS);
391         else {
392             report(stderr, "lstat: %s: %s\n", pathname, strerror(errno));
393             return(PS_IOERR);
394         }
395     }
396
397     if (!securecheck)   return PS_SUCCESS;
398
399     if ((statbuf.st_mode & S_IFLNK) == S_IFLNK)
400     {
401         fprintf(stderr, "File %s must not be a symbolic link.\n", pathname);
402         return(PS_IOERR);
403     }
404
405     if (statbuf.st_mode & ~(S_IFREG | S_IREAD | S_IWRITE | S_IEXEC | S_IXGRP))
406     {
407         fprintf(stderr, "File %s must have no more than -rwx--x--- (0710) permissions.\n", 
408                 pathname);
409         return(PS_IOERR);
410     }
411
412 #ifdef HAVE_GETEUID
413     if (statbuf.st_uid != geteuid())
414 #else
415     if (statbuf.st_uid != getuid())
416 #endif /* HAVE_GETEUID */
417     {
418         fprintf(stderr, "File %s must be owned by you.\n", pathname);
419         return(PS_IOERR);
420     }
421 #endif
422     return(PS_SUCCESS);
423 }
424
425 int prc_parse_file (const char *pathname, const flag securecheck)
426 /* digest the configuration into a linked list of host records */
427 {
428     prc_errflag = 0;
429     querylist = hosttail = (struct query *)NULL;
430
431     errno = 0;
432
433     /* Check that the file is secure */
434     if ( (prc_errflag = prc_filecheck(pathname, securecheck)) != 0 )
435         return(prc_errflag);
436
437     /*
438      * Croak if the configuration directory does not exist.
439      * This probably means an NFS mount failed and we can't
440      * see a configuration file that ought to be there.
441      * Question: is this a portable check? It's not clear
442      * that all implementations of lstat() will return ENOTDIR
443      * rather than plain ENOENT in this case...
444      */
445     if (errno == ENOTDIR)
446         return(PS_IOERR);
447     else if (errno == ENOENT)
448         return(PS_SUCCESS);
449
450     /* Open the configuration file and feed it to the lexer. */
451     if (strcmp(pathname, "-") == 0)
452         yyin = stdin;
453     else if ((yyin = fopen(pathname,"r")) == (FILE *)NULL) {
454         report(stderr, "open: %s: %s\n", pathname, strerror(errno));
455         return(PS_IOERR);
456     }
457
458     yyparse();          /* parse entire file */
459
460     fclose(yyin);       /* not checking this should be safe, file mode was r */
461
462     if (prc_errflag) 
463         return(PS_SYNTAX);
464     else
465         return(PS_SUCCESS);
466 }
467
468 static void reset_server(const char *name, int skip)
469 /* clear the entire global record and initialize it with a new name */
470 {
471     trailer = FALSE;
472     memset(&current,'\0',sizeof(current));
473     current.smtp_socket = -1;
474     current.server.pollname = xstrdup(name);
475     current.server.skip = skip;
476 }
477
478
479 static void user_reset(void)
480 /* clear the global current record (user parameters) used by the parser */
481 {
482     struct hostdata save;
483
484     /*
485      * Purpose of this code is to initialize the new server block, but
486      * preserve whatever server name was previously set.  Also
487      * preserve server options unless the command-line explicitly
488      * overrides them.
489      */
490     save = current.server;
491
492     memset(&current, '\0', sizeof(current));
493     current.smtp_socket = -1;
494
495     current.server = save;
496 }
497
498 struct query *hostalloc(init)
499 /* append a host record to the host list */
500 struct query *init;     /* pointer to block containing initial values */
501 {
502     struct query *node;
503
504     /* allocate new node */
505     node = (struct query *) xmalloc(sizeof(struct query));
506
507     /* initialize it */
508     if (init)
509         memcpy(node, init, sizeof(struct query));
510     else
511     {
512         memset(node, '\0', sizeof(struct query));
513         node->smtp_socket = -1;
514     }
515
516     /* append to end of list */
517     if (hosttail != (struct query *) 0)
518         hosttail->next = node;  /* list contains at least one element */
519     else
520         querylist = node;       /* list is empty */
521     hosttail = node;
522
523     if (trailer)
524         node->server.lead_server = leadentry;
525     else
526     {
527         node->server.lead_server = NULL;
528         leadentry = &node->server;
529     }
530
531     return(node);
532 }
533
534 static void record_current(void)
535 /* register current parameters and append to the host list */
536 {
537     (void) hostalloc(&current);
538     trailer = TRUE;
539 }
540
541 /* easier to do this than cope with variations in where the library lives */
542 int yywrap(void) {return 1;}
543
544 /* rcfile_y.y ends here */
545
546