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