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