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$";
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)
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->lvalue) - ntohl(check->lvalue);
136 * See what attribute we want to compare with.
138 static int otherattr(int attr)
142 for (c = cmp; c; c = c->next) {
143 if (c->attribute == attr)
151 * Register a function as compare function.
152 * compare_attr is the attribute in the request we want to
153 * compare with. Normally this is the same as "attr".
154 * You can set this to:
156 * -1 the same as "attr"
157 * 0 always call compare function, not tied to request attribute
158 * >0 Attribute to compare with.
160 * For example, PW_GROUP in a check item needs to be compared
161 * with PW_USER_NAME in the incoming request.
163 int paircompare_register(int attr, int compare_attr, RAD_COMPARE_FUNC fun, void *instance)
167 paircompare_unregister(attr, fun);
169 c = rad_malloc(sizeof(struct cmp));
171 if (compare_attr < 0)
175 c->otherattr = compare_attr;
176 c->instance = instance;
184 * Unregister a function.
186 void paircompare_unregister(int attr, RAD_COMPARE_FUNC fun)
188 struct cmp *c, *last;
191 for (c = cmp; c; c = c->next) {
192 if (c->attribute == attr && c->compare == fun)
197 if (c == NULL) return;
200 last->next = c->next;
208 * Compare two pair lists except for the password information.
209 * For every element in "check" at least one matching copy must
210 * be present in "reply".
214 int paircompare(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check, VALUE_PAIR **reply)
216 VALUE_PAIR *check_item;
217 VALUE_PAIR *auth_item;
225 for (check_item = check; check_item != NULL; check_item = check_item->next) {
227 * If the user is setting a configuration value,
228 * then don't bother comparing it to any attributes
229 * sent to us by the user. It ALWAYS matches.
231 if ((check_item->operator == T_OP_SET) ||
232 (check_item->operator == T_OP_ADD)) {
236 switch (check_item->attribute) {
238 * Attributes we skip during comparison.
239 * These are "server" check items.
241 case PW_CRYPT_PASSWORD:
245 case PW_SESSION_TYPE:
246 case PW_STRIP_USER_NAME:
251 * IF the password attribute exists, THEN
252 * we can do comparisons against it. If not,
253 * then the request did NOT contain a
254 * User-Password attribute, so we CANNOT do
255 * comparisons against it.
257 * This hack makes CHAP-Password work..
260 if (pairfind(request, PW_PASSWORD) == NULL) {
267 * See if this item is present in the request.
269 other = otherattr(check_item->attribute);
273 for (; auth_item != NULL; auth_item = auth_item->next) {
274 if (auth_item->attribute == other || other == 0)
279 * Not found, it's not a match.
281 if (auth_item == NULL) {
283 * Didn't find it. If we were *trying*
284 * to not find it, then we succeeded.
286 if (check_item->operator == T_OP_CMP_FALSE)
293 * Else we found it, but we were trying to not
294 * find it, so we failed.
296 if (check_item->operator == T_OP_CMP_FALSE)
301 * We've got to xlat the string before doing
304 if (check_item->flags.do_xlat) {
306 char buffer[sizeof(check_item->vp_strvalue)];
308 check_item->flags.do_xlat = 0;
309 rcode = radius_xlat(buffer, sizeof(buffer),
310 check_item->vp_strvalue,
314 * Parse the string into a new value.
316 pairparsevalue(check_item, buffer);
320 * OK it is present now compare them.
322 compare = compare_pair(req, auth_item, check_item, check, reply);
324 switch (check_item->operator) {
327 radlog(L_INFO, "Invalid operator for item %s: "
328 "reverting to '=='", check_item->name);
330 case T_OP_CMP_TRUE: /* compare always == 0 */
331 case T_OP_CMP_FALSE: /* compare always == 1 */
333 if (compare != 0) result = -1;
337 if (compare == 0) result = -1;
341 if (compare >= 0) result = -1;
345 if (compare <= 0) result = -1;
349 if (compare > 0) result = -1;
353 if (compare < 0) result = -1;
360 regmatch_t rxmatch[REQUEST_MAX_REGEX + 1];
362 if ((auth_item->type == PW_TYPE_IPADDR) &&
363 (auth_item->vp_strvalue[0] == '\0')) {
364 inet_ntop(AF_INET, &(auth_item->lvalue),
365 auth_item->vp_strvalue,
366 sizeof(auth_item->vp_strvalue));
370 * Include substring matches.
372 regcomp(®, (char *)check_item->vp_strvalue,
374 compare = regexec(®,
375 (char *)auth_item->vp_strvalue,
376 REQUEST_MAX_REGEX + 1,
381 * Add %{0}, %{1}, etc.
383 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
385 char buffer[sizeof(check_item->vp_strvalue)];
388 * Didn't match: delete old
389 * match, if it existed.
391 if ((compare != 0) ||
392 (rxmatch[i].rm_so == -1)) {
393 p = request_data_get(req, req,
394 REQUEST_DATA_REGEX | i);
408 * Copy substring into buffer.
411 auth_item->vp_strvalue + rxmatch[i].rm_so,
412 rxmatch[i].rm_eo - rxmatch[i].rm_so);
413 buffer[rxmatch[i].rm_eo - rxmatch[i].rm_so] = '\0';
416 * Copy substring, and add it to
419 * Note that we don't check
420 * for out of memory, which is
421 * the only error we can get...
424 request_data_add(req,
426 REQUEST_DATA_REGEX | i,
430 if (compare != 0) result = -1;
434 if ((auth_item->type == PW_TYPE_IPADDR) &&
435 (auth_item->vp_strvalue[0] == '\0')) {
436 inet_ntop(AF_INET, &(auth_item->lvalue),
437 auth_item->vp_strvalue,
438 sizeof(auth_item->vp_strvalue));
441 regcomp(®, (char *)check_item->vp_strvalue, REG_EXTENDED|REG_NOSUB);
442 compare = regexec(®, (char *)auth_item->vp_strvalue,
445 if (compare == 0) result = -1;
449 } /* switch over the operator of the check item */
452 * This attribute didn't match, but maybe there's
453 * another of the same attribute, which DOES match.
456 auth_item = auth_item->next;
461 } /* for every entry in the check item list */
463 return 0; /* it matched */
467 * Compare two attributes simply. Calls compare_pair.
470 int simplepaircmp(REQUEST *req, VALUE_PAIR *first, VALUE_PAIR *second)
472 return compare_pair( req, first, second, NULL, NULL );
477 * Move pairs, replacing/over-writing them, and doing xlat.
480 * Move attributes from one list to the other
481 * if not already present.
483 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
485 VALUE_PAIR **tailto, *i, *j, *next;
486 VALUE_PAIR *tailfrom = NULL;
490 * Point "tailto" to the end of the "to" list.
493 for(i = *to; i; i = i->next) {
498 * Loop over the "from" list.
500 for(i = *from; i; i = next) {
504 * Don't move 'fallthrough' over.
506 if (i->attribute == PW_FALL_THROUGH) {
511 * We've got to xlat the string before moving
514 if (i->flags.do_xlat) {
516 char buffer[sizeof(i->vp_strvalue)];
518 i->flags.do_xlat = 0;
519 rcode = radius_xlat(buffer, sizeof(buffer),
524 * Parse the string into a new value.
526 pairparsevalue(i, buffer);
529 found = pairfind(*to, i->attribute);
530 switch (i->operator) {
533 * If a similar attribute is found,
536 case T_OP_SUB: /* -= */
538 if (!i->vp_strvalue[0] ||
539 (strcmp((char *)found->vp_strvalue,
540 (char *)i->vp_strvalue) == 0)){
541 pairdelete(to, found->attribute);
544 * 'tailto' may have been
548 for(j = *to; j; j = j->next) {
558 * Add it, if it's not already there.
560 case T_OP_EQ: /* = */
563 continue; /* with the loop */
568 * If a similar attribute is found,
569 * replace it with the new one. Otherwise,
570 * add the new one to the list.
572 case T_OP_SET: /* := */
577 memcpy(found, i, sizeof(*found));
584 * FIXME: Add support for <=, >=, <, >
586 * which will mean (for integers)
587 * 'make the attribute the smaller, etc'
591 * Add the new element to the list, even
592 * if similar ones already exist.
595 case T_OP_ADD: /* += */
600 tailfrom->next = next;
605 * If ALL of the 'to' attributes have been deleted,
606 * then ensure that the 'tail' is updated to point
617 } /* loop over the 'from' list */