clientid.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include <config.h>
  12. #include <includes.h>
  13. #include <freeradius-client.h>
  14. #include "util.h"
  15. struct map2id_s {
  16. char *name;
  17. uint32_t id;
  18. struct map2id_s *next;
  19. };
  20. /** Read in the ttyname to port id map file
  21. *
  22. * @param rh a handle to parsed configuration.
  23. * @param filename the file name of the map file.
  24. * @return zero on success, negative integer on failure.
  25. */
  26. int rc_read_mapfile(rc_handle *rh, char const *filename)
  27. {
  28. char buffer[1024];
  29. FILE *mapfd;
  30. char *c, *name, *id, *q;
  31. struct map2id_s *p;
  32. int lnr = 0;
  33. if ((mapfd = fopen(filename,"r")) == NULL)
  34. {
  35. rc_log(LOG_ERR,"rc_read_mapfile: can't read %s: %s", filename, strerror(errno));
  36. return -1;
  37. }
  38. #define SKIP(p) while(*p && isspace(*p)) p++;
  39. while (fgets(buffer, sizeof(buffer), mapfd) != NULL)
  40. {
  41. lnr++;
  42. q = buffer;
  43. SKIP(q);
  44. if ((*q == '\n') || (*q == '#') || (*q == '\0'))
  45. continue;
  46. if (( c = strchr(q, ' ')) || (c = strchr(q,'\t'))) {
  47. *c = '\0'; c++;
  48. SKIP(c);
  49. name = q;
  50. id = c;
  51. if ((p = (struct map2id_s *)malloc(sizeof(*p))) == NULL) {
  52. rc_log(LOG_CRIT,"rc_read_mapfile: out of memory");
  53. fclose(mapfd);
  54. return -1;
  55. }
  56. p->name = strdup(name);
  57. p->id = atoi(id);
  58. p->next = rh->map2id_list;
  59. rh->map2id_list = p;
  60. } else {
  61. rc_log(LOG_ERR, "rc_read_mapfile: malformed line in %s, line %d", filename, lnr);
  62. fclose(mapfd);
  63. return -1;
  64. }
  65. }
  66. #undef SKIP
  67. fclose(mapfd);
  68. return 0;
  69. }
  70. /** Maps ttyname to port id
  71. *
  72. * @param rh a handle to parsed configuration.
  73. * @param name full pathname of the tty.
  74. * @return port id, or zero if no entry found.
  75. */
  76. uint32_t rc_map2id(rc_handle const *rh, char const *name)
  77. {
  78. struct map2id_s *p;
  79. char ttyname[PATH_MAX];
  80. unsigned pos = 0;
  81. *ttyname = '\0';
  82. if (*name != '/') {
  83. strcpy(ttyname, "/dev/");
  84. pos = 5;
  85. }
  86. strlcpy(&ttyname[pos], name, sizeof(ttyname)-pos);
  87. for(p = rh->map2id_list; p; p = p->next)
  88. if (!strcmp(ttyname, p->name)) return p->id;
  89. rc_log(LOG_WARNING,"rc_map2id: can't find tty %s in map database", ttyname);
  90. return 0;
  91. }
  92. /** Free allocated map2id list
  93. *
  94. * @param rh a handle to parsed configuration.
  95. */
  96. void rc_map2id_free(rc_handle *rh)
  97. {
  98. struct map2id_s *p, *np;
  99. if (rh->map2id_list == NULL)
  100. return;
  101. for(p = rh->map2id_list; p != NULL; p = np) {
  102. np = p->next;
  103. free(p->name);
  104. free(p);
  105. }
  106. rh->map2id_list = NULL;
  107. }