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$";
28 #include "libradius.h"
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
42 * For POSIX Regular expressions.
43 * (0) Means no extended regular expressions.
44 * REG_EXTENDED means use extended regular expressions.
47 #define REG_EXTENDED (0)
60 void *instance; /* module instance */
61 RAD_COMPARE_FUNC compare;
64 static struct cmp *cmp;
68 * Compare 2 attributes. May call the attribute compare function.
70 static int paircompare(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
71 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
80 if (request->attribute != check->attribute)
85 * Check for =* and !* and return appropriately
87 if( check->operator == T_OP_CMP_TRUE )
88 return 0; /* always return 0/EQUAL */
89 if( check->operator == T_OP_CMP_FALSE )
90 return 1; /* always return 1/NOT EQUAL */
93 * See if there is a special compare function.
95 for (c = cmp; c; c = c->next)
96 if (c->attribute == check->attribute)
97 return (c->compare)(c->instance, req, request, check,
98 check_pairs, reply_pairs);
100 switch(check->type) {
103 * Ascend binary attributes can be treated
104 * as opaque objects, I guess...
106 case PW_TYPE_ABINARY:
109 if (request->length != check->length) {
110 ret = 1; /* NOT equal */
113 ret = memcmp(request->strvalue, check->strvalue,
117 if (check->flags.caseless) {
118 ret = strcasecmp((char *)request->strvalue,
119 (char *)check->strvalue);
121 ret = strcmp((char *)request->strvalue,
122 (char *)check->strvalue);
125 case PW_TYPE_INTEGER:
127 ret = request->lvalue - check->lvalue;
130 ret = ntohl(request->lvalue) - ntohl(check->lvalue);
141 * See what attribute we want to compare with.
143 static int otherattr(int attr)
147 for (c = cmp; c; c = c->next) {
148 if (c->attribute == attr)
156 * Register a function as compare function.
157 * compare_attr is the attribute in the request we want to
158 * compare with. Normally this is the same as "attr".
159 * You can set this to:
161 * -1 the same as "attr"
162 * 0 always call compare function, not tied to request attribute
163 * >0 Attribute to compare with.
165 * For example, PW_GROUP in a check item needs to be compared
166 * with PW_USER_NAME in the incoming request.
168 int paircompare_register(int attr, int compare_attr, RAD_COMPARE_FUNC fun, void *instance)
172 paircompare_unregister(attr, fun);
174 c = rad_malloc(sizeof(struct cmp));
176 if (compare_attr < 0)
180 c->otherattr = compare_attr;
181 c->instance = instance;
189 * Unregister a function.
191 void paircompare_unregister(int attr, RAD_COMPARE_FUNC fun)
193 struct cmp *c, *last;
196 for (c = cmp; c; c = c->next) {
197 if (c->attribute == attr && c->compare == fun)
202 if (c == NULL) return;
205 last->next = c->next;
213 * Compare two pair lists except for the password information.
214 * For every element in "check" at least one matching copy must
215 * be present in "reply".
219 int paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check, VALUE_PAIR **reply)
221 VALUE_PAIR *check_item;
222 VALUE_PAIR *auth_item;
230 for (check_item = check; check_item != NULL; check_item = check_item->next) {
232 * If the user is setting a configuration value,
233 * then don't bother comparing it to any attributes
234 * sent to us by the user. It ALWAYS matches.
236 if ((check_item->operator == T_OP_SET) ||
237 (check_item->operator == T_OP_ADD)) {
241 switch (check_item->attribute) {
243 * Attributes we skip during comparison.
244 * These are "server" check items.
246 case PW_CRYPT_PASSWORD:
250 case PW_SESSION_TYPE:
251 case PW_STRIP_USER_NAME:
256 * IF the password attribute exists, THEN
257 * we can do comparisons against it. If not,
258 * then the request did NOT contain a
259 * User-Password attribute, so we CANNOT do
260 * comparisons against it.
262 * This hack makes CHAP-Password work..
265 if (pairfind(request, PW_PASSWORD) == NULL) {
272 * See if this item is present in the request.
274 other = otherattr(check_item->attribute);
278 for (; auth_item != NULL; auth_item = auth_item->next) {
279 if (auth_item->attribute == other || other == 0)
284 * Not found, it's not a match.
286 if (auth_item == NULL) {
288 * Didn't find it. If we were *trying*
289 * to not find it, then we succeeded.
291 if (check_item->operator == T_OP_CMP_FALSE)
298 * Else we found it, but we were trying to not
299 * find it, so we failed.
301 if (check_item->operator == T_OP_CMP_FALSE)
306 * We've got to xlat the string before doing
309 if (check_item->flags.do_xlat) {
311 char buffer[sizeof(check_item->strvalue)];
313 check_item->flags.do_xlat = 0;
314 rcode = radius_xlat(buffer, sizeof(buffer),
315 check_item->strvalue,
319 * Parse the string into a new value.
321 pairparsevalue(check_item, buffer);
325 * OK it is present now compare them.
327 compare = paircompare(req, auth_item, check_item, check, reply);
329 switch (check_item->operator) {
332 radlog(L_INFO, "Invalid operator for item %s: "
333 "reverting to '=='", check_item->name);
335 case T_OP_CMP_TRUE: /* compare always == 0 */
336 case T_OP_CMP_FALSE: /* compare always == 1 */
338 if (compare != 0) result = -1;
342 if (compare == 0) result = -1;
346 if (compare >= 0) result = -1;
350 if (compare <= 0) result = -1;
354 if (compare > 0) result = -1;
358 if (compare < 0) result = -1;
365 regmatch_t rxmatch[9];
368 * Include substring matches.
370 regcomp(®, (char *)check_item->strvalue,
372 compare = regexec(®,
373 (char *)auth_item->strvalue,
378 * Add %{0}, %{1}, etc.
380 for (i = 0; i <= 8; i++) {
382 char buffer[sizeof(check_item->strvalue)];
385 * Didn't match: delete old
386 * match, if it existed.
388 if ((compare != 0) ||
389 (rxmatch[i].rm_so == -1)) {
390 p = request_data_get(req, req,
391 REQUEST_DATA_REGEX | i);
405 * Copy substring into buffer.
408 auth_item->strvalue + rxmatch[i].rm_so,
409 rxmatch[i].rm_eo - rxmatch[i].rm_so);
410 buffer[rxmatch[i].rm_eo - rxmatch[i].rm_so] = '\0';
413 * Copy substring, and add it to
416 * Note that we don't check
417 * for out of memory, which is
418 * the only error we can get...
421 request_data_add(req,
423 REQUEST_DATA_REGEX | i,
427 if (compare != 0) result = -1;
431 regcomp(®, (char *)check_item->strvalue, REG_EXTENDED|REG_NOSUB);
432 compare = regexec(®, (char *)auth_item->strvalue,
435 if (compare == 0) result = -1;
439 } /* switch over the operator of the check item */
442 * This attribute didn't match, but maybe there's
443 * another of the same attribute, which DOES match.
446 auth_item = auth_item->next;
451 } /* for every entry in the check item list */
453 return 0; /* it matched */
457 * Compare two attributes simply. Calls paircompare.
460 int simplepaircmp(REQUEST *req, VALUE_PAIR *first, VALUE_PAIR *second)
462 return paircompare( req, first, second, NULL, NULL );
467 * Move pairs, replacing/over-writing them, and doing xlat.
470 * Move attributes from one list to the other
471 * if not already present.
473 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
475 VALUE_PAIR **tailto, *i, *j, *next;
476 VALUE_PAIR *tailfrom = NULL;
480 * Point "tailto" to the end of the "to" list.
483 for(i = *to; i; i = i->next) {
488 * Loop over the "from" list.
490 for(i = *from; i; i = next) {
494 * Don't move 'fallthrough' over.
496 if (i->attribute == PW_FALL_THROUGH) {
501 * We've got to xlat the string before moving
504 if (i->flags.do_xlat) {
506 char buffer[sizeof(i->strvalue)];
508 i->flags.do_xlat = 0;
509 rcode = radius_xlat(buffer, sizeof(buffer),
514 * Parse the string into a new value.
516 pairparsevalue(i, buffer);
519 found = pairfind(*to, i->attribute);
520 switch (i->operator) {
523 * If a similar attribute is found,
526 case T_OP_SUB: /* -= */
528 if (!i->strvalue[0] ||
529 (strcmp((char *)found->strvalue,
530 (char *)i->strvalue) == 0)){
531 pairdelete(to, found->attribute);
534 * 'tailto' may have been
538 for(j = *to; j; j = j->next) {
548 * Add it, if it's not already there.
550 case T_OP_EQ: /* = */
553 continue; /* with the loop */
558 * If a similar attribute is found,
559 * replace it with the new one. Otherwise,
560 * add the new one to the list.
562 case T_OP_SET: /* := */
567 memcpy(found, i, sizeof(*found));
574 * FIXME: Add support for <=, >=, <, >
576 * which will mean (for integers)
577 * 'make the attribute the smaller, etc'
581 * Add the new element to the list, even
582 * if similar ones already exist.
585 case T_OP_ADD: /* += */
590 tailfrom->next = next;
595 * If ALL of the 'to' attributes have been deleted,
596 * then ensure that the 'tail' is updated to point
607 } /* loop over the 'from' list */