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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * Copyright 2000,2006 The FreeRADIUS server project
22 * Copyright 2000 Alan DeKok <aland@ox.org>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/rad_assert.h>
35 * For POSIX Regular expressions.
36 * (0) Means no extended regular expressions.
37 * REG_EXTENDED means use extended regular expressions.
40 #define REG_EXTENDED (0)
51 void *instance; /* module instance */
52 RAD_COMPARE_FUNC compare;
55 static struct cmp *cmp;
57 int radius_compare_vps(REQUEST *request, VALUE_PAIR *check, VALUE_PAIR *vp)
62 * Check for =* and !* and return appropriately
64 if( check->operator == T_OP_CMP_TRUE )
66 if( check->operator == T_OP_CMP_FALSE )
70 if (check->operator == T_OP_REG_EQ) {
75 regmatch_t rxmatch[REQUEST_MAX_REGEX + 1];
77 snprintf(name, sizeof(name), "%%{%s}", check->name);
78 radius_xlat(value, sizeof(value), name, request, NULL);
81 * Include substring matches.
83 regcomp(®, (char *)check->vp_strvalue,
85 compare = regexec(®, value, REQUEST_MAX_REGEX + 1,
90 * Add %{0}, %{1}, etc.
92 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
94 char buffer[sizeof(check->vp_strvalue)];
97 * Didn't match: delete old
98 * match, if it existed.
100 if ((compare != 0) ||
101 (rxmatch[i].rm_so == -1)) {
102 p = request_data_get(request, request,
103 REQUEST_DATA_REGEX | i);
117 * Copy substring into buffer.
119 memcpy(buffer, value + rxmatch[i].rm_so,
120 rxmatch[i].rm_eo - rxmatch[i].rm_so);
121 buffer[rxmatch[i].rm_eo - rxmatch[i].rm_so] = '\0';
124 * Copy substring, and add it to
127 * Note that we don't check
128 * for out of memory, which is
129 * the only error we can get...
132 request_data_add(request, request,
133 REQUEST_DATA_REGEX | i,
136 if (compare == 0) return 0;
140 if (check->operator == T_OP_REG_NE) {
145 regmatch_t rxmatch[REQUEST_MAX_REGEX + 1];
147 snprintf(name, sizeof(name), "%%{%s}", check->name);
148 radius_xlat(value, sizeof(value), name, request, NULL);
151 * Include substring matches.
153 regcomp(®, (char *)check->vp_strvalue,
155 compare = regexec(®, value, REQUEST_MAX_REGEX + 1,
159 if (compare != 0) return 0;
166 * Not a regular expression, compare the types.
168 switch(check->type) {
171 * Ascend binary attributes can be treated
172 * as opaque objects, I guess...
174 case PW_TYPE_ABINARY:
177 if (vp->length != check->length) {
178 ret = 1; /* NOT equal */
181 ret = memcmp(vp->vp_strvalue, check->vp_strvalue,
185 if (check->flags.caseless) {
186 ret = strcasecmp((char *)vp->vp_strvalue,
187 (char *)check->vp_strvalue);
189 ret = strcmp((char *)vp->vp_strvalue,
190 (char *)check->vp_strvalue);
195 case PW_TYPE_INTEGER:
196 ret = vp->vp_integer - check->vp_integer;
199 ret = vp->vp_date - check->vp_date;
202 ret = ntohl(vp->vp_ipaddr) - ntohl(check->vp_ipaddr);
204 case PW_TYPE_IPV6ADDR:
205 ret = memcmp(&vp->vp_ipv6addr, &check->vp_ipv6addr,
206 sizeof(vp->vp_ipv6addr));
209 case PW_TYPE_IPV6PREFIX:
210 ret = memcmp(&vp->vp_ipv6prefix, &check->vp_ipv6prefix,
211 sizeof(vp->vp_ipv6prefix));
215 ret = memcmp(&vp->vp_ifid, &check->vp_ifid,
216 sizeof(vp->vp_ifid));
228 * Compare 2 attributes. May call the attribute compare function.
230 int radius_callback_compare(REQUEST *req, VALUE_PAIR *request,
231 VALUE_PAIR *check, VALUE_PAIR *check_pairs,
232 VALUE_PAIR **reply_pairs)
237 * Check for =* and !* and return appropriately
239 if( check->operator == T_OP_CMP_TRUE )
240 return 0; /* always return 0/EQUAL */
241 if( check->operator == T_OP_CMP_FALSE )
242 return 1; /* always return 1/NOT EQUAL */
245 * See if there is a special compare function.
247 * FIXME: use new RB-Tree code.
249 for (c = cmp; c; c = c->next)
250 if (c->attribute == check->attribute)
251 return (c->compare)(c->instance, req, request, check,
252 check_pairs, reply_pairs);
254 if (!request) return -1; /* doesn't exist, don't compare it */
256 return radius_compare_vps(req, check, request);
261 * See what attribute we want to compare with.
263 static int otherattr(int attr)
267 for (c = cmp; c; c = c->next) {
268 if (c->attribute == attr)
276 * Register a function as compare function.
277 * compare_attr is the attribute in the request we want to
278 * compare with. Normally this is the same as "attr".
279 * You can set this to:
281 * -1 the same as "attr"
282 * 0 always call compare function, not tied to request attribute
283 * >0 Attribute to compare with.
285 * For example, PW_GROUP in a check item needs to be compared
286 * with PW_USER_NAME in the incoming request.
288 int paircompare_register(int attr, int compare_attr, RAD_COMPARE_FUNC fun, void *instance)
292 paircompare_unregister(attr, fun);
294 c = rad_malloc(sizeof(struct cmp));
298 c->otherattr = compare_attr;
299 c->instance = instance;
307 * Unregister a function.
309 void paircompare_unregister(int attr, RAD_COMPARE_FUNC fun)
311 struct cmp *c, *last;
314 for (c = cmp; c; c = c->next) {
315 if (c->attribute == attr && c->compare == fun)
320 if (c == NULL) return;
323 last->next = c->next;
331 * Compare two pair lists except for the password information.
332 * For every element in "check" at least one matching copy must
333 * be present in "reply".
337 int paircompare(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check, VALUE_PAIR **reply)
339 VALUE_PAIR *check_item;
340 VALUE_PAIR *auth_item;
345 for (check_item = check; check_item != NULL; check_item = check_item->next) {
347 * If the user is setting a configuration value,
348 * then don't bother comparing it to any attributes
349 * sent to us by the user. It ALWAYS matches.
351 if ((check_item->operator == T_OP_SET) ||
352 (check_item->operator == T_OP_ADD)) {
356 switch (check_item->attribute) {
358 * Attributes we skip during comparison.
359 * These are "server" check items.
361 case PW_CRYPT_PASSWORD:
365 case PW_SESSION_TYPE:
366 case PW_STRIP_USER_NAME:
371 * IF the password attribute exists, THEN
372 * we can do comparisons against it. If not,
373 * then the request did NOT contain a
374 * User-Password attribute, so we CANNOT do
375 * comparisons against it.
377 * This hack makes CHAP-Password work..
379 case PW_USER_PASSWORD:
380 if (pairfind(request, PW_USER_PASSWORD) == NULL) {
387 * See if this item is present in the request.
389 other = otherattr(check_item->attribute);
394 for (; auth_item != NULL; auth_item = auth_item->next) {
395 if (auth_item->attribute == other || other == 0)
401 * Not found, it's not a match.
403 if (auth_item == NULL) {
405 * Didn't find it. If we were *trying*
406 * to not find it, then we succeeded.
408 if (check_item->operator == T_OP_CMP_FALSE)
415 * Else we found it, but we were trying to not
416 * find it, so we failed.
418 if (check_item->operator == T_OP_CMP_FALSE)
423 * We've got to xlat the string before doing
426 if (check_item->flags.do_xlat) {
428 char buffer[sizeof(check_item->vp_strvalue)];
430 check_item->flags.do_xlat = 0;
431 rcode = radius_xlat(buffer, sizeof(buffer),
432 check_item->vp_strvalue,
436 * Parse the string into a new value.
438 pairparsevalue(check_item, buffer);
442 * OK it is present now compare them.
444 compare = radius_callback_compare(req, auth_item, check_item,
447 switch (check_item->operator) {
450 radlog(L_INFO, "Invalid operator for item %s: "
451 "reverting to '=='", check_item->name);
453 case T_OP_CMP_TRUE: /* compare always == 0 */
454 case T_OP_CMP_FALSE: /* compare always == 1 */
456 if (compare != 0) result = -1;
460 if (compare == 0) result = -1;
464 if (compare >= 0) result = -1;
468 if (compare <= 0) result = -1;
472 if (compare > 0) result = -1;
476 if (compare < 0) result = -1;
485 } /* switch over the operator of the check item */
488 * This attribute didn't match, but maybe there's
489 * another of the same attribute, which DOES match.
491 if ((result != 0) && (other >= 0)) {
492 auth_item = auth_item->next;
497 } /* for every entry in the check item list */
503 * Move pairs, replacing/over-writing them, and doing xlat.
506 * Move attributes from one list to the other
507 * if not already present.
509 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
511 VALUE_PAIR **tailto, *i, *j, *next;
512 VALUE_PAIR *tailfrom = NULL;
516 * Point "tailto" to the end of the "to" list.
519 for(i = *to; i; i = i->next) {
524 * Loop over the "from" list.
526 for(i = *from; i; i = next) {
530 * Don't move 'fallthrough' over.
532 if (i->attribute == PW_FALL_THROUGH) {
537 * We've got to xlat the string before moving
540 if (i->flags.do_xlat) {
542 char buffer[sizeof(i->vp_strvalue)];
544 i->flags.do_xlat = 0;
545 rcode = radius_xlat(buffer, sizeof(buffer),
550 * Parse the string into a new value.
552 pairparsevalue(i, buffer);
555 found = pairfind(*to, i->attribute);
556 switch (i->operator) {
559 * If a similar attribute is found,
562 case T_OP_SUB: /* -= */
564 if (!i->vp_strvalue[0] ||
565 (strcmp((char *)found->vp_strvalue,
566 (char *)i->vp_strvalue) == 0)){
567 pairdelete(to, found->attribute);
570 * 'tailto' may have been
574 for(j = *to; j; j = j->next) {
584 * Add it, if it's not already there.
586 case T_OP_EQ: /* = */
589 continue; /* with the loop */
594 * If a similar attribute is found,
595 * replace it with the new one. Otherwise,
596 * add the new one to the list.
598 case T_OP_SET: /* := */
603 memcpy(found, i, sizeof(*found));
610 * FIXME: Add support for <=, >=, <, >
612 * which will mean (for integers)
613 * 'make the attribute the smaller, etc'
617 * Add the new element to the list, even
618 * if similar ones already exist.
621 case T_OP_ADD: /* += */
626 tailfrom->next = next;
631 * If ALL of the 'to' attributes have been deleted,
632 * then ensure that the 'tail' is updated to point
643 } /* loop over the 'from' list */
647 * Create a pair, and add it to a particular list of VPs
649 * Note that this function ALWAYS returns. If we're OOM, then
650 * it causes the server to exit!
652 VALUE_PAIR *radius_paircreate(REQUEST *request, VALUE_PAIR **vps,
653 int attribute, int type)
657 request = request; /* -Wunused */
659 vp = paircreate(attribute, type);
661 radlog(L_ERR, "No memory!");
662 rad_assert("No memory" == NULL);