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