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 * Tagged attributes are equal if and only if both the
167 * tag AND value match.
169 if ((ret == 0) && check->flags.has_tag) {
170 ret = ((int) vp->flags.tag) - ((int) check->flags.tag);
171 if (ret != 0) return ret;
175 * Not a regular expression, compare the types.
177 switch(check->type) {
180 * Ascend binary attributes can be treated
181 * as opaque objects, I guess...
183 case PW_TYPE_ABINARY:
186 if (vp->length != check->length) {
187 ret = 1; /* NOT equal */
190 ret = memcmp(vp->vp_strvalue, check->vp_strvalue,
194 ret = strcmp((char *)vp->vp_strvalue,
195 (char *)check->vp_strvalue);
199 case PW_TYPE_INTEGER:
200 ret = vp->vp_integer - check->vp_integer;
203 ret = vp->vp_date - check->vp_date;
206 ret = ntohl(vp->vp_ipaddr) - ntohl(check->vp_ipaddr);
208 case PW_TYPE_IPV6ADDR:
209 ret = memcmp(&vp->vp_ipv6addr, &check->vp_ipv6addr,
210 sizeof(vp->vp_ipv6addr));
213 case PW_TYPE_IPV6PREFIX:
214 ret = memcmp(&vp->vp_ipv6prefix, &check->vp_ipv6prefix,
215 sizeof(vp->vp_ipv6prefix));
219 ret = memcmp(&vp->vp_ifid, &check->vp_ifid,
220 sizeof(vp->vp_ifid));
232 * Compare 2 attributes. May call the attribute compare function.
234 int radius_callback_compare(REQUEST *req, VALUE_PAIR *request,
235 VALUE_PAIR *check, VALUE_PAIR *check_pairs,
236 VALUE_PAIR **reply_pairs)
241 * Check for =* and !* and return appropriately
243 if( check->operator == T_OP_CMP_TRUE )
244 return 0; /* always return 0/EQUAL */
245 if( check->operator == T_OP_CMP_FALSE )
246 return 1; /* always return 1/NOT EQUAL */
249 * See if there is a special compare function.
251 * FIXME: use new RB-Tree code.
253 for (c = cmp; c; c = c->next)
254 if (c->attribute == check->attribute) {
255 return (c->compare)(c->instance, req, request, check,
256 check_pairs, reply_pairs);
259 if (!request) return -1; /* doesn't exist, don't compare it */
261 return radius_compare_vps(req, check, request);
266 * See what attribute we want to compare with.
268 static int otherattr(int attr)
272 for (c = cmp; c; c = c->next) {
273 if (c->attribute == attr)
281 * Register a function as compare function.
282 * compare_attr is the attribute in the request we want to
283 * compare with. Normally this is the same as "attr".
284 * You can set this to:
286 * -1 the same as "attr"
287 * 0 always call compare function, not tied to request attribute
288 * >0 Attribute to compare with.
290 * For example, PW_GROUP in a check item needs to be compared
291 * with PW_USER_NAME in the incoming request.
293 int paircompare_register(int attr, int compare_attr, RAD_COMPARE_FUNC fun, void *instance)
297 paircompare_unregister(attr, fun);
299 c = rad_malloc(sizeof(struct cmp));
303 c->otherattr = compare_attr;
304 c->instance = instance;
312 * Unregister a function.
314 void paircompare_unregister(int attr, RAD_COMPARE_FUNC fun)
316 struct cmp *c, *last;
319 for (c = cmp; c; c = c->next) {
320 if (c->attribute == attr && c->compare == fun)
325 if (c == NULL) return;
328 last->next = c->next;
336 * Compare two pair lists except for the password information.
337 * For every element in "check" at least one matching copy must
338 * be present in "reply".
342 int paircompare(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check, VALUE_PAIR **reply)
344 VALUE_PAIR *check_item;
345 VALUE_PAIR *auth_item;
350 for (check_item = check; check_item != NULL; check_item = check_item->next) {
352 * If the user is setting a configuration value,
353 * then don't bother comparing it to any attributes
354 * sent to us by the user. It ALWAYS matches.
356 if ((check_item->operator == T_OP_SET) ||
357 (check_item->operator == T_OP_ADD)) {
361 switch (check_item->attribute) {
363 * Attributes we skip during comparison.
364 * These are "server" check items.
366 case PW_CRYPT_PASSWORD:
370 case PW_SESSION_TYPE:
371 case PW_STRIP_USER_NAME:
376 * IF the password attribute exists, THEN
377 * we can do comparisons against it. If not,
378 * then the request did NOT contain a
379 * User-Password attribute, so we CANNOT do
380 * comparisons against it.
382 * This hack makes CHAP-Password work..
384 case PW_USER_PASSWORD:
385 if (check_item->operator == T_OP_CMP_EQ) {
386 DEBUG("WARNING: Found User-Password == \"...\".");
387 DEBUG("WARNING: Are you sure you don't mean Cleartext-Password?");
388 DEBUG("WARNING: See \"man rlm_pap\" for more information.");
390 if (pairfind(request, PW_USER_PASSWORD) == NULL) {
397 * See if this item is present in the request.
399 other = otherattr(check_item->attribute);
404 for (; auth_item != NULL; auth_item = auth_item->next) {
405 if (auth_item->attribute == other || other == 0)
411 * Not found, it's not a match.
413 if (auth_item == NULL) {
415 * Didn't find it. If we were *trying*
416 * to not find it, then we succeeded.
418 if (check_item->operator == T_OP_CMP_FALSE)
425 * Else we found it, but we were trying to not
426 * find it, so we failed.
428 if (check_item->operator == T_OP_CMP_FALSE)
433 * We've got to xlat the string before doing
436 if (check_item->flags.do_xlat) {
438 char buffer[sizeof(check_item->vp_strvalue)];
440 check_item->flags.do_xlat = 0;
441 rcode = radius_xlat(buffer, sizeof(buffer),
442 check_item->vp_strvalue,
446 * Parse the string into a new value.
448 pairparsevalue(check_item, buffer);
452 * OK it is present now compare them.
454 compare = radius_callback_compare(req, auth_item, check_item,
457 switch (check_item->operator) {
460 radlog(L_INFO, "Invalid operator for item %s: "
461 "reverting to '=='", check_item->name);
463 case T_OP_CMP_TRUE: /* compare always == 0 */
464 case T_OP_CMP_FALSE: /* compare always == 1 */
466 if (compare != 0) result = -1;
470 if (compare == 0) result = -1;
474 if (compare >= 0) result = -1;
478 if (compare <= 0) result = -1;
482 if (compare > 0) result = -1;
486 if (compare < 0) result = -1;
495 } /* switch over the operator of the check item */
498 * This attribute didn't match, but maybe there's
499 * another of the same attribute, which DOES match.
501 if ((result != 0) && (other >= 0)) {
502 auth_item = auth_item->next;
507 } /* for every entry in the check item list */
513 * Move pairs, replacing/over-writing them, and doing xlat.
516 * Move attributes from one list to the other
517 * if not already present.
519 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
521 VALUE_PAIR **tailto, *i, *j, *next;
522 VALUE_PAIR *tailfrom = NULL;
526 * Point "tailto" to the end of the "to" list.
529 for(i = *to; i; i = i->next) {
534 * Loop over the "from" list.
536 for(i = *from; i; i = next) {
540 * Don't move 'fallthrough' over.
542 if (i->attribute == PW_FALL_THROUGH) {
547 * We've got to xlat the string before moving
550 if (i->flags.do_xlat) {
552 char buffer[sizeof(i->vp_strvalue)];
554 i->flags.do_xlat = 0;
555 rcode = radius_xlat(buffer, sizeof(buffer),
560 * Parse the string into a new value.
562 pairparsevalue(i, buffer);
565 found = pairfind(*to, i->attribute);
566 switch (i->operator) {
569 * If a similar attribute is found,
572 case T_OP_SUB: /* -= */
574 if (!i->vp_strvalue[0] ||
575 (strcmp((char *)found->vp_strvalue,
576 (char *)i->vp_strvalue) == 0)){
577 pairdelete(to, found->attribute);
580 * 'tailto' may have been
584 for(j = *to; j; j = j->next) {
594 * Add it, if it's not already there.
596 case T_OP_EQ: /* = */
599 continue; /* with the loop */
604 * If a similar attribute is found,
605 * replace it with the new one. Otherwise,
606 * add the new one to the list.
608 case T_OP_SET: /* := */
613 memcpy(found, i, sizeof(*found));
620 * FIXME: Add support for <=, >=, <, >
622 * which will mean (for integers)
623 * 'make the attribute the smaller, etc'
627 * Add the new element to the list, even
628 * if similar ones already exist.
631 case T_OP_ADD: /* += */
636 tailfrom->next = next;
641 * If ALL of the 'to' attributes have been deleted,
642 * then ensure that the 'tail' is updated to point
653 } /* loop over the 'from' list */
657 * Create a pair, and add it to a particular list of VPs
659 * Note that this function ALWAYS returns. If we're OOM, then
660 * it causes the server to exit!
662 VALUE_PAIR *radius_paircreate(REQUEST *request, VALUE_PAIR **vps,
663 int attribute, int type)
667 request = request; /* -Wunused */
669 vp = paircreate(attribute, type);
671 radlog(L_ERR, "No memory!");
672 rad_assert("No memory" == NULL);
676 if (vps) pairadd(vps, vp);
682 * Create a pair, and add it to a particular list of VPs
684 * Note that this function ALWAYS returns. If we're OOM, then
685 * it causes the server to exit!
687 VALUE_PAIR *radius_pairmake(REQUEST *request, VALUE_PAIR **vps,
688 const char *attribute, const char *value,
693 request = request; /* -Wunused */
695 vp = pairmake(attribute, value, operator);
696 if (!vp) return NULL;
698 if (vps) pairadd(vps, vp);