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 paircompare(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
70 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
79 if (request->attribute != check->attribute)
84 * Check for =* and !* and return appropriately
86 if( check->operator == T_OP_CMP_TRUE )
87 return 0; /* always return 0/EQUAL */
88 if( check->operator == T_OP_CMP_FALSE )
89 return 1; /* always return 1/NOT EQUAL */
92 * See if there is a special compare function.
94 * FIXME: use new RB-Tree code.
96 for (c = cmp; c; c = c->next)
97 if (c->attribute == check->attribute)
98 return (c->compare)(c->instance, req, request, check,
99 check_pairs, reply_pairs);
101 if (!request) return -1; /* doesn't exist, don't compare it */
103 switch(check->type) {
106 * Ascend binary attributes can be treated
107 * as opaque objects, I guess...
109 case PW_TYPE_ABINARY:
112 if (request->length != check->length) {
113 ret = 1; /* NOT equal */
116 ret = memcmp(request->vp_strvalue, check->vp_strvalue,
120 if (check->flags.caseless) {
121 ret = strcasecmp((char *)request->vp_strvalue,
122 (char *)check->vp_strvalue);
124 ret = strcmp((char *)request->vp_strvalue,
125 (char *)check->vp_strvalue);
128 case PW_TYPE_INTEGER:
130 ret = request->lvalue - check->lvalue;
133 ret = ntohl(request->lvalue) - ntohl(check->lvalue);
144 * See what attribute we want to compare with.
146 static int otherattr(int attr)
150 for (c = cmp; c; c = c->next) {
151 if (c->attribute == attr)
159 * Register a function as compare function.
160 * compare_attr is the attribute in the request we want to
161 * compare with. Normally this is the same as "attr".
162 * You can set this to:
164 * -1 the same as "attr"
165 * 0 always call compare function, not tied to request attribute
166 * >0 Attribute to compare with.
168 * For example, PW_GROUP in a check item needs to be compared
169 * with PW_USER_NAME in the incoming request.
171 int paircompare_register(int attr, int compare_attr, RAD_COMPARE_FUNC fun, void *instance)
175 paircompare_unregister(attr, fun);
177 c = rad_malloc(sizeof(struct cmp));
179 if (compare_attr < 0)
183 c->otherattr = compare_attr;
184 c->instance = instance;
192 * Unregister a function.
194 void paircompare_unregister(int attr, RAD_COMPARE_FUNC fun)
196 struct cmp *c, *last;
199 for (c = cmp; c; c = c->next) {
200 if (c->attribute == attr && c->compare == fun)
205 if (c == NULL) return;
208 last->next = c->next;
216 * Compare two pair lists except for the password information.
217 * For every element in "check" at least one matching copy must
218 * be present in "reply".
222 int paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check, VALUE_PAIR **reply)
224 VALUE_PAIR *check_item;
225 VALUE_PAIR *auth_item;
233 for (check_item = check; check_item != NULL; check_item = check_item->next) {
235 * If the user is setting a configuration value,
236 * then don't bother comparing it to any attributes
237 * sent to us by the user. It ALWAYS matches.
239 if ((check_item->operator == T_OP_SET) ||
240 (check_item->operator == T_OP_ADD)) {
244 switch (check_item->attribute) {
246 * Attributes we skip during comparison.
247 * These are "server" check items.
249 case PW_CRYPT_PASSWORD:
253 case PW_SESSION_TYPE:
254 case PW_STRIP_USER_NAME:
259 * IF the password attribute exists, THEN
260 * we can do comparisons against it. If not,
261 * then the request did NOT contain a
262 * User-Password attribute, so we CANNOT do
263 * comparisons against it.
265 * This hack makes CHAP-Password work..
268 if (pairfind(request, PW_PASSWORD) == NULL) {
275 * See if this item is present in the request.
277 other = otherattr(check_item->attribute);
281 for (; auth_item != NULL; auth_item = auth_item->next) {
282 if (auth_item->attribute == other || other == 0)
287 * Not found, it's not a match.
289 if (auth_item == NULL) {
291 * Didn't find it. If we were *trying*
292 * to not find it, then we succeeded.
294 if (check_item->operator == T_OP_CMP_FALSE)
301 * Else we found it, but we were trying to not
302 * find it, so we failed.
304 if (check_item->operator == T_OP_CMP_FALSE)
309 * We've got to xlat the string before doing
312 if (check_item->flags.do_xlat) {
314 char buffer[sizeof(check_item->vp_strvalue)];
316 check_item->flags.do_xlat = 0;
317 rcode = radius_xlat(buffer, sizeof(buffer),
318 check_item->vp_strvalue,
322 * Parse the string into a new value.
324 pairparsevalue(check_item, buffer);
328 * OK it is present now compare them.
330 compare = paircompare(req, auth_item, check_item, check, reply);
332 switch (check_item->operator) {
335 radlog(L_INFO, "Invalid operator for item %s: "
336 "reverting to '=='", check_item->name);
338 case T_OP_CMP_TRUE: /* compare always == 0 */
339 case T_OP_CMP_FALSE: /* compare always == 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;
357 if (compare > 0) result = -1;
361 if (compare < 0) result = -1;
368 regmatch_t rxmatch[REQUEST_MAX_REGEX + 1];
370 if ((auth_item->type == PW_TYPE_IPADDR) &&
371 (auth_item->vp_strvalue[0] == '\0')) {
372 inet_ntop(AF_INET, &(auth_item->lvalue),
373 auth_item->vp_strvalue,
374 sizeof(auth_item->vp_strvalue));
378 * Include substring matches.
380 regcomp(®, (char *)check_item->vp_strvalue,
382 compare = regexec(®,
383 (char *)auth_item->vp_strvalue,
384 REQUEST_MAX_REGEX + 1,
389 * Add %{0}, %{1}, etc.
391 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
393 char buffer[sizeof(check_item->vp_strvalue)];
396 * Didn't match: delete old
397 * match, if it existed.
399 if ((compare != 0) ||
400 (rxmatch[i].rm_so == -1)) {
401 p = request_data_get(req, req,
402 REQUEST_DATA_REGEX | i);
416 * Copy substring into buffer.
419 auth_item->vp_strvalue + rxmatch[i].rm_so,
420 rxmatch[i].rm_eo - rxmatch[i].rm_so);
421 buffer[rxmatch[i].rm_eo - rxmatch[i].rm_so] = '\0';
424 * Copy substring, and add it to
427 * Note that we don't check
428 * for out of memory, which is
429 * the only error we can get...
432 request_data_add(req,
434 REQUEST_DATA_REGEX | i,
438 if (compare != 0) result = -1;
442 if ((auth_item->type == PW_TYPE_IPADDR) &&
443 (auth_item->vp_strvalue[0] == '\0')) {
444 inet_ntop(AF_INET, &(auth_item->lvalue),
445 auth_item->vp_strvalue,
446 sizeof(auth_item->vp_strvalue));
449 regcomp(®, (char *)check_item->vp_strvalue, REG_EXTENDED|REG_NOSUB);
450 compare = regexec(®, (char *)auth_item->vp_strvalue,
453 if (compare == 0) result = -1;
457 } /* switch over the operator of the check item */
460 * This attribute didn't match, but maybe there's
461 * another of the same attribute, which DOES match.
464 auth_item = auth_item->next;
469 } /* for every entry in the check item list */
471 return 0; /* it matched */
475 * Compare two attributes simply. Calls paircompare.
478 int simplepaircmp(REQUEST *req, VALUE_PAIR *first, VALUE_PAIR *second)
480 return paircompare( req, first, second, NULL, NULL );
485 * Move pairs, replacing/over-writing them, and doing xlat.
488 * Move attributes from one list to the other
489 * if not already present.
491 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
493 VALUE_PAIR **tailto, *i, *j, *next;
494 VALUE_PAIR *tailfrom = NULL;
498 * Point "tailto" to the end of the "to" list.
501 for(i = *to; i; i = i->next) {
506 * Loop over the "from" list.
508 for(i = *from; i; i = next) {
512 * Don't move 'fallthrough' over.
514 if (i->attribute == PW_FALL_THROUGH) {
519 * We've got to xlat the string before moving
522 if (i->flags.do_xlat) {
524 char buffer[sizeof(i->vp_strvalue)];
526 i->flags.do_xlat = 0;
527 rcode = radius_xlat(buffer, sizeof(buffer),
532 * Parse the string into a new value.
534 pairparsevalue(i, buffer);
537 found = pairfind(*to, i->attribute);
538 switch (i->operator) {
541 * If a similar attribute is found,
544 case T_OP_SUB: /* -= */
546 if (!i->vp_strvalue[0] ||
547 (strcmp((char *)found->vp_strvalue,
548 (char *)i->vp_strvalue) == 0)){
549 pairdelete(to, found->attribute);
552 * 'tailto' may have been
556 for(j = *to; j; j = j->next) {
566 * Add it, if it's not already there.
568 case T_OP_EQ: /* = */
571 continue; /* with the loop */
576 * If a similar attribute is found,
577 * replace it with the new one. Otherwise,
578 * add the new one to the list.
580 case T_OP_SET: /* := */
585 memcpy(found, i, sizeof(*found));
592 * FIXME: Add support for <=, >=, <, >
594 * which will mean (for integers)
595 * 'make the attribute the smaller, etc'
599 * Add the new element to the list, even
600 * if similar ones already exist.
603 case T_OP_ADD: /* += */
608 tailfrom->next = next;
613 * If ALL of the 'to' attributes have been deleted,
614 * then ensure that the 'tail' is updated to point
625 } /* loop over the 'from' list */