acleffectiverights.c 18 KB

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