clientid.c 2.1 KB

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