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