2 * nas.c Functions to do with a NASLIST. This is here because
3 * radzap needs it as well.
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 Miquel van Smoorenburg <miquels@cistron.nl>
23 * Copyright 2000 Alan DeKok <aland@ox.org>
26 static const char rcsid[] = "$Id$";
29 #include "libradius.h"
39 static NAS *naslist = NULL;
44 static void nas_free(NAS *cl)
58 int read_naslist_file(char *file)
72 if ((fp = fopen(file, "r")) == NULL) {
73 /* The naslist file is no longer required. All configuration
74 information comes from radiusd.conf. If naslist exists it
75 will be used, but if it doesn't exist it will be silently
79 radlog(L_INFO, "Using deprecated naslist file. Support for this will go away soon.");
80 while(fgets(buffer, 256, fp) != NULL) {
82 if (!feof(fp) && (strchr(buffer, '\n') == NULL)) {
83 radlog(L_ERR, "%s[%d]: line too long", file, lineno);
86 if (buffer[0] == '#' || buffer[0] == '\n')
90 if (!getword(&p, hostnm, sizeof(hostnm)) ||
91 !getword(&p, shortnm, sizeof(shortnm))) {
92 radlog(L_ERR, "%s[%d]: unexpected end of line", file, lineno);
95 (void)getword(&p, nastype, sizeof(nastype));
98 * Double-check lengths to be sure they're sane
100 if (strlen(hostnm) >= sizeof(nas->longname)) {
101 radlog(L_ERR, "%s[%d]: host name of length %d is greater than the allowed maximum of %d.",
103 strlen(hostnm), sizeof(nas->longname) - 1);
106 if (strlen(shortnm) > sizeof(nas->shortname)) {
107 radlog(L_ERR, "%s[%d]: short name of length %d is greater than the allowed maximum of %d.",
109 strlen(shortnm), sizeof(nas->shortname) - 1);
112 if (strlen(nastype) >= sizeof(nas->nastype)) {
113 radlog(L_ERR, "%s[%d]: NAS type of length %d is greater than the allowed maximum of %d.",
115 strlen(nastype), sizeof(nas->nastype) - 1);
120 * It should be OK now, let's create the buffer.
122 nas = rad_malloc(sizeof(NAS));
123 memset(nas, 0, sizeof(*nas));
125 strcpy(nas->nastype, nastype);
126 strcpy(nas->shortname, shortnm);
128 if (strcmp(hostnm, "DEFAULT") == 0) {
130 strcpy(nas->longname, hostnm);
132 nas->ipaddr = ip_getaddr(hostnm);
133 ip_hostname(nas->longname, sizeof(nas->longname),
147 * Find a nas by IP address.
148 * If it can't be found, return the DEFAULT nas, instead.
150 NAS *nas_find(uint32_t ipaddr)
157 for (nas = naslist; nas; nas = nas->next) {
158 if (ipaddr == nas->ipaddr)
160 if (strcmp(nas->longname, "DEFAULT") == 0)
169 * Find a nas by name.
170 * If it can't be found, return the DEFAULT nas, instead.
172 NAS *nas_findbyname(char *nasname)
179 for (nas = naslist; nas; nas = nas->next) {
180 if (strcmp(nasname, nas->shortname) == 0 ||
181 strcmp(nasname, nas->longname) == 0)
183 if (strcmp(nas->longname, "DEFAULT") == 0)
192 * Find the name of a nas (prefer short name).
194 const char *nas_name(uint32_t ipaddr)
198 if ((nas = nas_find(ipaddr)) != NULL) {
199 if (nas->shortname[0])
200 return nas->shortname;
202 return nas->longname;
205 return "UNKNOWN-NAS";
209 * Find the name of a nas (prefer short name) based on the request.
211 const char *nas_name2(RADIUS_PACKET *packet)
215 if ((nas = nas_find(packet->src_ipaddr)) != NULL) {
216 if (nas->shortname[0])
217 return nas->shortname;
219 return nas->longname;
222 return "UNKNOWN-NAS";
226 * Find the name of a nas (prefer short name) based on ipaddr,
227 * store in passed buffer. If NAS is unknown, return dotted quad.
229 char * nas_name3(char *buf, size_t buflen, uint32_t ipaddr)
233 if ((nas = nas_find(ipaddr)) != NULL) {
234 if (nas->shortname[0]) {
235 strNcpy(buf, (char *)nas->shortname, buflen);
239 strNcpy(buf, (char *)nas->longname, buflen);
243 ip_ntoa(buf, ipaddr);