utils.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. /***********************************************************************
  42. ** NAME
  43. ** utils.c
  44. **
  45. ** DESCRIPTION
  46. **
  47. **
  48. ** AUTHOR
  49. ** <[email protected]>
  50. **
  51. ***********************************************************************/
  52. /***********************************************************************
  53. ** Includes
  54. ***********************************************************************/
  55. #include "plugin-utils.h"
  56. #include "nspr.h"
  57. static char *plugin_name = "utils";
  58. /*
  59. * Lock for updating a counter (global for all counters)
  60. */
  61. static Slapi_Mutex *counter_lock = NULL;
  62. /* ------------------------------------------------------------ */
  63. /*
  64. * op_error - Record (and report) an operational error.
  65. */
  66. int
  67. op_error(int internal_error) {
  68. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  69. "Internal error: %d\n", internal_error);
  70. return LDAP_OPERATIONS_ERROR;
  71. }
  72. int initCounterLock() {
  73. if ( NULL == counter_lock ) {
  74. if ( !(counter_lock = slapi_new_mutex()) ) {
  75. return 200;
  76. }
  77. }
  78. return 0;
  79. }
  80. /* ------------------------------------------------------------ */
  81. /*
  82. * readPblockAndEntry - search for and read an entry
  83. * Return:
  84. * A pblock containing the entry, or NULL
  85. */
  86. Slapi_PBlock *
  87. readPblockAndEntry( const char *baseDN, const char *filter,
  88. char *attrs[] ) {
  89. int result = 0;
  90. Slapi_PBlock *spb = NULL;
  91. BEGIN
  92. int sres;
  93. /* Perform the search - the new pblock needs to be freed */
  94. spb = slapi_search_internal((char *)baseDN, LDAP_SCOPE_BASE,
  95. (char *)filter, NULL, attrs, 0);
  96. if ( !spb ) {
  97. result = op_error(20);
  98. break;
  99. }
  100. if ( slapi_pblock_get( spb, SLAPI_PLUGIN_INTOP_RESULT, &sres ) ) {
  101. result = op_error(21);
  102. break;
  103. } else if (sres) {
  104. result = op_error(22);
  105. break;
  106. }
  107. END
  108. return spb;
  109. }
  110. /* ------------------------------------------------------------ */
  111. /*
  112. * hasObjectClass - read an entry and check if it has a
  113. * particular object class value
  114. * Return:
  115. * 1 - the entry contains the object class value
  116. * 0 - the entry doesn't contain the object class value
  117. */
  118. int
  119. entryHasObjectClass(Slapi_PBlock *pb, Slapi_Entry *e,
  120. const char *objectClass) {
  121. Slapi_Attr *attr;
  122. Slapi_Value *v;
  123. const struct berval *bv;
  124. int vhint;
  125. if ( slapi_entry_attr_find(e, "objectclass", &attr) ) {
  126. return 0; /* no objectclass values! */
  127. }
  128. /*
  129. * Check each of the object class values in turn.
  130. */
  131. for ( vhint = slapi_attr_first_value( attr, &v );
  132. vhint != -1;
  133. vhint = slapi_attr_next_value( attr, vhint, &v )) {
  134. bv = slapi_value_get_berval(v);
  135. if ( NULL != bv && NULL != bv->bv_val &&
  136. !strcasecmp(bv->bv_val, objectClass) ) {
  137. return 1;
  138. }
  139. }
  140. return 0;
  141. }
  142. /* ------------------------------------------------------------ */
  143. /*
  144. * dnHasObjectClass - read an entry if it has a particular object class
  145. * Return:
  146. * A pblock containing the entry, or NULL
  147. */
  148. Slapi_PBlock *
  149. dnHasObjectClass( const char *baseDN, const char *objectClass ) {
  150. int result = 0;
  151. char *filter = NULL;
  152. Slapi_PBlock *spb = NULL;
  153. BEGIN
  154. Slapi_Entry **entries;
  155. char *attrs[2];
  156. /* Perform the search - the new pblock needs to be freed */
  157. attrs[0] = "objectclass";
  158. attrs[1] = NULL;
  159. filter = PR_smprintf("objectclass=%s", objectClass );
  160. if ( !(spb = readPblockAndEntry( baseDN, filter, attrs) ) ) {
  161. break;
  162. }
  163. if ( slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  164. &entries) ) {
  165. result = op_error(23);
  166. break;
  167. }
  168. /*
  169. * Can only be one entry returned on a base search; just check
  170. * the first one
  171. */
  172. if ( !*entries ) {
  173. /* Clean up */
  174. slapi_free_search_results_internal(spb);
  175. slapi_pblock_destroy(spb);
  176. spb = NULL;
  177. }
  178. END
  179. if (filter) {
  180. PR_smprintf_free(filter);
  181. }
  182. return spb;
  183. }
  184. /* ------------------------------------------------------------ */
  185. /*
  186. * dnHasAttribute - read an entry if it has a particular attribute
  187. * Return:
  188. * The entry, or NULL
  189. */
  190. Slapi_PBlock *
  191. dnHasAttribute( const char *baseDN, const char *attrName ) {
  192. int result = 0;
  193. Slapi_PBlock *spb = NULL;
  194. char *filter = NULL;
  195. BEGIN
  196. int sres;
  197. Slapi_Entry **entries;
  198. char *attrs[2];
  199. /* Perform the search - the new pblock needs to be freed */
  200. attrs[0] = (char *)attrName;
  201. attrs[1] = NULL;
  202. filter = PR_smprintf( "%s=*", attrName );
  203. spb = slapi_search_internal((char *)baseDN, LDAP_SCOPE_BASE,
  204. filter, NULL, attrs, 0);
  205. if ( !spb ) {
  206. result = op_error(20);
  207. break;
  208. }
  209. if ( slapi_pblock_get( spb, SLAPI_PLUGIN_INTOP_RESULT, &sres ) ) {
  210. result = op_error(21);
  211. break;
  212. } else if (sres) {
  213. result = op_error(22);
  214. break;
  215. }
  216. if ( slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  217. &entries) ) {
  218. result = op_error(23);
  219. break;
  220. }
  221. /*
  222. * Can only be one entry returned on a base search; just check
  223. * the first one
  224. */
  225. if ( !*entries ) {
  226. /* Clean up */
  227. slapi_free_search_results_internal(spb);
  228. slapi_pblock_destroy(spb);
  229. spb = NULL;
  230. }
  231. END
  232. if (filter) {
  233. PR_smprintf_free(filter);
  234. }
  235. return spb;
  236. }
  237. /* ------------------------------------------------------------ */
  238. /*
  239. * setCounter - set the value of a counter
  240. *
  241. * Return:
  242. * LDAP_SUCCESS - updated the attribute
  243. * other - failure to update the count
  244. */
  245. int
  246. setCounter( Slapi_Entry *e, const char *attrName, int value ) {
  247. int result = LDAP_SUCCESS;
  248. Slapi_PBlock *modifySpb = NULL;
  249. char strValue[16];
  250. char *strValues[2] = { NULL };
  251. LDAPMod mod;
  252. LDAPMod *mods[2];
  253. int res;
  254. BEGIN
  255. /* Store the updated value */
  256. strValues[0] = strValue;
  257. sprintf( strValue, "%d", value );
  258. mod.mod_op = LDAP_MOD_REPLACE;
  259. mod.mod_type = (char *)attrName;
  260. mod.mod_values = strValues;
  261. mods[0] = &mod;
  262. mods[1] = NULL;
  263. modifySpb = slapi_modify_internal( slapi_entry_get_dn(e), mods,
  264. NULL, 1 );
  265. /* Check if the operation succeeded */
  266. if ( slapi_pblock_get( modifySpb, SLAPI_PLUGIN_INTOP_RESULT,
  267. &res ) ) {
  268. result = op_error(33);
  269. break;
  270. } else if (res) {
  271. result = op_error(34);
  272. break;
  273. }
  274. slapi_pblock_destroy(modifySpb);
  275. END
  276. return result;
  277. }
  278. /* ------------------------------------------------------------ */
  279. /*
  280. * updateCounter - read and increment/decrement the value of a counter
  281. *
  282. * Return:
  283. * LDAP_SUCCESS - updated the attribute
  284. * other - failure to update the count
  285. */
  286. int
  287. updateCounter( Slapi_Entry *e, const char *attrName, int increment ) {
  288. int result = LDAP_SUCCESS;
  289. Slapi_PBlock *modifySpb = NULL;
  290. Slapi_Attr *attr;
  291. int value = 0;
  292. char strValue[16];
  293. char *strValues[2] = { NULL };
  294. LDAPMod mod;
  295. LDAPMod *mods[2];
  296. int res;
  297. BEGIN
  298. /* Lock the entry */
  299. slapi_lock_mutex(counter_lock);
  300. /* Get the count attribute */
  301. if ( slapi_entry_attr_find(e, (char *)attrName, &attr) ) {
  302. /* No count yet; that's OK */
  303. } else {
  304. /* Get the first value for the attribute */
  305. Slapi_Value *v = NULL;
  306. const struct berval *bv = NULL;
  307. if ( -1 == slapi_attr_first_value( attr, &v ) || NULL == v ||
  308. NULL == ( bv = slapi_value_get_berval(v)) ||
  309. NULL == bv->bv_val ) {
  310. /* No values yet; that's OK, too */
  311. } else {
  312. value = atoi( bv->bv_val );
  313. }
  314. }
  315. /* Add the increment */
  316. value += increment;
  317. if ( value < 0 ) {
  318. value = 0;
  319. }
  320. /* Store the updated value */
  321. strValues[0] = strValue;
  322. sprintf( strValue, "%d", value );
  323. mod.mod_op = LDAP_MOD_REPLACE;
  324. mod.mod_type = (char *)attrName;
  325. mod.mod_values = strValues;
  326. mods[0] = &mod;
  327. mods[1] = NULL;
  328. modifySpb = slapi_modify_internal( slapi_entry_get_dn(e), mods,
  329. NULL, 1 );
  330. /* Check if the operation succeeded */
  331. if ( slapi_pblock_get( modifySpb, SLAPI_PLUGIN_INTOP_RESULT,
  332. &res ) ) {
  333. result = op_error(33);
  334. break;
  335. } else if (res) {
  336. result = op_error(34);
  337. break;
  338. }
  339. slapi_pblock_destroy(modifySpb);
  340. /* Unlock the entry */
  341. slapi_unlock_mutex(counter_lock);
  342. #ifdef DEBUG
  343. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  344. "adjusted %s in %s by %d to %d\n",
  345. attrName, slapi_entry_get_dn(e), increment, value);
  346. #endif
  347. END
  348. return result;
  349. }
  350. /* ------------------------------------------------------------ */
  351. /*
  352. * updateCounterByDN - read and increment/decrement the value of a counter
  353. *
  354. * Return:
  355. * LDAP_SUCCESS - updated the attribute
  356. * other - failure to update the count
  357. */
  358. int
  359. updateCounterByDN( const char *dn, const char *attrName, int increment ) {
  360. int result = LDAP_SUCCESS;
  361. Slapi_PBlock *spb = NULL;
  362. Slapi_Entry **entries;
  363. BEGIN
  364. char *attrs[2];
  365. int sres;
  366. /* Perform the search - the new pblock needs to be freed */
  367. attrs[0] = (char *)attrName;
  368. attrs[1] = NULL;
  369. spb = slapi_search_internal((char *)dn, LDAP_SCOPE_BASE,
  370. "objectclass=*", NULL, attrs, 0);
  371. if ( !spb ) {
  372. result = op_error(20);
  373. break;
  374. }
  375. if ( slapi_pblock_get( spb, SLAPI_PLUGIN_INTOP_RESULT, &sres ) ) {
  376. result = op_error(21);
  377. break;
  378. } else if (sres) {
  379. result = op_error(22);
  380. break;
  381. }
  382. if ( slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  383. &entries) ) {
  384. result = op_error(23);
  385. break;
  386. }
  387. END
  388. if ( 0 == result ) {
  389. result = updateCounter( *entries, attrName, increment );
  390. }
  391. if ( NULL != spb ) {
  392. /* Clean up */
  393. slapi_free_search_results_internal(spb);
  394. slapi_pblock_destroy(spb);
  395. }
  396. return result;
  397. }
  398. /*
  399. * Lock for accessing a cache (global for all caches)
  400. */
  401. static Slapi_Mutex *cache_lock = NULL;
  402. DNLink *cacheInit() {
  403. DNLink *root;
  404. slapi_lock_mutex(cache_lock);
  405. root = (DNLink *)malloc( sizeof(DNLink) );
  406. root->next = NULL;
  407. root->data = NULL;
  408. root->dn = (char *)malloc(1);
  409. root->dn[0] = 0;
  410. slapi_unlock_mutex(cache_lock);
  411. return root;
  412. }
  413. DNLink *cacheAdd( DNLink *root, char *dn, void *data ) {
  414. if ( NULL == root ) {
  415. return NULL;
  416. }
  417. slapi_lock_mutex(cache_lock);
  418. for( ; root->next; root = root->next ) {
  419. }
  420. root->next = (DNLink *)malloc( sizeof(DNLink) );
  421. root = root->next;
  422. root->dn = dn;
  423. root->data = data;
  424. root->next = NULL;
  425. slapi_unlock_mutex(cache_lock);
  426. return root;
  427. }
  428. char *cacheRemove( DNLink *root, char *dn ) {
  429. char *found = NULL;
  430. DNLink *current = root;
  431. DNLink *prev = NULL;
  432. if ( NULL == root ) {
  433. return NULL;
  434. }
  435. slapi_lock_mutex(cache_lock);
  436. for( ; current; prev = current, current = current->next ) {
  437. if ( !strcmp( current->dn, dn ) ) {
  438. found = current->dn;
  439. prev->next = current->next;
  440. slapi_ch_free( (void **)&current );
  441. break;
  442. }
  443. }
  444. slapi_unlock_mutex(cache_lock);
  445. return found;
  446. }
  447. int cacheDelete( DNLink *root, char *dn ) {
  448. char *found = cacheRemove( root, dn );
  449. if ( found ) {
  450. slapi_ch_free( (void **)&found );
  451. return 0;
  452. } else {
  453. return 1;
  454. }
  455. }
  456. DNLink *cacheFind( DNLink *root, char *dn ) {
  457. if ( NULL == root ) {
  458. return NULL;
  459. }
  460. slapi_lock_mutex(cache_lock);
  461. for( ; root && strcmp(dn, root->dn); root = root->next ) {
  462. }
  463. slapi_unlock_mutex(cache_lock);
  464. return root;
  465. }