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.
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.
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,
20 * $Id: ircd_parser.y,v 1.56.2.2 2005/12/13 23:12:41 entrope Exp $
29 #include "ircd_features.h"
34 #include "ircd_alloc.h"
35 #include "ircd_auth.h"
36 #include "ircd_chattr.h"
38 #include "ircd_reply.h"
39 #include "ircd_snprintf.h"
40 #include "ircd_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;
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;
79 struct Privs privs_dirty;
81 static void parse_error(char *pattern,...) {
82 static char error_buffer[1024];
85 ircd_vsnprintf(NULL, error_buffer, sizeof(error_buffer), pattern, vl);
87 yyerror(error_buffer);
159 /* and now a lot of privileges... */
160 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
161 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
162 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
163 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
164 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
165 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
166 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE TPRIV_APASS_OPMODE
167 /* and some types... */
169 %type <num> timespec timefactor factoredtimes factoredtime
170 %type <num> expr yesorno privtype
180 /* Blocks in the config file... */
181 blocks: blocks block | block;
182 block: adminblock | generalblock | classblock | connectblock |
183 uworldblock | operblock | portblock | jupeblock | clientblock |
184 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
185 pseudoblock | iauthblock | error ';';
187 /* The timespec, sizespec and expr was ripped straight from
189 timespec: expr | factoredtimes;
191 factoredtimes: factoredtimes factoredtime
196 factoredtime: expr timefactor
201 timefactor: SECONDS { $$ = 1; }
202 | MINUTES { $$ = 60; }
203 | HOURS { $$ = 60 * 60; }
204 | DAYS { $$ = 60 * 60 * 24; }
205 | WEEKS { $$ = 60 * 60 * 24 * 7; }
206 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
207 | YEARS { $$ = 60 * 60 * 24 * 365; }
208 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
221 $$ = $1 * 1024 * 1024;
224 $$ = $1 * 1024 * 1024 * 1024;
227 $$ = $1 * 1024 * 1024 * 1024;
231 /* this is an arithmetic expression */
248 /* leave this out until we find why it makes BSD yacc dump core -larne
249 | '-' expr %prec NEG {
257 jupeblock: JUPE '{' jupeitems '}' ';' ;
258 jupeitems: jupeitem jupeitems | jupeitem;
260 jupenick: NICK '=' QSTRING ';'
266 generalblock: GENERAL
268 /* Zero out the vhost addresses, in case they were removed. */
269 memset(&VirtualHost_v4.addr, 0, sizeof(VirtualHost_v4.addr));
270 memset(&VirtualHost_v6.addr, 0, sizeof(VirtualHost_v6.addr));
271 } '{' generalitems '}' ';' {
272 if (localConf.name == NULL)
273 parse_error("Your General block must contain a name.");
274 if (localConf.numeric == 0)
275 parse_error("Your General block must contain a numeric (between 1 and 4095).");
277 generalitems: generalitem generalitems | generalitem;
278 generalitem: generalnumeric | generalname | generalvhost | generaldesc;
279 generalnumeric: NUMERIC '=' NUMBER ';'
281 if (localConf.numeric == 0)
282 localConf.numeric = $3;
283 else if (localConf.numeric != $3)
284 parse_error("Redefinition of server numeric %i (%i)", $3,
288 generalname: NAME '=' QSTRING ';'
290 if (localConf.name == NULL)
293 if (strcmp(localConf.name, $3))
294 parse_error("Redefinition of server name %s (%s)", $3,
300 generaldesc: DESCRIPTION '=' QSTRING ';'
302 MyFree(localConf.description);
303 localConf.description = $3;
304 ircd_strncpy(cli_info(&me), $3, REALLEN);
307 generalvhost: VHOST '=' QSTRING ';'
309 struct irc_in_addr addr;
310 if (!strcmp($3, "*")) {
311 /* This traditionally meant bind to all interfaces and connect
312 * from the default. */
313 } else if (!ircd_aton(&addr, $3))
314 parse_error("Invalid virtual host '%s'.", $3);
315 else if (irc_in_addr_is_ipv4(&addr))
316 memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
318 memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
322 adminblock: ADMIN '{' adminitems '}' ';'
324 if (localConf.location1 == NULL)
325 DupString(localConf.location1, "");
326 if (localConf.location2 == NULL)
327 DupString(localConf.location2, "");
328 if (localConf.contact == NULL)
329 DupString(localConf.contact, "");
331 adminitems: adminitems adminitem | adminitem;
332 adminitem: adminlocation | admincontact;
333 adminlocation: LOCATION '=' QSTRING ';'
335 if (localConf.location1 == NULL)
336 localConf.location1 = $3;
337 else if (localConf.location2 == NULL)
338 localConf.location2 = $3;
339 else /* Otherwise just drop it. -A1kmm */
342 admincontact: CONTACT '=' QSTRING ';'
344 MyFree(localConf.contact);
345 localConf.contact = $3;
350 } '{' classitems '}' ';'
354 struct ConnectionClass *c_class;
355 add_class(name, tping, tconn, maxlinks, sendq);
356 c_class = find_class(name);
357 MyFree(c_class->default_umode);
358 c_class->default_umode = pass;
359 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
360 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
363 parse_error("Missing name in class block");
370 memset(&privs, 0, sizeof(privs));
371 memset(&privs_dirty, 0, sizeof(privs_dirty));
373 classitems: classitem classitems | classitem;
374 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
375 classsendq | classusermode | priv;
376 classname: NAME '=' QSTRING ';'
381 classpingfreq: PINGFREQ '=' timespec ';'
385 classconnfreq: CONNECTFREQ '=' timespec ';'
389 classmaxlinks: MAXLINKS '=' expr ';'
393 classsendq: SENDQ '=' sizespec ';'
397 classusermode: USERMODE '=' QSTRING ';'
403 connectblock: CONNECT
406 flags = CONF_AUTOCONNECT;
407 } '{' connectitems '}' ';'
409 struct ConfItem *aconf = NULL;
411 parse_error("Missing name in connect block");
412 else if (pass == NULL)
413 parse_error("Missing password in connect block");
414 else if (host == NULL)
415 parse_error("Missing host in connect block");
416 else if (strchr(host, '*') || strchr(host, '?'))
417 parse_error("Invalid host '%s' in connect block", host);
418 else if (c_class == NULL)
419 parse_error("Missing or non-existent class in connect block");
421 aconf = make_conf(CONF_SERVER);
423 aconf->origin_name = origin;
424 aconf->passwd = pass;
425 aconf->conn_class = c_class;
426 aconf->address.port = port;
428 aconf->maximum = maxlinks;
429 aconf->hub_limit = hub_limit;
430 aconf->flags = flags;
431 lookup_confhost(aconf);
440 name = pass = host = origin = hub_limit = NULL;
444 connectitems: connectitem connectitems | connectitem;
445 connectitem: connectname | connectpass | connectclass | connecthost
446 | connectport | connectvhost | connectleaf | connecthub
447 | connecthublimit | connectmaxhops | connectauto;
448 connectname: NAME '=' QSTRING ';'
453 connectpass: PASS '=' QSTRING ';'
458 connectclass: CLASS '=' QSTRING ';'
460 c_class = find_class($3);
462 parse_error("No such connection class '%s' for Connect block", $3);
465 connecthost: HOST '=' QSTRING ';'
470 connectport: PORT '=' NUMBER ';'
474 connectvhost: VHOST '=' QSTRING ';'
479 connectleaf: LEAF ';'
486 DupString(hub_limit, "*");
488 connecthublimit: HUB '=' QSTRING ';'
493 connectmaxhops: MAXHOPS '=' expr ';'
497 connectauto: AUTOCONNECT '=' YES ';' { flags |= CONF_AUTOCONNECT; }
498 | AUTOCONNECT '=' NO ';' { flags &= ~CONF_AUTOCONNECT; };
500 uworldblock: UWORLD '{' uworlditems '}' ';';
501 uworlditems: uworlditem uworlditems | uworlditem;
502 uworlditem: uworldname;
503 uworldname: NAME '=' QSTRING ';'
505 make_conf(CONF_UWORLD)->host = $3;
508 operblock: OPER '{' operitems '}' ';'
510 struct ConfItem *aconf = NULL;
512 parse_error("Missing name in operator block");
513 else if (pass == NULL)
514 parse_error("Missing password in operator block");
515 else if (host == NULL)
516 parse_error("Missing host in operator block");
517 else if (c_class == NULL)
518 parse_error("Invalid or missing class in operator block");
520 aconf = make_conf(CONF_OPERATOR);
522 aconf->passwd = pass;
523 conf_parse_userhost(aconf, host);
524 aconf->conn_class = c_class;
525 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
526 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
527 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
528 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
529 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
536 name = pass = host = NULL;
538 memset(&privs, 0, sizeof(privs));
539 memset(&privs_dirty, 0, sizeof(privs_dirty));
541 operitems: operitem | operitems operitem;
542 operitem: opername | operpass | operhost | operclass | priv;
543 opername: NAME '=' QSTRING ';'
548 operpass: PASS '=' QSTRING ';'
553 operhost: HOST '=' QSTRING ';'
556 if (!strchr($3, '@'))
559 host = (char*) MyMalloc((uh_len = strlen($3)+3));
560 ircd_snprintf(0, host, uh_len, "*@%s", $3);
566 operclass: CLASS '=' QSTRING ';'
568 c_class = find_class($3);
570 parse_error("No such connection class '%s' for Operator block", $3);
574 priv: privtype '=' yesorno ';'
576 FlagSet(&privs_dirty, $1);
577 if (($3 == 1) ^ invert)
584 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
585 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
586 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
587 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
588 KILL { $$ = PRIV_KILL; } |
589 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
590 TPRIV_REHASH { $$ = PRIV_REHASH; } |
591 TPRIV_RESTART { $$ = PRIV_RESTART; } |
592 TPRIV_DIE { $$ = PRIV_DIE; } |
593 TPRIV_GLINE { $$ = PRIV_GLINE; } |
594 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
595 JUPE { $$ = PRIV_JUPE; } |
596 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
597 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
598 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
599 TPRIV_SET { $$ = PRIV_SET; } |
600 TPRIV_WHOX { $$ = PRIV_WHOX; } |
601 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
602 TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
603 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
604 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
605 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
606 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
607 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
608 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
609 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
610 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
611 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
612 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
613 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
614 TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
616 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
618 /* The port block... */
619 portblock: PORT '{' portitems '}' ';'
621 if (port > 0 && port <= 0xFFFF)
622 add_listener(port, host, pass, tconn, tping);
624 parse_error("Port %d is out of range", port);
628 port = tconn = tping = 0;
630 portitems: portitem portitems | portitem;
631 portitem: portnumber | portvhost | portmask | portserver | porthidden;
632 portnumber: PORT '=' NUMBER ';'
637 portvhost: VHOST '=' QSTRING ';'
643 portmask: MASK '=' QSTRING ';'
649 portserver: SERVER '=' YES ';'
652 } | SERVER '=' NO ';'
657 porthidden: HIDDEN '=' YES ';'
660 } | HIDDEN '=' NO ';'
670 '{' clientitems '}' ';'
672 struct ConfItem *aconf = 0;
673 struct irc_in_addr addr;
674 unsigned char addrbits = 0;
677 parse_error("Invalid or missing class in Client block");
678 else if (ip && !ipmask_parse(ip, &addr, &addrbits))
679 parse_error("Invalid IP address %s in Client block", ip);
681 aconf = make_conf(CONF_CLIENT);
682 aconf->username = username;
685 memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
687 memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
688 aconf->address.port = port;
689 aconf->addrbits = addrbits;
691 aconf->conn_class = c_class;
692 aconf->maximum = maxlinks;
693 aconf->passwd = pass;
708 clientitems: clientitem clientitems | clientitem;
709 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | clientport;
710 clienthost: HOST '=' QSTRING ';'
712 char *sep = strchr($3, '@');
717 DupString(host, sep);
723 clientip: IP '=' QSTRING ';'
726 sep = strchr($3, '@');
737 clientusername: USERNAME '=' QSTRING ';'
742 clientclass: CLASS '=' QSTRING ';'
744 c_class = find_class($3);
746 parse_error("No such connection class '%s' for Client block", $3);
749 clientpass: PASS '=' QSTRING ';'
754 clientmaxlinks: MAXLINKS '=' expr ';'
758 clientport: PORT '=' expr ';'
765 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
766 } '{' killitems '}' ';'
768 if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
769 dconf->next = denyConfList;
770 denyConfList = dconf;
774 MyFree(dconf->usermask);
775 MyFree(dconf->hostmask);
776 MyFree(dconf->realmask);
777 MyFree(dconf->message);
779 parse_error("Kill block must match on at least one of username, host or realname");
783 killitems: killitem killitems | killitem;
784 killitem: killuhost | killreal | killusername | killreasonfile | killreason;
785 killuhost: HOST '=' QSTRING ';'
788 MyFree(dconf->hostmask);
789 MyFree(dconf->usermask);
790 if ((h = strchr($3, '@')) == NULL)
792 DupString(dconf->usermask, "*");
793 dconf->hostmask = $3;
798 DupString(dconf->hostmask, h);
799 dconf->usermask = $3;
801 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
804 killusername: USERNAME '=' QSTRING ';'
806 MyFree(dconf->usermask);
807 dconf->usermask = $3;
810 killreal: REAL '=' QSTRING ';'
812 MyFree(dconf->realmask);
813 dconf->realmask = $3;
816 killreason: REASON '=' QSTRING ';'
818 dconf->flags &= ~DENY_FLAGS_FILE;
819 MyFree(dconf->message);
823 killreasonfile: TFILE '=' QSTRING ';'
825 dconf->flags |= DENY_FLAGS_FILE;
826 MyFree(dconf->message);
833 } '{' cruleitems '}' ';'
835 struct CRuleNode *node = NULL;
837 parse_error("Missing host in crule block");
838 else if (pass == NULL)
839 parse_error("Missing rule in crule block");
840 else if ((node = crule_parse(pass)) == NULL)
841 parse_error("Invalid rule '%s' in crule block", pass);
844 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
849 p->next = cruleConfList;
861 cruleitems: cruleitem cruleitems | cruleitem;
862 cruleitem: cruleserver | crulerule | cruleall;
864 cruleserver: SERVER '=' QSTRING ';'
871 crulerule: RULE '=' QSTRING ';'
877 cruleall: ALL '=' YES ';'
885 motdblock: MOTD '{' motditems '}' ';'
887 if (host != NULL && pass != NULL)
888 motd_add(host, pass);
894 motditems: motditem motditems | motditem;
895 motditem: motdhost | motdfile;
896 motdhost: HOST '=' QSTRING ';'
901 motdfile: TFILE '=' QSTRING ';'
906 featuresblock: FEATURES '{' featureitems '}' ';';
907 featureitems: featureitems featureitem | featureitem;
913 } '=' stringlist ';';
922 feature_set(NULL, (const char * const *)stringlist, stringno);
923 for (ii = 0; ii < stringno; ++ii)
924 MyFree(stringlist[ii]);
926 posextrastrings: /* empty */ | extrastrings;
927 extrastrings: extrastrings extrastring | extrastring;
930 if (stringno < MAX_STRINGS)
931 stringlist[stringno++] = $1;
936 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
937 quarantineitems: quarantineitems quarantineitem | quarantineitem;
938 quarantineitem: QSTRING '=' QSTRING ';'
940 struct qline *qconf = MyCalloc(1, sizeof(*qconf));
943 qconf->next = GlobalQuarantineList;
944 GlobalQuarantineList = qconf;
947 pseudoblock: PSEUDO QSTRING '{'
949 smap = MyCalloc(1, sizeof(struct s_map));
957 parse_error("Missing name in pseudo %s block", smap->command);
958 else if (!smap->services)
959 parse_error("Missing nick in pseudo %s block", smap->command);
962 if (valid && register_mapping(smap))
964 smap->next = GlobalServiceMapList;
965 GlobalServiceMapList = smap;
974 pseudoitems: pseudoitem pseudoitems | pseudoitem;
975 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags;
976 pseudoname: NAME '=' QSTRING ';'
981 pseudoprepend: PREPEND '=' QSTRING ';'
983 MyFree(smap->prepend);
986 pseudonick: NICK '=' QSTRING ';'
988 char *sep = strchr($3, '@');
991 size_t slen = strlen($3);
992 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
993 memcpy(nh->nick, $3, slen + 1);
994 nh->nicklen = sep - $3;
995 nh->next = smap->services;
1000 pseudoflags: FAST ';'
1002 smap->flags |= SMAP_FAST;
1005 iauthblock: IAUTH '{'
1009 } iauthitems '}' ';'
1012 parse_error("Missing host in iauth block");
1014 parse_error("Missing port in iauth block");
1016 iauth_connect(host, port, pass, tconn, tping);
1020 port = tconn = tping = 0;
1023 iauthitems: iauthitem iauthitems | iauthitem;
1024 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout;
1025 iauthpass: PASS '=' QSTRING ';'
1030 iauthhost: HOST '=' QSTRING ';'
1035 iauthport: PORT '=' NUMBER ';'
1039 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1043 iauthtimeout: TIMEOUT '=' timespec ';'