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 The FreeRADIUS server project
22 * Copyright 2000 Alan DeKok <aland@ox.org>
25 static const char rcsid[] = "$Id$";
27 #include <freeradius-devel/autoconf.h>
33 #ifdef HAVE_NETINET_IN_H
34 # include <netinet/in.h>
41 * For POSIX Regular expressions.
42 * (0) Means no extended regular expressions.
43 * REG_EXTENDED means use extended regular expressions.
46 #define REG_EXTENDED (0)
54 #include <freeradius-devel/radiusd.h>
59 void *instance; /* module instance */
60 RAD_COMPARE_FUNC compare;
63 static struct cmp *cmp;
67 * Compare 2 attributes. May call the attribute compare function.
69 static int compare_pair(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
70 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
76 * Check for =* and !* and return appropriately
78 if( check->operator == T_OP_CMP_TRUE )
79 return 0; /* always return 0/EQUAL */
80 if( check->operator == T_OP_CMP_FALSE )
81 return 1; /* always return 1/NOT EQUAL */
84 * See if there is a special compare function.
86 * FIXME: use new RB-Tree code.
88 for (c = cmp; c; c = c->next)
89 if (c->attribute == check->attribute)
90 return (c->compare)(c->instance, req, request, check,
91 check_pairs, reply_pairs);
93 if (!request) return -1; /* doesn't exist, don't compare it */
98 * Ascend binary attributes can be treated
99 * as opaque objects, I guess...
101 case PW_TYPE_ABINARY:
104 if (request->length != check->length) {
105 ret = 1; /* NOT equal */
108 ret = memcmp(request->vp_strvalue, check->vp_strvalue,
112 if (check->flags.caseless) {
113 ret = strcasecmp((char *)request->vp_strvalue,
114 (char *)check->vp_strvalue);
116 ret = strcmp((char *)request->vp_strvalue,
117 (char *)check->vp_strvalue);
120 case PW_TYPE_INTEGER:
122 ret = request->lvalue - check->lvalue;
125 ret = ntohl(request->vp_ipaddr) - ntohl(check->vp_ipaddr);
127 case PW_TYPE_IPV6ADDR:
128 ret = memcmp(&request->vp_ipv6addr, &check->vp_ipv6addr,
129 sizeof(request->vp_ipv6addr));
132 case PW_TYPE_IPV6PREFIX:
133 ret = memcmp(&request->vp_ipv6prefix, &check->vp_ipv6prefix,
134 sizeof(request->vp_ipv6prefix));
138 ret = memcmp(&request->vp_ifid, &check->vp_ifid,
139 sizeof(request->vp_ifid));
151 * See what attribute we want to compare with.
153 static int otherattr(int attr)
157 for (c = cmp; c; c = c->next) {
158 if (c->attribute == attr)
166 * Register a function as compare function.
167 * compare_attr is the attribute in the request we want to
168 * compare with. Normally this is the same as "attr".
169 * You can set this to:
171 * -1 the same as "attr"
172 * 0 always call compare function, not tied to request attribute
173 * >0 Attribute to compare with.
175 * For example, PW_GROUP in a check item needs to be compared
176 * with PW_USER_NAME in the incoming request.
178 int paircompare_register(int attr, int compare_attr, RAD_COMPARE_FUNC fun, void *instance)
182 paircompare_unregister(attr, fun);
184 c = rad_malloc(sizeof(struct cmp));
186 if (compare_attr < 0)
190 c->otherattr = compare_attr;
191 c->instance = instance;
199 * Unregister a function.
201 void paircompare_unregister(int attr, RAD_COMPARE_FUNC fun)
203 struct cmp *c, *last;
206 for (c = cmp; c; c = c->next) {
207 if (c->attribute == attr && c->compare == fun)
212 if (c == NULL) return;
215 last->next = c->next;
223 * Compare two pair lists except for the password information.
224 * For every element in "check" at least one matching copy must
225 * be present in "reply".
229 int paircompare(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check, VALUE_PAIR **reply)
231 VALUE_PAIR *check_item;
232 VALUE_PAIR *auth_item;
240 for (check_item = check; check_item != NULL; check_item = check_item->next) {
242 * If the user is setting a configuration value,
243 * then don't bother comparing it to any attributes
244 * sent to us by the user. It ALWAYS matches.
246 if ((check_item->operator == T_OP_SET) ||
247 (check_item->operator == T_OP_ADD)) {
251 switch (check_item->attribute) {
253 * Attributes we skip during comparison.
254 * These are "server" check items.
256 case PW_CRYPT_PASSWORD:
260 case PW_SESSION_TYPE:
261 case PW_STRIP_USER_NAME:
266 * IF the password attribute exists, THEN
267 * we can do comparisons against it. If not,
268 * then the request did NOT contain a
269 * User-Password attribute, so we CANNOT do
270 * comparisons against it.
272 * This hack makes CHAP-Password work..
275 if (pairfind(request, PW_PASSWORD) == NULL) {
282 * See if this item is present in the request.
284 other = otherattr(check_item->attribute);
288 for (; auth_item != NULL; auth_item = auth_item->next) {
289 if (auth_item->attribute == other || other == 0)
294 * Not found, it's not a match.
296 if (auth_item == NULL) {
298 * Didn't find it. If we were *trying*
299 * to not find it, then we succeeded.
301 if (check_item->operator == T_OP_CMP_FALSE)
308 * Else we found it, but we were trying to not
309 * find it, so we failed.
311 if (check_item->operator == T_OP_CMP_FALSE)
316 * We've got to xlat the string before doing
319 if (check_item->flags.do_xlat) {
321 char buffer[sizeof(check_item->vp_strvalue)];
323 check_item->flags.do_xlat = 0;
324 rcode = radius_xlat(buffer, sizeof(buffer),
325 check_item->vp_strvalue,
329 * Parse the string into a new value.
331 pairparsevalue(check_item, buffer);
335 * OK it is present now compare them.
337 compare = compare_pair(req, auth_item, check_item, check, reply);
339 switch (check_item->operator) {
342 radlog(L_INFO, "Invalid operator for item %s: "
343 "reverting to '=='", check_item->name);
345 case T_OP_CMP_TRUE: /* compare always == 0 */
346 case T_OP_CMP_FALSE: /* compare always == 1 */
348 if (compare != 0) result = -1;
352 if (compare == 0) result = -1;
356 if (compare >= 0) result = -1;
360 if (compare <= 0) result = -1;
364 if (compare > 0) result = -1;
368 if (compare < 0) result = -1;
375 regmatch_t rxmatch[REQUEST_MAX_REGEX + 1];
377 if ((auth_item->type == PW_TYPE_IPADDR) &&
378 (auth_item->vp_strvalue[0] == '\0')) {
379 inet_ntop(AF_INET, &(auth_item->lvalue),
380 auth_item->vp_strvalue,
381 sizeof(auth_item->vp_strvalue));
385 * Include substring matches.
387 regcomp(®, (char *)check_item->vp_strvalue,
389 compare = regexec(®,
390 (char *)auth_item->vp_strvalue,
391 REQUEST_MAX_REGEX + 1,
396 * Add %{0}, %{1}, etc.
398 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
400 char buffer[sizeof(check_item->vp_strvalue)];
403 * Didn't match: delete old
404 * match, if it existed.
406 if ((compare != 0) ||
407 (rxmatch[i].rm_so == -1)) {
408 p = request_data_get(req, req,
409 REQUEST_DATA_REGEX | i);
423 * Copy substring into buffer.
426 auth_item->vp_strvalue + rxmatch[i].rm_so,
427 rxmatch[i].rm_eo - rxmatch[i].rm_so);
428 buffer[rxmatch[i].rm_eo - rxmatch[i].rm_so] = '\0';
431 * Copy substring, and add it to
434 * Note that we don't check
435 * for out of memory, which is
436 * the only error we can get...
439 request_data_add(req,
441 REQUEST_DATA_REGEX | i,
445 if (compare != 0) result = -1;
449 if ((auth_item->type == PW_TYPE_IPADDR) &&
450 (auth_item->vp_strvalue[0] == '\0')) {
451 inet_ntop(AF_INET, &(auth_item->lvalue),
452 auth_item->vp_strvalue,
453 sizeof(auth_item->vp_strvalue));
456 regcomp(®, (char *)check_item->vp_strvalue, REG_EXTENDED|REG_NOSUB);
457 compare = regexec(®, (char *)auth_item->vp_strvalue,
460 if (compare == 0) result = -1;
464 } /* switch over the operator of the check item */
467 * This attribute didn't match, but maybe there's
468 * another of the same attribute, which DOES match.
471 auth_item = auth_item->next;
476 } /* for every entry in the check item list */
478 return 0; /* it matched */
482 * Compare two attributes simply. Calls compare_pair.
485 int simplepaircmp(REQUEST *req, VALUE_PAIR *first, VALUE_PAIR *second)
487 return compare_pair( req, first, second, NULL, NULL );
492 * Move pairs, replacing/over-writing them, and doing xlat.
495 * Move attributes from one list to the other
496 * if not already present.
498 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
500 VALUE_PAIR **tailto, *i, *j, *next;
501 VALUE_PAIR *tailfrom = NULL;
505 * Point "tailto" to the end of the "to" list.
508 for(i = *to; i; i = i->next) {
513 * Loop over the "from" list.
515 for(i = *from; i; i = next) {
519 * Don't move 'fallthrough' over.
521 if (i->attribute == PW_FALL_THROUGH) {
526 * We've got to xlat the string before moving
529 if (i->flags.do_xlat) {
531 char buffer[sizeof(i->vp_strvalue)];
533 i->flags.do_xlat = 0;
534 rcode = radius_xlat(buffer, sizeof(buffer),
539 * Parse the string into a new value.
541 pairparsevalue(i, buffer);
544 found = pairfind(*to, i->attribute);
545 switch (i->operator) {
548 * If a similar attribute is found,
551 case T_OP_SUB: /* -= */
553 if (!i->vp_strvalue[0] ||
554 (strcmp((char *)found->vp_strvalue,
555 (char *)i->vp_strvalue) == 0)){
556 pairdelete(to, found->attribute);
559 * 'tailto' may have been
563 for(j = *to; j; j = j->next) {
573 * Add it, if it's not already there.
575 case T_OP_EQ: /* = */
578 continue; /* with the loop */
583 * If a similar attribute is found,
584 * replace it with the new one. Otherwise,
585 * add the new one to the list.
587 case T_OP_SET: /* := */
592 memcpy(found, i, sizeof(*found));
599 * FIXME: Add support for <=, >=, <, >
601 * which will mean (for integers)
602 * 'make the attribute the smaller, etc'
606 * Add the new element to the list, even
607 * if similar ones already exist.
610 case T_OP_ADD: /* += */
615 tailfrom->next = next;
620 * If ALL of the 'to' attributes have been deleted,
621 * then ensure that the 'tail' is updated to point
632 } /* loop over the 'from' list */