3 * connection rule patch
4 * by Tony Vencill (Tonto on IRC) <vencill@bga.com>
6 * The majority of this file is a recusive descent parser used to convert
7 * connection rules into expression trees when the conf file is read.
8 * All parsing structures and types are hidden in the interest of good
9 * programming style and to make possible future data structure changes
10 * without affecting the interface between this patch and the rest of the
11 * server. The only functions accessible externally are crule_parse,
12 * crule_free, and crule_eval. Prototypes for these functions can be
15 * Please direct any connection rule or SmartRoute questions to Tonto on
16 * IRC or by email to vencill@bga.com.
18 * For parser testing, defining CR_DEBUG generates a stand-alone parser
19 * that takes rules from stdin and prints out memory allocation
20 * information and the parsed rule. This stand alone parser is ignorant
21 * of the irc server and thus cannot do rule evaluation. Do not define
22 * this flag when compiling the server! If you wish to generate the
23 * test parser, compile from the ircd directory with a line similar to
24 * cc -o parser -DCR_DEBUG crule.c
26 * The define CR_CHKCONF is provided to generate routines needed in
27 * chkconf. These consist of the parser, a different crule_parse that
28 * prints errors to stderr, and crule_free (just for good style and to
29 * more closely simulate the actual ircd environment). crule_eval and
30 * the rule functions are made empty functions as in the stand-alone
38 /* ircd functions and types we need */
41 #include "ircd_alloc.h"
42 #include "ircd_chattr.h"
43 #include "ircd_string.h"
52 #else /* includes and defines to make the stand-alone test parser */
57 #define BadPtr(x) (!(x) || (*(x) == '\0'))
58 #define DupString(x,y) \
60 x = (char*) MyMalloc(strlen(y)+1); \
64 /* We don't care about collation discrepacies here, it seems.... */
65 #define ircd_strcmp strcasecmp
72 #if defined(CR_DEBUG) || defined(CR_CHKCONF)
75 #define MyMalloc malloc
81 /* some constants and shared data types */
82 #define CR_MAXARGLEN 80 /* why 80? why not? it's > hostname lengths */
83 #define CR_MAXARGS 3 /* There's a better way to do this,
87 * Some symbols for easy reading
91 CR_UNKNOWN, CR_END, CR_AND, CR_OR, CR_NOT, CR_OPENPAREN, CR_CLOSEPAREN,
96 CR_NOERR, CR_UNEXPCTTOK, CR_UNKNWTOK, CR_EXPCTAND, CR_EXPCTOR,
97 CR_EXPCTPRIM, CR_EXPCTOPEN, CR_EXPCTCLOSE, CR_UNKNWFUNC, CR_ARGMISMAT
101 * Expression tree structure, function pointer, and tree pointer local!
103 typedef int (*crule_funcptr) (int, void **);
105 struct crule_treestruct {
106 crule_funcptr funcptr;
108 void *arg[CR_MAXARGS]; /* For operators arg points to a tree element;
109 for functions arg points to a char string. */
112 typedef struct crule_treestruct crule_treeelem;
113 typedef crule_treeelem *crule_treeptr;
115 /* local rule function prototypes */
116 static int crule_connected(int, void **);
117 static int crule_directcon(int, void **);
118 static int crule_via(int, void **);
119 static int crule_directop(int, void **);
120 static int crule__andor(int, void **);
121 static int crule__not(int, void **);
123 /* local parsing function prototypes */
124 static int crule_gettoken(int *, char **);
125 static void crule_getword(char *, int *, size_t, char **);
126 static int crule_parseandexpr(crule_treeptr *, int *, char **);
127 static int crule_parseorexpr(crule_treeptr *, int *, char **);
128 static int crule_parseprimary(crule_treeptr *, int *, char **);
129 static int crule_parsefunction(crule_treeptr *, int *, char **);
130 static int crule_parsearglist(crule_treeptr, int *, char **);
132 #if defined(CR_DEBUG) || defined(CR_CHKCONF)
134 * Prototypes for the test parser; if not debugging,
135 * these are defined in h.h
137 char *crule_parse(char *);
138 void crule_free(char **);
140 void print_tree(crule_treeptr);
145 char *crule_errstr[] = {
146 "Unknown error", /* NOERR? - for completeness */
147 "Unexpected token", /* UNEXPCTTOK */
148 "Unknown token", /* UNKNWTOK */
149 "And expr expected", /* EXPCTAND */
150 "Or expr expected", /* EXPCTOR */
151 "Primary expected", /* EXPCTPRIM */
152 "( expected", /* EXPCTOPEN */
153 ") expected", /* EXPCTCLOSE */
154 "Unknown function", /* UNKNWFUNC */
155 "Argument mismatch" /* ARGMISMAT */
158 /* function table - null terminated */
159 struct crule_funclistent {
160 char name[15]; /* MAXIMUM FUNCTION NAME LENGTH IS 14 CHARS!! */
162 crule_funcptr funcptr;
165 struct crule_funclistent crule_funclist[] = {
166 /* maximum function name length is 14 chars */
167 {"connected", 1, crule_connected},
168 {"directcon", 1, crule_directcon},
169 {"via", 2, crule_via},
170 {"directop", 0, crule_directop},
171 {"", 0, NULL} /* this must be here to mark end of list */
174 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
175 static int crule_connected(int numargs, void *crulearg[])
177 struct Client *acptr;
179 /* taken from m_links */
180 for (acptr = GlobalClientList; acptr; acptr = acptr->next)
182 if (!IsServer(acptr) && !IsMe(acptr))
184 if (match((char *)crulearg[0], acptr->name))
191 static int crule_connected(int numargs, void **crulearg)
197 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
198 static int crule_directcon(int numargs, void *crulearg[])
201 struct Client *acptr;
203 /* adapted from m_trace and exit_one_client */
204 for (i = 0; i <= HighestFd; i++)
206 if (!(acptr = LocalClientArray[i]) || !IsServer(acptr))
208 if (match((char *)crulearg[0], acptr->name))
215 static int crule_directcon(int numargs, void **crulearg)
221 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
222 static int crule_via(int numargs, void *crulearg[])
224 struct Client *acptr;
226 /* adapted from m_links */
227 for (acptr = GlobalClientList; acptr; acptr = acptr->next)
229 if (!IsServer(acptr) && !IsMe(acptr))
231 if (match((char *)crulearg[1], acptr->name))
233 if (match((char *)crulearg[0], (LocalClientArray[acptr->from->fd])->name))
240 static int crule_via(int numargs, void **crulearg)
246 static int crule_directop(int numargs, void **crulearg)
248 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
250 struct Client *acptr;
252 /* adapted from m_trace */
253 for (i = 0; i <= HighestFd; i++)
255 if (!(acptr = LocalClientArray[i]) || !IsAnOper(acptr))
263 static int crule__andor(int numargs, void *crulearg[])
267 result1 = ((crule_treeptr) crulearg[0])->funcptr
268 (((crule_treeptr) crulearg[0])->numargs,
269 ((crule_treeptr) crulearg[0])->arg);
270 if (crulearg[2]) /* or */
272 ((crule_treeptr) crulearg[1])->funcptr
273 (((crule_treeptr) crulearg[1])->numargs,
274 ((crule_treeptr) crulearg[1])->arg));
277 ((crule_treeptr) crulearg[1])->funcptr
278 (((crule_treeptr) crulearg[1])->numargs,
279 ((crule_treeptr) crulearg[1])->arg));
282 static int crule__not(int numargs, void *crulearg[])
284 return (!((crule_treeptr) crulearg[0])->funcptr
285 (((crule_treeptr) crulearg[0])->numargs,
286 ((crule_treeptr) crulearg[0])->arg));
289 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
290 int crule_eval(char *rule)
292 return (((crule_treeptr) rule)->funcptr
293 (((crule_treeptr) rule)->numargs, ((crule_treeptr) rule)->arg));
297 static int crule_gettoken(int *next_tokp, char **ruleptr)
301 *next_tokp = CR_UNKNOWN;
302 while (*next_tokp == CR_UNKNOWN)
303 switch (*(*ruleptr)++)
311 else if (pending == '&')
314 return (CR_UNKNWTOK);
319 else if (pending == '|')
322 return (CR_UNKNWTOK);
328 *next_tokp = CR_OPENPAREN;
331 *next_tokp = CR_CLOSEPAREN;
334 *next_tokp = CR_COMMA;
344 if ((IsAlpha(*(--(*ruleptr)))) || (**ruleptr == '*') ||
345 (**ruleptr == '?') || (**ruleptr == '.') || (**ruleptr == '-'))
346 *next_tokp = CR_WORD;
348 return (CR_UNKNWTOK);
354 static void crule_getword(char *word, int *wordlenp, size_t maxlen,
360 while ((size_t)(word_ptr - word) < maxlen
361 && (IsAlnum(**ruleptr)
362 || **ruleptr == '*' || **ruleptr == '?'
363 || **ruleptr == '.' || **ruleptr == '-'))
364 *word_ptr++ = *(*ruleptr)++;
366 *wordlenp = word_ptr - word;
372 * orexpr END END is end of input or :
384 * word ( ) word is alphanumeric string, first character
385 * word ( arglist ) must be a letter
390 char *crule_parse(char *rule)
392 char *ruleptr = rule;
394 crule_treeptr ruleroot = NULL;
395 int errcode = CR_NOERR;
397 if ((errcode = crule_gettoken(&next_tok, &ruleptr)) == CR_NOERR)
399 if ((errcode = crule_parseorexpr(&ruleroot, &next_tok, &ruleptr))
402 if (ruleroot != NULL)
404 if (next_tok == CR_END)
405 return ((char *)ruleroot);
407 errcode = CR_UNEXPCTTOK;
410 errcode = CR_EXPCTOR;
413 if (ruleroot != NULL)
414 crule_free((char **)&ruleroot);
415 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
416 Debug((DEBUG_ERROR, "%s in rule: %s", crule_errstr[errcode], rule));
418 fprintf(stderr, "%s in rule: %s\n", crule_errstr[errcode], rule);
423 static int crule_parseorexpr(crule_treeptr * orrootp, int *next_tokp,
426 int errcode = CR_NOERR;
427 crule_treeptr andexpr;
431 while (errcode == CR_NOERR)
433 errcode = crule_parseandexpr(&andexpr, next_tokp, ruleptr);
434 if ((errcode == CR_NOERR) && (*next_tokp == CR_OR))
436 orptr = (crule_treeptr) MyMalloc(sizeof(crule_treeelem));
438 fprintf(stderr, "allocating or element at %ld\n", orptr);
440 orptr->funcptr = crule__andor;
442 orptr->arg[2] = (void *)1;
443 if (*orrootp != NULL)
445 (*orrootp)->arg[1] = andexpr;
446 orptr->arg[0] = *orrootp;
449 orptr->arg[0] = andexpr;
454 if (*orrootp != NULL)
458 (*orrootp)->arg[1] = andexpr;
463 (*orrootp)->arg[1] = NULL; /* so free doesn't seg fault */
464 return (CR_EXPCTAND);
473 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
479 static int crule_parseandexpr(crule_treeptr * androotp, int *next_tokp,
482 int errcode = CR_NOERR;
483 crule_treeptr primary;
484 crule_treeptr andptr;
487 while (errcode == CR_NOERR)
489 errcode = crule_parseprimary(&primary, next_tokp, ruleptr);
490 if ((errcode == CR_NOERR) && (*next_tokp == CR_AND))
492 andptr = (crule_treeptr) MyMalloc(sizeof(crule_treeelem));
494 fprintf(stderr, "allocating and element at %ld\n", andptr);
496 andptr->funcptr = crule__andor;
498 andptr->arg[2] = (void *)0;
499 if (*androotp != NULL)
501 (*androotp)->arg[1] = primary;
502 andptr->arg[0] = *androotp;
505 andptr->arg[0] = primary;
510 if (*androotp != NULL)
514 (*androotp)->arg[1] = primary;
519 (*androotp)->arg[1] = NULL; /* so free doesn't seg fault */
520 return (CR_EXPCTPRIM);
529 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
535 static int crule_parseprimary(crule_treeptr * primrootp,
536 int *next_tokp, char **ruleptr)
538 crule_treeptr *insertionp;
539 int errcode = CR_NOERR;
542 insertionp = primrootp;
543 while (errcode == CR_NOERR)
548 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
550 if ((errcode = crule_parseorexpr(insertionp, next_tokp,
551 ruleptr)) != CR_NOERR)
553 if (*insertionp == NULL)
555 errcode = CR_EXPCTAND;
558 if (*next_tokp != CR_CLOSEPAREN)
560 errcode = CR_EXPCTCLOSE;
563 errcode = crule_gettoken(next_tokp, ruleptr);
566 *insertionp = (crule_treeptr) MyMalloc(sizeof(crule_treeelem));
568 fprintf(stderr, "allocating primary element at %ld\n", *insertionp);
570 (*insertionp)->funcptr = crule__not;
571 (*insertionp)->numargs = 1;
572 (*insertionp)->arg[0] = NULL;
573 insertionp = (crule_treeptr *) & ((*insertionp)->arg[0]);
574 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
578 errcode = crule_parsefunction(insertionp, next_tokp, ruleptr);
581 if (*primrootp == NULL)
584 errcode = CR_EXPCTPRIM;
592 static int crule_parsefunction(crule_treeptr * funcrootp,
593 int *next_tokp, char **ruleptr)
595 int errcode = CR_NOERR;
596 char funcname[CR_MAXARGLEN];
601 crule_getword(funcname, &namelen, CR_MAXARGLEN - 1, ruleptr);
602 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
604 if (*next_tokp == CR_OPENPAREN)
606 for (funcnum = 0;; funcnum++)
608 if (0 == ircd_strcmp(crule_funclist[funcnum].name, funcname))
610 if (crule_funclist[funcnum].name[0] == '\0')
611 return (CR_UNKNWFUNC);
613 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
615 *funcrootp = (crule_treeptr) MyMalloc(sizeof(crule_treeelem));
617 fprintf(stderr, "allocating function element at %ld\n", *funcrootp);
619 (*funcrootp)->funcptr = NULL; /* for freeing aborted trees */
621 crule_parsearglist(*funcrootp, next_tokp, ruleptr)) != CR_NOERR)
623 if (*next_tokp != CR_CLOSEPAREN)
624 return (CR_EXPCTCLOSE);
625 if ((crule_funclist[funcnum].reqnumargs != (*funcrootp)->numargs) &&
626 (crule_funclist[funcnum].reqnumargs != -1))
627 return (CR_ARGMISMAT);
628 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
630 (*funcrootp)->funcptr = crule_funclist[funcnum].funcptr;
634 return (CR_EXPCTOPEN);
637 static int crule_parsearglist(crule_treeptr argrootp, int *next_tokp,
640 int errcode = CR_NOERR;
641 char *argelemp = NULL;
642 char currarg[CR_MAXARGLEN];
644 char word[CR_MAXARGLEN];
647 argrootp->numargs = 0;
649 while (errcode == CR_NOERR)
654 crule_getword(word, &wordlen, CR_MAXARGLEN - 1, ruleptr);
655 if (currarg[0] != '\0')
657 if ((arglen + wordlen) < (CR_MAXARGLEN - 1))
659 strcat(currarg, " ");
660 strcat(currarg, word);
661 arglen += wordlen + 1;
666 strcpy(currarg, word);
669 errcode = crule_gettoken(next_tokp, ruleptr);
672 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
675 if (!BadPtr(currarg))
677 DupString(argelemp, currarg);
678 argrootp->arg[argrootp->numargs++] = (void *)argelemp;
680 if (*next_tokp != CR_COMMA)
683 errcode = crule_gettoken(next_tokp, ruleptr);
691 * This function is recursive.. I wish I knew a nonrecursive way but
692 * I dont. anyway, recursion is fun.. :)
693 * DO NOT CALL THIS FUNTION WITH A POINTER TO A NULL POINTER
694 * (ie: If *elem is NULL, you're doing it wrong - seg fault)
696 void crule_free(char **elem)
700 if ((*((crule_treeptr *) elem))->funcptr == crule__not)
702 /* type conversions and ()'s are fun! ;) here have an asprin.. */
703 if ((*((crule_treeptr *) elem))->arg[0] != NULL)
704 crule_free((char **)&((*((crule_treeptr *) elem))->arg[0]));
706 else if ((*((crule_treeptr *) elem))->funcptr == crule__andor)
708 crule_free((char **)&((*((crule_treeptr *) elem))->arg[0]));
709 if ((*((crule_treeptr *) elem))->arg[1] != NULL)
710 crule_free((char **)&((*((crule_treeptr *) elem))->arg[1]));
714 numargs = (*((crule_treeptr *) elem))->numargs;
715 for (arg = 0; arg < numargs; arg++)
716 MyFree((*((crule_treeptr *) elem))->arg[arg]);
719 fprintf(stderr, "freeing element at %ld\n", *elem);
726 static void print_tree(crule_treeptr printelem)
730 if (printelem->funcptr == crule__not)
733 print_tree((crule_treeptr) printelem->arg[0]);
736 else if (printelem->funcptr == crule__andor)
739 print_tree((crule_treeptr) printelem->arg[0]);
740 if (printelem->arg[2])
744 print_tree((crule_treeptr) printelem->arg[1]);
749 for (funcnum = 0;; funcnum++)
751 if (printelem->funcptr == crule_funclist[funcnum].funcptr)
753 if (crule_funclist[funcnum].funcptr == NULL)
756 printf("%s(", crule_funclist[funcnum].name);
757 for (arg = 0; arg < printelem->numargs; arg++)
761 printf("%s", (char *)printelem->arg[arg]);
776 while (fgets(indata, 256, stdin) != NULL)
778 indata[strlen(indata) - 1] = '\0'; /* lose the newline */
779 if ((rule = crule_parse(indata)) != NULL)
781 printf("equivalent rule: ");
782 print_tree((crule_treeptr) rule);