2 * valuepair.c Valuepair functions that are radiusd-specific
3 * and as such do not belong in the library.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Copyright 2000 The FreeRADIUS server project
22 * Copyright 2000 Alan DeKok <aland@ox.org>
25 static const char rcsid[] = "$Id$";
28 #include "libradius.h"
35 # include <netinet/in.h>
42 * For POSIX Regular expressions.
43 * (0) Means no extended regular expressions.
44 * REG_EXTENDED means use extended regular expressions.
47 #define REG_EXTENDED (0)
56 void *instance; /* module instance */
57 RAD_COMPARE_FUNC compare;
60 static struct cmp *cmp;
64 * Compare 2 attributes. May call the attribute compare function.
66 static int paircompare(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
67 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
76 if (request->attribute != check->attribute)
81 * Check for =* and !* and return appropriately
83 if( check->operator == T_OP_CMP_TRUE )
84 return 0; /* always return 0/EQUAL */
85 if( check->operator == T_OP_CMP_FALSE )
86 return 1; /* always return 1/NOT EQUAL */
89 * See if there is a special compare function.
91 for (c = cmp; c; c = c->next)
92 if (c->attribute == check->attribute)
93 return (c->compare)(c->instance, req, request, check,
94 check_pairs, reply_pairs);
99 * Ascend binary attributes can be treated
100 * as opaque objects, I guess...
102 case PW_TYPE_ABINARY:
105 if (request->length != check->length) {
106 ret = 1; /* NOT equal */
109 ret = memcmp(request->strvalue, check->strvalue,
113 ret = strcmp((char *)request->strvalue,
114 (char *)check->strvalue);
116 case PW_TYPE_INTEGER:
118 ret = request->lvalue - check->lvalue;
121 ret = ntohl(request->lvalue) - ntohl(check->lvalue);
132 * See what attribute we want to compare with.
134 static int otherattr(int attr)
138 for (c = cmp; c; c = c->next) {
139 if (c->attribute == attr)
147 * Register a function as compare function.
148 * compare_attr is the attribute in the request we want to
149 * compare with. Normally this is the same as "attr".
150 * You can set this to:
152 * -1 the same as "attr"
153 * 0 always call compare function, not tied to request attribute
154 * >0 Attribute to compare with.
156 * For example, PW_GROUP in a check item needs to be compared
157 * with PW_USER_NAME in the incoming request.
159 int paircompare_register(int attr, int compare_attr, RAD_COMPARE_FUNC fun, void *instance)
163 paircompare_unregister(attr, fun);
165 c = rad_malloc(sizeof(struct cmp));
167 if (compare_attr < 0)
171 c->otherattr = compare_attr;
172 c->instance = instance;
180 * Unregister a function.
182 void paircompare_unregister(int attr, RAD_COMPARE_FUNC fun)
184 struct cmp *c, *last;
187 for (c = cmp; c; c = c->next) {
188 if (c->attribute == attr && c->compare == fun)
193 if (c == NULL) return;
196 last->next = c->next;
204 * Compare two pair lists except for the password information.
205 * For every element in "check" at least one matching copy must
206 * be present in "reply".
210 int paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check, VALUE_PAIR **reply)
212 VALUE_PAIR *check_item;
213 VALUE_PAIR *auth_item;
221 for (check_item = check; check_item != NULL; check_item = check_item->next) {
223 * If the user is setting a configuration value,
224 * then don't bother comparing it to any attributes
225 * sent to us by the user. It ALWAYS matches.
227 if ((check_item->operator == T_OP_SET) ||
228 (check_item->operator == T_OP_ADD)) {
232 switch (check_item->attribute) {
234 * Attributes we skip during comparison.
235 * These are "server" check items.
237 case PW_CRYPT_PASSWORD:
244 * IF the password attribute exists, THEN
245 * we can do comparisons against it. If not,
246 * then the request did NOT contain a
247 * User-Password attribute, so we CANNOT do
248 * comparisons against it.
250 * This hack makes CHAP-Password work..
253 if (pairfind(request, PW_PASSWORD) == NULL) {
260 * See if this item is present in the request.
262 other = otherattr(check_item->attribute);
266 for (; auth_item != NULL; auth_item = auth_item->next) {
267 if (auth_item->attribute == other || other == 0)
272 * Not found, it's not a match.
274 if (auth_item == NULL) {
279 * We've got to xlat the string before doing
282 if (check_item->flags.do_xlat) {
284 char buffer[sizeof(check_item->strvalue)];
286 check_item->flags.do_xlat = 0;
287 rcode = radius_xlat(buffer, sizeof(buffer),
288 check_item->strvalue,
292 * Parse the string into a new value.
294 pairparsevalue(check_item, buffer);
298 * OK it is present now compare them.
300 compare = paircompare(req, auth_item, check_item, check, reply);
302 switch (check_item->operator) {
305 radlog(L_ERR, "Invalid operator for item %s: "
306 "reverting to '=='", check_item->name);
308 case T_OP_CMP_TRUE: /* compare always == 0 */
309 case T_OP_CMP_FALSE: /* compare always == 1 */
311 if (compare != 0) result = -1;
315 if (compare == 0) result = -1;
319 if (compare >= 0) result = -1;
323 if (compare <= 0) result = -1;
327 if (compare > 0) result = -1;
331 if (compare < 0) result = -1;
336 regcomp(®, (char *)check_item->strvalue, REG_EXTENDED);
337 compare = regexec(®, (char *)auth_item->strvalue,
340 if (compare != 0) result = -1;
344 regcomp(®, (char *)check_item->strvalue, REG_EXTENDED);
345 compare = regexec(®, (char *)auth_item->strvalue,
348 if (compare == 0) result = -1;
352 } /* switch over the operator of the check item */
355 * This attribute didn't match, but maybe there's
356 * another of the same attribute, which DOES match.
359 auth_item = auth_item->next;
364 } /* for every entry in the check item list */
366 return 0; /* it matched */
370 * Compare two attributes simply. Calls paircompare.
373 int simplepaircmp(REQUEST *req, VALUE_PAIR *first, VALUE_PAIR *second)
375 return paircompare( req, first, second, NULL, NULL );
380 * Compare a Connect-Info and a Connect-Rate
382 static int connectcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
383 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
388 check_pairs = check_pairs; /* shut the compiler up */
389 reply_pairs = reply_pairs;
391 rate = atoi((char *)request->strvalue);
392 return rate - check->lvalue;
397 * Compare a portno with a range.
399 static int portcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
400 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
402 char buf[MAX_STRING_LEN];
405 uint32_t port = request->lvalue;
408 check_pairs = check_pairs; /* shut the compiler up */
409 reply_pairs = reply_pairs;
411 if ((strchr((char *)check->strvalue, ',') == NULL) &&
412 (strchr((char *)check->strvalue, '-') == NULL)) {
413 return (request->lvalue - check->lvalue);
417 strcpy(buf, (char *)check->strvalue);
418 s = strtok(buf, ",");
421 if ((p = strchr(s, '-')) != NULL)
425 lo = strtoul(s, NULL, 10);
426 hi = strtoul(p, NULL, 10);
427 if (lo <= port && port <= hi) {
430 s = strtok(NULL, ",");
437 * Compare prefix/suffix.
440 * - if PW_STRIP_USER_NAME is present in check_pairs,
441 * strip the username of prefix/suffix.
442 * - if PW_STRIP_USER_NAME is not present in check_pairs,
443 * add a PW_STRIPPED_USER_NAME to the request.
445 static int presufcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
446 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
449 char *name = (char *)request->strvalue;
450 char rest[MAX_STRING_LEN];
455 reply_pairs = reply_pairs; /* shut the compiler up */
458 printf("Comparing %s and %s, check->attr is %d\n",
459 name, check->strvalue, check->attribute);
462 len = strlen((char *)check->strvalue);
463 switch (check->attribute) {
465 ret = strncmp(name, (char *)check->strvalue, len);
466 if (ret == 0 && rest)
467 strcpy(rest, name + len);
470 namelen = strlen(name);
473 ret = strcmp(name + namelen - len,
474 (char *)check->strvalue);
475 if (ret == 0 && rest) {
476 strncpy(rest, name, namelen - len);
477 rest[namelen - len] = 0;
484 if (pairfind(check_pairs, PW_STRIP_USER_NAME)) {
486 * I don't think we want to update the User-Name
487 * attribute in place... - atd
489 strcpy((char *)request->strvalue, rest);
490 request->length = strlen(rest);
492 if ((vp = pairfind(check_pairs, PW_STRIPPED_USER_NAME)) != NULL){
493 strcpy((char *)vp->strvalue, rest);
494 vp->length = strlen(rest);
495 } else if ((vp = paircreate(PW_STRIPPED_USER_NAME,
496 PW_TYPE_STRING)) != NULL) {
497 strcpy((char *)vp->strvalue, rest);
498 vp->length = strlen(rest);
499 pairadd(&request, vp);
500 } /* else no memory! Die, die!: FIXME!! */
508 * Compare the current time to a range.
509 * Hmm... it would save work, and probably be better,
510 * if we were passed the REQUEST data structure, so we
511 * could use it's 'timestamp' element. That way, we could
512 * do the comparison against when the packet came in, not now,
513 * and have one less system call to do.
515 static int timecmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
516 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
519 request = request; /* shut the compiler up */
520 check_pairs = check_pairs;
521 reply_pairs = reply_pairs;
523 if (timestr_match((char *)check->strvalue, time(NULL)) >= 0) {
530 * Matches if there is NO SUCH ATTRIBUTE as the one named
531 * in check->strvalue. If there IS such an attribute, it
534 * This is ugly, and definitely non-optimal. We should be
535 * doing the lookup only ONCE, and storing the result
536 * in check->lvalue...
538 static int attrcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
539 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
546 check_pairs = check_pairs; /* shut the compiler up */
547 reply_pairs = reply_pairs;
549 if (check->lvalue == 0) {
550 dict = dict_attrbyname((char *)check->strvalue);
556 attr = check->lvalue;
560 * If there's no such attribute, then return MATCH,
563 pair = pairfind(request, attr);
572 * Compare the expiration date.
574 static int expirecmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
575 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
580 request = request; /* shut the compiler up */
581 check_pairs = check_pairs;
582 reply_pairs = reply_pairs;
585 * FIXME! This should be request->timestamp!
589 if (now <= check->lvalue) {
597 * Register server-builtin special attributes.
599 void pair_builtincompare_init(void)
601 paircompare_register(PW_NAS_PORT_ID, -1, portcmp, NULL);
602 paircompare_register(PW_PREFIX, PW_USER_NAME, presufcmp, NULL);
603 paircompare_register(PW_SUFFIX, PW_USER_NAME, presufcmp, NULL);
604 paircompare_register(PW_CONNECT_RATE, PW_CONNECT_INFO, connectcmp, NULL);
605 paircompare_register(PW_CURRENT_TIME, 0, timecmp, NULL);
606 paircompare_register(PW_NO_SUCH_ATTRIBUTE, 0, attrcmp, NULL);
607 paircompare_register(PW_EXPIRATION, 0, expirecmp, NULL);
611 * Move pairs, replacing/over-writing them, and doing xlat.
614 * Move attributes from one list to the other
615 * if not already present.
617 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
619 VALUE_PAIR **tailto, *i, *j, *next;
620 VALUE_PAIR *tailfrom = NULL;
624 * Point "tailto" to the end of the "to" list.
627 for(i = *to; i; i = i->next) {
632 * Loop over the "from" list.
634 for(i = *from; i; i = next) {
638 * Don't move 'fallthrough' over.
640 if (i->attribute == PW_FALL_THROUGH) {
645 * We've got to xlat the string before moving
648 if (i->flags.do_xlat) {
650 char buffer[sizeof(i->strvalue)];
652 i->flags.do_xlat = 0;
653 rcode = radius_xlat(buffer, sizeof(buffer),
658 * Parse the string into a new value.
660 pairparsevalue(i, buffer);
663 found = pairfind(*to, i->attribute);
664 switch (i->operator) {
667 * If a similar attribute is found,
670 case T_OP_SUB: /* -= */
672 if (!i->strvalue[0] ||
673 (strcmp((char *)found->strvalue,
674 (char *)i->strvalue) == 0)){
675 pairdelete(to, found->attribute);
678 * 'tailto' may have been
682 for(j = *to; j; j = j->next) {
692 * Add it, if it's not already there.
694 case T_OP_EQ: /* = */
697 continue; /* with the loop */
702 * If a similar attribute is found,
703 * replace it with the new one. Otherwise,
704 * add the new one to the list.
706 case T_OP_SET: /* := */
711 memcpy(found, i, sizeof(*found));
717 * FIXME: Add support for <=, >=, <, >
719 * which will mean (for integers)
720 * 'make the attribute the smaller, etc'
724 * Add the new element to the list, even
725 * if similar ones already exist.
728 case T_OP_ADD: /* += */
733 tailfrom->next = next;
738 * If ALL of the 'to' attributes have been deleted,
739 * then ensure that the 'tail' is updated to point
750 } /* loop over the 'from' list */