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);
578 * If Strip-User-Name == No, then don't do any more.
580 vp = pairfind(check_pairs, PW_STRIP_USER_NAME);
581 if (vp && !vp->lvalue) return ret;
584 * See where to put the stripped user name.
586 vp = pairfind(check_pairs, PW_STRIPPED_USER_NAME);
588 vp = paircreate(PW_STRIPPED_USER_NAME, PW_TYPE_STRING);
589 if (!vp) return ret; /* no memory, do anything? */
591 pairadd(&request, vp);
594 strcpy((char *)vp->strvalue, rest);
595 vp->length = strlen(rest);
602 * Compare the current time to a range.
604 static int timecmp(void *instance,
606 VALUE_PAIR *request, VALUE_PAIR *check,
607 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
610 request = request; /* shut the compiler up */
611 check_pairs = check_pairs;
612 reply_pairs = reply_pairs;
615 * If there's a request, use that timestamp.
617 if (timestr_match((char *)check->strvalue,
618 req ? req->timestamp : time(NULL)) >= 0) {
625 * Matches if there is NO SUCH ATTRIBUTE as the one named
626 * in check->strvalue. If there IS such an attribute, it
629 * This is ugly, and definitely non-optimal. We should be
630 * doing the lookup only ONCE, and storing the result
631 * in check->lvalue...
633 static int attrcmp(void *instance,
635 VALUE_PAIR *request, VALUE_PAIR *check,
636 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
643 check_pairs = check_pairs; /* shut the compiler up */
644 reply_pairs = reply_pairs;
646 if (check->lvalue == 0) {
647 dict = dict_attrbyname((char *)check->strvalue);
653 attr = check->lvalue;
657 * If there's no such attribute, then return MATCH,
660 pair = pairfind(request, attr);
669 * Compare the expiration date.
671 static int expirecmp(void *instance, REQUEST *req UNUSED,
672 VALUE_PAIR *request, VALUE_PAIR *check,
673 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
678 request = request; /* shut the compiler up */
679 check_pairs = check_pairs;
680 reply_pairs = reply_pairs;
683 * FIXME! This should be request->timestamp!
687 if (now <= (signed)check->lvalue) {
695 * Compare the request packet type.
697 static int packetcmp(void *instance UNUSED, REQUEST *req,
698 VALUE_PAIR *request UNUSED,
700 VALUE_PAIR *check_pairs UNUSED,
701 VALUE_PAIR **reply_pairs UNUSED)
703 if (req->packet->code == check->lvalue) {
711 * Compare the response packet type.
713 static int responsecmp(void *instance UNUSED,
715 VALUE_PAIR *request UNUSED,
717 VALUE_PAIR *check_pairs UNUSED,
718 VALUE_PAIR **reply_pairs UNUSED)
720 if (req->reply->code == check->lvalue) {
728 * Register server-builtin special attributes.
730 void pair_builtincompare_init(void)
732 paircompare_register(PW_NAS_PORT, -1, portcmp, NULL);
733 paircompare_register(PW_PREFIX, PW_USER_NAME, presufcmp, NULL);
734 paircompare_register(PW_SUFFIX, PW_USER_NAME, presufcmp, NULL);
735 paircompare_register(PW_CONNECT_RATE, PW_CONNECT_INFO, connectcmp, NULL);
736 paircompare_register(PW_CURRENT_TIME, 0, timecmp, NULL);
737 paircompare_register(PW_NO_SUCH_ATTRIBUTE, 0, attrcmp, NULL);
738 paircompare_register(PW_EXPIRATION, 0, expirecmp, NULL);
739 paircompare_register(PW_PACKET_TYPE, 0, packetcmp, NULL);
740 paircompare_register(PW_RESPONSE_PACKET_TYPE, 0, responsecmp, NULL);
744 * Move pairs, replacing/over-writing them, and doing xlat.
747 * Move attributes from one list to the other
748 * if not already present.
750 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
752 VALUE_PAIR **tailto, *i, *j, *next;
753 VALUE_PAIR *tailfrom = NULL;
757 * Point "tailto" to the end of the "to" list.
760 for(i = *to; i; i = i->next) {
765 * Loop over the "from" list.
767 for(i = *from; i; i = next) {
771 * Don't move 'fallthrough' over.
773 if (i->attribute == PW_FALL_THROUGH) {
778 * We've got to xlat the string before moving
781 if (i->flags.do_xlat) {
783 char buffer[sizeof(i->strvalue)];
785 i->flags.do_xlat = 0;
786 rcode = radius_xlat(buffer, sizeof(buffer),
791 * Parse the string into a new value.
793 pairparsevalue(i, buffer);
796 found = pairfind(*to, i->attribute);
797 switch (i->operator) {
800 * If a similar attribute is found,
803 case T_OP_SUB: /* -= */
805 if (!i->strvalue[0] ||
806 (strcmp((char *)found->strvalue,
807 (char *)i->strvalue) == 0)){
808 pairdelete(to, found->attribute);
811 * 'tailto' may have been
815 for(j = *to; j; j = j->next) {
825 * Add it, if it's not already there.
827 case T_OP_EQ: /* = */
830 continue; /* with the loop */
835 * If a similar attribute is found,
836 * replace it with the new one. Otherwise,
837 * add the new one to the list.
839 case T_OP_SET: /* := */
844 memcpy(found, i, sizeof(*found));
851 * FIXME: Add support for <=, >=, <, >
853 * which will mean (for integers)
854 * 'make the attribute the smaller, etc'
858 * Add the new element to the list, even
859 * if similar ones already exist.
862 case T_OP_ADD: /* += */
867 tailfrom->next = next;
872 * If ALL of the 'to' attributes have been deleted,
873 * then ensure that the 'tail' is updated to point
884 } /* loop over the 'from' list */