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