1/*
2 * $Id: clientid.c,v 1.1.1.1 2008/10/15 03:30:50 james26_jang 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 <radiusclient.h>
15
16struct map2id_s {
17	char *name;
18	UINT4 id;
19
20	struct map2id_s *next;
21};
22
23static struct map2id_s *map2id_list = NULL;
24
25/*
26 * Function: rc_read_mapfile
27 *
28 * Purpose: Read in the ttyname to port id map file
29 *
30 * Arguments: the file name of the map file
31 *
32 * Returns: zero on success, negative integer on failure
33 */
34
35int rc_read_mapfile(char *filename)
36{
37	char buffer[1024];
38	FILE *mapfd;
39	char *c, *name, *id, *q;
40	struct map2id_s *p;
41	int lnr = 0;
42
43	if ((mapfd = fopen(filename,"r")) == NULL)
44	{
45		rc_log(LOG_ERR,"rc_read_mapfile: can't read %s: %s", filename, strerror(errno));
46		return (-1);
47	}
48
49#define SKIP(p) while(*p && isspace(*p)) p++;
50
51	while (fgets(buffer, sizeof(buffer), mapfd) != NULL)
52	{
53		lnr++;
54
55		q = buffer;
56
57		SKIP(q);
58
59		if ((*q == '\n') || (*q == '#') || (*q == '\0'))
60			continue;
61
62		if (( c = strchr(q, ' ')) || (c = strchr(q,'\t'))) {
63
64			*c = '\0'; c++;
65			SKIP(c);
66
67			name = q;
68			id = c;
69
70			if ((p = (struct map2id_s *)malloc(sizeof(*p))) == NULL) {
71				rc_log(LOG_CRIT,"rc_read_mapfile: out of memory");
72				return (-1);
73			}
74
75			p->name = strdup(name);
76			p->id = atoi(id);
77			p->next = map2id_list;
78			map2id_list = p;
79
80		} else {
81
82			rc_log(LOG_ERR, "rc_read_mapfile: malformed line in %s, line %d", filename, lnr);
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
105UINT4 rc_map2id(char *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));
115
116	for(p = 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