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"
61 #include <arpa/inet.h>
62 #define MAX_STRINGS 80 /* Maximum number of feature params. */
63 extern struct LocalConf localConf;
64 extern struct DenyConf* denyConfList;
65 extern struct CRuleConf* cruleConfList;
66 extern struct ServerConf* serverConfList;
67 extern struct s_map* GlobalServiceMapList;
68 extern struct qline* GlobalQuarantineList;
72 /* Now all the globals we need :/... */
73 int tping, tconn, maxlinks, sendq, port, invert;
75 char *name, *pass, *host;
76 char *stringlist[MAX_STRINGS];
77 struct ConnectionClass *c_class;
78 struct ConfItem *aconf;
79 struct DenyConf *dconf;
80 struct ServerConf *sconf;
81 struct qline *qconf = NULL;
84 struct Privs privs_dirty;
86 static void parse_error(char *pattern,...) {
87 static char error_buffer[1024];
90 ircd_vsnprintf(NULL, error_buffer, sizeof(error_buffer), pattern, vl);
92 yyerror(error_buffer);
163 /* and now a lot of priviledges... */
164 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
165 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
166 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
167 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
168 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
169 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
170 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE
171 /* and some types... */
173 %type <num> timespec timefactor factoredtimes factoredtime
174 %type <num> expr yesorno privtype
184 /* Blocks in the config file... */
185 blocks: blocks block | block;
186 block: adminblock | generalblock | classblock | connectblock |
187 serverblock | operblock | portblock | jupeblock | clientblock |
188 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
189 pseudoblock | iauthblock | error;
191 /* The timespec, sizespec and expr was ripped straight from
193 timespec: expr | factoredtimes;
195 factoredtimes: factoredtimes factoredtime
200 factoredtime: expr timefactor
205 timefactor: SECONDS { $$ = 1; }
206 | MINUTES { $$ = 60; }
207 | HOURS { $$ = 60 * 60; }
208 | DAYS { $$ = 60 * 60 * 24; }
209 | WEEKS { $$ = 60 * 60 * 24 * 7; }
210 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
211 | YEARS { $$ = 60 * 60 * 24 * 365; }
212 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
225 $$ = $1 * 1024 * 1024;
228 $$ = $1 * 1024 * 1024 * 1024;
231 $$ = $1 * 1024 * 1024 * 1024;
235 /* this is an arithmatic expression */
252 /* leave this out until we find why it makes BSD yacc dump core -larne
262 jupeblock: JUPE '{' jupeitems '}' ';' ;
263 jupeitems: jupeitem jupeitems | jupeitem;
264 jupeitem: jupenick | error;
265 jupenick: NICK '=' QSTRING
267 addNickJupes(yylval.text);
270 generalblock: GENERAL '{' generalitems '}' ';' ;
271 generalitems: generalitem generalitems | generalitem;
272 generalitem: generalnumeric | generalname | generalvhost | generaldesc | error;
273 generalnumeric: NUMERIC '=' NUMBER ';'
275 if (localConf.numeric == 0)
276 localConf.numeric = yylval.num;
277 else if (localConf.numeric != yylval.num)
278 parse_error("Redefinition of server numeric %i (%i)",yylval.num,
282 generalname: NAME '=' QSTRING ';'
284 if (localConf.name == NULL)
285 DupString(localConf.name, yylval.text);
286 else if (strcmp(localConf.name, yylval.text))
287 parse_error("Redefinition of server name %s (%s)",yylval.text,
291 generaldesc: DESCRIPTION '=' QSTRING ';'
293 MyFree(localConf.description);
294 DupString(localConf.description, yylval.text);
295 ircd_strncpy(cli_info(&me), yylval.text, REALLEN);
298 generalvhost: VHOST '=' QSTRING ';'
301 (localConf.vhost_address.s_addr = inet_addr(yylval.text)))
302 localConf.vhost_address.s_addr = INADDR_ANY;
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, yylval.text);
320 else if (localConf.location2 == NULL)
321 DupString(localConf.location2, yylval.text);
322 /* Otherwise just drop it. -A1kmm */
324 admincontact: CONTACT '=' QSTRING ';'
326 if (localConf.contact != NULL)
327 MyFree(localConf.contact);
328 DupString(localConf.contact, yylval.text);
338 memset(&privs, 0, sizeof(privs));
339 memset(&privs_dirty, 0, sizeof(privs_dirty));
344 struct ConnectionClass *c_class;
345 add_class(name, tping, tconn, maxlinks, sendq);
346 c_class = find_class(name);
347 c_class->default_umode = pass;
348 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
349 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
352 parse_error("Missing name in class block");
356 classitems: classitem classitems | classitem;
357 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
358 classsendq | classusermode | priv | error;
359 classname: NAME '=' QSTRING ';'
362 DupString(name, yylval.text);
364 classpingfreq: PINGFREQ '=' timespec ';'
368 classconnfreq: CONNECTFREQ '=' timespec ';'
372 classmaxlinks: MAXLINKS '=' expr ';'
374 maxlinks = yylval.num;
376 classsendq: SENDQ '=' sizespec ';'
380 classusermode: USERMODE '=' QSTRING ';'
384 DupString(pass, yylval.text);
387 connectblock: CONNECT
389 name = pass = host = NULL;
392 } '{' connectitems '}'
394 if (name != NULL && pass != NULL && host != NULL && c_class != NULL &&
395 /*ccount < MAXCONFLINKS &&*/ !strchr(host, '*') &&
399 aconf->status = CONF_SERVER;
401 aconf->passwd = pass;
402 aconf->conn_class = c_class;
404 aconf->status = CONF_SERVER;
406 aconf->next = GlobalConfList;
407 aconf->ipnum.s_addr = INADDR_NONE;
408 lookup_confhost(aconf);
409 GlobalConfList = aconf;
416 name = pass = host = NULL;
417 parse_error("Bad connect block");
420 connectitems: connectitem connectitems | connectitem;
421 connectitem: connectname | connectpass | connectclass | connecthost
422 | connectport | error;
423 connectname: NAME '=' QSTRING ';'
426 DupString(name, yylval.text);
428 connectpass: PASS '=' QSTRING ';'
431 DupString(pass, yylval.text);
433 connectclass: CLASS '=' QSTRING ';'
435 c_class = find_class(yylval.text);
437 connecthost: HOST '=' QSTRING ';'
440 DupString(host, yylval.text);
442 connectport: PORT '=' NUMBER ';'
449 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
450 memset(aconf, 0, sizeof(*aconf));
451 } '{' serveritems '}'
453 if (aconf->status == 0)
459 parse_error("Bad server block");
463 aconf->next = GlobalConfList;
464 GlobalConfList = aconf;
467 serveritems: serveritem serveritems | serveritem;
468 serveritem: servername | servermask | serverhub | serverleaf |
469 serveruworld | error;
470 servername: NAME '=' QSTRING
473 DupString(aconf->name, yylval.text);
475 servermask: MASK '=' QSTRING
478 DupString(aconf->host, yylval.text);
480 /* XXX - perhaps we should do this the hybrid way in connect blocks
482 serverhub: HUB '=' YES ';'
484 aconf->status |= CONF_HUB;
485 aconf->status &= ~CONF_LEAF;
489 aconf->status &= ~CONF_HUB;
491 serverleaf: LEAF '=' YES ';'
493 if (!(aconf->status & CONF_HUB && aconf->status & CONF_UWORLD))
494 aconf->status |= CONF_LEAF;
496 parse_error("Server is both leaf and a hub");
500 aconf->status &= ~CONF_LEAF;
502 serveruworld: UWORLD '=' YES ';'
504 aconf->status |= CONF_UWORLD;
505 aconf->status &= ~CONF_LEAF;
509 aconf->status &= ~CONF_UWORLD;
514 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
515 memset(aconf, 0, sizeof(*aconf));
516 memset(&privs, 0, sizeof(privs));
517 memset(&privs_dirty, 0, sizeof(privs_dirty));
518 aconf->status = CONF_OPERATOR;
519 } '{' operitems '}' ';'
521 if (aconf->name != NULL && aconf->passwd != NULL && aconf->host != NULL
522 && aconf->conn_class != NULL)
524 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
525 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
526 if (!PrivHas(&privs_dirty, PRIV_PROPAGATE)
527 && !PrivHas(&aconf->conn_class->privs_dirty, PRIV_PROPAGATE))
528 parse_error("Operator block for %s and class %s have no LOCAL setting", aconf->name, aconf->conn_class->cc_name);
529 aconf->next = GlobalConfList;
530 GlobalConfList = aconf;
534 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
536 MyFree(aconf->passwd);
542 operitems: operitem | operitems operitem;
543 operitem: opername | operpass | operhost | operclass | priv | error;
545 opername: NAME '=' QSTRING ';'
548 DupString(aconf->name, yylval.text);
551 operpass: PASS '=' QSTRING ';'
553 MyFree(aconf->passwd);
554 DupString(aconf->passwd, yylval.text);
557 operhost: HOST '=' QSTRING ';'
560 if (!strchr(yylval.text, '@'))
563 char *b = (char*) MyMalloc((uh_len = strlen(yylval.text)+3));
564 ircd_snprintf(0, b, uh_len, "*@%s", yylval.text);
568 DupString(aconf->host, yylval.text);
571 operclass: CLASS '=' QSTRING ';'
573 aconf->conn_class = find_class(yylval.text);
576 priv: privtype '=' yesorno ';'
578 PrivSet(&privs_dirty, $1);
579 if (($3 == 1) ^ invert)
586 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
587 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
588 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
589 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
590 KILL { $$ = PRIV_KILL; } |
591 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
592 TPRIV_REHASH { $$ = PRIV_REHASH; } |
593 TPRIV_RESTART { $$ = PRIV_RESTART; } |
594 TPRIV_DIE { $$ = PRIV_DIE; } |
595 TPRIV_GLINE { $$ = PRIV_GLINE; } |
596 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
597 JUPE { $$ = PRIV_JUPE; } |
598 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
599 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
600 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
601 TPRIV_SET { $$ = PRIV_SET; } |
602 TPRIV_WHOX { $$ = PRIV_WHOX; } |
603 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
604 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
605 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
606 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
607 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
608 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
609 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
610 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
611 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
612 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
613 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
614 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
615 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
617 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
619 /* The port block... */
623 /* Hijack these for is_server, is_hidden to cut down on globals... */
626 /* and this for mask... */
628 } '{' portitems '}' ';'
630 if (port > 0 && port <= 0xFFFF)
632 add_listener(port, host, pass, tconn, tping);
636 parse_error("Bad port block");
642 portitems: portitem portitems | portitem;
643 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
644 portnumber: PORT '=' NUMBER ';'
649 portvhost: VHOST '=' QSTRING ';'
652 DupString(host, yylval.text);
655 portmask: MASK '=' QSTRING ';'
658 DupString(pass, yylval.text);
661 portserver: SERVER '=' YES ';'
664 } | SERVER '=' NO ';'
669 porthidden: HIDDEN '=' YES ';'
672 } | HIDDEN '=' NO ';'
679 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
680 memset(aconf, 0, sizeof(*aconf));
681 aconf->status = CONF_CLIENT;
682 } '{' clientitems '}'
684 if ((aconf->host != NULL || aconf->name!=NULL))
686 if (aconf->host == NULL)
687 DupString(aconf->host, "");
688 if (aconf->name == NULL)
689 DupString(aconf->name, "");
690 if (aconf->conn_class == NULL)
691 aconf->conn_class = find_class("default");
692 aconf->next = GlobalConfList;
693 GlobalConfList = aconf;
699 MyFree(aconf->passwd);
702 parse_error("Bad client block");
705 clientitems: clientitem clientitems | clientitem;
706 clientitem: clienthost | clientclass | clientpass | clientip | error;
707 clientip: IP '=' QSTRING ';'
710 DupString(aconf->host, yylval.text);
713 clienthost: HOST '=' QSTRING ';'
716 DupString(aconf->name, yylval.text);
719 clientclass: CLASS '=' QSTRING ';'
721 aconf->conn_class = find_class(yylval.text);
724 clientpass: PASS '=' QSTRING ';'
726 MyFree(aconf->passwd);
727 DupString(aconf->passwd, yylval.text);
732 dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
733 memset(dconf, 0, sizeof(*dconf));
736 if (dconf->hostmask != NULL)
738 if (dconf->usermask == NULL)
739 DupString(dconf->usermask, "*");
740 dconf->next = denyConfList;
741 denyConfList = dconf;
746 MyFree(dconf->hostmask);
747 MyFree(dconf->message);
750 parse_error("Bad kill block");
753 killitems: killitem killitems | killitem;
754 killitem: killuhost | killreal | killreasonfile | killreason | error;
755 killuhost: HOST '=' QSTRING ';'
758 dconf->flags &= ~DENY_FLAGS_REALNAME;
759 MyFree(dconf->hostmask);
760 MyFree(dconf->usermask);
761 if ((h = strchr(yylval.text, '@')) == NULL)
771 DupString(dconf->hostmask, h);
772 DupString(dconf->usermask, u);
773 if (strchr(yylval.text, '.'))
779 dconf->flags |= DENY_FLAGS_IP;
780 c_class = sscanf(dconf->hostmask, "%d.%d.%d.%d/%d",
781 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
783 dconf->bits = c_class * 8;
788 ircd_snprintf(0, ipname, sizeof(ipname), "%d.%d.%d.%d", ad[0], ad[1],
790 dconf->address = inet_addr(ipname);
794 killreal: REAL '=' QSTRING ';'
796 dconf->flags &= ~DENY_FLAGS_IP;
797 dconf->flags |= DENY_FLAGS_REALNAME;
798 MyFree(dconf->hostmask);
799 /* Leave usermask so you can specify user and real... */
800 DupString(dconf->hostmask, yylval.text);
803 killreason: REASON '=' QSTRING ';'
805 dconf->flags &= DENY_FLAGS_FILE;
806 MyFree(dconf->message);
807 DupString(dconf->message, yylval.text);
810 killreasonfile: TFILE '=' QSTRING ';'
812 dconf->flags |= DENY_FLAGS_FILE;
813 MyFree(dconf->message);
814 DupString(dconf->message, yylval.text);
823 struct CRuleNode *node;
824 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
826 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
831 p->next = cruleConfList;
838 parse_error("Bad CRule block");
842 cruleitems: cruleitem cruleitems | cruleitem;
843 cruleitem: cruleserver | crulerule | cruleall | error;
845 cruleserver: SERVER '=' QSTRING ';'
848 collapse(yylval.text);
849 DupString(host, yylval.text);
852 crulerule: RULE '=' QSTRING ';'
855 DupString(pass, yylval.text);
858 cruleall: ALL '=' YES ';'
870 if (host != NULL && pass != NULL)
871 motd_add(host, pass);
877 motditems: motditem motditems | motditem;
878 motditem: motdhost | motdfile | error;
879 motdhost: HOST '=' QSTRING ';'
881 DupString(host, yylval.text);
884 motdfile: TFILE '=' QSTRING ';'
886 DupString(pass, yylval.text);
889 featuresblock: FEATURES '{' featureitems '}' ';';
890 featureitems: featureitems featureitem | featureitem;
896 } '=' stringlist ';';
904 feature_set(NULL, (const char * const *)stringlist, stringno);
906 posextrastrings: /* empty */ | extrastrings;
907 extrastrings: extrastrings extrastring | extrastring;
910 if (stringno < MAX_STRINGS)
911 stringlist[stringno++] = $1;
914 quarantineblock: QUARANTINE '{'
917 qconf = (struct qline*) MyMalloc(sizeof(*qconf));
920 if (qconf->chname != NULL)
921 MyFree(qconf->chname);
922 if (qconf->reason != NULL)
923 MyFree(qconf->reason);
925 memset(qconf, 0, sizeof(*qconf));
926 } quarantineitems '}' ';'
928 if (qconf->chname == NULL || qconf->reason == NULL)
930 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
934 qconf->next = GlobalQuarantineList;
935 GlobalQuarantineList = qconf;
939 quarantineitems: CHANNEL NAME '=' QSTRING ';'
941 DupString(qconf->chname, yylval.text);
942 } | REASON '=' QSTRING ';'
944 DupString(qconf->reason, yylval.text);
947 pseudoblock: PSEUDO QSTRING '{'
949 smap = MyCalloc(1, sizeof(struct s_map));
950 DupString(smap->command, $2);
954 if (!smap->name || !smap->services)
956 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
959 if (register_mapping(smap))
961 smap->next = GlobalServiceMapList;
962 GlobalServiceMapList = smap;
966 struct nick_host *nh, *next;
967 for (nh = smap->services; nh; nh = next)
973 MyFree(smap->command);
974 MyFree(smap->prepend);
980 pseudoitems: pseudoitem pseudoitems | pseudoitem;
981 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
982 pseudoname: NAME '=' QSTRING ';'
984 DupString(smap->name, yylval.text);
986 pseudoprepend: PREPEND '=' QSTRING ';'
988 DupString(smap->prepend, yylval.text);
990 pseudonick: NICK '=' QSTRING ';'
992 char *sep = strchr(yylval.text, '@');
995 size_t slen = strlen(yylval.text);
996 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
997 memcpy(nh->nick, yylval.text, slen + 1);
998 nh->nicklen = sep - yylval.text;
999 nh->next = smap->services;
1000 smap->services = nh;
1004 iauthblock: IAUTH '{'
1010 } iauthitems '}' ';'
1012 if (!name || !host || !port) {
1013 log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
1016 iauth_connect(host, port, pass, tconn, tping);
1022 iauthitems: iauthitem iauthitems | iauthitem;
1023 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
1024 iauthpass: PASS '=' QSTRING ';'
1027 DupString(pass, yylval.text);
1029 iauthhost: HOST '=' QSTRING ';'
1032 DupString(host, yylval.text);
1034 iauthport: PORT '=' NUMBER ';'
1038 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1042 iauthtimeout: TIMEOUT '=' timespec ';'