7bit.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. * 7bit.c
  43. *
  44. * Implements a directory server pre-operation plugin to test
  45. * attributes for 7 bit clean within a defined subtree in the
  46. * directory.
  47. *
  48. */
  49. #include <stdio.h>
  50. #include <slapi-plugin.h>
  51. #include <string.h>
  52. /* DBDB this should be pulled from a common header file */
  53. #ifdef _WIN32
  54. #ifndef strcasecmp
  55. #define strcasecmp(x,y) strcmpi(x,y)
  56. #endif
  57. #endif
  58. #if defined( LDAP_DEBUG ) && !defined( DEBUG )
  59. #define DEBUG
  60. #endif
  61. /*
  62. * ISSUES:
  63. * How should this plugin handle ACL issues? It seems wrong to reject
  64. * adds and modifies because there is already a conflicting UID, when
  65. * the request would have failed because of an ACL check anyway.
  66. *
  67. * This code currently defines a maximum filter string size of 512. Is
  68. * this large enough?
  69. *
  70. * This code currently does not quote the value portion of the filter as
  71. * it is created. This is a bug.
  72. */
  73. /* */
  74. #define BEGIN do {
  75. #define END } while(0);
  76. /*
  77. * Slapi plugin descriptor
  78. */
  79. static char *plugin_name = "NS7bitAttr";
  80. static Slapi_PluginDesc
  81. pluginDesc = { "NS7bitAttr", VENDOR, DS_PACKAGE_VERSION,
  82. "Enforce 7-bit clean attribute values" };
  83. /*
  84. * More information about constraint failure
  85. */
  86. static char *moreInfo =
  87. "The value is not 7-bit clean: ";
  88. /* ------------------------------------------------------------ */
  89. /*
  90. * op_error - Record (and report) an operational error.
  91. */
  92. static int
  93. op_error(int internal_error)
  94. {
  95. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  96. "Internal error: %d\n", internal_error);
  97. return LDAP_OPERATIONS_ERROR;
  98. }
  99. static void
  100. issue_error(Slapi_PBlock *pb, int result, char *type, char *value)
  101. {
  102. char *moreinfop;
  103. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  104. "%s result %d\n", type, result);
  105. if (value == NULL) {
  106. value = "unknown";
  107. }
  108. moreinfop = slapi_ch_smprintf("%s%s", moreInfo, value);
  109. /* Send failure to the client */
  110. slapi_send_ldap_result(pb, result, 0, moreinfop, 0, 0);
  111. slapi_ch_free((void **)&moreinfop);
  112. return;
  113. }
  114. /*
  115. * Check 'value' for 7-bit cleanliness.
  116. */
  117. static int
  118. bit_check_one_berval(const struct berval *value, char **violated)
  119. {
  120. int result;
  121. char *ch;
  122. int i;
  123. #ifdef DEBUG
  124. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "7-bit checking begin\n");
  125. #endif
  126. result = LDAP_SUCCESS;
  127. /* If no value, can't possibly be a conflict */
  128. if ( (struct berval *)NULL == value )
  129. return result;
  130. for(i=0, ch=value->bv_val; ch && i < (int)(value->bv_len) ;
  131. ch++, i++)
  132. {
  133. if (( 0x80 & *ch ) != 0 )
  134. {
  135. result = LDAP_CONSTRAINT_VIOLATION;
  136. *violated = value->bv_val;
  137. break;
  138. }
  139. }
  140. return result;
  141. }
  142. /*
  143. * Check a set of values for 7-bit cleanliness.
  144. *
  145. * If 'attr' is NULL, the values are taken from 'values'.
  146. * If 'attr' is non-NULL, the values are taken from 'attr'.
  147. */
  148. static int
  149. bit_check(Slapi_Attr *attr, struct berval **values, char **violated)
  150. {
  151. int result = LDAP_SUCCESS;
  152. *violated = NULL;
  153. /* If no values, can't possibly be a conflict */
  154. if ( (Slapi_Attr *)NULL == attr && (struct berval **)NULL == values )
  155. return result;
  156. if ( (Slapi_Attr *)NULL != attr )
  157. {
  158. Slapi_Value *v = NULL;
  159. int vhint = -1;
  160. for ( vhint = slapi_attr_first_value( attr, &v );
  161. vhint != -1 && LDAP_SUCCESS == result;
  162. vhint = slapi_attr_next_value( attr, vhint, &v ))
  163. {
  164. result = bit_check_one_berval(slapi_value_get_berval(v), violated);
  165. }
  166. }
  167. else
  168. {
  169. for (;*values != NULL && LDAP_SUCCESS == result; values++)
  170. {
  171. result = bit_check_one_berval(*values, violated);
  172. }
  173. }
  174. #ifdef DEBUG
  175. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  176. "7 bit check result = %d\n", result);
  177. #endif
  178. return result;
  179. }
  180. /* ------------------------------------------------------------ */
  181. /*
  182. * preop_add - pre-operation plug-in for add
  183. */
  184. static int
  185. preop_add(Slapi_PBlock *pb)
  186. {
  187. int result;
  188. char *violated = NULL;
  189. #ifdef DEBUG
  190. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD begin\n");
  191. #endif
  192. result = LDAP_SUCCESS;
  193. /*
  194. * Do constraint check on the added entry. Set result.
  195. */
  196. BEGIN
  197. int err;
  198. int argc;
  199. char **argv;
  200. char **attrName;
  201. const char *dn;
  202. Slapi_DN *sdn = NULL;
  203. Slapi_Entry *e;
  204. Slapi_Attr *attr;
  205. char **firstSubtree;
  206. char **subtreeDN;
  207. int subtreeCnt;
  208. int is_replicated_operation;
  209. /*
  210. * Get the arguments
  211. */
  212. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  213. if (err) { result = op_error(53); break; }
  214. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  215. if (err) { result = op_error(54); break; }
  216. /*
  217. * If this is a replication update, just be a noop.
  218. */
  219. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation);
  220. if (err) { result = op_error(56); break; }
  221. if (is_replicated_operation)
  222. {
  223. break;
  224. }
  225. /*
  226. * Get the target DN for this add operation
  227. */
  228. err = slapi_pblock_get(pb, SLAPI_ADD_TARGET_SDN, &sdn);
  229. if (err) { result = op_error(50); break; }
  230. dn = slapi_sdn_get_dn(sdn);
  231. #ifdef DEBUG
  232. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD target=%s\n", dn);
  233. #endif
  234. /*
  235. * Get the entry data for this add. Check whether it
  236. * contains a value for the unique attribute
  237. */
  238. err = slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  239. if (err) { result = op_error(51); break; }
  240. for ( firstSubtree = argv; strcmp(*firstSubtree, ",") != 0;
  241. firstSubtree++, argc--) {}
  242. firstSubtree++;
  243. argc--;
  244. for (attrName = argv; strcmp(*attrName, ",") != 0; attrName++ )
  245. {
  246. /*
  247. * if the attribute is userpassword, check unhashed#user#password
  248. * instead. "userpassword" is encoded; it will always pass the 7bit
  249. * check.
  250. */
  251. char *attr_name;
  252. if ( strcasecmp(*attrName, "userpassword") == 0 )
  253. {
  254. attr_name = "unhashed#user#password";
  255. } else {
  256. attr_name = *attrName;
  257. }
  258. err = slapi_entry_attr_find(e, attr_name, &attr);
  259. if (err) continue; /* break;*/ /* no 7-bit attribute */
  260. /*
  261. * For each DN in the managed list, do 7-bit checking if
  262. * the target DN is a subnode in the tree.
  263. */
  264. for( subtreeDN=firstSubtree, subtreeCnt=argc ;subtreeCnt > 0;
  265. subtreeCnt--,subtreeDN++)
  266. {
  267. /*
  268. * issuffix determines whether the target is under the
  269. * subtree *subtreeDN
  270. */
  271. if (slapi_dn_issuffix(dn, *subtreeDN))
  272. {
  273. #ifdef DEBUG
  274. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  275. "ADD subtree=%s\n", *subtreeDN);
  276. #endif
  277. /*
  278. * Check if the value is 7-bit clean
  279. */
  280. result = bit_check(attr, NULL, &violated);
  281. if (result) break;
  282. }
  283. }
  284. /* don't have to go on if there is a value not 7-bit clean */
  285. if (result) break;
  286. }
  287. END
  288. if (result) {
  289. issue_error(pb, result, "ADD", violated);
  290. }
  291. return (result==LDAP_SUCCESS)?0:-1;
  292. }
  293. static void
  294. addMod(LDAPMod ***modary, int *capacity, int *nmods, LDAPMod *toadd)
  295. {
  296. if (*nmods == *capacity) {
  297. *capacity += 4;
  298. if (*modary) {
  299. *modary = (LDAPMod **)slapi_ch_realloc((char *)*modary, *capacity * sizeof(LDAPMod *));
  300. } else {
  301. *modary = (LDAPMod **)slapi_ch_malloc(*capacity * sizeof(LDAPMod *));
  302. }
  303. }
  304. (*modary)[*nmods] = toadd;
  305. (*nmods)++;
  306. }
  307. /* ------------------------------------------------------------ */
  308. /*
  309. * preop_modify - pre-operation plug-in for modify
  310. */
  311. static int
  312. preop_modify(Slapi_PBlock *pb)
  313. {
  314. int result;
  315. char *violated = NULL;
  316. LDAPMod **checkmods = NULL; /* holds mods to check */
  317. int checkmodsCapacity = 0; /* max capacity of checkmods */
  318. #ifdef DEBUG
  319. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  320. "MODIFY begin\n");
  321. #endif
  322. result = LDAP_SUCCESS;
  323. BEGIN
  324. int err;
  325. int argc;
  326. char **argv;
  327. char **attrName;
  328. LDAPMod **mods;
  329. LDAPMod **firstMods;
  330. LDAPMod *mod;
  331. const char *target;
  332. Slapi_DN *target_sdn = NULL;
  333. char **firstSubtree;
  334. char **subtreeDN;
  335. int subtreeCnt;
  336. int is_replicated_operation;
  337. /*
  338. * Get the arguments
  339. */
  340. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  341. if (err) { result = op_error(13); break; }
  342. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  343. if (err) { result = op_error(14); break; }
  344. /*
  345. * If this is a replication update, just be a noop.
  346. */
  347. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation);
  348. if (err) { result = op_error(16); break; }
  349. if (is_replicated_operation)
  350. {
  351. break;
  352. }
  353. err = slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &firstMods);
  354. if (err) { result = op_error(10); break; }
  355. /* Get the target DN */
  356. err = slapi_pblock_get(pb, SLAPI_MODIFY_TARGET_SDN, &target_sdn);
  357. if (err) { result = op_error(11); break; }
  358. target = slapi_sdn_get_dn(target_sdn);
  359. /*
  360. * Look for managed trees that include the target
  361. * Arguments before "," are the 7-bit clean attribute names. Arguemnts
  362. * after "," are subtreeDN's.
  363. */
  364. for ( firstSubtree = argv; strcmp(*firstSubtree, ",") != 0;
  365. firstSubtree++, argc--) {}
  366. firstSubtree++;
  367. argc--;
  368. for (attrName = argv; strcmp(*attrName, ",") != 0; attrName++ )
  369. {
  370. int modcount = 0;
  371. int ii = 0;
  372. /*
  373. * if the attribute is userpassword, check unhashed#user#password
  374. * instead. "userpassword" is encoded; it will always pass the 7bit
  375. * check.
  376. */
  377. char *attr_name;
  378. if ( strcasecmp(*attrName, "userpassword") == 0 )
  379. {
  380. attr_name = "unhashed#user#password";
  381. } else {
  382. attr_name = *attrName;
  383. }
  384. /* There may be more than one mod that matches e.g.
  385. changetype: modify
  386. delete: uid
  387. uid: balster1950
  388. -
  389. add: uid
  390. uid: scottg
  391. So, we need to first find all mods that contain the attribute
  392. which are add or replace ops and are bvalue encoded
  393. */
  394. /* find out how many mods meet this criteria */
  395. for(mods=firstMods;*mods;mods++)
  396. {
  397. mod = *mods;
  398. if ((slapi_attr_type_cmp(mod->mod_type, attr_name, 1) == 0) && /* mod contains target attr */
  399. (mod->mod_op & LDAP_MOD_BVALUES) && /* mod is bval encoded (not string val) */
  400. (mod->mod_bvalues && mod->mod_bvalues[0]) && /* mod actually contains some values */
  401. (SLAPI_IS_MOD_ADD(mod->mod_op) || /* mod is add */
  402. SLAPI_IS_MOD_REPLACE(mod->mod_op))) /* mod is replace */
  403. {
  404. addMod(&checkmods, &checkmodsCapacity, &modcount, mod);
  405. }
  406. }
  407. if (modcount == 0) {
  408. continue; /* no mods to check, go to next attr */
  409. }
  410. /*
  411. * stop checking at first mod that fails the check
  412. */
  413. for (ii = 0; (result == 0) && (ii < modcount); ++ii)
  414. {
  415. mod = checkmods[ii];
  416. /*
  417. * For each DN in the managed list, do 7-bit checking if
  418. * the target DN is a subnode in the tree.
  419. */
  420. for( subtreeDN=firstSubtree, subtreeCnt=argc ;subtreeCnt > 0;
  421. subtreeCnt--,subtreeDN++)
  422. {
  423. /*
  424. * issuffix determines whether the target is under the
  425. * subtree *subtreeDN
  426. */
  427. if (slapi_dn_issuffix(target, *subtreeDN))
  428. {
  429. #ifdef DEBUG
  430. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  431. "MODIFY subtree=%s\n", *subtreeDN);
  432. #endif
  433. /*
  434. * Check if the value is 7-bit clean
  435. */
  436. result = bit_check(NULL, mod->mod_bvalues, &violated);
  437. if (result) break;
  438. }
  439. }
  440. }
  441. /* don't have to go on if there is a value not 7-bit clean */
  442. if (result) break;
  443. }
  444. END
  445. slapi_ch_free((void **)&checkmods);
  446. if (result) {
  447. issue_error(pb, result, "MODIFY", violated);
  448. }
  449. return (result==LDAP_SUCCESS)?0:-1;
  450. }
  451. /* ------------------------------------------------------------ */
  452. /*
  453. * preop_modrdn - Pre-operation call for modify RDN
  454. *
  455. * Check that the new RDN does not include attributes that
  456. * cause a constraint violation
  457. */
  458. static int
  459. preop_modrdn(Slapi_PBlock *pb)
  460. {
  461. int result;
  462. Slapi_Entry *e;
  463. char *violated = NULL;
  464. #ifdef DEBUG
  465. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  466. "MODRDN begin\n");
  467. #endif
  468. /* Init */
  469. result = LDAP_SUCCESS;
  470. e = 0;
  471. BEGIN
  472. int err;
  473. int argc;
  474. char **argv;
  475. char **attrName;
  476. Slapi_DN *target_sdn = NULL;
  477. Slapi_DN *superior;
  478. char *rdn;
  479. Slapi_Attr *attr;
  480. char **firstSubtree;
  481. char **subtreeDN;
  482. int subtreeCnt;
  483. int is_replicated_operation;
  484. /*
  485. * Get the arguments
  486. */
  487. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  488. if (err) { result = op_error(30); break; }
  489. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  490. if (err) { result = op_error(31); break; }
  491. /*
  492. * If this is a replication update, just be a noop.
  493. */
  494. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation);
  495. if (err) { result = op_error(16); break; }
  496. if (is_replicated_operation)
  497. {
  498. break;
  499. }
  500. /* Get the DN of the entry being renamed */
  501. err = slapi_pblock_get(pb, SLAPI_MODRDN_TARGET_SDN, &target_sdn);
  502. if (err) { result = op_error(22); break; }
  503. /* Get superior value - unimplemented in 3.0 DS */
  504. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &superior);
  505. if (err) { result = op_error(20); break; }
  506. /*
  507. * No superior means the entry is just renamed at
  508. * its current level in the tree. Use the target DN for
  509. * determining which managed tree this belongs to
  510. */
  511. if (!superior) superior = target_sdn;
  512. /* Get the new RDN - this has the attribute values */
  513. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &rdn);
  514. if (err) { result = op_error(33); break; }
  515. #ifdef DEBUG
  516. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  517. "MODRDN newrdn=%s\n", rdn);
  518. #endif
  519. /*
  520. * Parse the RDN into attributes by creating a "dummy" entry
  521. * and setting the attributes from the RDN.
  522. *
  523. * The new entry must be freed.
  524. */
  525. e = slapi_entry_alloc();
  526. if (!e) { result = op_error(32); break; }
  527. /* NOTE: strdup on the rdn, since it will be freed when
  528. * the entry is freed */
  529. /* slapi_entry_set_normdn expects rdn normalized, but not decapitalized */
  530. slapi_entry_set_normdn(e, slapi_ch_strdup(rdn));
  531. err = slapi_entry_add_rdn_values(e);
  532. if (err)
  533. {
  534. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  535. "MODRDN bad rdn value=%s\n", rdn);
  536. break; /* Bad DN */
  537. }
  538. /*
  539. * arguments before "," are the 7-bit clean attribute names. Arguemnts
  540. * after "," are subtreeDN's.
  541. */
  542. for ( firstSubtree = argv; strcmp(*firstSubtree, ",") != 0;
  543. firstSubtree++, argc--) {}
  544. firstSubtree++;
  545. argc--;
  546. /*
  547. * Find out if the node is being moved into one of
  548. * the managed subtrees
  549. */
  550. for (attrName = argv; strcmp(*attrName, ",") != 0; attrName++ )
  551. {
  552. /*
  553. * If the attribut type is userpassword, do not replace it by
  554. * unhashed#user#password because unhashed#user#password does not exist
  555. * in this case.
  556. */
  557. /*
  558. * Find any 7-bit attribute data in the new RDN
  559. */
  560. err = slapi_entry_attr_find(e, *attrName, &attr);
  561. if (err) continue; /* break;*/ /* no 7-bit attribute */
  562. /*
  563. * For each DN in the managed list, do 7-bit checking if
  564. * the target DN is a subnode in the tree.
  565. */
  566. for( subtreeDN=firstSubtree, subtreeCnt=argc ;subtreeCnt > 0;
  567. subtreeCnt--,subtreeDN++)
  568. {
  569. /*
  570. * issuffix determines whether the target is under the
  571. * subtree *subtreeDN
  572. */
  573. if (slapi_dn_issuffix(slapi_sdn_get_dn(superior), *subtreeDN))
  574. {
  575. #ifdef DEBUG
  576. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  577. "MODRDN subtree=%s\n", *subtreeDN);
  578. #endif
  579. /*
  580. * Check if the value is 7-bit clean
  581. */
  582. result = bit_check(attr, NULL, &violated);
  583. if (result) break;
  584. }
  585. }
  586. /* don't have to go on if there is a value not 7-bit clean */
  587. if (result) break;
  588. }
  589. END
  590. /* Clean-up */
  591. if (e) slapi_entry_free(e);
  592. if (result) {
  593. issue_error(pb, result, "MODRDN", violated);
  594. }
  595. return (result==LDAP_SUCCESS)?0:-1;
  596. }
  597. /* ------------------------------------------------------------ */
  598. /*
  599. * Initialize the plugin
  600. *
  601. */
  602. int
  603. NS7bitAttr_Init(Slapi_PBlock *pb)
  604. {
  605. int err = 0;
  606. Slapi_Entry *plugin_entry = NULL;
  607. char *plugin_type = NULL;
  608. int preadd = SLAPI_PLUGIN_PRE_ADD_FN;
  609. int premod = SLAPI_PLUGIN_PRE_MODIFY_FN;
  610. int premdn = SLAPI_PLUGIN_PRE_MODRDN_FN;
  611. BEGIN
  612. int argc;
  613. char **argv;
  614. /* Declare plugin version */
  615. err = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  616. SLAPI_PLUGIN_VERSION_01);
  617. if (err) break;
  618. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  619. plugin_entry &&
  620. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  621. plugin_type && strstr(plugin_type, "betxn")) {
  622. preadd = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
  623. premod = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  624. premdn = SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN;
  625. }
  626. slapi_ch_free_string(&plugin_type);
  627. /*
  628. * Get and normalize arguments
  629. */
  630. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  631. if (err) break;
  632. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  633. if (err) break;
  634. /*
  635. * Arguments before "," are the 7-bit attribute names. Arguments after
  636. * "," are the subtree DN's.
  637. */
  638. if (argc < 1) { err = -1; break; }
  639. for(;strcmp(*argv, ",") != 0 && argc > 0; argc--, argv++)
  640. {};
  641. if (argc == 0) { err = -1; break; }
  642. argv++; argc--;
  643. for(;argc > 0;argc--, argv++) {
  644. char *normdn = slapi_create_dn_string_case("%s", *argv);
  645. slapi_ch_free_string(argv);
  646. *argv = normdn;
  647. }
  648. /* Provide descriptive information */
  649. err = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  650. (void*)&pluginDesc);
  651. if (err) break;
  652. /* Register functions */
  653. err = slapi_pblock_set(pb, preadd, (void*)preop_add);
  654. if (err) break;
  655. err = slapi_pblock_set(pb, premod, (void*)preop_modify);
  656. if (err) break;
  657. err = slapi_pblock_set(pb, premdn, (void*)preop_modrdn);
  658. if (err) break;
  659. END
  660. if (err) {
  661. slapi_log_error(SLAPI_LOG_PLUGIN, "NS7bitAttr_Init",
  662. "Error: %d\n", err);
  663. err = -1;
  664. }
  665. else
  666. slapi_log_error(SLAPI_LOG_PLUGIN, "NS7bitAttr_Init",
  667. "plugin loaded\n");
  668. return err;
  669. }