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,
410 buffer[rxmatch[i].rm_eo] = '\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 * Compare a Connect-Info and a Connect-Rate
469 static int connectcmp(void *instance,
473 VALUE_PAIR *check_pairs,
474 VALUE_PAIR **reply_pairs)
479 check_pairs = check_pairs; /* shut the compiler up */
480 reply_pairs = reply_pairs;
482 rate = atoi((char *)request->strvalue);
483 return rate - check->lvalue;
488 * Compare a portno with a range.
490 static int portcmp(void *instance,
491 REQUEST *req UNUSED, VALUE_PAIR *request, VALUE_PAIR *check,
492 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
494 char buf[MAX_STRING_LEN];
497 uint32_t port = request->lvalue;
500 check_pairs = check_pairs; /* shut the compiler up */
501 reply_pairs = reply_pairs;
503 if ((strchr((char *)check->strvalue, ',') == NULL) &&
504 (strchr((char *)check->strvalue, '-') == NULL)) {
505 return (request->lvalue - check->lvalue);
509 strcpy(buf, (char *)check->strvalue);
510 s = strtok(buf, ",");
513 if ((p = strchr(s, '-')) != NULL)
517 lo = strtoul(s, NULL, 10);
518 hi = strtoul(p, NULL, 10);
519 if (lo <= port && port <= hi) {
522 s = strtok(NULL, ",");
529 * Compare prefix/suffix.
532 * - if PW_STRIP_USER_NAME is present in check_pairs,
533 * strip the username of prefix/suffix.
534 * - if PW_STRIP_USER_NAME is not present in check_pairs,
535 * add a PW_STRIPPED_USER_NAME to the request.
537 static int presufcmp(void *instance,
539 VALUE_PAIR *request, VALUE_PAIR *check,
540 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
543 char *name = (char *)request->strvalue;
544 char rest[MAX_STRING_LEN];
549 reply_pairs = reply_pairs; /* shut the compiler up */
552 printf("Comparing %s and %s, check->attr is %d\n",
553 name, check->strvalue, check->attribute);
556 len = strlen((char *)check->strvalue);
557 switch (check->attribute) {
559 ret = strncmp(name, (char *)check->strvalue, len);
560 if (ret == 0 && rest)
561 strcpy(rest, name + len);
564 namelen = strlen(name);
567 ret = strcmp(name + namelen - len,
568 (char *)check->strvalue);
569 if (ret == 0 && rest) {
570 strNcpy(rest, name, namelen - len + 1);
577 if (pairfind(check_pairs, PW_STRIP_USER_NAME)) {
579 * I don't think we want to update the User-Name
580 * attribute in place... - atd
582 strcpy((char *)request->strvalue, rest);
583 request->length = strlen(rest);
585 if ((vp = pairfind(check_pairs, PW_STRIPPED_USER_NAME)) != NULL){
586 strcpy((char *)vp->strvalue, rest);
587 vp->length = strlen(rest);
588 } else if ((vp = paircreate(PW_STRIPPED_USER_NAME,
589 PW_TYPE_STRING)) != NULL) {
590 strcpy((char *)vp->strvalue, rest);
591 vp->length = strlen(rest);
592 pairadd(&request, vp);
593 } /* else no memory! Die, die!: FIXME!! */
601 * Compare the current time to a range.
603 static int timecmp(void *instance,
605 VALUE_PAIR *request, VALUE_PAIR *check,
606 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
609 request = request; /* shut the compiler up */
610 check_pairs = check_pairs;
611 reply_pairs = reply_pairs;
614 * If there's a request, use that timestamp.
616 if (timestr_match((char *)check->strvalue,
617 req ? req->timestamp : time(NULL)) >= 0) {
624 * Matches if there is NO SUCH ATTRIBUTE as the one named
625 * in check->strvalue. If there IS such an attribute, it
628 * This is ugly, and definitely non-optimal. We should be
629 * doing the lookup only ONCE, and storing the result
630 * in check->lvalue...
632 static int attrcmp(void *instance,
634 VALUE_PAIR *request, VALUE_PAIR *check,
635 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
642 check_pairs = check_pairs; /* shut the compiler up */
643 reply_pairs = reply_pairs;
645 if (check->lvalue == 0) {
646 dict = dict_attrbyname((char *)check->strvalue);
652 attr = check->lvalue;
656 * If there's no such attribute, then return MATCH,
659 pair = pairfind(request, attr);
668 * Compare the expiration date.
670 static int expirecmp(void *instance, REQUEST *req UNUSED,
671 VALUE_PAIR *request, VALUE_PAIR *check,
672 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
677 request = request; /* shut the compiler up */
678 check_pairs = check_pairs;
679 reply_pairs = reply_pairs;
682 * FIXME! This should be request->timestamp!
686 if (now <= (signed)check->lvalue) {
694 * Compare the request packet type.
696 static int packetcmp(void *instance UNUSED, REQUEST *req,
697 VALUE_PAIR *request UNUSED,
699 VALUE_PAIR *check_pairs UNUSED,
700 VALUE_PAIR **reply_pairs UNUSED)
702 if (req->packet->code == check->lvalue) {
710 * Compare the response packet type.
712 static int responsecmp(void *instance UNUSED,
714 VALUE_PAIR *request UNUSED,
716 VALUE_PAIR *check_pairs UNUSED,
717 VALUE_PAIR **reply_pairs UNUSED)
719 if (req->reply->code == check->lvalue) {
727 * Register server-builtin special attributes.
729 void pair_builtincompare_init(void)
731 paircompare_register(PW_NAS_PORT, -1, portcmp, NULL);
732 paircompare_register(PW_PREFIX, PW_USER_NAME, presufcmp, NULL);
733 paircompare_register(PW_SUFFIX, PW_USER_NAME, presufcmp, NULL);
734 paircompare_register(PW_CONNECT_RATE, PW_CONNECT_INFO, connectcmp, NULL);
735 paircompare_register(PW_CURRENT_TIME, 0, timecmp, NULL);
736 paircompare_register(PW_NO_SUCH_ATTRIBUTE, 0, attrcmp, NULL);
737 paircompare_register(PW_EXPIRATION, 0, expirecmp, NULL);
738 paircompare_register(PW_PACKET_TYPE, 0, packetcmp, NULL);
739 paircompare_register(PW_RESPONSE_PACKET_TYPE, 0, responsecmp, NULL);
743 * Move pairs, replacing/over-writing them, and doing xlat.
746 * Move attributes from one list to the other
747 * if not already present.
749 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
751 VALUE_PAIR **tailto, *i, *j, *next;
752 VALUE_PAIR *tailfrom = NULL;
756 * Point "tailto" to the end of the "to" list.
759 for(i = *to; i; i = i->next) {
764 * Loop over the "from" list.
766 for(i = *from; i; i = next) {
770 * Don't move 'fallthrough' over.
772 if (i->attribute == PW_FALL_THROUGH) {
777 * We've got to xlat the string before moving
780 if (i->flags.do_xlat) {
782 char buffer[sizeof(i->strvalue)];
784 i->flags.do_xlat = 0;
785 rcode = radius_xlat(buffer, sizeof(buffer),
790 * Parse the string into a new value.
792 pairparsevalue(i, buffer);
795 found = pairfind(*to, i->attribute);
796 switch (i->operator) {
799 * If a similar attribute is found,
802 case T_OP_SUB: /* -= */
804 if (!i->strvalue[0] ||
805 (strcmp((char *)found->strvalue,
806 (char *)i->strvalue) == 0)){
807 pairdelete(to, found->attribute);
810 * 'tailto' may have been
814 for(j = *to; j; j = j->next) {
824 * Add it, if it's not already there.
826 case T_OP_EQ: /* = */
829 continue; /* with the loop */
834 * If a similar attribute is found,
835 * replace it with the new one. Otherwise,
836 * add the new one to the list.
838 case T_OP_SET: /* := */
843 memcpy(found, i, sizeof(*found));
850 * FIXME: Add support for <=, >=, <, >
852 * which will mean (for integers)
853 * 'make the attribute the smaller, etc'
857 * Add the new element to the list, even
858 * if similar ones already exist.
861 case T_OP_ADD: /* += */
866 tailfrom->next = next;
871 * If ALL of the 'to' attributes have been deleted,
872 * then ensure that the 'tail' is updated to point
883 } /* loop over the 'from' list */