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