utils.c 11 KB

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