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,
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;
259 jupeitem: jupenick | error;
260 jupenick: NICK '=' QSTRING
266 generalblock: GENERAL '{' generalitems '}'
268 if (localConf.name == NULL)
269 parse_error("Your General block must contain a name.");
270 if (localConf.numeric == 0)
271 parse_error("Your General block must contain a numeric (between 1 and 4095).");
273 generalitems: generalitem generalitems | generalitem;
274 generalitem: generalnumeric | generalname | generalvhost | generaldesc | error;
275 generalnumeric: NUMERIC '=' NUMBER ';'
277 if (localConf.numeric == 0)
278 localConf.numeric = $3;
279 else if (localConf.numeric != $3)
280 parse_error("Redefinition of server numeric %i (%i)", $3,
284 generalname: NAME '=' QSTRING ';'
286 if (localConf.name == NULL)
289 if (strcmp(localConf.name, $3))
290 parse_error("Redefinition of server name %s (%s)", $3,
296 generaldesc: DESCRIPTION '=' QSTRING ';'
298 MyFree(localConf.description);
299 localConf.description = $3;
300 ircd_strncpy(cli_info(&me), $3, REALLEN);
303 generalvhost: VHOST '=' QSTRING ';'
305 struct irc_in_addr addr;
306 if (!ircd_aton(&addr, $3))
307 parse_error("Invalid virtual host '%s'.", $3);
308 else if (irc_in_addr_is_ipv4(&addr))
309 memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
311 memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
315 adminblock: ADMIN '{' adminitems '}'
317 if (localConf.location1 == NULL)
318 DupString(localConf.location1, "");
319 if (localConf.location2 == NULL)
320 DupString(localConf.location2, "");
321 if (localConf.contact == NULL)
322 DupString(localConf.contact, "");
324 adminitems: adminitems adminitem | adminitem;
325 adminitem: adminlocation | admincontact | error;
326 adminlocation: LOCATION '=' QSTRING ';'
328 if (localConf.location1 == NULL)
329 localConf.location1 = $3;
330 else if (localConf.location2 == NULL)
331 localConf.location2 = $3;
332 else /* Otherwise just drop it. -A1kmm */
335 admincontact: CONTACT '=' QSTRING ';'
337 MyFree(localConf.contact);
338 localConf.contact = $3;
347 struct ConnectionClass *c_class;
348 add_class(name, tping, tconn, maxlinks, sendq);
349 c_class = find_class(name);
350 MyFree(c_class->default_umode);
351 c_class->default_umode = pass;
352 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
353 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
356 parse_error("Missing name in class block");
363 memset(&privs, 0, sizeof(privs));
364 memset(&privs_dirty, 0, sizeof(privs_dirty));
366 classitems: classitem classitems | classitem;
367 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
368 classsendq | classusermode | priv | error;
369 classname: NAME '=' QSTRING ';'
374 classpingfreq: PINGFREQ '=' timespec ';'
378 classconnfreq: CONNECTFREQ '=' timespec ';'
382 classmaxlinks: MAXLINKS '=' expr ';'
386 classsendq: SENDQ '=' sizespec ';'
390 classusermode: USERMODE '=' QSTRING ';'
396 connectblock: CONNECT
399 flags = CONF_AUTOCONNECT;
400 } '{' connectitems '}'
402 struct ConfItem *aconf = NULL;
404 parse_error("Missing name in connect block");
405 else if (pass == NULL)
406 parse_error("Missing password in connect block");
407 else if (host == NULL)
408 parse_error("Missing host in connect block");
409 else if (strchr(host, '*') || strchr(host, '?'))
410 parse_error("Invalid host '%s' in connect block", host);
411 else if (c_class == NULL)
412 parse_error("Missing class in connect block");
414 aconf = make_conf(CONF_SERVER);
416 aconf->origin_name = origin;
417 aconf->passwd = pass;
418 aconf->conn_class = c_class;
419 aconf->address.port = port;
421 aconf->maximum = maxlinks;
422 aconf->hub_limit = hub_limit;
423 aconf->flags = flags;
424 lookup_confhost(aconf);
433 name = pass = host = origin = hub_limit = NULL;
437 connectitems: connectitem connectitems | connectitem;
438 connectitem: connectname | connectpass | connectclass | connecthost
439 | connectport | connectvhost | connectleaf | connecthub
440 | connecthublimit | connectmaxhops | connectauto | error;
441 connectname: NAME '=' QSTRING ';'
446 connectpass: PASS '=' QSTRING ';'
451 connectclass: CLASS '=' QSTRING ';'
453 c_class = find_class($3);
456 connecthost: HOST '=' QSTRING ';'
461 connectport: PORT '=' NUMBER ';'
465 connectvhost: VHOST '=' QSTRING ';'
470 connectleaf: LEAF ';'
477 DupString(hub_limit, "*");
479 connecthublimit: HUB '=' QSTRING ';'
484 connectmaxhops: MAXHOPS '=' expr ';'
488 connectauto: AUTOCONNECT '=' YES ';' { flags |= CONF_AUTOCONNECT; }
489 | AUTOCONNECT '=' NO ';' { flags &= ~CONF_AUTOCONNECT; };
491 uworldblock: UWORLD '{' uworlditems '}' ';';
492 uworlditems: uworlditem uworlditems | uworlditem;
493 uworlditem: uworldname | error;
494 uworldname: NAME '=' QSTRING ';'
496 make_conf(CONF_UWORLD)->host = $3;
499 operblock: OPER '{' operitems '}' ';'
501 struct ConfItem *aconf = NULL;
503 parse_error("Missing name in operator block");
504 else if (pass == NULL)
505 parse_error("Missing password in operator block");
506 else if (host == NULL)
507 parse_error("Missing host in operator block");
508 else if (c_class == NULL)
509 parse_error("Missing class in operator block");
511 aconf = make_conf(CONF_OPERATOR);
513 aconf->passwd = pass;
514 conf_parse_userhost(aconf, host);
515 aconf->conn_class = c_class;
516 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
517 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
518 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
519 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
520 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
527 name = pass = host = NULL;
529 memset(&privs, 0, sizeof(privs));
530 memset(&privs_dirty, 0, sizeof(privs_dirty));
532 operitems: operitem | operitems operitem;
533 operitem: opername | operpass | operhost | operclass | priv | error;
534 opername: NAME '=' QSTRING ';'
539 operpass: PASS '=' QSTRING ';'
544 operhost: HOST '=' QSTRING ';'
547 if (!strchr($3, '@'))
550 host = (char*) MyMalloc((uh_len = strlen($3)+3));
551 ircd_snprintf(0, host, uh_len, "*@%s", $3);
557 operclass: CLASS '=' QSTRING ';'
559 c_class = find_class($3);
563 priv: privtype '=' yesorno ';'
565 FlagSet(&privs_dirty, $1);
566 if (($3 == 1) ^ invert)
573 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
574 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
575 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
576 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
577 KILL { $$ = PRIV_KILL; } |
578 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
579 TPRIV_REHASH { $$ = PRIV_REHASH; } |
580 TPRIV_RESTART { $$ = PRIV_RESTART; } |
581 TPRIV_DIE { $$ = PRIV_DIE; } |
582 TPRIV_GLINE { $$ = PRIV_GLINE; } |
583 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
584 JUPE { $$ = PRIV_JUPE; } |
585 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
586 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
587 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
588 TPRIV_SET { $$ = PRIV_SET; } |
589 TPRIV_WHOX { $$ = PRIV_WHOX; } |
590 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
591 TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
592 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
593 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
594 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
595 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
596 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
597 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
598 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
599 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
600 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
601 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
602 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
603 TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
605 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
607 /* The port block... */
608 portblock: PORT '{' portitems '}' ';'
610 if (port > 0 && port <= 0xFFFF)
611 add_listener(port, host, pass, tconn, tping);
613 parse_error("Port %d is out of range", port);
617 port = tconn = tping = 0;
619 portitems: portitem portitems | portitem;
620 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
621 portnumber: PORT '=' NUMBER ';'
626 portvhost: VHOST '=' QSTRING ';'
632 portmask: MASK '=' QSTRING ';'
638 portserver: SERVER '=' YES ';'
641 } | SERVER '=' NO ';'
646 porthidden: HIDDEN '=' YES ';'
649 } | HIDDEN '=' NO ';'
658 '{' clientitems '}' ';'
660 struct irc_in_addr addr;
661 unsigned char addrbits = 0;
663 if (ip && !ipmask_parse(ip, &addr, &addrbits)) {
664 parse_error("Invalid IP address %s in block", ip);
670 struct ConfItem *aconf = make_conf(CONF_CLIENT);
671 aconf->username = username;
674 memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
676 memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
677 aconf->addrbits = addrbits;
679 aconf->conn_class = c_class ? c_class : find_class("default");
680 aconf->maximum = maxlinks;
681 aconf->passwd = pass;
689 clientitems: clientitem clientitems | clientitem;
690 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | error;
691 clienthost: HOST '=' QSTRING ';'
693 char *sep = strchr($3, '@');
698 DupString(host, sep);
704 clientip: IP '=' QSTRING ';'
707 sep = strchr($3, '@');
718 clientusername: USERNAME '=' QSTRING ';'
723 clientclass: CLASS '=' QSTRING ';'
725 c_class = find_class($3);
728 clientpass: PASS '=' QSTRING ';'
733 clientmaxlinks: MAXLINKS '=' expr ';'
740 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
743 if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
744 dconf->next = denyConfList;
745 denyConfList = dconf;
749 MyFree(dconf->usermask);
750 MyFree(dconf->hostmask);
751 MyFree(dconf->realmask);
752 MyFree(dconf->message);
754 parse_error("Kill block must match on at least one of username, host or realname");
758 killitems: killitem killitems | killitem;
759 killitem: killuhost | killreal | killusername | killreasonfile | killreason | error;
760 killuhost: HOST '=' QSTRING ';'
763 MyFree(dconf->hostmask);
764 MyFree(dconf->usermask);
765 if ((h = strchr($3, '@')) == NULL)
767 DupString(dconf->usermask, "*");
768 dconf->hostmask = $3;
773 DupString(dconf->hostmask, h);
774 dconf->usermask = $3;
776 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
779 killusername: USERNAME '=' QSTRING ';'
781 MyFree(dconf->usermask);
782 dconf->usermask = $3;
785 killreal: REAL '=' QSTRING ';'
787 MyFree(dconf->realmask);
788 dconf->realmask = $3;
791 killreason: REASON '=' QSTRING ';'
793 dconf->flags &= ~DENY_FLAGS_FILE;
794 MyFree(dconf->message);
798 killreasonfile: TFILE '=' QSTRING ';'
800 dconf->flags |= DENY_FLAGS_FILE;
801 MyFree(dconf->message);
808 } '{' cruleitems '}' ';'
810 struct CRuleNode *node = NULL;
812 parse_error("Missing host in crule block");
813 else if (pass == NULL)
814 parse_error("Missing rule in crule block");
815 else if ((node = crule_parse(pass)) == NULL)
816 parse_error("Invalid rule '%s' in crule block", pass);
819 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
824 p->next = cruleConfList;
836 cruleitems: cruleitem cruleitems | cruleitem;
837 cruleitem: cruleserver | crulerule | cruleall | error;
839 cruleserver: SERVER '=' QSTRING ';'
846 crulerule: RULE '=' QSTRING ';'
852 cruleall: ALL '=' YES ';'
860 motdblock: MOTD '{' motditems '}' ';'
862 if (host != NULL && pass != NULL)
863 motd_add(host, pass);
869 motditems: motditem motditems | motditem;
870 motditem: motdhost | motdfile | error;
871 motdhost: HOST '=' QSTRING ';'
876 motdfile: TFILE '=' QSTRING ';'
881 featuresblock: FEATURES '{' featureitems '}' ';';
882 featureitems: featureitems featureitem | featureitem;
888 } '=' stringlist ';';
897 feature_set(NULL, (const char * const *)stringlist, stringno);
898 for (ii = 0; ii < stringno; ++ii)
899 MyFree(stringlist[ii]);
901 posextrastrings: /* empty */ | extrastrings;
902 extrastrings: extrastrings extrastring | extrastring;
905 if (stringno < MAX_STRINGS)
906 stringlist[stringno++] = $1;
911 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
912 quarantineitems: quarantineitems quarantineitem | quarantineitem;
913 quarantineitem: QSTRING '=' QSTRING ';'
915 struct qline *qconf = MyCalloc(1, sizeof(*qconf));
918 qconf->next = GlobalQuarantineList;
919 GlobalQuarantineList = qconf;
922 pseudoblock: PSEUDO QSTRING '{'
924 smap = MyCalloc(1, sizeof(struct s_map));
932 parse_error("Missing name in pseudo %s block", smap->command);
933 else if (!smap->services)
934 parse_error("Missing nick in pseudo %s block", smap->command);
937 if (valid && register_mapping(smap))
939 smap->next = GlobalServiceMapList;
940 GlobalServiceMapList = smap;
944 struct nick_host *nh, *next;
945 for (nh = smap->services; nh; nh = next)
951 MyFree(smap->command);
952 MyFree(smap->prepend);
958 pseudoitems: pseudoitem pseudoitems | pseudoitem;
959 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags | error;
960 pseudoname: NAME '=' QSTRING ';'
965 pseudoprepend: PREPEND '=' QSTRING ';'
967 MyFree(smap->prepend);
970 pseudonick: NICK '=' QSTRING ';'
972 char *sep = strchr($3, '@');
975 size_t slen = strlen($3);
976 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
977 memcpy(nh->nick, $3, slen + 1);
978 nh->nicklen = sep - $3;
979 nh->next = smap->services;
984 pseudoflags: FAST ';'
986 smap->flags |= SMAP_FAST;
989 iauthblock: IAUTH '{'
996 parse_error("Missing host in iauth block");
998 parse_error("Missing port in iauth block");
1000 iauth_connect(host, port, pass, tconn, tping);
1004 port = tconn = tping = 0;
1007 iauthitems: iauthitem iauthitems | iauthitem;
1008 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
1009 iauthpass: PASS '=' QSTRING ';'
1014 iauthhost: HOST '=' QSTRING ';'
1019 iauthport: PORT '=' NUMBER ';'
1023 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1027 iauthtimeout: TIMEOUT '=' timespec ';'