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 /* and some types... */
170 %type <num> timespec timefactor factoredtimes factoredtime
171 %type <num> expr yesorno privtype
181 /* Blocks in the config file... */
182 blocks: blocks block | block;
183 block: adminblock | generalblock | classblock | connectblock |
184 uworldblock | operblock | portblock | jupeblock | clientblock |
185 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
186 pseudoblock | iauthblock | error ';';
188 /* The timespec, sizespec and expr was ripped straight from
190 timespec: expr | factoredtimes;
192 factoredtimes: factoredtimes factoredtime
197 factoredtime: expr timefactor
202 timefactor: SECONDS { $$ = 1; }
203 | MINUTES { $$ = 60; }
204 | HOURS { $$ = 60 * 60; }
205 | DAYS { $$ = 60 * 60 * 24; }
206 | WEEKS { $$ = 60 * 60 * 24 * 7; }
207 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
208 | YEARS { $$ = 60 * 60 * 24 * 365; }
209 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
222 $$ = $1 * 1024 * 1024;
225 $$ = $1 * 1024 * 1024 * 1024;
228 $$ = $1 * 1024 * 1024 * 1024;
232 /* this is an arithmetic expression */
249 /* leave this out until we find why it makes BSD yacc dump core -larne
250 | '-' expr %prec NEG {
258 jupeblock: JUPE '{' jupeitems '}' ';' ;
259 jupeitems: jupeitem jupeitems | jupeitem;
261 jupenick: NICK '=' QSTRING ';'
267 generalblock: GENERAL
269 /* Zero out the vhost addresses, in case they were removed. */
270 memset(&VirtualHost_v4.addr, 0, sizeof(VirtualHost_v4.addr));
271 memset(&VirtualHost_v6.addr, 0, sizeof(VirtualHost_v6.addr));
272 } '{' generalitems '}' ';' {
273 if (localConf.name == NULL)
274 parse_error("Your General block must contain a name.");
275 if (localConf.numeric == 0)
276 parse_error("Your General block must contain a numeric (between 1 and 4095).");
278 generalitems: generalitem generalitems | generalitem;
279 generalitem: generalnumeric | generalname | generalvhost | generaldesc;
280 generalnumeric: NUMERIC '=' NUMBER ';'
282 if (localConf.numeric == 0)
283 localConf.numeric = $3;
284 else if (localConf.numeric != $3)
285 parse_error("Redefinition of server numeric %i (%i)", $3,
289 generalname: NAME '=' QSTRING ';'
291 if (localConf.name == NULL)
294 if (strcmp(localConf.name, $3))
295 parse_error("Redefinition of server name %s (%s)", $3,
301 generaldesc: DESCRIPTION '=' QSTRING ';'
303 MyFree(localConf.description);
304 localConf.description = $3;
305 ircd_strncpy(cli_info(&me), $3, REALLEN);
308 generalvhost: VHOST '=' QSTRING ';'
310 struct irc_in_addr addr;
311 if (!strcmp($3, "*")) {
312 /* This traditionally meant bind to all interfaces and connect
313 * from the default. */
314 } else if (!ircd_aton(&addr, $3))
315 parse_error("Invalid virtual host '%s'.", $3);
316 else if (irc_in_addr_is_ipv4(&addr))
317 memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
319 memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
323 adminblock: ADMIN '{' adminitems '}' ';'
325 if (localConf.location1 == NULL)
326 DupString(localConf.location1, "");
327 if (localConf.location2 == NULL)
328 DupString(localConf.location2, "");
329 if (localConf.contact == NULL)
330 DupString(localConf.contact, "");
332 adminitems: adminitems adminitem | adminitem;
333 adminitem: adminlocation | admincontact;
334 adminlocation: LOCATION '=' QSTRING ';'
336 if (localConf.location1 == NULL)
337 localConf.location1 = $3;
338 else if (localConf.location2 == NULL)
339 localConf.location2 = $3;
340 else /* Otherwise just drop it. -A1kmm */
343 admincontact: CONTACT '=' QSTRING ';'
345 MyFree(localConf.contact);
346 localConf.contact = $3;
351 } '{' classitems '}' ';'
355 struct ConnectionClass *c_class;
356 add_class(name, tping, tconn, maxlinks, sendq);
357 c_class = find_class(name);
358 MyFree(c_class->default_umode);
359 c_class->default_umode = pass;
360 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
361 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
364 parse_error("Missing name in class block");
371 memset(&privs, 0, sizeof(privs));
372 memset(&privs_dirty, 0, sizeof(privs_dirty));
374 classitems: classitem classitems | classitem;
375 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
376 classsendq | classusermode | priv;
377 classname: NAME '=' QSTRING ';'
382 classpingfreq: PINGFREQ '=' timespec ';'
386 classconnfreq: CONNECTFREQ '=' timespec ';'
390 classmaxlinks: MAXLINKS '=' expr ';'
394 classsendq: SENDQ '=' sizespec ';'
398 classusermode: USERMODE '=' QSTRING ';'
404 connectblock: CONNECT
407 flags = CONF_AUTOCONNECT;
408 } '{' connectitems '}' ';'
410 struct ConfItem *aconf = NULL;
412 parse_error("Missing name in connect block");
413 else if (pass == NULL)
414 parse_error("Missing password in connect block");
415 else if (host == NULL)
416 parse_error("Missing host in connect block");
417 else if (strchr(host, '*') || strchr(host, '?'))
418 parse_error("Invalid host '%s' in connect block", host);
419 else if (c_class == NULL)
420 parse_error("Missing or non-existent class in connect block");
422 aconf = make_conf(CONF_SERVER);
424 aconf->origin_name = origin;
425 aconf->passwd = pass;
426 aconf->conn_class = c_class;
427 aconf->address.port = port;
429 aconf->maximum = maxlinks;
430 aconf->hub_limit = hub_limit;
431 aconf->flags = flags;
432 lookup_confhost(aconf);
441 name = pass = host = origin = hub_limit = NULL;
445 connectitems: connectitem connectitems | connectitem;
446 connectitem: connectname | connectpass | connectclass | connecthost
447 | connectport | connectvhost | connectleaf | connecthub
448 | connecthublimit | connectmaxhops | connectauto;
449 connectname: NAME '=' QSTRING ';'
454 connectpass: PASS '=' QSTRING ';'
459 connectclass: CLASS '=' QSTRING ';'
461 c_class = find_class($3);
463 parse_error("No such connection class '%s' for Connect block", $3);
466 connecthost: HOST '=' QSTRING ';'
471 connectport: PORT '=' NUMBER ';'
475 connectvhost: VHOST '=' QSTRING ';'
480 connectleaf: LEAF ';'
487 DupString(hub_limit, "*");
489 connecthublimit: HUB '=' QSTRING ';'
494 connectmaxhops: MAXHOPS '=' expr ';'
498 connectauto: AUTOCONNECT '=' YES ';' { flags |= CONF_AUTOCONNECT; }
499 | AUTOCONNECT '=' NO ';' { flags &= ~CONF_AUTOCONNECT; };
501 uworldblock: UWORLD '{' uworlditems '}' ';';
502 uworlditems: uworlditem uworlditems | uworlditem;
503 uworlditem: uworldname;
504 uworldname: NAME '=' QSTRING ';'
506 make_conf(CONF_UWORLD)->host = $3;
509 operblock: OPER '{' operitems '}' ';'
511 struct ConfItem *aconf = NULL;
513 parse_error("Missing name in operator block");
514 else if (pass == NULL)
515 parse_error("Missing password in operator block");
516 else if (host == NULL)
517 parse_error("Missing host in operator block");
518 else if (c_class == NULL)
519 parse_error("Invalid or missing class in operator block");
521 aconf = make_conf(CONF_OPERATOR);
523 aconf->passwd = pass;
524 conf_parse_userhost(aconf, host);
525 aconf->conn_class = c_class;
526 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
527 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
528 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
529 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
530 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
537 name = pass = host = NULL;
539 memset(&privs, 0, sizeof(privs));
540 memset(&privs_dirty, 0, sizeof(privs_dirty));
542 operitems: operitem | operitems operitem;
543 operitem: opername | operpass | operhost | operclass | priv;
544 opername: NAME '=' QSTRING ';'
549 operpass: PASS '=' QSTRING ';'
554 operhost: HOST '=' QSTRING ';'
557 if (!strchr($3, '@'))
560 host = (char*) MyMalloc((uh_len = strlen($3)+3));
561 ircd_snprintf(0, host, uh_len, "*@%s", $3);
567 operclass: CLASS '=' QSTRING ';'
569 c_class = find_class($3);
571 parse_error("No such connection class '%s' for Operator block", $3);
575 priv: privtype '=' yesorno ';'
577 FlagSet(&privs_dirty, $1);
578 if (($3 == 1) ^ invert)
585 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
586 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
587 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
588 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
589 KILL { $$ = PRIV_KILL; } |
590 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
591 TPRIV_REHASH { $$ = PRIV_REHASH; } |
592 TPRIV_RESTART { $$ = PRIV_RESTART; } |
593 TPRIV_DIE { $$ = PRIV_DIE; } |
594 TPRIV_GLINE { $$ = PRIV_GLINE; } |
595 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
596 JUPE { $$ = PRIV_JUPE; } |
597 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
598 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
599 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
600 TPRIV_SET { $$ = PRIV_SET; } |
601 TPRIV_WHOX { $$ = PRIV_WHOX; } |
602 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
603 TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
604 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
605 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
606 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
607 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
608 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
609 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
610 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
611 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
612 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
613 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
614 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
615 TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
617 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
619 /* The port block... */
620 portblock: PORT '{' portitems '}' ';'
622 if (port > 0 && port <= 0xFFFF)
623 add_listener(port, host, pass, tconn, tping);
625 parse_error("Port %d is out of range", port);
629 port = tconn = tping = 0;
631 portitems: portitem portitems | portitem;
632 portitem: portnumber | portvhost | portmask | portserver | porthidden;
633 portnumber: PORT '=' NUMBER ';'
638 portvhost: VHOST '=' QSTRING ';'
644 portmask: MASK '=' QSTRING ';'
650 portserver: SERVER '=' YES ';'
653 } | SERVER '=' NO ';'
658 porthidden: HIDDEN '=' YES ';'
661 } | HIDDEN '=' NO ';'
671 '{' clientitems '}' ';'
673 struct ConfItem *aconf = 0;
674 struct irc_in_addr addr;
675 unsigned char addrbits = 0;
678 parse_error("Invalid or missing class in Client block");
679 else if (ip && !ipmask_parse(ip, &addr, &addrbits))
680 parse_error("Invalid IP address %s in Client block", ip);
682 aconf = make_conf(CONF_CLIENT);
683 aconf->username = username;
686 memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
688 memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
689 aconf->address.port = port;
690 aconf->addrbits = addrbits;
692 aconf->conn_class = c_class;
693 aconf->maximum = maxlinks;
694 aconf->passwd = pass;
709 clientitems: clientitem clientitems | clientitem;
710 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | clientport;
711 clienthost: HOST '=' QSTRING ';'
713 char *sep = strchr($3, '@');
718 DupString(host, sep);
724 clientip: IP '=' QSTRING ';'
727 sep = strchr($3, '@');
738 clientusername: USERNAME '=' QSTRING ';'
743 clientclass: CLASS '=' QSTRING ';'
745 c_class = find_class($3);
747 parse_error("No such connection class '%s' for Client block", $3);
750 clientpass: PASS '=' QSTRING ';'
755 clientmaxlinks: MAXLINKS '=' expr ';'
759 clientport: PORT '=' expr ';'
766 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
767 } '{' killitems '}' ';'
769 if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
770 dconf->next = denyConfList;
771 denyConfList = dconf;
775 MyFree(dconf->usermask);
776 MyFree(dconf->hostmask);
777 MyFree(dconf->realmask);
778 MyFree(dconf->message);
780 parse_error("Kill block must match on at least one of username, host or realname");
784 killitems: killitem killitems | killitem;
785 killitem: killuhost | killreal | killusername | killreasonfile | killreason;
786 killuhost: HOST '=' QSTRING ';'
789 MyFree(dconf->hostmask);
790 MyFree(dconf->usermask);
791 if ((h = strchr($3, '@')) == NULL)
793 DupString(dconf->usermask, "*");
794 dconf->hostmask = $3;
799 DupString(dconf->hostmask, h);
800 dconf->usermask = $3;
802 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
805 killusername: USERNAME '=' QSTRING ';'
807 MyFree(dconf->usermask);
808 dconf->usermask = $3;
811 killreal: REAL '=' QSTRING ';'
813 MyFree(dconf->realmask);
814 dconf->realmask = $3;
817 killreason: REASON '=' QSTRING ';'
819 dconf->flags &= ~DENY_FLAGS_FILE;
820 MyFree(dconf->message);
824 killreasonfile: TFILE '=' QSTRING ';'
826 dconf->flags |= DENY_FLAGS_FILE;
827 MyFree(dconf->message);
834 } '{' cruleitems '}' ';'
836 struct CRuleNode *node = NULL;
838 parse_error("Missing host in crule block");
839 else if (pass == NULL)
840 parse_error("Missing rule in crule block");
841 else if ((node = crule_parse(pass)) == NULL)
842 parse_error("Invalid rule '%s' in crule block", pass);
845 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
850 p->next = cruleConfList;
862 cruleitems: cruleitem cruleitems | cruleitem;
863 cruleitem: cruleserver | crulerule | cruleall;
865 cruleserver: SERVER '=' QSTRING ';'
872 crulerule: RULE '=' QSTRING ';'
878 cruleall: ALL '=' YES ';'
886 motdblock: MOTD '{' motditems '}' ';'
888 if (host != NULL && pass != NULL)
889 motd_add(host, pass);
895 motditems: motditem motditems | motditem;
896 motditem: motdhost | motdfile;
897 motdhost: HOST '=' QSTRING ';'
902 motdfile: TFILE '=' QSTRING ';'
907 featuresblock: FEATURES '{' featureitems '}' ';';
908 featureitems: featureitems featureitem | featureitem;
914 } '=' stringlist ';' {
916 feature_set(NULL, (const char * const *)stringlist, stringno);
917 for (ii = 0; ii < stringno; ++ii)
918 MyFree(stringlist[ii]);
921 stringlist: stringlist extrastring | extrastring;
924 if (stringno < MAX_STRINGS)
925 stringlist[stringno++] = $1;
930 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
931 quarantineitems: quarantineitems quarantineitem | quarantineitem;
932 quarantineitem: QSTRING '=' QSTRING ';'
934 struct qline *qconf = MyCalloc(1, sizeof(*qconf));
937 qconf->next = GlobalQuarantineList;
938 GlobalQuarantineList = qconf;
941 pseudoblock: PSEUDO QSTRING '{'
943 smap = MyCalloc(1, sizeof(struct s_map));
951 parse_error("Missing name in pseudo %s block", smap->command);
952 else if (!smap->services)
953 parse_error("Missing nick in pseudo %s block", smap->command);
956 if (valid && register_mapping(smap))
958 smap->next = GlobalServiceMapList;
959 GlobalServiceMapList = smap;
968 pseudoitems: pseudoitem pseudoitems | pseudoitem;
969 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags;
970 pseudoname: NAME '=' QSTRING ';'
975 pseudoprepend: PREPEND '=' QSTRING ';'
977 MyFree(smap->prepend);
980 pseudonick: NICK '=' QSTRING ';'
982 char *sep = strchr($3, '@');
985 size_t slen = strlen($3);
986 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
987 memcpy(nh->nick, $3, slen + 1);
988 nh->nicklen = sep - $3;
989 nh->next = smap->services;
994 pseudoflags: FAST ';'
996 smap->flags |= SMAP_FAST;
999 iauthblock: IAUTH '{' iauthitems '}' ';'
1001 auth_spawn(stringno, stringlist);
1002 while (stringno > 0)
1003 MyFree(stringlist[stringno--]);
1006 iauthitems: iauthitem iauthitems | iauthitem;
1007 iauthitem: iauthprogram;
1008 iauthprogram: PROGRAM '='
1010 while (stringno > 0)
1011 MyFree(stringlist[stringno--]);