deref.c 27 KB

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