misc.c 18 KB

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