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