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_chattr.h"
37 #include "ircd_reply.h"
38 #include "ircd_snprintf.h"
39 #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);
160 /* and now a lot of privileges... */
161 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
162 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
163 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
164 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
165 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
166 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
167 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE TPRIV_APASS_OPMODE
168 %token TPRIV_LIST_CHAN
169 /* and some types... */
171 %type <num> timespec timefactor factoredtimes factoredtime
172 %type <num> expr yesorno privtype
182 /* Blocks in the config file... */
183 blocks: blocks block | block;
184 block: adminblock | generalblock | classblock | connectblock |
185 uworldblock | operblock | portblock | jupeblock | clientblock |
186 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
187 pseudoblock | iauthblock | error ';';
189 /* The timespec, sizespec and expr was ripped straight from
191 timespec: expr | factoredtimes;
193 factoredtimes: factoredtimes factoredtime
198 factoredtime: expr timefactor
203 timefactor: SECONDS { $$ = 1; }
204 | MINUTES { $$ = 60; }
205 | HOURS { $$ = 60 * 60; }
206 | DAYS { $$ = 60 * 60 * 24; }
207 | WEEKS { $$ = 60 * 60 * 24 * 7; }
208 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
209 | YEARS { $$ = 60 * 60 * 24 * 365; }
210 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
223 $$ = $1 * 1024 * 1024;
226 $$ = $1 * 1024 * 1024 * 1024;
229 $$ = $1 * 1024 * 1024 * 1024;
233 /* this is an arithmetic expression */
250 /* leave this out until we find why it makes BSD yacc dump core -larne
251 | '-' expr %prec NEG {
259 jupeblock: JUPE '{' jupeitems '}' ';' ;
260 jupeitems: jupeitem jupeitems | jupeitem;
262 jupenick: NICK '=' QSTRING ';'
268 generalblock: GENERAL
270 /* Zero out the vhost addresses, in case they were removed. */
271 memset(&VirtualHost_v4.addr, 0, sizeof(VirtualHost_v4.addr));
272 memset(&VirtualHost_v6.addr, 0, sizeof(VirtualHost_v6.addr));
273 } '{' generalitems '}' ';' {
274 if (localConf.name == NULL)
275 parse_error("Your General block must contain a name.");
276 if (localConf.numeric == 0)
277 parse_error("Your General block must contain a numeric (between 1 and 4095).");
279 generalitems: generalitem generalitems | generalitem;
280 generalitem: generalnumeric | generalname | generalvhost | generaldesc;
281 generalnumeric: NUMERIC '=' NUMBER ';'
283 if (localConf.numeric == 0)
284 localConf.numeric = $3;
285 else if (localConf.numeric != $3)
286 parse_error("Redefinition of server numeric %i (%i)", $3,
290 generalname: NAME '=' QSTRING ';'
292 if (localConf.name == NULL)
295 if (strcmp(localConf.name, $3))
296 parse_error("Redefinition of server name %s (%s)", $3,
302 generaldesc: DESCRIPTION '=' QSTRING ';'
304 MyFree(localConf.description);
305 localConf.description = $3;
306 ircd_strncpy(cli_info(&me), $3, REALLEN);
309 generalvhost: VHOST '=' QSTRING ';'
311 struct irc_in_addr addr;
312 if (!strcmp($3, "*")) {
313 /* This traditionally meant bind to all interfaces and connect
314 * from the default. */
315 } else if (!ircd_aton(&addr, $3))
316 parse_error("Invalid virtual host '%s'.", $3);
317 else if (irc_in_addr_is_ipv4(&addr))
318 memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
320 memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
326 MyFree(localConf.location1);
327 MyFree(localConf.location2);
328 MyFree(localConf.contact);
329 localConf.location1 = localConf.location2 = localConf.contact = NULL;
331 '{' adminitems '}' ';'
333 if (localConf.location1 == NULL)
334 DupString(localConf.location1, "");
335 if (localConf.location2 == NULL)
336 DupString(localConf.location2, "");
337 if (localConf.contact == NULL)
338 DupString(localConf.contact, "");
340 adminitems: adminitems adminitem | adminitem;
341 adminitem: adminlocation | admincontact;
342 adminlocation: LOCATION '=' QSTRING ';'
344 if (localConf.location1 == NULL)
345 localConf.location1 = $3;
346 else if (localConf.location2 == NULL)
347 localConf.location2 = $3;
348 else /* Otherwise just drop it. -A1kmm */
351 admincontact: CONTACT '=' QSTRING ';'
353 MyFree(localConf.contact);
354 localConf.contact = $3;
359 } '{' classitems '}' ';'
363 struct ConnectionClass *c_class;
364 add_class(name, tping, tconn, maxlinks, sendq);
365 c_class = find_class(name);
366 MyFree(c_class->default_umode);
367 c_class->default_umode = pass;
368 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
369 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
372 parse_error("Missing name in class block");
379 memset(&privs, 0, sizeof(privs));
380 memset(&privs_dirty, 0, sizeof(privs_dirty));
382 classitems: classitem classitems | classitem;
383 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
384 classsendq | classusermode | priv;
385 classname: NAME '=' QSTRING ';'
390 classpingfreq: PINGFREQ '=' timespec ';'
394 classconnfreq: CONNECTFREQ '=' timespec ';'
398 classmaxlinks: MAXLINKS '=' expr ';'
402 classsendq: SENDQ '=' sizespec ';'
406 classusermode: USERMODE '=' QSTRING ';'
412 connectblock: CONNECT
415 flags = CONF_AUTOCONNECT;
416 } '{' connectitems '}' ';'
418 struct ConfItem *aconf = NULL;
420 parse_error("Missing name in connect block");
421 else if (pass == NULL)
422 parse_error("Missing password in connect block");
423 else if (strlen(pass) > PASSWDLEN)
424 parse_error("Password too long in connect block");
425 else if (host == NULL)
426 parse_error("Missing host in connect block");
427 else if (strchr(host, '*') || strchr(host, '?'))
428 parse_error("Invalid host '%s' in connect block", host);
429 else if (c_class == NULL)
430 parse_error("Missing or non-existent class in connect block");
432 aconf = make_conf(CONF_SERVER);
434 aconf->origin_name = origin;
435 aconf->passwd = pass;
436 aconf->conn_class = c_class;
437 aconf->address.port = port;
439 aconf->maximum = maxlinks;
440 aconf->hub_limit = hub_limit;
441 aconf->flags = flags;
442 lookup_confhost(aconf);
451 name = pass = host = origin = hub_limit = NULL;
455 connectitems: connectitem connectitems | connectitem;
456 connectitem: connectname | connectpass | connectclass | connecthost
457 | connectport | connectvhost | connectleaf | connecthub
458 | connecthublimit | connectmaxhops | connectauto;
459 connectname: NAME '=' QSTRING ';'
464 connectpass: PASS '=' QSTRING ';'
469 connectclass: CLASS '=' QSTRING ';'
471 c_class = find_class($3);
473 parse_error("No such connection class '%s' for Connect block", $3);
476 connecthost: HOST '=' QSTRING ';'
481 connectport: PORT '=' NUMBER ';'
485 connectvhost: VHOST '=' QSTRING ';'
490 connectleaf: LEAF ';'
497 DupString(hub_limit, "*");
499 connecthublimit: HUB '=' QSTRING ';'
504 connectmaxhops: MAXHOPS '=' expr ';'
508 connectauto: AUTOCONNECT '=' YES ';' { flags |= CONF_AUTOCONNECT; }
509 | AUTOCONNECT '=' NO ';' { flags &= ~CONF_AUTOCONNECT; };
511 uworldblock: UWORLD '{' uworlditems '}' ';';
512 uworlditems: uworlditem uworlditems | uworlditem;
513 uworlditem: uworldname;
514 uworldname: NAME '=' QSTRING ';'
516 make_conf(CONF_UWORLD)->host = $3;
519 operblock: OPER '{' operitems '}' ';'
521 struct ConfItem *aconf = NULL;
523 parse_error("Missing name in operator block");
524 else if (pass == NULL)
525 parse_error("Missing password in operator block");
526 /* Do not check password length because it may be crypted. */
527 else if (host == NULL)
528 parse_error("Missing host in operator block");
529 else if (c_class == NULL)
530 parse_error("Invalid or missing class in operator block");
531 else if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
532 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
533 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
535 aconf = make_conf(CONF_OPERATOR);
537 aconf->passwd = pass;
538 conf_parse_userhost(aconf, host);
539 aconf->conn_class = c_class;
540 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
541 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
548 name = pass = host = NULL;
550 memset(&privs, 0, sizeof(privs));
551 memset(&privs_dirty, 0, sizeof(privs_dirty));
553 operitems: operitem | operitems operitem;
554 operitem: opername | operpass | operhost | operclass | priv;
555 opername: NAME '=' QSTRING ';'
560 operpass: PASS '=' QSTRING ';'
565 operhost: HOST '=' QSTRING ';'
568 if (!strchr($3, '@'))
571 host = (char*) MyMalloc((uh_len = strlen($3)+3));
572 ircd_snprintf(0, host, uh_len, "*@%s", $3);
578 operclass: CLASS '=' QSTRING ';'
580 c_class = find_class($3);
582 parse_error("No such connection class '%s' for Operator block", $3);
586 priv: privtype '=' yesorno ';'
588 FlagSet(&privs_dirty, $1);
589 if (($3 == 1) ^ invert)
596 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
597 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
598 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
599 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
600 KILL { $$ = PRIV_KILL; } |
601 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
602 TPRIV_REHASH { $$ = PRIV_REHASH; } |
603 TPRIV_RESTART { $$ = PRIV_RESTART; } |
604 TPRIV_DIE { $$ = PRIV_DIE; } |
605 TPRIV_GLINE { $$ = PRIV_GLINE; } |
606 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
607 JUPE { $$ = PRIV_JUPE; } |
608 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
609 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
610 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
611 TPRIV_SET { $$ = PRIV_SET; } |
612 TPRIV_WHOX { $$ = PRIV_WHOX; } |
613 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
614 TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
615 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
616 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
617 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
618 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
619 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
620 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
621 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
622 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
623 TPRIV_LIST_CHAN { $$ = PRIV_LIST_CHAN; } |
624 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
625 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
626 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
627 TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
629 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
631 /* The port block... */
632 portblock: PORT '{' portitems '}' ';'
634 if (port > 0 && port <= 0xFFFF)
635 add_listener(port, host, pass, tconn, tping);
637 parse_error("Port %d is out of range", port);
641 port = tconn = tping = 0;
643 portitems: portitem portitems | portitem;
644 portitem: portnumber | portvhost | portmask | portserver | porthidden;
645 portnumber: PORT '=' NUMBER ';'
650 portvhost: VHOST '=' QSTRING ';'
656 portmask: MASK '=' QSTRING ';'
662 portserver: SERVER '=' YES ';'
665 } | SERVER '=' NO ';'
670 porthidden: HIDDEN '=' YES ';'
673 } | HIDDEN '=' NO ';'
683 '{' clientitems '}' ';'
685 struct ConfItem *aconf = 0;
686 struct irc_in_addr addr;
687 unsigned char addrbits = 0;
690 parse_error("Invalid or missing class in Client block");
691 else if (pass && strlen(pass) > PASSWDLEN)
692 parse_error("Password too long in connect block");
693 else if (ip && !ipmask_parse(ip, &addr, &addrbits))
694 parse_error("Invalid IP address %s in Client block", ip);
696 aconf = make_conf(CONF_CLIENT);
697 aconf->username = username;
700 memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
702 memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
703 aconf->address.port = port;
704 aconf->addrbits = addrbits;
706 aconf->conn_class = c_class;
707 aconf->maximum = maxlinks;
708 aconf->passwd = pass;
723 clientitems: clientitem clientitems | clientitem;
724 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | clientport;
725 clienthost: HOST '=' QSTRING ';'
727 char *sep = strchr($3, '@');
732 DupString(host, sep);
738 clientip: IP '=' QSTRING ';'
741 sep = strchr($3, '@');
752 clientusername: USERNAME '=' QSTRING ';'
757 clientclass: CLASS '=' QSTRING ';'
759 c_class = find_class($3);
761 parse_error("No such connection class '%s' for Client block", $3);
764 clientpass: PASS '=' QSTRING ';'
769 clientmaxlinks: MAXLINKS '=' expr ';'
773 clientport: PORT '=' expr ';'
780 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
781 } '{' killitems '}' ';'
783 if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
784 dconf->next = denyConfList;
785 denyConfList = dconf;
789 MyFree(dconf->usermask);
790 MyFree(dconf->hostmask);
791 MyFree(dconf->realmask);
792 MyFree(dconf->message);
794 parse_error("Kill block must match on at least one of username, host or realname");
798 killitems: killitem killitems | killitem;
799 killitem: killuhost | killreal | killusername | killreasonfile | killreason;
800 killuhost: HOST '=' QSTRING ';'
803 MyFree(dconf->hostmask);
804 MyFree(dconf->usermask);
805 if ((h = strchr($3, '@')) == NULL)
807 DupString(dconf->usermask, "*");
808 dconf->hostmask = $3;
813 DupString(dconf->hostmask, h);
814 dconf->usermask = $3;
816 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
819 killusername: USERNAME '=' QSTRING ';'
821 MyFree(dconf->usermask);
822 dconf->usermask = $3;
825 killreal: REAL '=' QSTRING ';'
827 MyFree(dconf->realmask);
828 dconf->realmask = $3;
831 killreason: REASON '=' QSTRING ';'
833 dconf->flags &= ~DENY_FLAGS_FILE;
834 MyFree(dconf->message);
838 killreasonfile: TFILE '=' QSTRING ';'
840 dconf->flags |= DENY_FLAGS_FILE;
841 MyFree(dconf->message);
848 } '{' cruleitems '}' ';'
850 struct CRuleNode *node = NULL;
852 parse_error("Missing host in crule block");
853 else if (pass == NULL)
854 parse_error("Missing rule in crule block");
855 else if ((node = crule_parse(pass)) == NULL)
856 parse_error("Invalid rule '%s' in crule block", pass);
859 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
864 p->next = cruleConfList;
876 cruleitems: cruleitem cruleitems | cruleitem;
877 cruleitem: cruleserver | crulerule | cruleall;
879 cruleserver: SERVER '=' QSTRING ';'
886 crulerule: RULE '=' QSTRING ';'
892 cruleall: ALL '=' YES ';'
900 motdblock: MOTD '{' motditems '}' ';'
902 if (host != NULL && pass != NULL)
903 motd_add(host, pass);
909 motditems: motditem motditems | motditem;
910 motditem: motdhost | motdfile;
911 motdhost: HOST '=' QSTRING ';'
916 motdfile: TFILE '=' QSTRING ';'
921 featuresblock: FEATURES '{' featureitems '}' ';';
922 featureitems: featureitems featureitem | featureitem;
928 } '=' stringlist ';' {
930 feature_set(NULL, (const char * const *)stringlist, stringno);
931 for (ii = 0; ii < stringno; ++ii)
932 MyFree(stringlist[ii]);
935 stringlist: stringlist extrastring | extrastring;
938 if (stringno < MAX_STRINGS)
939 stringlist[stringno++] = $1;
944 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
945 quarantineitems: quarantineitems quarantineitem | quarantineitem;
946 quarantineitem: QSTRING '=' QSTRING ';'
948 struct qline *qconf = MyCalloc(1, sizeof(*qconf));
951 qconf->next = GlobalQuarantineList;
952 GlobalQuarantineList = qconf;
955 pseudoblock: PSEUDO QSTRING '{'
957 smap = MyCalloc(1, sizeof(struct s_map));
965 parse_error("Missing name in pseudo %s block", smap->command);
966 else if (!smap->services)
967 parse_error("Missing nick in pseudo %s block", smap->command);
970 if (valid && register_mapping(smap))
972 smap->next = GlobalServiceMapList;
973 GlobalServiceMapList = smap;
982 pseudoitems: pseudoitem pseudoitems | pseudoitem;
983 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags;
984 pseudoname: NAME '=' QSTRING ';'
989 pseudoprepend: PREPEND '=' QSTRING ';'
991 MyFree(smap->prepend);
994 pseudonick: NICK '=' QSTRING ';'
996 char *sep = strchr($3, '@');
999 size_t slen = strlen($3);
1000 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
1001 memcpy(nh->nick, $3, slen + 1);
1002 nh->nicklen = sep - $3;
1003 nh->next = smap->services;
1004 smap->services = nh;
1008 pseudoflags: FAST ';'
1010 smap->flags |= SMAP_FAST;
1013 iauthblock: IAUTH '{' iauthitems '}' ';'
1015 auth_spawn(stringno, stringlist);
1016 while (stringno > 0)
1017 MyFree(stringlist[--stringno]);
1020 iauthitems: iauthitem iauthitems | iauthitem;
1021 iauthitem: iauthprogram;
1022 iauthprogram: PROGRAM '='
1024 while (stringno > 0)
1025 MyFree(stringlist[--stringno]);