deref.c 26 KB

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