Initial revision
[freeradius.git] / src / main / nas.c
1 /*
2  * nas.c        Functions to do with a NASLIST. This is here because
3  *              radzap needs it as well.
4  *
5  * Version:     @(#)nas.c  1.00  08-Aug-1999  miquels@cistron.nl
6  *
7  */
8
9 char nas_sccsid[] =
10 "@(#)nas.c      1.00 Copyright 1999 Cistron Internet Services B.V.";
11
12 #include        "autoconf.h"
13
14 #include        <sys/types.h>
15 #include        <sys/stat.h>
16
17 #include        <stdio.h>
18 #include        <stdlib.h>
19 #include        <string.h>
20
21 #if HAVE_MALLOC_H
22 #  include      <malloc.h>
23 #endif
24
25 #include        "radiusd.h"
26
27 NAS             *naslist;
28
29 /*
30  *      Free a NAS list.
31  */
32 static void nas_free(NAS *cl)
33 {
34         NAS *next;
35
36         while(cl) {
37                 next = cl->next;
38                 free(cl);
39                 cl = next;
40         }
41 }
42
43 /*
44  *      Read the nas file.
45  */
46 int read_naslist_file(char *file)
47 {
48         FILE    *fp;
49         char    buffer[256];
50         char    hostnm[128];
51         char    shortnm[32];
52         char    nastype[32];
53         int     lineno = 0;
54         char    *p;
55         NAS     *c;
56
57         nas_free(naslist);
58         naslist = NULL;
59
60         if ((fp = fopen(file, "r")) == NULL) {
61                 log(L_CONS|L_ERR, "cannot open %s", file);
62                 return -1;
63         }
64         while(fgets(buffer, 256, fp) != NULL) {
65                 lineno++;
66                 if (buffer[0] == '#' || buffer[0] == '\n')
67                         continue;
68                 p = buffer;
69                 if (!getword(&p, hostnm, sizeof(hostnm)) ||
70                     !getword(&p, shortnm, sizeof(shortnm))) {
71                         log(L_ERR, "%s[%d]: syntax error", file, lineno);
72                         continue;
73                 }
74                 (void)getword(&p, nastype, sizeof(nastype));
75
76                 if ((c = malloc(sizeof(NAS))) == NULL) {
77                         log(L_CONS|L_ERR, "%s[%d]: out of memory",
78                                 file, lineno);
79                         return -1;
80                 }
81
82                 c->ipaddr = ip_getaddr(hostnm);
83                 strcpy(c->nastype, nastype);
84                 strcpy(c->shortname, shortnm);
85                 strcpy(c->longname, ip_hostname(c->ipaddr));
86
87                 c->next = naslist;
88                 naslist = c;
89         }
90         fclose(fp);
91
92         return 0;
93 }
94
95
96 /*
97  *      Find a nas in the NAS list.
98  */
99 NAS *nas_find(UINT4 ipaddr)
100 {
101         NAS *cl;
102
103         for (cl = naslist; cl; cl = cl->next)
104                 if (ipaddr == cl->ipaddr)
105                         break;
106
107         return cl;
108 }
109
110
111 /*
112  *      Find a nas by name.
113  */
114 NAS *nas_findbyname(char *nasname)
115 {
116         NAS     *nas;
117
118         for (nas = naslist; nas; nas = nas->next) {
119                 if (strcmp(nasname, nas->shortname) == 0 ||
120                     strcmp(nasname, nas->longname) == 0)
121                         break;
122         }
123
124         return nas;
125 }
126
127
128 /*
129  *      Find the name of a nas (prefer short name).
130  */
131 char *nas_name(UINT4 ipaddr)
132 {
133         NAS *cl;
134
135         if ((cl = nas_find(ipaddr)) != NULL) {
136                 if (cl->shortname[0])
137                         return cl->shortname;
138                 else
139                         return cl->longname;
140         }
141         return ip_hostname(ipaddr);
142 }
143
144 /*
145  *      Find the name of a nas (prefer short name) based on the request.
146  */
147 char *nas_name2(RADIUS_PACKET *packet)
148 {
149         UINT4   ipaddr;
150         NAS     *cl;
151         VALUE_PAIR      *pair;
152
153         if ((pair = pairfind(packet->vps, PW_NAS_IP_ADDRESS)) != NULL)
154                 ipaddr = pair->lvalue;
155         else
156                 ipaddr = packet->src_ipaddr;
157
158         if ((cl = nas_find(ipaddr)) != NULL) {
159                 if (cl->shortname[0])
160                         return cl->shortname;
161                 else
162                         return cl->longname;
163         }
164         return ip_hostname(ipaddr);
165 }
166