dict.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * $Id: dict.c,v 1.10 2007/07/11 17:29:29 cparker Exp $
  3. *
  4. * Copyright (C) 1995,1996,1997 Lars Fenneberg
  5. *
  6. * Copyright 1992 Livingston Enterprises, Inc.
  7. *
  8. * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
  9. * and Merit Network, Inc. All Rights Reserved
  10. *
  11. * See the file COPYRIGHT for the respective terms and conditions.
  12. * If the file is missing contact me at lf@elemental.net
  13. * and I'll send you a copy.
  14. *
  15. */
  16. #include <radius_config.h>
  17. #include <includes.h>
  18. #include <freeradius-client.h>
  19. #if defined ( __GNUC__ )
  20. #include <my_strings.h>
  21. #endif
  22. /** Initialize the dictionary
  23. *
  24. * Read all ATTRIBUTES into the dictionary_attributes list.
  25. * Read all VALUES into the dictionary_values list.
  26. *
  27. * @param rh a handle to parsed configuration.
  28. * @param filename the name of the dictionary file.
  29. * @return 0 on success, -1 on failure.
  30. */
  31. int rc_read_dictionary (rc_handle *rh, char const *filename)
  32. {
  33. #if 0
  34. FILE *dictfd;
  35. char dummystr[AUTH_ID_LEN];
  36. char namestr[AUTH_ID_LEN];
  37. char valstr[AUTH_ID_LEN];
  38. char attrstr[AUTH_ID_LEN];
  39. char typestr[AUTH_ID_LEN];
  40. char optstr[AUTH_ID_LEN];
  41. char *cp, *ifilename;
  42. int line_no;
  43. DICT_ATTR *attr;
  44. DICT_VALUE *dval;
  45. DICT_VENDOR *dvend;
  46. char buffer[256];
  47. int value;
  48. int type;
  49. unsigned attr_vendorspec = 0;
  50. if ((dictfd = fopen (filename, "r")) == NULL)
  51. {
  52. rc_log(LOG_ERR, "rc_read_dictionary couldn't open dictionary %s: %s",
  53. filename, strerror(errno));
  54. return -1;
  55. }
  56. line_no = 0;
  57. while (fgets (buffer, sizeof (buffer), dictfd) != NULL)
  58. {
  59. line_no++;
  60. /* Skip empty space */
  61. if (*buffer == '#' || *buffer == '\0' || *buffer == '\n' || \
  62. *buffer == '\r')
  63. {
  64. continue;
  65. }
  66. /* Strip out comments */
  67. cp = strchr(buffer, '#');
  68. if (cp != NULL)
  69. {
  70. *cp = '\0';
  71. }
  72. if (strncmp (buffer, "ATTRIBUTE", 9) == 0)
  73. {
  74. optstr[0] = '\0';
  75. /* Read the ATTRIBUTE line */
  76. if (sscanf (buffer, "%63s%63s%63s%63s%63s", dummystr, namestr,
  77. valstr, typestr, optstr) < 4)
  78. {
  79. rc_log(LOG_ERR, "rc_read_dictionary: invalid attribute on line %d of dictionary %s",
  80. line_no, filename);
  81. fclose(dictfd);
  82. return -1;
  83. }
  84. /*
  85. * Validate all entries
  86. */
  87. if (strlen (namestr) > NAME_LENGTH)
  88. {
  89. rc_log(LOG_ERR, "rc_read_dictionary: invalid name length on line %d of dictionary %s",
  90. line_no, filename);
  91. fclose(dictfd);
  92. return -1;
  93. }
  94. if (!isdigit (*valstr))
  95. {
  96. rc_log(LOG_ERR,
  97. "rc_read_dictionary: invalid value on line %d of dictionary %s",
  98. line_no, filename);
  99. fclose(dictfd);
  100. return -1;
  101. }
  102. value = atoi (valstr);
  103. if (strcmp (typestr, "string") == 0)
  104. {
  105. type = PW_TYPE_STRING;
  106. }
  107. else if (strcmp (typestr, "integer") == 0)
  108. {
  109. type = PW_TYPE_INTEGER;
  110. }
  111. else if (strcmp (typestr, "ipaddr") == 0)
  112. {
  113. type = PW_TYPE_IPADDR;
  114. }
  115. else if (strcmp (typestr, "ipv6addr") == 0)
  116. {
  117. type = PW_TYPE_IPV6ADDR;
  118. }
  119. else if (strcmp (typestr, "ipv6prefix") == 0)
  120. {
  121. type = PW_TYPE_IPV6PREFIX;
  122. }
  123. else if (strcmp (typestr, "date") == 0)
  124. {
  125. type = PW_TYPE_DATE;
  126. }
  127. else
  128. {
  129. rc_log(LOG_ERR,
  130. "rc_read_dictionary: invalid type on line %d of dictionary %s",
  131. line_no, filename);
  132. fclose(dictfd);
  133. return -1;
  134. }
  135. dvend = NULL;
  136. if (optstr[0] != '\0') {
  137. char *cp1;
  138. for (cp1 = optstr; cp1 != NULL; cp1 = cp) {
  139. cp = strchr(cp1, ',');
  140. if (cp != NULL) {
  141. *cp = '\0';
  142. cp++;
  143. }
  144. if (strncmp(cp1, "vendor=", 7) == 0)
  145. cp1 += 7;
  146. dvend = rc_dict_findvend(rh, cp1);
  147. if (dvend == NULL) {
  148. rc_log(LOG_ERR,
  149. "rc_read_dictionary: unknown Vendor-Id %s on line %d of dictionary %s",
  150. cp1, line_no, filename);
  151. fclose(dictfd);
  152. return -1;
  153. }
  154. }
  155. }
  156. /* Create a new attribute for the list */
  157. if ((attr = malloc (sizeof (DICT_ATTR))) == NULL)
  158. {
  159. rc_log(LOG_CRIT, "rc_read_dictionary: out of memory");
  160. fclose(dictfd);
  161. return -1;
  162. }
  163. strcpy (attr->name, namestr);
  164. attr->value = value | (attr_vendorspec << 16);
  165. attr->type = type;
  166. if (dvend != NULL) {
  167. attr->value = value | (dvend->vendorpec << 16);
  168. } else {
  169. attr->value = value | (attr_vendorspec << 16);
  170. }
  171. /* Insert it into the list */
  172. attr->next = rh->dictionary_attributes;
  173. rh->dictionary_attributes = attr;
  174. }
  175. else if (strncmp (buffer, "VALUE", 5) == 0)
  176. {
  177. /* Read the VALUE line */
  178. if (sscanf (buffer, "%63s%63s%63s%63s", dummystr, attrstr,
  179. namestr, valstr) != 4)
  180. {
  181. rc_log(LOG_ERR,
  182. "rc_read_dictionary: invalid value entry on line %d of dictionary %s",
  183. line_no, filename);
  184. fclose(dictfd);
  185. return -1;
  186. }
  187. /*
  188. * Validate all entries
  189. */
  190. if (strlen (attrstr) > NAME_LENGTH)
  191. {
  192. rc_log(LOG_ERR,
  193. "rc_read_dictionary: invalid attribute length on line %d of dictionary %s",
  194. line_no, filename);
  195. fclose(dictfd);
  196. return -1;
  197. }
  198. if (strlen (namestr) > NAME_LENGTH)
  199. {
  200. rc_log(LOG_ERR,
  201. "rc_read_dictionary: invalid name length on line %d of dictionary %s",
  202. line_no, filename);
  203. fclose(dictfd);
  204. return -1;
  205. }
  206. if (!isdigit (*valstr))
  207. {
  208. rc_log(LOG_ERR,
  209. "rc_read_dictionary: invalid value on line %d of dictionary %s",
  210. line_no, filename);
  211. fclose(dictfd);
  212. return -1;
  213. }
  214. value = atoi (valstr);
  215. /* Create a new VALUE entry for the list */
  216. if ((dval = malloc (sizeof (DICT_VALUE))) == NULL)
  217. {
  218. rc_log(LOG_CRIT, "rc_read_dictionary: out of memory");
  219. fclose(dictfd);
  220. return -1;
  221. }
  222. strcpy (dval->attrname, attrstr);
  223. strcpy (dval->name, namestr);
  224. dval->value = value;
  225. /* Insert it into the list */
  226. dval->next = rh->dictionary_values;
  227. rh->dictionary_values = dval;
  228. }
  229. else if (strncmp (buffer, "$INCLUDE", 8) == 0)
  230. {
  231. /* Read the $INCLUDE line */
  232. if (sscanf (buffer, "%63s%63s", dummystr, namestr) != 2)
  233. {
  234. rc_log(LOG_ERR,
  235. "rc_read_dictionary: invalid include entry on line %d of dictionary %s",
  236. line_no, filename);
  237. fclose(dictfd);
  238. return -1;
  239. }
  240. ifilename = namestr;
  241. /* Append directory if necessary */
  242. if (namestr[0] != '/') {
  243. cp = strrchr(filename, '/');
  244. if (cp != NULL) {
  245. ifilename = malloc(AUTH_ID_LEN);
  246. *cp = '\0';
  247. snprintf(ifilename, AUTH_ID_LEN, "%s/%s", filename, namestr);
  248. *cp = '/';
  249. }
  250. }
  251. if (rc_read_dictionary(rh, ifilename) < 0)
  252. {
  253. fclose(dictfd);
  254. return -1;
  255. }
  256. }
  257. else if (strncmp (buffer, "END-VENDOR", 10) == 0)
  258. {
  259. attr_vendorspec = 0;
  260. }
  261. else if (strncmp (buffer, "BEGIN-VENDOR", 12) == 0)
  262. {
  263. DICT_VENDOR *v;
  264. /* Read the vendor name */
  265. if (sscanf (buffer+12, "%63s", dummystr) != 1)
  266. {
  267. rc_log(LOG_ERR,
  268. "rc_read_dictionary: invalid Vendor-Id on line %d of dictionary %s",
  269. line_no, filename);
  270. fclose(dictfd);
  271. return -1;
  272. }
  273. v = rc_dict_findvend(rh, dummystr);
  274. if (v == NULL) {
  275. rc_log(LOG_ERR,
  276. "rc_read_dictionary: unknown Vendor %s on line %d of dictionary %s",
  277. dummystr, line_no, filename);
  278. fclose(dictfd);
  279. return -1;
  280. }
  281. attr_vendorspec = v->vendorpec;
  282. }
  283. else if (strncmp (buffer, "VENDOR", 6) == 0)
  284. {
  285. /* Read the VALUE line */
  286. if (sscanf (buffer, "%63s%63s%63s", dummystr, attrstr, valstr) != 3)
  287. {
  288. rc_log(LOG_ERR,
  289. "rc_read_dictionary: invalid Vendor-Id on line %d of dictionary %s",
  290. line_no, filename);
  291. fclose(dictfd);
  292. return -1;
  293. }
  294. /* Validate all entries */
  295. if (strlen (attrstr) > NAME_LENGTH)
  296. {
  297. rc_log(LOG_ERR,
  298. "rc_read_dictionary: invalid attribute length on line %d of dictionary %s",
  299. line_no, filename);
  300. fclose(dictfd);
  301. return -1;
  302. }
  303. if (!isdigit (*valstr))
  304. {
  305. rc_log(LOG_ERR,
  306. "rc_read_dictionary: invalid Vendor-Id on line %d of dictionary %s",
  307. line_no, filename);
  308. fclose(dictfd);
  309. return -1;
  310. }
  311. value = atoi (valstr);
  312. /* Create a new VENDOR entry for the list */
  313. dvend = malloc(sizeof(DICT_VENDOR));
  314. if (dvend == NULL)
  315. {
  316. rc_log(LOG_CRIT, "rc_read_dictionary: out of memory");
  317. fclose(dictfd);
  318. return -1;
  319. }
  320. strcpy (dvend->vendorname, attrstr);
  321. dvend->vendorpec = value;
  322. /* Insert it into the list */
  323. dvend->next = rh->dictionary_vendors;
  324. rh->dictionary_vendors = dvend;
  325. }
  326. }
  327. fclose (dictfd);
  328. #endif
  329. return 0;
  330. }
  331. /** Lookup a DICT_ATTR by attribute number
  332. *
  333. * @param rh a handle to parsed configuration.
  334. * @param attribute the attribute ID.
  335. * @return the full attribute structure based on the attribute id number.
  336. */
  337. DICT_ATTR *rc_dict_getattr(rc_handle const *rh, int attribute)
  338. {
  339. DICT_ATTR *attr;
  340. attr = rh->dictionary_attributes;
  341. while (attr != NULL)
  342. {
  343. if (attr->value == attribute)
  344. {
  345. return attr;
  346. }
  347. attr = attr->next;
  348. }
  349. return NULL;
  350. }
  351. /** Lookup a DICT_ATTR by its name
  352. *
  353. * @param rh a handle to parsed configuration.
  354. * @param attrname the attribute name.
  355. *
  356. * @return the full attribute structure based on the attribute name.
  357. */
  358. DICT_ATTR *rc_dict_findattr(rc_handle const *rh, char const *attrname)
  359. {
  360. DICT_ATTR *attr;
  361. attr = rh->dictionary_attributes;
  362. while (attr != NULL)
  363. {
  364. if (strcasecmp (attr->name, attrname) == 0)
  365. {
  366. return attr;
  367. }
  368. attr = attr->next;
  369. }
  370. return NULL;
  371. }
  372. /** Lookup a DICT_VALUE by its name
  373. *
  374. * @param rh a handle to parsed configuration.
  375. * @param valname the value name.
  376. * @return the full value structure based on the value name.
  377. */
  378. DICT_VALUE *rc_dict_findval(rc_handle const *rh, char const *valname)
  379. {
  380. DICT_VALUE *val;
  381. val = rh->dictionary_values;
  382. while (val != NULL)
  383. {
  384. if (strcasecmp (val->name, valname) == 0)
  385. {
  386. return val;
  387. }
  388. val = val->next;
  389. }
  390. return NULL;
  391. }
  392. /** Lookup a DICT_VENDOR by its name
  393. *
  394. * @param rh a handle to parsed configuration.
  395. * @param valname the vendor name.
  396. * @return the full vendor structure based on the vendor name.
  397. */
  398. DICT_VENDOR *rc_dict_findvend(rc_handle const *rh, char const *vendorname)
  399. {
  400. DICT_VENDOR *vend;
  401. for (vend = rh->dictionary_vendors; vend != NULL; vend = vend->next)
  402. if (strcasecmp(vend->vendorname, vendorname) == 0)
  403. return vend;
  404. return NULL;
  405. }
  406. /** Lookup a DICT_VENDOR by its IANA number
  407. *
  408. * @param rh a handle to parsed configuration.
  409. * @param vendorpec the vendor ID.
  410. * @return the full vendor structure based on the vendor id number.
  411. */
  412. DICT_VENDOR *rc_dict_getvend (rc_handle const *rh, int vendorpec)
  413. {
  414. DICT_VENDOR *vend;
  415. for (vend = rh->dictionary_vendors; vend != NULL; vend = vend->next)
  416. if (vend->vendorpec == vendorpec)
  417. return vend;
  418. return NULL;
  419. }
  420. /** Get DICT_VALUE based on attribute name and integer value number
  421. *
  422. * @param rh a handle to parsed configuration.
  423. * @param value the attribute value.
  424. * @param attrname the attribute name.
  425. * @return the full value structure based on the actual value and the associated attribute name.
  426. */
  427. DICT_VALUE *rc_dict_getval(rc_handle const *rh, uint32_t value, char const *attrname)
  428. {
  429. DICT_VALUE *val;
  430. val = rh->dictionary_values;
  431. while (val != NULL)
  432. {
  433. if (strcmp (val->attrname, attrname) == 0 &&
  434. val->value == value)
  435. {
  436. return val;
  437. }
  438. val = val->next;
  439. }
  440. return NULL;
  441. }
  442. /** Frees the allocated av lists
  443. *
  444. * @param rh a handle to parsed configuration.
  445. */
  446. void rc_dict_free(rc_handle *rh)
  447. {
  448. DICT_ATTR *attr, *nattr;
  449. DICT_VALUE *val, *nval;
  450. DICT_VENDOR *vend, *nvend;
  451. for (attr = rh->dictionary_attributes; attr != NULL; attr = nattr) {
  452. nattr = attr->next;
  453. free(attr);
  454. }
  455. for (val = rh->dictionary_values; val != NULL; val = nval) {
  456. nval = val->next;
  457. free(val);
  458. }
  459. for (vend = rh->dictionary_vendors; vend != NULL; vend = nvend) {
  460. nvend = vend->next;
  461. free(vend);
  462. }
  463. rh->dictionary_attributes = NULL;
  464. rh->dictionary_values = NULL;
  465. rh->dictionary_vendors = NULL;
  466. }