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);
161 /* and now a lot of priviledges... */
162 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
163 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
164 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
165 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
166 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
167 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
168 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE
169 /* and some types... */
171 %type <num> timespec timefactor factoredtimes factoredtime
172 %type <num> expr yesorno privtype
182 /* Blocks in the config file... */
183 blocks: blocks block | block;
184 block: adminblock | generalblock | classblock | connectblock |
185 serverblock | operblock | portblock | jupeblock | clientblock |
186 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
187 pseudoblock | iauthblock | error;
189 /* The timespec, sizespec and expr was ripped straight from
191 timespec: expr | factoredtimes;
193 factoredtimes: factoredtimes factoredtime
198 factoredtime: expr timefactor
203 timefactor: SECONDS { $$ = 1; }
204 | MINUTES { $$ = 60; }
205 | HOURS { $$ = 60 * 60; }
206 | DAYS { $$ = 60 * 60 * 24; }
207 | WEEKS { $$ = 60 * 60 * 24 * 7; }
208 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
209 | YEARS { $$ = 60 * 60 * 24 * 365; }
210 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
223 $$ = $1 * 1024 * 1024;
226 $$ = $1 * 1024 * 1024 * 1024;
229 $$ = $1 * 1024 * 1024 * 1024;
233 /* this is an arithmatic expression */
250 /* leave this out until we find why it makes BSD yacc dump core -larne
260 jupeblock: JUPE '{' jupeitems '}' ';' ;
261 jupeitems: jupeitem jupeitems | jupeitem;
262 jupeitem: jupenick | error;
263 jupenick: NICK '=' QSTRING
265 addNickJupes(yylval.text);
268 generalblock: GENERAL '{' generalitems '}' ';' ;
269 generalitems: generalitem generalitems | generalitem;
270 generalitem: generalnumeric | generalname | generalvhost | generaldesc | error;
271 generalnumeric: NUMERIC '=' NUMBER ';'
273 if (localConf.numeric == 0)
274 localConf.numeric = yylval.num;
275 else if (localConf.numeric != yylval.num)
276 parse_error("Redefinition of server numeric %i (%i)",yylval.num,
280 generalname: NAME '=' QSTRING ';'
282 if (localConf.name == NULL)
283 DupString(localConf.name, yylval.text);
284 else if (strcmp(localConf.name, yylval.text))
285 parse_error("Redefinition of server name %s (%s)",yylval.text,
289 generaldesc: DESCRIPTION '=' QSTRING ';'
291 MyFree(localConf.description);
292 DupString(localConf.description, yylval.text);
293 ircd_strncpy(cli_info(&me), yylval.text, REALLEN);
296 generalvhost: VHOST '=' QSTRING ';'
299 (localConf.vhost_address.s_addr = inet_addr(yylval.text)))
300 localConf.vhost_address.s_addr = INADDR_ANY;
303 adminblock: ADMIN '{' adminitems '}'
305 if (localConf.location1 == NULL)
306 DupString(localConf.location1, "");
307 if (localConf.location2 == NULL)
308 DupString(localConf.location2, "");
309 if (localConf.contact == NULL)
310 DupString(localConf.contact, "");
312 adminitems: adminitems adminitem | adminitem;
313 adminitem: adminlocation | admincontact | error;
314 adminlocation: LOCATION '=' QSTRING ';'
316 if (localConf.location1 == NULL)
317 DupString(localConf.location1, yylval.text);
318 else if (localConf.location2 == NULL)
319 DupString(localConf.location2, yylval.text);
320 /* Otherwise just drop it. -A1kmm */
322 admincontact: CONTACT '=' QSTRING ';'
324 if (localConf.contact != NULL)
325 MyFree(localConf.contact);
326 DupString(localConf.contact, yylval.text);
336 memset(&privs, 0, sizeof(privs));
337 memset(&privs_dirty, 0, sizeof(privs_dirty));
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");
354 classitems: classitem classitems | classitem;
355 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
356 classsendq | classusermode | priv | error;
357 classname: NAME '=' QSTRING ';'
360 DupString(name, yylval.text);
362 classpingfreq: PINGFREQ '=' timespec ';'
366 classconnfreq: CONNECTFREQ '=' timespec ';'
370 classmaxlinks: MAXLINKS '=' expr ';'
372 maxlinks = yylval.num;
374 classsendq: SENDQ '=' sizespec ';'
378 classusermode: USERMODE '=' QSTRING ';'
382 DupString(pass, yylval.text);
385 connectblock: CONNECT
387 name = pass = host = NULL;
390 } '{' connectitems '}'
392 if (name != NULL && pass != NULL && host != NULL && c_class != NULL &&
393 /*ccount < MAXCONFLINKS &&*/ !strchr(host, '*') &&
397 aconf->status = CONF_SERVER;
399 aconf->passwd = pass;
400 aconf->conn_class = c_class;
402 aconf->status = CONF_SERVER;
404 aconf->next = GlobalConfList;
405 aconf->ipnum.s_addr = INADDR_NONE;
406 lookup_confhost(aconf);
407 GlobalConfList = aconf;
414 name = pass = host = NULL;
415 parse_error("Bad connect block");
418 connectitems: connectitem connectitems | connectitem;
419 connectitem: connectname | connectpass | connectclass | connecthost
420 | connectport | error;
421 connectname: NAME '=' QSTRING ';'
424 DupString(name, yylval.text);
426 connectpass: PASS '=' QSTRING ';'
429 DupString(pass, yylval.text);
431 connectclass: CLASS '=' QSTRING ';'
433 c_class = find_class(yylval.text);
435 connecthost: HOST '=' QSTRING ';'
438 DupString(host, yylval.text);
440 connectport: PORT '=' NUMBER ';'
447 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
448 memset(aconf, 0, sizeof(*aconf));
449 } '{' serveritems '}'
451 if (aconf->status == 0)
457 parse_error("Bad server block");
461 aconf->next = GlobalConfList;
462 GlobalConfList = aconf;
465 serveritems: serveritem serveritems | serveritem;
466 serveritem: servername | servermask | serverhub | serverleaf |
467 serveruworld | error;
468 servername: NAME '=' QSTRING
471 DupString(aconf->name, yylval.text);
473 servermask: MASK '=' QSTRING
476 DupString(aconf->host, yylval.text);
478 /* XXX - perhaps we should do this the hybrid way in connect blocks
480 serverhub: HUB '=' YES ';'
482 aconf->status |= CONF_HUB;
483 aconf->status &= ~CONF_LEAF;
487 aconf->status &= ~CONF_HUB;
489 serverleaf: LEAF '=' YES ';'
491 if (!(aconf->status & CONF_HUB && aconf->status & CONF_UWORLD))
492 aconf->status |= CONF_LEAF;
494 parse_error("Server is both leaf and a hub");
498 aconf->status &= ~CONF_LEAF;
500 serveruworld: UWORLD '=' YES ';'
502 aconf->status |= CONF_UWORLD;
503 aconf->status &= ~CONF_LEAF;
507 aconf->status &= ~CONF_UWORLD;
512 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
513 memset(aconf, 0, sizeof(*aconf));
514 memset(&privs, 0, sizeof(privs));
515 memset(&privs_dirty, 0, sizeof(privs_dirty));
516 aconf->status = CONF_OPERATOR;
517 } '{' operitems '}' ';'
519 if (aconf->name != NULL && aconf->passwd != NULL && aconf->host != NULL
520 && aconf->conn_class != NULL)
522 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
523 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
524 if (!PrivHas(&privs_dirty, PRIV_PROPAGATE)
525 && !PrivHas(&aconf->conn_class->privs_dirty, PRIV_PROPAGATE))
526 parse_error("Operator block for %s and class %s have no LOCAL setting", aconf->name, aconf->conn_class->cc_name);
527 aconf->next = GlobalConfList;
528 GlobalConfList = aconf;
532 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
534 MyFree(aconf->passwd);
540 operitems: operitem | operitems operitem;
541 operitem: opername | operpass | operhost | operclass | priv | error;
543 opername: NAME '=' QSTRING ';'
546 DupString(aconf->name, yylval.text);
549 operpass: PASS '=' QSTRING ';'
551 MyFree(aconf->passwd);
552 DupString(aconf->passwd, yylval.text);
555 operhost: HOST '=' QSTRING ';'
558 if (!strchr(yylval.text, '@'))
561 char *b = (char*) MyMalloc((uh_len = strlen(yylval.text)+3));
562 ircd_snprintf(0, b, uh_len, "*@%s", yylval.text);
566 DupString(aconf->host, yylval.text);
569 operclass: CLASS '=' QSTRING ';'
571 aconf->conn_class = find_class(yylval.text);
574 priv: privtype '=' yesorno ';'
576 PrivSet(&privs_dirty, $1);
577 if (($3 == 1) ^ invert)
584 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
585 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
586 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
587 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
588 KILL { $$ = PRIV_KILL; } |
589 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
590 TPRIV_REHASH { $$ = PRIV_REHASH; } |
591 TPRIV_RESTART { $$ = PRIV_RESTART; } |
592 TPRIV_DIE { $$ = PRIV_DIE; } |
593 TPRIV_GLINE { $$ = PRIV_GLINE; } |
594 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
595 JUPE { $$ = PRIV_JUPE; } |
596 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
597 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
598 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
599 TPRIV_SET { $$ = PRIV_SET; } |
600 TPRIV_WHOX { $$ = PRIV_WHOX; } |
601 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
602 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
603 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
604 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
605 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
606 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
607 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
608 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
609 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
610 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
611 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
612 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
613 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
615 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
617 /* The port block... */
621 /* Hijack these for is_server, is_hidden to cut down on globals... */
624 /* and this for mask... */
626 } '{' portitems '}' ';'
628 if (port > 0 && port <= 0xFFFF)
630 add_listener(port, host, pass, tconn, tping);
634 parse_error("Bad port block");
640 portitems: portitem portitems | portitem;
641 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
642 portnumber: PORT '=' NUMBER ';'
647 portvhost: VHOST '=' QSTRING ';'
650 DupString(host, yylval.text);
653 portmask: MASK '=' QSTRING ';'
656 DupString(pass, yylval.text);
659 portserver: SERVER '=' YES ';'
662 } | SERVER '=' NO ';'
667 porthidden: HIDDEN '=' YES ';'
670 } | HIDDEN '=' NO ';'
677 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
678 memset(aconf, 0, sizeof(*aconf));
679 aconf->status = CONF_CLIENT;
680 } '{' clientitems '}'
682 if ((aconf->host != NULL || aconf->name!=NULL))
684 if (aconf->host == NULL)
685 DupString(aconf->host, "");
686 if (aconf->name == NULL)
687 DupString(aconf->name, "");
688 if (aconf->conn_class == NULL)
689 aconf->conn_class = find_class("default");
690 aconf->next = GlobalConfList;
691 GlobalConfList = aconf;
697 MyFree(aconf->passwd);
700 parse_error("Bad client block");
703 clientitems: clientitem clientitems | clientitem;
704 clientitem: clienthost | clientclass | clientpass | clientip | error;
705 clientip: IP '=' QSTRING ';'
708 DupString(aconf->host, yylval.text);
711 clienthost: HOST '=' QSTRING ';'
714 DupString(aconf->name, yylval.text);
717 clientclass: CLASS '=' QSTRING ';'
719 aconf->conn_class = find_class(yylval.text);
722 clientpass: PASS '=' QSTRING ';'
724 MyFree(aconf->passwd);
725 DupString(aconf->passwd, yylval.text);
730 dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
731 memset(dconf, 0, sizeof(*dconf));
734 if (dconf->hostmask != NULL)
736 if (dconf->usermask == NULL)
737 DupString(dconf->usermask, "*");
738 dconf->next = denyConfList;
739 denyConfList = dconf;
744 MyFree(dconf->hostmask);
745 MyFree(dconf->message);
748 parse_error("Bad kill block");
751 killitems: killitem killitems | killitem;
752 killitem: killuhost | killreal | killreasonfile | killreason | error;
753 killuhost: HOST '=' QSTRING ';'
756 dconf->flags &= ~DENY_FLAGS_REALNAME;
757 MyFree(dconf->hostmask);
758 MyFree(dconf->usermask);
759 if ((h = strchr(yylval.text, '@')) == NULL)
769 DupString(dconf->hostmask, h);
770 DupString(dconf->usermask, u);
771 if (strchr(yylval.text, '.'))
777 dconf->flags |= DENY_FLAGS_IP;
778 c_class = sscanf(dconf->hostmask, "%d.%d.%d.%d/%d",
779 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
781 dconf->bits = c_class * 8;
786 ircd_snprintf(0, ipname, sizeof(ipname), "%d.%d.%d.%d", ad[0], ad[1],
788 dconf->address = inet_addr(ipname);
792 killreal: REAL '=' QSTRING ';'
794 dconf->flags &= ~DENY_FLAGS_IP;
795 dconf->flags |= DENY_FLAGS_REALNAME;
796 MyFree(dconf->hostmask);
797 /* Leave usermask so you can specify user and real... */
798 DupString(dconf->hostmask, yylval.text);
801 killreason: REASON '=' QSTRING ';'
803 dconf->flags &= DENY_FLAGS_FILE;
804 MyFree(dconf->message);
805 DupString(dconf->message, yylval.text);
808 killreasonfile: TFILE '=' QSTRING ';'
810 dconf->flags |= DENY_FLAGS_FILE;
811 MyFree(dconf->message);
812 DupString(dconf->message, yylval.text);
821 struct CRuleNode *node;
822 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
824 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
829 p->next = cruleConfList;
836 parse_error("Bad CRule block");
840 cruleitems: cruleitem cruleitems | cruleitem;
841 cruleitem: cruleserver | crulerule | cruleall | error;
843 cruleserver: SERVER '=' QSTRING ';'
846 collapse(yylval.text);
847 DupString(host, yylval.text);
850 crulerule: RULE '=' QSTRING ';'
853 DupString(pass, yylval.text);
856 cruleall: ALL '=' YES ';'
868 if (host != NULL && pass != NULL)
869 motd_add(host, pass);
875 motditems: motditem motditems | motditem;
876 motditem: motdhost | motdfile | error;
877 motdhost: HOST '=' QSTRING ';'
879 DupString(host, yylval.text);
882 motdfile: TFILE '=' QSTRING ';'
884 DupString(pass, yylval.text);
887 featuresblock: FEATURES '{' featureitems '}' ';';
888 featureitems: featureitems featureitem | featureitem;
894 } '=' stringlist ';';
902 feature_set(NULL, (const char * const *)stringlist, stringno);
904 posextrastrings: /* empty */ | extrastrings;
905 extrastrings: extrastrings extrastring | extrastring;
908 if (stringno < MAX_STRINGS)
909 stringlist[stringno++] = $1;
912 quarantineblock: QUARANTINE '{'
915 qconf = (struct qline*) MyMalloc(sizeof(*qconf));
918 if (qconf->chname != NULL)
919 MyFree(qconf->chname);
920 if (qconf->reason != NULL)
921 MyFree(qconf->reason);
923 memset(qconf, 0, sizeof(*qconf));
924 } quarantineitems '}' ';'
926 if (qconf->chname == NULL || qconf->reason == NULL)
928 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
932 qconf->next = GlobalQuarantineList;
933 GlobalQuarantineList = qconf;
937 quarantineitems: CHANNEL NAME '=' QSTRING ';'
939 DupString(qconf->chname, yylval.text);
940 } | REASON '=' QSTRING ';'
942 DupString(qconf->reason, yylval.text);
945 pseudoblock: PSEUDO QSTRING '{'
947 smap = MyCalloc(1, sizeof(struct s_map));
948 DupString(smap->command, $2);
952 if (!smap->name || !smap->services)
954 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
957 if (register_mapping(smap))
959 smap->next = GlobalServiceMapList;
960 GlobalServiceMapList = smap;
964 struct nick_host *nh, *next;
965 for (nh = smap->services; nh; nh = next)
971 MyFree(smap->command);
972 MyFree(smap->prepend);
978 pseudoitems: pseudoitem pseudoitems | pseudoitem;
979 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
980 pseudoname: NAME '=' QSTRING ';'
982 DupString(smap->name, yylval.text);
984 pseudoprepend: PREPEND '=' QSTRING ';'
986 DupString(smap->prepend, yylval.text);
988 pseudonick: NICK '=' QSTRING ';'
990 char *sep = strchr(yylval.text, '@');
993 size_t slen = strlen(yylval.text);
994 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
995 memcpy(nh->nick, yylval.text, slen + 1);
996 nh->nicklen = sep - yylval.text;
997 nh->next = smap->services;
1002 iauthblock: IAUTH '{'
1008 } iauthitems '}' ';'
1010 if (!name || !host || !port) {
1011 log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
1014 iauth_connect(host, port, pass, tconn, tping);
1020 iauthitems: iauthitem iauthitems | iauthitem;
1021 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
1022 iauthpass: PASS '=' QSTRING ';'
1025 DupString(pass, yylval.text);
1027 iauthhost: HOST '=' QSTRING ';'
1030 DupString(host, yylval.text);
1032 iauthport: PORT '=' NUMBER ';'
1036 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1040 iauthtimeout: TIMEOUT '=' timespec ';'