dbconf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include <string.h>
  13. /* This was malloc.h - but it's moved to stdlib.h on most platforms, and FBSD is strict */
  14. /* Make it stdlib.h, and revert to malloc.h with ifdefs if we have issues here. WB 2016 */
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. #include <ldaputil/errors.h>
  18. #include <ldaputil/certmap.h>
  19. #include <ldaputil/encode.h>
  20. #include <ldaputil/dbconf.h>
  21. #define BIG_LINE 1024
  22. static const char *DB_DIRECTIVE = "directory";
  23. static const int DB_DIRECTIVE_LEN = 9; /* strlen("DB_DIRECTIVE") */
  24. static const char *ENCODED = "encoded";
  25. static void insert_dbinfo_propval(DBConfDBInfo_t *db_info,
  26. DBPropVal_t *propval)
  27. {
  28. if (db_info->lastprop) {
  29. db_info->lastprop->next = propval;
  30. }
  31. else {
  32. db_info->firstprop = propval;
  33. }
  34. db_info->lastprop = propval;
  35. }
  36. static void insert_dbconf_dbinfo(DBConfInfo_t *conf_info,
  37. DBConfDBInfo_t *db_info)
  38. {
  39. if (conf_info->lastdb) {
  40. conf_info->lastdb->next = db_info;
  41. }
  42. else {
  43. conf_info->firstdb = db_info;
  44. }
  45. conf_info->lastdb = db_info;
  46. }
  47. void dbconf_free_propval (DBPropVal_t *propval)
  48. {
  49. if (propval) {
  50. if (propval->prop) free(propval->prop);
  51. if (propval->val) free(propval->val);
  52. memset((void *)propval, 0, sizeof(DBPropVal_t));
  53. free(propval);
  54. }
  55. }
  56. NSAPI_PUBLIC void dbconf_free_dbinfo (DBConfDBInfo_t *db_info)
  57. {
  58. if (db_info) {
  59. DBPropVal_t *next;
  60. DBPropVal_t *cur;
  61. if (db_info->dbname) free(db_info->dbname);
  62. if (db_info->url) free(db_info->url);
  63. cur = db_info->firstprop;
  64. while(cur) {
  65. next = cur->next;
  66. dbconf_free_propval(cur);
  67. cur = next;
  68. }
  69. memset((void *)db_info, 0, sizeof(DBConfDBInfo_t));
  70. free(db_info);
  71. }
  72. }
  73. NSAPI_PUBLIC void dbconf_free_confinfo (DBConfInfo_t *conf_info)
  74. {
  75. DBConfDBInfo_t *next;
  76. DBConfDBInfo_t *cur;
  77. if (conf_info) {
  78. cur = conf_info->firstdb;
  79. while (cur) {
  80. next = cur->next;
  81. dbconf_free_dbinfo(cur);
  82. cur = next;
  83. }
  84. memset((void *)conf_info, 0, sizeof(DBConfInfo_t));
  85. free(conf_info);
  86. }
  87. }
  88. static int skip_blank_lines_and_spaces(FILE *fp, char *buf, char **ptr_out,
  89. int *eof)
  90. {
  91. char *ptr = buf;
  92. char *end;
  93. while(buf && (*buf || fgets(buf, BIG_LINE, fp))) {
  94. ptr = buf;
  95. /* skip leading whitespace */
  96. while(*ptr && isspace(*ptr)) ++ptr;
  97. /* skip blank line or comment */
  98. if (!*ptr || *ptr == '#') {
  99. *buf = 0; /* to force reading of next line */
  100. continue;
  101. }
  102. /* Non-blank line found */
  103. break;
  104. }
  105. *ptr_out = ptr;
  106. if (!*ptr) {
  107. *eof = 1;
  108. }
  109. else {
  110. /* skip trailing whitespace */
  111. end = ptr + strlen(ptr) - 1;
  112. while(isspace(*end)) *end-- = 0;
  113. }
  114. return LDAPU_SUCCESS;
  115. }
  116. static int dbconf_parse_propval (char *buf, char *ptr,
  117. DBConfDBInfo_t *db_info)
  118. {
  119. char *dbname = db_info->dbname;
  120. int dbname_len = strlen(dbname);
  121. char *prop;
  122. char *val;
  123. DBPropVal_t *propval;
  124. char *delimeter_chars = " \t";
  125. char *lastchar;
  126. int end_of_prop;
  127. char *encval = 0; /* encoded value */
  128. char *origprop = 0;
  129. if ((ptr - buf + dbname_len > BIG_LINE) ||
  130. strncmp(ptr, dbname, dbname_len) ||
  131. !(ptr[dbname_len] == ':' || isspace(ptr[dbname_len])))
  132. {
  133. /* Not a prop-val for the current db but not an error */
  134. return LDAPU_ERR_NOT_PROPVAL;
  135. }
  136. /* remove the last char if it is newline */
  137. lastchar = strrchr(buf, '\n');
  138. if (lastchar) *lastchar = '\0';
  139. prop = ptr + dbname_len + 1;
  140. while(*prop && (isspace(*prop) || *prop == ':')) ++prop;
  141. if (!*prop) {
  142. return LDAPU_ERR_PROP_IS_MISSING;
  143. }
  144. end_of_prop = strcspn(prop, delimeter_chars);
  145. if (prop[end_of_prop] != '\0') {
  146. /* buf doesn't end here -- val is present */
  147. prop[end_of_prop] = '\0';
  148. val = &prop[end_of_prop + 1];
  149. while(*val && isspace(*val)) ++val;
  150. if (*val == '\0') val = 0;
  151. }
  152. else {
  153. val = 0;
  154. }
  155. /*
  156. * The prop-val line could be one of the following:
  157. * "<dbname>:prop val" OR "<dbname>:encoded prop encval"
  158. * If (prop == "encoded") then the val has "prop encval".
  159. * Get the actual prop from val and get encval (i.e. encoded value)
  160. * and decode it. If it is encoded then the val part must be non-NULL.
  161. */
  162. if (val && *val && !strcmp(prop, ENCODED)) {
  163. /* val has the actual prop followed by the encoded value */
  164. origprop = prop;
  165. prop = val;
  166. while(*prop && (isspace(*prop) || *prop == ':')) ++prop;
  167. if (!*prop) {
  168. return LDAPU_ERR_PROP_IS_MISSING;
  169. }
  170. end_of_prop = strcspn(prop, delimeter_chars);
  171. if (prop[end_of_prop] != '\0') {
  172. /* buf doesn't end here -- encval is present */
  173. prop[end_of_prop] = '\0';
  174. encval = &prop[end_of_prop + 1];
  175. while(*encval && isspace(*encval)) ++encval;
  176. if (*encval == '\0') encval = 0;
  177. }
  178. else {
  179. encval = 0;
  180. }
  181. if (!encval) {
  182. /* special case - if encval is null, "encoded" itself is a
  183. * property and what we have in prop now is the value. */
  184. val = prop;
  185. prop = origprop;
  186. }
  187. else {
  188. /* decode the value */
  189. val = dbconf_decodeval(encval);
  190. }
  191. }
  192. /* Success - we have prop & val */
  193. propval = (DBPropVal_t *)malloc(sizeof(DBPropVal_t));
  194. if (!propval){
  195. if (encval && val) free(val);
  196. return LDAPU_ERR_OUT_OF_MEMORY;
  197. }
  198. memset((void *)propval, 0, sizeof(DBPropVal_t));
  199. propval->prop = strdup(prop);
  200. propval->val = val ? strdup(val) : 0;
  201. if (encval && val) free(val); /* val was allocated by dbconf_decodeval */
  202. if (!propval->prop || (val && !propval->val)) {
  203. dbconf_free_propval(propval);
  204. return LDAPU_ERR_OUT_OF_MEMORY;
  205. }
  206. insert_dbinfo_propval(db_info, propval);
  207. return LDAPU_SUCCESS;
  208. }
  209. static int dbconf_read_propval (FILE *fp, char *buf, DBConfDBInfo_t *db_info,
  210. int *eof)
  211. {
  212. int rv;
  213. char *ptr = buf;
  214. while(buf && (*buf || fgets(buf, BIG_LINE, fp))) {
  215. ptr = buf;
  216. rv = skip_blank_lines_and_spaces(fp, buf, &ptr, eof);
  217. if (rv != LDAPU_SUCCESS || *eof) return rv;
  218. /* We have a non-blank line which could be prop-val pair for the
  219. * dbname in the db_info. parse the prop-val pair and continue.
  220. */
  221. rv = dbconf_parse_propval(buf, ptr, db_info);
  222. if (rv == LDAPU_ERR_NOT_PROPVAL) return LDAPU_SUCCESS;
  223. if (rv != LDAPU_SUCCESS) return rv;
  224. *buf = 0; /* to force reading of next line */
  225. }
  226. if (!*buf) *eof = 1;
  227. return LDAPU_SUCCESS;
  228. }
  229. static int parse_directive(char *buf, const char *directive,
  230. const int directive_len,
  231. DBConfDBInfo_t **db_info_out)
  232. {
  233. DBConfDBInfo_t *db_info;
  234. char *dbname;
  235. char *url;
  236. int end_of_dbname;
  237. char *delimeter_chars = " \t";
  238. char *lastchar;
  239. /* remove the last char if it is newline */
  240. lastchar = strrchr(buf, '\n');
  241. if (lastchar) *lastchar = '\0';
  242. if (strncmp(buf, directive, directive_len) ||
  243. !isspace(buf[directive_len]))
  244. {
  245. return LDAPU_ERR_DIRECTIVE_IS_MISSING;
  246. }
  247. dbname = buf + directive_len + 1;
  248. while(*dbname && isspace(*dbname)) ++dbname;
  249. if (!*dbname) {
  250. return LDAPU_ERR_DBNAME_IS_MISSING;
  251. }
  252. end_of_dbname = strcspn(dbname, delimeter_chars);
  253. if (dbname[end_of_dbname] != '\0') {
  254. /* buf doesn't end here -- url is present */
  255. dbname[end_of_dbname] = '\0';
  256. url = &dbname[end_of_dbname + 1];
  257. while(*url && isspace(*url)) ++url;
  258. if (*url == '\0') url = 0;
  259. }
  260. else {
  261. url = 0;
  262. }
  263. /* Success - we have dbname & url */
  264. db_info = (DBConfDBInfo_t *)malloc(sizeof(DBConfDBInfo_t));
  265. if (!db_info) return LDAPU_ERR_OUT_OF_MEMORY;
  266. memset((void *)db_info, 0, sizeof(DBConfDBInfo_t));
  267. db_info->dbname = strdup(dbname);
  268. db_info->url = url ? strdup(url) : 0;
  269. if (!db_info->dbname || (url && !db_info->url)) {
  270. dbconf_free_dbinfo(db_info);
  271. return LDAPU_ERR_OUT_OF_MEMORY;
  272. }
  273. *db_info_out = db_info;
  274. return LDAPU_SUCCESS;
  275. }
  276. /* Read the next database info from the file and put it in db_info_out. The
  277. * buf may contain first line of the database info. When this function
  278. * finishes, the buf may contain unprocessed information (which should be
  279. * passed to the next call to read_db_info).
  280. */
  281. static int read_db_info (FILE *fp, char *buf, DBConfDBInfo_t **db_info_out,
  282. const char *directive, const int directive_len,
  283. int *eof)
  284. {
  285. char *ptr;
  286. DBConfDBInfo_t *db_info;
  287. int rv;
  288. *db_info_out = 0;
  289. rv = skip_blank_lines_and_spaces(fp, buf, &ptr, eof);
  290. if (rv != LDAPU_SUCCESS || *eof) return rv;
  291. /* We possibly have a directive of the form "directory <name> <url>" */
  292. rv = parse_directive(ptr, directive, directive_len, &db_info);
  293. if (rv != LDAPU_SUCCESS) return rv;
  294. /* We have parsed the directive successfully -- lets look for additional
  295. * property-value pairs for the database.
  296. */
  297. if (!fgets(buf, BIG_LINE, fp)) {
  298. *eof = 1;
  299. rv = LDAPU_SUCCESS;
  300. }
  301. else {
  302. rv = dbconf_read_propval(fp, buf, db_info, eof);
  303. }
  304. if (rv != LDAPU_SUCCESS) {
  305. dbconf_free_dbinfo(db_info);
  306. *db_info_out = 0;
  307. }
  308. else {
  309. *db_info_out = db_info;
  310. }
  311. return rv;
  312. }
  313. int dbconf_read_config_file_sub (const char *file,
  314. const char *directive,
  315. const int directive_len,
  316. DBConfInfo_t **conf_info_out)
  317. {
  318. FILE *fp;
  319. DBConfInfo_t *conf_info;
  320. DBConfDBInfo_t *db_info;
  321. char buf[BIG_LINE];
  322. int rv;
  323. int eof;
  324. buf[0] = 0;
  325. if ((fp = fopen(file, "r")) == NULL)
  326. {
  327. return LDAPU_ERR_CANNOT_OPEN_FILE;
  328. }
  329. /* Allocate DBConfInfo_t */
  330. conf_info = (DBConfInfo_t *)malloc(sizeof(DBConfInfo_t));
  331. if (!conf_info) {
  332. fclose(fp);
  333. return LDAPU_ERR_OUT_OF_MEMORY;
  334. }
  335. memset((void *)conf_info, 0, sizeof(DBConfInfo_t));
  336. /* Read each db info */
  337. eof = 0;
  338. while(!eof &&
  339. ((rv = read_db_info(fp, buf, &db_info, directive, directive_len, &eof)) == LDAPU_SUCCESS))
  340. {
  341. insert_dbconf_dbinfo(conf_info, db_info);
  342. }
  343. if (rv != LDAPU_SUCCESS) {
  344. dbconf_free_confinfo(conf_info);
  345. *conf_info_out = 0;
  346. }
  347. else {
  348. *conf_info_out = conf_info;
  349. }
  350. fclose(fp);
  351. return rv;
  352. }
  353. NSAPI_PUBLIC int dbconf_read_config_file (const char *file, DBConfInfo_t **conf_info_out)
  354. {
  355. return dbconf_read_config_file_sub(file, DB_DIRECTIVE, DB_DIRECTIVE_LEN,
  356. conf_info_out);
  357. }
  358. int dbconf_read_default_dbinfo_sub (const char *file,
  359. const char *directive,
  360. const int directive_len,
  361. DBConfDBInfo_t **db_info_out)
  362. {
  363. FILE *fp;
  364. DBConfDBInfo_t *db_info;
  365. char buf[BIG_LINE];
  366. int rv;
  367. int eof;
  368. buf[0] = 0;
  369. if ((fp = fopen(file, "r")) == NULL)
  370. {
  371. return LDAPU_ERR_CANNOT_OPEN_FILE;
  372. }
  373. /* Read each db info until eof or dbname == default*/
  374. eof = 0;
  375. while(!eof &&
  376. ((rv = read_db_info(fp, buf, &db_info, directive, directive_len, &eof)) == LDAPU_SUCCESS))
  377. {
  378. if (!strcmp(db_info->dbname, DBCONF_DEFAULT_DBNAME)) break;
  379. dbconf_free_dbinfo(db_info);
  380. db_info = NULL;
  381. }
  382. if (rv != LDAPU_SUCCESS) {
  383. *db_info_out = 0;
  384. }
  385. else {
  386. *db_info_out = db_info;
  387. }
  388. fclose(fp);
  389. return rv;
  390. }
  391. NSAPI_PUBLIC int dbconf_read_default_dbinfo (const char *file,
  392. DBConfDBInfo_t **db_info_out)
  393. {
  394. return dbconf_read_default_dbinfo_sub(file, DB_DIRECTIVE, DB_DIRECTIVE_LEN,
  395. db_info_out);
  396. }
  397. /*
  398. * ldapu_strcasecmp - is like strcasecmp on UNIX but also accepts null strings.
  399. */
  400. int ldapu_strcasecmp (const char *s1, const char *s2)
  401. {
  402. if (!s1) return !s2 ? 0 : 0-tolower(*s2);
  403. else if (!s2) return tolower(*s1);
  404. return strcasecmp(s1, s2);
  405. }
  406. NSAPI_PUBLIC int ldapu_dbinfo_attrval (DBConfDBInfo_t *db_info,
  407. const char *attr, char **val)
  408. {
  409. /* Look for given attr in the db_info and return its value */
  410. int rv = LDAPU_ATTR_NOT_FOUND;
  411. DBPropVal_t *next;
  412. *val = 0;
  413. if (db_info) {
  414. next = db_info->firstprop;
  415. while (next) {
  416. rv = ldapu_strcasecmp(attr, next->prop);
  417. if (!rv) {
  418. /* Found the property */
  419. *val = next->val ? strdup(next->val) : 0;
  420. if (next->val && !*val) {
  421. rv = LDAPU_ERR_OUT_OF_MEMORY;
  422. }
  423. else {
  424. rv = LDAPU_SUCCESS;
  425. }
  426. break;
  427. }
  428. next = next->next;
  429. }
  430. }
  431. return rv;
  432. }
  433. void dbconf_print_propval (DBPropVal_t *propval)
  434. {
  435. if (propval) {
  436. fprintf(stderr, "\tprop: \"%s\"\tval: \"%s\"\n", propval->prop,
  437. propval->val ? propval->val : "");
  438. }
  439. else {
  440. fprintf(stderr, "Null propval\n");
  441. }
  442. }
  443. void dbconf_print_dbinfo (DBConfDBInfo_t *db_info)
  444. {
  445. DBPropVal_t *next;
  446. if (db_info) {
  447. fprintf(stderr, "dbname: \"%s\"\n", db_info->dbname);
  448. fprintf(stderr, "url: \t\"%s\"\n", db_info->url ? db_info->url : "");
  449. next = db_info->firstprop;
  450. while (next) {
  451. dbconf_print_propval(next);
  452. next = next->next;
  453. }
  454. }
  455. else {
  456. fprintf(stderr, "Null db_info\n");
  457. }
  458. }
  459. void dbconf_print_confinfo (DBConfInfo_t *conf_info)
  460. {
  461. DBConfDBInfo_t *next;
  462. if (conf_info) {
  463. next = conf_info->firstdb;
  464. while (next) {
  465. dbconf_print_dbinfo(next);
  466. next = next->next;
  467. }
  468. }
  469. else {
  470. fprintf(stderr, "Null conf_info\n");
  471. }
  472. }
  473. NSAPI_PUBLIC int dbconf_output_db_directive (FILE *fp, const char *dbname,
  474. const char *url)
  475. {
  476. fprintf(fp, "%s %s %s\n", DB_DIRECTIVE, dbname, url);
  477. return LDAPU_SUCCESS;
  478. }
  479. NSAPI_PUBLIC int dbconf_output_propval (FILE *fp, const char *dbname,
  480. const char *prop, const char *val, const int encoded)
  481. {
  482. if (encoded && val && *val) {
  483. char *new_val = dbconf_encodeval(val);
  484. if (!new_val) return LDAPU_ERR_OUT_OF_MEMORY;
  485. fprintf(fp, "%s:%s %s %s\n", dbname, ENCODED,
  486. prop, new_val);
  487. free(new_val);
  488. }
  489. else {
  490. fprintf(fp, "%s:%s %s\n", dbname, prop, val ? val : "");
  491. }
  492. return LDAPU_SUCCESS;
  493. }
  494. NSAPI_PUBLIC int dbconf_get_dbnames (const char *dbmap, char ***dbnames_out, int *cnt_out)
  495. {
  496. DBConfInfo_t *conf_info = 0;
  497. DBConfDBInfo_t *db = 0;
  498. int cnt = 0;
  499. char **dbnames = 0;
  500. char *heap = 0;
  501. int rv;
  502. *dbnames_out = 0;
  503. *cnt_out = 0;
  504. rv = dbconf_read_config_file(dbmap, &conf_info);
  505. if (rv != LDAPU_SUCCESS) return rv;
  506. db = conf_info->firstdb;
  507. dbnames = (char **)malloc(32*1024);
  508. heap = (char *)dbnames + 2*1024;
  509. if (!dbnames) {
  510. dbconf_free_confinfo(conf_info);
  511. return LDAPU_ERR_OUT_OF_MEMORY;
  512. }
  513. *dbnames_out = dbnames;
  514. while(db) {
  515. *dbnames++ = heap;
  516. strcpy(heap, db->dbname);
  517. heap += strlen(db->dbname)+1;
  518. db = db->next;
  519. cnt++;
  520. }
  521. *dbnames = NULL;
  522. *cnt_out = cnt;
  523. dbconf_free_confinfo(conf_info);
  524. return LDAPU_SUCCESS;
  525. }
  526. NSAPI_PUBLIC int dbconf_free_dbnames (char **dbnames)
  527. {
  528. if (dbnames)
  529. free(dbnames);
  530. return LDAPU_SUCCESS;
  531. }