2 * paircmp.c Valuepair functions for various attributes
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000 The FreeRADIUS server project
21 * Copyright 2000 Alan DeKok <aland@ox.org>
24 static const char rcsid[] = "$Id$";
27 #include "libradius.h"
37 * Compare a Connect-Info and a Connect-Rate
39 static int connectcmp(void *instance,
43 VALUE_PAIR *check_pairs,
44 VALUE_PAIR **reply_pairs)
49 check_pairs = check_pairs; /* shut the compiler up */
50 reply_pairs = reply_pairs;
52 rate = atoi((char *)request->strvalue);
53 return rate - check->lvalue;
58 * Compare a portno with a range.
60 static int portcmp(void *instance,
61 REQUEST *req UNUSED, VALUE_PAIR *request, VALUE_PAIR *check,
62 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
64 char buf[MAX_STRING_LEN];
67 uint32_t port = request->lvalue;
70 check_pairs = check_pairs; /* shut the compiler up */
71 reply_pairs = reply_pairs;
73 if ((strchr((char *)check->strvalue, ',') == NULL) &&
74 (strchr((char *)check->strvalue, '-') == NULL)) {
75 return (request->lvalue - check->lvalue);
79 strcpy(buf, (char *)check->strvalue);
83 if ((p = strchr(s, '-')) != NULL)
87 lo = strtoul(s, NULL, 10);
88 hi = strtoul(p, NULL, 10);
89 if (lo <= port && port <= hi) {
92 s = strtok(NULL, ",");
99 * Compare prefix/suffix.
102 * - if PW_STRIP_USER_NAME is present in check_pairs,
103 * strip the username of prefix/suffix.
104 * - if PW_STRIP_USER_NAME is not present in check_pairs,
105 * add a PW_STRIPPED_USER_NAME to the request.
107 static int presufcmp(void *instance,
109 VALUE_PAIR *request, VALUE_PAIR *check,
110 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
113 char *name = (char *)request->strvalue;
114 char rest[MAX_STRING_LEN];
119 reply_pairs = reply_pairs; /* shut the compiler up */
122 printf("Comparing %s and %s, check->attr is %d\n",
123 name, check->strvalue, check->attribute);
126 len = strlen((char *)check->strvalue);
127 switch (check->attribute) {
129 ret = strncmp(name, (char *)check->strvalue, len);
130 if (ret == 0 && rest)
131 strcpy(rest, name + len);
134 namelen = strlen(name);
137 ret = strcmp(name + namelen - len,
138 (char *)check->strvalue);
139 if (ret == 0 && rest) {
140 strNcpy(rest, name, namelen - len + 1);
148 * If Strip-User-Name == No, then don't do any more.
150 vp = pairfind(check_pairs, PW_STRIP_USER_NAME);
151 if (vp && !vp->lvalue) return ret;
154 * See where to put the stripped user name.
156 vp = pairfind(check_pairs, PW_STRIPPED_USER_NAME);
158 vp = paircreate(PW_STRIPPED_USER_NAME, PW_TYPE_STRING);
159 if (!vp) return ret; /* no memory, do anything? */
161 pairadd(&request, vp);
164 strcpy((char *)vp->strvalue, rest);
165 vp->length = strlen(rest);
172 * Matches if there is NO SUCH ATTRIBUTE as the one named
173 * in check->strvalue. If there IS such an attribute, it
176 * This is ugly, and definitely non-optimal. We should be
177 * doing the lookup only ONCE, and storing the result
178 * in check->lvalue...
180 static int attrcmp(void *instance,
182 VALUE_PAIR *request, VALUE_PAIR *check,
183 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
190 check_pairs = check_pairs; /* shut the compiler up */
191 reply_pairs = reply_pairs;
193 if (check->lvalue == 0) {
194 dict = dict_attrbyname((char *)check->strvalue);
200 attr = check->lvalue;
204 * If there's no such attribute, then return MATCH,
207 pair = pairfind(request, attr);
216 * Compare the request packet type.
218 static int packetcmp(void *instance UNUSED, REQUEST *req,
219 VALUE_PAIR *request UNUSED,
221 VALUE_PAIR *check_pairs UNUSED,
222 VALUE_PAIR **reply_pairs UNUSED)
224 if (req->packet->code == check->lvalue) {
232 * Compare the response packet type.
234 static int responsecmp(void *instance UNUSED,
236 VALUE_PAIR *request UNUSED,
238 VALUE_PAIR *check_pairs UNUSED,
239 VALUE_PAIR **reply_pairs UNUSED)
241 if (req->reply->code == check->lvalue) {
249 * Register server-builtin special attributes.
251 void pair_builtincompare_init(void)
253 paircompare_register(PW_NAS_PORT, -1, portcmp, NULL);
254 paircompare_register(PW_PREFIX, PW_USER_NAME, presufcmp, NULL);
255 paircompare_register(PW_SUFFIX, PW_USER_NAME, presufcmp, NULL);
256 paircompare_register(PW_CONNECT_RATE, PW_CONNECT_INFO, connectcmp, NULL);
257 paircompare_register(PW_NO_SUCH_ATTRIBUTE, 0, attrcmp, NULL);
258 paircompare_register(PW_PACKET_TYPE, 0, packetcmp, NULL);
259 paircompare_register(PW_RESPONSE_PACKET_TYPE, 0, responsecmp, NULL);