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_UNLIMIT_FLOOD TPRIV_DISPLAY TPRIV_SEE_OPERS
189 %token TPRIV_WIDE_GLINE TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE
190 %token TPRIV_APASS_OPMODE 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_UNLIMIT_FLOOD { $$ = PRIV_UNLIMIT_FLOOD; } |
716           TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
717           TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
718           TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
719           TPRIV_LIST_CHAN { $$ = PRIV_LIST_CHAN; } |
720           LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
721           TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
722           TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
723           TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
724
725 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
726
727 /* not a recursive definition because some pedant will just come along
728  * and whine that the parser accepts "ipv4 ipv4 ipv4 ipv4"
729  */
730 address_family:
731                { $$ = 0; }
732     | TOK_IPV4 { $$ = USE_IPV4; }
733     | TOK_IPV6 { $$ = USE_IPV6; }
734     | TOK_IPV4 TOK_IPV6 { $$ = USE_IPV4 | USE_IPV6; }
735     | TOK_IPV6 TOK_IPV4 { $$ = USE_IPV6 | USE_IPV4; }
736     ;
737
738 /* The port block... */
739 portblock: PORT '{' portitems '}' ';' {
740   struct ListenerFlags flags_here;
741   struct SLink *link;
742   if (hosts == NULL) {
743     struct SLink *link;
744     link = make_link();
745     DupString(link->value.cp, "*");
746     link->flags = 0;
747     link->next = hosts;
748     hosts = link;
749   }
750   for (link = hosts; link != NULL; link = link->next) {
751     memcpy(&flags_here, &listen_flags, sizeof(flags_here));
752     switch (link->flags & (USE_IPV4 | USE_IPV6)) {
753     case USE_IPV4:
754       FlagSet(&flags_here, LISTEN_IPV4);
755       break;
756     case USE_IPV6:
757       FlagSet(&flags_here, LISTEN_IPV6);
758       break;
759     default: /* 0 or USE_IPV4|USE_IPV6 */
760       FlagSet(&flags_here, LISTEN_IPV4);
761       FlagSet(&flags_here, LISTEN_IPV6);
762       break;
763     }
764     
765     if (link->flags & 65535)
766       port = link->flags & 65535;
767     add_listener(port, link->value.cp, pass, &flags_here);
768   }
769   free_slist(&hosts);
770   MyFree(pass);
771   memset(&listen_flags, 0, sizeof(listen_flags));
772   pass = NULL;
773   port = 0;
774 };
775 portitems: portitem portitems | portitem;
776 portitem: portnumber | portvhost | portvhostnumber | portmask | portserver | portssl | porthidden;
777 portnumber: PORT '=' address_family NUMBER ';'
778 {
779   if ($4 < 1 || $4 > 65535) {
780     parse_error("Port %d is out of range", port);
781   } else {
782     port = $3 | $4;
783     if (hosts && (0 == (hosts->flags & 65535)))
784       hosts->flags = (hosts->flags & ~65535) | port;
785   }
786 };
787
788 portvhost: VHOST '=' address_family QSTRING ';'
789 {
790   struct SLink *link;
791   link = make_link();
792   link->value.cp = $4;
793   link->flags = $3 | port;
794   link->next = hosts;
795   hosts = link;
796 };
797
798 portvhostnumber: VHOST '=' address_family QSTRING NUMBER ';'
799 {
800   if ($5 < 1 || $5 > 65535) {
801     parse_error("Port %d is out of range", port);
802   } else {
803     struct SLink *link;
804     link = make_link();
805     link->value.cp = $4;
806     link->flags = $3 | $5;
807     link->next = hosts;
808     hosts = link;
809   }
810 };
811
812 portmask: MASK '=' QSTRING ';'
813 {
814   MyFree(pass);
815   pass = $3;
816 };
817
818 portserver: SERVER '=' YES ';'
819 {
820   FlagSet(&listen_flags, LISTEN_SERVER);
821 } | SERVER '=' NO ';'
822 {
823   FlagClr(&listen_flags, LISTEN_SERVER);
824 };
825
826 portssl: SSL '=' YES ';'
827 {
828   FlagSet(&listen_flags, LISTEN_SSL);
829 } | SSL '=' NO ';'
830 {
831   FlagClr(&listen_flags, LISTEN_SSL);
832 };
833
834 porthidden: HIDDEN '=' YES ';'
835 {
836   FlagSet(&listen_flags, LISTEN_HIDDEN);
837 } | HIDDEN '=' NO ';'
838 {
839   FlagClr(&listen_flags, LISTEN_HIDDEN);
840 };
841
842 clientblock: CLIENT
843 {
844   maxlinks = 65535;
845   port = 0;
846 }
847 '{' clientitems '}' ';'
848 {
849   struct ConfItem *aconf = 0;
850   struct irc_in_addr addr;
851   unsigned char addrbits = 0;
852
853   if (!c_class)
854     parse_error("Invalid or missing class in Client block");
855   else if (pass && strlen(pass) > PASSWDLEN)
856     parse_error("Password too long in connect block");
857   else if (ip && !ipmask_parse(ip, &addr, &addrbits))
858     parse_error("Invalid IP address %s in Client block", ip);
859   else {
860     aconf = make_conf(CONF_CLIENT);
861     aconf->username = username;
862     aconf->host = host;
863     if (ip)
864       memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
865     else
866       memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
867     aconf->address.port = port;
868     aconf->addrbits = addrbits;
869     aconf->name = ip;
870     aconf->conn_class = c_class;
871     aconf->maximum = maxlinks;
872     aconf->passwd = pass;
873   }
874   if (!aconf) {
875     MyFree(username);
876     MyFree(host);
877     MyFree(ip);
878     MyFree(pass);
879   }
880   host = NULL;
881   username = NULL;
882   c_class = NULL;
883   maxlinks = 0;
884   ip = NULL;
885   pass = NULL;
886   port = 0;
887 };
888 clientitems: clientitem clientitems | clientitem;
889 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | clientport;
890 clienthost: HOST '=' QSTRING ';'
891 {
892   char *sep = strchr($3, '@');
893   MyFree(host);
894   if (sep) {
895     *sep++ = '\0';
896     MyFree(username);
897     DupString(host, sep);
898     username = $3;
899   } else {
900     host = $3;
901   }
902 };
903 clientip: IP '=' QSTRING ';'
904 {
905   char *sep;
906   sep = strchr($3, '@');
907   MyFree(ip);
908   if (sep) {
909     *sep++ = '\0';
910     MyFree(username);
911     DupString(ip, sep);
912     username = $3;
913   } else {
914     ip = $3;
915   }
916 };
917 clientusername: USERNAME '=' QSTRING ';'
918 {
919   MyFree(username);
920   username = $3;
921 };
922 clientclass: CLASS '=' QSTRING ';'
923 {
924   c_class = find_class($3);
925   if (!c_class)
926     parse_error("No such connection class '%s' for Client block", $3);
927   MyFree($3);
928 };
929 clientpass: PASS '=' QSTRING ';'
930 {
931   MyFree(pass);
932   pass = $3;
933 };
934 clientmaxlinks: MAXLINKS '=' expr ';'
935 {
936   maxlinks = $3;
937 };
938 clientport: PORT '=' expr ';'
939 {
940   port = $3;
941 };
942
943 killblock: KILL
944 {
945   dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
946 } '{' killitems '}' ';'
947 {
948   if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
949     dconf->next = denyConfList;
950     denyConfList = dconf;
951   }
952   else
953   {
954     MyFree(dconf->usermask);
955     MyFree(dconf->hostmask);
956     MyFree(dconf->realmask);
957     MyFree(dconf->message);
958     MyFree(dconf);
959     parse_error("Kill block must match on at least one of username, host or realname");
960   }
961   dconf = NULL;
962 };
963 killitems: killitem killitems | killitem;
964 killitem: killuhost | killreal | killusername | killreasonfile | killreason;
965 killuhost: HOST '=' QSTRING ';'
966 {
967   char *h;
968   MyFree(dconf->hostmask);
969   MyFree(dconf->usermask);
970   if ((h = strchr($3, '@')) == NULL)
971   {
972     DupString(dconf->usermask, "*");
973     dconf->hostmask = $3;
974   }
975   else
976   {
977     *h++ = '\0';
978     DupString(dconf->hostmask, h);
979     dconf->usermask = $3;
980   }
981   ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
982 };
983
984 killusername: USERNAME '=' QSTRING ';'
985 {
986   MyFree(dconf->usermask);
987   dconf->usermask = $3;
988 };
989
990 killreal: REAL '=' QSTRING ';'
991 {
992  MyFree(dconf->realmask);
993  dconf->realmask = $3;
994 };
995
996 killreason: REASON '=' QSTRING ';'
997 {
998  dconf->flags &= ~DENY_FLAGS_FILE;
999  MyFree(dconf->message);
1000  dconf->message = $3;
1001 };
1002
1003 killreasonfile: TFILE '=' QSTRING ';'
1004 {
1005  dconf->flags |= DENY_FLAGS_FILE;
1006  MyFree(dconf->message);
1007  dconf->message = $3;
1008 };
1009
1010 cruleblock: CRULE
1011 {
1012   tconn = CRULE_AUTO;
1013 } '{' cruleitems '}' ';'
1014 {
1015   struct CRuleNode *node = NULL;
1016   struct SLink *link;
1017
1018   if (hosts == NULL)
1019     parse_error("Missing server(s) in crule block");
1020   else if (pass == NULL)
1021     parse_error("Missing rule in crule block");
1022   else if ((node = crule_parse(pass)) == NULL)
1023     parse_error("Invalid rule '%s' in crule block", pass);
1024   else for (link = hosts; link != NULL; link = link->next)
1025   {
1026     struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
1027     if (node == NULL)
1028       node = crule_parse(pass);
1029     DupString(p->hostmask, link->value.cp);
1030     DupString(p->rule, pass);
1031     p->type = tconn;
1032     p->node = node;
1033     node = NULL;
1034     p->next = cruleConfList;
1035     cruleConfList = p;
1036   }
1037   free_slist(&hosts);
1038   MyFree(pass);
1039   pass = NULL;
1040   tconn = 0;
1041 };
1042
1043 cruleitems: cruleitem cruleitems | cruleitem;
1044 cruleitem: cruleserver | crulerule | cruleall;
1045
1046 cruleserver: SERVER '=' QSTRING ';'
1047 {
1048   struct SLink *link;
1049   link = make_link();
1050   link->value.cp = $3;
1051   link->next = hosts;
1052   hosts = link;
1053 };
1054
1055 crulerule: RULE '=' QSTRING ';'
1056 {
1057  MyFree(pass);
1058  pass = $3;
1059 };
1060
1061 cruleall: ALL '=' YES ';'
1062 {
1063  tconn = CRULE_ALL;
1064 } | ALL '=' NO ';'
1065 {
1066  tconn = CRULE_AUTO;
1067 };
1068
1069 motdblock: MOTD '{' motditems '}' ';'
1070 {
1071   struct SLink *link;
1072   if (pass != NULL)
1073     for (link = hosts; link != NULL; link = link->next)
1074       motd_add(link->value.cp, pass);
1075   free_slist(&hosts);
1076   MyFree(pass);
1077   pass = NULL;
1078 };
1079
1080 motditems: motditem motditems | motditem;
1081 motditem: motdhost | motdfile;
1082 motdhost: HOST '=' QSTRING ';'
1083 {
1084   struct SLink *link;
1085   link = make_link();
1086   link->value.cp = $3;
1087   link->next = hosts;
1088   hosts = link;
1089 };
1090
1091 motdfile: TFILE '=' QSTRING ';'
1092 {
1093   MyFree(pass);
1094   pass = $3;
1095 };
1096
1097 featuresblock: FEATURES '{' featureitems '}' ';';
1098 featureitems: featureitems featureitem | featureitem;
1099
1100 featureitem: QSTRING
1101 {
1102   stringlist[0] = $1;
1103   stringno = 1;
1104 } '=' stringlist ';' {
1105   unsigned int ii;
1106   feature_set(NULL, (const char * const *)stringlist, stringno);
1107   for (ii = 0; ii < stringno; ++ii)
1108     MyFree(stringlist[ii]);
1109 };
1110
1111 stringlist: stringlist extrastring | extrastring;
1112 extrastring: QSTRING
1113 {
1114   if (stringno < MAX_STRINGS)
1115     stringlist[stringno++] = $1;
1116   else
1117     MyFree($1);
1118 };
1119
1120 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
1121 quarantineitems: quarantineitems quarantineitem | quarantineitem;
1122 quarantineitem: QSTRING '=' QSTRING ';'
1123 {
1124   struct qline *qconf = MyCalloc(1, sizeof(*qconf));
1125   qconf->chname = $1;
1126   qconf->reason = $3;
1127   qconf->next = GlobalQuarantineList;
1128   GlobalQuarantineList = qconf;
1129 };
1130
1131 pseudoblock: PSEUDO QSTRING '{'
1132 {
1133   smap = MyCalloc(1, sizeof(struct s_map));
1134   smap->command = $2;
1135 }
1136 pseudoitems '}' ';'
1137 {
1138   int valid = 0;
1139
1140   if (!smap->name)
1141     parse_error("Missing name in pseudo %s block", smap->command);
1142   else if (!smap->services)
1143     parse_error("Missing nick in pseudo %s block", smap->command);
1144   else if (!strIsAlpha(smap->command))
1145     parse_error("Pseudo command %s invalid: must all be letters", smap->command);
1146   else
1147     valid = 1;
1148   if (valid && register_mapping(smap))
1149   {
1150     smap->next = GlobalServiceMapList;
1151     GlobalServiceMapList = smap;
1152   }
1153   else
1154   {
1155     free_mapping(smap);
1156   }
1157   smap = NULL;
1158 };
1159
1160 pseudoitems: pseudoitem pseudoitems | pseudoitem;
1161 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags;
1162 pseudoname: NAME '=' QSTRING ';'
1163 {
1164   MyFree(smap->name);
1165   smap->name = $3;
1166 };
1167 pseudoprepend: PREPEND '=' QSTRING ';'
1168 {
1169   MyFree(smap->prepend);
1170   smap->prepend = $3;
1171 };
1172 pseudonick: NICK '=' QSTRING ';'
1173 {
1174   char *sep = strchr($3, '@');
1175
1176   if (sep != NULL) {
1177     size_t slen = strlen($3);
1178     struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
1179     memcpy(nh->nick, $3, slen + 1);
1180     nh->nicklen = sep - $3;
1181     nh->next = smap->services;
1182     smap->services = nh;
1183   }
1184   MyFree($3);
1185 };
1186 pseudoflags: FAST ';'
1187 {
1188   smap->flags |= SMAP_FAST;
1189 };
1190
1191 iauthblock: IAUTH '{' iauthitems '}' ';'
1192 {
1193   auth_spawn(stringno, stringlist);
1194   while (stringno > 0)
1195   {
1196     --stringno;
1197     MyFree(stringlist[stringno]);
1198   }
1199 };
1200
1201 iauthitems: iauthitem iauthitems | iauthitem;
1202 iauthitem: iauthprogram;
1203 iauthprogram: PROGRAM '='
1204 {
1205   while (stringno > 0)
1206   {
1207     --stringno;
1208     MyFree(stringlist[stringno]);
1209   }
1210 } stringlist ';';