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;
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;
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;
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;
343 } '{' classitems '}' ';'
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;
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 or non-existent 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;
441 connectname: NAME '=' QSTRING ';'
446 connectpass: PASS '=' QSTRING ';'
451 connectclass: CLASS '=' QSTRING ';'
453 c_class = find_class($3);
455 parse_error("No such connection class '%s' for Connect block", $3);
458 connecthost: HOST '=' QSTRING ';'
463 connectport: PORT '=' NUMBER ';'
467 connectvhost: VHOST '=' QSTRING ';'
472 connectleaf: LEAF ';'
479 DupString(hub_limit, "*");
481 connecthublimit: HUB '=' QSTRING ';'
486 connectmaxhops: MAXHOPS '=' expr ';'
490 connectauto: AUTOCONNECT '=' YES ';' { flags |= CONF_AUTOCONNECT; }
491 | AUTOCONNECT '=' NO ';' { flags &= ~CONF_AUTOCONNECT; };
493 uworldblock: UWORLD '{' uworlditems '}' ';';
494 uworlditems: uworlditem uworlditems | uworlditem;
495 uworlditem: uworldname;
496 uworldname: NAME '=' QSTRING ';'
498 make_conf(CONF_UWORLD)->host = $3;
501 operblock: OPER '{' operitems '}' ';'
503 struct ConfItem *aconf = NULL;
505 parse_error("Missing name in operator block");
506 else if (pass == NULL)
507 parse_error("Missing password in operator block");
508 else if (host == NULL)
509 parse_error("Missing host in operator block");
510 else if (c_class == NULL)
511 parse_error("Invalid or missing class in operator block");
513 aconf = make_conf(CONF_OPERATOR);
515 aconf->passwd = pass;
516 conf_parse_userhost(aconf, host);
517 aconf->conn_class = c_class;
518 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
519 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
520 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
521 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
522 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
529 name = pass = host = NULL;
531 memset(&privs, 0, sizeof(privs));
532 memset(&privs_dirty, 0, sizeof(privs_dirty));
534 operitems: operitem | operitems operitem;
535 operitem: opername | operpass | operhost | operclass | priv;
536 opername: NAME '=' QSTRING ';'
541 operpass: PASS '=' QSTRING ';'
546 operhost: HOST '=' QSTRING ';'
549 if (!strchr($3, '@'))
552 host = (char*) MyMalloc((uh_len = strlen($3)+3));
553 ircd_snprintf(0, host, uh_len, "*@%s", $3);
559 operclass: CLASS '=' QSTRING ';'
561 c_class = find_class($3);
563 parse_error("No such connection class '%s' for Operator block", $3);
567 priv: privtype '=' yesorno ';'
569 FlagSet(&privs_dirty, $1);
570 if (($3 == 1) ^ invert)
577 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
578 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
579 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
580 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
581 KILL { $$ = PRIV_KILL; } |
582 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
583 TPRIV_REHASH { $$ = PRIV_REHASH; } |
584 TPRIV_RESTART { $$ = PRIV_RESTART; } |
585 TPRIV_DIE { $$ = PRIV_DIE; } |
586 TPRIV_GLINE { $$ = PRIV_GLINE; } |
587 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
588 JUPE { $$ = PRIV_JUPE; } |
589 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
590 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
591 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
592 TPRIV_SET { $$ = PRIV_SET; } |
593 TPRIV_WHOX { $$ = PRIV_WHOX; } |
594 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
595 TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
596 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
597 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
598 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
599 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
600 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
601 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
602 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
603 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
604 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
605 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
606 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
607 TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
609 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
611 /* The port block... */
612 portblock: PORT '{' portitems '}' ';'
614 if (port > 0 && port <= 0xFFFF)
615 add_listener(port, host, pass, tconn, tping);
617 parse_error("Port %d is out of range", port);
621 port = tconn = tping = 0;
623 portitems: portitem portitems | portitem;
624 portitem: portnumber | portvhost | portmask | portserver | porthidden;
625 portnumber: PORT '=' NUMBER ';'
630 portvhost: VHOST '=' QSTRING ';'
636 portmask: MASK '=' QSTRING ';'
642 portserver: SERVER '=' YES ';'
645 } | SERVER '=' NO ';'
650 porthidden: HIDDEN '=' YES ';'
653 } | HIDDEN '=' NO ';'
662 '{' clientitems '}' ';'
664 struct ConfItem *aconf = 0;
665 struct irc_in_addr addr;
666 unsigned char addrbits = 0;
669 parse_error("Invalid or missing class in Client block");
670 else if (ip && !ipmask_parse(ip, &addr, &addrbits))
671 parse_error("Invalid IP address %s in Client block", ip);
673 aconf = make_conf(CONF_CLIENT);
674 aconf->username = username;
677 memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
679 memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
680 aconf->addrbits = addrbits;
682 aconf->conn_class = c_class;
683 aconf->maximum = maxlinks;
684 aconf->passwd = pass;
698 clientitems: clientitem clientitems | clientitem;
699 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks;
700 clienthost: HOST '=' QSTRING ';'
702 char *sep = strchr($3, '@');
707 DupString(host, sep);
713 clientip: IP '=' QSTRING ';'
716 sep = strchr($3, '@');
727 clientusername: USERNAME '=' QSTRING ';'
732 clientclass: CLASS '=' QSTRING ';'
734 c_class = find_class($3);
736 parse_error("No such connection class '%s' for Class block", $3);
739 clientpass: PASS '=' QSTRING ';'
744 clientmaxlinks: MAXLINKS '=' expr ';'
751 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
752 } '{' killitems '}' ';'
754 if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
755 dconf->next = denyConfList;
756 denyConfList = dconf;
760 MyFree(dconf->usermask);
761 MyFree(dconf->hostmask);
762 MyFree(dconf->realmask);
763 MyFree(dconf->message);
765 parse_error("Kill block must match on at least one of username, host or realname");
769 killitems: killitem killitems | killitem;
770 killitem: killuhost | killreal | killusername | killreasonfile | killreason;
771 killuhost: HOST '=' QSTRING ';'
774 MyFree(dconf->hostmask);
775 MyFree(dconf->usermask);
776 if ((h = strchr($3, '@')) == NULL)
778 DupString(dconf->usermask, "*");
779 dconf->hostmask = $3;
784 DupString(dconf->hostmask, h);
785 dconf->usermask = $3;
787 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
790 killusername: USERNAME '=' QSTRING ';'
792 MyFree(dconf->usermask);
793 dconf->usermask = $3;
796 killreal: REAL '=' QSTRING ';'
798 MyFree(dconf->realmask);
799 dconf->realmask = $3;
802 killreason: REASON '=' QSTRING ';'
804 dconf->flags &= ~DENY_FLAGS_FILE;
805 MyFree(dconf->message);
809 killreasonfile: TFILE '=' QSTRING ';'
811 dconf->flags |= DENY_FLAGS_FILE;
812 MyFree(dconf->message);
819 } '{' cruleitems '}' ';'
821 struct CRuleNode *node = NULL;
823 parse_error("Missing host in crule block");
824 else if (pass == NULL)
825 parse_error("Missing rule in crule block");
826 else if ((node = crule_parse(pass)) == NULL)
827 parse_error("Invalid rule '%s' in crule block", pass);
830 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
835 p->next = cruleConfList;
847 cruleitems: cruleitem cruleitems | cruleitem;
848 cruleitem: cruleserver | crulerule | cruleall;
850 cruleserver: SERVER '=' QSTRING ';'
857 crulerule: RULE '=' QSTRING ';'
863 cruleall: ALL '=' YES ';'
871 motdblock: MOTD '{' motditems '}' ';'
873 if (host != NULL && pass != NULL)
874 motd_add(host, pass);
880 motditems: motditem motditems | motditem;
881 motditem: motdhost | motdfile;
882 motdhost: HOST '=' QSTRING ';'
887 motdfile: TFILE '=' QSTRING ';'
892 featuresblock: FEATURES '{' featureitems '}' ';';
893 featureitems: featureitems featureitem | featureitem;
899 } '=' stringlist ';';
908 feature_set(NULL, (const char * const *)stringlist, stringno);
909 for (ii = 0; ii < stringno; ++ii)
910 MyFree(stringlist[ii]);
912 posextrastrings: /* empty */ | extrastrings;
913 extrastrings: extrastrings extrastring | extrastring;
916 if (stringno < MAX_STRINGS)
917 stringlist[stringno++] = $1;
922 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
923 quarantineitems: quarantineitems quarantineitem | quarantineitem;
924 quarantineitem: QSTRING '=' QSTRING ';'
926 struct qline *qconf = MyCalloc(1, sizeof(*qconf));
929 qconf->next = GlobalQuarantineList;
930 GlobalQuarantineList = qconf;
933 pseudoblock: PSEUDO QSTRING '{'
935 smap = MyCalloc(1, sizeof(struct s_map));
943 parse_error("Missing name in pseudo %s block", smap->command);
944 else if (!smap->services)
945 parse_error("Missing nick in pseudo %s block", smap->command);
948 if (valid && register_mapping(smap))
950 smap->next = GlobalServiceMapList;
951 GlobalServiceMapList = smap;
955 struct nick_host *nh, *next;
956 for (nh = smap->services; nh; nh = next)
962 MyFree(smap->command);
963 MyFree(smap->prepend);
969 pseudoitems: pseudoitem pseudoitems | pseudoitem;
970 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags;
971 pseudoname: NAME '=' QSTRING ';'
976 pseudoprepend: PREPEND '=' QSTRING ';'
978 MyFree(smap->prepend);
981 pseudonick: NICK '=' QSTRING ';'
983 char *sep = strchr($3, '@');
986 size_t slen = strlen($3);
987 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
988 memcpy(nh->nick, $3, slen + 1);
989 nh->nicklen = sep - $3;
990 nh->next = smap->services;
995 pseudoflags: FAST ';'
997 smap->flags |= SMAP_FAST;
1000 iauthblock: IAUTH '{'
1004 } iauthitems '}' ';'
1007 parse_error("Missing host in iauth block");
1009 parse_error("Missing port in iauth block");
1011 iauth_connect(host, port, pass, tconn, tping);
1015 port = tconn = tping = 0;
1018 iauthitems: iauthitem iauthitems | iauthitem;
1019 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout;
1020 iauthpass: PASS '=' QSTRING ';'
1025 iauthhost: HOST '=' QSTRING ';'
1030 iauthport: PORT '=' NUMBER ';'
1034 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1038 iauthtimeout: TIMEOUT '=' timespec ';'