dc815f937cfee5cf0e3cb35cc41c1a479ee6b877
[ircu2.10.12-pk.git] / ircd / ircd_parser.y
1 /*
2  * ircd_parser.y: A yacc/bison parser for ircd config files.
3  * This is part of ircu, an Internet Relay Chat server.
4  * The contents of this file are Copyright 2001 Diane Bruce,
5  * Andrew Miller, the ircd-hybrid team and the ircu team.
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19  *  USA.
20  * $Id$
21  */
22 %{
23
24 #include "config.h"
25 #include "s_conf.h"
26 #include "class.h"
27 #include "client.h"
28 #include "crule.h"
29 #include "ircd_features.h"
30 #include "fileio.h"
31 #include "gline.h"
32 #include "hash.h"
33 #include "ircd.h"
34 #include "ircd_alloc.h"
35 #include "ircd_chattr.h"
36 #include "ircd_log.h"
37 #include "ircd_reply.h"
38 #include "ircd_snprintf.h"
39 #include "ircd_string.h"
40 #include "list.h"
41 #include "listener.h"
42 #include "match.h"
43 #include "motd.h"
44 #include "numeric.h"
45 #include "numnicks.h"
46 #include "opercmds.h"
47 #include "parse.h"
48 #include "res.h"
49 #include "s_auth.h"
50 #include "s_bsd.h"
51 #include "s_conf.h"
52 #include "s_debug.h"
53 #include "s_misc.h"
54 #include "send.h"
55 #include "struct.h"
56 #include "sys.h"
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <string.h>
60 #include <arpa/inet.h>
61
62 #define MAX_STRINGS 80 /* Maximum number of feature params. */
63 #define USE_IPV4 (1 << 0)
64 #define USE_IPV6 (1 << 1)
65
66   extern struct LocalConf   localConf;
67   extern struct DenyConf*   denyConfList;
68   extern struct CRuleConf*  cruleConfList;
69   extern struct ServerConf* serverConfList;
70   extern struct s_map*      GlobalServiceMapList;
71   extern struct qline*      GlobalQuarantineList;
72
73   int yylex(void);
74   /* Now all the globals we need :/... */
75   int tping, tconn, maxlinks, sendq, port, invert, stringno, flags;
76   char *name, *pass, *host, *ip, *username, *origin, *hub_limit;
77   struct SLink *hosts;
78   char *stringlist[MAX_STRINGS];
79   struct ListenerFlags listen_flags;
80   struct ConnectionClass *c_class;
81   struct DenyConf *dconf;
82   struct ServerConf *sconf;
83   struct s_map *smap;
84   struct Privs privs;
85   struct Privs privs_dirty;
86
87 static void parse_error(char *pattern,...) {
88   static char error_buffer[1024];
89   va_list vl;
90   va_start(vl,pattern);
91   ircd_vsnprintf(NULL, error_buffer, sizeof(error_buffer), pattern, vl);
92   va_end(vl);
93   yyerror(error_buffer);
94 }
95
96 static void free_slist(struct SLink **link) {
97   struct SLink *next;
98   while (*link != NULL) {
99     next = (*link)->next;
100     MyFree((*link)->value.cp);
101     free_link(*link);
102     *link = next;
103   }
104 }
105
106 %}
107
108 %token <text> QSTRING
109 %token <num> NUMBER
110
111 %token GENERAL
112 %token ADMIN
113 %token LOCATION
114 %token CONTACT
115 %token CONNECT
116 %token CLASS
117 %token CHANNEL
118 %token PINGFREQ
119 %token CONNECTFREQ
120 %token MAXLINKS
121 %token MAXHOPS
122 %token SENDQ
123 %token NAME
124 %token HOST
125 %token IP
126 %token USERNAME
127 %token PASS
128 %token LOCAL
129 %token SECONDS
130 %token MINUTES
131 %token HOURS
132 %token DAYS
133 %token WEEKS
134 %token MONTHS
135 %token YEARS
136 %token DECADES
137 %token BYTES
138 %token KBYTES
139 %token MBYTES
140 %token GBYTES
141 %token TBYTES
142 %token SERVER
143 %token PORT
144 %token MASK
145 %token HUB
146 %token LEAF
147 %token UWORLD
148 %token YES
149 %token NO
150 %token OPER
151 %token VHOST
152 %token HIDDEN
153 %token MOTD
154 %token JUPE
155 %token NICK
156 %token NUMERIC
157 %token DESCRIPTION
158 %token CLIENT
159 %token KILL
160 %token CRULE
161 %token REAL
162 %token REASON
163 %token TFILE
164 %token RULE
165 %token ALL
166 %token FEATURES
167 %token QUARANTINE
168 %token PSEUDO
169 %token PREPEND
170 %token USERMODE
171 %token IAUTH
172 %token TIMEOUT
173 %token FAST
174 %token AUTOCONNECT
175 %token PROGRAM
176 %token TOK_IPV4 TOK_IPV6
177 %token DNS
178 /* and now a lot of privileges... */
179 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
180 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
181 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
182 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
183 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
184 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
185 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE TPRIV_APASS_OPMODE
186 %token TPRIV_LIST_CHAN
187 /* and some types... */
188 %type <num> sizespec
189 %type <num> timespec timefactor factoredtimes factoredtime
190 %type <num> expr yesorno privtype address_family
191 %left '+' '-'
192 %left '*' '/'
193
194 %union{
195  char *text;
196  int num;
197 }
198
199 %%
200 /* Blocks in the config file... */
201 blocks: blocks block | block;
202 block: adminblock | generalblock | classblock | connectblock |
203        uworldblock | operblock | portblock | jupeblock | clientblock |
204        killblock | cruleblock | motdblock | featuresblock | quarantineblock |
205        pseudoblock | iauthblock | error ';';
206
207 /* The timespec, sizespec and expr was ripped straight from
208  * ircd-hybrid-7. */
209 timespec: expr | factoredtimes;
210
211 factoredtimes: factoredtimes factoredtime
212 {
213   $$ = $1 + $2;
214 } | factoredtime;
215
216 factoredtime: expr timefactor
217 {
218   $$ = $1 * $2;
219 };
220
221 timefactor: SECONDS { $$ = 1; }
222 | MINUTES { $$ = 60; }
223 | HOURS { $$ = 60 * 60; }
224 | DAYS { $$ = 60 * 60 * 24; }
225 | WEEKS { $$ = 60 * 60 * 24 * 7; }
226 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
227 | YEARS { $$ = 60 * 60 * 24 * 365; }
228 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
229
230
231 sizespec:       expr    {
232                         $$ = $1;
233                 }
234                 | expr BYTES  { 
235                         $$ = $1;
236                 }
237                 | expr KBYTES {
238                         $$ = $1 * 1024;
239                 }
240                 | expr MBYTES {
241                         $$ = $1 * 1024 * 1024;
242                 }
243                 | expr GBYTES {
244                         $$ = $1 * 1024 * 1024 * 1024;
245                 }
246                 | expr TBYTES {
247                         $$ = $1 * 1024 * 1024 * 1024;
248                 }
249                 ;
250
251 /* this is an arithmetic expression */
252 expr: NUMBER
253                 { 
254                         $$ = $1;
255                 }
256                 | expr '+' expr { 
257                         $$ = $1 + $3;
258                 }
259                 | expr '-' expr { 
260                         $$ = $1 - $3;
261                 }
262                 | expr '*' expr { 
263                         $$ = $1 * $3;
264                 }
265                 | expr '/' expr { 
266                         $$ = $1 / $3;
267                 }
268 /* leave this out until we find why it makes BSD yacc dump core -larne
269                 | '-' expr  %prec NEG {
270                         $$ = -$2;
271                 } */
272                 | '(' expr ')' {
273                         $$ = $2;
274                 }
275                 ;
276
277 jupeblock: JUPE '{' jupeitems '}' ';' ;
278 jupeitems: jupeitem jupeitems | jupeitem;
279 jupeitem: jupenick;
280 jupenick: NICK '=' QSTRING ';'
281 {
282   addNickJupes($3);
283   MyFree($3);
284 };
285
286 generalblock: GENERAL
287 {
288     /* Zero out the vhost addresses, in case they were removed. */
289     memset(&VirtualHost_v4.addr, 0, sizeof(VirtualHost_v4.addr));
290     memset(&VirtualHost_v6.addr, 0, sizeof(VirtualHost_v6.addr));
291 } '{' generalitems '}' ';' {
292   if (localConf.name == NULL)
293     parse_error("Your General block must contain a name.");
294   if (localConf.numeric == 0)
295     parse_error("Your General block must contain a numeric (between 1 and 4095).");
296 };
297 generalitems: generalitem generalitems | generalitem;
298 generalitem: generalnumeric | generalname | generalvhost | generaldesc
299   | generaldnsvhost | generaldnsserver;
300
301 generalnumeric: NUMERIC '=' NUMBER ';'
302 {
303   if (localConf.numeric == 0)
304     localConf.numeric = $3;
305   else if (localConf.numeric != $3)
306     parse_error("Redefinition of server numeric %i (%i)", $3,
307                 localConf.numeric);
308 };
309
310 generalname: NAME '=' QSTRING ';'
311 {
312   if (localConf.name == NULL)
313     localConf.name = $3;
314   else {
315     if (strcmp(localConf.name, $3))
316       parse_error("Redefinition of server name %s (%s)", $3,
317                   localConf.name);
318     MyFree($3);
319   }
320 };
321
322 generaldesc: DESCRIPTION '=' QSTRING ';'
323 {
324   MyFree(localConf.description);
325   localConf.description = $3;
326   ircd_strncpy(cli_info(&me), $3, REALLEN);
327 };
328
329 generalvhost: VHOST '=' QSTRING ';'
330 {
331   struct irc_in_addr addr;
332   char *vhost = $3;
333
334   if (!strcmp(vhost, "*")) {
335     /* This traditionally meant bind to all interfaces and connect
336      * from the default. */
337   } else if (!ircd_aton(&addr, vhost))
338     parse_error("Invalid virtual host '%s'.", vhost);
339   else if (irc_in_addr_is_ipv4(&addr))
340     memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
341   else
342     memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
343   MyFree(vhost);
344 };
345
346 generaldnsvhost: DNS VHOST '=' address_family QSTRING ';'
347 {
348   struct irc_in_addr addr;
349   int families = $4;
350   char *vhost = $5;
351
352   if (!strcmp(vhost, "*")) {
353     /* Let the operating system assign the default. */
354   } else if (!ircd_aton(&addr, vhost))
355     parse_error("Invalid DNS virtual host '%s'.", vhost);
356   else
357   {
358     if ((families & USE_IPV4)
359         || (!families && irc_in_addr_is_ipv4(&addr)))
360       memcpy(&VirtualHost_dns_v4.addr, &addr, sizeof(addr));
361     if ((families & USE_IPV6)
362         || (!families && !irc_in_addr_is_ipv4(&addr)))
363       memcpy(&VirtualHost_dns_v6.addr, &addr, sizeof(addr));
364   }
365   MyFree(vhost);
366 };
367
368 generaldnsserver: DNS SERVER '=' QSTRING ';'
369 {
370   char *server = $4;
371
372   add_nameserver(server);
373   MyFree(server);
374 };
375
376 adminblock: ADMIN
377 {
378   MyFree(localConf.location1);
379   MyFree(localConf.location2);
380   MyFree(localConf.contact);
381   localConf.location1 = localConf.location2 = localConf.contact = NULL;
382 }
383 '{' adminitems '}' ';'
384 {
385   if (localConf.location1 == NULL)
386     DupString(localConf.location1, "");
387   if (localConf.location2 == NULL)
388     DupString(localConf.location2, "");
389   if (localConf.contact == NULL)
390     DupString(localConf.contact, "");
391 };
392 adminitems: adminitems adminitem | adminitem;
393 adminitem: adminlocation | admincontact;
394 adminlocation: LOCATION '=' QSTRING ';'
395 {
396   if (localConf.location1 == NULL)
397     localConf.location1 = $3;
398   else if (localConf.location2 == NULL)
399     localConf.location2 = $3;
400   else /* Otherwise just drop it. -A1kmm */
401     MyFree($3);
402 };
403 admincontact: CONTACT '=' QSTRING ';'
404 {
405  MyFree(localConf.contact);
406  localConf.contact = $3;
407 };
408
409 classblock: CLASS {
410   tping = 90;
411 } '{' classitems '}' ';'
412 {
413   if (name != NULL)
414   {
415     struct ConnectionClass *c_class;
416     add_class(name, tping, tconn, maxlinks, sendq);
417     c_class = find_class(name);
418     MyFree(c_class->default_umode);
419     c_class->default_umode = pass;
420     memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
421     memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
422   }
423   else {
424    parse_error("Missing name in class block");
425   }
426   name = NULL;
427   pass = NULL;
428   tconn = 0;
429   maxlinks = 0;
430   sendq = 0;
431   memset(&privs, 0, sizeof(privs));
432   memset(&privs_dirty, 0, sizeof(privs_dirty));
433 };
434 classitems: classitem classitems | classitem;
435 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
436            classsendq | classusermode | priv;
437 classname: NAME '=' QSTRING ';'
438 {
439   MyFree(name);
440   name = $3;
441 };
442 classpingfreq: PINGFREQ '=' timespec ';'
443 {
444   tping = $3;
445 };
446 classconnfreq: CONNECTFREQ '=' timespec ';'
447 {
448   tconn = $3;
449 };
450 classmaxlinks: MAXLINKS '=' expr ';'
451 {
452   maxlinks = $3;
453 };
454 classsendq: SENDQ '=' sizespec ';'
455 {
456   sendq = $3;
457 };
458 classusermode: USERMODE '=' QSTRING ';'
459 {
460   MyFree(pass);
461   pass = $3;
462 };
463
464 connectblock: CONNECT
465 {
466  maxlinks = 65535;
467  flags = CONF_AUTOCONNECT;
468 } '{' connectitems '}' ';'
469 {
470  struct ConfItem *aconf = NULL;
471  if (name == NULL)
472   parse_error("Missing name in connect block");
473  else if (pass == NULL)
474   parse_error("Missing password in connect block");
475  else if (strlen(pass) > PASSWDLEN)
476   parse_error("Password too long in connect block");
477  else if (host == NULL)
478   parse_error("Missing host in connect block");
479  else if (strchr(host, '*') || strchr(host, '?'))
480   parse_error("Invalid host '%s' in connect block", host);
481  else if (c_class == NULL)
482   parse_error("Missing or non-existent class in connect block");
483  else {
484    aconf = make_conf(CONF_SERVER);
485    aconf->name = name;
486    aconf->origin_name = origin;
487    aconf->passwd = pass;
488    aconf->conn_class = c_class;
489    aconf->address.port = port;
490    aconf->host = host;
491    aconf->maximum = maxlinks;
492    aconf->hub_limit = hub_limit;
493    aconf->flags = flags;
494    lookup_confhost(aconf);
495  }
496  if (!aconf) {
497    MyFree(name);
498    MyFree(pass);
499    MyFree(host);
500    MyFree(origin);
501    MyFree(hub_limit);
502  }
503  name = pass = host = origin = hub_limit = NULL;
504  c_class = NULL;
505  port = flags = 0;
506 };
507 connectitems: connectitem connectitems | connectitem;
508 connectitem: connectname | connectpass | connectclass | connecthost
509               | connectport | connectvhost | connectleaf | connecthub
510               | connecthublimit | connectmaxhops | connectauto;
511 connectname: NAME '=' QSTRING ';'
512 {
513  MyFree(name);
514  name = $3;
515 };
516 connectpass: PASS '=' QSTRING ';'
517 {
518  MyFree(pass);
519  pass = $3;
520 };
521 connectclass: CLASS '=' QSTRING ';'
522 {
523  c_class = find_class($3);
524  if (!c_class)
525   parse_error("No such connection class '%s' for Connect block", $3);
526  MyFree($3);
527 };
528 connecthost: HOST '=' QSTRING ';'
529 {
530  MyFree(host);
531  host = $3;
532 };
533 connectport: PORT '=' NUMBER ';'
534 {
535  port = $3;
536 };
537 connectvhost: VHOST '=' QSTRING ';'
538 {
539  MyFree(origin);
540  origin = $3;
541 };
542 connectleaf: LEAF ';'
543 {
544  maxlinks = 0;
545 };
546 connecthub: HUB ';'
547 {
548  MyFree(hub_limit);
549  DupString(hub_limit, "*");
550 };
551 connecthublimit: HUB '=' QSTRING ';'
552 {
553  MyFree(hub_limit);
554  hub_limit = $3;
555 };
556 connectmaxhops: MAXHOPS '=' expr ';'
557 {
558   maxlinks = $3;
559 };
560 connectauto: AUTOCONNECT '=' YES ';' { flags |= CONF_AUTOCONNECT; }
561  | AUTOCONNECT '=' NO ';' { flags &= ~CONF_AUTOCONNECT; };
562
563 uworldblock: UWORLD '{' uworlditems '}' ';';
564 uworlditems: uworlditem uworlditems | uworlditem;
565 uworlditem: uworldname;
566 uworldname: NAME '=' QSTRING ';'
567 {
568   make_conf(CONF_UWORLD)->host = $3;
569 };
570
571 operblock: OPER '{' operitems '}' ';'
572 {
573   struct ConfItem *aconf = NULL;
574   struct SLink *link;
575
576   if (name == NULL)
577     parse_error("Missing name in operator block");
578   else if (pass == NULL)
579     parse_error("Missing password in operator block");
580   /* Do not check password length because it may be crypted. */
581   else if (hosts == NULL)
582     parse_error("Missing host(s) in operator block");
583   else if (c_class == NULL)
584     parse_error("Invalid or missing class in operator block");
585   else if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
586            && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
587     parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
588   else for (link = hosts; link != NULL; link = link->next) {
589     aconf = make_conf(CONF_OPERATOR);
590     DupString(aconf->name, name);
591     DupString(aconf->passwd, pass);
592     conf_parse_userhost(aconf, link->value.cp);
593     aconf->conn_class = c_class;
594     memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
595     memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
596   }
597   MyFree(name);
598   MyFree(pass);
599   free_slist(&hosts);
600   name = pass = NULL;
601   c_class = NULL;
602   memset(&privs, 0, sizeof(privs));
603   memset(&privs_dirty, 0, sizeof(privs_dirty));
604 };
605 operitems: operitem | operitems operitem;
606 operitem: opername | operpass | operhost | operclass | priv;
607 opername: NAME '=' QSTRING ';'
608 {
609   MyFree(name);
610   name = $3;
611 };
612 operpass: PASS '=' QSTRING ';'
613 {
614   MyFree(pass);
615   pass = $3;
616 };
617 operhost: HOST '=' QSTRING ';'
618 {
619  struct SLink *link;
620  link = make_link();
621  if (!strchr($3, '@'))
622  {
623    int uh_len;
624    link->value.cp = (char*) MyMalloc((uh_len = strlen($3)+3));
625    ircd_snprintf(0, link->value.cp, uh_len, "*@%s", $3);
626  }
627  else
628    DupString(link->value.cp, $3);
629  MyFree($3);
630  link->next = hosts;
631  hosts = link;
632 };
633 operclass: CLASS '=' QSTRING ';'
634 {
635  c_class = find_class($3);
636  if (!c_class)
637   parse_error("No such connection class '%s' for Operator block", $3);
638  MyFree($3);
639 };
640
641 priv: privtype '=' yesorno ';'
642 {
643   FlagSet(&privs_dirty, $1);
644   if (($3 == 1) ^ invert)
645     FlagSet(&privs, $1);
646   else
647     FlagClr(&privs, $1);
648   invert = 0;
649 };
650
651 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
652           TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
653           TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
654           TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
655           KILL { $$ = PRIV_KILL; } |
656           TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
657           TPRIV_REHASH { $$ = PRIV_REHASH; } |
658           TPRIV_RESTART { $$ = PRIV_RESTART; } |
659           TPRIV_DIE { $$ = PRIV_DIE; } |
660           TPRIV_GLINE { $$ = PRIV_GLINE; } |
661           TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
662           JUPE { $$ = PRIV_JUPE; } |
663           TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
664           TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
665           TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
666           TPRIV_SET { $$ = PRIV_SET; } |
667           TPRIV_WHOX { $$ = PRIV_WHOX; } |
668           TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
669           TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
670           TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
671           TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
672           TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
673           TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
674           TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
675           TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
676           TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
677           TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
678           TPRIV_LIST_CHAN { $$ = PRIV_LIST_CHAN; } |
679           LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
680           TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
681           TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
682           TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
683
684 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
685
686 /* not a recursive definition because some pedant will just come along
687  * and whine that the parser accepts "ipv4 ipv4 ipv4 ipv4"
688  */
689 address_family:
690                { $$ = 0; }
691     | TOK_IPV4 { $$ = USE_IPV4; }
692     | TOK_IPV6 { $$ = USE_IPV6; }
693     | TOK_IPV4 TOK_IPV6 { $$ = USE_IPV4 | USE_IPV6; }
694     | TOK_IPV6 TOK_IPV4 { $$ = USE_IPV6 | USE_IPV4; }
695     ;
696
697 /* The port block... */
698 portblock: PORT '{' portitems '}' ';'
699 {
700   if (!FlagHas(&listen_flags, LISTEN_IPV4)
701       && !FlagHas(&listen_flags, LISTEN_IPV6))
702   {
703     FlagSet(&listen_flags, LISTEN_IPV4);
704     FlagSet(&listen_flags, LISTEN_IPV6);
705   }
706   if (port > 0 && port <= 0xFFFF)
707     add_listener(port, host, pass, &listen_flags);
708   else
709     parse_error("Port %d is out of range", port);
710   MyFree(host);
711   MyFree(pass);
712   memset(&listen_flags, 0, sizeof(listen_flags));
713   host = pass = NULL;
714   port = 0;
715 };
716 portitems: portitem portitems | portitem;
717 portitem: portnumber | portvhost | portmask | portserver | porthidden;
718 portnumber: PORT '=' address_family NUMBER ';'
719 {
720   int families = $3;
721   if (families & USE_IPV4)
722     FlagSet(&listen_flags, LISTEN_IPV4);
723   else if (families & USE_IPV6)
724     FlagSet(&listen_flags, LISTEN_IPV6);
725   port = $4;
726 };
727
728 portvhost: VHOST '=' address_family QSTRING ';'
729 {
730   int families = $3;
731   if (families & USE_IPV4)
732     FlagSet(&listen_flags, LISTEN_IPV4);
733   else if (families & USE_IPV6)
734     FlagSet(&listen_flags, LISTEN_IPV6);
735   MyFree(host);
736   host = $4;
737 };
738
739 portmask: MASK '=' QSTRING ';'
740 {
741   MyFree(pass);
742   pass = $3;
743 };
744
745 portserver: SERVER '=' YES ';'
746 {
747   FlagSet(&listen_flags, LISTEN_SERVER);
748 } | SERVER '=' NO ';'
749 {
750   FlagClr(&listen_flags, LISTEN_SERVER);
751 };
752
753 porthidden: HIDDEN '=' YES ';'
754 {
755   FlagSet(&listen_flags, LISTEN_HIDDEN);
756 } | HIDDEN '=' NO ';'
757 {
758   FlagClr(&listen_flags, LISTEN_HIDDEN);
759 };
760
761 clientblock: CLIENT
762 {
763   maxlinks = 65535;
764   port = 0;
765 }
766 '{' clientitems '}' ';'
767 {
768   struct ConfItem *aconf = 0;
769   struct irc_in_addr addr;
770   unsigned char addrbits = 0;
771
772   if (!c_class)
773     parse_error("Invalid or missing class in Client block");
774   else if (pass && strlen(pass) > PASSWDLEN)
775     parse_error("Password too long in connect block");
776   else if (ip && !ipmask_parse(ip, &addr, &addrbits))
777     parse_error("Invalid IP address %s in Client block", ip);
778   else {
779     aconf = make_conf(CONF_CLIENT);
780     aconf->username = username;
781     aconf->host = host;
782     if (ip)
783       memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
784     else
785       memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
786     aconf->address.port = port;
787     aconf->addrbits = addrbits;
788     aconf->name = ip;
789     aconf->conn_class = c_class;
790     aconf->maximum = maxlinks;
791     aconf->passwd = pass;
792   }
793   if (!aconf) {
794     MyFree(username);
795     MyFree(host);
796     MyFree(ip);
797     MyFree(pass);
798   }
799   host = NULL;
800   username = NULL;
801   c_class = NULL;
802   ip = NULL;
803   pass = NULL;
804   port = 0;
805 };
806 clientitems: clientitem clientitems | clientitem;
807 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | clientport;
808 clienthost: HOST '=' QSTRING ';'
809 {
810   char *sep = strchr($3, '@');
811   MyFree(host);
812   if (sep) {
813     *sep++ = '\0';
814     MyFree(username);
815     DupString(host, sep);
816     username = $3;
817   } else {
818     host = $3;
819   }
820 };
821 clientip: IP '=' QSTRING ';'
822 {
823   char *sep;
824   sep = strchr($3, '@');
825   MyFree(ip);
826   if (sep) {
827     *sep++ = '\0';
828     MyFree(username);
829     DupString(ip, sep);
830     username = $3;
831   } else {
832     ip = $3;
833   }
834 };
835 clientusername: USERNAME '=' QSTRING ';'
836 {
837   MyFree(username);
838   username = $3;
839 };
840 clientclass: CLASS '=' QSTRING ';'
841 {
842   c_class = find_class($3);
843   if (!c_class)
844     parse_error("No such connection class '%s' for Client block", $3);
845   MyFree($3);
846 };
847 clientpass: PASS '=' QSTRING ';'
848 {
849   MyFree(pass);
850   pass = $3;
851 };
852 clientmaxlinks: MAXLINKS '=' expr ';'
853 {
854   maxlinks = $3;
855 };
856 clientport: PORT '=' expr ';'
857 {
858   port = $3;
859 };
860
861 killblock: KILL
862 {
863   dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
864 } '{' killitems '}' ';'
865 {
866   if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
867     dconf->next = denyConfList;
868     denyConfList = dconf;
869   }
870   else
871   {
872     MyFree(dconf->usermask);
873     MyFree(dconf->hostmask);
874     MyFree(dconf->realmask);
875     MyFree(dconf->message);
876     MyFree(dconf);
877     parse_error("Kill block must match on at least one of username, host or realname");
878   }
879   dconf = NULL;
880 };
881 killitems: killitem killitems | killitem;
882 killitem: killuhost | killreal | killusername | killreasonfile | killreason;
883 killuhost: HOST '=' QSTRING ';'
884 {
885   char *h;
886   MyFree(dconf->hostmask);
887   MyFree(dconf->usermask);
888   if ((h = strchr($3, '@')) == NULL)
889   {
890     DupString(dconf->usermask, "*");
891     dconf->hostmask = $3;
892   }
893   else
894   {
895     *h++ = '\0';
896     DupString(dconf->hostmask, h);
897     dconf->usermask = $3;
898   }
899   ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
900 };
901
902 killusername: USERNAME '=' QSTRING ';'
903 {
904   MyFree(dconf->usermask);
905   dconf->usermask = $3;
906 };
907
908 killreal: REAL '=' QSTRING ';'
909 {
910  MyFree(dconf->realmask);
911  dconf->realmask = $3;
912 };
913
914 killreason: REASON '=' QSTRING ';'
915 {
916  dconf->flags &= ~DENY_FLAGS_FILE;
917  MyFree(dconf->message);
918  dconf->message = $3;
919 };
920
921 killreasonfile: TFILE '=' QSTRING ';'
922 {
923  dconf->flags |= DENY_FLAGS_FILE;
924  MyFree(dconf->message);
925  dconf->message = $3;
926 };
927
928 cruleblock: CRULE
929 {
930   tconn = CRULE_AUTO;
931 } '{' cruleitems '}' ';'
932 {
933   struct CRuleNode *node = NULL;
934   if (host == NULL)
935     parse_error("Missing host in crule block");
936   else if (pass == NULL)
937     parse_error("Missing rule in crule block");
938   else if ((node = crule_parse(pass)) == NULL)
939     parse_error("Invalid rule '%s' in crule block", pass);
940   else
941   {
942     struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
943     p->hostmask = host;
944     p->rule = pass;
945     p->type = tconn;
946     p->node = node;
947     p->next = cruleConfList;
948     cruleConfList = p;
949   }
950   if (!node)
951   {
952     MyFree(host);
953     MyFree(pass);
954   }
955   host = pass = NULL;
956   tconn = 0;
957 };
958
959 cruleitems: cruleitem cruleitems | cruleitem;
960 cruleitem: cruleserver | crulerule | cruleall;
961
962 cruleserver: SERVER '=' QSTRING ';'
963 {
964   MyFree(host);
965   collapse($3);
966   host = $3;
967 };
968
969 crulerule: RULE '=' QSTRING ';'
970 {
971  MyFree(pass);
972  pass = $3;
973 };
974
975 cruleall: ALL '=' YES ';'
976 {
977  tconn = CRULE_ALL;
978 } | ALL '=' NO ';'
979 {
980  tconn = CRULE_AUTO;
981 };
982
983 motdblock: MOTD '{' motditems '}' ';'
984 {
985   struct SLink *link;
986   if (pass != NULL)
987     for (link = hosts; link != NULL; link = link->next)
988       motd_add(link->value.cp, pass);
989   free_slist(&hosts);
990   MyFree(pass);
991   pass = NULL;
992 };
993
994 motditems: motditem motditems | motditem;
995 motditem: motdhost | motdfile;
996 motdhost: HOST '=' QSTRING ';'
997 {
998   struct SLink *link;
999   link = make_link();
1000   link->value.cp = $3;
1001   link->next = hosts;
1002   hosts = link;
1003 };
1004
1005 motdfile: TFILE '=' QSTRING ';'
1006 {
1007   MyFree(pass);
1008   pass = $3;
1009 };
1010
1011 featuresblock: FEATURES '{' featureitems '}' ';';
1012 featureitems: featureitems featureitem | featureitem;
1013
1014 featureitem: QSTRING
1015 {
1016   stringlist[0] = $1;
1017   stringno = 1;
1018 } '=' stringlist ';' {
1019   unsigned int ii;
1020   feature_set(NULL, (const char * const *)stringlist, stringno);
1021   for (ii = 0; ii < stringno; ++ii)
1022     MyFree(stringlist[ii]);
1023 };
1024
1025 stringlist: stringlist extrastring | extrastring;
1026 extrastring: QSTRING
1027 {
1028   if (stringno < MAX_STRINGS)
1029     stringlist[stringno++] = $1;
1030   else
1031     MyFree($1);
1032 };
1033
1034 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
1035 quarantineitems: quarantineitems quarantineitem | quarantineitem;
1036 quarantineitem: QSTRING '=' QSTRING ';'
1037 {
1038   struct qline *qconf = MyCalloc(1, sizeof(*qconf));
1039   qconf->chname = $1;
1040   qconf->reason = $3;
1041   qconf->next = GlobalQuarantineList;
1042   GlobalQuarantineList = qconf;
1043 };
1044
1045 pseudoblock: PSEUDO QSTRING '{'
1046 {
1047   smap = MyCalloc(1, sizeof(struct s_map));
1048   smap->command = $2;
1049 }
1050 pseudoitems '}' ';'
1051 {
1052   int valid = 0;
1053
1054   if (!smap->name)
1055     parse_error("Missing name in pseudo %s block", smap->command);
1056   else if (!smap->services)
1057     parse_error("Missing nick in pseudo %s block", smap->command);
1058   else
1059     valid = 1;
1060   if (valid && register_mapping(smap))
1061   {
1062     smap->next = GlobalServiceMapList;
1063     GlobalServiceMapList = smap;
1064   }
1065   else
1066   {
1067     free_mapping(smap);
1068   }
1069   smap = NULL;
1070 };
1071
1072 pseudoitems: pseudoitem pseudoitems | pseudoitem;
1073 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags;
1074 pseudoname: NAME '=' QSTRING ';'
1075 {
1076   MyFree(smap->name);
1077   smap->name = $3;
1078 };
1079 pseudoprepend: PREPEND '=' QSTRING ';'
1080 {
1081   MyFree(smap->prepend);
1082   smap->prepend = $3;
1083 };
1084 pseudonick: NICK '=' QSTRING ';'
1085 {
1086   char *sep = strchr($3, '@');
1087
1088   if (sep != NULL) {
1089     size_t slen = strlen($3);
1090     struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
1091     memcpy(nh->nick, $3, slen + 1);
1092     nh->nicklen = sep - $3;
1093     nh->next = smap->services;
1094     smap->services = nh;
1095   }
1096   MyFree($3);
1097 };
1098 pseudoflags: FAST ';'
1099 {
1100   smap->flags |= SMAP_FAST;
1101 };
1102
1103 iauthblock: IAUTH '{' iauthitems '}' ';'
1104 {
1105   auth_spawn(stringno, stringlist);
1106   while (stringno > 0)
1107     MyFree(stringlist[--stringno]);
1108 };
1109
1110 iauthitems: iauthitem iauthitems | iauthitem;
1111 iauthitem: iauthprogram;
1112 iauthprogram: PROGRAM '='
1113 {
1114   while (stringno > 0)
1115     MyFree(stringlist[--stringno]);
1116 } stringlist ';';