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 priviledges... */
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 arithmatic expression */
248 /* leave this out until we find why it makes BSD yacc dump core -larne
258 jupeblock: JUPE '{' jupeitems '}' ';' ;
259 jupeitems: jupeitem jupeitems | jupeitem;
260 jupeitem: jupenick | error;
261 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 | error;
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)
287 DupString(localConf.name, $3);
288 else if (strcmp(localConf.name, $3))
289 parse_error("Redefinition of server name %s (%s)", $3,
293 generaldesc: DESCRIPTION '=' QSTRING ';'
295 MyFree(localConf.description);
296 DupString(localConf.description, $3);
297 ircd_strncpy(cli_info(&me), $3, REALLEN);
300 generalvhost: VHOST '=' QSTRING ';'
302 struct irc_in_addr addr;
303 if (!ircd_aton(&addr, $3))
304 parse_error("Invalid virtual host '%s'.", $3);
305 else if (irc_in_addr_is_ipv4(&addr))
306 memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
308 memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
311 adminblock: ADMIN '{' adminitems '}'
313 if (localConf.location1 == NULL)
314 DupString(localConf.location1, "");
315 if (localConf.location2 == NULL)
316 DupString(localConf.location2, "");
317 if (localConf.contact == NULL)
318 DupString(localConf.contact, "");
320 adminitems: adminitems adminitem | adminitem;
321 adminitem: adminlocation | admincontact | error;
322 adminlocation: LOCATION '=' QSTRING ';'
324 if (localConf.location1 == NULL)
325 DupString(localConf.location1, $3);
326 else if (localConf.location2 == NULL)
327 DupString(localConf.location2, $3);
328 /* Otherwise just drop it. -A1kmm */
330 admincontact: CONTACT '=' QSTRING ';'
332 if (localConf.contact != NULL)
333 MyFree(localConf.contact);
334 DupString(localConf.contact, $3);
343 struct ConnectionClass *c_class;
344 add_class(name, tping, tconn, maxlinks, sendq);
345 c_class = find_class(name);
346 c_class->default_umode = pass;
347 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
348 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
351 parse_error("Missing name in class block");
358 memset(&privs, 0, sizeof(privs));
359 memset(&privs_dirty, 0, sizeof(privs_dirty));
361 classitems: classitem classitems | classitem;
362 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
363 classsendq | classusermode | priv | error;
364 classname: NAME '=' QSTRING ';'
369 classpingfreq: PINGFREQ '=' timespec ';'
373 classconnfreq: CONNECTFREQ '=' timespec ';'
377 classmaxlinks: MAXLINKS '=' expr ';'
381 classsendq: SENDQ '=' sizespec ';'
385 classusermode: USERMODE '=' QSTRING ';'
391 connectblock: CONNECT
394 } '{' connectitems '}'
396 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
397 && !strchr(host, '*') && !strchr(host, '?'))
399 struct ConfItem *aconf = make_conf(CONF_SERVER);
401 aconf->origin_name = origin;
402 aconf->passwd = pass;
403 aconf->conn_class = c_class;
404 aconf->address.port = port;
406 aconf->maximum = maxlinks;
407 aconf->hub_limit = hub_limit;
408 lookup_confhost(aconf);
417 parse_error("Bad connect block");
419 name = pass = host = origin = hub_limit = NULL;
423 connectitems: connectitem connectitems | connectitem;
424 connectitem: connectname | connectpass | connectclass | connecthost
425 | connectport | connectvhost | connectleaf | connecthub
426 | connecthublimit | connectmaxhops | error;
427 connectname: NAME '=' QSTRING ';'
432 connectpass: PASS '=' QSTRING ';'
437 connectclass: CLASS '=' QSTRING ';'
439 c_class = find_class($3);
441 connecthost: HOST '=' QSTRING ';'
446 connectport: PORT '=' NUMBER ';'
450 connectvhost: VHOST '=' QSTRING ';'
453 DupString(origin, $3);
455 connectleaf: LEAF ';'
462 DupString(hub_limit, "*");
464 connecthublimit: HUB '=' QSTRING ';'
467 DupString(hub_limit, $3);
469 connectmaxhops: MAXHOPS '=' expr ';'
474 uworldblock: UWORLD '{' uworlditems '}' ';'
478 struct ConfItem *aconf = make_conf(CONF_UWORLD);
484 parse_error("Bad UWorld block");
488 uworlditems: uworlditem uworlditems | uworlditem;
489 uworlditem: uworldname | error;
490 uworldname: NAME '=' QSTRING ';'
496 operblock: OPER '{' operitems '}' ';'
498 if (name && pass && host && c_class)
500 struct ConfItem *aconf = make_conf(CONF_OPERATOR);
502 aconf->passwd = pass;
503 conf_parse_userhost(aconf, host);
504 aconf->conn_class = c_class;
505 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
506 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
507 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
508 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
509 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
513 parse_error("operator blocks need a name, password, class and host.");
518 name = pass = host = NULL;
520 memset(&privs, 0, sizeof(privs));
521 memset(&privs_dirty, 0, sizeof(privs_dirty));
523 operitems: operitem | operitems operitem;
524 operitem: opername | operpass | operhost | operclass | priv | error;
525 opername: NAME '=' QSTRING ';'
530 operpass: PASS '=' QSTRING ';'
535 operhost: HOST '=' QSTRING ';'
538 if (!strchr($3, '@'))
541 char *b = (char*) MyMalloc((uh_len = strlen($3)+3));
542 ircd_snprintf(0, b, uh_len, "*@%s", $3);
548 operclass: CLASS '=' QSTRING ';'
550 c_class = find_class($3);
553 priv: privtype '=' yesorno ';'
555 FlagSet(&privs_dirty, $1);
556 if (($3 == 1) ^ invert)
563 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
564 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
565 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
566 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
567 KILL { $$ = PRIV_KILL; } |
568 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
569 TPRIV_REHASH { $$ = PRIV_REHASH; } |
570 TPRIV_RESTART { $$ = PRIV_RESTART; } |
571 TPRIV_DIE { $$ = PRIV_DIE; } |
572 TPRIV_GLINE { $$ = PRIV_GLINE; } |
573 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
574 JUPE { $$ = PRIV_JUPE; } |
575 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
576 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
577 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
578 TPRIV_SET { $$ = PRIV_SET; } |
579 TPRIV_WHOX { $$ = PRIV_WHOX; } |
580 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
581 TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
582 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
583 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
584 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
585 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
586 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
587 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
588 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
589 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
590 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
591 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
592 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
593 TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
595 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
597 /* The port block... */
598 portblock: PORT '{' portitems '}' ';'
600 if (port > 0 && port <= 0xFFFF)
602 add_listener(port, host, pass, tconn, tping);
606 parse_error("Bad port block");
611 port = tconn = tping = 0;
613 portitems: portitem portitems | portitem;
614 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
615 portnumber: PORT '=' NUMBER ';'
620 portvhost: VHOST '=' QSTRING ';'
626 portmask: MASK '=' QSTRING ';'
632 portserver: SERVER '=' YES ';'
635 } | SERVER '=' NO ';'
640 porthidden: HIDDEN '=' YES ';'
643 } | HIDDEN '=' NO ';'
652 '{' clientitems '}' ';'
654 struct irc_in_addr addr;
655 unsigned char addrbits;
657 if (ip && !ipmask_parse(ip, &addr, &addrbits)) {
658 parse_error("Invalid IP address in block");
663 struct ConfItem *aconf = make_conf(CONF_CLIENT);
664 aconf->username = username;
666 memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
667 aconf->addrbits = addrbits;
669 aconf->conn_class = c_class ? c_class : find_class("default");
670 aconf->maximum = maxlinks;
677 clientitems: clientitem clientitems | clientitem;
678 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | error;
679 clienthost: HOST '=' QSTRING ';'
681 char *sep = strchr($3, '@');
686 DupString(username, $3);
687 DupString(host, sep);
692 clientip: IP '=' QSTRING ';'
694 char *sep = strchr($3, '@');
699 DupString(username, $3);
705 clientusername: USERNAME '=' QSTRING ';'
708 DupString(username, $3);
710 clientclass: CLASS '=' QSTRING ';'
712 c_class = find_class($3);
714 clientpass: PASS '=' QSTRING ';'
719 clientmaxlinks: MAXLINKS '=' expr ';'
726 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
729 if (dconf->hostmask != NULL)
731 if (dconf->usermask == NULL)
732 DupString(dconf->usermask, "*");
733 dconf->next = denyConfList;
734 denyConfList = dconf;
738 MyFree(dconf->hostmask);
739 MyFree(dconf->message);
741 parse_error("Bad kill block");
745 killitems: killitem killitems | killitem;
746 killitem: killuhost | killreal | killreasonfile | killreason | error;
747 killuhost: HOST '=' QSTRING ';'
750 dconf->flags &= ~DENY_FLAGS_REALNAME;
751 MyFree(dconf->hostmask);
752 MyFree(dconf->usermask);
753 if ((h = strchr($3, '@')) == NULL)
763 DupString(dconf->hostmask, h);
764 DupString(dconf->usermask, u);
765 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
768 killreal: REAL '=' QSTRING ';'
770 dconf->flags &= ~DENY_FLAGS_IP;
771 dconf->flags |= DENY_FLAGS_REALNAME;
772 MyFree(dconf->hostmask);
773 /* Leave usermask so you can specify user and real... */
774 DupString(dconf->hostmask, $3);
777 killreason: REASON '=' QSTRING ';'
779 dconf->flags &= ~DENY_FLAGS_FILE;
780 MyFree(dconf->message);
781 DupString(dconf->message, $3);
784 killreasonfile: TFILE '=' QSTRING ';'
786 dconf->flags |= DENY_FLAGS_FILE;
787 MyFree(dconf->message);
788 DupString(dconf->message, $3);
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 ';';
877 feature_set(NULL, (const char * const *)stringlist, stringno);
879 posextrastrings: /* empty */ | extrastrings;
880 extrastrings: extrastrings extrastring | extrastring;
883 if (stringno < MAX_STRINGS)
884 stringlist[stringno++] = $1;
887 quarantineblock: QUARANTINE '{'
889 qconf = (struct qline*) MyCalloc(1, sizeof(*qconf));
890 } quarantineitems '}' ';'
892 if (qconf->chname == NULL || qconf->reason == NULL)
894 parse_error("quarantine blocks need a channel name and a reason.");
897 qconf->next = GlobalQuarantineList;
898 GlobalQuarantineList = qconf;
902 quarantineitems: CHANNEL NAME '=' QSTRING ';'
904 DupString(qconf->chname, $4);
905 } | REASON '=' QSTRING ';'
907 DupString(qconf->reason, $3);
910 pseudoblock: PSEUDO QSTRING '{'
912 smap = MyCalloc(1, sizeof(struct s_map));
913 DupString(smap->command, $2);
917 if (!smap->name || !smap->services)
919 parse_error("pseudo commands need a service name and list of target nicks.");
922 if (register_mapping(smap))
924 smap->next = GlobalServiceMapList;
925 GlobalServiceMapList = smap;
929 struct nick_host *nh, *next;
930 for (nh = smap->services; nh; nh = next)
936 MyFree(smap->command);
937 MyFree(smap->prepend);
943 pseudoitems: pseudoitem pseudoitems | pseudoitem;
944 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
945 pseudoname: NAME '=' QSTRING ';'
947 DupString(smap->name, $3);
949 pseudoprepend: PREPEND '=' QSTRING ';'
951 DupString(smap->prepend, $3);
953 pseudonick: NICK '=' QSTRING ';'
955 char *sep = strchr($3, '@');
958 size_t slen = strlen($3);
959 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
960 memcpy(nh->nick, $3, slen + 1);
961 nh->nicklen = sep - $3;
962 nh->next = smap->services;
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 ';'