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 ';'
298 ircd_aton(&localConf.vhost_address, yylval.text);
301 adminblock: ADMIN '{' adminitems '}'
303 if (localConf.location1 == NULL)
304 DupString(localConf.location1, "");
305 if (localConf.location2 == NULL)
306 DupString(localConf.location2, "");
307 if (localConf.contact == NULL)
308 DupString(localConf.contact, "");
310 adminitems: adminitems adminitem | adminitem;
311 adminitem: adminlocation | admincontact | error;
312 adminlocation: LOCATION '=' QSTRING ';'
314 if (localConf.location1 == NULL)
315 DupString(localConf.location1, yylval.text);
316 else if (localConf.location2 == NULL)
317 DupString(localConf.location2, yylval.text);
318 /* Otherwise just drop it. -A1kmm */
320 admincontact: CONTACT '=' QSTRING ';'
322 if (localConf.contact != NULL)
323 MyFree(localConf.contact);
324 DupString(localConf.contact, yylval.text);
334 memset(&privs, 0, sizeof(privs));
335 memset(&privs_dirty, 0, sizeof(privs_dirty));
340 struct ConnectionClass *c_class;
341 add_class(name, tping, tconn, maxlinks, sendq);
342 c_class = find_class(name);
343 c_class->default_umode = pass;
344 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
345 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
348 parse_error("Missing name in class block");
352 classitems: classitem classitems | classitem;
353 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
354 classsendq | classusermode | priv | error;
355 classname: NAME '=' QSTRING ';'
358 DupString(name, yylval.text);
360 classpingfreq: PINGFREQ '=' timespec ';'
364 classconnfreq: CONNECTFREQ '=' timespec ';'
368 classmaxlinks: MAXLINKS '=' expr ';'
370 maxlinks = yylval.num;
372 classsendq: SENDQ '=' sizespec ';'
376 classusermode: USERMODE '=' QSTRING ';'
380 DupString(pass, yylval.text);
383 connectblock: CONNECT
385 name = pass = host = NULL;
388 } '{' connectitems '}'
390 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
391 && !strchr(host, '*') && !strchr(host, '?'))
394 aconf->status = CONF_SERVER;
396 aconf->passwd = pass;
397 aconf->conn_class = c_class;
398 aconf->address.port = port;
399 aconf->status = CONF_SERVER;
401 aconf->next = GlobalConfList;
402 lookup_confhost(aconf);
403 GlobalConfList = aconf;
410 name = pass = host = NULL;
411 parse_error("Bad connect block");
414 connectitems: connectitem connectitems | connectitem;
415 connectitem: connectname | connectpass | connectclass | connecthost
416 | connectport | error;
417 connectname: NAME '=' QSTRING ';'
420 DupString(name, yylval.text);
422 connectpass: PASS '=' QSTRING ';'
425 DupString(pass, yylval.text);
427 connectclass: CLASS '=' QSTRING ';'
429 c_class = find_class(yylval.text);
431 connecthost: HOST '=' QSTRING ';'
434 DupString(host, yylval.text);
436 connectport: PORT '=' NUMBER ';'
443 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
444 memset(aconf, 0, sizeof(*aconf));
445 } '{' serveritems '}'
447 if (aconf->status == 0)
453 parse_error("Bad server block");
457 aconf->next = GlobalConfList;
458 GlobalConfList = aconf;
461 serveritems: serveritem serveritems | serveritem;
462 serveritem: servername | servermask | serverhub | serverleaf |
463 serveruworld | error;
464 servername: NAME '=' QSTRING
467 DupString(aconf->name, yylval.text);
469 servermask: MASK '=' QSTRING
472 DupString(aconf->host, yylval.text);
474 /* XXX - perhaps we should do this the hybrid way in connect blocks
476 serverhub: HUB '=' YES ';'
478 aconf->status |= CONF_HUB;
479 aconf->status &= ~CONF_LEAF;
483 aconf->status &= ~CONF_HUB;
485 serverleaf: LEAF '=' YES ';'
487 if (!(aconf->status & CONF_HUB && aconf->status & CONF_UWORLD))
488 aconf->status |= CONF_LEAF;
490 parse_error("Server is both leaf and a hub");
494 aconf->status &= ~CONF_LEAF;
496 serveruworld: UWORLD '=' YES ';'
498 aconf->status |= CONF_UWORLD;
499 aconf->status &= ~CONF_LEAF;
503 aconf->status &= ~CONF_UWORLD;
508 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
509 memset(aconf, 0, sizeof(*aconf));
510 memset(&privs, 0, sizeof(privs));
511 memset(&privs_dirty, 0, sizeof(privs_dirty));
512 aconf->status = CONF_OPERATOR;
513 } '{' operitems '}' ';'
515 if (aconf->name != NULL && aconf->passwd != NULL && aconf->host != NULL
516 && aconf->conn_class != NULL)
518 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
519 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
520 if (!PrivHas(&privs_dirty, PRIV_PROPAGATE)
521 && !PrivHas(&aconf->conn_class->privs_dirty, PRIV_PROPAGATE))
522 parse_error("Operator block for %s and class %s have no LOCAL setting", aconf->name, aconf->conn_class->cc_name);
523 aconf->next = GlobalConfList;
524 GlobalConfList = aconf;
528 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
530 MyFree(aconf->passwd);
536 operitems: operitem | operitems operitem;
537 operitem: opername | operpass | operhost | operclass | priv | error;
539 opername: NAME '=' QSTRING ';'
542 DupString(aconf->name, yylval.text);
545 operpass: PASS '=' QSTRING ';'
547 MyFree(aconf->passwd);
548 DupString(aconf->passwd, yylval.text);
551 operhost: HOST '=' QSTRING ';'
554 if (!strchr(yylval.text, '@'))
557 char *b = (char*) MyMalloc((uh_len = strlen(yylval.text)+3));
558 ircd_snprintf(0, b, uh_len, "*@%s", yylval.text);
562 DupString(aconf->host, yylval.text);
565 operclass: CLASS '=' QSTRING ';'
567 aconf->conn_class = find_class(yylval.text);
570 priv: privtype '=' yesorno ';'
572 PrivSet(&privs_dirty, $1);
573 if (($3 == 1) ^ invert)
580 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
581 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
582 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
583 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
584 KILL { $$ = PRIV_KILL; } |
585 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
586 TPRIV_REHASH { $$ = PRIV_REHASH; } |
587 TPRIV_RESTART { $$ = PRIV_RESTART; } |
588 TPRIV_DIE { $$ = PRIV_DIE; } |
589 TPRIV_GLINE { $$ = PRIV_GLINE; } |
590 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
591 JUPE { $$ = PRIV_JUPE; } |
592 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
593 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
594 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
595 TPRIV_SET { $$ = PRIV_SET; } |
596 TPRIV_WHOX { $$ = PRIV_WHOX; } |
597 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
598 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
599 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
600 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
601 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
602 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
603 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
604 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
605 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
606 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
607 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
608 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
609 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
611 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
613 /* The port block... */
617 /* Hijack these for is_server, is_hidden to cut down on globals... */
620 /* and this for mask... */
622 } '{' portitems '}' ';'
624 if (port > 0 && port <= 0xFFFF)
626 add_listener(port, host, pass, tconn, tping);
630 parse_error("Bad port block");
636 portitems: portitem portitems | portitem;
637 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
638 portnumber: PORT '=' NUMBER ';'
643 portvhost: VHOST '=' QSTRING ';'
646 DupString(host, yylval.text);
649 portmask: MASK '=' QSTRING ';'
652 DupString(pass, yylval.text);
655 portserver: SERVER '=' YES ';'
658 } | SERVER '=' NO ';'
663 porthidden: HIDDEN '=' YES ';'
666 } | HIDDEN '=' NO ';'
673 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
674 memset(aconf, 0, sizeof(*aconf));
675 aconf->status = CONF_CLIENT;
676 } '{' clientitems '}'
678 if ((aconf->host != NULL || aconf->name!=NULL))
680 if (aconf->host == NULL)
681 DupString(aconf->host, "");
682 if (aconf->name == NULL)
683 DupString(aconf->name, "");
684 if (aconf->conn_class == NULL)
685 aconf->conn_class = find_class("default");
686 aconf->next = GlobalConfList;
687 GlobalConfList = aconf;
693 MyFree(aconf->passwd);
696 parse_error("Bad client block");
699 clientitems: clientitem clientitems | clientitem;
700 clientitem: clienthost | clientclass | clientpass | clientip | error;
701 clientip: IP '=' QSTRING ';'
704 DupString(aconf->host, yylval.text);
707 clienthost: HOST '=' QSTRING ';'
710 DupString(aconf->name, yylval.text);
713 clientclass: CLASS '=' QSTRING ';'
715 aconf->conn_class = find_class(yylval.text);
718 clientpass: PASS '=' QSTRING ';'
720 MyFree(aconf->passwd);
721 DupString(aconf->passwd, yylval.text);
726 dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
727 memset(dconf, 0, sizeof(*dconf));
730 if (dconf->hostmask != NULL)
732 if (dconf->usermask == NULL)
733 DupString(dconf->usermask, "*");
734 dconf->next = denyConfList;
735 denyConfList = dconf;
740 MyFree(dconf->hostmask);
741 MyFree(dconf->message);
744 parse_error("Bad kill block");
747 killitems: killitem killitems | killitem;
748 killitem: killuhost | killreal | killreasonfile | killreason | error;
749 killuhost: HOST '=' QSTRING ';'
752 dconf->flags &= ~DENY_FLAGS_REALNAME;
753 MyFree(dconf->hostmask);
754 MyFree(dconf->usermask);
755 if ((h = strchr(yylval.text, '@')) == NULL)
765 DupString(dconf->hostmask, h);
766 DupString(dconf->usermask, u);
767 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
770 killreal: REAL '=' QSTRING ';'
772 dconf->flags &= ~DENY_FLAGS_IP;
773 dconf->flags |= DENY_FLAGS_REALNAME;
774 MyFree(dconf->hostmask);
775 /* Leave usermask so you can specify user and real... */
776 DupString(dconf->hostmask, yylval.text);
779 killreason: REASON '=' QSTRING ';'
781 dconf->flags &= DENY_FLAGS_FILE;
782 MyFree(dconf->message);
783 DupString(dconf->message, yylval.text);
786 killreasonfile: TFILE '=' QSTRING ';'
788 dconf->flags |= DENY_FLAGS_FILE;
789 MyFree(dconf->message);
790 DupString(dconf->message, yylval.text);
799 struct CRuleNode *node;
800 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
802 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
807 p->next = cruleConfList;
814 parse_error("Bad CRule block");
818 cruleitems: cruleitem cruleitems | cruleitem;
819 cruleitem: cruleserver | crulerule | cruleall | error;
821 cruleserver: SERVER '=' QSTRING ';'
824 collapse(yylval.text);
825 DupString(host, yylval.text);
828 crulerule: RULE '=' QSTRING ';'
831 DupString(pass, yylval.text);
834 cruleall: ALL '=' YES ';'
846 if (host != NULL && pass != NULL)
847 motd_add(host, pass);
853 motditems: motditem motditems | motditem;
854 motditem: motdhost | motdfile | error;
855 motdhost: HOST '=' QSTRING ';'
857 DupString(host, yylval.text);
860 motdfile: TFILE '=' QSTRING ';'
862 DupString(pass, yylval.text);
865 featuresblock: FEATURES '{' featureitems '}' ';';
866 featureitems: featureitems featureitem | featureitem;
872 } '=' stringlist ';';
880 feature_set(NULL, (const char * const *)stringlist, stringno);
882 posextrastrings: /* empty */ | extrastrings;
883 extrastrings: extrastrings extrastring | extrastring;
886 if (stringno < MAX_STRINGS)
887 stringlist[stringno++] = $1;
890 quarantineblock: QUARANTINE '{'
893 qconf = (struct qline*) MyMalloc(sizeof(*qconf));
896 if (qconf->chname != NULL)
897 MyFree(qconf->chname);
898 if (qconf->reason != NULL)
899 MyFree(qconf->reason);
901 memset(qconf, 0, sizeof(*qconf));
902 } quarantineitems '}' ';'
904 if (qconf->chname == NULL || qconf->reason == NULL)
906 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
910 qconf->next = GlobalQuarantineList;
911 GlobalQuarantineList = qconf;
915 quarantineitems: CHANNEL NAME '=' QSTRING ';'
917 DupString(qconf->chname, yylval.text);
918 } | REASON '=' QSTRING ';'
920 DupString(qconf->reason, yylval.text);
923 pseudoblock: PSEUDO QSTRING '{'
925 smap = MyCalloc(1, sizeof(struct s_map));
926 DupString(smap->command, $2);
930 if (!smap->name || !smap->services)
932 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
935 if (register_mapping(smap))
937 smap->next = GlobalServiceMapList;
938 GlobalServiceMapList = smap;
942 struct nick_host *nh, *next;
943 for (nh = smap->services; nh; nh = next)
949 MyFree(smap->command);
950 MyFree(smap->prepend);
956 pseudoitems: pseudoitem pseudoitems | pseudoitem;
957 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
958 pseudoname: NAME '=' QSTRING ';'
960 DupString(smap->name, yylval.text);
962 pseudoprepend: PREPEND '=' QSTRING ';'
964 DupString(smap->prepend, yylval.text);
966 pseudonick: NICK '=' QSTRING ';'
968 char *sep = strchr(yylval.text, '@');
971 size_t slen = strlen(yylval.text);
972 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
973 memcpy(nh->nick, yylval.text, slen + 1);
974 nh->nicklen = sep - yylval.text;
975 nh->next = smap->services;
980 iauthblock: IAUTH '{'
988 if (!name || !host || !port) {
989 log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
992 iauth_connect(host, port, pass, tconn, tping);
998 iauthitems: iauthitem iauthitems | iauthitem;
999 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
1000 iauthpass: PASS '=' QSTRING ';'
1003 DupString(pass, yylval.text);
1005 iauthhost: HOST '=' QSTRING ';'
1008 DupString(host, yylval.text);
1010 iauthport: PORT '=' NUMBER ';'
1014 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1018 iauthtimeout: TIMEOUT '=' timespec ';'