port fix from branch_1_1
[freeradius.git] / src / main / client.c
1 /*
2  * client.c     Read clients into memory.
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2000,2006  The FreeRADIUS server project
21  * Copyright 2000  Miquel van Smoorenburg <miquels@cistron.nl>
22  * Copyright 2000  Alan DeKok <aland@ox.org>
23  */
24
25 #include <freeradius-devel/ident.h>
26 RCSID("$Id$")
27
28 #include <freeradius-devel/autoconf.h>
29
30 #include <sys/stat.h>
31
32 #ifdef HAVE_NETINET_IN_H
33 #       include <netinet/in.h>
34 #endif
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <netdb.h>
39 #include <ctype.h>
40 #include <fcntl.h>
41
42 #include <freeradius-devel/radiusd.h>
43 #include <freeradius-devel/rad_assert.h>
44
45 struct radclient_list {
46         /*
47          *      FIXME: One set of trees for IPv4, and another for IPv6?
48          */
49         rbtree_t        *trees[129]; /* for 0..128, inclusive. */
50         int             min_prefix;
51 #ifdef WITH_SNMP
52         rbtree_t        *num;   /* client numbers 0..N */
53         int             max;
54 #endif
55 };
56
57 /*
58  *      Callback for freeing a client.
59  */
60 void client_free(RADCLIENT *client)
61 {
62         free(client->longname);
63         free(client->secret);
64         free(client->shortname);
65         free(client->nastype);
66         free(client->login);
67         free(client->password);
68         
69         free(client);
70 }
71
72
73 /*
74  *      Callback for comparing two clients.
75  */
76 static int client_ipaddr_cmp(const void *one, const void *two)
77 {
78         const RADCLIENT *a = one;
79         const RADCLIENT *b = two;
80
81         if (a->ipaddr.af < b->ipaddr.af) return -1;
82         if (a->ipaddr.af > b->ipaddr.af) return +1;
83
84         rad_assert(a->prefix == b->prefix);
85
86         switch (a->ipaddr.af) {
87         case AF_INET:
88                 return memcmp(&a->ipaddr.ipaddr.ip4addr,
89                               &b->ipaddr.ipaddr.ip4addr,
90                               sizeof(a->ipaddr.ipaddr.ip4addr));
91                 break;
92
93         case AF_INET6:
94                 return memcmp(&a->ipaddr.ipaddr.ip6addr,
95                               &b->ipaddr.ipaddr.ip6addr,
96                               sizeof(a->ipaddr.ipaddr.ip6addr));
97                 break;
98
99         default:
100                 break;
101         }
102
103         /*
104          *      Something bad happened...
105          */
106         rad_assert("Internal sanity check failed");
107         return -1;
108 }
109
110 #ifdef WITH_SNMP
111 static int client_num_cmp(const void *one, const void *two)
112 {
113         const RADCLIENT *a = one;
114         const RADCLIENT *b = two;
115
116         return (a->number - b->number);
117 }
118 #endif
119
120 /*
121  *      Free a RADCLIENT list.
122  */
123 void clients_free(RADCLIENT_LIST *clients)
124 {
125         int i;
126
127         if (!clients) return;
128
129         for (i = 0; i <= 128; i++) {
130                 if (clients->trees[i]) rbtree_free(clients->trees[i]);
131                 clients->trees[i] = NULL;
132         }
133 #ifdef WITH_SNMP
134         if (clients->num) rbtree_free(clients->num);
135 #endif
136         free(clients);
137 }
138
139 /*
140  *      Return a new, initialized, set of clients.
141  */
142 RADCLIENT_LIST *clients_init(void)
143 {
144         RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
145
146         if (!clients) return NULL;
147
148         clients->min_prefix = 128;
149
150         return clients;
151 }
152
153
154 /*
155  *      Sanity check a client.
156  */
157 static int client_sane(RADCLIENT *client)
158 {
159         switch (client->ipaddr.af) {
160         case AF_INET:
161                 if (client->prefix > 32) {
162                         return 0;
163                 }
164
165                 /*
166                  *      Zero out the subnet bits.
167                  */
168                 if (client->prefix < 32) {
169                         uint32_t mask = ~0;
170
171                         mask <<= (32 - client->prefix);
172                         client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
173                 }
174                 break;
175
176         case AF_INET6:
177                 if (client->prefix > 128) return 0;
178
179                 if (client->prefix < 128) {
180                         int i;
181                         uint32_t mask, *addr;
182
183                         addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
184
185                         for (i = client->prefix; i < 128; i += 32) {
186                                 mask = ~0;
187                                 mask <<= ((128 - i) & 0x1f);
188                                 addr[i / 32] &= mask;
189                         }
190                 }
191                 break;
192
193         default:
194                 return 0;
195         }
196
197         return 1;
198 }
199
200
201 /*
202  *      Add a client to the tree.
203  */
204 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
205 {
206         if (!clients || !client) {
207                 return 0;
208         }
209
210         if (client->prefix < 0) {
211                 return 0;
212         }
213
214         if (!client_sane(client)) return 0;
215
216         /*
217          *      Create a tree for it.
218          */
219         if (!clients->trees[client->prefix]) {
220                 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
221                                                                client_free, 0);
222                 if (!clients->trees[client->prefix]) {
223                         return 0;
224                 }
225         }
226
227         /*
228          *      Duplicate?
229          */
230         if (!rbtree_insert(clients->trees[client->prefix], client)) {
231                 return 0;
232         }
233
234 #ifdef WITH_SNMP
235         if (!clients->num) rbtree_create(client_num_cmp, NULL, 0);
236
237         client->number = clients->max;
238         clients->max++;
239         if (clients->num) rbtree_insert(clients->num, client);
240 #endif
241
242         if (client->prefix < clients->min_prefix) {
243                 clients->min_prefix = client->prefix;
244         }
245
246         return 1;
247 }
248
249
250 /*
251  *      Find a client in the RADCLIENTS list by number.
252  *      This is a support function for the SNMP code.
253  */
254 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
255                                int number)
256 {
257 #ifdef WITH_SNMP
258         if (!clients) return NULL;
259
260         if (clients->num) {
261                 RADCLIENT myclient;
262                 
263                 myclient.number = number;
264                 
265                 return rbtree_finddata(clients->num, &myclient);
266         }
267 #endif
268         return NULL;
269 }
270
271
272 /*
273  *      Find a client in the RADCLIENTS list.
274  */
275 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
276                        const lrad_ipaddr_t *ipaddr)
277 {
278         int i, max_prefix;
279         RADCLIENT myclient;
280
281         if (!clients || !ipaddr) return NULL;
282
283         switch (ipaddr->af) {
284         case AF_INET:
285                 max_prefix = 32;
286                 break;
287
288         case AF_INET6:
289                 max_prefix = 128;
290                 break;
291
292         default :
293                 return NULL;
294         }
295
296         for (i = max_prefix; i >= clients->min_prefix; i--) {
297                 void *data;
298
299                 myclient.prefix = i;
300                 myclient.ipaddr = *ipaddr;
301                 client_sane(&myclient); /* clean up the ipaddress */
302
303                 if (!clients->trees[i]) continue;
304                 
305                 data = rbtree_finddata(clients->trees[i], &myclient);
306                 if (data) {
307                         return data;
308                 }
309         }
310
311         return NULL;
312 }
313
314
315 /*
316  *      Old wrapper for client_find
317  */
318 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
319 {
320         return client_find(mainconfig.clients, ipaddr);
321 }
322
323
324 /*
325  *      Find the name of a client (prefer short name).
326  */
327 const char *client_name(const RADCLIENT_LIST *clients,
328                         const lrad_ipaddr_t *ipaddr)
329 {
330         /* We don't call this unless we should know about the client. */
331         RADCLIENT *cl;
332         char host_ipaddr[128];
333
334         if ((cl = client_find(clients, ipaddr)) != NULL) {
335                 if (cl->shortname && cl->shortname[0])
336                         return cl->shortname;
337                 else
338                         return cl->longname;
339         }
340
341         /*
342          * this isn't normally reachable, but if a loggable event happens just
343          * after a client list change and a HUP, then we may not know this
344          * information any more.
345          *
346          * If you see lots of these, then there's something wrong.
347          */
348         radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
349                ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
350
351         return "UNKNOWN-CLIENT";
352 }
353
354 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
355 {
356         return client_name(mainconfig.clients, ipaddr);
357 }
358
359 static const CONF_PARSER client_config[] = {
360         { "secret",  PW_TYPE_STRING_PTR, 
361           offsetof(RADCLIENT, secret), 0, NULL },
362         { "shortname",  PW_TYPE_STRING_PTR, 
363           offsetof(RADCLIENT, shortname), 0, NULL },
364         { "nastype",  PW_TYPE_STRING_PTR, 
365           offsetof(RADCLIENT, nastype), 0, NULL },
366         { "login",  PW_TYPE_STRING_PTR, 
367           offsetof(RADCLIENT, login), 0, NULL },
368         { "password",  PW_TYPE_STRING_PTR, 
369           offsetof(RADCLIENT, password), 0, NULL },
370
371         { NULL, -1, 0, NULL, NULL }
372 };
373
374
375 /*
376  *      Create the linked list of clients from the new configuration
377  *      type.  This way we don't have to change too much in the other
378  *      source-files.
379  */
380 RADCLIENT_LIST *clients_parse_section(const char *filename,
381                                       CONF_SECTION *section)
382 {
383         CONF_SECTION    *cs;
384         RADCLIENT       *c;
385         char            *hostnm, *prefix_ptr = NULL;
386         const char      *name2;
387         RADCLIENT_LIST  *clients;
388
389         /*
390          *      Be forgiving.  If there's already a clients, return
391          *      it.  Otherwise create a new one.
392          */
393         clients = cf_data_find(section, "clients");
394         if (clients) return clients;
395
396         clients = clients_init();
397         if (!clients) return NULL;
398
399         /*
400          *      Associate the clients structure with the section, where
401          *      it will be freed once the section is freed.
402          */
403         if (cf_data_add(section, "clients", clients, clients_free) < 0) {
404                 radlog(L_ERR, "%s[%d]: Failed to associate clients with section %s",
405                        filename, cf_section_lineno(section),
406                        cf_section_name1(section));
407                 clients_free(clients);
408                 return NULL;
409         }
410
411         for (cs = cf_subsection_find_next(section, NULL, "client");
412              cs != NULL;
413              cs = cf_subsection_find_next(section, cs, "client")) {
414                 name2 = cf_section_name2(cs);
415                 if (!name2) {
416                         radlog(L_CONS|L_ERR, "%s[%d]: Missing client name",
417                                filename, cf_section_lineno(cs));
418                         return NULL;
419                 }
420                 /*
421                  * Check the lengths, we don't want any core dumps
422                  */
423                 hostnm = name2;
424                 prefix_ptr = strchr(hostnm, '/');
425
426                 /*
427                  * The size is fine.. Let's create the buffer
428                  */
429                 c = rad_malloc(sizeof(RADCLIENT));
430                 memset(c, 0, sizeof(RADCLIENT));
431
432                 if (cf_section_parse(cs, c, client_config) < 0) {
433                         radlog(L_CONS|L_ERR, "%s[%d]: Error parsing client section.",
434                                filename, cf_section_lineno(cs));
435                         return NULL;
436                 }
437
438                 /*
439                  * Look for prefixes.
440                  */
441                 c->prefix = -1;
442                 if (prefix_ptr) {
443                         c->prefix = atoi(prefix_ptr + 1);
444                         if ((c->prefix < 0) || (c->prefix > 128)) {
445                                 radlog(L_ERR, "%s[%d]: Invalid Prefix value '%s' for IP.",
446                                                 filename, cf_section_lineno(cs), prefix_ptr + 1);
447                                 return NULL;
448                         }
449                         /* Replace '/' with '\0' */
450                         *prefix_ptr = '\0';
451                 }
452
453                 /*
454                  * Always get the numeric representation of IP
455                  */
456                 if (ip_hton(hostnm, AF_UNSPEC, &c->ipaddr) < 0) {
457                         radlog(L_CONS|L_ERR, "%s[%d]: Failed to look up hostname %s: %s",
458                                filename, cf_section_lineno(cs),
459                                hostnm, librad_errstr);
460                         return NULL;
461                 } else {
462                         char buffer[256];
463                         ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
464                         c->longname = strdup(buffer);
465                 }
466
467                 /*
468                  *      This makes later life easier.
469                  */
470                 if (!c->shortname) c->shortname = strdup(c->longname);
471
472                 if (c->prefix < 0) switch (c->ipaddr.af) {
473                 case AF_INET:
474                         c->prefix = 32;
475                         break;
476                 case AF_INET6:
477                         c->prefix = 128;
478                         break;
479                 default:
480                         break;
481                 }
482
483                 /*
484                  *      FIXME: Add the client as data via cf_data_add,
485                  *      for migration issues.
486                  */
487
488                 if (!client_add(clients, c)) {
489                         radlog(L_CONS|L_ERR, "%s[%d]: Failed to add client %s",
490                                filename, cf_section_lineno(cs), hostnm);
491                         client_free(c);
492                         return NULL;
493                 }
494         }
495
496         return clients;
497 }