deref.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2009 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. /*
  12. * Dereference plug-in
  13. */
  14. #include <string.h>
  15. #include "deref.h"
  16. #include <nspr.h>
  17. #ifndef DN_SYNTAX_OID
  18. #define DN_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.12"
  19. #endif
  20. #ifndef NAME_AND_OPTIONAL_UID_SYNTAX_OID
  21. #define NAME_AND_OPTIONAL_UID_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.34"
  22. #endif
  23. /*
  24. * Plug-in globals
  25. */
  26. static void *_PluginID = NULL;
  27. static char *_PluginDN = NULL;
  28. static Slapi_PluginDesc pdesc = { DEREF_FEATURE_DESC,
  29. VENDOR,
  30. DS_PACKAGE_VERSION,
  31. DEREF_PLUGIN_DESC };
  32. /*
  33. * Plug-in management functions
  34. */
  35. int deref_init(Slapi_PBlock * pb);
  36. static int deref_start(Slapi_PBlock * pb);
  37. static int deref_close(Slapi_PBlock * pb);
  38. /*
  39. * Operation callbacks (where the real work is done)
  40. */
  41. static int deref_pre_search(Slapi_PBlock * pb);
  42. static int deref_pre_entry(Slapi_PBlock *pb);
  43. typedef struct _DerefSpec {
  44. char *derefattr; /* attribute to deref - must have DN syntax */
  45. char **attrs; /* attributes to return from dereferenced entry */
  46. } DerefSpec;
  47. static DerefSpec*
  48. new_DerefSpec(char *derefattr, char **attrs) {
  49. DerefSpec *spec = (DerefSpec *)slapi_ch_calloc(1, sizeof(DerefSpec));
  50. spec->derefattr = derefattr;
  51. spec->attrs = attrs;
  52. return spec;
  53. }
  54. static void
  55. delete_DerefSpec(DerefSpec **spec)
  56. {
  57. if (spec && *spec) {
  58. slapi_ch_free_string(&((*spec)->derefattr));
  59. slapi_ch_array_free((*spec)->attrs);
  60. slapi_ch_free((void **)spec);
  61. }
  62. }
  63. typedef struct _DerefSpecList {
  64. DerefSpec **list;
  65. int count;
  66. } DerefSpecList;
  67. static DerefSpecList*
  68. new_DerefSpecList(void) {
  69. DerefSpecList *speclist = (DerefSpecList *)slapi_ch_calloc(1, sizeof(DerefSpecList));
  70. return speclist;
  71. }
  72. static void
  73. delete_DerefSpecList(DerefSpecList **speclist)
  74. {
  75. if (speclist && *speclist) {
  76. int ii;
  77. for (ii = 0; ii < (*speclist)->count; ++ii) {
  78. delete_DerefSpec(&((*speclist)->list[ii]));
  79. }
  80. slapi_ch_free((void **)&((*speclist)->list));
  81. slapi_ch_free((void **)speclist);
  82. }
  83. }
  84. static int deref_register_operation_extension(void);
  85. static int deref_extension_type;
  86. static int deref_extension_handle;
  87. static const DerefSpecList *
  88. deref_get_operation_extension(Slapi_PBlock *pb)
  89. {
  90. Slapi_Operation *op;
  91. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  92. return (const DerefSpecList *)slapi_get_object_extension(deref_extension_type,
  93. op, deref_extension_handle);
  94. }
  95. static void
  96. deref_set_operation_extension(Slapi_PBlock *pb, DerefSpecList *speclist)
  97. {
  98. Slapi_Operation *op;
  99. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  100. slapi_set_object_extension(deref_extension_type, op,
  101. deref_extension_handle, (void *)speclist);
  102. }
  103. /*
  104. * Plugin identity functions
  105. */
  106. void
  107. deref_set_plugin_id(void *pluginID)
  108. {
  109. _PluginID = pluginID;
  110. }
  111. void *
  112. deref_get_plugin_id(void)
  113. {
  114. return _PluginID;
  115. }
  116. void
  117. deref_set_plugin_dn(char *pluginDN)
  118. {
  119. _PluginDN = pluginDN;
  120. }
  121. char *
  122. deref_get_plugin_dn(void)
  123. {
  124. return _PluginDN;
  125. }
  126. /*
  127. * Plug-in initialization functions
  128. */
  129. int
  130. deref_init(Slapi_PBlock *pb)
  131. {
  132. int status = 0;
  133. char *plugin_identity = NULL;
  134. slapi_log_error(SLAPI_LOG_TRACE, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  135. "--> deref_init\n");
  136. /* Store the plugin identity for later use.
  137. * Used for internal operations. */
  138. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  139. PR_ASSERT(plugin_identity);
  140. deref_set_plugin_id(plugin_identity);
  141. /* Register callbacks */
  142. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  143. SLAPI_PLUGIN_VERSION_01) != 0 ||
  144. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  145. (void *) deref_start) != 0 ||
  146. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  147. (void *) deref_close) != 0 ||
  148. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  149. (void *) &pdesc) != 0 ||
  150. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_SEARCH_FN,
  151. (void *) deref_pre_search) != 0 ||
  152. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ENTRY_FN,
  153. (void *) deref_pre_entry) != 0 ||
  154. deref_register_operation_extension()
  155. ) {
  156. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, DEREF_PLUGIN_SUBSYSTEM,
  157. "deref_init: failed to register plugin\n");
  158. status = -1;
  159. }
  160. if (status == 0) {
  161. slapi_register_supported_control(LDAP_CONTROL_X_DEREF, SLAPI_OPERATION_SEARCH);
  162. }
  163. slapi_log_error(SLAPI_LOG_TRACE, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  164. "<-- deref_init\n");
  165. return status;
  166. }
  167. /*
  168. * deref_start()
  169. *
  170. * Creates config lock and loads config cache.
  171. */
  172. static int
  173. deref_start(Slapi_PBlock * pb)
  174. {
  175. slapi_log_error(SLAPI_LOG_TRACE, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  176. "--> deref_start\n");
  177. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  178. "linked attributes plug-in: ready for service\n");
  179. slapi_log_error(SLAPI_LOG_TRACE, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  180. "<-- deref_start\n");
  181. return 0;
  182. }
  183. /*
  184. * deref_close()
  185. *
  186. * Cleans up the config cache.
  187. */
  188. static int
  189. deref_close(Slapi_PBlock * pb)
  190. {
  191. slapi_log_error(SLAPI_LOG_TRACE, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  192. "--> deref_close\n");
  193. slapi_log_error(SLAPI_LOG_TRACE, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  194. "<-- deref_close\n");
  195. return 0;
  196. }
  197. static int
  198. deref_attr_in_spec_list(DerefSpecList *speclist, const char *derefattr)
  199. {
  200. int ii;
  201. int ret = 0;
  202. PR_ASSERT(speclist && derefattr);
  203. for (ii = 0; !ret && (ii < speclist->count); ++ii) {
  204. if (slapi_attr_types_equivalent(derefattr, speclist->list[ii]->derefattr)) {
  205. ret = 1; /* match */
  206. }
  207. }
  208. return ret;
  209. }
  210. /* grows spec list, consumes spec */
  211. static void
  212. deref_add_spec_to_list(DerefSpecList *speclist, DerefSpec *spec)
  213. {
  214. PR_ASSERT(speclist && spec);
  215. speclist->count++;
  216. speclist->list = (DerefSpec **)slapi_ch_realloc((char *)speclist->list,
  217. speclist->count * sizeof(DerefSpec *));
  218. speclist->list[speclist->count-1] = spec; /* consumed */
  219. }
  220. static const DerefSpec *
  221. deref_get_next_spec(const DerefSpecList *speclist, int *ii)
  222. {
  223. const DerefSpec *spec = NULL;
  224. if (*ii < speclist->count) {
  225. spec = speclist->list[*ii];
  226. (*ii)++;
  227. }
  228. return spec;
  229. }
  230. static const DerefSpec *
  231. deref_get_first_spec(const DerefSpecList *speclist, int *ii)
  232. {
  233. *ii = 0;
  234. return deref_get_next_spec(speclist, ii);
  235. }
  236. static int
  237. deref_check_for_dn_syntax(const char *derefattr)
  238. {
  239. int ret = 0;
  240. Slapi_Attr *attr = slapi_attr_new();
  241. if (attr) {
  242. char *oid = NULL;
  243. slapi_attr_init(attr, derefattr);
  244. slapi_attr_get_syntax_oid_copy(attr, &oid);
  245. ret = oid && (!strcmp(oid, DN_SYNTAX_OID) || !strcmp(oid, NAME_AND_OPTIONAL_UID_SYNTAX_OID));
  246. slapi_ch_free_string(&oid);
  247. slapi_attr_free(&attr);
  248. }
  249. return ret;
  250. }
  251. static void
  252. deref_add_spec(DerefSpecList *speclist, char **derefattr, char ***attrs, int critical, int *ldapcode, const char **ldaperrtext)
  253. {
  254. PR_ASSERT(speclist && derefattr && attrs && ldapcode && ldaperrtext);
  255. if (!deref_check_for_dn_syntax(*derefattr)) { /* derefattr must have DN syntax */
  256. if (critical) {
  257. /* not DN syntax */
  258. *ldapcode = LDAP_PROTOCOL_ERROR;
  259. *ldaperrtext = "A dereference attribute must have DN syntax";
  260. }
  261. } else if (deref_attr_in_spec_list(speclist, *derefattr)) {
  262. /* duplicate */
  263. *ldapcode = LDAP_PROTOCOL_ERROR;
  264. *ldaperrtext = "A dereference attribute was specified more than once in a dereference specification";
  265. } else {
  266. DerefSpec *spec = new_DerefSpec(*derefattr, *attrs);
  267. *derefattr = NULL; /* consumed */
  268. *attrs = NULL; /* consumed */
  269. deref_add_spec_to_list(speclist, spec); /* consumes spec */
  270. }
  271. return;
  272. }
  273. /*
  274. controlValue ::= SEQUENCE OF derefSpec DerefSpec
  275. DerefSpec ::= SEQUENCE {
  276. derefAttr attributeDescription, ; with DN syntax
  277. attributes AttributeList }
  278. AttributeList ::= SEQUENCE OF attr AttributeDescription
  279. Each derefSpec.derefAttr MUST be unique within controlValue.
  280. */
  281. static void
  282. deref_parse_ctrl_value(DerefSpecList *speclist, const struct berval *ctrlbv, int critical, int *ldapcode, const char **ldaperrtext)
  283. {
  284. BerElement *ber = NULL;
  285. ber_tag_t tag;
  286. ber_len_t len = -1;
  287. char *last;
  288. PR_ASSERT(ctrlbv && ctrlbv->bv_val && ctrlbv->bv_len && ldapcode && ldaperrtext);
  289. if (!BV_HAS_DATA(ctrlbv)) {
  290. *ldapcode = LDAP_PROTOCOL_ERROR;
  291. *ldaperrtext = "Empty deref control value";
  292. return;
  293. }
  294. ber = ber_init((struct berval *)ctrlbv);
  295. for (tag = ber_first_element(ber, &len, &last);
  296. (tag != LBER_ERROR) && (tag != LBER_END_OF_SEQORSET);
  297. tag = ber_next_element(ber, &len, last)) {
  298. char *derefattr = NULL;
  299. char **attrs = NULL;
  300. len = -1; /* reset */
  301. if ((LBER_ERROR == ber_scanf(ber, "{a{v}}", &derefattr, &attrs)) ||
  302. !derefattr || !attrs || !attrs[0]){
  303. if (critical)
  304. *ldapcode = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  305. else
  306. *ldapcode = LDAP_PROTOCOL_ERROR;
  307. if (!derefattr) {
  308. *ldaperrtext = "Missing dereference attribute name";
  309. } else {
  310. *ldaperrtext = "Missing list of attributes to dereference";
  311. }
  312. } else {
  313. /* will consume derefattr and attrs and set them to NULL if successful */
  314. deref_add_spec(speclist, &derefattr, &attrs, critical, ldapcode, ldaperrtext);
  315. }
  316. if (derefattr) { /* not consumed - error */
  317. slapi_ch_free_string(&derefattr);
  318. }
  319. if (attrs) { /* not consumed - error */
  320. slapi_ch_array_free(attrs);
  321. }
  322. }
  323. ber_free(ber, 1);
  324. }
  325. static int
  326. deref_incompatible_ctrl(const char *oid)
  327. {
  328. return 0; /* no known incompatible ctrls yet */
  329. }
  330. /*
  331. * Operation callback functions
  332. */
  333. /*
  334. * deref_pre_search()
  335. *
  336. * see if the dereference control has been specified
  337. * if so, parse it, and check to make sure it meets the
  338. * protocol specification (no duplicate attributes, etc.)
  339. */
  340. static int
  341. deref_pre_search(Slapi_PBlock *pb)
  342. {
  343. int ldapcode = LDAP_SUCCESS;
  344. const LDAPControl **reqctrls = NULL;
  345. const LDAPControl *derefctrl = NULL;
  346. const char *ldaperrtext = "Unknown error";
  347. const char *incompatible = NULL;
  348. DerefSpecList *speclist = NULL;
  349. int ii;
  350. int iscritical = 0;
  351. slapi_log_error(SLAPI_LOG_TRACE, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  352. "--> deref_pre_search\n");
  353. /* see if the deref request control is in the list of
  354. controls - if so, parse and validate it */
  355. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &reqctrls);
  356. for (ii = 0; (ldapcode == LDAP_SUCCESS) && reqctrls && reqctrls[ii]; ++ii) {
  357. const LDAPControl *ctrl = reqctrls[ii];
  358. if (!strcmp(ctrl->ldctl_oid, LDAP_CONTROL_X_DEREF)) {
  359. if (derefctrl) { /* already specified */
  360. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, DEREF_PLUGIN_SUBSYSTEM,
  361. "The dereference control was specified more than once - it must be specified only once in the search request\n");
  362. ldapcode = LDAP_PROTOCOL_ERROR;
  363. ldaperrtext = "The dereference control cannot be specified more than once";
  364. derefctrl = NULL;
  365. } else if (!ctrl->ldctl_value.bv_len) {
  366. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, DEREF_PLUGIN_SUBSYSTEM,
  367. "No control value specified for dereference control\n");
  368. ldapcode = LDAP_PROTOCOL_ERROR;
  369. ldaperrtext = "The dereference control must have a value";
  370. iscritical = ctrl->ldctl_iscritical;
  371. } else if (!ctrl->ldctl_value.bv_val) {
  372. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, DEREF_PLUGIN_SUBSYSTEM,
  373. "No control value specified for dereference control\n");
  374. ldapcode = LDAP_PROTOCOL_ERROR;
  375. ldaperrtext = "The dereference control must have a value";
  376. iscritical = ctrl->ldctl_iscritical;
  377. } else if (!ctrl->ldctl_value.bv_val[0] || !ctrl->ldctl_value.bv_len) {
  378. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, DEREF_PLUGIN_SUBSYSTEM,
  379. "Empty control value specified for dereference control\n");
  380. ldapcode = LDAP_PROTOCOL_ERROR;
  381. ldaperrtext = "The dereference control must have a non-empty value";
  382. iscritical = ctrl->ldctl_iscritical;
  383. } else {
  384. derefctrl = ctrl;
  385. iscritical = ctrl->ldctl_iscritical;
  386. }
  387. } else if (deref_incompatible_ctrl(ctrl->ldctl_oid)) {
  388. incompatible = ctrl->ldctl_oid;
  389. }
  390. }
  391. if (derefctrl && incompatible) {
  392. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, DEREF_PLUGIN_SUBSYSTEM,
  393. "Cannot use the dereference control and control [%s] for the same search operation\n",
  394. incompatible);
  395. /* not sure if this is a hard failure - the current spec says:
  396. The semantics of the criticality field are specified in [RFC4511].
  397. In detail, the criticality of the control determines whether the
  398. control will or will not be used, and if it will not be used, whether
  399. the operation will continue without returning the control in the
  400. response, or fail, returning unavailableCriticalExtension. If the
  401. control is appropriate for an operation and, for any reason, it
  402. cannot be applied in its entirety to a single SearchResultEntry
  403. response, it MUST NOT be applied to that specific SearchResultEntry
  404. response, without affecting its application to any subsequent
  405. SearchResultEntry response.
  406. */
  407. /* so for now, just return LDAP_SUCCESS and don't do anything else */
  408. derefctrl = NULL;
  409. }
  410. /* we have something now - see if it is a valid control */
  411. if (derefctrl) {
  412. speclist = new_DerefSpecList();
  413. deref_parse_ctrl_value(speclist, &derefctrl->ldctl_value, derefctrl->ldctl_iscritical,
  414. &ldapcode, &ldaperrtext);
  415. }
  416. if (ldapcode != LDAP_SUCCESS) {
  417. if (iscritical) {
  418. ldapcode = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  419. }
  420. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &ldapcode);
  421. slapi_send_ldap_result(pb, ldapcode, NULL, (char *)ldaperrtext, 0, NULL);
  422. delete_DerefSpecList(&speclist);
  423. } else {
  424. /* save spec list away for the pre_entry callback */
  425. deref_set_operation_extension(pb, speclist);
  426. }
  427. slapi_log_error(SLAPI_LOG_TRACE, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  428. "<-- deref_pre_op\n");
  429. return ldapcode;
  430. }
  431. /*
  432. See if the client has the requested rights over the entry and the specified
  433. attributes. Each attribute in attrs will be tested. The retattrs array will
  434. hold the attributes that could be read. If NULL, this means the entry is
  435. not allowed, or none of the requested attributes are allowed. If non-NULL, this
  436. array can be passed to a subsequent search operation.
  437. */
  438. static int
  439. deref_check_access(Slapi_PBlock *pb, const Slapi_Entry *ent, const char *entdn,
  440. const char **attrs, char ***retattrs, int rights)
  441. {
  442. Slapi_Entry *etest = NULL;
  443. const char *attrname;
  444. /* if there is no entry, create a dummy entry - this can save a search
  445. on an entry we should not be allowed to search */
  446. if (!ent) {
  447. etest = slapi_entry_alloc();
  448. slapi_sdn_set_dn_byref(slapi_entry_get_sdn(etest), entdn);
  449. } else {
  450. etest = (Slapi_Entry *)ent;
  451. }
  452. *retattrs = NULL;
  453. for (attrname = *attrs; attrname; attrname = *(++attrs)) {
  454. /* first, check access control - see if client can read the requested attribute */
  455. int ret = slapi_access_allowed(pb, etest, (char *)attrname, NULL, rights);
  456. if (ret != LDAP_SUCCESS) {
  457. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  458. "The client does not have permission to read attribute %s in entry %s\n",
  459. attrname, slapi_entry_get_dn_const(etest));
  460. } else {
  461. slapi_ch_array_add(retattrs, (char *)attrname); /* retattrs and attrs share pointer to attrname */
  462. }
  463. }
  464. if (ent != etest) {
  465. slapi_entry_free(etest);
  466. }
  467. /* see if we had at least one attribute that could be accessed */
  468. return *retattrs == NULL ? LDAP_INSUFFICIENT_ACCESS : LDAP_SUCCESS;
  469. }
  470. /*
  471. must check access before calling this function
  472. */
  473. static int
  474. deref_get_values(const Slapi_Entry *ent, const char *attrname,
  475. Slapi_ValueSet** results, int *type_name_disposition,
  476. char** actual_type_name, int flags, int *buffer_flags)
  477. {
  478. int ret = slapi_vattr_values_get((Slapi_Entry *)ent, (char *)attrname, results, type_name_disposition,
  479. actual_type_name, flags, buffer_flags);
  480. return ret;
  481. }
  482. static void
  483. deref_values_free(Slapi_ValueSet** results, char** actual_type_name, int buffer_flags)
  484. {
  485. slapi_vattr_values_free(results, actual_type_name, buffer_flags);
  486. }
  487. static void
  488. deref_do_deref_attr(Slapi_PBlock *pb, BerElement *ctrlber, const char *derefdn, const char *derefattr, const char **attrs)
  489. {
  490. char **retattrs = NULL;
  491. Slapi_PBlock *derefpb = NULL;
  492. Slapi_Entry **entries = NULL;
  493. int rc;
  494. /* If the access check on the attributes is done without retrieveing the entry
  495. * it cannot handle acis which need teh entry, eg to apply a targetfilter rule
  496. * So the determination of attrs which can be dereferenced is delayed
  497. */
  498. derefpb = slapi_pblock_new();
  499. slapi_search_internal_set_pb(derefpb, derefdn, LDAP_SCOPE_BASE,
  500. "(objectclass=*)", retattrs, 0,
  501. NULL, NULL, deref_get_plugin_id(), 0);
  502. slapi_search_internal_pb(derefpb);
  503. slapi_pblock_get(derefpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  504. if (LDAP_SUCCESS == rc) {
  505. slapi_pblock_get(derefpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  506. if (entries) {
  507. if (entries[1]) {
  508. /* too many entries */
  509. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  510. "More than one entry matching DN [%s]\n",
  511. derefdn);
  512. } else {
  513. int ii;
  514. int needattrvals = 1; /* need attrvals sequence? */
  515. if (deref_check_access(pb, entries[0], derefdn, attrs, &retattrs,
  516. (SLAPI_ACL_SEARCH|SLAPI_ACL_READ))) {
  517. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  518. "The client does not have permission to read the requested "
  519. "attributes in entry %s\n", derefdn);
  520. } else {
  521. ber_printf(ctrlber, "{ss", derefattr, derefdn); /* begin DerefRes + derefAttr + derefVal */
  522. for (ii = 0; retattrs[ii]; ++ii) {
  523. Slapi_Value *sv;
  524. int idx = 0;
  525. Slapi_ValueSet* results = NULL;
  526. int type_name_disposition = 0;
  527. char* actual_type_name = NULL;
  528. int flags = 0;
  529. int buffer_flags = 0;
  530. int needpartialattr = 1; /* need PartialAttribute sequence? */
  531. int needvalsset = 1;
  532. #if defined(USE_OLD_UNHASHED)
  533. if (is_type_forbidden(retattrs[ii])) {
  534. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  535. "skip forbidden attribute [%s]\n", derefdn);
  536. continue;
  537. }
  538. #endif
  539. deref_get_values(entries[0], retattrs[ii], &results, &type_name_disposition,
  540. &actual_type_name, flags, &buffer_flags);
  541. if (results) {
  542. idx = slapi_valueset_first_value(results, &sv);
  543. }
  544. for (; results && sv; idx = slapi_valueset_next_value(results, idx, &sv)) {
  545. const struct berval *bv = slapi_value_get_berval(sv);
  546. if (needattrvals) {
  547. /* we have at least one attribute with values in
  548. DerefRes.attrVals */
  549. /* attrVals is OPTIONAL - only added if there are
  550. any values to send */
  551. ber_printf(ctrlber, "t{", (LBER_CLASS_CONTEXT|LBER_CONSTRUCTED));
  552. needattrvals = 0;
  553. }
  554. if (needpartialattr) {
  555. /* This attribute in attrVals has values */
  556. ber_printf(ctrlber, "{s", retattrs[ii]);
  557. needpartialattr = 0;
  558. }
  559. if (needvalsset) {
  560. /* begin the vals SET of values for this attribute */
  561. ber_printf(ctrlber, "[");
  562. needvalsset = 0;
  563. }
  564. ber_printf(ctrlber, "O", bv);
  565. } /* for each value in retattrs[ii] */
  566. deref_values_free(&results, &actual_type_name, buffer_flags);
  567. if (needvalsset == 0) {
  568. ber_printf(ctrlber, "]");
  569. }
  570. if (needpartialattr == 0) {
  571. ber_printf(ctrlber, "}");
  572. }
  573. } /* for each attr in retattrs */
  574. if (needattrvals == 0) {
  575. ber_printf(ctrlber, "}");
  576. }
  577. ber_printf(ctrlber, "}"); /* end DerefRes */
  578. }
  579. }
  580. } else { /* nothing */
  581. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  582. "No entries matching [%s]\n", derefdn);
  583. }
  584. } else {
  585. /* handle error */
  586. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  587. "Could not read entry with DN [%s]: error %d:%s\n",
  588. derefdn, rc, ldap_err2string(rc));
  589. }
  590. slapi_free_search_results_internal(derefpb);
  591. slapi_pblock_destroy(derefpb);
  592. slapi_ch_free((void **)&retattrs); /* retattrs does not own the strings */
  593. }
  594. static int
  595. deref_pre_entry(Slapi_PBlock *pb)
  596. {
  597. int ii = 0;
  598. const DerefSpec *spec;
  599. const Slapi_Entry *ent = NULL;
  600. const DerefSpecList *speclist = deref_get_operation_extension(pb);
  601. BerElement *ctrlber = NULL;
  602. LDAPControl *ctrl = NULL;
  603. const LDAPControl **searchctrls = NULL;
  604. LDAPControl **newsearchctrls = NULL;
  605. if (!speclist) {
  606. return 0; /* nothing to do */
  607. }
  608. ctrlber = ber_alloc();
  609. ber_printf(ctrlber, "{"); /* begin control value */
  610. slapi_pblock_get(pb, SLAPI_SEARCH_ENTRY_ORIG, &ent);
  611. for (spec = deref_get_first_spec(speclist, &ii); ent && spec;
  612. spec = deref_get_next_spec(speclist, &ii)) {
  613. Slapi_Value *sv;
  614. int idx = 0;
  615. Slapi_ValueSet* results = NULL;
  616. int type_name_disposition = 0;
  617. char* actual_type_name = NULL;
  618. int flags = 0;
  619. int buffer_flags = 0;
  620. const char *attrs[2];
  621. char **retattrs = NULL;
  622. attrs[0] = spec->derefattr;
  623. attrs[1] = NULL;
  624. if (deref_check_access(pb, ent, NULL, attrs, &retattrs,
  625. (SLAPI_ACL_SEARCH|SLAPI_ACL_READ))) {
  626. slapi_log_error(SLAPI_LOG_PLUGIN, LOG_DEBUG, DEREF_PLUGIN_SUBSYSTEM,
  627. "The client does not have permission to read attribute %s in entry %s\n",
  628. spec->derefattr, slapi_entry_get_dn_const(ent));
  629. continue;
  630. }
  631. slapi_ch_free((void **)&retattrs); /* retattrs does not own strings */
  632. deref_get_values(ent, spec->derefattr, &results, &type_name_disposition,
  633. &actual_type_name, flags, &buffer_flags);
  634. /* spec->derefattr is DN valued attr - results will be a list of DNs */
  635. if (results) {
  636. idx = slapi_valueset_first_value(results, &sv);
  637. }
  638. for (; results && sv; idx = slapi_valueset_next_value(results, idx, &sv)) {
  639. const char *derefdn = slapi_value_get_string(sv);
  640. deref_do_deref_attr(pb, ctrlber, derefdn, spec->derefattr, (const char **)spec->attrs);
  641. }
  642. deref_values_free(&results, &actual_type_name, buffer_flags);
  643. }
  644. ber_printf(ctrlber, "}"); /* end control val */
  645. slapi_build_control(LDAP_CONTROL_X_DEREF, ctrlber, 0, &ctrl);
  646. /* get the list of controls */
  647. slapi_pblock_get(pb, SLAPI_SEARCH_CTRLS, &searchctrls);
  648. /* dup them */
  649. slapi_add_controls(&newsearchctrls, (LDAPControl **)searchctrls, 1);
  650. /* add our control */
  651. slapi_add_control_ext(&newsearchctrls, ctrl, 0);
  652. ctrl = NULL; /* newsearchctrls owns it now */
  653. /* set the controls in the pblock */
  654. slapi_pblock_set(pb, SLAPI_SEARCH_CTRLS, newsearchctrls);
  655. ber_free(ctrlber, 1);
  656. return 0;
  657. }
  658. /* consumer operation extension constructor */
  659. static void *
  660. deref_operation_extension_ctor(void *object, void *parent)
  661. {
  662. /* we only set the extension value explicitly if the
  663. client requested the control - see deref_pre_search */
  664. return NULL; /* we don't set anything in the ctor */
  665. }
  666. /* consumer operation extension destructor */
  667. static void
  668. deref_operation_extension_dtor(void *ext, void *object, void *parent)
  669. {
  670. DerefSpecList *speclist = (DerefSpecList *)ext;
  671. delete_DerefSpecList(&speclist);
  672. }
  673. static int
  674. deref_register_operation_extension(void)
  675. {
  676. return slapi_register_object_extension(DEREF_PLUGIN_SUBSYSTEM,
  677. SLAPI_EXT_OPERATION,
  678. deref_operation_extension_ctor,
  679. deref_operation_extension_dtor,
  680. &deref_extension_type,
  681. &deref_extension_handle);
  682. }