dict.c 12 KB

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