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