acleffectiverights.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. #include "acl.h"
  41. /* safer than doing strcat unprotected */
  42. /* news2 is optional, provided as a convenience */
  43. /* capacity is the capacity of the gerstr, size is the current length */
  44. static void
  45. _append_gerstr(char **gerstr, size_t *capacity, size_t *size, const char *news, const char *news2)
  46. {
  47. size_t len;
  48. size_t increment = 128;
  49. size_t fornull;
  50. if (!news) {
  51. return;
  52. }
  53. /* find out how much space we need */
  54. len = strlen(news);
  55. fornull = 1;
  56. if (news2) {
  57. len += strlen(news2);
  58. fornull++;
  59. }
  60. /* increase space if needed */
  61. while ((*size + len + fornull) > *capacity) {
  62. if ((len + fornull) > increment) {
  63. *capacity += len + fornull; /* just go ahead and grow the string enough */
  64. } else {
  65. *capacity += increment; /* rather than having lots of small increments */
  66. }
  67. }
  68. if (!*gerstr) {
  69. *gerstr = slapi_ch_malloc(*capacity);
  70. **gerstr = 0;
  71. } else {
  72. *gerstr = slapi_ch_realloc(*gerstr, *capacity);
  73. }
  74. strcat(*gerstr, news);
  75. if (news2) {
  76. strcat(*gerstr, news2);
  77. }
  78. *size += len;
  79. return;
  80. }
  81. static int
  82. _ger_g_permission_granted ( Slapi_PBlock *pb, Slapi_Entry *e, char **errbuf )
  83. {
  84. char *proxydn = NULL;
  85. Slapi_DN *requestor_sdn, *entry_sdn;
  86. char *errtext = NULL;
  87. int isroot;
  88. int rc;
  89. /*
  90. * Theorically, we should check if the entry has "g"
  91. * permission granted to the requestor. If granted,
  92. * allows the effective rights on that entry and its
  93. * attributes within the entry to be returned for
  94. * ANY subject.
  95. *
  96. * "G" permission granting has not been implemented yet,
  97. * the current release assumes that "g" permission be
  98. * granted to root and owner of any entry.
  99. */
  100. /*
  101. * The requestor may be either the bind dn or a proxy dn
  102. */
  103. acl_get_proxyauth_dn ( pb, &proxydn, &errtext );
  104. if ( proxydn != NULL )
  105. {
  106. requestor_sdn = slapi_sdn_new_dn_passin ( proxydn );
  107. }
  108. else
  109. {
  110. requestor_sdn = &(pb->pb_op->o_sdn);
  111. }
  112. if ( slapi_sdn_get_dn (requestor_sdn) == NULL )
  113. {
  114. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  115. "_ger_g_permission_granted: anonymous has no g permission\n" );
  116. rc = LDAP_INSUFFICIENT_ACCESS;
  117. goto bailout;
  118. }
  119. isroot = slapi_dn_isroot ( slapi_sdn_get_dn (requestor_sdn) );
  120. if ( isroot )
  121. {
  122. /* Root has "g" permission on any entry */
  123. rc = LDAP_SUCCESS;
  124. goto bailout;
  125. }
  126. entry_sdn = slapi_entry_get_sdn ( e );
  127. if ( entry_sdn == NULL || slapi_sdn_get_dn (entry_sdn) == NULL )
  128. {
  129. rc = LDAP_SUCCESS;
  130. goto bailout;
  131. }
  132. if ( slapi_sdn_compare ( requestor_sdn, entry_sdn ) == 0 )
  133. {
  134. /* Owner has "g" permission on his own entry */
  135. rc = LDAP_SUCCESS;
  136. goto bailout;
  137. }
  138. aclutil_str_appened ( errbuf, "get-effective-rights: requestor has no g permission on the entry" );
  139. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  140. "_ger_g_permission_granted: %s\n", *errbuf);
  141. rc = LDAP_INSUFFICIENT_ACCESS;
  142. bailout:
  143. if ( proxydn )
  144. {
  145. /* The ownership of proxydn has passed to requestor_sdn */
  146. slapi_sdn_free ( &requestor_sdn );
  147. }
  148. return rc;
  149. }
  150. static int
  151. _ger_parse_control ( Slapi_PBlock *pb, char **subjectndn, int *iscritical, char **errbuf )
  152. {
  153. LDAPControl **requestcontrols;
  154. struct berval *subjectber;
  155. BerElement *ber;
  156. if (NULL == subjectndn)
  157. {
  158. return LDAP_OPERATIONS_ERROR;
  159. }
  160. *subjectndn = NULL;
  161. /*
  162. * Get the control
  163. */
  164. slapi_pblock_get ( pb, SLAPI_REQCONTROLS, (void *) &requestcontrols );
  165. slapi_control_present ( requestcontrols,
  166. LDAP_CONTROL_GET_EFFECTIVE_RIGHTS,
  167. &subjectber,
  168. iscritical );
  169. if ( subjectber == NULL || subjectber->bv_val == NULL ||
  170. subjectber->bv_len == 0 )
  171. {
  172. aclutil_str_appened ( errbuf, "get-effective-rights: missing subject" );
  173. slapi_log_error (SLAPI_LOG_FATAL, plugin_name, "%s\n", *errbuf );
  174. return LDAP_INVALID_SYNTAX;
  175. }
  176. if ( strncasecmp ( "dn:", subjectber->bv_val, 3 ) == 0 )
  177. {
  178. /*
  179. * This is a non-standard support to allow the subject being a plain
  180. * or base64 encoding string. Hence users using -J option in
  181. * ldapsearch don't have to do BER encoding for the subject.
  182. */
  183. *subjectndn = slapi_ch_malloc ( subjectber->bv_len + 1 );
  184. strncpy ( *subjectndn, subjectber->bv_val, subjectber->bv_len );
  185. *(*subjectndn + subjectber->bv_len) = '\0';
  186. }
  187. else
  188. {
  189. ber = ber_init (subjectber);
  190. if ( ber == NULL )
  191. {
  192. aclutil_str_appened ( errbuf, "get-effective-rights: ber_init failed for the subject" );
  193. slapi_log_error (SLAPI_LOG_FATAL, plugin_name, "%s\n", *errbuf );
  194. return LDAP_OPERATIONS_ERROR;
  195. }
  196. /* "a" means to allocate storage as needed for octet string */
  197. if ( ber_scanf (ber, "a", subjectndn) == LBER_ERROR )
  198. {
  199. aclutil_str_appened ( errbuf, "get-effective-rights: invalid ber tag in the subject" );
  200. slapi_log_error (SLAPI_LOG_FATAL, plugin_name, "%s\n", *errbuf );
  201. ber_free ( ber, 1 );
  202. return LDAP_INVALID_SYNTAX;
  203. }
  204. ber_free ( ber, 1 );
  205. }
  206. /*
  207. * The current implementation limits the subject to authorization ID
  208. * (see section 9 of RFC 2829) only. It also only supports the "dnAuthzId"
  209. * flavor, which looks like "dn:<DN>" where null <DN> is for anonymous.
  210. */
  211. if ( NULL == *subjectndn || strlen (*subjectndn) < 3 ||
  212. strncasecmp ( "dn:", *subjectndn, 3 ) != 0 )
  213. {
  214. aclutil_str_appened ( errbuf, "get-effective-rights: subject is not dnAuthzId" );
  215. slapi_log_error (SLAPI_LOG_FATAL, plugin_name, "%s\n", *errbuf );
  216. return LDAP_INVALID_SYNTAX;
  217. }
  218. strcpy ( *subjectndn, *subjectndn + 3 );
  219. slapi_dn_normalize ( *subjectndn );
  220. return LDAP_SUCCESS;
  221. }
  222. static void
  223. _ger_release_gerpb (
  224. Slapi_PBlock **gerpb,
  225. void **aclcb, /* original aclcb */
  226. Slapi_PBlock *pb /* original pb */
  227. )
  228. {
  229. if ( *gerpb )
  230. {
  231. /* Return conn to pb */
  232. slapi_pblock_set ( *gerpb, SLAPI_CONNECTION, NULL );
  233. slapi_pblock_destroy ( *gerpb );
  234. *gerpb = NULL;
  235. }
  236. /* Put the original aclcb back to pb */
  237. if ( *aclcb )
  238. {
  239. Connection *conn = NULL;
  240. slapi_pblock_get ( pb, SLAPI_CONNECTION, &conn );
  241. if (conn)
  242. {
  243. struct aclcb *geraclcb;
  244. geraclcb = (struct aclcb *) acl_get_ext ( ACL_EXT_CONNECTION, conn );
  245. acl_conn_ext_destructor ( geraclcb, NULL, NULL );
  246. acl_set_ext ( ACL_EXT_CONNECTION, conn, *aclcb );
  247. *aclcb = NULL;
  248. }
  249. }
  250. }
  251. static int
  252. _ger_new_gerpb (
  253. Slapi_PBlock *pb,
  254. Slapi_Entry *e,
  255. const char *subjectndn,
  256. Slapi_PBlock **gerpb,
  257. void **aclcb, /* original aclcb */
  258. char **errbuf
  259. )
  260. {
  261. Connection *conn;
  262. struct acl_cblock *geraclcb;
  263. Acl_PBlock *geraclpb;
  264. Operation *gerop;
  265. int rc = LDAP_SUCCESS;
  266. *aclcb = NULL;
  267. *gerpb = slapi_pblock_new ();
  268. if ( *gerpb == NULL )
  269. {
  270. rc = LDAP_NO_MEMORY;
  271. goto bailout;
  272. }
  273. {
  274. /* aclpb initialization needs the backend */
  275. Slapi_Backend *be;
  276. slapi_pblock_get ( pb, SLAPI_BACKEND, &be );
  277. slapi_pblock_set ( *gerpb, SLAPI_BACKEND, be );
  278. }
  279. {
  280. int isroot = slapi_dn_isroot ( subjectndn );
  281. slapi_pblock_set ( *gerpb, SLAPI_REQUESTOR_ISROOT, &isroot );
  282. }
  283. /* Save requestor's aclcb and set subjectdn's one */
  284. {
  285. slapi_pblock_get ( pb, SLAPI_CONNECTION, &conn );
  286. slapi_pblock_set ( *gerpb, SLAPI_CONNECTION, conn );
  287. /* Can't share the conn->aclcb because of different context */
  288. geraclcb = (struct acl_cblock *) acl_conn_ext_constructor ( NULL, NULL);
  289. if ( geraclcb == NULL )
  290. {
  291. rc = LDAP_NO_MEMORY;
  292. goto bailout;
  293. }
  294. slapi_sdn_set_ndn_byval ( geraclcb->aclcb_sdn, subjectndn );
  295. *aclcb = acl_get_ext ( ACL_EXT_CONNECTION, conn );
  296. acl_set_ext ( ACL_EXT_CONNECTION, conn, (void *) geraclcb );
  297. }
  298. {
  299. gerop = operation_new ( OP_FLAG_INTERNAL );
  300. if ( gerop == NULL )
  301. {
  302. rc = LDAP_NO_MEMORY;
  303. goto bailout;
  304. }
  305. /*
  306. * conn is a no-use parameter in the functions
  307. * chained down from factory_create_extension
  308. */
  309. gerop->o_extension = factory_create_extension ( get_operation_object_type(), (void *)gerop, (void *)conn );
  310. slapi_pblock_set ( *gerpb, SLAPI_OPERATION, gerop );
  311. slapi_sdn_set_dn_byval ( &gerop->o_sdn, subjectndn );
  312. geraclpb = acl_get_ext ( ACL_EXT_OPERATION, (void *)gerop);
  313. acl_init_aclpb ( *gerpb, geraclpb, subjectndn, 0 );
  314. geraclpb->aclpb_res_type |= ACLPB_EFFECTIVE_RIGHTS;
  315. }
  316. bailout:
  317. if ( rc != LDAP_SUCCESS )
  318. {
  319. _ger_release_gerpb ( gerpb, aclcb, pb );
  320. }
  321. return rc;
  322. }
  323. /*
  324. * Callers should have already allocated *gerstr to hold at least
  325. * "entryLevelRights: adnvxxx\n".
  326. */
  327. unsigned long
  328. _ger_get_entry_rights (
  329. Slapi_PBlock *gerpb,
  330. Slapi_Entry *e,
  331. const char *subjectndn,
  332. char **gerstr,
  333. size_t *gerstrsize,
  334. size_t *gerstrcap,
  335. char **errbuf
  336. )
  337. {
  338. unsigned long entryrights = 0;
  339. Slapi_RDN *rdn = NULL;
  340. char *rdntype = NULL;
  341. char *rdnvalue = NULL;
  342. _append_gerstr(gerstr, gerstrsize, gerstrcap, "entryLevelRights: ", NULL);
  343. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  344. "_ger_get_entry_rights: SLAPI_ACL_READ\n" );
  345. if (acl_access_allowed(gerpb, e, "*", NULL, SLAPI_ACL_READ) == LDAP_SUCCESS)
  346. {
  347. /* v - view e */
  348. entryrights |= SLAPI_ACL_READ;
  349. _append_gerstr(gerstr, gerstrsize, gerstrcap, "v", NULL);
  350. }
  351. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  352. "_ger_get_entry_rights: SLAPI_ACL_ADD\n" );
  353. if (acl_access_allowed(gerpb, e, NULL, NULL, SLAPI_ACL_ADD) == LDAP_SUCCESS)
  354. {
  355. /* a - add child entry below e */
  356. entryrights |= SLAPI_ACL_ADD;
  357. _append_gerstr(gerstr, gerstrsize, gerstrcap, "a", NULL);
  358. }
  359. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  360. "_ger_get_entry_rights: SLAPI_ACL_DELETE\n" );
  361. if (acl_access_allowed(gerpb, e, NULL, NULL, SLAPI_ACL_DELETE) == LDAP_SUCCESS)
  362. {
  363. /* d - delete e */
  364. entryrights |= SLAPI_ACL_DELETE;
  365. _append_gerstr(gerstr, gerstrsize, gerstrcap, "d", NULL);
  366. }
  367. /*
  368. * Some limitation/simplification applied here:
  369. * - The modrdn right requires the rights to delete the old rdn and
  370. * the new one. However we have no knowledge of what the new rdn
  371. * is going to be.
  372. * - In multi-valued RDN case, we check the right on
  373. * the first rdn type only for now.
  374. */
  375. rdn = slapi_rdn_new_dn ( slapi_entry_get_ndn (e) );
  376. slapi_rdn_get_first(rdn, &rdntype, &rdnvalue);
  377. if ( NULL != rdntype ) {
  378. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  379. "_ger_get_entry_rights: SLAPI_ACL_WRITE_DEL & _ADD %s\n", rdntype );
  380. if (acl_access_allowed(gerpb, e, rdntype, NULL,
  381. ACLPB_SLAPI_ACL_WRITE_DEL) == LDAP_SUCCESS &&
  382. acl_access_allowed(gerpb, e, rdntype, NULL,
  383. ACLPB_SLAPI_ACL_WRITE_ADD) == LDAP_SUCCESS)
  384. {
  385. /* n - rename e */
  386. entryrights |= SLAPI_ACL_WRITE;
  387. _append_gerstr(gerstr, gerstrsize, gerstrcap, "n", NULL);
  388. }
  389. }
  390. slapi_rdn_free ( &rdn );
  391. if ( entryrights == 0 )
  392. {
  393. _append_gerstr(gerstr, gerstrsize, gerstrcap, "none", NULL);
  394. }
  395. _append_gerstr(gerstr, gerstrsize, gerstrcap, "\n", NULL);
  396. return entryrights;
  397. }
  398. /*
  399. * *gerstr should point to a heap buffer since it may need
  400. * to expand dynamically.
  401. */
  402. unsigned long
  403. _ger_get_attr_rights (
  404. Slapi_PBlock *gerpb,
  405. Slapi_Entry *e,
  406. const char *subjectndn,
  407. char *type,
  408. char **gerstr,
  409. size_t *gerstrsize,
  410. size_t *gerstrcap,
  411. int isfirstattr,
  412. char **errbuf
  413. )
  414. {
  415. unsigned long attrrights = 0;
  416. if (!isfirstattr)
  417. {
  418. _append_gerstr(gerstr, gerstrsize, gerstrcap, ", ", NULL);
  419. }
  420. _append_gerstr(gerstr, gerstrsize, gerstrcap, type, ":");
  421. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  422. "_ger_get_attr_rights: SLAPI_ACL_READ %s\n", type );
  423. if (acl_access_allowed(gerpb, e, type, NULL, SLAPI_ACL_READ) == LDAP_SUCCESS)
  424. {
  425. /* r - read the values of type */
  426. attrrights |= SLAPI_ACL_READ;
  427. _append_gerstr(gerstr, gerstrsize, gerstrcap, "r", NULL);
  428. }
  429. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  430. "_ger_get_attr_rights: SLAPI_ACL_SEARCH %s\n", type );
  431. if (acl_access_allowed(gerpb, e, type, NULL, SLAPI_ACL_SEARCH) == LDAP_SUCCESS)
  432. {
  433. /* s - search the values of type */
  434. attrrights |= SLAPI_ACL_SEARCH;
  435. _append_gerstr(gerstr, gerstrsize, gerstrcap, "s", NULL);
  436. }
  437. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  438. "_ger_get_attr_rights: SLAPI_ACL_COMPARE %s\n", type );
  439. if (acl_access_allowed(gerpb, e, type, NULL, SLAPI_ACL_COMPARE) == LDAP_SUCCESS)
  440. {
  441. /* c - compare the values of type */
  442. attrrights |= SLAPI_ACL_COMPARE;
  443. _append_gerstr(gerstr, gerstrsize, gerstrcap, "c", NULL);
  444. }
  445. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  446. "_ger_get_attr_rights: SLAPI_ACL_WRITE_ADD %s\n", type );
  447. if (acl_access_allowed(gerpb, e, type, NULL, ACLPB_SLAPI_ACL_WRITE_ADD) == LDAP_SUCCESS)
  448. {
  449. /* w - add the values of type */
  450. attrrights |= ACLPB_SLAPI_ACL_WRITE_ADD;
  451. _append_gerstr(gerstr, gerstrsize, gerstrcap, "w", NULL);
  452. }
  453. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  454. "_ger_get_attr_rights: SLAPI_ACL_WRITE_DEL %s\n", type );
  455. if (acl_access_allowed(gerpb, e, type, NULL, ACLPB_SLAPI_ACL_WRITE_DEL) == LDAP_SUCCESS)
  456. {
  457. /* o - delete the values of type */
  458. attrrights |= ACLPB_SLAPI_ACL_WRITE_DEL;
  459. _append_gerstr(gerstr, gerstrsize, gerstrcap, "o", NULL);
  460. }
  461. /* If subjectdn has no general write right, check for self write */
  462. if ( 0 == (attrrights & (ACLPB_SLAPI_ACL_WRITE_DEL | ACLPB_SLAPI_ACL_WRITE_ADD)) )
  463. {
  464. struct berval val;
  465. val.bv_val = (char *)subjectndn;
  466. val.bv_len = strlen (subjectndn);
  467. if (acl_access_allowed(gerpb, e, type, &val, ACLPB_SLAPI_ACL_WRITE_ADD) == LDAP_SUCCESS)
  468. {
  469. /* W - add self to the attribute */
  470. attrrights |= ACLPB_SLAPI_ACL_WRITE_ADD;
  471. _append_gerstr(gerstr, gerstrsize, gerstrcap, "W", NULL);
  472. }
  473. if (acl_access_allowed(gerpb, e, type, &val, ACLPB_SLAPI_ACL_WRITE_DEL) == LDAP_SUCCESS)
  474. {
  475. /* O - delete self from the attribute */
  476. attrrights |= ACLPB_SLAPI_ACL_WRITE_DEL;
  477. _append_gerstr(gerstr, gerstrsize, gerstrcap, "O", NULL);
  478. }
  479. }
  480. if ( attrrights == 0 )
  481. {
  482. _append_gerstr(gerstr, gerstrsize, gerstrcap, "none", NULL);
  483. }
  484. return attrrights;
  485. }
  486. void
  487. _ger_get_attrs_rights (
  488. Slapi_PBlock *gerpb,
  489. Slapi_Entry *e,
  490. const char *subjectndn,
  491. char **attrs,
  492. char **gerstr,
  493. size_t *gerstrsize,
  494. size_t *gerstrcap,
  495. char **errbuf
  496. )
  497. {
  498. int isfirstattr = 1;
  499. /* gerstr was initially allocated with enough space for one more line */
  500. _append_gerstr(gerstr, gerstrsize, gerstrcap, "attributeLevelRights: ", NULL);
  501. if (attrs && *attrs)
  502. {
  503. int i;
  504. for ( i = 0; attrs[i]; i++ )
  505. {
  506. _ger_get_attr_rights ( gerpb, e, subjectndn, attrs[i], gerstr, gerstrsize, gerstrcap, isfirstattr, errbuf );
  507. isfirstattr = 0;
  508. }
  509. }
  510. else
  511. {
  512. Slapi_Attr *prevattr = NULL, *attr;
  513. char *type;
  514. while ( slapi_entry_next_attr ( e, prevattr, &attr ) == 0 )
  515. {
  516. if ( ! slapi_attr_flag_is_set (attr, SLAPI_ATTR_FLAG_OPATTR) )
  517. {
  518. slapi_attr_get_type ( attr, &type );
  519. _ger_get_attr_rights ( gerpb, e, subjectndn, type, gerstr, gerstrsize, gerstrcap, isfirstattr, errbuf );
  520. isfirstattr = 0;
  521. }
  522. prevattr = attr;
  523. }
  524. }
  525. if ( isfirstattr )
  526. {
  527. /* not a single attribute was retrived or specified */
  528. _append_gerstr(gerstr, gerstrsize, gerstrcap, "*:none", NULL);
  529. }
  530. return;
  531. }
  532. /*
  533. * controlType = LDAP_CONTROL_GET_EFFECTIVE_RIGHTS;
  534. * criticality = n/a;
  535. * controlValue = OCTET STRING of BER encoding of the SEQUENCE of
  536. * ENUMERATED LDAP code
  537. */
  538. void
  539. _ger_set_response_control (
  540. Slapi_PBlock *pb,
  541. int iscritical,
  542. int rc
  543. )
  544. {
  545. LDAPControl **resultctrls = NULL;
  546. LDAPControl gerrespctrl;
  547. BerElement *ber = NULL;
  548. struct berval *berval = NULL;
  549. int found = 0;
  550. int i;
  551. if ( (ber = der_alloc ()) == NULL )
  552. {
  553. goto bailout;
  554. }
  555. /* begin sequence, enumeration, end sequence */
  556. ber_printf ( ber, "{e}", rc );
  557. if ( ber_flatten ( ber, &berval ) != LDAP_SUCCESS )
  558. {
  559. goto bailout;
  560. }
  561. gerrespctrl.ldctl_oid = LDAP_CONTROL_GET_EFFECTIVE_RIGHTS;
  562. gerrespctrl.ldctl_iscritical = iscritical;
  563. gerrespctrl.ldctl_value.bv_val = berval->bv_val;
  564. gerrespctrl.ldctl_value.bv_len = berval->bv_len;
  565. slapi_pblock_get ( pb, SLAPI_RESCONTROLS, &resultctrls );
  566. for (i = 0; resultctrls && resultctrls[i]; i++)
  567. {
  568. if (strcmp(resultctrls[i]->ldctl_oid, LDAP_CONTROL_GET_EFFECTIVE_RIGHTS) == 0)
  569. {
  570. /*
  571. * We get here if search returns more than one entry
  572. * and this is not the first entry.
  573. */
  574. ldap_control_free ( resultctrls[i] );
  575. resultctrls[i] = slapi_dup_control (&gerrespctrl);
  576. found = 1;
  577. break;
  578. }
  579. }
  580. if ( !found )
  581. {
  582. /* slapi_pblock_set() will dup the control */
  583. slapi_pblock_set ( pb, SLAPI_ADD_RESCONTROL, &gerrespctrl );
  584. }
  585. bailout:
  586. ber_free ( ber, 1 ); /* ber_free() checks for NULL param */
  587. ber_bvfree ( berval ); /* ber_bvfree() checks for NULL param */
  588. }
  589. int
  590. acl_get_effective_rights (
  591. Slapi_PBlock *pb,
  592. Slapi_Entry *e, /* target entry */
  593. char **attrs, /* Attribute of the entry */
  594. struct berval *val, /* value of attr. NOT USED */
  595. int access, /* requested access rights */
  596. char **errbuf
  597. )
  598. {
  599. Slapi_PBlock *gerpb = NULL;
  600. void *aclcb = NULL;
  601. char *subjectndn = NULL;
  602. char *gerstr = NULL;
  603. size_t gerstrsize = 0;
  604. size_t gerstrcap = 0;
  605. int iscritical = 1;
  606. int rc;
  607. *errbuf = '\0';
  608. /*
  609. * Get the subject
  610. */
  611. rc = _ger_parse_control (pb, &subjectndn, &iscritical, errbuf );
  612. if ( rc != LDAP_SUCCESS )
  613. {
  614. goto bailout;
  615. }
  616. /*
  617. * The requestor should have g permission on the entry
  618. * to get the effective rights.
  619. */
  620. rc = _ger_g_permission_granted (pb, e, errbuf);
  621. if ( rc != LDAP_SUCCESS )
  622. {
  623. goto bailout;
  624. }
  625. /*
  626. * Construct a new pb
  627. */
  628. rc = _ger_new_gerpb ( pb, e, subjectndn, &gerpb, &aclcb, errbuf );
  629. if ( rc != LDAP_SUCCESS )
  630. {
  631. goto bailout;
  632. }
  633. /* Get entry level effective rights */
  634. _ger_get_entry_rights ( gerpb, e, subjectndn, &gerstr, &gerstrsize, &gerstrcap, errbuf );
  635. /*
  636. * Attribute level effective rights may not be NULL
  637. * even if entry level's is.
  638. */
  639. _ger_get_attrs_rights ( gerpb, e, subjectndn, attrs, &gerstr, &gerstrsize, &gerstrcap, errbuf );
  640. bailout:
  641. /*
  642. * Now construct the response control
  643. */
  644. _ger_set_response_control ( pb, iscritical, rc );
  645. if ( rc != LDAP_SUCCESS )
  646. {
  647. gerstr = slapi_ch_smprintf("entryLevelRights: %d\nattributeLevelRights: *:%d", rc, rc );
  648. }
  649. slapi_log_error (SLAPI_LOG_ACLSUMMARY, plugin_name,
  650. "###### Effective Rights on Entry (%s) for Subject (%s) ######\n",
  651. slapi_entry_get_ndn (e), subjectndn);
  652. slapi_log_error (SLAPI_LOG_ACLSUMMARY, plugin_name, "%s\n", gerstr);
  653. /* Restore pb */
  654. _ger_release_gerpb ( &gerpb, &aclcb, pb );
  655. /*
  656. * General plugin uses SLAPI_RESULT_TEXT for error text. Here
  657. * SLAPI_PB_RESULT_TEXT is exclusively shared with add, dse and schema.
  658. * slapi_pblock_set() will free any previous data, and
  659. * pblock_done() will free SLAPI_PB_RESULT_TEXT.
  660. */
  661. slapi_pblock_set (pb, SLAPI_PB_RESULT_TEXT, gerstr);
  662. if ( !iscritical )
  663. {
  664. /*
  665. * If return code is not LDAP_SUCCESS, the server would
  666. * abort sending the data of the entry to the client.
  667. */
  668. rc = LDAP_SUCCESS;
  669. }
  670. slapi_ch_free ( (void **) &subjectndn );
  671. slapi_ch_free ( (void **) &gerstr );
  672. return rc;
  673. }