deref.c 28 KB

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