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
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 ircd_aton(&VirtualHost.addr, $3);
305 adminblock: ADMIN '{' adminitems '}'
307 if (localConf.location1 == NULL)
308 DupString(localConf.location1, "");
309 if (localConf.location2 == NULL)
310 DupString(localConf.location2, "");
311 if (localConf.contact == NULL)
312 DupString(localConf.contact, "");
314 adminitems: adminitems adminitem | adminitem;
315 adminitem: adminlocation | admincontact | error;
316 adminlocation: LOCATION '=' QSTRING ';'
318 if (localConf.location1 == NULL)
319 DupString(localConf.location1, $3);
320 else if (localConf.location2 == NULL)
321 DupString(localConf.location2, $3);
322 /* Otherwise just drop it. -A1kmm */
324 admincontact: CONTACT '=' QSTRING ';'
326 if (localConf.contact != NULL)
327 MyFree(localConf.contact);
328 DupString(localConf.contact, $3);
337 struct ConnectionClass *c_class;
338 add_class(name, tping, tconn, maxlinks, sendq);
339 c_class = find_class(name);
340 c_class->default_umode = pass;
341 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
342 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
345 parse_error("Missing name in class block");
352 memset(&privs, 0, sizeof(privs));
353 memset(&privs_dirty, 0, sizeof(privs_dirty));
355 classitems: classitem classitems | classitem;
356 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
357 classsendq | classusermode | priv | error;
358 classname: NAME '=' QSTRING ';'
363 classpingfreq: PINGFREQ '=' timespec ';'
367 classconnfreq: CONNECTFREQ '=' timespec ';'
371 classmaxlinks: MAXLINKS '=' expr ';'
375 classsendq: SENDQ '=' sizespec ';'
379 classusermode: USERMODE '=' QSTRING ';'
385 connectblock: CONNECT
388 } '{' connectitems '}'
390 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
391 && !strchr(host, '*') && !strchr(host, '?'))
393 struct ConfItem *aconf = make_conf(CONF_SERVER);
395 aconf->origin_name = origin;
396 aconf->passwd = pass;
397 aconf->conn_class = c_class;
398 aconf->address.port = port;
400 aconf->maximum = maxlinks;
401 aconf->hub_limit = hub_limit;
402 lookup_confhost(aconf);
411 parse_error("Bad connect block");
413 name = pass = host = origin = hub_limit = NULL;
417 connectitems: connectitem connectitems | connectitem;
418 connectitem: connectname | connectpass | connectclass | connecthost
419 | connectport | connectvhost | connectleaf | connecthub
420 | connecthublimit | connectmaxhops | error;
421 connectname: NAME '=' QSTRING ';'
426 connectpass: PASS '=' QSTRING ';'
431 connectclass: CLASS '=' QSTRING ';'
433 c_class = find_class($3);
435 connecthost: HOST '=' QSTRING ';'
440 connectport: PORT '=' NUMBER ';'
444 connectvhost: VHOST '=' QSTRING ';'
447 DupString(origin, $3);
449 connectleaf: LEAF ';'
456 DupString(hub_limit, "*");
458 connecthublimit: HUB '=' QSTRING ';'
461 DupString(hub_limit, $3);
463 connectmaxhops: MAXHOPS '=' expr ';'
468 uworldblock: UWORLD '{' uworlditems '}' ';'
472 struct ConfItem *aconf = make_conf(CONF_UWORLD);
478 parse_error("Bad UWorld block");
482 uworlditems: uworlditem uworlditems | uworlditem;
483 uworlditem: uworldname | error;
484 uworldname: NAME '=' QSTRING ';'
490 operblock: OPER '{' operitems '}' ';'
492 if (name && pass && host && c_class)
494 struct ConfItem *aconf = make_conf(CONF_OPERATOR);
496 aconf->passwd = pass;
497 conf_parse_userhost(aconf, host);
498 aconf->conn_class = c_class;
499 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
500 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
501 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
502 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
503 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
507 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
512 name = pass = host = NULL;
514 memset(&privs, 0, sizeof(privs));
515 memset(&privs_dirty, 0, sizeof(privs_dirty));
517 operitems: operitem | operitems operitem;
518 operitem: opername | operpass | operhost | operclass | priv | error;
519 opername: NAME '=' QSTRING ';'
524 operpass: PASS '=' QSTRING ';'
529 operhost: HOST '=' QSTRING ';'
532 if (!strchr($3, '@'))
535 char *b = (char*) MyMalloc((uh_len = strlen($3)+3));
536 ircd_snprintf(0, b, uh_len, "*@%s", $3);
542 operclass: CLASS '=' QSTRING ';'
544 c_class = find_class($3);
547 priv: privtype '=' yesorno ';'
549 FlagSet(&privs_dirty, $1);
550 if (($3 == 1) ^ invert)
557 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
558 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
559 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
560 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
561 KILL { $$ = PRIV_KILL; } |
562 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
563 TPRIV_REHASH { $$ = PRIV_REHASH; } |
564 TPRIV_RESTART { $$ = PRIV_RESTART; } |
565 TPRIV_DIE { $$ = PRIV_DIE; } |
566 TPRIV_GLINE { $$ = PRIV_GLINE; } |
567 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
568 JUPE { $$ = PRIV_JUPE; } |
569 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
570 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
571 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
572 TPRIV_SET { $$ = PRIV_SET; } |
573 TPRIV_WHOX { $$ = PRIV_WHOX; } |
574 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
575 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
576 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
577 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
578 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
579 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
580 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
581 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
582 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
583 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
584 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
585 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
586 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
588 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
590 /* The port block... */
591 portblock: PORT '{' portitems '}' ';'
593 if (port > 0 && port <= 0xFFFF)
595 add_listener(port, host, pass, tconn, tping);
599 parse_error("Bad port block");
604 port = tconn = tping = 0;
606 portitems: portitem portitems | portitem;
607 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
608 portnumber: PORT '=' NUMBER ';'
613 portvhost: VHOST '=' QSTRING ';'
619 portmask: MASK '=' QSTRING ';'
625 portserver: SERVER '=' YES ';'
628 } | SERVER '=' NO ';'
633 porthidden: HIDDEN '=' YES ';'
636 } | HIDDEN '=' NO ';'
645 '{' clientitems '}' ';'
647 struct ConfItem *aconf = make_conf(CONF_CLIENT);
648 unsigned char addrbits;
649 aconf->username = username;
651 if (ip && ipmask_parse(ip, &aconf->address.addr, &addrbits))
652 aconf->addrbits = addrbits;
654 aconf->addrbits = -1;
655 aconf->conn_class = c_class ? c_class : find_class("default");
656 aconf->maximum = maxlinks;
662 clientitems: clientitem clientitems | clientitem;
663 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | error;
664 clienthost: HOST '=' QSTRING ';'
669 clientip: IP '=' QSTRING ';'
674 clientusername: USERNAME '=' QSTRING ';'
677 DupString(username, $3);
679 clientclass: CLASS '=' QSTRING ';'
681 c_class = find_class($3);
683 clientpass: PASS '=' QSTRING ';'
688 clientmaxlinks: MAXLINKS '=' expr ';'
695 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
698 if (dconf->hostmask != NULL)
700 if (dconf->usermask == NULL)
701 DupString(dconf->usermask, "*");
702 dconf->next = denyConfList;
703 denyConfList = dconf;
707 MyFree(dconf->hostmask);
708 MyFree(dconf->message);
710 parse_error("Bad kill block");
714 killitems: killitem killitems | killitem;
715 killitem: killuhost | killreal | killreasonfile | killreason | error;
716 killuhost: HOST '=' QSTRING ';'
719 dconf->flags &= ~DENY_FLAGS_REALNAME;
720 MyFree(dconf->hostmask);
721 MyFree(dconf->usermask);
722 if ((h = strchr($3, '@')) == NULL)
732 DupString(dconf->hostmask, h);
733 DupString(dconf->usermask, u);
734 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
737 killreal: REAL '=' QSTRING ';'
739 dconf->flags &= ~DENY_FLAGS_IP;
740 dconf->flags |= DENY_FLAGS_REALNAME;
741 MyFree(dconf->hostmask);
742 /* Leave usermask so you can specify user and real... */
743 DupString(dconf->hostmask, $3);
746 killreason: REASON '=' QSTRING ';'
748 dconf->flags &= DENY_FLAGS_FILE;
749 MyFree(dconf->message);
750 DupString(dconf->message, $3);
753 killreasonfile: TFILE '=' QSTRING ';'
755 dconf->flags |= DENY_FLAGS_FILE;
756 MyFree(dconf->message);
757 DupString(dconf->message, $3);
763 } '{' cruleitems '}' ';'
765 struct CRuleNode *node;
766 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
768 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
773 p->next = cruleConfList;
780 parse_error("Bad CRule block");
785 cruleitems: cruleitem cruleitems | cruleitem;
786 cruleitem: cruleserver | crulerule | cruleall | error;
788 cruleserver: SERVER '=' QSTRING ';'
795 crulerule: RULE '=' QSTRING ';'
801 cruleall: ALL '=' YES ';'
809 motdblock: MOTD '{' motditems '}' ';'
811 if (host != NULL && pass != NULL)
812 motd_add(host, pass);
818 motditems: motditem motditems | motditem;
819 motditem: motdhost | motdfile | error;
820 motdhost: HOST '=' QSTRING ';'
825 motdfile: TFILE '=' QSTRING ';'
830 featuresblock: FEATURES '{' featureitems '}' ';';
831 featureitems: featureitems featureitem | featureitem;
837 } '=' stringlist ';';
845 feature_set(NULL, (const char * const *)stringlist, stringno);
847 posextrastrings: /* empty */ | extrastrings;
848 extrastrings: extrastrings extrastring | extrastring;
851 if (stringno < MAX_STRINGS)
852 stringlist[stringno++] = $1;
855 quarantineblock: QUARANTINE '{'
857 qconf = (struct qline*) MyCalloc(1, sizeof(*qconf));
858 } quarantineitems '}' ';'
860 if (qconf->chname == NULL || qconf->reason == NULL)
862 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
866 qconf->next = GlobalQuarantineList;
867 GlobalQuarantineList = qconf;
871 quarantineitems: CHANNEL NAME '=' QSTRING ';'
873 DupString(qconf->chname, $4);
874 } | REASON '=' QSTRING ';'
876 DupString(qconf->reason, $3);
879 pseudoblock: PSEUDO QSTRING '{'
881 smap = MyCalloc(1, sizeof(struct s_map));
882 DupString(smap->command, $2);
886 if (!smap->name || !smap->services)
888 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
891 if (register_mapping(smap))
893 smap->next = GlobalServiceMapList;
894 GlobalServiceMapList = smap;
898 struct nick_host *nh, *next;
899 for (nh = smap->services; nh; nh = next)
905 MyFree(smap->command);
906 MyFree(smap->prepend);
912 pseudoitems: pseudoitem pseudoitems | pseudoitem;
913 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
914 pseudoname: NAME '=' QSTRING ';'
916 DupString(smap->name, $3);
918 pseudoprepend: PREPEND '=' QSTRING ';'
920 DupString(smap->prepend, $3);
922 pseudonick: NICK '=' QSTRING ';'
924 char *sep = strchr($3, '@');
927 size_t slen = strlen($3);
928 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
929 memcpy(nh->nick, $3, slen + 1);
930 nh->nicklen = sep - $3;
931 nh->next = smap->services;
936 iauthblock: IAUTH '{'
942 if (!host || !port) {
943 log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
946 iauth_connect(host, port, pass, tconn, tping);
953 iauthitems: iauthitem iauthitems | iauthitem;
954 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
955 iauthpass: PASS '=' QSTRING ';'
960 iauthhost: HOST '=' QSTRING ';'
965 iauthport: PORT '=' NUMBER ';'
969 iauthconnfreq: CONNECTFREQ '=' timespec ';'
973 iauthtimeout: TIMEOUT '=' timespec ';'