1 /*
2  * $Id: clientid.c,v 1.7 2007/07/11 17:29:29 cparker Exp $
3  *
4  * Copyright (C) 1995,1996,1997 Lars Fenneberg
5  *
6  * See the file COPYRIGHT for the respective terms and conditions.
7  * If the file is missing contact me at lf@elemental.net
8  * and I'll send you a copy.
9  *
10  */
11 
12 #include <config.h>
13 #include <includes.h>
14 #include <freeradius-client.h>
15 
16 struct map2id_s {
17 	char *name;
18 	uint32_t id;
19 
20 	struct map2id_s *next;
21 };
22 
23 /*
24  * Function: rc_read_mapfile
25  *
26  * Purpose: Read in the ttyname to port id map file
27  *
28  * Arguments: the file name of the map file
29  *
30  * Returns: zero on success, negative integer on failure
31  */
32 
rc_read_mapfile(rc_handle * rh,char const * filename)33 int rc_read_mapfile(rc_handle *rh, char const *filename)
34 {
35 	char buffer[1024];
36 	FILE *mapfd;
37 	char *c, *name, *id, *q;
38 	struct map2id_s *p;
39 	int lnr = 0;
40 
41         if ((mapfd = fopen(filename,"r")) == NULL)
42         {
43 		rc_log(LOG_ERR,"rc_read_mapfile: can't read %s: %s", filename, strerror(errno));
44 		return -1;
45 	}
46 
47 #define SKIP(p) while(*p && isspace(*p)) p++;
48 
49         while (fgets(buffer, sizeof(buffer), mapfd) != NULL)
50         {
51         	lnr++;
52 
53 		q = buffer;
54 
55                 SKIP(q);
56 
57                 if ((*q == '\n') || (*q == '#') || (*q == '\0'))
58 			continue;
59 
60 		if (( c = strchr(q, ' ')) || (c = strchr(q,'\t'))) {
61 
62 			*c = '\0'; c++;
63 			SKIP(c);
64 
65 			name = q;
66 			id = c;
67 
68 			if ((p = (struct map2id_s *)malloc(sizeof(*p))) == NULL) {
69 				rc_log(LOG_CRIT,"rc_read_mapfile: out of memory");
70 				fclose(mapfd);
71 				return -1;
72 			}
73 
74 			p->name = strdup(name);
75 			p->id = atoi(id);
76 			p->next = rh->map2id_list;
77 			rh->map2id_list = p;
78 
79 		} else {
80 
81 			rc_log(LOG_ERR, "rc_read_mapfile: malformed line in %s, line %d", filename, lnr);
82 			fclose(mapfd);
83 			return -1;
84 
85 		}
86 	}
87 
88 #undef SKIP
89 
90 	fclose(mapfd);
91 
92 	return 0;
93 }
94 
95 /*
96  * Function: rc_map2id
97  *
98  * Purpose: Map ttyname to port id
99  *
100  * Arguments: full pathname of the tty
101  *
102  * Returns: port id, zero if no entry found
103  */
104 
rc_map2id(rc_handle const * rh,char const * name)105 uint32_t rc_map2id(rc_handle const *rh, char const *name)
106 {
107 	struct map2id_s *p;
108 	char ttyname[PATH_MAX];
109 
110 	*ttyname = '\0';
111 	if (*name != '/')
112 		strcpy(ttyname, "/dev/");
113 
114 	strncat(ttyname, name, sizeof(ttyname)-strlen(ttyname)-1);
115 
116 	for(p = rh->map2id_list; p; p = p->next)
117 		if (!strcmp(ttyname, p->name)) return p->id;
118 
119 	rc_log(LOG_WARNING,"rc_map2id: can't find tty %s in map database", ttyname);
120 
121 	return 0;
122 }
123 
124 /*
125  * Function: rc_map2id_free
126  *
127  * Purpose: Free allocated map2id list
128  *
129  * Arguments: Radius Client handle
130  */
131 
132 void
rc_map2id_free(rc_handle * rh)133 rc_map2id_free(rc_handle *rh)
134 {
135 	struct map2id_s *p, *np;
136 
137 	if (rh->map2id_list == NULL)
138 		return;
139 
140 	for(p = rh->map2id_list; p != NULL; p = np) {
141 		np = p->next;
142 		free(p->name);
143 		free(p);
144 	}
145 	rh->map2id_list = NULL;
146 }
147