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;
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;
77 struct qline *qconf = NULL;
80 struct Privs privs_dirty;
82 static void parse_error(char *pattern,...) {
83 static char error_buffer[1024];
86 ircd_vsnprintf(NULL, error_buffer, sizeof(error_buffer), pattern, vl);
88 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
265 generalblock: GENERAL '{' generalitems '}'
267 if (localConf.name == NULL)
268 parse_error("Your General block must contain a name.");
269 if (localConf.numeric == 0)
270 parse_error("Your General block must contain a numeric (between 1 and 4095).");
272 generalitems: generalitem generalitems | generalitem;
273 generalitem: generalnumeric | generalname | generalvhost | generaldesc | error;
274 generalnumeric: NUMERIC '=' NUMBER ';'
276 if (localConf.numeric == 0)
277 localConf.numeric = $3;
278 else if (localConf.numeric != $3)
279 parse_error("Redefinition of server numeric %i (%i)", $3,
283 generalname: NAME '=' QSTRING ';'
285 if (localConf.name == NULL)
286 DupString(localConf.name, $3);
287 else if (strcmp(localConf.name, $3))
288 parse_error("Redefinition of server name %s (%s)", $3,
292 generaldesc: DESCRIPTION '=' QSTRING ';'
294 MyFree(localConf.description);
295 DupString(localConf.description, $3);
296 ircd_strncpy(cli_info(&me), $3, REALLEN);
299 generalvhost: VHOST '=' QSTRING ';'
301 struct irc_in_addr addr;
302 if (!ircd_aton(&addr, $3))
303 parse_error("Invalid virtual host '%s'.", $3);
304 else if (irc_in_addr_is_ipv4(&addr))
305 memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
307 memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
310 adminblock: ADMIN '{' adminitems '}'
312 if (localConf.location1 == NULL)
313 DupString(localConf.location1, "");
314 if (localConf.location2 == NULL)
315 DupString(localConf.location2, "");
316 if (localConf.contact == NULL)
317 DupString(localConf.contact, "");
319 adminitems: adminitems adminitem | adminitem;
320 adminitem: adminlocation | admincontact | error;
321 adminlocation: LOCATION '=' QSTRING ';'
323 if (localConf.location1 == NULL)
324 DupString(localConf.location1, $3);
325 else if (localConf.location2 == NULL)
326 DupString(localConf.location2, $3);
327 /* Otherwise just drop it. -A1kmm */
329 admincontact: CONTACT '=' QSTRING ';'
331 if (localConf.contact != NULL)
332 MyFree(localConf.contact);
333 DupString(localConf.contact, $3);
342 struct ConnectionClass *c_class;
343 add_class(name, tping, tconn, maxlinks, sendq);
344 c_class = find_class(name);
345 c_class->default_umode = pass;
346 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
347 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
350 parse_error("Missing name in class block");
357 memset(&privs, 0, sizeof(privs));
358 memset(&privs_dirty, 0, sizeof(privs_dirty));
360 classitems: classitem classitems | classitem;
361 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
362 classsendq | classusermode | priv | error;
363 classname: NAME '=' QSTRING ';'
368 classpingfreq: PINGFREQ '=' timespec ';'
372 classconnfreq: CONNECTFREQ '=' timespec ';'
376 classmaxlinks: MAXLINKS '=' expr ';'
380 classsendq: SENDQ '=' sizespec ';'
384 classusermode: USERMODE '=' QSTRING ';'
390 connectblock: CONNECT
393 } '{' connectitems '}'
395 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
396 && !strchr(host, '*') && !strchr(host, '?'))
398 struct ConfItem *aconf = make_conf(CONF_SERVER);
400 aconf->origin_name = origin;
401 aconf->passwd = pass;
402 aconf->conn_class = c_class;
403 aconf->address.port = port;
405 aconf->maximum = maxlinks;
406 aconf->hub_limit = hub_limit;
407 lookup_confhost(aconf);
416 parse_error("Bad connect block");
418 name = pass = host = origin = hub_limit = NULL;
422 connectitems: connectitem connectitems | connectitem;
423 connectitem: connectname | connectpass | connectclass | connecthost
424 | connectport | connectvhost | connectleaf | connecthub
425 | connecthublimit | connectmaxhops | error;
426 connectname: NAME '=' QSTRING ';'
431 connectpass: PASS '=' QSTRING ';'
436 connectclass: CLASS '=' QSTRING ';'
438 c_class = find_class($3);
440 connecthost: HOST '=' QSTRING ';'
445 connectport: PORT '=' NUMBER ';'
449 connectvhost: VHOST '=' QSTRING ';'
452 DupString(origin, $3);
454 connectleaf: LEAF ';'
461 DupString(hub_limit, "*");
463 connecthublimit: HUB '=' QSTRING ';'
466 DupString(hub_limit, $3);
468 connectmaxhops: MAXHOPS '=' expr ';'
473 uworldblock: UWORLD '{' uworlditems '}' ';'
477 struct ConfItem *aconf = make_conf(CONF_UWORLD);
483 parse_error("Bad UWorld block");
487 uworlditems: uworlditem uworlditems | uworlditem;
488 uworlditem: uworldname | error;
489 uworldname: NAME '=' QSTRING ';'
495 operblock: OPER '{' operitems '}' ';'
497 if (name && pass && host && c_class)
499 struct ConfItem *aconf = make_conf(CONF_OPERATOR);
501 aconf->passwd = pass;
502 conf_parse_userhost(aconf, host);
503 aconf->conn_class = c_class;
504 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
505 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
506 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
507 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
508 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
512 parse_error("operator blocks need a name, password, class and host.");
517 name = pass = host = NULL;
519 memset(&privs, 0, sizeof(privs));
520 memset(&privs_dirty, 0, sizeof(privs_dirty));
522 operitems: operitem | operitems operitem;
523 operitem: opername | operpass | operhost | operclass | priv | error;
524 opername: NAME '=' QSTRING ';'
529 operpass: PASS '=' QSTRING ';'
534 operhost: HOST '=' QSTRING ';'
537 if (!strchr($3, '@'))
540 char *b = (char*) MyMalloc((uh_len = strlen($3)+3));
541 ircd_snprintf(0, b, uh_len, "*@%s", $3);
547 operclass: CLASS '=' QSTRING ';'
549 c_class = find_class($3);
552 priv: privtype '=' yesorno ';'
554 FlagSet(&privs_dirty, $1);
555 if (($3 == 1) ^ invert)
562 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
563 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
564 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
565 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
566 KILL { $$ = PRIV_KILL; } |
567 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
568 TPRIV_REHASH { $$ = PRIV_REHASH; } |
569 TPRIV_RESTART { $$ = PRIV_RESTART; } |
570 TPRIV_DIE { $$ = PRIV_DIE; } |
571 TPRIV_GLINE { $$ = PRIV_GLINE; } |
572 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
573 JUPE { $$ = PRIV_JUPE; } |
574 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
575 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
576 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
577 TPRIV_SET { $$ = PRIV_SET; } |
578 TPRIV_WHOX { $$ = PRIV_WHOX; } |
579 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
580 TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
581 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
582 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
583 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
584 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
585 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
586 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
587 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
588 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
589 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
590 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
591 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
592 TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
594 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
596 /* The port block... */
597 portblock: PORT '{' portitems '}' ';'
599 if (port > 0 && port <= 0xFFFF)
601 add_listener(port, host, pass, tconn, tping);
605 parse_error("Bad port block");
610 port = tconn = tping = 0;
612 portitems: portitem portitems | portitem;
613 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
614 portnumber: PORT '=' NUMBER ';'
619 portvhost: VHOST '=' QSTRING ';'
625 portmask: MASK '=' QSTRING ';'
631 portserver: SERVER '=' YES ';'
634 } | SERVER '=' NO ';'
639 porthidden: HIDDEN '=' YES ';'
642 } | HIDDEN '=' NO ';'
651 '{' clientitems '}' ';'
653 struct irc_in_addr addr;
654 unsigned char addrbits = 0;
656 if (ip && !ipmask_parse(ip, &addr, &addrbits)) {
657 parse_error("Invalid IP address in block");
662 struct ConfItem *aconf = make_conf(CONF_CLIENT);
663 aconf->username = username;
666 memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
668 memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
669 aconf->addrbits = addrbits;
671 aconf->conn_class = c_class ? c_class : find_class("default");
672 aconf->maximum = maxlinks;
679 clientitems: clientitem clientitems | clientitem;
680 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | error;
681 clienthost: HOST '=' QSTRING ';'
683 char *sep = strchr($3, '@');
688 DupString(username, $3);
689 DupString(host, sep);
694 clientip: IP '=' QSTRING ';'
696 char *sep = strchr($3, '@');
701 DupString(username, $3);
707 clientusername: USERNAME '=' QSTRING ';'
710 DupString(username, $3);
712 clientclass: CLASS '=' QSTRING ';'
714 c_class = find_class($3);
716 clientpass: PASS '=' QSTRING ';'
721 clientmaxlinks: MAXLINKS '=' expr ';'
728 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
731 if (dconf->hostmask != NULL)
733 if (dconf->usermask == NULL)
734 DupString(dconf->usermask, "*");
735 dconf->next = denyConfList;
736 denyConfList = dconf;
740 MyFree(dconf->hostmask);
741 MyFree(dconf->message);
743 parse_error("Bad kill block");
747 killitems: killitem killitems | killitem;
748 killitem: killuhost | killreal | killreasonfile | killreason | error;
749 killuhost: HOST '=' QSTRING ';'
752 dconf->flags &= ~DENY_FLAGS_REALNAME;
753 MyFree(dconf->hostmask);
754 MyFree(dconf->usermask);
755 if ((h = strchr($3, '@')) == NULL)
765 DupString(dconf->hostmask, h);
766 DupString(dconf->usermask, u);
767 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
770 killreal: REAL '=' QSTRING ';'
772 dconf->flags &= ~DENY_FLAGS_IP;
773 dconf->flags |= DENY_FLAGS_REALNAME;
774 MyFree(dconf->hostmask);
775 /* Leave usermask so you can specify user and real... */
776 DupString(dconf->hostmask, $3);
779 killreason: REASON '=' QSTRING ';'
781 dconf->flags &= ~DENY_FLAGS_FILE;
782 MyFree(dconf->message);
783 DupString(dconf->message, $3);
786 killreasonfile: TFILE '=' QSTRING ';'
788 dconf->flags |= DENY_FLAGS_FILE;
789 MyFree(dconf->message);
790 DupString(dconf->message, $3);
796 } '{' cruleitems '}' ';'
798 struct CRuleNode *node;
799 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
801 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
806 p->next = cruleConfList;
813 parse_error("Bad CRule block");
819 cruleitems: cruleitem cruleitems | cruleitem;
820 cruleitem: cruleserver | crulerule | cruleall | error;
822 cruleserver: SERVER '=' QSTRING ';'
829 crulerule: RULE '=' QSTRING ';'
835 cruleall: ALL '=' YES ';'
843 motdblock: MOTD '{' motditems '}' ';'
845 if (host != NULL && pass != NULL)
846 motd_add(host, pass);
852 motditems: motditem motditems | motditem;
853 motditem: motdhost | motdfile | error;
854 motdhost: HOST '=' QSTRING ';'
859 motdfile: TFILE '=' QSTRING ';'
864 featuresblock: FEATURES '{' featureitems '}' ';';
865 featureitems: featureitems featureitem | featureitem;
871 } '=' stringlist ';';
879 feature_set(NULL, (const char * const *)stringlist, stringno);
881 posextrastrings: /* empty */ | extrastrings;
882 extrastrings: extrastrings extrastring | extrastring;
885 if (stringno < MAX_STRINGS)
886 stringlist[stringno++] = $1;
889 quarantineblock: QUARANTINE '{'
891 qconf = (struct qline*) MyCalloc(1, sizeof(*qconf));
892 } quarantineitems '}' ';'
894 if (qconf->chname == NULL || qconf->reason == NULL)
896 parse_error("quarantine blocks need a channel name and a reason.");
899 qconf->next = GlobalQuarantineList;
900 GlobalQuarantineList = qconf;
904 quarantineitems: CHANNEL NAME '=' QSTRING ';'
906 DupString(qconf->chname, $4);
907 } | REASON '=' QSTRING ';'
909 DupString(qconf->reason, $3);
912 pseudoblock: PSEUDO QSTRING '{'
914 smap = MyCalloc(1, sizeof(struct s_map));
915 DupString(smap->command, $2);
919 if (!smap->name || !smap->services)
921 parse_error("pseudo commands need a service name and list of target nicks.");
924 if (register_mapping(smap))
926 smap->next = GlobalServiceMapList;
927 GlobalServiceMapList = smap;
931 struct nick_host *nh, *next;
932 for (nh = smap->services; nh; nh = next)
938 MyFree(smap->command);
939 MyFree(smap->prepend);
945 pseudoitems: pseudoitem pseudoitems | pseudoitem;
946 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
947 pseudoname: NAME '=' QSTRING ';'
949 DupString(smap->name, $3);
951 pseudoprepend: PREPEND '=' QSTRING ';'
953 DupString(smap->prepend, $3);
955 pseudonick: NICK '=' QSTRING ';'
957 char *sep = strchr($3, '@');
960 size_t slen = strlen($3);
961 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
962 memcpy(nh->nick, $3, slen + 1);
963 nh->nicklen = sep - $3;
964 nh->next = smap->services;
969 iauthblock: IAUTH '{'
975 if (!host || !port) {
976 parse_error("IAuth block needs a server name and port.");
979 iauth_connect(host, port, pass, tconn, tping);
983 port = tconn = tping = 0;
986 iauthitems: iauthitem iauthitems | iauthitem;
987 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
988 iauthpass: PASS '=' QSTRING ';'
993 iauthhost: HOST '=' QSTRING ';'
998 iauthport: PORT '=' NUMBER ';'
1002 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1006 iauthtimeout: TIMEOUT '=' timespec ';'