static const char rcsid[] = "$Id: nas.c,v 1.22.4.1 2007/02/09 15:56:12 aland Exp $";
#include "autoconf.h"
#include "libradius.h"
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "radiusd.h"
static NAS *naslist = NULL;
static void nas_free(NAS *cl)
{
NAS *next;
while(cl) {
next = cl->next;
free(cl);
cl = next;
}
}
int read_naslist_file(char *file)
{
FILE *fp;
char buffer[256];
char hostnm[256];
char shortnm[256];
char nastype[256];
int lineno = 0;
char *p;
NAS *nas;
nas_free(naslist);
naslist = NULL;
if (!file) return 0;
if ((fp = fopen(file, "r")) == NULL) {
return 0;
}
radlog(L_INFO, "Using deprecated naslist file. Support for this will go away soon.");
while(fgets(buffer, 256, fp) != NULL) {
lineno++;
if (!feof(fp) && (strchr(buffer, '\n') == NULL)) {
radlog(L_ERR, "%s[%d]: line too long", file, lineno);
return -1;
}
if (buffer[0] == '#' || buffer[0] == '\n')
continue;
p = buffer;
if (!getword(&p, hostnm, sizeof(hostnm)) ||
!getword(&p, shortnm, sizeof(shortnm))) {
radlog(L_ERR, "%s[%d]: unexpected end of line",
file, lineno);
continue;
}
(void)getword(&p, nastype, sizeof(nastype));
if (strlen(hostnm) >= sizeof(nas->longname)) {
radlog(L_ERR, "%s[%d]: host name of length %d is greater than the allowed maximum of %d.",
file, lineno,
(int) strlen(hostnm),
(int) sizeof(nas->longname) - 1);
return -1;
}
if (strlen(shortnm) > sizeof(nas->shortname)) {
radlog(L_ERR, "%s[%d]: short name of length %d is greater than the allowed maximum of %d.",
file, lineno,
(int) strlen(shortnm),
(int) sizeof(nas->shortname) - 1);
return -1;
}
if (strlen(nastype) >= sizeof(nas->nastype)) {
radlog(L_ERR, "%s[%d]: NAS type of length %d is greater than the allowed maximum of %d.",
file, lineno,
(int) strlen(nastype),
(int) sizeof(nas->nastype) - 1);
return -1;
}
nas = rad_malloc(sizeof(NAS));
memset(nas, 0, sizeof(*nas));
strcpy(nas->nastype, nastype);
strcpy(nas->shortname, shortnm);
if (strcmp(hostnm, "DEFAULT") == 0) {
nas->ipaddr = 0;
strcpy(nas->longname, hostnm);
} else {
nas->ipaddr = ip_getaddr(hostnm);
ip_hostname(nas->longname, sizeof(nas->longname),
nas->ipaddr);
}
nas->next = naslist;
naslist = nas;
}
fclose(fp);
return 0;
}
NAS *nas_find(uint32_t ipaddr)
{
NAS *nas;
NAS *default_nas;
default_nas = NULL;
for (nas = naslist; nas; nas = nas->next) {
if (ipaddr == nas->ipaddr)
return nas;
if (strcmp(nas->longname, "DEFAULT") == 0)
default_nas = nas;
}
return default_nas;
}
NAS *nas_findbyname(char *nasname)
{
NAS *nas;
NAS *default_nas;
default_nas = NULL;
for (nas = naslist; nas; nas = nas->next) {
if (strcmp(nasname, nas->shortname) == 0 ||
strcmp(nasname, nas->longname) == 0)
return nas;
if (strcmp(nas->longname, "DEFAULT") == 0)
default_nas = nas;
}
return default_nas;
}
const char *nas_name(uint32_t ipaddr)
{
NAS *nas;
if ((nas = nas_find(ipaddr)) != NULL) {
if (nas->shortname[0])
return nas->shortname;
else
return nas->longname;
}
return "UNKNOWN-NAS";
}
const char *nas_name2(RADIUS_PACKET *packet)
{
NAS *nas;
if ((nas = nas_find(packet->src_ipaddr)) != NULL) {
if (nas->shortname[0])
return nas->shortname;
else
return nas->longname;
}
return "UNKNOWN-NAS";
}
char * nas_name3(char *buf, size_t buflen, uint32_t ipaddr)
{
NAS *nas;
if ((nas = nas_find(ipaddr)) != NULL) {
if (nas->shortname[0]) {
strNcpy(buf, (char *)nas->shortname, buflen);
return buf;
}
else {
strNcpy(buf, (char *)nas->longname, buflen);
return buf;
}
}
ip_ntoa(buf, ipaddr);
return buf;
}