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,...) {
82 static char error_buffer[1024];
85 ircd_vsnprintf(NULL, error_buffer, sizeof(error_buffer), pattern, vl);
87 yyerror(error_buffer);
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 |
181 /* The timespec, sizespec and expr was ripped straight from
183 timespec: expr | factoredtimes;
185 factoredtimes: factoredtimes factoredtime
190 factoredtime: expr timefactor
195 timefactor: SECONDS { $$ = 1; }
196 | MINUTES { $$ = 60; }
197 | HOURS { $$ = 60 * 60; }
198 | DAYS { $$ = 60 * 60 * 24; }
199 | WEEKS { $$ = 60 * 60 * 24 * 7; }
200 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
201 | YEARS { $$ = 60 * 60 * 24 * 365; }
202 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
215 $$ = $1 * 1024 * 1024;
218 $$ = $1 * 1024 * 1024 * 1024;
221 $$ = $1 * 1024 * 1024 * 1024;
225 /* this is an arithmatic expression */
242 /* leave this out until we find why it makes BSD yacc dump core -larne
252 jupeblock: JUPE '{' jupeitems '}' ';' ;
253 jupeitems: jupeitem jupeitems | jupeitem;
254 jupeitem: jupenick | error;
255 jupenick: NICK '=' QSTRING
257 addNickJupes(yylval.text);
260 generalblock: GENERAL '{' generalitems '}' ';' ;
261 generalitems: generalitem generalitems | generalitem;
262 generalitem: generalnumeric | generalname | generalvhost | generaldesc | error;
263 generalnumeric: NUMERIC '=' NUMBER ';'
265 if (localConf.numeric == 0)
266 localConf.numeric = yylval.num;
268 parse_error("Redefinition of server numeric %i (%i)",yylval.num,
272 generalname: NAME '=' QSTRING ';'
274 if (localConf.name == NULL)
275 DupString(localConf.name, yylval.text);
277 parse_error("Redefinition of server name %s (%s)",yylval.text,
281 generaldesc: DESCRIPTION '=' QSTRING ';'
283 MyFree(localConf.description);
284 DupString(localConf.description, yylval.text);
285 ircd_strncpy(cli_info(&me), yylval.text, REALLEN);
288 generalvhost: VHOST '=' QSTRING ';'
291 (localConf.vhost_address.s_addr = inet_addr(yylval.text)))
292 localConf.vhost_address.s_addr = INADDR_ANY;
295 adminblock: ADMIN '{' adminitems '}'
297 if (localConf.location1 == NULL)
298 DupString(localConf.location1, "");
299 if (localConf.location2 == NULL)
300 DupString(localConf.location2, "");
301 if (localConf.contact == NULL)
302 DupString(localConf.contact, "");
304 adminitems: adminitems adminitem | adminitem;
305 adminitem: adminlocation | admincontact | error;
306 adminlocation: LOCATION '=' QSTRING ';'
308 if (localConf.location1 == NULL)
309 DupString(localConf.location1, yylval.text);
310 else if (localConf.location2 == NULL)
311 DupString(localConf.location2, yylval.text);
312 /* Otherwise just drop it. -A1kmm */
314 admincontact: CONTACT '=' QSTRING ';'
316 if (localConf.contact != NULL)
317 MyFree(localConf.contact);
318 DupString(localConf.contact, yylval.text);
331 add_class(name, tping, tconn, maxlinks, sendq);
334 parse_error("Missing name in class block");
337 classitems: classitem classitems | classitem;
338 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
340 classname: NAME '=' QSTRING ';'
343 DupString(name, yylval.text);
345 classpingfreq: PINGFREQ '=' timespec ';'
349 classconnfreq: CONNECTFREQ '=' timespec ';'
353 classmaxlinks: MAXLINKS '=' expr ';'
355 maxlinks = yylval.num;
357 classsendq: SENDQ '=' sizespec ';'
362 connectblock: CONNECT
364 name = pass = host = NULL;
367 } '{' connectitems '}'
369 if (name != NULL && pass != NULL && host != NULL && c_class != NULL &&
370 /*ccount < MAXCONFLINKS &&*/ !strchr(host, '*') &&
374 aconf->status = CONF_SERVER;
376 aconf->passwd = pass;
377 aconf->conn_class = c_class;
379 aconf->status = CONF_SERVER;
381 aconf->next = GlobalConfList;
382 aconf->ipnum.s_addr = INADDR_NONE;
383 lookup_confhost(aconf);
384 GlobalConfList = aconf;
391 name = pass = host = NULL;
392 parse_error("Bad connect block");
395 connectitems: connectitem connectitems | connectitem;
396 connectitem: connectname | connectpass | connectclass | connecthost
397 | connectport | error;
398 connectname: NAME '=' QSTRING ';'
401 DupString(name, yylval.text);
403 connectpass: PASS '=' QSTRING ';'
406 DupString(pass, yylval.text);
408 connectclass: CLASS '=' QSTRING ';'
410 c_class = find_class(yylval.text);
412 connecthost: HOST '=' QSTRING ';'
415 DupString(host, yylval.text);
417 connectport: PORT '=' NUMBER ';'
424 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
425 memset(aconf, 0, sizeof(*aconf));
426 } '{' serveritems '}'
428 if (aconf->status == 0)
434 parse_error("Bad server block");
438 aconf->next = GlobalConfList;
439 GlobalConfList = aconf;
442 serveritems: serveritem serveritems | serveritem;
443 serveritem: servername | servermask | serverhub | serverleaf |
444 serveruworld | error;
445 servername: NAME '=' QSTRING
448 DupString(aconf->name, yylval.text);
450 servermask: MASK '=' QSTRING
453 DupString(aconf->host, yylval.text);
455 /* XXX - perhaps we should do this the hybrid way in connect blocks
457 serverhub: HUB '=' YES ';'
459 aconf->status |= CONF_HUB;
460 aconf->status &= ~CONF_LEAF;
464 aconf->status &= ~CONF_HUB;
466 serverleaf: LEAF '=' YES ';'
468 if (!(aconf->status & CONF_HUB && aconf->status & CONF_UWORLD))
469 aconf->status |= CONF_LEAF;
471 parse_error("Server is both leaf and a hub");
475 aconf->status &= ~CONF_LEAF;
477 serveruworld: UWORLD '=' YES ';'
479 aconf->status |= CONF_UWORLD;
480 aconf->status &= ~CONF_LEAF;
484 aconf->status &= ~CONF_UWORLD;
489 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
490 memset(aconf, 0, sizeof(*aconf));
491 aconf->status = CONF_OPERATOR;
492 } '{' operitems '}' ';'
494 if (aconf->name != NULL && aconf->passwd != NULL && aconf->host != NULL)
496 aconf->next = GlobalConfList;
497 GlobalConfList = aconf;
501 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
503 MyFree(aconf->passwd);
509 operitems: operitem | operitems operitem;
510 operitem: opername | operpass | operlocal | operhost | operclass | operpriv | error;
512 opername: NAME '=' QSTRING ';'
515 DupString(aconf->name, yylval.text);
518 operpass: PASS '=' QSTRING ';'
520 MyFree(aconf->passwd);
521 DupString(aconf->passwd, yylval.text);
524 operlocal: LOCAL '=' YES ';'
526 /* XXX it would be good to get rid of local operators and add same
527 * permission values here. But for now, I am just going with local
529 aconf->status = CONF_LOCOP;
532 aconf->status = CONF_OPERATOR;
535 operhost: HOST '=' QSTRING ';'
538 if (!strchr(yylval.text, '@'))
541 char *b = (char*) MyMalloc((uh_len = strlen(yylval.text)+3));
542 ircd_snprintf(0, b, uh_len, "*@%s", yylval.text);
546 DupString(aconf->host, yylval.text);
549 operclass: CLASS '=' QSTRING ';'
551 aconf->conn_class = find_class(yylval.text);
554 operpriv: privtype '=' yesorno ';'
558 PrivSet(&aconf->privs_dirty, $1);
559 PrivSet(&aconf->privs, $1);
563 PrivSet(&aconf->privs_dirty, $1);
564 PrivClr(&aconf->privs, $1);
568 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
569 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
570 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
571 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
572 TPRIV_KILL { $$ = PRIV_KILL; } |
573 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
574 TPRIV_REHASH { $$ = PRIV_REHASH; } |
575 TPRIV_RESTART { $$ = PRIV_RESTART; } |
576 TPRIV_DIE { $$ = PRIV_DIE; } |
577 TPRIV_GLINE { $$ = PRIV_GLINE; } |
578 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
579 TPRIV_JUPE { $$ = PRIV_JUPE; } |
580 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
581 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
582 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
583 TPRIV_SET { $$ = PRIV_SET; } |
584 TPRIV_WHOX { $$ = PRIV_WHOX; } |
585 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
586 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
587 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
588 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
589 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
590 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
591 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
592 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
593 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
594 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; };
596 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
598 /* The port block... */
602 /* Hijack these for is_server, is_hidden to cut down on globals... */
605 /* and this for mask... */
607 } '{' portitems '}' ';'
609 if (port > 0 && port <= 0xFFFF)
611 add_listener(port, host, pass, tconn, tping);
618 parse_error("Bad port block");
621 portitems: portitem portitems | portitem;
622 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
623 portnumber: PORT '=' NUMBER ';'
628 portvhost: VHOST '=' QSTRING ';'
631 DupString(host, yylval.text);
634 portmask: MASK '=' QSTRING ';'
637 DupString(pass, yylval.text);
640 portserver: SERVER '=' YES ';'
643 } | SERVER '=' NO ';'
648 porthidden: HIDDEN '=' YES ';'
651 } | HIDDEN '=' NO ';'
658 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
659 memset(aconf, 0, sizeof(*aconf));
660 aconf->status = CONF_CLIENT;
661 } '{' clientitems '}'
663 if ((aconf->host != NULL || aconf->name!=NULL))
665 if (aconf->host == NULL)
666 DupString(aconf->host, "");
667 if (aconf->name == NULL)
668 DupString(aconf->name, "");
669 if (aconf->conn_class == NULL)
670 aconf->conn_class = find_class("default");
671 aconf->next = GlobalConfList;
672 GlobalConfList = aconf;
678 MyFree(aconf->passwd);
681 parse_error("Bad client block");
684 clientitems: clientitem clientitems | clientitem;
685 clientitem: clienthost | clientclass | clientpass | clientip | error;
686 clientip: IP '=' QSTRING ';'
689 DupString(aconf->host, yylval.text);
692 clienthost: HOST '=' QSTRING ';'
695 DupString(aconf->name, yylval.text);
698 clientclass: CLASS '=' QSTRING ';'
700 aconf->conn_class = find_class(yylval.text);
703 clientpass: PASS '=' QSTRING ';'
705 MyFree(aconf->passwd);
706 DupString(aconf->passwd, yylval.text);
711 dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
712 memset(dconf, 0, sizeof(*dconf));
715 if (dconf->hostmask != NULL)
717 if (dconf->usermask == NULL)
718 DupString(dconf->usermask, "*");
719 dconf->next = denyConfList;
720 denyConfList = dconf;
725 MyFree(dconf->hostmask);
726 MyFree(dconf->message);
729 parse_error("Bad kill block");
732 killitems: killitem killitems | killitem;
733 killitem: killuhost | killreal | killreasonfile | killreason | error;
734 killuhost: HOST '=' QSTRING ';'
737 dconf->flags &= ~DENY_FLAGS_REALNAME;
738 MyFree(dconf->hostmask);
739 MyFree(dconf->usermask);
740 if ((h = strchr(yylval.text, '@')) == NULL)
750 DupString(dconf->hostmask, h);
751 DupString(dconf->usermask, u);
752 if (strchr(yylval.text, '.'))
758 dconf->flags |= DENY_FLAGS_IP;
759 c_class = sscanf(dconf->hostmask, "%d.%d.%d.%d/%d",
760 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
762 dconf->bits = c_class * 8;
767 ircd_snprintf(0, ipname, sizeof(ipname), "%d.%d.%d.%d", ad[0], ad[1],
769 dconf->address = inet_addr(ipname);
773 killreal: REAL '=' QSTRING ';'
775 dconf->flags &= ~DENY_FLAGS_IP;
776 dconf->flags |= DENY_FLAGS_REALNAME;
777 MyFree(dconf->hostmask);
778 /* Leave usermask so you can specify user and real... */
779 DupString(dconf->hostmask, yylval.text);
782 killreason: REASON '=' QSTRING ';'
784 dconf->flags &= DENY_FLAGS_FILE;
785 MyFree(dconf->message);
786 DupString(dconf->message, yylval.text);
789 killreasonfile: TFILE '=' QSTRING ';'
791 dconf->flags |= DENY_FLAGS_FILE;
792 MyFree(dconf->message);
793 DupString(dconf->message, yylval.text);
802 struct CRuleNode *node;
803 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
805 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
810 p->next = cruleConfList;
817 parse_error("Bad CRule block");
821 cruleitems: cruleitem cruleitems | cruleitem;
822 cruleitem: cruleserver | crulerule | cruleall | error;
824 cruleserver: SERVER '=' QSTRING ';'
827 collapse(yylval.text);
828 DupString(host, yylval.text);
831 crulerule: RULE '=' QSTRING ';'
834 DupString(pass, yylval.text);
837 cruleall: ALL '=' YES ';'
849 if (host != NULL && pass != NULL)
850 motd_add(host, pass);
856 motditems: motditem motditems | motditem;
857 motditem: motdhost | motdfile | error;
858 motdhost: HOST '=' QSTRING ';'
860 DupString(host, yylval.text);
863 motdfile: TFILE '=' QSTRING ';'
865 DupString(pass, yylval.text);
868 featuresblock: FEATURES '{' featureitems '}' ';';
869 featureitems: featureitems featureitem | featureitem;
875 } '=' stringlist ';';
883 feature_set(NULL, (const char * const *)stringlist, stringno);
885 posextrastrings: /* empty */ | extrastrings;
886 extrastrings: extrastrings extrastring | extrastring;
889 if (stringno < MAX_STRINGS)
890 stringlist[stringno++] = $1;
893 quarantineblock: QUARANTINE '{'
896 qconf = (struct qline*) MyMalloc(sizeof(*qconf));
899 if (qconf->chname != NULL)
900 MyFree(qconf->chname);
901 if (qconf->reason != NULL)
902 MyFree(qconf->reason);
904 memset(qconf, 0, sizeof(*qconf));
905 } quarantineitems '}' ';'
907 if (qconf->chname == NULL || qconf->reason == NULL)
909 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
913 qconf->next = GlobalQuarantineList;
914 GlobalQuarantineList = qconf;
918 quarantineitems: CHANNEL NAME '=' QSTRING ';'
920 DupString(qconf->chname, yylval.text);
921 } | REASON '=' QSTRING ';'
923 DupString(qconf->reason, yylval.text);