dict.c 12 KB

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