7bit.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. char *pwd = NULL;
  190. char *origpwd = NULL;
  191. #ifdef DEBUG
  192. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD begin\n");
  193. #endif
  194. result = LDAP_SUCCESS;
  195. /*
  196. * Do constraint check on the added entry. Set result.
  197. */
  198. BEGIN
  199. int err;
  200. int argc;
  201. char **argv;
  202. char **attrName;
  203. const char *dn;
  204. Slapi_DN *sdn = NULL;
  205. Slapi_Entry *e;
  206. char **firstSubtree;
  207. char **subtreeDN;
  208. int subtreeCnt;
  209. int is_replicated_operation;
  210. struct berval *vals[2];
  211. struct berval val;
  212. vals[0] = &val;
  213. vals[1] = NULL;
  214. /*
  215. * Get the arguments
  216. */
  217. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  218. if (err) { result = op_error(53); break; }
  219. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  220. if (err) { result = op_error(54); break; }
  221. /*
  222. * If this is a replication update, just be a noop.
  223. */
  224. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation);
  225. if (err) { result = op_error(56); break; }
  226. if (is_replicated_operation)
  227. {
  228. break;
  229. }
  230. /*
  231. * Get the target DN for this add operation
  232. */
  233. err = slapi_pblock_get(pb, SLAPI_ADD_TARGET_SDN, &sdn);
  234. if (err) { result = op_error(50); break; }
  235. dn = slapi_sdn_get_dn(sdn);
  236. #ifdef DEBUG
  237. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name, "ADD target=%s\n", dn);
  238. #endif
  239. /*
  240. * Get the entry data for this add. Check whether it
  241. * contains a value for the unique attribute
  242. */
  243. err = slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  244. if (err) { result = op_error(51); break; }
  245. for ( firstSubtree = argv; strcmp(*firstSubtree, ",") != 0;
  246. firstSubtree++, argc--) {}
  247. firstSubtree++;
  248. argc--;
  249. for (attrName = argv; strcmp(*attrName, ",") != 0; attrName++ )
  250. {
  251. /*
  252. * if the attribute is userpassword, check unhashed user password
  253. * instead. "userpassword" is encoded; it will always pass the 7bit
  254. * check.
  255. */
  256. char *attr_name = NULL;
  257. Slapi_Attr *attr = NULL;
  258. if ( strcasecmp(*attrName, "userpassword") == 0 )
  259. {
  260. origpwd = pwd = slapi_get_first_clear_text_pw(e);
  261. if (pwd == NULL)
  262. {
  263. continue;
  264. }
  265. val.bv_val = pwd;
  266. val.bv_len = strlen(val.bv_val);
  267. } else {
  268. attr_name = *attrName;
  269. err = slapi_entry_attr_find(e, attr_name, &attr);
  270. if (err) continue; /* break;*/ /* no 7-bit attribute */
  271. }
  272. /*
  273. * For each DN in the managed list, do 7-bit checking if
  274. * the target DN is a subnode in the tree.
  275. */
  276. for( subtreeDN=firstSubtree, subtreeCnt=argc ;subtreeCnt > 0;
  277. subtreeCnt--,subtreeDN++)
  278. {
  279. /*
  280. * issuffix determines whether the target is under the
  281. * subtree *subtreeDN
  282. */
  283. if (slapi_dn_issuffix(dn, *subtreeDN))
  284. {
  285. #ifdef DEBUG
  286. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  287. "ADD subtree=%s\n", *subtreeDN);
  288. #endif
  289. /*
  290. * Check if the value is 7-bit clean
  291. */
  292. if(pwd)
  293. {
  294. result = bit_check(attr, vals, &violated);
  295. if(!result)
  296. pwd = NULL;
  297. }
  298. else
  299. result = bit_check(attr, NULL, &violated);
  300. if (result) break;
  301. }
  302. }
  303. /* don't have to go on if there is a value not 7-bit clean */
  304. if (result) break;
  305. }
  306. END
  307. if (result) {
  308. issue_error(pb, result, "ADD", violated);
  309. }
  310. slapi_ch_free_string(&origpwd);
  311. return (result==LDAP_SUCCESS)?0:-1;
  312. }
  313. static void
  314. addMod(LDAPMod ***modary, int *capacity, int *nmods, LDAPMod *toadd)
  315. {
  316. if (*nmods == *capacity) {
  317. *capacity += 4;
  318. if (*modary) {
  319. *modary = (LDAPMod **)slapi_ch_realloc((char *)*modary, *capacity * sizeof(LDAPMod *));
  320. } else {
  321. *modary = (LDAPMod **)slapi_ch_malloc(*capacity * sizeof(LDAPMod *));
  322. }
  323. }
  324. (*modary)[*nmods] = toadd;
  325. (*nmods)++;
  326. }
  327. /* ------------------------------------------------------------ */
  328. /*
  329. * preop_modify - pre-operation plug-in for modify
  330. */
  331. static int
  332. preop_modify(Slapi_PBlock *pb)
  333. {
  334. int result;
  335. char *violated = NULL;
  336. LDAPMod **checkmods = NULL; /* holds mods to check */
  337. int checkmodsCapacity = 0; /* max capacity of checkmods */
  338. #ifdef DEBUG
  339. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  340. "MODIFY begin\n");
  341. #endif
  342. result = LDAP_SUCCESS;
  343. BEGIN
  344. int err;
  345. int argc;
  346. char **argv;
  347. char **attrName;
  348. LDAPMod **mods;
  349. LDAPMod **firstMods;
  350. LDAPMod *mod;
  351. const char *target;
  352. Slapi_DN *target_sdn = NULL;
  353. char **firstSubtree;
  354. char **subtreeDN;
  355. int subtreeCnt;
  356. int is_replicated_operation;
  357. /*
  358. * Get the arguments
  359. */
  360. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  361. if (err) { result = op_error(13); break; }
  362. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  363. if (err) { result = op_error(14); break; }
  364. /*
  365. * If this is a replication update, just be a noop.
  366. */
  367. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation);
  368. if (err) { result = op_error(16); break; }
  369. if (is_replicated_operation)
  370. {
  371. break;
  372. }
  373. err = slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &firstMods);
  374. if (err) { result = op_error(10); break; }
  375. /* Get the target DN */
  376. err = slapi_pblock_get(pb, SLAPI_MODIFY_TARGET_SDN, &target_sdn);
  377. if (err) { result = op_error(11); break; }
  378. target = slapi_sdn_get_dn(target_sdn);
  379. /*
  380. * Look for managed trees that include the target
  381. * Arguments before "," are the 7-bit clean attribute names. Arguemnts
  382. * after "," are subtreeDN's.
  383. */
  384. for ( firstSubtree = argv; strcmp(*firstSubtree, ",") != 0;
  385. firstSubtree++, argc--) {}
  386. firstSubtree++;
  387. argc--;
  388. for (attrName = argv; strcmp(*attrName, ",") != 0; attrName++ )
  389. {
  390. int modcount = 0;
  391. int ii = 0;
  392. /*
  393. * if the attribute is userpassword, check unhashed#user#password
  394. * instead. "userpassword" is encoded; it will always pass the 7bit
  395. * check.
  396. */
  397. char *attr_name;
  398. if ( strcasecmp(*attrName, "userpassword") == 0 )
  399. {
  400. attr_name = "unhashed#user#password";
  401. } else {
  402. attr_name = *attrName;
  403. }
  404. /* There may be more than one mod that matches e.g.
  405. changetype: modify
  406. delete: uid
  407. uid: balster1950
  408. -
  409. add: uid
  410. uid: scottg
  411. So, we need to first find all mods that contain the attribute
  412. which are add or replace ops and are bvalue encoded
  413. */
  414. /* find out how many mods meet this criteria */
  415. for(mods=firstMods;mods && *mods;mods++)
  416. {
  417. mod = *mods;
  418. if ((slapi_attr_type_cmp(mod->mod_type, attr_name, 1) == 0) && /* mod contains target attr */
  419. (mod->mod_op & LDAP_MOD_BVALUES) && /* mod is bval encoded (not string val) */
  420. (mod->mod_bvalues && mod->mod_bvalues[0]) && /* mod actually contains some values */
  421. (SLAPI_IS_MOD_ADD(mod->mod_op) || /* mod is add */
  422. SLAPI_IS_MOD_REPLACE(mod->mod_op))) /* mod is replace */
  423. {
  424. addMod(&checkmods, &checkmodsCapacity, &modcount, mod);
  425. }
  426. }
  427. if (modcount == 0) {
  428. continue; /* no mods to check, go to next attr */
  429. }
  430. /*
  431. * stop checking at first mod that fails the check
  432. */
  433. for (ii = 0; (result == 0) && (ii < modcount); ++ii)
  434. {
  435. mod = checkmods[ii];
  436. /*
  437. * For each DN in the managed list, do 7-bit checking if
  438. * the target DN is a subnode in the tree.
  439. */
  440. for( subtreeDN=firstSubtree, subtreeCnt=argc ;subtreeCnt > 0;
  441. subtreeCnt--,subtreeDN++)
  442. {
  443. /*
  444. * issuffix determines whether the target is under the
  445. * subtree *subtreeDN
  446. */
  447. if (slapi_dn_issuffix(target, *subtreeDN))
  448. {
  449. #ifdef DEBUG
  450. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  451. "MODIFY subtree=%s\n", *subtreeDN);
  452. #endif
  453. /*
  454. * Check if the value is 7-bit clean
  455. */
  456. result = bit_check(NULL, mod->mod_bvalues, &violated);
  457. if (result) break;
  458. }
  459. }
  460. }
  461. /* don't have to go on if there is a value not 7-bit clean */
  462. if (result) break;
  463. }
  464. END
  465. slapi_ch_free((void **)&checkmods);
  466. if (result) {
  467. issue_error(pb, result, "MODIFY", violated);
  468. }
  469. return (result==LDAP_SUCCESS)?0:-1;
  470. }
  471. /* ------------------------------------------------------------ */
  472. /*
  473. * preop_modrdn - Pre-operation call for modify RDN
  474. *
  475. * Check that the new RDN does not include attributes that
  476. * cause a constraint violation
  477. */
  478. static int
  479. preop_modrdn(Slapi_PBlock *pb)
  480. {
  481. int result;
  482. Slapi_Entry *e;
  483. char *violated = NULL;
  484. #ifdef DEBUG
  485. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  486. "MODRDN begin\n");
  487. #endif
  488. /* Init */
  489. result = LDAP_SUCCESS;
  490. e = 0;
  491. BEGIN
  492. int err;
  493. int argc;
  494. char **argv;
  495. char **attrName;
  496. Slapi_DN *target_sdn = NULL;
  497. Slapi_DN *superior = NULL;
  498. char *rdn;
  499. Slapi_Attr *attr;
  500. char **firstSubtree;
  501. char **subtreeDN;
  502. int subtreeCnt;
  503. int is_replicated_operation;
  504. /*
  505. * Get the arguments
  506. */
  507. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  508. if (err) { result = op_error(30); break; }
  509. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  510. if (err) { result = op_error(31); break; }
  511. /*
  512. * If this is a replication update, just be a noop.
  513. */
  514. err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation);
  515. if (err) { result = op_error(16); break; }
  516. if (is_replicated_operation)
  517. {
  518. break;
  519. }
  520. /* Get the DN of the entry being renamed */
  521. err = slapi_pblock_get(pb, SLAPI_MODRDN_TARGET_SDN, &target_sdn);
  522. if (err) { result = op_error(22); break; }
  523. /* Get superior value - unimplemented in 3.0 DS */
  524. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &superior);
  525. if (err) { result = op_error(20); break; }
  526. /*
  527. * No superior means the entry is just renamed at
  528. * its current level in the tree. Use the target DN for
  529. * determining which managed tree this belongs to
  530. */
  531. if (!slapi_sdn_get_dn(superior)) superior = target_sdn;
  532. /* Get the new RDN - this has the attribute values */
  533. err = slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &rdn);
  534. if (err) { result = op_error(33); break; }
  535. #ifdef DEBUG
  536. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  537. "MODRDN newrdn=%s\n", rdn);
  538. #endif
  539. /*
  540. * Parse the RDN into attributes by creating a "dummy" entry
  541. * and setting the attributes from the RDN.
  542. *
  543. * The new entry must be freed.
  544. */
  545. e = slapi_entry_alloc();
  546. if (!e) { result = op_error(32); break; }
  547. /* NOTE: strdup on the rdn, since it will be freed when
  548. * the entry is freed */
  549. /* slapi_entry_set_normdn expects rdn normalized, but not decapitalized */
  550. slapi_entry_set_normdn(e, slapi_ch_strdup(rdn));
  551. err = slapi_entry_add_rdn_values(e);
  552. if (err)
  553. {
  554. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  555. "MODRDN bad rdn value=%s\n", rdn);
  556. break; /* Bad DN */
  557. }
  558. /*
  559. * arguments before "," are the 7-bit clean attribute names. Arguments
  560. * after "," are subtreeDN's.
  561. */
  562. for ( firstSubtree = argv; strcmp(*firstSubtree, ",") != 0;
  563. firstSubtree++, argc--) {}
  564. firstSubtree++;
  565. argc--;
  566. /*
  567. * Find out if the node is being moved into one of
  568. * the managed subtrees
  569. */
  570. for (attrName = argv; strcmp(*attrName, ",") != 0; attrName++ )
  571. {
  572. /*
  573. * If the attribute type is userpassword, do not replace it by
  574. * unhashed#user#password because unhashed#user#password does not exist
  575. * in this case.
  576. */
  577. /*
  578. * Find any 7-bit attribute data in the new RDN
  579. */
  580. err = slapi_entry_attr_find(e, *attrName, &attr);
  581. if (err) continue; /* break;*/ /* no 7-bit attribute */
  582. /*
  583. * For each DN in the managed list, do 7-bit checking if
  584. * the target DN is a subnode in the tree.
  585. */
  586. for( subtreeDN=firstSubtree, subtreeCnt=argc ;subtreeCnt > 0;
  587. subtreeCnt--,subtreeDN++)
  588. {
  589. /*
  590. * issuffix determines whether the target is under the
  591. * subtree *subtreeDN
  592. */
  593. if (slapi_dn_issuffix(slapi_sdn_get_dn(superior), *subtreeDN))
  594. {
  595. #ifdef DEBUG
  596. slapi_log_error(SLAPI_LOG_PLUGIN, plugin_name,
  597. "MODRDN subtree=%s\n", *subtreeDN);
  598. #endif
  599. /*
  600. * Check if the value is 7-bit clean
  601. */
  602. result = bit_check(attr, NULL, &violated);
  603. if (result) break;
  604. }
  605. }
  606. /* don't have to go on if there is a value not 7-bit clean */
  607. if (result) break;
  608. }
  609. END
  610. /* Clean-up */
  611. if (e) slapi_entry_free(e);
  612. if (result) {
  613. issue_error(pb, result, "MODRDN", violated);
  614. }
  615. return (result==LDAP_SUCCESS)?0:-1;
  616. }
  617. /* ------------------------------------------------------------ */
  618. /*
  619. * Initialize the plugin
  620. *
  621. */
  622. int
  623. NS7bitAttr_Init(Slapi_PBlock *pb)
  624. {
  625. int err = 0;
  626. Slapi_Entry *plugin_entry = NULL;
  627. char *plugin_type = NULL;
  628. int preadd = SLAPI_PLUGIN_PRE_ADD_FN;
  629. int premod = SLAPI_PLUGIN_PRE_MODIFY_FN;
  630. int premdn = SLAPI_PLUGIN_PRE_MODRDN_FN;
  631. BEGIN
  632. int attr_count = 0;
  633. int argc;
  634. char **argv;
  635. int valid_suffix = 0;
  636. /* Declare plugin version */
  637. err = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  638. SLAPI_PLUGIN_VERSION_01);
  639. if (err) break;
  640. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  641. plugin_entry &&
  642. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  643. plugin_type && strstr(plugin_type, "betxn")) {
  644. preadd = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
  645. premod = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  646. premdn = SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN;
  647. }
  648. slapi_ch_free_string(&plugin_type);
  649. /*
  650. * Get and normalize arguments
  651. */
  652. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
  653. if (err) break;
  654. err = slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv);
  655. if (err) break;
  656. /*
  657. * Arguments before "," are the 7-bit attribute names. Arguments after
  658. * "," are the subtree DN's.
  659. */
  660. if (argc < 1) { err = -2; break; } /* missing arguments */
  661. for(;*argv && strcmp(*argv, ",") != 0 && argc > 0; attr_count++, argc--, argv++);
  662. if (argc == 0) { err = -3; break; } /* no comma separator */
  663. if(attr_count == 0){ err = -4; break; } /* no attributes */
  664. argv++; argc--;
  665. if(argc == 0){ err = -5; break; } /* no suffix */
  666. for(;argc > 0;argc--, argv++) {
  667. err = slapi_dn_syntax_check(pb, *argv, 1);
  668. if (err) {
  669. slapi_log_error(SLAPI_LOG_FATAL, "NS7bitAttr_Init",
  670. "Invalid suffix: %s\n", *argv);
  671. continue;
  672. }
  673. if (!valid_suffix)
  674. valid_suffix = 1;
  675. char *normdn = slapi_create_dn_string_case("%s", *argv);
  676. slapi_ch_free_string(argv);
  677. *argv = normdn;
  678. }
  679. if (!valid_suffix) { err = -6; break; } /* Invalid suffix list */
  680. /* Provide descriptive information */
  681. err = slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  682. (void*)&pluginDesc);
  683. if (err) break;
  684. /* Register functions */
  685. err = slapi_pblock_set(pb, preadd, (void*)preop_add);
  686. if (err) break;
  687. err = slapi_pblock_set(pb, premod, (void*)preop_modify);
  688. if (err) break;
  689. err = slapi_pblock_set(pb, premdn, (void*)preop_modrdn);
  690. if (err) break;
  691. END
  692. if (err) {
  693. if(err == -1){
  694. slapi_log_error(SLAPI_LOG_PLUGIN, "NS7bitAttr_Init","Error: %d\n", err);
  695. } else if(err == -2){
  696. slapi_log_error(SLAPI_LOG_FATAL, "NS7bitAttr_Init",
  697. "Invalid plugin arguments - missing arguments\n");
  698. } else if(err == -3){
  699. slapi_log_error(SLAPI_LOG_FATAL, "NS7bitAttr_Init",
  700. "Invalid plugin arguments - missing \",\" separator argument\n");
  701. } else if(err == -4){
  702. slapi_log_error(SLAPI_LOG_FATAL, "NS7bitAttr_Init",
  703. "Invalid plugin arguments - missing attributes\n");
  704. } else if(err == -5){
  705. slapi_log_error(SLAPI_LOG_FATAL, "NS7bitAttr_Init",
  706. "Invalid plugin arguments - missing suffix\n");
  707. } else if(err == -6){
  708. slapi_log_error(SLAPI_LOG_FATAL, "NS7bitAttr_Init",
  709. "Invalid plugin arguments - Invalid suffix list\n");
  710. }
  711. err = -1;
  712. }
  713. else
  714. slapi_log_error(SLAPI_LOG_PLUGIN, "NS7bitAttr_Init",
  715. "plugin loaded\n");
  716. return err;
  717. }