7bit.c 21 KB

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