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(C) 2001 by Andrew Miller, the
5 * 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_chattr.h"
37 #include "ircd_reply.h"
38 #include "ircd_snprintf.h"
39 #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 qline *GlobalQuarantineList;
70 /* Now all the globals we need :/... */
71 int tping, tconn, maxlinks, sendq, port;
73 char *name, *pass, *host;
74 char *stringlist[MAX_STRINGS];
75 struct ConnectionClass *c_class;
76 struct ConfItem *aconf;
77 struct DenyConf *dconf;
78 struct ServerConf *sconf;
79 struct qline *qconf = NULL;
81 static void parse_error(char *pattern,...) {
85 vd.vd_format = pattern;
87 sendto_opmask_butone(0, SNO_OLDSNO, "Config: %v", &vd);
153 /* and now a lot of priviledges... */
154 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
155 %token TPRIV_KILL TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
156 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_JUPE TPRIV_LOCAL_JUPE
157 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
158 %token TPRIV_LOCAL_BADCHAN
159 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
160 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
161 /* and some types... */
163 %type <num> timespec timefactor factoredtimes factoredtime
164 %type <num> expr yesorno privtype
174 /* Blocks in the config file... */
175 blocks: blocks block | block;
176 block: adminblock | generalblock | classblock | connectblock |
177 serverblock | operblock | portblock | jupeblock | clientblock |
178 killblock | cruleblock | motdblock | featuresblock | quarantineblock;
180 /* The timespec, sizespec and expr was ripped straight from
182 timespec: expr | factoredtimes;
184 factoredtimes: factoredtimes factoredtime
189 factoredtime: expr timefactor
194 timefactor: SECONDS { $$ = 1; }
195 | MINUTES { $$ = 60; }
196 | HOURS { $$ = 60 * 60; }
197 | DAYS { $$ = 60 * 60 * 24; }
198 | WEEKS { $$ = 60 * 60 * 24 * 7; }
199 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
200 | YEARS { $$ = 60 * 60 * 24 * 365; }
201 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
214 $$ = $1 * 1024 * 1024;
217 $$ = $1 * 1024 * 1024 * 1024;
220 $$ = $1 * 1024 * 1024 * 1024;
224 /* this is an arithmatic expression */
241 /* leave this out until we find why it makes BSD yacc dump core -larne
251 jupeblock: JUPE '{' jupeitems '}' ';' ;
252 jupeitems: jupeitem jupeitems | jupeitem;
254 jupenick: NICK '=' QSTRING
256 addNickJupes(yylval.text);
259 generalblock: GENERAL '{' generalitems '}' ';' ;
260 generalitems: generalitem generalitems | generalitem;
261 generalitem: generalnumeric | generalname | generalvhost | generaldesc;
262 generalnumeric: NUMERIC '=' NUMBER ';'
264 if (localConf.numeric == 0)
265 localConf.numeric = yylval.num;
267 parse_error("Redefinition of server numeric %i (%i)",yylval.num,
271 generalname: NAME '=' QSTRING ';'
273 if (localConf.name == NULL)
274 DupString(localConf.name, yylval.text);
276 parse_error("Redefinition of server name %s (%s)",yylval.text,
280 generaldesc: DESCRIPTION '=' QSTRING ';'
282 MyFree(localConf.description);
283 DupString(localConf.description, yylval.text);
284 ircd_strncpy(cli_info(&me), yylval.text, REALLEN);
287 generalvhost: VHOST '=' QSTRING ';'
290 (localConf.vhost_address.s_addr = inet_addr(yylval.text)))
291 localConf.vhost_address.s_addr = INADDR_ANY;
294 adminblock: ADMIN '{' adminitems '}'
296 if (localConf.location1 == NULL)
297 DupString(localConf.location1, "");
298 if (localConf.location2 == NULL)
299 DupString(localConf.location2, "");
300 if (localConf.contact == NULL)
301 DupString(localConf.contact, "");
303 adminitems: adminitems adminitem | adminitem;
304 adminitem: adminlocation | admincontact;
305 adminlocation: LOCATION '=' QSTRING ';'
307 if (localConf.location1 == NULL)
308 DupString(localConf.location1, yylval.text);
309 else if (localConf.location2 == NULL)
310 DupString(localConf.location2, yylval.text);
311 /* Otherwise just drop it. -A1kmm */
313 admincontact: CONTACT '=' QSTRING ';'
315 if (localConf.contact != NULL)
316 MyFree(localConf.contact);
317 DupString(localConf.contact, yylval.text);
330 add_class(name, tping, tconn, maxlinks, sendq);
333 parse_error("Missing name in class block");
336 classitems: classitem classitems | classitem;
337 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
339 classname: NAME '=' QSTRING ';'
342 DupString(name, yylval.text);
344 classpingfreq: PINGFREQ '=' timespec ';'
348 classconnfreq: CONNECTFREQ '=' timespec ';'
352 classmaxlinks: MAXLINKS '=' expr ';'
354 maxlinks = yylval.num;
356 classsendq: SENDQ '=' sizespec ';'
361 connectblock: CONNECT
363 name = pass = host = NULL;
366 } '{' connectitems '}'
368 if (name != NULL && pass != NULL && host != NULL && c_class != NULL &&
369 /*ccount < MAXCONFLINKS &&*/ !strchr(host, '*') &&
373 aconf->status = CONF_SERVER;
375 aconf->passwd = pass;
376 aconf->conn_class = c_class;
378 aconf->status = CONF_SERVER;
380 aconf->next = GlobalConfList;
381 aconf->ipnum.s_addr = INADDR_NONE;
382 lookup_confhost(aconf);
383 GlobalConfList = aconf;
390 name = pass = host = NULL;
391 parse_error("Bad connect block");
394 connectitems: connectitem connectitems | connectitem;
395 connectitem: connectname | connectpass | connectclass | connecthost
397 connectname: NAME '=' QSTRING ';'
400 DupString(name, yylval.text);
402 connectpass: PASS '=' QSTRING ';'
405 DupString(pass, yylval.text);
407 connectclass: CLASS '=' QSTRING ';'
409 c_class = find_class(yylval.text);
411 connecthost: HOST '=' QSTRING ';'
414 DupString(host, yylval.text);
416 connectport: PORT '=' NUMBER ';'
423 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
424 memset(aconf, 0, sizeof(*aconf));
425 } '{' serveritems '}'
427 if (aconf->status == 0)
433 parse_error("Bad server block");
437 aconf->next = GlobalConfList;
438 GlobalConfList = aconf;
441 serveritems: serveritem serveritems | serveritem;
442 serveritem: servername | servermask | serverhub | serverleaf |
444 servername: NAME '=' QSTRING
447 DupString(aconf->name, yylval.text);
449 servermask: MASK '=' QSTRING
452 DupString(aconf->host, yylval.text);
454 /* XXX - perhaps we should do this the hybrid way in connect blocks
456 serverhub: HUB '=' YES ';'
458 aconf->status |= CONF_HUB;
459 aconf->status &= ~CONF_LEAF;
463 aconf->status &= ~CONF_HUB;
465 serverleaf: LEAF '=' YES ';'
467 if (!(aconf->status & CONF_HUB && aconf->status & CONF_UWORLD))
468 aconf->status |= CONF_LEAF;
470 parse_error("Server is both leaf and a hub");
474 aconf->status &= ~CONF_LEAF;
476 serveruworld: UWORLD '=' YES ';'
478 aconf->status |= CONF_UWORLD;
479 aconf->status &= ~CONF_LEAF;
483 aconf->status &= ~CONF_UWORLD;
488 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
489 memset(aconf, 0, sizeof(*aconf));
490 aconf->status = CONF_OPERATOR;
491 } '{' operitems '}' ';'
493 if (aconf->name != NULL && aconf->passwd != NULL && aconf->host != NULL)
495 aconf->next = GlobalConfList;
496 GlobalConfList = aconf;
500 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
502 MyFree(aconf->passwd);
508 operitems: operitem | operitems operitem;
509 operitem: opername | operpass | operlocal | operhost | operclass | operpriv;
511 opername: NAME '=' QSTRING ';'
514 DupString(aconf->name, yylval.text);
517 operpass: PASS '=' QSTRING ';'
519 MyFree(aconf->passwd);
520 DupString(aconf->passwd, yylval.text);
523 operlocal: LOCAL '=' YES ';'
525 /* XXX it would be good to get rid of local operators and add same
526 * permission values here. But for now, I am just going with local
528 aconf->status = CONF_LOCOP;
531 aconf->status = CONF_OPERATOR;
534 operhost: HOST '=' QSTRING ';'
537 if (!strchr(yylval.text, '@'))
540 char *b = (char*) MyMalloc((uh_len = strlen(yylval.text)+3));
541 ircd_snprintf(0, b, uh_len, "*@%s", yylval.text);
545 DupString(aconf->host, yylval.text);
548 operclass: CLASS '=' QSTRING ';'
550 aconf->conn_class = find_class(yylval.text);
553 operpriv: privtype '=' yesorno ';'
557 PrivSet(&aconf->privs_dirty, $1);
558 PrivSet(&aconf->privs, $1);
562 PrivSet(&aconf->privs_dirty, $1);
563 PrivClr(&aconf->privs, $1);
567 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
568 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
569 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
570 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
571 TPRIV_KILL { $$ = PRIV_KILL; } |
572 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
573 TPRIV_REHASH { $$ = PRIV_REHASH; } |
574 TPRIV_RESTART { $$ = PRIV_RESTART; } |
575 TPRIV_DIE { $$ = PRIV_DIE; } |
576 TPRIV_GLINE { $$ = PRIV_GLINE; } |
577 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
578 TPRIV_JUPE { $$ = PRIV_JUPE; } |
579 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
580 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
581 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
582 TPRIV_SET { $$ = PRIV_SET; } |
583 TPRIV_WHOX { $$ = PRIV_WHOX; } |
584 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
585 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
586 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
587 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
588 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
589 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
590 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
591 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
592 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
593 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; };
595 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
597 /* The port block... */
601 /* Hijack these for is_server, is_hidden to cut down on globals... */
604 /* and this for mask... */
606 } '{' portitems '}' ';'
608 if (port > 0 && port <= 0xFFFF)
610 add_listener(port, host, pass, tconn, tping);
617 parse_error("Bad port block");
620 portitems: portitem portitems | portitem;
621 portitem: portnumber | portvhost | portmask | portserver | porthidden;
622 portnumber: PORT '=' NUMBER ';'
627 portvhost: VHOST '=' QSTRING ';'
630 DupString(host, yylval.text);
633 portmask: MASK '=' QSTRING ';'
636 DupString(pass, yylval.text);
639 portserver: SERVER '=' YES ';'
642 } | SERVER '=' NO ';'
647 porthidden: HIDDEN '=' YES ';'
650 } | HIDDEN '=' NO ';'
657 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
658 memset(aconf, 0, sizeof(*aconf));
659 aconf->status = CONF_CLIENT;
660 } '{' clientitems '}'
662 if ((aconf->host != NULL || aconf->name!=NULL))
664 if (aconf->host == NULL)
665 DupString(aconf->host, "");
666 if (aconf->name == NULL)
667 DupString(aconf->name, "");
668 if (aconf->conn_class == NULL)
669 aconf->conn_class = find_class("default");
670 aconf->next = GlobalConfList;
671 GlobalConfList = aconf;
677 MyFree(aconf->passwd);
680 parse_error("Bad client block");
683 clientitems: clientitem clientitems | clientitem;
684 clientitem: clienthost | clientclass | clientpass | clientip;
685 clientip: IP '=' QSTRING ';'
688 DupString(aconf->host, yylval.text);
691 clienthost: HOST '=' QSTRING ';'
694 DupString(aconf->name, yylval.text);
697 clientclass: CLASS '=' QSTRING ';'
699 aconf->conn_class = find_class(yylval.text);
702 clientpass: PASS '=' QSTRING ';'
704 MyFree(aconf->passwd);
705 DupString(aconf->passwd, yylval.text);
710 dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
711 memset(dconf, 0, sizeof(*dconf));
714 if (dconf->hostmask != NULL)
716 if (dconf->usermask == NULL)
717 DupString(dconf->usermask, "*");
718 dconf->next = denyConfList;
719 denyConfList = dconf;
724 MyFree(dconf->hostmask);
725 MyFree(dconf->message);
728 parse_error("Bad kill block");
731 killitems: killitem killitems | killitem;
732 killitem: killuhost | killreal | killreasonfile | killreason;
733 killuhost: HOST '=' QSTRING ';'
736 dconf->flags &= ~DENY_FLAGS_REALNAME;
737 MyFree(dconf->hostmask);
738 MyFree(dconf->usermask);
739 if ((h = strchr(yylval.text, '@')) == NULL)
749 DupString(dconf->hostmask, h);
750 DupString(dconf->usermask, u);
751 if (strchr(yylval.text, '.'))
757 dconf->flags |= DENY_FLAGS_IP;
758 c_class = sscanf(dconf->hostmask, "%d.%d.%d.%d/%d",
759 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
761 dconf->bits = c_class * 8;
766 ircd_snprintf(0, ipname, sizeof(ipname), "%d.%d.%d.%d", ad[0], ad[1],
768 dconf->address = inet_addr(ipname);
772 killreal: REAL '=' QSTRING ';'
774 dconf->flags &= ~DENY_FLAGS_IP;
775 dconf->flags |= DENY_FLAGS_REALNAME;
776 MyFree(dconf->hostmask);
777 /* Leave usermask so you can specify user and real... */
778 DupString(dconf->hostmask, yylval.text);
781 killreason: REASON '=' QSTRING ';'
783 dconf->flags &= DENY_FLAGS_FILE;
784 MyFree(dconf->message);
785 DupString(dconf->message, yylval.text);
788 killreasonfile: TFILE '=' QSTRING ';'
790 dconf->flags |= DENY_FLAGS_FILE;
791 MyFree(dconf->message);
792 DupString(dconf->message, yylval.text);
801 struct CRuleNode *node;
802 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
804 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
809 p->next = cruleConfList;
816 parse_error("Bad CRule block");
820 cruleitems: cruleitem cruleitems | cruleitem;
821 cruleitem: cruleserver | crulerule | cruleall;
823 cruleserver: SERVER '=' QSTRING ';'
826 collapse(yylval.text);
827 DupString(host, yylval.text);
830 crulerule: RULE '=' QSTRING ';'
833 DupString(pass, yylval.text);
836 cruleall: ALL '=' YES ';'
848 if (host != NULL && pass != NULL)
849 motd_add(host, pass);
855 motditems: motditem motditems | motditem;
856 motditem: motdhost | motdfile;
857 motdhost: HOST '=' QSTRING ';'
859 DupString(host, yylval.text);
862 motdfile: TFILE '=' QSTRING ';'
864 DupString(pass, yylval.text);
867 featuresblock: FEATURES '{' featureitems '}' ';';
868 featureitems: featureitems featureitem | featureitem;
874 } '=' stringlist ';';
882 feature_set(NULL, (const char * const *)stringlist, stringno);
884 posextrastrings: /* empty */ | extrastrings;
885 extrastrings: extrastrings extrastring | extrastring;
888 if (stringno < MAX_STRINGS)
889 stringlist[stringno++] = $1;
892 quarantineblock: QUARANTINE '{'
895 qconf = (struct qline*) MyMalloc(sizeof(*qconf));
898 if (qconf->chname != NULL)
899 MyFree(qconf->chname);
900 if (qconf->reason != NULL)
901 MyFree(qconf->reason);
903 memset(qconf, 0, sizeof(*qconf));
904 } quarantineitems '}' ';'
906 if (qconf->chname == NULL || qconf->reason == NULL)
908 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
912 qconf->next = GlobalQuarantineList;
913 GlobalQuarantineList = qconf;
917 quarantineitems: CHANNEL NAME '=' QSTRING ';'
919 DupString(qconf->chname, yylval.text);
920 } | REASON '=' QSTRING ';'
922 DupString(qconf->reason, yylval.text);