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;
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);
158 /* and now a lot of privileges... */
159 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
160 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
161 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
162 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
163 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
164 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
165 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE TPRIV_APASS_OPMODE
166 /* and some types... */
168 %type <num> timespec timefactor factoredtimes factoredtime
169 %type <num> expr yesorno privtype
179 /* Blocks in the config file... */
180 blocks: blocks block | block;
181 block: adminblock | generalblock | classblock | connectblock |
182 uworldblock | operblock | portblock | jupeblock | clientblock |
183 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
184 pseudoblock | iauthblock | error;
186 /* The timespec, sizespec and expr was ripped straight from
188 timespec: expr | factoredtimes;
190 factoredtimes: factoredtimes factoredtime
195 factoredtime: expr timefactor
200 timefactor: SECONDS { $$ = 1; }
201 | MINUTES { $$ = 60; }
202 | HOURS { $$ = 60 * 60; }
203 | DAYS { $$ = 60 * 60 * 24; }
204 | WEEKS { $$ = 60 * 60 * 24 * 7; }
205 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
206 | YEARS { $$ = 60 * 60 * 24 * 365; }
207 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
220 $$ = $1 * 1024 * 1024;
223 $$ = $1 * 1024 * 1024 * 1024;
226 $$ = $1 * 1024 * 1024 * 1024;
230 /* this is an arithmetic expression */
247 /* leave this out until we find why it makes BSD yacc dump core -larne
248 | '-' expr %prec NEG {
256 jupeblock: JUPE '{' jupeitems '}' ';' ;
257 jupeitems: jupeitem jupeitems | jupeitem;
258 jupeitem: jupenick | error;
259 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)
288 if (strcmp(localConf.name, $3))
289 parse_error("Redefinition of server name %s (%s)", $3,
295 generaldesc: DESCRIPTION '=' QSTRING ';'
297 MyFree(localConf.description);
298 localConf.description = $3;
299 ircd_strncpy(cli_info(&me), $3, REALLEN);
302 generalvhost: VHOST '=' QSTRING ';'
304 struct irc_in_addr addr;
305 if (!ircd_aton(&addr, $3))
306 parse_error("Invalid virtual host '%s'.", $3);
307 else if (irc_in_addr_is_ipv4(&addr))
308 memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
310 memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
314 adminblock: ADMIN '{' adminitems '}'
316 if (localConf.location1 == NULL)
317 DupString(localConf.location1, "");
318 if (localConf.location2 == NULL)
319 DupString(localConf.location2, "");
320 if (localConf.contact == NULL)
321 DupString(localConf.contact, "");
323 adminitems: adminitems adminitem | adminitem;
324 adminitem: adminlocation | admincontact | error;
325 adminlocation: LOCATION '=' QSTRING ';'
327 if (localConf.location1 == NULL)
328 localConf.location1 = $3;
329 else if (localConf.location2 == NULL)
330 localConf.location2 = $3;
331 else /* Otherwise just drop it. -A1kmm */
334 admincontact: CONTACT '=' QSTRING ';'
336 MyFree(localConf.contact);
337 localConf.contact = $3;
346 struct ConnectionClass *c_class;
347 add_class(name, tping, tconn, maxlinks, sendq);
348 c_class = find_class(name);
349 MyFree(c_class->default_umode);
350 c_class->default_umode = pass;
351 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
352 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
355 parse_error("Missing name in class block");
362 memset(&privs, 0, sizeof(privs));
363 memset(&privs_dirty, 0, sizeof(privs_dirty));
365 classitems: classitem classitems | classitem;
366 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
367 classsendq | classusermode | priv | error;
368 classname: NAME '=' QSTRING ';'
373 classpingfreq: PINGFREQ '=' timespec ';'
377 classconnfreq: CONNECTFREQ '=' timespec ';'
381 classmaxlinks: MAXLINKS '=' expr ';'
385 classsendq: SENDQ '=' sizespec ';'
389 classusermode: USERMODE '=' QSTRING ';'
395 connectblock: CONNECT
398 } '{' connectitems '}'
400 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
401 && !strchr(host, '*') && !strchr(host, '?'))
403 struct ConfItem *aconf = make_conf(CONF_SERVER);
405 aconf->origin_name = origin;
406 aconf->passwd = pass;
407 aconf->conn_class = c_class;
408 aconf->address.port = port;
410 aconf->maximum = maxlinks;
411 aconf->hub_limit = hub_limit;
412 lookup_confhost(aconf);
421 parse_error("Bad connect block");
423 name = pass = host = origin = hub_limit = NULL;
427 connectitems: connectitem connectitems | connectitem;
428 connectitem: connectname | connectpass | connectclass | connecthost
429 | connectport | connectvhost | connectleaf | connecthub
430 | connecthublimit | connectmaxhops | error;
431 connectname: NAME '=' QSTRING ';'
436 connectpass: PASS '=' QSTRING ';'
441 connectclass: CLASS '=' QSTRING ';'
443 c_class = find_class($3);
446 connecthost: HOST '=' QSTRING ';'
451 connectport: PORT '=' NUMBER ';'
455 connectvhost: VHOST '=' QSTRING ';'
460 connectleaf: LEAF ';'
467 DupString(hub_limit, "*");
469 connecthublimit: HUB '=' QSTRING ';'
474 connectmaxhops: MAXHOPS '=' expr ';'
479 uworldblock: UWORLD '{' uworlditems '}' ';';
480 uworlditems: uworlditem uworlditems | uworlditem;
481 uworlditem: uworldname | error;
482 uworldname: NAME '=' QSTRING ';'
484 make_conf(CONF_UWORLD)->host = $3;
487 operblock: OPER '{' operitems '}' ';'
489 if (name && pass && host && c_class)
491 struct ConfItem *aconf = make_conf(CONF_OPERATOR);
493 aconf->passwd = pass;
494 conf_parse_userhost(aconf, host);
495 aconf->conn_class = c_class;
496 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
497 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
498 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
499 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
500 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
504 parse_error("operator blocks need a name, password, class and host.");
509 name = pass = host = NULL;
511 memset(&privs, 0, sizeof(privs));
512 memset(&privs_dirty, 0, sizeof(privs_dirty));
514 operitems: operitem | operitems operitem;
515 operitem: opername | operpass | operhost | operclass | priv | error;
516 opername: NAME '=' QSTRING ';'
521 operpass: PASS '=' QSTRING ';'
526 operhost: HOST '=' QSTRING ';'
529 if (!strchr($3, '@'))
532 host = (char*) MyMalloc((uh_len = strlen($3)+3));
533 ircd_snprintf(0, host, uh_len, "*@%s", $3);
539 operclass: CLASS '=' QSTRING ';'
541 c_class = find_class($3);
545 priv: privtype '=' yesorno ';'
547 FlagSet(&privs_dirty, $1);
548 if (($3 == 1) ^ invert)
555 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
556 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
557 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
558 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
559 KILL { $$ = PRIV_KILL; } |
560 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
561 TPRIV_REHASH { $$ = PRIV_REHASH; } |
562 TPRIV_RESTART { $$ = PRIV_RESTART; } |
563 TPRIV_DIE { $$ = PRIV_DIE; } |
564 TPRIV_GLINE { $$ = PRIV_GLINE; } |
565 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
566 JUPE { $$ = PRIV_JUPE; } |
567 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
568 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
569 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
570 TPRIV_SET { $$ = PRIV_SET; } |
571 TPRIV_WHOX { $$ = PRIV_WHOX; } |
572 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
573 TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
574 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
575 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
576 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
577 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
578 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
579 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
580 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
581 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
582 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
583 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
584 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
585 TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
587 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
589 /* The port block... */
590 portblock: PORT '{' portitems '}' ';'
592 if (port > 0 && port <= 0xFFFF)
594 add_listener(port, host, pass, tconn, tping);
598 parse_error("Bad port block");
603 port = tconn = tping = 0;
605 portitems: portitem portitems | portitem;
606 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
607 portnumber: PORT '=' NUMBER ';'
612 portvhost: VHOST '=' QSTRING ';'
618 portmask: MASK '=' QSTRING ';'
624 portserver: SERVER '=' YES ';'
627 } | SERVER '=' NO ';'
632 porthidden: HIDDEN '=' YES ';'
635 } | HIDDEN '=' NO ';'
644 '{' clientitems '}' ';'
646 struct irc_in_addr addr;
647 unsigned char addrbits = 0;
649 if (ip && !ipmask_parse(ip, &addr, &addrbits)) {
650 parse_error("Invalid IP address in block");
656 struct ConfItem *aconf = make_conf(CONF_CLIENT);
657 aconf->username = username;
660 memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
662 memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
663 aconf->addrbits = addrbits;
665 aconf->conn_class = c_class ? c_class : find_class("default");
666 aconf->maximum = maxlinks;
667 aconf->passwd = pass;
675 clientitems: clientitem clientitems | clientitem;
676 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | error;
677 clienthost: HOST '=' QSTRING ';'
679 char *sep = strchr($3, '@');
684 DupString(host, sep);
690 clientip: IP '=' QSTRING ';'
693 sep = strchr($3, '@');
704 clientusername: USERNAME '=' QSTRING ';'
709 clientclass: CLASS '=' QSTRING ';'
711 c_class = find_class($3);
714 clientpass: PASS '=' QSTRING ';'
719 clientmaxlinks: MAXLINKS '=' expr ';'
726 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
729 if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
730 dconf->next = denyConfList;
731 denyConfList = dconf;
735 MyFree(dconf->usermask);
736 MyFree(dconf->hostmask);
737 MyFree(dconf->realmask);
738 MyFree(dconf->message);
740 parse_error("Bad kill block");
744 killitems: killitem killitems | killitem;
745 killitem: killuhost | killreal | killusername | killreasonfile | killreason | error;
746 killuhost: HOST '=' QSTRING ';'
749 MyFree(dconf->hostmask);
750 MyFree(dconf->usermask);
751 if ((h = strchr($3, '@')) == NULL)
753 DupString(dconf->usermask, "*");
754 dconf->hostmask = $3;
759 DupString(dconf->hostmask, h);
760 dconf->usermask = $3;
762 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
765 killusername: USERNAME '=' QSTRING ';'
767 MyFree(dconf->usermask);
768 dconf->usermask = $3;
771 killreal: REAL '=' QSTRING ';'
773 MyFree(dconf->realmask);
774 dconf->realmask = $3;
777 killreason: REASON '=' QSTRING ';'
779 dconf->flags &= ~DENY_FLAGS_FILE;
780 MyFree(dconf->message);
784 killreasonfile: TFILE '=' QSTRING ';'
786 dconf->flags |= DENY_FLAGS_FILE;
787 MyFree(dconf->message);
794 } '{' cruleitems '}' ';'
796 struct CRuleNode *node;
797 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
799 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
804 p->next = cruleConfList;
811 parse_error("Bad CRule block");
817 cruleitems: cruleitem cruleitems | cruleitem;
818 cruleitem: cruleserver | crulerule | cruleall | error;
820 cruleserver: SERVER '=' QSTRING ';'
827 crulerule: RULE '=' QSTRING ';'
833 cruleall: ALL '=' YES ';'
841 motdblock: MOTD '{' motditems '}' ';'
843 if (host != NULL && pass != NULL)
844 motd_add(host, pass);
850 motditems: motditem motditems | motditem;
851 motditem: motdhost | motdfile | error;
852 motdhost: HOST '=' QSTRING ';'
857 motdfile: TFILE '=' QSTRING ';'
862 featuresblock: FEATURES '{' featureitems '}' ';';
863 featureitems: featureitems featureitem | featureitem;
869 } '=' stringlist ';';
878 feature_set(NULL, (const char * const *)stringlist, stringno);
879 for (ii = 0; ii < stringno; ++ii)
880 MyFree(stringlist[ii]);
882 posextrastrings: /* empty */ | extrastrings;
883 extrastrings: extrastrings extrastring | extrastring;
886 if (stringno < MAX_STRINGS)
887 stringlist[stringno++] = $1;
892 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
893 quarantineitems: quarantineitems quarantineitem | quarantineitem;
894 quarantineitem: QSTRING '=' QSTRING ';'
896 struct qline *qconf = MyCalloc(1, sizeof(*qconf));
899 qconf->next = GlobalQuarantineList;
900 GlobalQuarantineList = qconf;
903 pseudoblock: PSEUDO QSTRING '{'
905 smap = MyCalloc(1, sizeof(struct s_map));
910 if (!smap->name || !smap->services)
912 parse_error("pseudo commands need a service name and list of target nicks.");
915 if (register_mapping(smap))
917 smap->next = GlobalServiceMapList;
918 GlobalServiceMapList = smap;
922 struct nick_host *nh, *next;
923 for (nh = smap->services; nh; nh = next)
929 MyFree(smap->command);
930 MyFree(smap->prepend);
936 pseudoitems: pseudoitem pseudoitems | pseudoitem;
937 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags | error;
938 pseudoname: NAME '=' QSTRING ';'
943 pseudoprepend: PREPEND '=' QSTRING ';'
945 MyFree(smap->prepend);
948 pseudonick: NICK '=' QSTRING ';'
950 char *sep = strchr($3, '@');
953 size_t slen = strlen($3);
954 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
955 memcpy(nh->nick, $3, slen + 1);
956 nh->nicklen = sep - $3;
957 nh->next = smap->services;
962 pseudoflags: FAST ';'
964 smap->flags |= SMAP_FAST;
967 iauthblock: IAUTH '{'
973 if (!host || !port) {
974 parse_error("IAuth block needs a server name and port.");
977 iauth_connect(host, port, pass, tconn, tping);
981 port = tconn = tping = 0;
984 iauthitems: iauthitem iauthitems | iauthitem;
985 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
986 iauthpass: PASS '=' QSTRING ';'
991 iauthhost: HOST '=' QSTRING ';'
996 iauthport: PORT '=' NUMBER ';'
1000 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1004 iauthtimeout: TIMEOUT '=' timespec ';'