fixed ssl.c bug when ssl backend returns IO_BLOCKED but IO engine doesn't get informe...
[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: ircd_parser.y 1907 2009-02-09 04:11:04Z entrope $
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 << 16)
64 #define USE_IPV6 (1 << 17)
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, maxchans, iauth_required = 0;
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   struct webirc_block *webirc;
87
88 static void parse_error(char *pattern,...) {
89   static char error_buffer[1024];
90   va_list vl;
91   va_start(vl,pattern);
92   ircd_vsnprintf(NULL, error_buffer, sizeof(error_buffer), pattern, vl);
93   va_end(vl);
94   yyerror(error_buffer);
95 }
96
97 static void free_slist(struct SLink **link) {
98   struct SLink *next;
99   while (*link != NULL) {
100     next = (*link)->next;
101     MyFree((*link)->value.cp);
102     free_link(*link);
103     *link = next;
104   }
105 }
106
107 %}
108
109 %token <text> QSTRING
110 %token <num> NUMBER
111
112 %token GENERAL
113 %token ADMIN
114 %token LOCATION
115 %token CONTACT
116 %token CONNECT
117 %token CLASS
118 %token CHANNEL
119 %token PINGFREQ
120 %token CONNECTFREQ
121 %token MAXLINKS
122 %token MAXHOPS
123 %token SENDQ
124 %token NAME
125 %token HOST
126 %token IP
127 %token USERNAME
128 %token PASS
129 %token LOCAL
130 %token SECONDS
131 %token MINUTES
132 %token HOURS
133 %token DAYS
134 %token WEEKS
135 %token MONTHS
136 %token YEARS
137 %token DECADES
138 %token BYTES
139 %token KBYTES
140 %token MBYTES
141 %token GBYTES
142 %token TBYTES
143 %token SERVER
144 %token PORT
145 %token MASK
146 %token HUB
147 %token LEAF
148 %token UWORLD
149 %token YES
150 %token NO
151 %token OPER
152 %token VHOST
153 %token HIDDEN
154 %token MOTD
155 %token JUPE
156 %token NICK
157 %token NUMERIC
158 %token DESCRIPTION
159 %token CLIENT
160 %token KILL
161 %token CRULE
162 %token REAL
163 %token REASON
164 %token TFILE
165 %token RULE
166 %token ALL
167 %token FEATURES
168 %token QUARANTINE
169 %token PSEUDO
170 %token PREPEND
171 %token USERMODE
172 %token IAUTH
173 %token TIMEOUT
174 %token FAST
175 %token AUTOCONNECT
176 %token PROGRAM
177 %token TOK_IPV4 TOK_IPV6
178 %token DNS
179 %token FORWARDS
180 %token SECURE
181 %token WEBIRC
182 %token SPOOF
183 %token MAXCHANS
184 %token REQUIRED
185 %token SSL
186 %token CERT
187 %token CACERT
188 /* and now a lot of privileges... */
189 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
190 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
191 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
192 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
193 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
194 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
195 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE TPRIV_APASS_OPMODE
196 %token TPRIV_LIST_CHAN TPRIV_SEE_IDLETIME TPRIV_UMODE_NETSERV
197 %token TPRIV_UMODE_NOCHAN TPRIV_UMODE_NOIDLE TPRIV_UMODE_CHSERV TPRIV_UMODE_XTRAOP
198 %token TPRIV_FLOOD TPRIV_HALFFLOOD TPRIV_UNLIMITED_TARGET TPRIV_UMODE_OVERRIDECC
199 %token TPRIV_HIDE_IDLETIME TPRIV_NOAMSG_OVERRIDE
200 /* and some types... */
201 %type <num> sizespec
202 %type <num> timespec timefactor factoredtimes factoredtime
203 %type <num> expr yesorno privtype address_family
204 %left '+' '-'
205 %left '*' '/'
206
207 %union{
208  char *text;
209  int num;
210 }
211
212 %%
213 /* Blocks in the config file... */
214 blocks: blocks block | block;
215 block: adminblock | generalblock | classblock | connectblock |
216        uworldblock | operblock | portblock | jupeblock | clientblock |
217        killblock | cruleblock | motdblock | featuresblock | quarantineblock |
218        pseudoblock | iauthblock | forwardsblock | webircblock | sslblock | error ';';
219
220 /* The timespec, sizespec and expr was ripped straight from
221  * ircd-hybrid-7. */
222 timespec: expr | factoredtimes;
223
224 factoredtimes: factoredtimes factoredtime
225 {
226   $$ = $1 + $2;
227 } | factoredtime;
228
229 factoredtime: expr timefactor
230 {
231   $$ = $1 * $2;
232 };
233
234 timefactor: SECONDS { $$ = 1; }
235 | MINUTES { $$ = 60; }
236 | HOURS { $$ = 60 * 60; }
237 | DAYS { $$ = 60 * 60 * 24; }
238 | WEEKS { $$ = 60 * 60 * 24 * 7; }
239 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
240 | YEARS { $$ = 60 * 60 * 24 * 365; }
241 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
242
243
244 sizespec:       expr    {
245                         $$ = $1;
246                 }
247                 | expr BYTES  { 
248                         $$ = $1;
249                 }
250                 | expr KBYTES {
251                         $$ = $1 * 1024;
252                 }
253                 | expr MBYTES {
254                         $$ = $1 * 1024 * 1024;
255                 }
256                 | expr GBYTES {
257                         $$ = $1 * 1024 * 1024 * 1024;
258                 }
259                 | expr TBYTES {
260                         $$ = $1 * 1024 * 1024 * 1024;
261                 }
262                 ;
263
264 /* this is an arithmetic expression */
265 expr: NUMBER
266                 { 
267                         $$ = $1;
268                 }
269                 | expr '+' expr { 
270                         $$ = $1 + $3;
271                 }
272                 | expr '-' expr { 
273                         $$ = $1 - $3;
274                 }
275                 | expr '*' expr { 
276                         $$ = $1 * $3;
277                 }
278                 | expr '/' expr { 
279                         $$ = $1 / $3;
280                 }
281 /* leave this out until we find why it makes BSD yacc dump core -larne
282                 | '-' expr  %prec NEG {
283                         $$ = -$2;
284                 } */
285                 | '(' expr ')' {
286                         $$ = $2;
287                 }
288                 ;
289
290 jupeblock: JUPE '{' jupeitems '}' ';' ;
291 jupeitems: jupeitem jupeitems | jupeitem;
292 jupeitem: jupenick;
293 jupenick: NICK '=' QSTRING ';'
294 {
295   addNickJupes($3);
296   MyFree($3);
297 };
298
299 generalblock: GENERAL
300 {
301     /* Zero out the vhost addresses, in case they were removed. */
302     memset(&VirtualHost_v4.addr, 0, sizeof(VirtualHost_v4.addr));
303     memset(&VirtualHost_v6.addr, 0, sizeof(VirtualHost_v6.addr));
304 } '{' generalitems '}' ';' {
305   if (localConf.name == NULL)
306     parse_error("Your General block must contain a name.");
307   if (localConf.numeric == 0)
308     parse_error("Your General block must contain a numeric (between 1 and 4095).");
309 };
310 generalitems: generalitem generalitems | generalitem;
311 generalitem: generalnumeric | generalname | generalvhost | generaldesc
312   | generaldnsvhost | generaldnsserver;
313
314 generalnumeric: NUMERIC '=' NUMBER ';'
315 {
316   if (localConf.numeric == 0)
317     localConf.numeric = $3;
318   else if (localConf.numeric != $3)
319     parse_error("Redefinition of server numeric %i (%i)", $3,
320                 localConf.numeric);
321 };
322
323 generalname: NAME '=' QSTRING ';'
324 {
325   if (localConf.name == NULL)
326     localConf.name = $3;
327   else {
328     if (strcmp(localConf.name, $3))
329       parse_error("Redefinition of server name %s (%s)", $3,
330                   localConf.name);
331     MyFree($3);
332   }
333 };
334
335 generaldesc: DESCRIPTION '=' QSTRING ';'
336 {
337   MyFree(localConf.description);
338   localConf.description = $3;
339   ircd_strncpy(cli_info(&me), $3, REALLEN);
340 };
341
342 generalvhost: VHOST '=' QSTRING ';'
343 {
344   struct irc_in_addr addr;
345   char *vhost = $3;
346
347   if (!strcmp(vhost, "*")) {
348     /* This traditionally meant bind to all interfaces and connect
349      * from the default. */
350   } else if (!ircd_aton(&addr, vhost))
351     parse_error("Invalid virtual host '%s'.", vhost);
352   else if (irc_in_addr_is_ipv4(&addr))
353     memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
354   else
355     memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
356   MyFree(vhost);
357 };
358
359 generaldnsvhost: DNS VHOST '=' address_family QSTRING ';'
360 {
361   struct irc_in_addr addr;
362   int families = $4;
363   char *vhost = $5;
364
365   if (!strcmp(vhost, "*")) {
366     /* Let the operating system assign the default. */
367   } else if (!ircd_aton(&addr, vhost))
368     parse_error("Invalid DNS virtual host '%s'.", vhost);
369   else
370   {
371     if ((families & USE_IPV4)
372         || (!families && irc_in_addr_is_ipv4(&addr)))
373       memcpy(&VirtualHost_dns_v4.addr, &addr, sizeof(addr));
374     if ((families & USE_IPV6)
375         || (!families && !irc_in_addr_is_ipv4(&addr)))
376       memcpy(&VirtualHost_dns_v6.addr, &addr, sizeof(addr));
377   }
378   MyFree(vhost);
379 };
380
381 generaldnsserver: DNS SERVER '=' QSTRING ';'
382 {
383   char *server = $4;
384
385   add_nameserver(server);
386   MyFree(server);
387 };
388
389 adminblock: ADMIN
390 {
391   MyFree(localConf.location1);
392   MyFree(localConf.location2);
393   MyFree(localConf.contact);
394   localConf.location1 = localConf.location2 = localConf.contact = NULL;
395 }
396 '{' adminitems '}' ';'
397 {
398   if (localConf.location1 == NULL)
399     DupString(localConf.location1, "");
400   if (localConf.location2 == NULL)
401     DupString(localConf.location2, "");
402   if (localConf.contact == NULL)
403     DupString(localConf.contact, "");
404 };
405 adminitems: adminitems adminitem | adminitem;
406 adminitem: adminlocation | admincontact;
407 adminlocation: LOCATION '=' QSTRING ';'
408 {
409   if (localConf.location1 == NULL)
410     localConf.location1 = $3;
411   else if (localConf.location2 == NULL)
412     localConf.location2 = $3;
413   else /* Otherwise just drop it. -A1kmm */
414     MyFree($3);
415 };
416 admincontact: CONTACT '=' QSTRING ';'
417 {
418  MyFree(localConf.contact);
419  localConf.contact = $3;
420 };
421
422 classblock: CLASS {
423   tping = 90;
424   maxchans = 0;
425 } '{' classitems '}' ';'
426 {
427   if (name != NULL)
428   {
429     struct ConnectionClass *c_class;
430     add_class(name, tping, tconn, maxlinks, sendq, maxchans);
431     c_class = find_class(name);
432     MyFree(c_class->default_umode);
433     c_class->default_umode = pass;
434     memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
435     memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
436   }
437   else {
438    parse_error("Missing name in class block");
439   }
440   name = NULL;
441   pass = NULL;
442   tconn = 0;
443   maxlinks = 0;
444   sendq = 0;
445   memset(&privs, 0, sizeof(privs));
446   memset(&privs_dirty, 0, sizeof(privs_dirty));
447 };
448 classitems: classitem classitems | classitem;
449 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
450            classsendq | classusermode | priv | maxchans;
451 classname: NAME '=' QSTRING ';'
452 {
453   MyFree(name);
454   name = $3;
455 };
456 maxchans: MAXCHANS '=' expr ';'
457 {
458   maxchans = $3;
459 };
460 classpingfreq: PINGFREQ '=' timespec ';'
461 {
462   tping = $3;
463 };
464 classconnfreq: CONNECTFREQ '=' timespec ';'
465 {
466   tconn = $3;
467 };
468 classmaxlinks: MAXLINKS '=' expr ';'
469 {
470   maxlinks = $3;
471 };
472 classsendq: SENDQ '=' sizespec ';'
473 {
474   sendq = $3;
475 };
476 classusermode: USERMODE '=' QSTRING ';'
477 {
478   MyFree(pass);
479   pass = $3;
480 };
481
482 connectblock: CONNECT
483 {
484  flags = CONF_AUTOCONNECT;
485 } '{' connectitems '}' ';'
486 {
487  struct ConfItem *aconf = NULL;
488  if (name == NULL)
489   parse_error("Missing name in connect block");
490  else if (pass == NULL)
491   parse_error("Missing password in connect block");
492  else if (strlen(pass) > PASSWDLEN)
493   parse_error("Password too long in connect block");
494  else if (host == NULL)
495   parse_error("Missing host in connect block");
496  else if (strchr(host, '*') || strchr(host, '?'))
497   parse_error("Invalid host '%s' in connect block", host);
498  else if (c_class == NULL)
499   parse_error("Missing or non-existent class in connect block");
500  else {
501    aconf = make_conf(CONF_SERVER);
502    aconf->name = name;
503    aconf->origin_name = origin;
504    aconf->passwd = pass;
505    aconf->conn_class = c_class;
506    aconf->address.port = port;
507    aconf->host = host;
508    /* If the user specified a hub allowance, but not maximum links,
509     * allow an effectively unlimited number of hops.
510     */
511    aconf->maximum = (hub_limit != NULL && maxlinks == 0) ? 65535 : maxlinks;
512    aconf->hub_limit = hub_limit;
513    aconf->flags = flags;
514    lookup_confhost(aconf);
515  }
516  if (!aconf) {
517    MyFree(name);
518    MyFree(pass);
519    MyFree(host);
520    MyFree(origin);
521    MyFree(hub_limit);
522  }
523  name = pass = host = origin = hub_limit = NULL;
524  c_class = NULL;
525  port = flags = maxlinks = 0;
526 };
527 connectitems: connectitem connectitems | connectitem;
528 connectitem: connectname | connectpass | connectclass | connecthost
529               | connectport | connectvhost | connectleaf | connecthub
530               | connecthublimit | connectmaxhops | connectauto | connectsecure;
531 connectname: NAME '=' QSTRING ';'
532 {
533  MyFree(name);
534  name = $3;
535 };
536 connectpass: PASS '=' QSTRING ';'
537 {
538  MyFree(pass);
539  pass = $3;
540 };
541 connectclass: CLASS '=' QSTRING ';'
542 {
543  c_class = find_class($3);
544  if (!c_class)
545   parse_error("No such connection class '%s' for Connect block", $3);
546  MyFree($3);
547 };
548 connecthost: HOST '=' QSTRING ';'
549 {
550  MyFree(host);
551  host = $3;
552 };
553 connectport: PORT '=' NUMBER ';'
554 {
555  port = $3;
556 };
557 connectvhost: VHOST '=' QSTRING ';'
558 {
559  MyFree(origin);
560  origin = $3;
561 };
562 connectleaf: LEAF ';'
563 {
564  maxlinks = 0;
565 };
566 connecthub: HUB ';'
567 {
568  MyFree(hub_limit);
569  DupString(hub_limit, "*");
570 };
571 connecthublimit: HUB '=' QSTRING ';'
572 {
573  MyFree(hub_limit);
574  hub_limit = $3;
575 };
576 connectmaxhops: MAXHOPS '=' expr ';'
577 {
578   maxlinks = $3;
579 };
580 connectauto: AUTOCONNECT '=' YES ';' { flags |= CONF_AUTOCONNECT; }
581  | AUTOCONNECT '=' NO ';' { flags &= ~CONF_AUTOCONNECT; };
582 connectsecure: SECURE '=' YES ';' { flags |= CONF_SECURE; }
583  | SECURE '=' NO ';' { flags &= ~CONF_SECURE; };
584
585 uworldblock: UWORLD '{' uworlditems '}' ';';
586 uworlditems: uworlditem uworlditems | uworlditem;
587 uworlditem: uworldname;
588 uworldname: NAME '=' QSTRING ';'
589 {
590   make_conf(CONF_UWORLD)->host = $3;
591 };
592
593 operblock: OPER '{' operitems '}' ';'
594 {
595   struct ConfItem *aconf = NULL;
596   struct SLink *link;
597
598   if (name == NULL)
599     parse_error("Missing name in operator block");
600   else if (pass == NULL)
601     parse_error("Missing password in operator block");
602   /* Do not check password length because it may be crypted. */
603   else if (hosts == NULL)
604     parse_error("Missing host(s) in operator block");
605   else if (c_class == NULL)
606     parse_error("Invalid or missing class in operator block");
607   else if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
608            && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
609     parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
610   else for (link = hosts; link != NULL; link = link->next) {
611     aconf = make_conf(CONF_OPERATOR);
612     DupString(aconf->name, name);
613     DupString(aconf->passwd, pass);
614     conf_parse_userhost(aconf, link->value.cp);
615     aconf->conn_class = c_class;
616     memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
617     memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
618   }
619   MyFree(name);
620   MyFree(pass);
621   free_slist(&hosts);
622   name = pass = NULL;
623   c_class = NULL;
624   memset(&privs, 0, sizeof(privs));
625   memset(&privs_dirty, 0, sizeof(privs_dirty));
626 };
627 operitems: operitem | operitems operitem;
628 operitem: opername | operpass | operhost | operclass | priv;
629 opername: NAME '=' QSTRING ';'
630 {
631   MyFree(name);
632   name = $3;
633 };
634 operpass: PASS '=' QSTRING ';'
635 {
636   MyFree(pass);
637   pass = $3;
638 };
639 operhost: HOST '=' QSTRING ';'
640 {
641  struct SLink *link;
642  link = make_link();
643  if (!strchr($3, '@'))
644  {
645    int uh_len;
646    link->value.cp = (char*) MyMalloc((uh_len = strlen($3)+3));
647    ircd_snprintf(0, link->value.cp, uh_len, "*@%s", $3);
648  }
649  else
650    DupString(link->value.cp, $3);
651  MyFree($3);
652  link->next = hosts;
653  hosts = link;
654 };
655 operclass: CLASS '=' QSTRING ';'
656 {
657  c_class = find_class($3);
658  if (!c_class)
659   parse_error("No such connection class '%s' for Operator block", $3);
660  MyFree($3);
661 };
662
663 priv: privtype '=' yesorno ';'
664 {
665   FlagSet(&privs_dirty, $1);
666   if (($3 == 1) ^ invert)
667     FlagSet(&privs, $1);
668   else
669     FlagClr(&privs, $1);
670   invert = 0;
671 };
672
673 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
674           TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
675           TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
676           TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
677           TPRIV_SEE_IDLETIME { $$ = PRIV_SEE_IDLETIME; } |
678           TPRIV_HIDE_IDLETIME { $$ = PRIV_HIDE_IDLETIME; } |
679           TPRIV_UMODE_NOCHAN { $$ = PRIV_UMODE_NOCHAN; } |
680           TPRIV_UMODE_NOIDLE { $$ = PRIV_UMODE_NOIDLE; } |
681           TPRIV_UMODE_CHSERV { $$ = PRIV_UMODE_CHSERV; } |
682           TPRIV_UMODE_XTRAOP { $$ = PRIV_UMODE_XTRAOP; } |
683           TPRIV_UMODE_NETSERV { $$ = PRIV_UMODE_NETSERV; } |
684           TPRIV_FLOOD { $$ = PRIV_FLOOD; } |
685           TPRIV_HALFFLOOD { $$ = PRIV_HALFFLOOD; } |
686           TPRIV_UNLIMITED_TARGET { $$ = PRIV_UNLIMITED_TARGET; } |
687           TPRIV_UMODE_OVERRIDECC { $$ = PRIV_UMODE_OVERRIDECC; } |
688           KILL { $$ = PRIV_KILL; } |
689           TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
690           TPRIV_REHASH { $$ = PRIV_REHASH; } |
691           TPRIV_RESTART { $$ = PRIV_RESTART; } |
692           TPRIV_DIE { $$ = PRIV_DIE; } |
693           TPRIV_GLINE { $$ = PRIV_GLINE; } |
694           TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
695           JUPE { $$ = PRIV_JUPE; } |
696           TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
697           TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
698           TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
699           TPRIV_SET { $$ = PRIV_SET; } |
700           TPRIV_WHOX { $$ = PRIV_WHOX; } |
701           TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
702           TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
703           TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
704           TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
705           TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
706           TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
707           TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
708           TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
709           TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
710           TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
711           TPRIV_LIST_CHAN { $$ = PRIV_LIST_CHAN; } |
712           LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
713           TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
714           TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
715           TPRIV_NOAMSG_OVERRIDE { $$ = PRIV_NOAMSG_OVERRIDE; } |
716           TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
717
718 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
719
720 /* not a recursive definition because some pedant will just come along
721  * and whine that the parser accepts "ipv4 ipv4 ipv4 ipv4"
722  */
723 address_family:
724                { $$ = 0; }
725     | TOK_IPV4 { $$ = USE_IPV4; }
726     | TOK_IPV6 { $$ = USE_IPV6; }
727     | TOK_IPV4 TOK_IPV6 { $$ = USE_IPV4 | USE_IPV6; }
728     | TOK_IPV6 TOK_IPV4 { $$ = USE_IPV6 | USE_IPV4; }
729     ;
730
731 /* The port block... */
732 portblock: PORT '{' portitems '}' ';' {
733   struct ListenerFlags flags_here;
734   struct SLink *link;
735   if (hosts == NULL) {
736     struct SLink *link;
737     link = make_link();
738     DupString(link->value.cp, "*");
739     link->flags = 0;
740     link->next = hosts;
741     hosts = link;
742   }
743   for (link = hosts; link != NULL; link = link->next) {
744     memcpy(&flags_here, &listen_flags, sizeof(&flags_here));
745     switch (link->flags & (USE_IPV4 | USE_IPV6)) {
746     case USE_IPV4:
747       FlagSet(&flags_here, LISTEN_IPV4);
748       break;
749     case USE_IPV6:
750       FlagSet(&flags_here, LISTEN_IPV6);
751       break;
752     default: /* 0 or USE_IPV4|USE_IPV6 */
753       FlagSet(&flags_here, LISTEN_IPV4);
754       FlagSet(&flags_here, LISTEN_IPV6);
755       break;
756     }
757     if (link->flags & 65535)
758       port = link->flags & 65535;
759     add_listener(port, link->value.cp, pass, &flags_here);
760   }
761   free_slist(&hosts);
762   MyFree(pass);
763   memset(&listen_flags, 0, sizeof(listen_flags));
764   pass = NULL;
765   port = 0;
766 };
767 portitems: portitem portitems | portitem;
768 portitem: portnumber | portvhost | portvhostnumber | portmask | portserver | porthidden | portsecure;
769 portnumber: PORT '=' address_family NUMBER ';'
770 {
771   if ($4 < 1 || $4 > 65535) {
772     parse_error("Port %d is out of range", port);
773   } else {
774     port = $3 | $4;
775     if (hosts && (0 == (hosts->flags & 65535)))
776       hosts->flags = (hosts->flags & ~65535) | port;
777   }
778 };
779
780 portvhost: VHOST '=' address_family QSTRING ';'
781 {
782   struct SLink *link;
783   link = make_link();
784   link->value.cp = $4;
785   link->flags = $3 | port;
786   link->next = hosts;
787   hosts = link;
788 };
789
790 portvhostnumber: VHOST '=' address_family QSTRING NUMBER ';'
791 {
792   if ($5 < 1 || $5 > 65535) {
793     parse_error("Port %d is out of range", port);
794   } else {
795     struct SLink *link;
796     link = make_link();
797     link->value.cp = $4;
798     link->flags = $3 | $5;
799     link->next = hosts;
800     hosts = link;
801   }
802 };
803
804 portmask: MASK '=' QSTRING ';'
805 {
806   MyFree(pass);
807   pass = $3;
808 };
809
810 portserver: SERVER '=' YES ';'
811 {
812   FlagSet(&listen_flags, LISTEN_SERVER);
813 } | SERVER '=' NO ';'
814 {
815   FlagClr(&listen_flags, LISTEN_SERVER);
816 };
817
818 porthidden: HIDDEN '=' YES ';'
819 {
820   FlagSet(&listen_flags, LISTEN_HIDDEN);
821 } | HIDDEN '=' NO ';'
822 {
823   FlagClr(&listen_flags, LISTEN_HIDDEN);
824 };
825
826 portsecure: SECURE '=' YES ';'
827 {
828   FlagSet(&listen_flags, LISTEN_SSL);
829 } | SECURE '=' NO ';'
830 {
831   FlagClr(&listen_flags, LISTEN_SSL);
832 };
833
834 clientblock: CLIENT
835 {
836   maxlinks = 65535;
837   port = 0;
838 }
839 '{' clientitems '}' ';'
840 {
841   struct ConfItem *aconf = 0;
842   struct irc_in_addr addr;
843   unsigned char addrbits = 0;
844
845   if (!c_class)
846     parse_error("Invalid or missing class in Client block");
847   else if (pass && strlen(pass) > PASSWDLEN)
848     parse_error("Password too long in connect block");
849   else if (ip && !ipmask_parse(ip, &addr, &addrbits))
850     parse_error("Invalid IP address %s in Client block", ip);
851   else {
852     aconf = make_conf(CONF_CLIENT);
853     aconf->username = username;
854     aconf->host = host;
855     if (ip)
856       memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
857     else
858       memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
859     aconf->address.port = port;
860     aconf->addrbits = addrbits;
861     aconf->name = ip;
862     aconf->conn_class = c_class;
863     aconf->maximum = maxlinks;
864     aconf->passwd = pass;
865   }
866   if (!aconf) {
867     MyFree(username);
868     MyFree(host);
869     MyFree(ip);
870     MyFree(pass);
871   }
872   host = NULL;
873   username = NULL;
874   c_class = NULL;
875   maxlinks = 0;
876   ip = NULL;
877   pass = NULL;
878   port = 0;
879 };
880 clientitems: clientitem clientitems | clientitem;
881 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | clientport;
882 clienthost: HOST '=' QSTRING ';'
883 {
884   char *sep = strchr($3, '@');
885   MyFree(host);
886   if (sep) {
887     *sep++ = '\0';
888     MyFree(username);
889     DupString(host, sep);
890     username = $3;
891   } else {
892     host = $3;
893   }
894 };
895 clientip: IP '=' QSTRING ';'
896 {
897   char *sep;
898   sep = strchr($3, '@');
899   MyFree(ip);
900   if (sep) {
901     *sep++ = '\0';
902     MyFree(username);
903     DupString(ip, sep);
904     username = $3;
905   } else {
906     ip = $3;
907   }
908 };
909 clientusername: USERNAME '=' QSTRING ';'
910 {
911   MyFree(username);
912   username = $3;
913 };
914 clientclass: CLASS '=' QSTRING ';'
915 {
916   c_class = find_class($3);
917   if (!c_class)
918     parse_error("No such connection class '%s' for Client block", $3);
919   MyFree($3);
920 };
921 clientpass: PASS '=' QSTRING ';'
922 {
923   MyFree(pass);
924   pass = $3;
925 };
926 clientmaxlinks: MAXLINKS '=' expr ';'
927 {
928   maxlinks = $3;
929 };
930 clientport: PORT '=' expr ';'
931 {
932   port = $3;
933 };
934
935 killblock: KILL
936 {
937   dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
938 } '{' killitems '}' ';'
939 {
940   if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
941     dconf->next = denyConfList;
942     denyConfList = dconf;
943   }
944   else
945   {
946     MyFree(dconf->usermask);
947     MyFree(dconf->hostmask);
948     MyFree(dconf->realmask);
949     MyFree(dconf->message);
950     MyFree(dconf);
951     parse_error("Kill block must match on at least one of username, host or realname");
952   }
953   dconf = NULL;
954 };
955 killitems: killitem killitems | killitem;
956 killitem: killuhost | killreal | killusername | killreasonfile | killreason;
957 killuhost: HOST '=' QSTRING ';'
958 {
959   char *h;
960   MyFree(dconf->hostmask);
961   MyFree(dconf->usermask);
962   if ((h = strchr($3, '@')) == NULL)
963   {
964     DupString(dconf->usermask, "*");
965     dconf->hostmask = $3;
966   }
967   else
968   {
969     *h++ = '\0';
970     DupString(dconf->hostmask, h);
971     dconf->usermask = $3;
972   }
973   ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
974 };
975
976 killusername: USERNAME '=' QSTRING ';'
977 {
978   MyFree(dconf->usermask);
979   dconf->usermask = $3;
980 };
981
982 killreal: REAL '=' QSTRING ';'
983 {
984  MyFree(dconf->realmask);
985  dconf->realmask = $3;
986 };
987
988 killreason: REASON '=' QSTRING ';'
989 {
990  dconf->flags &= ~DENY_FLAGS_FILE;
991  MyFree(dconf->message);
992  dconf->message = $3;
993 };
994
995 killreasonfile: TFILE '=' QSTRING ';'
996 {
997  dconf->flags |= DENY_FLAGS_FILE;
998  MyFree(dconf->message);
999  dconf->message = $3;
1000 };
1001
1002 cruleblock: CRULE
1003 {
1004   tconn = CRULE_AUTO;
1005 } '{' cruleitems '}' ';'
1006 {
1007   struct CRuleNode *node = NULL;
1008   struct SLink *link;
1009
1010   if (hosts == NULL)
1011     parse_error("Missing server(s) in crule block");
1012   else if (pass == NULL)
1013     parse_error("Missing rule in crule block");
1014   else if ((node = crule_parse(pass)) == NULL)
1015     parse_error("Invalid rule '%s' in crule block", pass);
1016   else for (link = hosts; link != NULL; link = link->next)
1017   {
1018     struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
1019     if (node == NULL)
1020       node = crule_parse(pass);
1021     DupString(p->hostmask, link->value.cp);
1022     DupString(p->rule, pass);
1023     p->type = tconn;
1024     p->node = node;
1025     node = NULL;
1026     p->next = cruleConfList;
1027     cruleConfList = p;
1028   }
1029   free_slist(&hosts);
1030   MyFree(pass);
1031   pass = NULL;
1032   tconn = 0;
1033 };
1034
1035 cruleitems: cruleitem cruleitems | cruleitem;
1036 cruleitem: cruleserver | crulerule | cruleall;
1037
1038 cruleserver: SERVER '=' QSTRING ';'
1039 {
1040   struct SLink *link;
1041   link = make_link();
1042   link->value.cp = $3;
1043   link->next = hosts;
1044   hosts = link;
1045 };
1046
1047 crulerule: RULE '=' QSTRING ';'
1048 {
1049  MyFree(pass);
1050  pass = $3;
1051 };
1052
1053 cruleall: ALL '=' YES ';'
1054 {
1055  tconn = CRULE_ALL;
1056 } | ALL '=' NO ';'
1057 {
1058  tconn = CRULE_AUTO;
1059 };
1060
1061 motdblock: MOTD '{' motditems '}' ';'
1062 {
1063   struct SLink *link;
1064   if (pass != NULL)
1065     for (link = hosts; link != NULL; link = link->next)
1066       motd_add(link->value.cp, pass);
1067   free_slist(&hosts);
1068   MyFree(pass);
1069   pass = NULL;
1070 };
1071
1072 motditems: motditem motditems | motditem;
1073 motditem: motdhost | motdfile;
1074 motdhost: HOST '=' QSTRING ';'
1075 {
1076   struct SLink *link;
1077   link = make_link();
1078   link->value.cp = $3;
1079   link->next = hosts;
1080   hosts = link;
1081 };
1082
1083 motdfile: TFILE '=' QSTRING ';'
1084 {
1085   MyFree(pass);
1086   pass = $3;
1087 };
1088
1089 featuresblock: FEATURES '{' featureitems '}' ';';
1090 featureitems: featureitems featureitem | featureitem;
1091
1092 featureitem: QSTRING
1093 {
1094   stringlist[0] = $1;
1095   stringno = 1;
1096 } '=' stringlist ';' {
1097   unsigned int ii;
1098   feature_set(NULL, (const char * const *)stringlist, stringno);
1099   for (ii = 0; ii < stringno; ++ii)
1100     MyFree(stringlist[ii]);
1101 };
1102
1103 stringlist: stringlist extrastring | extrastring;
1104 extrastring: QSTRING
1105 {
1106   if (stringno < MAX_STRINGS)
1107     stringlist[stringno++] = $1;
1108   else
1109     MyFree($1);
1110 };
1111
1112 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
1113 quarantineitems: quarantineitems quarantineitem | quarantineitem;
1114 quarantineitem: QSTRING '=' QSTRING ';'
1115 {
1116   struct qline *qconf = MyCalloc(1, sizeof(*qconf));
1117   qconf->chname = $1;
1118   qconf->reason = $3;
1119   qconf->next = GlobalQuarantineList;
1120   GlobalQuarantineList = qconf;
1121 };
1122
1123 pseudoblock: PSEUDO QSTRING '{'
1124 {
1125   smap = MyCalloc(1, sizeof(struct s_map));
1126   smap->command = $2;
1127 }
1128 pseudoitems '}' ';'
1129 {
1130   int valid = 0;
1131
1132   if (!smap->name)
1133     parse_error("Missing name in pseudo %s block", smap->command);
1134   else if (!smap->services)
1135     parse_error("Missing nick in pseudo %s block", smap->command);
1136   else if (!strIsAlpha(smap->command))
1137     parse_error("Pseudo command %s invalid: must all be letters", smap->command);
1138   else
1139     valid = 1;
1140   if (valid && register_mapping(smap))
1141   {
1142     smap->next = GlobalServiceMapList;
1143     GlobalServiceMapList = smap;
1144   }
1145   else
1146   {
1147     free_mapping(smap);
1148   }
1149   smap = NULL;
1150 };
1151
1152 pseudoitems: pseudoitem pseudoitems | pseudoitem;
1153 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags;
1154 pseudoname: NAME '=' QSTRING ';'
1155 {
1156   MyFree(smap->name);
1157   smap->name = $3;
1158 };
1159 pseudoprepend: PREPEND '=' QSTRING ';'
1160 {
1161   MyFree(smap->prepend);
1162   smap->prepend = $3;
1163 };
1164 pseudonick: NICK '=' QSTRING ';'
1165 {
1166   char *sep = strchr($3, '@');
1167
1168   if (sep != NULL) {
1169     size_t slen = strlen($3);
1170     struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
1171     memcpy(nh->nick, $3, slen + 1);
1172     nh->nicklen = sep - $3;
1173     nh->next = smap->services;
1174     smap->services = nh;
1175   }
1176   MyFree($3);
1177 };
1178 pseudoflags: FAST ';'
1179 {
1180   smap->flags |= SMAP_FAST;
1181 };
1182
1183 iauthblock: IAUTH '{' iauthitems '}' ';'
1184 {
1185   auth_spawn(stringno, stringlist, iauth_required);
1186   while (stringno > 0)
1187   {
1188     --stringno;
1189     MyFree(stringlist[stringno]);
1190   }
1191   iauth_required = 0;
1192 };
1193
1194 iauthitems: iauthitem iauthitems | iauthitem;
1195 iauthitem: iauthprogram | iauthrequired;
1196 iauthprogram: PROGRAM '='
1197 {
1198   while (stringno > 0)
1199   {
1200     --stringno;
1201     MyFree(stringlist[stringno]);
1202   }
1203 } stringlist ';';
1204
1205 iauthrequired: REQUIRED '=' yesorno ';'
1206 {
1207   iauth_required = $3;
1208 }
1209
1210 forwardsblock: FORWARDS {
1211   unsigned int ii;
1212   for(ii = 0; ii < 256; ++ii) {
1213     MyFree(GlobalForwards[ii]);
1214   }
1215 } '{' forwarditems '}' ';';
1216 forwarditems: forwarditems forwarditem | forwarditem;
1217 forwarditem: QSTRING '=' QSTRING ';'
1218 {
1219   unsigned char ch = $1[0];
1220   MyFree(GlobalForwards[ch]);
1221   GlobalForwards[ch] = $3;
1222   MyFree($1);
1223 };
1224
1225 webircblock: WEBIRC {
1226     /* If we read a new webirc block, we create a new block. */
1227     webirc = webirc_block();
1228 } '{' webircitems '}' ';' {
1229     /* check for integrity */
1230     if(!webirc->name[0]) {
1231         parse_error("Every WebIRC block needs at least a name entry.");
1232         webirc_list_clear(webirc);
1233         MyFree(webirc);
1234         webirc = NULL;
1235     }
1236     else {
1237         webirc_establish(webirc);
1238         webirc = NULL;
1239     }
1240 };
1241 webircitems: webircitems webircitem | webircitem;
1242 webircitem: webircpass | webirchost | webircnhost | webircspoof | webircnspoof | webircname;
1243 webircpass: PASS '=' QSTRING ';' {
1244     unsigned int len;
1245     if((len = strlen($3)) > 0)
1246         webirc_set(webirc, WEBIRC_PASS, $3, len, 0);
1247     MyFree($3);
1248 };
1249 webirchost: HOST '=' QSTRING ';' {
1250     unsigned int len;
1251     if((len = strlen($3)) > 0)
1252         webirc_set(webirc, WEBIRC_HOST, $3, len, 0);
1253     MyFree($3);
1254 };
1255 webircnhost: HOST '!' '=' QSTRING ';' {
1256     unsigned int len;
1257     if((len = strlen($4)) > 0)
1258         webirc_set(webirc, WEBIRC_HOST, $4, len, 1);
1259     MyFree($4);
1260 };
1261 webircspoof: SPOOF '=' QSTRING ';' {
1262     unsigned int len;
1263     if((len = strlen($3)) > 0)
1264         webirc_set(webirc, WEBIRC_SPOOF, $3, len, 0);
1265     MyFree($3);
1266 };
1267 webircnspoof: SPOOF '!' '=' QSTRING ';' {
1268     unsigned int len;
1269     if((len = strlen($4)) > 0)
1270         webirc_set(webirc, WEBIRC_SPOOF, $4, len, 1);
1271     MyFree($4);
1272 };
1273 webircname: NAME '=' QSTRING ';' {
1274     unsigned int len;
1275     if(webirc->name[0]) {
1276         MyFree($3);
1277         parse_error("Only one name entry per WebIRC is allowed.");
1278     }
1279     else if((len = strlen($3)) > NICKLEN) {
1280         MyFree($3);
1281         parse_error("WebIRC block name length is limited to NICKLEN.");
1282     }
1283     else {
1284         if((len = strlen($3)) > 0) {
1285             strcpy(webirc->name, $3);
1286             webirc->name[len] = '\0';
1287         }
1288         MyFree($3);
1289     }
1290 };
1291
1292 sslblock: SSL '{' sslitems '}' ';' ;
1293 sslitems: sslitems sslitem | sslitem;
1294 sslitem: sslcert | sslcacert;
1295 sslcert: CERT '=' QSTRING ';' {
1296     ssl_setcert($3);
1297     MyFree($3);
1298 };
1299 sslcacert: CACERT '=' QSTRING ';' {
1300     ssl_addtrust($3);
1301     MyFree($3);
1302 };
1303