1/*
2 * $Id: clientid.c,v 1.1 2004/11/14 07:26:26 paulus 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 <includes.h>
13#include <radiusclient.h>
14
15struct map2id_s {
16	char *name;
17	UINT4 id;
18
19	struct map2id_s *next;
20};
21
22static struct map2id_s *map2id_list = NULL;
23
24/*
25 * Function: rc_read_mapfile
26 *
27 * Purpose: Read in the ttyname to port id map file
28 *
29 * Arguments: the file name of the map file
30 *
31 * Returns: zero on success, negative integer on failure
32 */
33
34int rc_read_mapfile(char *filename)
35{
36	char buffer[1024];
37	FILE *mapfd;
38	char *c, *name, *id, *q;
39	struct map2id_s *p;
40	int lnr = 0;
41
42	if ((mapfd = fopen(filename,"r")) == NULL)
43	{
44		error("rc_read_mapfile: can't read %s: %s", filename, strerror(errno));
45		return (-1);
46	}
47
48#define SKIP(p) while(*p && isspace(*p)) p++;
49
50	while (fgets(buffer, sizeof(buffer), mapfd) != NULL)
51	{
52		lnr++;
53
54		q = buffer;
55
56		SKIP(q);
57
58		if ((*q == '\n') || (*q == '#') || (*q == '\0'))
59			continue;
60
61		if (( c = strchr(q, ' ')) || (c = strchr(q,'\t'))) {
62
63			*c = '\0'; c++;
64			SKIP(c);
65
66			name = q;
67			id = c;
68
69			if ((p = (struct map2id_s *)malloc(sizeof(*p))) == NULL) {
70				novm("rc_read_mapfile");
71				return (-1);
72			}
73
74			p->name = strdup(name);
75			p->id = atoi(id);
76			p->next = map2id_list;
77			map2id_list = p;
78
79		} else {
80
81			error("rc_read_mapfile: malformed line in %s, line %d", filename, lnr);
82			return (-1);
83
84		}
85	}
86
87#undef SKIP
88
89	fclose(mapfd);
90
91	return 0;
92}
93
94/*
95 * Function: rc_map2id
96 *
97 * Purpose: Map ttyname to port id
98 *
99 * Arguments: full pathname of the tty
100 *
101 * Returns: port id, zero if no entry found
102 */
103
104UINT4 rc_map2id(char *name)
105{
106	struct map2id_s *p;
107	char ttyname[PATH_MAX];
108
109	*ttyname = '\0';
110	if (*name != '/')
111		strcpy(ttyname, "/dev/");
112
113	strncat(ttyname, name, sizeof(ttyname));
114
115	for(p = map2id_list; p; p = p->next)
116		if (!strcmp(ttyname, p->name)) return p->id;
117
118	warn("rc_map2id: can't find tty %s in map database", ttyname);
119
120	return 0;
121}
122