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"
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, *origin;
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 |
186 pseudoblock | iauthblock | error;
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
267 generalblock: GENERAL '{' generalitems '}'
269 if (localConf.name == NULL)
270 parse_error("Your General block must contain a name.");
271 if (localConf.numeric == 0)
272 parse_error("Your General block must contain a numeric (between 1 and 4095).");
274 generalitems: generalitem generalitems | generalitem;
275 generalitem: generalnumeric | generalname | generalvhost | generaldesc | error;
276 generalnumeric: NUMERIC '=' NUMBER ';'
278 if (localConf.numeric == 0)
279 localConf.numeric = $3;
280 else if (localConf.numeric != $3)
281 parse_error("Redefinition of server numeric %i (%i)", $3,
285 generalname: NAME '=' QSTRING ';'
287 if (localConf.name == NULL)
288 DupString(localConf.name, $3);
289 else if (strcmp(localConf.name, $3))
290 parse_error("Redefinition of server name %s (%s)", $3,
294 generaldesc: DESCRIPTION '=' QSTRING ';'
296 MyFree(localConf.description);
297 DupString(localConf.description, $3);
298 ircd_strncpy(cli_info(&me), $3, REALLEN);
301 generalvhost: VHOST '=' QSTRING ';'
303 ircd_aton(&VirtualHost.addr, $3);
306 adminblock: ADMIN '{' adminitems '}'
308 if (localConf.location1 == NULL)
309 DupString(localConf.location1, "");
310 if (localConf.location2 == NULL)
311 DupString(localConf.location2, "");
312 if (localConf.contact == NULL)
313 DupString(localConf.contact, "");
315 adminitems: adminitems adminitem | adminitem;
316 adminitem: adminlocation | admincontact | error;
317 adminlocation: LOCATION '=' QSTRING ';'
319 if (localConf.location1 == NULL)
320 DupString(localConf.location1, $3);
321 else if (localConf.location2 == NULL)
322 DupString(localConf.location2, $3);
323 /* Otherwise just drop it. -A1kmm */
325 admincontact: CONTACT '=' QSTRING ';'
327 if (localConf.contact != NULL)
328 MyFree(localConf.contact);
329 DupString(localConf.contact, $3);
339 memset(&privs, 0, sizeof(privs));
340 memset(&privs_dirty, 0, sizeof(privs_dirty));
345 struct ConnectionClass *c_class;
346 add_class(name, tping, tconn, maxlinks, sendq);
347 c_class = find_class(name);
348 c_class->default_umode = pass;
349 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
350 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
353 parse_error("Missing name in class block");
357 classitems: classitem classitems | classitem;
358 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
359 classsendq | classusermode | priv | error;
360 classname: NAME '=' QSTRING ';'
365 classpingfreq: PINGFREQ '=' timespec ';'
369 classconnfreq: CONNECTFREQ '=' timespec ';'
373 classmaxlinks: MAXLINKS '=' expr ';'
377 classsendq: SENDQ '=' sizespec ';'
381 classusermode: USERMODE '=' QSTRING ';'
388 connectblock: CONNECT
390 name = pass = host = origin = NULL;
393 } '{' connectitems '}'
395 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
396 && !strchr(host, '*') && !strchr(host, '?'))
399 aconf->status = CONF_SERVER;
401 aconf->origin_name = origin;
402 aconf->passwd = pass;
403 aconf->conn_class = c_class;
404 aconf->address.port = port;
405 aconf->status = CONF_SERVER;
407 aconf->next = GlobalConfList;
408 lookup_confhost(aconf);
409 GlobalConfList = aconf;
417 parse_error("Bad connect block");
419 name = pass = host = origin = NULL;
421 connectitems: connectitem connectitems | connectitem;
422 connectitem: connectname | connectpass | connectclass | connecthost
423 | connectport | connectvhost | error;
424 connectname: NAME '=' QSTRING ';'
429 connectpass: PASS '=' QSTRING ';'
434 connectclass: CLASS '=' QSTRING ';'
436 c_class = find_class($3);
438 connecthost: HOST '=' QSTRING ';'
443 connectport: PORT '=' NUMBER ';'
447 connectvhost: VHOST '=' QSTRING ';'
450 DupString(origin, $3);
455 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
456 memset(aconf, 0, sizeof(*aconf));
457 aconf->status = CONF_LEAF;
458 } '{' serveritems '}'
460 if (aconf->status == 0)
466 parse_error("Bad server block");
470 aconf->next = GlobalConfList;
471 GlobalConfList = aconf;
474 serveritems: serveritem serveritems | serveritem;
475 serveritem: servername | servermask | serverhub | serverleaf |
476 serveruworld | error;
477 servername: NAME '=' QSTRING
480 DupString(aconf->name, $3);
482 servermask: MASK '=' QSTRING
485 DupString(aconf->host, $3);
487 /* XXX - perhaps we should do this the hybrid way in connect blocks
489 serverhub: HUB '=' YES ';'
491 aconf->status |= CONF_HUB;
492 aconf->status &= ~CONF_LEAF;
496 aconf->status &= ~CONF_HUB;
498 serverleaf: LEAF '=' YES ';'
500 if (!(aconf->status & CONF_HUB && aconf->status & CONF_UWORLD))
501 aconf->status |= CONF_LEAF;
503 parse_error("Server is both leaf and a hub");
507 aconf->status &= ~CONF_LEAF;
509 serveruworld: UWORLD '=' YES ';'
511 aconf->status |= CONF_UWORLD;
512 aconf->status &= ~CONF_LEAF;
516 aconf->status &= ~CONF_UWORLD;
521 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
522 memset(aconf, 0, sizeof(*aconf));
523 memset(&privs, 0, sizeof(privs));
524 memset(&privs_dirty, 0, sizeof(privs_dirty));
525 aconf->status = CONF_OPERATOR;
526 } '{' operitems '}' ';'
528 if (aconf->name != NULL && aconf->passwd != NULL && aconf->host != NULL
529 && aconf->conn_class != NULL)
531 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
532 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
533 if (!PrivHas(&privs_dirty, PRIV_PROPAGATE)
534 && !PrivHas(&aconf->conn_class->privs_dirty, PRIV_PROPAGATE))
535 parse_error("Operator block for %s and class %s have no LOCAL setting", aconf->name, aconf->conn_class->cc_name);
536 aconf->next = GlobalConfList;
537 GlobalConfList = aconf;
541 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
543 MyFree(aconf->passwd);
549 operitems: operitem | operitems operitem;
550 operitem: opername | operpass | operhost | operclass | priv | error;
552 opername: NAME '=' QSTRING ';'
555 DupString(aconf->name, $3);
558 operpass: PASS '=' QSTRING ';'
560 MyFree(aconf->passwd);
561 DupString(aconf->passwd, $3);
564 operhost: HOST '=' QSTRING ';'
567 if (!strchr($3, '@'))
570 char *b = (char*) MyMalloc((uh_len = strlen($3)+3));
571 ircd_snprintf(0, b, uh_len, "*@%s", $3);
575 DupString(aconf->host, $3);
578 operclass: CLASS '=' QSTRING ';'
580 aconf->conn_class = find_class($3);
583 priv: privtype '=' yesorno ';'
585 PrivSet(&privs_dirty, $1);
586 if (($3 == 1) ^ invert)
593 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
594 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
595 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
596 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
597 KILL { $$ = PRIV_KILL; } |
598 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
599 TPRIV_REHASH { $$ = PRIV_REHASH; } |
600 TPRIV_RESTART { $$ = PRIV_RESTART; } |
601 TPRIV_DIE { $$ = PRIV_DIE; } |
602 TPRIV_GLINE { $$ = PRIV_GLINE; } |
603 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
604 JUPE { $$ = PRIV_JUPE; } |
605 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
606 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
607 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
608 TPRIV_SET { $$ = PRIV_SET; } |
609 TPRIV_WHOX { $$ = PRIV_WHOX; } |
610 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
611 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
612 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
613 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
614 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
615 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
616 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
617 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
618 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
619 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
620 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
621 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
622 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
624 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
626 /* The port block... */
630 /* Hijack these for is_server, is_hidden to cut down on globals... */
633 /* and this for mask... */
635 } '{' portitems '}' ';'
637 if (port > 0 && port <= 0xFFFF)
639 add_listener(port, host, pass, tconn, tping);
643 parse_error("Bad port block");
649 portitems: portitem portitems | portitem;
650 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
651 portnumber: PORT '=' NUMBER ';'
656 portvhost: VHOST '=' QSTRING ';'
662 portmask: MASK '=' QSTRING ';'
668 portserver: SERVER '=' YES ';'
671 } | SERVER '=' NO ';'
676 porthidden: HIDDEN '=' YES ';'
679 } | HIDDEN '=' NO ';'
686 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
687 memset(aconf, 0, sizeof(*aconf));
688 aconf->status = CONF_CLIENT;
689 } '{' clientitems '}'
691 if ((aconf->host != NULL || aconf->name!=NULL))
693 if (aconf->host == NULL)
694 DupString(aconf->host, "");
695 if (aconf->name == NULL)
696 DupString(aconf->name, "");
697 if (aconf->conn_class == NULL)
698 aconf->conn_class = find_class("default");
699 aconf->next = GlobalConfList;
700 GlobalConfList = aconf;
706 MyFree(aconf->passwd);
709 parse_error("Bad client block");
712 clientitems: clientitem clientitems | clientitem;
713 clientitem: clienthost | clientclass | clientpass | clientip | error;
714 clientip: IP '=' QSTRING ';'
717 DupString(aconf->host, $3);
720 clienthost: HOST '=' QSTRING ';'
723 DupString(aconf->name, $3);
726 clientclass: CLASS '=' QSTRING ';'
728 aconf->conn_class = find_class($3);
731 clientpass: PASS '=' QSTRING ';'
733 MyFree(aconf->passwd);
734 DupString(aconf->passwd, $3);
739 dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
740 memset(dconf, 0, sizeof(*dconf));
743 if (dconf->hostmask != NULL)
745 if (dconf->usermask == NULL)
746 DupString(dconf->usermask, "*");
747 dconf->next = denyConfList;
748 denyConfList = dconf;
753 MyFree(dconf->hostmask);
754 MyFree(dconf->message);
757 parse_error("Bad kill block");
760 killitems: killitem killitems | killitem;
761 killitem: killuhost | killreal | killreasonfile | killreason | error;
762 killuhost: HOST '=' QSTRING ';'
765 dconf->flags &= ~DENY_FLAGS_REALNAME;
766 MyFree(dconf->hostmask);
767 MyFree(dconf->usermask);
768 if ((h = strchr($3, '@')) == NULL)
778 DupString(dconf->hostmask, h);
779 DupString(dconf->usermask, u);
780 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
783 killreal: REAL '=' QSTRING ';'
785 dconf->flags &= ~DENY_FLAGS_IP;
786 dconf->flags |= DENY_FLAGS_REALNAME;
787 MyFree(dconf->hostmask);
788 /* Leave usermask so you can specify user and real... */
789 DupString(dconf->hostmask, $3);
792 killreason: REASON '=' QSTRING ';'
794 dconf->flags &= DENY_FLAGS_FILE;
795 MyFree(dconf->message);
796 DupString(dconf->message, $3);
799 killreasonfile: TFILE '=' QSTRING ';'
801 dconf->flags |= DENY_FLAGS_FILE;
802 MyFree(dconf->message);
803 DupString(dconf->message, $3);
812 struct CRuleNode *node;
813 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
815 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
820 p->next = cruleConfList;
827 parse_error("Bad CRule block");
831 cruleitems: cruleitem cruleitems | cruleitem;
832 cruleitem: cruleserver | crulerule | cruleall | error;
834 cruleserver: SERVER '=' QSTRING ';'
841 crulerule: RULE '=' QSTRING ';'
847 cruleall: ALL '=' YES ';'
859 if (host != NULL && pass != NULL)
860 motd_add(host, pass);
866 motditems: motditem motditems | motditem;
867 motditem: motdhost | motdfile | error;
868 motdhost: HOST '=' QSTRING ';'
873 motdfile: TFILE '=' QSTRING ';'
878 featuresblock: FEATURES '{' featureitems '}' ';';
879 featureitems: featureitems featureitem | featureitem;
885 } '=' stringlist ';';
893 feature_set(NULL, (const char * const *)stringlist, stringno);
895 posextrastrings: /* empty */ | extrastrings;
896 extrastrings: extrastrings extrastring | extrastring;
899 if (stringno < MAX_STRINGS)
900 stringlist[stringno++] = $1;
903 quarantineblock: QUARANTINE '{'
906 qconf = (struct qline*) MyMalloc(sizeof(*qconf));
909 if (qconf->chname != NULL)
910 MyFree(qconf->chname);
911 if (qconf->reason != NULL)
912 MyFree(qconf->reason);
914 memset(qconf, 0, sizeof(*qconf));
915 } quarantineitems '}' ';'
917 if (qconf->chname == NULL || qconf->reason == NULL)
919 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
923 qconf->next = GlobalQuarantineList;
924 GlobalQuarantineList = qconf;
928 quarantineitems: CHANNEL NAME '=' QSTRING ';'
930 DupString(qconf->chname, $4);
931 } | REASON '=' QSTRING ';'
933 DupString(qconf->reason, $3);
936 pseudoblock: PSEUDO QSTRING '{'
938 smap = MyCalloc(1, sizeof(struct s_map));
939 DupString(smap->command, $2);
943 if (!smap->name || !smap->services)
945 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
948 if (register_mapping(smap))
950 smap->next = GlobalServiceMapList;
951 GlobalServiceMapList = smap;
955 struct nick_host *nh, *next;
956 for (nh = smap->services; nh; nh = next)
962 MyFree(smap->command);
963 MyFree(smap->prepend);
969 pseudoitems: pseudoitem pseudoitems | pseudoitem;
970 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
971 pseudoname: NAME '=' QSTRING ';'
973 DupString(smap->name, $3);
975 pseudoprepend: PREPEND '=' QSTRING ';'
977 DupString(smap->prepend, $3);
979 pseudonick: NICK '=' QSTRING ';'
981 char *sep = strchr($3, '@');
984 size_t slen = strlen($3);
985 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
986 memcpy(nh->nick, $3, slen + 1);
987 nh->nicklen = sep - $3;
988 nh->next = smap->services;
993 iauthblock: IAUTH '{'
1001 if (!host || !port) {
1002 log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
1005 iauth_connect(host, port, pass, tconn, tping);
1011 iauthitems: iauthitem iauthitems | iauthitem;
1012 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
1013 iauthpass: PASS '=' QSTRING ';'
1016 DupString(pass, $3);
1018 iauthhost: HOST '=' QSTRING ';'
1021 DupString(host, $3);
1023 iauthport: PORT '=' NUMBER ';'
1027 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1031 iauthtimeout: TIMEOUT '=' timespec ';'