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/autoconf.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)
55 #include <freeradius-devel/radiusd.h>
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 compare_pair(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
71 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
77 * Check for =* and !* and return appropriately
79 if( check->operator == T_OP_CMP_TRUE )
80 return 0; /* always return 0/EQUAL */
81 if( check->operator == T_OP_CMP_FALSE )
82 return 1; /* always return 1/NOT EQUAL */
85 * See if there is a special compare function.
87 * FIXME: use new RB-Tree code.
89 for (c = cmp; c; c = c->next)
90 if (c->attribute == check->attribute)
91 return (c->compare)(c->instance, req, request, check,
92 check_pairs, reply_pairs);
94 if (!request) return -1; /* doesn't exist, don't compare it */
99 * Ascend binary attributes can be treated
100 * as opaque objects, I guess...
102 case PW_TYPE_ABINARY:
105 if (request->length != check->length) {
106 ret = 1; /* NOT equal */
109 ret = memcmp(request->vp_strvalue, check->vp_strvalue,
113 if (check->flags.caseless) {
114 ret = strcasecmp((char *)request->vp_strvalue,
115 (char *)check->vp_strvalue);
117 ret = strcmp((char *)request->vp_strvalue,
118 (char *)check->vp_strvalue);
121 case PW_TYPE_INTEGER:
123 ret = request->lvalue - check->lvalue;
126 ret = ntohl(request->vp_ipaddr) - ntohl(check->vp_ipaddr);
128 case PW_TYPE_IPV6ADDR:
129 ret = memcmp(&request->vp_ipv6addr, &check->vp_ipv6addr,
130 sizeof(request->vp_ipv6addr));
133 case PW_TYPE_IPV6PREFIX:
134 ret = memcmp(&request->vp_ipv6prefix, &check->vp_ipv6prefix,
135 sizeof(request->vp_ipv6prefix));
139 ret = memcmp(&request->vp_ifid, &check->vp_ifid,
140 sizeof(request->vp_ifid));
152 * See what attribute we want to compare with.
154 static int otherattr(int attr)
158 for (c = cmp; c; c = c->next) {
159 if (c->attribute == attr)
167 * Register a function as compare function.
168 * compare_attr is the attribute in the request we want to
169 * compare with. Normally this is the same as "attr".
170 * You can set this to:
172 * -1 the same as "attr"
173 * 0 always call compare function, not tied to request attribute
174 * >0 Attribute to compare with.
176 * For example, PW_GROUP in a check item needs to be compared
177 * with PW_USER_NAME in the incoming request.
179 int paircompare_register(int attr, int compare_attr, RAD_COMPARE_FUNC fun, void *instance)
183 paircompare_unregister(attr, fun);
185 c = rad_malloc(sizeof(struct cmp));
187 if (compare_attr < 0)
191 c->otherattr = compare_attr;
192 c->instance = instance;
200 * Unregister a function.
202 void paircompare_unregister(int attr, RAD_COMPARE_FUNC fun)
204 struct cmp *c, *last;
207 for (c = cmp; c; c = c->next) {
208 if (c->attribute == attr && c->compare == fun)
213 if (c == NULL) return;
216 last->next = c->next;
224 * Compare two pair lists except for the password information.
225 * For every element in "check" at least one matching copy must
226 * be present in "reply".
230 int paircompare(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check, VALUE_PAIR **reply)
232 VALUE_PAIR *check_item;
233 VALUE_PAIR *auth_item;
241 for (check_item = check; check_item != NULL; check_item = check_item->next) {
243 * If the user is setting a configuration value,
244 * then don't bother comparing it to any attributes
245 * sent to us by the user. It ALWAYS matches.
247 if ((check_item->operator == T_OP_SET) ||
248 (check_item->operator == T_OP_ADD)) {
252 switch (check_item->attribute) {
254 * Attributes we skip during comparison.
255 * These are "server" check items.
257 case PW_CRYPT_PASSWORD:
261 case PW_SESSION_TYPE:
262 case PW_STRIP_USER_NAME:
267 * IF the password attribute exists, THEN
268 * we can do comparisons against it. If not,
269 * then the request did NOT contain a
270 * User-Password attribute, so we CANNOT do
271 * comparisons against it.
273 * This hack makes CHAP-Password work..
275 case PW_USER_PASSWORD:
276 if (pairfind(request, PW_USER_PASSWORD) == NULL) {
283 * See if this item is present in the request.
285 other = otherattr(check_item->attribute);
289 for (; auth_item != NULL; auth_item = auth_item->next) {
290 if (auth_item->attribute == other || other == 0)
295 * Not found, it's not a match.
297 if (auth_item == NULL) {
299 * Didn't find it. If we were *trying*
300 * to not find it, then we succeeded.
302 if (check_item->operator == T_OP_CMP_FALSE)
309 * Else we found it, but we were trying to not
310 * find it, so we failed.
312 if (check_item->operator == T_OP_CMP_FALSE)
317 * We've got to xlat the string before doing
320 if (check_item->flags.do_xlat) {
322 char buffer[sizeof(check_item->vp_strvalue)];
324 check_item->flags.do_xlat = 0;
325 rcode = radius_xlat(buffer, sizeof(buffer),
326 check_item->vp_strvalue,
330 * Parse the string into a new value.
332 pairparsevalue(check_item, buffer);
336 * OK it is present now compare them.
338 compare = compare_pair(req, auth_item, check_item, check, reply);
340 switch (check_item->operator) {
343 radlog(L_INFO, "Invalid operator for item %s: "
344 "reverting to '=='", check_item->name);
346 case T_OP_CMP_TRUE: /* compare always == 0 */
347 case T_OP_CMP_FALSE: /* compare always == 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;
365 if (compare > 0) result = -1;
369 if (compare < 0) result = -1;
376 regmatch_t rxmatch[REQUEST_MAX_REGEX + 1];
378 if ((auth_item->type == PW_TYPE_IPADDR) &&
379 (auth_item->vp_strvalue[0] == '\0')) {
380 inet_ntop(AF_INET, &(auth_item->lvalue),
381 auth_item->vp_strvalue,
382 sizeof(auth_item->vp_strvalue));
386 * Include substring matches.
388 regcomp(®, (char *)check_item->vp_strvalue,
390 compare = regexec(®,
391 (char *)auth_item->vp_strvalue,
392 REQUEST_MAX_REGEX + 1,
397 * Add %{0}, %{1}, etc.
399 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
401 char buffer[sizeof(check_item->vp_strvalue)];
404 * Didn't match: delete old
405 * match, if it existed.
407 if ((compare != 0) ||
408 (rxmatch[i].rm_so == -1)) {
409 p = request_data_get(req, req,
410 REQUEST_DATA_REGEX | i);
424 * Copy substring into buffer.
427 auth_item->vp_strvalue + rxmatch[i].rm_so,
428 rxmatch[i].rm_eo - rxmatch[i].rm_so);
429 buffer[rxmatch[i].rm_eo - rxmatch[i].rm_so] = '\0';
432 * Copy substring, and add it to
435 * Note that we don't check
436 * for out of memory, which is
437 * the only error we can get...
440 request_data_add(req,
442 REQUEST_DATA_REGEX | i,
446 if (compare != 0) result = -1;
450 if ((auth_item->type == PW_TYPE_IPADDR) &&
451 (auth_item->vp_strvalue[0] == '\0')) {
452 inet_ntop(AF_INET, &(auth_item->lvalue),
453 auth_item->vp_strvalue,
454 sizeof(auth_item->vp_strvalue));
457 regcomp(®, (char *)check_item->vp_strvalue, REG_EXTENDED|REG_NOSUB);
458 compare = regexec(®, (char *)auth_item->vp_strvalue,
461 if (compare == 0) result = -1;
465 } /* switch over the operator of the check item */
468 * This attribute didn't match, but maybe there's
469 * another of the same attribute, which DOES match.
472 auth_item = auth_item->next;
477 } /* for every entry in the check item list */
479 return 0; /* it matched */
483 * Compare two attributes simply. Calls compare_pair.
486 int simplepaircmp(REQUEST *req, VALUE_PAIR *first, VALUE_PAIR *second)
488 return compare_pair( req, first, second, NULL, NULL );
493 * Move pairs, replacing/over-writing them, and doing xlat.
496 * Move attributes from one list to the other
497 * if not already present.
499 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
501 VALUE_PAIR **tailto, *i, *j, *next;
502 VALUE_PAIR *tailfrom = NULL;
506 * Point "tailto" to the end of the "to" list.
509 for(i = *to; i; i = i->next) {
514 * Loop over the "from" list.
516 for(i = *from; i; i = next) {
520 * Don't move 'fallthrough' over.
522 if (i->attribute == PW_FALL_THROUGH) {
527 * We've got to xlat the string before moving
530 if (i->flags.do_xlat) {
532 char buffer[sizeof(i->vp_strvalue)];
534 i->flags.do_xlat = 0;
535 rcode = radius_xlat(buffer, sizeof(buffer),
540 * Parse the string into a new value.
542 pairparsevalue(i, buffer);
545 found = pairfind(*to, i->attribute);
546 switch (i->operator) {
549 * If a similar attribute is found,
552 case T_OP_SUB: /* -= */
554 if (!i->vp_strvalue[0] ||
555 (strcmp((char *)found->vp_strvalue,
556 (char *)i->vp_strvalue) == 0)){
557 pairdelete(to, found->attribute);
560 * 'tailto' may have been
564 for(j = *to; j; j = j->next) {
574 * Add it, if it's not already there.
576 case T_OP_EQ: /* = */
579 continue; /* with the loop */
584 * If a similar attribute is found,
585 * replace it with the new one. Otherwise,
586 * add the new one to the list.
588 case T_OP_SET: /* := */
593 memcpy(found, i, sizeof(*found));
600 * FIXME: Add support for <=, >=, <, >
602 * which will mean (for integers)
603 * 'make the attribute the smaller, etc'
607 * Add the new element to the list, even
608 * if similar ones already exist.
611 case T_OP_ADD: /* += */
616 tailfrom->next = next;
621 * If ALL of the 'to' attributes have been deleted,
622 * then ensure that the 'tail' is updated to point
633 } /* loop over the 'from' list */