misc.c 21 KB

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