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