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 s_map* GlobalServiceMapList;
67 extern struct qline* GlobalQuarantineList;
71 /* Now all the globals we need :/... */
72 int tping, tconn, maxlinks, sendq, port, invert;
74 char *name, *pass, *host;
75 char *stringlist[MAX_STRINGS];
76 struct ConnectionClass *c_class;
77 struct ConfItem *aconf;
78 struct DenyConf *dconf;
79 struct ServerConf *sconf;
80 struct qline *qconf = NULL;
83 struct Privs privs_dirty;
85 static void parse_error(char *pattern,...) {
86 static char error_buffer[1024];
89 ircd_vsnprintf(NULL, error_buffer, sizeof(error_buffer), pattern, vl);
91 yyerror(error_buffer);
160 /* and now a lot of priviledges... */
161 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
162 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
163 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
164 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
165 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
166 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
167 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE
168 /* and some types... */
170 %type <num> timespec timefactor factoredtimes factoredtime
171 %type <num> expr yesorno privtype
181 /* Blocks in the config file... */
182 blocks: blocks block | block;
183 block: adminblock | generalblock | classblock | connectblock |
184 serverblock | operblock | portblock | jupeblock | clientblock |
185 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
188 /* The timespec, sizespec and expr was ripped straight from
190 timespec: expr | factoredtimes;
192 factoredtimes: factoredtimes factoredtime
197 factoredtime: expr timefactor
202 timefactor: SECONDS { $$ = 1; }
203 | MINUTES { $$ = 60; }
204 | HOURS { $$ = 60 * 60; }
205 | DAYS { $$ = 60 * 60 * 24; }
206 | WEEKS { $$ = 60 * 60 * 24 * 7; }
207 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
208 | YEARS { $$ = 60 * 60 * 24 * 365; }
209 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
222 $$ = $1 * 1024 * 1024;
225 $$ = $1 * 1024 * 1024 * 1024;
228 $$ = $1 * 1024 * 1024 * 1024;
232 /* this is an arithmatic expression */
249 /* leave this out until we find why it makes BSD yacc dump core -larne
259 jupeblock: JUPE '{' jupeitems '}' ';' ;
260 jupeitems: jupeitem jupeitems | jupeitem;
261 jupeitem: jupenick | error;
262 jupenick: NICK '=' QSTRING
264 addNickJupes(yylval.text);
267 generalblock: GENERAL '{' generalitems '}' ';' ;
268 generalitems: generalitem generalitems | generalitem;
269 generalitem: generalnumeric | generalname | generalvhost | generaldesc | error;
270 generalnumeric: NUMERIC '=' NUMBER ';'
272 if (localConf.numeric == 0)
273 localConf.numeric = yylval.num;
275 parse_error("Redefinition of server numeric %i (%i)",yylval.num,
279 generalname: NAME '=' QSTRING ';'
281 if (localConf.name == NULL)
282 DupString(localConf.name, yylval.text);
284 parse_error("Redefinition of server name %s (%s)",yylval.text,
288 generaldesc: DESCRIPTION '=' QSTRING ';'
290 MyFree(localConf.description);
291 DupString(localConf.description, yylval.text);
292 ircd_strncpy(cli_info(&me), yylval.text, REALLEN);
295 generalvhost: VHOST '=' QSTRING ';'
298 (localConf.vhost_address.s_addr = inet_addr(yylval.text)))
299 localConf.vhost_address.s_addr = INADDR_ANY;
302 adminblock: ADMIN '{' adminitems '}'
304 if (localConf.location1 == NULL)
305 DupString(localConf.location1, "");
306 if (localConf.location2 == NULL)
307 DupString(localConf.location2, "");
308 if (localConf.contact == NULL)
309 DupString(localConf.contact, "");
311 adminitems: adminitems adminitem | adminitem;
312 adminitem: adminlocation | admincontact | error;
313 adminlocation: LOCATION '=' QSTRING ';'
315 if (localConf.location1 == NULL)
316 DupString(localConf.location1, yylval.text);
317 else if (localConf.location2 == NULL)
318 DupString(localConf.location2, yylval.text);
319 /* Otherwise just drop it. -A1kmm */
321 admincontact: CONTACT '=' QSTRING ';'
323 if (localConf.contact != NULL)
324 MyFree(localConf.contact);
325 DupString(localConf.contact, yylval.text);
335 memset(&privs, 0, sizeof(privs));
336 memset(&privs_dirty, 0, sizeof(privs_dirty));
341 struct ConnectionClass *c_class;
342 add_class(name, tping, tconn, maxlinks, sendq);
343 c_class = find_class(name);
344 c_class->default_umode = pass;
345 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
346 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
349 parse_error("Missing name in class block");
353 classitems: classitem classitems | classitem;
354 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
355 classsendq | classusermode | priv | error;
356 classname: NAME '=' QSTRING ';'
359 DupString(name, yylval.text);
361 classpingfreq: PINGFREQ '=' timespec ';'
365 classconnfreq: CONNECTFREQ '=' timespec ';'
369 classmaxlinks: MAXLINKS '=' expr ';'
371 maxlinks = yylval.num;
373 classsendq: SENDQ '=' sizespec ';'
377 classusermode: USERMODE '=' QSTRING ';'
381 DupString(pass, yylval.text);
384 connectblock: CONNECT
386 name = pass = host = NULL;
389 } '{' connectitems '}'
391 if (name != NULL && pass != NULL && host != NULL && c_class != NULL &&
392 /*ccount < MAXCONFLINKS &&*/ !strchr(host, '*') &&
396 aconf->status = CONF_SERVER;
398 aconf->passwd = pass;
399 aconf->conn_class = c_class;
401 aconf->status = CONF_SERVER;
403 aconf->next = GlobalConfList;
404 aconf->ipnum.s_addr = INADDR_NONE;
405 lookup_confhost(aconf);
406 GlobalConfList = aconf;
413 name = pass = host = NULL;
414 parse_error("Bad connect block");
417 connectitems: connectitem connectitems | connectitem;
418 connectitem: connectname | connectpass | connectclass | connecthost
419 | connectport | error;
420 connectname: NAME '=' QSTRING ';'
423 DupString(name, yylval.text);
425 connectpass: PASS '=' QSTRING ';'
428 DupString(pass, yylval.text);
430 connectclass: CLASS '=' QSTRING ';'
432 c_class = find_class(yylval.text);
434 connecthost: HOST '=' QSTRING ';'
437 DupString(host, yylval.text);
439 connectport: PORT '=' NUMBER ';'
446 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
447 memset(aconf, 0, sizeof(*aconf));
448 } '{' serveritems '}'
450 if (aconf->status == 0)
456 parse_error("Bad server block");
460 aconf->next = GlobalConfList;
461 GlobalConfList = aconf;
464 serveritems: serveritem serveritems | serveritem;
465 serveritem: servername | servermask | serverhub | serverleaf |
466 serveruworld | error;
467 servername: NAME '=' QSTRING
470 DupString(aconf->name, yylval.text);
472 servermask: MASK '=' QSTRING
475 DupString(aconf->host, yylval.text);
477 /* XXX - perhaps we should do this the hybrid way in connect blocks
479 serverhub: HUB '=' YES ';'
481 aconf->status |= CONF_HUB;
482 aconf->status &= ~CONF_LEAF;
486 aconf->status &= ~CONF_HUB;
488 serverleaf: LEAF '=' YES ';'
490 if (!(aconf->status & CONF_HUB && aconf->status & CONF_UWORLD))
491 aconf->status |= CONF_LEAF;
493 parse_error("Server is both leaf and a hub");
497 aconf->status &= ~CONF_LEAF;
499 serveruworld: UWORLD '=' YES ';'
501 aconf->status |= CONF_UWORLD;
502 aconf->status &= ~CONF_LEAF;
506 aconf->status &= ~CONF_UWORLD;
511 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
512 memset(aconf, 0, sizeof(*aconf));
513 memset(&privs, 0, sizeof(privs));
514 memset(&privs_dirty, 0, sizeof(privs_dirty));
515 aconf->status = CONF_OPERATOR;
516 } '{' operitems '}' ';'
518 if (aconf->name != NULL && aconf->passwd != NULL && aconf->host != NULL
519 && aconf->conn_class != NULL)
521 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
522 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
523 if (!PrivHas(&privs_dirty, PRIV_PROPAGATE)
524 && !PrivHas(&aconf->conn_class->privs_dirty, PRIV_PROPAGATE))
525 parse_error("Operator block for %s and class %s have no LOCAL setting", aconf->name, aconf->conn_class->cc_name);
526 aconf->next = GlobalConfList;
527 GlobalConfList = aconf;
531 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
533 MyFree(aconf->passwd);
539 operitems: operitem | operitems operitem;
540 operitem: opername | operpass | operhost | operclass | priv | error;
542 opername: NAME '=' QSTRING ';'
545 DupString(aconf->name, yylval.text);
548 operpass: PASS '=' QSTRING ';'
550 MyFree(aconf->passwd);
551 DupString(aconf->passwd, yylval.text);
554 operhost: HOST '=' QSTRING ';'
557 if (!strchr(yylval.text, '@'))
560 char *b = (char*) MyMalloc((uh_len = strlen(yylval.text)+3));
561 ircd_snprintf(0, b, uh_len, "*@%s", yylval.text);
565 DupString(aconf->host, yylval.text);
568 operclass: CLASS '=' QSTRING ';'
570 aconf->conn_class = find_class(yylval.text);
573 priv: privtype '=' yesorno ';'
575 PrivSet(&privs_dirty, $1);
576 if (($3 == 1) ^ invert)
583 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
584 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
585 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
586 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
587 KILL { $$ = PRIV_KILL; } |
588 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
589 TPRIV_REHASH { $$ = PRIV_REHASH; } |
590 TPRIV_RESTART { $$ = PRIV_RESTART; } |
591 TPRIV_DIE { $$ = PRIV_DIE; } |
592 TPRIV_GLINE { $$ = PRIV_GLINE; } |
593 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
594 JUPE { $$ = PRIV_JUPE; } |
595 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
596 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
597 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
598 TPRIV_SET { $$ = PRIV_SET; } |
599 TPRIV_WHOX { $$ = PRIV_WHOX; } |
600 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
601 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
602 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
603 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
604 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
605 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
606 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
607 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
608 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
609 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
610 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
611 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
612 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
614 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
616 /* The port block... */
620 /* Hijack these for is_server, is_hidden to cut down on globals... */
623 /* and this for mask... */
625 } '{' portitems '}' ';'
627 if (port > 0 && port <= 0xFFFF)
629 add_listener(port, host, pass, tconn, tping);
633 parse_error("Bad port block");
639 portitems: portitem portitems | portitem;
640 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
641 portnumber: PORT '=' NUMBER ';'
646 portvhost: VHOST '=' QSTRING ';'
649 DupString(host, yylval.text);
652 portmask: MASK '=' QSTRING ';'
655 DupString(pass, yylval.text);
658 portserver: SERVER '=' YES ';'
661 } | SERVER '=' NO ';'
666 porthidden: HIDDEN '=' YES ';'
669 } | HIDDEN '=' NO ';'
676 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
677 memset(aconf, 0, sizeof(*aconf));
678 aconf->status = CONF_CLIENT;
679 } '{' clientitems '}'
681 if ((aconf->host != NULL || aconf->name!=NULL))
683 if (aconf->host == NULL)
684 DupString(aconf->host, "");
685 if (aconf->name == NULL)
686 DupString(aconf->name, "");
687 if (aconf->conn_class == NULL)
688 aconf->conn_class = find_class("default");
689 aconf->next = GlobalConfList;
690 GlobalConfList = aconf;
696 MyFree(aconf->passwd);
699 parse_error("Bad client block");
702 clientitems: clientitem clientitems | clientitem;
703 clientitem: clienthost | clientclass | clientpass | clientip | error;
704 clientip: IP '=' QSTRING ';'
707 DupString(aconf->host, yylval.text);
710 clienthost: HOST '=' QSTRING ';'
713 DupString(aconf->name, yylval.text);
716 clientclass: CLASS '=' QSTRING ';'
718 aconf->conn_class = find_class(yylval.text);
721 clientpass: PASS '=' QSTRING ';'
723 MyFree(aconf->passwd);
724 DupString(aconf->passwd, yylval.text);
729 dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
730 memset(dconf, 0, sizeof(*dconf));
733 if (dconf->hostmask != NULL)
735 if (dconf->usermask == NULL)
736 DupString(dconf->usermask, "*");
737 dconf->next = denyConfList;
738 denyConfList = dconf;
743 MyFree(dconf->hostmask);
744 MyFree(dconf->message);
747 parse_error("Bad kill block");
750 killitems: killitem killitems | killitem;
751 killitem: killuhost | killreal | killreasonfile | killreason | error;
752 killuhost: HOST '=' QSTRING ';'
755 dconf->flags &= ~DENY_FLAGS_REALNAME;
756 MyFree(dconf->hostmask);
757 MyFree(dconf->usermask);
758 if ((h = strchr(yylval.text, '@')) == NULL)
768 DupString(dconf->hostmask, h);
769 DupString(dconf->usermask, u);
770 if (strchr(yylval.text, '.'))
776 dconf->flags |= DENY_FLAGS_IP;
777 c_class = sscanf(dconf->hostmask, "%d.%d.%d.%d/%d",
778 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
780 dconf->bits = c_class * 8;
785 ircd_snprintf(0, ipname, sizeof(ipname), "%d.%d.%d.%d", ad[0], ad[1],
787 dconf->address = inet_addr(ipname);
791 killreal: REAL '=' QSTRING ';'
793 dconf->flags &= ~DENY_FLAGS_IP;
794 dconf->flags |= DENY_FLAGS_REALNAME;
795 MyFree(dconf->hostmask);
796 /* Leave usermask so you can specify user and real... */
797 DupString(dconf->hostmask, yylval.text);
800 killreason: REASON '=' QSTRING ';'
802 dconf->flags &= DENY_FLAGS_FILE;
803 MyFree(dconf->message);
804 DupString(dconf->message, yylval.text);
807 killreasonfile: TFILE '=' QSTRING ';'
809 dconf->flags |= DENY_FLAGS_FILE;
810 MyFree(dconf->message);
811 DupString(dconf->message, yylval.text);
820 struct CRuleNode *node;
821 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
823 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
828 p->next = cruleConfList;
835 parse_error("Bad CRule block");
839 cruleitems: cruleitem cruleitems | cruleitem;
840 cruleitem: cruleserver | crulerule | cruleall | error;
842 cruleserver: SERVER '=' QSTRING ';'
845 collapse(yylval.text);
846 DupString(host, yylval.text);
849 crulerule: RULE '=' QSTRING ';'
852 DupString(pass, yylval.text);
855 cruleall: ALL '=' YES ';'
867 if (host != NULL && pass != NULL)
868 motd_add(host, pass);
874 motditems: motditem motditems | motditem;
875 motditem: motdhost | motdfile | error;
876 motdhost: HOST '=' QSTRING ';'
878 DupString(host, yylval.text);
881 motdfile: TFILE '=' QSTRING ';'
883 DupString(pass, yylval.text);
886 featuresblock: FEATURES '{' featureitems '}' ';';
887 featureitems: featureitems featureitem | featureitem;
893 } '=' stringlist ';';
901 feature_set(NULL, (const char * const *)stringlist, stringno);
903 posextrastrings: /* empty */ | extrastrings;
904 extrastrings: extrastrings extrastring | extrastring;
907 if (stringno < MAX_STRINGS)
908 stringlist[stringno++] = $1;
911 quarantineblock: QUARANTINE '{'
914 qconf = (struct qline*) MyMalloc(sizeof(*qconf));
917 if (qconf->chname != NULL)
918 MyFree(qconf->chname);
919 if (qconf->reason != NULL)
920 MyFree(qconf->reason);
922 memset(qconf, 0, sizeof(*qconf));
923 } quarantineitems '}' ';'
925 if (qconf->chname == NULL || qconf->reason == NULL)
927 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
931 qconf->next = GlobalQuarantineList;
932 GlobalQuarantineList = qconf;
936 quarantineitems: CHANNEL NAME '=' QSTRING ';'
938 DupString(qconf->chname, yylval.text);
939 } | REASON '=' QSTRING ';'
941 DupString(qconf->reason, yylval.text);
944 pseudoblock: PSEUDO QSTRING '{'
946 smap = MyCalloc(1, sizeof(struct s_map));
947 DupString(smap->command, $2);
951 if (!smap->name || !smap->services)
953 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
956 if (register_mapping(smap))
958 smap->next = GlobalServiceMapList;
959 GlobalServiceMapList = smap;
963 struct nick_host *nh, *next;
964 for (nh = smap->services; nh; nh = next)
970 MyFree(smap->command);
971 MyFree(smap->prepend);
977 pseudoitems: pseudoitem pseudoitems | pseudoitem;
978 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
979 pseudoname: NAME '=' QSTRING ';'
981 DupString(smap->name, yylval.text);
983 pseudoprepend: PREPEND '=' QSTRING ';'
985 DupString(smap->prepend, yylval.text);
987 pseudonick: NICK '=' QSTRING ';'
989 char *sep = strchr(yylval.text, '@');
992 size_t slen = strlen(yylval.text);
993 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
994 memcpy(nh->nick, yylval.text, slen + 1);
995 nh->nicklen = sep - yylval.text;
996 nh->next = smap->services;