1
0

misc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5. * All rights reserved.
  6. *
  7. * License: GPL (version 3 or any later version).
  8. * See LICENSE for details.
  9. * END COPYRIGHT BLOCK **/
  10. #ifdef HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. /* misc.c - backend misc routines */
  14. #include "back-ldbm.h"
  15. /* Takes a return code supposed to be errno or from lidb
  16. which we don't expect to see and prints a handy log message */
  17. void ldbm_nasty(const char* str, int c, int err)
  18. {
  19. char *msg = NULL;
  20. char buffer[200];
  21. if (err == DB_LOCK_DEADLOCK) {
  22. PR_snprintf(buffer,200,"%s WARNING %d",str,c);
  23. LDAPDebug(LDAP_DEBUG_BACKLDBM,"%s, err=%d %s\n",
  24. buffer,err,(msg = dblayer_strerror( err )) ? msg : "");
  25. } else if (err == DB_RUNRECOVERY) {
  26. LDAPDebug2Args(LDAP_DEBUG_ANY, "FATAL ERROR at %s (%d); "
  27. "server stopping as database recovery needed.\n", str, c);
  28. exit(1);
  29. } else {
  30. PR_snprintf(buffer,200,"%s BAD %d",str,c);
  31. LDAPDebug(LDAP_DEBUG_ANY, "%s, err=%d %s\n",
  32. buffer, err, (msg = dblayer_strerror( err )) ? msg : "");
  33. }
  34. }
  35. /* Put a message in the access log, complete with connection ID and operation ID */
  36. void ldbm_log_access_message(Slapi_PBlock *pblock,char *string)
  37. {
  38. int ret = 0;
  39. PRUint64 connection_id = 0;
  40. int operation_id = 0;
  41. Operation *operation = NULL; /* DBDB this is sneaky---opid should be covered by the API directly */
  42. ret = slapi_pblock_get(pblock,SLAPI_OPERATION,&operation);
  43. if (0 != ret) {
  44. return;
  45. }
  46. ret = slapi_pblock_get(pblock,SLAPI_CONN_ID,&connection_id);
  47. if (0 != ret) {
  48. return;
  49. }
  50. operation_id = operation->o_opid;
  51. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" NSPRIu64 " op=%d %s\n",
  52. connection_id, operation_id, string);
  53. }
  54. int return_on_disk_full(struct ldbminfo *li)
  55. {
  56. dblayer_remember_disk_filled(li);
  57. return SLAPI_FAIL_DISKFULL;
  58. }
  59. /* System Indexes */
  60. static const char *systemIndexes[] = {
  61. "aci",
  62. LDBM_ENTRYDN_STR,
  63. LDBM_ENTRYRDN_STR,
  64. LDBM_NUMSUBORDINATES_STR,
  65. LDBM_TOMBSTONE_NUMSUBORDINATES_STR,
  66. LDBM_PARENTID_STR,
  67. SLAPI_ATTR_OBJECTCLASS,
  68. SLAPI_ATTR_UNIQUEID,
  69. SLAPI_ATTR_NSCP_ENTRYDN,
  70. ATTR_NSDS5_REPLCONFLICT,
  71. SLAPI_ATTR_ENTRYUSN,
  72. NULL
  73. };
  74. int
  75. ldbm_attribute_always_indexed(const char *attrtype)
  76. {
  77. int r= 0;
  78. if(NULL != attrtype)
  79. {
  80. int i=0;
  81. while (!r && systemIndexes[i] != NULL)
  82. {
  83. if(!strcasecmp(attrtype,systemIndexes[i]))
  84. {
  85. r= 1;
  86. }
  87. i++;
  88. }
  89. }
  90. return(r);
  91. }
  92. /*
  93. * Given an entry dn and a uniqueid, compute the
  94. * DN of the entry's tombstone. Returns a pointer
  95. * to an allocated block of memory.
  96. */
  97. char *
  98. compute_entry_tombstone_dn(const char *entrydn, const char *uniqueid)
  99. {
  100. char *tombstone_dn;
  101. PR_ASSERT(NULL != entrydn);
  102. PR_ASSERT(NULL != uniqueid);
  103. tombstone_dn = slapi_ch_smprintf("%s=%s,%s",
  104. SLAPI_ATTR_UNIQUEID,
  105. uniqueid,
  106. entrydn);
  107. return tombstone_dn;
  108. }
  109. char *
  110. compute_entry_tombstone_rdn(const char *entryrdn, const char *uniqueid)
  111. {
  112. char *tombstone_rdn;
  113. PR_ASSERT(NULL != entryrdn);
  114. PR_ASSERT(NULL != uniqueid);
  115. tombstone_rdn = slapi_ch_smprintf("%s=%s,%s",
  116. SLAPI_ATTR_UNIQUEID,
  117. uniqueid,
  118. entryrdn);
  119. return tombstone_rdn;
  120. }
  121. /* mark a backend instance "busy"
  122. * returns 0 on success, -1 if the instance is ALREADY busy
  123. */
  124. int instance_set_busy(ldbm_instance *inst)
  125. {
  126. PR_Lock(inst->inst_config_mutex);
  127. if (is_instance_busy(inst)) {
  128. PR_Unlock(inst->inst_config_mutex);
  129. return -1;
  130. }
  131. inst->inst_flags |= INST_FLAG_BUSY;
  132. PR_Unlock(inst->inst_config_mutex);
  133. return 0;
  134. }
  135. int instance_set_busy_and_readonly(ldbm_instance *inst)
  136. {
  137. PR_Lock(inst->inst_config_mutex);
  138. if (is_instance_busy(inst)) {
  139. PR_Unlock(inst->inst_config_mutex);
  140. return -1;
  141. }
  142. inst->inst_flags |= INST_FLAG_BUSY;
  143. /* save old readonly state */
  144. if (slapi_be_get_readonly(inst->inst_be)) {
  145. inst->inst_flags |= INST_FLAG_READONLY;
  146. } else {
  147. inst->inst_flags &= ~INST_FLAG_READONLY;
  148. }
  149. /*
  150. * Normally, acquire rlock on be_lock, then lock inst_config_mutex.
  151. * instance_set_busy_and_readonly should release inst_config_mutex
  152. * before acquiring wlock on be_lock in slapi_mtn_be_set_readonly.
  153. */
  154. PR_Unlock(inst->inst_config_mutex);
  155. slapi_mtn_be_set_readonly(inst->inst_be, 1);
  156. return 0;
  157. }
  158. /* mark a backend instance to be not "busy" anymore */
  159. void instance_set_not_busy(ldbm_instance *inst)
  160. {
  161. int readonly;
  162. PR_Lock(inst->inst_config_mutex);
  163. inst->inst_flags &= ~INST_FLAG_BUSY;
  164. /* set backend readonly flag to match instance flags again
  165. * (sometimes the instance changes the readonly status when it's busy)
  166. */
  167. readonly = (inst->inst_flags & INST_FLAG_READONLY ? 1 : 0);
  168. slapi_mtn_be_set_readonly(inst->inst_be, readonly);
  169. PR_Unlock(inst->inst_config_mutex);
  170. }
  171. void
  172. allinstance_set_not_busy(struct ldbminfo *li)
  173. {
  174. ldbm_instance *inst;
  175. Object *inst_obj;
  176. /* server is up -- mark all backends busy */
  177. for (inst_obj = objset_first_obj(li->li_instance_set); inst_obj;
  178. inst_obj = objset_next_obj(li->li_instance_set, inst_obj)) {
  179. inst = (ldbm_instance *)object_get_data(inst_obj);
  180. instance_set_not_busy(inst);
  181. }
  182. }
  183. void
  184. allinstance_set_busy(struct ldbminfo *li)
  185. {
  186. ldbm_instance *inst;
  187. Object *inst_obj;
  188. /* server is up -- mark all backends busy */
  189. for (inst_obj = objset_first_obj(li->li_instance_set); inst_obj;
  190. inst_obj = objset_next_obj(li->li_instance_set, inst_obj)) {
  191. inst = (ldbm_instance *)object_get_data(inst_obj);
  192. if (instance_set_busy(inst)) {
  193. LDAPDebug1Arg(LDAP_DEBUG_TRACE, "could not set instance [%s] as busy, probably already busy\n",
  194. inst->inst_name);
  195. }
  196. }
  197. }
  198. int
  199. is_anyinstance_busy(struct ldbminfo *li)
  200. {
  201. ldbm_instance *inst;
  202. Object *inst_obj;
  203. int rval = 0;
  204. /* server is up -- mark all backends busy */
  205. for (inst_obj = objset_first_obj(li->li_instance_set); inst_obj;
  206. inst_obj = objset_next_obj(li->li_instance_set, inst_obj)) {
  207. inst = (ldbm_instance *)object_get_data(inst_obj);
  208. PR_Lock(inst->inst_config_mutex);
  209. rval = inst->inst_flags & INST_FLAG_BUSY;
  210. PR_Unlock(inst->inst_config_mutex);
  211. if (0 != rval) {
  212. break;
  213. }
  214. }
  215. if (inst_obj)
  216. object_release(inst_obj);
  217. return rval;
  218. }
  219. int
  220. is_instance_busy(ldbm_instance *inst)
  221. {
  222. return inst->inst_flags & INST_FLAG_BUSY;
  223. }
  224. /*
  225. * delete the given file/directory and its sub files/directories
  226. */
  227. int
  228. ldbm_delete_dirs(char *path)
  229. {
  230. PRDir *dirhandle = NULL;
  231. PRDirEntry *direntry = NULL;
  232. char fullpath[MAXPATHLEN];
  233. int rval = 0;
  234. PRFileInfo64 info;
  235. dirhandle = PR_OpenDir(path);
  236. if (! dirhandle)
  237. {
  238. PR_Delete(path);
  239. return 0;
  240. }
  241. while (NULL != (direntry =
  242. PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT)))
  243. {
  244. if (! direntry->name)
  245. break;
  246. PR_snprintf(fullpath, MAXPATHLEN, "%s/%s", path, direntry->name);
  247. rval = PR_GetFileInfo64(fullpath, &info);
  248. if (PR_SUCCESS == rval)
  249. {
  250. if (PR_FILE_DIRECTORY == info.type)
  251. rval += ldbm_delete_dirs(fullpath);
  252. }
  253. if (PR_FILE_DIRECTORY != info.type)
  254. PR_Delete(fullpath);
  255. }
  256. PR_CloseDir(dirhandle);
  257. /* remove the directory itself too */
  258. rval += PR_RmDir(path);
  259. return rval;
  260. }
  261. char
  262. get_sep(char *path)
  263. {
  264. if (NULL == path)
  265. return '/'; /* default */
  266. if (NULL != strchr(path, '/'))
  267. return '/';
  268. if (NULL != strchr(path, '\\'))
  269. return '\\';
  270. return '/'; /* default */
  271. }
  272. /* mkdir -p */
  273. int
  274. mkdir_p(char *dir, unsigned int mode)
  275. {
  276. PRFileInfo64 info;
  277. int rval;
  278. char sep = get_sep(dir);
  279. rval = PR_GetFileInfo64(dir, &info);
  280. if (PR_SUCCESS == rval)
  281. {
  282. if (PR_FILE_DIRECTORY != info.type) /* not a directory */
  283. {
  284. PR_Delete(dir);
  285. if (PR_SUCCESS != PR_MkDir(dir, mode))
  286. {
  287. LDAPDebug(LDAP_DEBUG_ANY, "mkdir_p %s: error %d (%s)\n",
  288. dir, PR_GetError(),slapd_pr_strerror(PR_GetError()));
  289. return -1;
  290. }
  291. }
  292. return 0;
  293. }
  294. else
  295. {
  296. /* does not exist */
  297. char *p, *e;
  298. char c[2] = {0, 0};
  299. int len = strlen(dir);
  300. rval = 0;
  301. e = dir + len - 1;
  302. if (*e == sep)
  303. {
  304. c[1] = *e;
  305. *e = '\0';
  306. }
  307. c[0] = '/';
  308. p = strrchr(dir, sep);
  309. if (NULL != p)
  310. {
  311. *p = '\0';
  312. rval = mkdir_p(dir, mode);
  313. *p = c[0];
  314. }
  315. if (c[1])
  316. *e = c[1];
  317. if (0 != rval)
  318. return rval;
  319. if (PR_SUCCESS != PR_MkDir(dir, mode))
  320. {
  321. LDAPDebug(LDAP_DEBUG_ANY, "mkdir_p %s: error %d (%s)\n",
  322. dir, PR_GetError(),slapd_pr_strerror(PR_GetError()));
  323. return -1;
  324. }
  325. return 0;
  326. }
  327. }
  328. /* This routine checks to see if there is a callback registered for retrieving
  329. * RUV updates to add to the datastore transaction. If so, it allocates a
  330. * modify_context for consumption by the caller. */
  331. int
  332. ldbm_txn_ruv_modify_context( Slapi_PBlock *pb, modify_context *mc )
  333. {
  334. char *uniqueid = NULL;
  335. backend *be;
  336. Slapi_Mods *smods = NULL;
  337. struct backentry *bentry;
  338. entry_address bentry_addr;
  339. IFP fn = NULL;
  340. int rc = 0;
  341. back_txn txn = {NULL};
  342. slapi_pblock_get(pb, SLAPI_TXN_RUV_MODS_FN, (void *)&fn);
  343. slapi_pblock_get(pb, SLAPI_TXN, &txn.back_txn_txn);
  344. if (NULL == fn) {
  345. return (0);
  346. }
  347. rc = (*fn)(pb, &uniqueid, &smods);
  348. /* Either something went wrong when the RUV callback tried to assemble
  349. * the updates for us, or there were no updates because the op doesn't
  350. * target a replica. */
  351. /* or, the CSN is already covered by the RUV */
  352. if (1 != rc || NULL == smods || NULL == uniqueid) {
  353. return (rc);
  354. }
  355. slapi_pblock_get( pb, SLAPI_BACKEND, &be);
  356. bentry_addr.sdn = NULL;
  357. bentry_addr.udn = NULL;
  358. bentry_addr.uniqueid = uniqueid;
  359. /* Note: if we find the bentry, it will stay locked until someone calls
  360. * modify_term on the mc we'll be associating the bentry with */
  361. bentry = find_entry2modify_only( pb, be, &bentry_addr, &txn );
  362. if (NULL == bentry) {
  363. /* Uh oh, we couldn't find and lock the RUV entry! */
  364. LDAPDebug( LDAP_DEBUG_ANY, "Error: ldbm_txn_ruv_modify_context failed to retrieve and lock RUV entry\n",
  365. 0, 0, 0 );
  366. rc = -1;
  367. goto done;
  368. }
  369. modify_init( mc, bentry );
  370. if (modify_apply_mods_ignore_error( mc, smods, LDAP_TYPE_OR_VALUE_EXISTS )) {
  371. LDAPDebug( LDAP_DEBUG_ANY, "Error: ldbm_txn_ruv_modify_context failed to apply updates to RUV entry\n",
  372. 0, 0, 0 );
  373. rc = -1;
  374. modify_term( mc, be );
  375. }
  376. done:
  377. slapi_ch_free_string( &uniqueid );
  378. /* No need to free smods; they get freed along with the modify context */
  379. return (rc);
  380. }
  381. int
  382. is_fullpath(char *path)
  383. {
  384. int len;
  385. if (NULL == path || '\0' == *path)
  386. return 0;
  387. if ('/' == *path || '\\' == *path)
  388. return 1;
  389. len = strlen(path);
  390. if (len > 2)
  391. {
  392. if (':' == path[1] && ('/' == path[2] || '\\' == path[2])) /* Windows */
  393. return 1;
  394. }
  395. return 0;
  396. }
  397. /* the problem with getline is that it inserts \0 for every
  398. newline \n or \r - this is a problem when you just want
  399. to grab some value from the ldif string but do not
  400. want to change the ldif string because it will be
  401. parsed again in the future
  402. openldap ldif_getline() is more of a problem because
  403. it does this for every comment line too, whereas mozldap
  404. ldif_getline() just skips comment lines
  405. */
  406. static void
  407. ldif_getline_fixline(char *start, char *end)
  408. {
  409. while (start && (start < end)) {
  410. if (*start == '\0') {
  411. /* the original ldif string will usually end with \n \0
  412. ldif_getline will turn this into \0 \0
  413. in this case, we don't want to turn it into
  414. \r \n we want \n \0
  415. */
  416. if ((start < (end - 1)) && (*(start + 1) == '\0')) {
  417. *start = '\r';
  418. start++;
  419. }
  420. *start = '\n';
  421. start++;
  422. } else {
  423. start++;
  424. }
  425. }
  426. return;
  427. }
  428. /*
  429. * Get value of type from string.
  430. * Note: this function is very primitive. It does not support multi values.
  431. * This could be used to retrieve a single value as a string from raw data
  432. * read from db.
  433. */
  434. /* caller is responsible to release "value" */
  435. int
  436. get_value_from_string(const char *string, char *type, char **value)
  437. {
  438. int rc = -1;
  439. size_t typelen = 0;
  440. char *ptr = NULL;
  441. char *copy = NULL;
  442. char *tmpptr = NULL;
  443. char *startptr = NULL;
  444. struct berval tmptype = {0, NULL};
  445. struct berval bvvalue = {0, NULL};
  446. int freeval = 0;
  447. if (NULL == string || NULL == type || NULL == value) {
  448. return rc;
  449. }
  450. *value = NULL;
  451. tmpptr = (char *)string;
  452. ptr = PL_strcasestr(tmpptr, type);
  453. if (NULL == ptr) {
  454. return rc;
  455. }
  456. typelen = strlen(type);
  457. startptr = tmpptr;
  458. while (NULL != (ptr = ldif_getline(&tmpptr))) {
  459. if ((0 != PL_strncasecmp(ptr, type, typelen)) ||
  460. (*(ptr + typelen) != ';' && *(ptr + typelen) != ':')) {
  461. /* did not match */
  462. ldif_getline_fixline(startptr, tmpptr);
  463. startptr = tmpptr;
  464. continue;
  465. }
  466. /* matched */
  467. copy = slapi_ch_strdup(ptr);
  468. ldif_getline_fixline(startptr, tmpptr);
  469. startptr = tmpptr;
  470. rc = slapi_ldif_parse_line(copy, &tmptype, &bvvalue, &freeval);
  471. if (0 > rc || NULL == tmptype.bv_val ||
  472. NULL == bvvalue.bv_val || 0 >= bvvalue.bv_len) {
  473. slapi_log_error(SLAPI_LOG_FATAL, "get_value_from_string", "parse "
  474. "failed: %d\n", rc);
  475. if (freeval) {
  476. slapi_ch_free_string(&bvvalue.bv_val);
  477. }
  478. rc = -1; /* set non-0 to rc */
  479. goto bail;
  480. }
  481. if (0 != PL_strncasecmp(type, tmptype.bv_val, tmptype.bv_len)) {
  482. slapi_log_error(SLAPI_LOG_FATAL, "get_value_from_string", "type "
  483. "does not match: %s != %s\n",
  484. type, tmptype.bv_val);
  485. if (freeval) {
  486. slapi_ch_free_string(&bvvalue.bv_val);
  487. }
  488. rc = -1; /* set non-0 to rc */
  489. goto bail;
  490. }
  491. rc = 0; /* set 0 to rc */
  492. if (freeval) {
  493. *value = bvvalue.bv_val; /* just hand off the memory */
  494. bvvalue.bv_val = NULL;
  495. } else { /* make a copy */
  496. *value = (char *)slapi_ch_malloc(bvvalue.bv_len + 1);
  497. memcpy(*value, bvvalue.bv_val, bvvalue.bv_len);
  498. *(*value + bvvalue.bv_len) = '\0';
  499. }
  500. slapi_ch_free_string(&copy);
  501. }
  502. bail:
  503. slapi_ch_free_string(&copy);
  504. return rc;
  505. }
  506. /*
  507. * Get value array of type from string.
  508. * multi-value support for get_value_from_string
  509. */
  510. /* caller is responsible to release "valuearray" */
  511. int
  512. get_values_from_string(const char *string, char *type, char ***valuearray)
  513. {
  514. int rc = -1;
  515. size_t typelen = 0;
  516. char *ptr = NULL;
  517. char *copy = NULL;
  518. char *tmpptr = NULL;
  519. char *startptr = NULL;
  520. struct berval tmptype = {0, NULL};
  521. struct berval bvvalue = {0, NULL};
  522. int freeval = 0;
  523. char *value = NULL;
  524. int idx = 0;
  525. #define get_values_INITIALMAXCNT 1
  526. int maxcnt = get_values_INITIALMAXCNT;
  527. if (NULL == string || NULL == type || NULL == valuearray) {
  528. return rc;
  529. }
  530. *valuearray = NULL;
  531. tmpptr = (char *)string;
  532. ptr = PL_strcasestr(tmpptr, type);
  533. if (NULL == ptr) {
  534. return rc;
  535. }
  536. typelen = strlen(type);
  537. startptr = tmpptr;
  538. while (NULL != (ptr = ldif_getline(&tmpptr))) {
  539. if ((0 != PL_strncasecmp(ptr, type, typelen)) ||
  540. (*(ptr + typelen) != ';' && *(ptr + typelen) != ':')) {
  541. /* did not match */
  542. ldif_getline_fixline(startptr, tmpptr);
  543. startptr = tmpptr;
  544. continue;
  545. }
  546. /* matched */
  547. copy = slapi_ch_strdup(ptr);
  548. ldif_getline_fixline(startptr, tmpptr);
  549. startptr = tmpptr;
  550. rc = slapi_ldif_parse_line(copy, &tmptype, &bvvalue, &freeval);
  551. if (0 > rc || NULL == bvvalue.bv_val || 0 >= bvvalue.bv_len) {
  552. continue;
  553. }
  554. if (0 != PL_strncasecmp(type, tmptype.bv_val, tmptype.bv_len)) {
  555. char *p = PL_strchr(tmptype.bv_val, ';'); /* subtype ? */
  556. if (p) {
  557. if (0 != strncasecmp(type, tmptype.bv_val, p - tmptype.bv_val)) {
  558. slapi_log_error(SLAPI_LOG_FATAL, "get_values_from_string",
  559. "type does not match: %s != %s\n",
  560. type, tmptype.bv_val);
  561. if (freeval) {
  562. slapi_ch_free_string(&bvvalue.bv_val);
  563. }
  564. goto bail;
  565. }
  566. } else {
  567. slapi_log_error(SLAPI_LOG_FATAL, "get_values_from_string",
  568. "type does not match: %s != %s\n",
  569. type, tmptype.bv_val);
  570. if (freeval) {
  571. slapi_ch_free_string(&bvvalue.bv_val);
  572. }
  573. goto bail;
  574. }
  575. }
  576. if (freeval) {
  577. value = bvvalue.bv_val; /* just hand off memory */
  578. bvvalue.bv_val = NULL;
  579. } else { /* copy */
  580. value = (char *)slapi_ch_malloc(bvvalue.bv_len + 1);
  581. memcpy(value, bvvalue.bv_val, bvvalue.bv_len);
  582. *(value + bvvalue.bv_len) = '\0';
  583. }
  584. if ((get_values_INITIALMAXCNT == maxcnt) || !valuearray ||
  585. (idx + 1 >= maxcnt)) {
  586. maxcnt *= 2;
  587. *valuearray = (char **)slapi_ch_realloc((char *)*valuearray,
  588. sizeof(char *) * maxcnt);
  589. }
  590. (*valuearray)[idx++] = value;
  591. (*valuearray)[idx] = NULL;
  592. slapi_ch_free_string(&copy);
  593. }
  594. bail:
  595. slapi_ch_free_string(&copy);
  596. return rc;
  597. }
  598. void
  599. normalize_dir(char *dir)
  600. {
  601. char *p = NULL;
  602. int l = 0;
  603. if (NULL == dir) {
  604. return;
  605. }
  606. l = strlen(dir);
  607. for (p = dir + l - 1; p && *p && (p > dir); p--) {
  608. if ((' ' != *p) && ('\t' != *p) && ('/' != *p) && ('\\' != *p)) {
  609. break;
  610. }
  611. }
  612. *(p+1) = '\0';
  613. }