7bit.c 23 KB

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