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