acleffectiverights.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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(
  46. char **gerstr,
  47. size_t *capacity,
  48. size_t *size,
  49. const char *news,
  50. const char *news2
  51. )
  52. {
  53. size_t len;
  54. size_t increment = 128;
  55. size_t fornull;
  56. if (!news) {
  57. return;
  58. }
  59. /* find out how much space we need */
  60. len = strlen(news);
  61. fornull = 1;
  62. if (news2) {
  63. len += strlen(news2);
  64. fornull++;
  65. }
  66. /* increase space if needed */
  67. while ((*size + len + fornull) > *capacity) {
  68. if ((len + fornull) > increment) {
  69. *capacity += len + fornull; /* just go ahead and grow the string enough */
  70. } else {
  71. *capacity += increment; /* rather than having lots of small increments */
  72. }
  73. }
  74. if (!*gerstr) {
  75. *gerstr = slapi_ch_malloc(*capacity);
  76. **gerstr = 0;
  77. } else {
  78. *gerstr = slapi_ch_realloc(*gerstr, *capacity);
  79. }
  80. strcat(*gerstr, news);
  81. if (news2) {
  82. strcat(*gerstr, news2);
  83. }
  84. *size += len;
  85. return;
  86. }
  87. static int
  88. _ger_g_permission_granted (
  89. Slapi_PBlock *pb,
  90. Slapi_Entry *e,
  91. const char *subjectdn,
  92. char **errbuf
  93. )
  94. {
  95. char *proxydn = NULL;
  96. Slapi_DN *requestor_sdn, *entry_sdn;
  97. char *errtext = NULL;
  98. int isroot;
  99. int rc;
  100. /*
  101. * Theorically, we should check if the entry has "g"
  102. * permission granted to the requestor. If granted,
  103. * allows the effective rights on that entry and its
  104. * attributes within the entry to be returned for
  105. * ANY subject.
  106. *
  107. * "G" permission granting has not been implemented yet,
  108. * the current release assumes that "g" permission be
  109. * granted to root and owner of any entry.
  110. */
  111. /*
  112. * The requestor may be either the bind dn or a proxy dn
  113. */
  114. proxyauth_get_dn ( pb, &proxydn, &errtext );
  115. if ( proxydn != NULL )
  116. {
  117. requestor_sdn = slapi_sdn_new_dn_passin ( proxydn );
  118. }
  119. else
  120. {
  121. requestor_sdn = &(pb->pb_op->o_sdn);
  122. }
  123. if ( slapi_sdn_get_dn (requestor_sdn) == NULL )
  124. {
  125. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  126. "_ger_g_permission_granted: anonymous has no g permission\n" );
  127. rc = LDAP_INSUFFICIENT_ACCESS;
  128. goto bailout;
  129. }
  130. isroot = slapi_dn_isroot ( slapi_sdn_get_dn (requestor_sdn) );
  131. if ( isroot )
  132. {
  133. /* Root has "g" permission on any entry */
  134. rc = LDAP_SUCCESS;
  135. goto bailout;
  136. }
  137. entry_sdn = slapi_entry_get_sdn ( e );
  138. if ( entry_sdn == NULL || slapi_sdn_get_dn (entry_sdn) == NULL )
  139. {
  140. rc = LDAP_SUCCESS;
  141. goto bailout;
  142. }
  143. if ( slapi_sdn_compare ( requestor_sdn, entry_sdn ) == 0 )
  144. {
  145. /* Owner has "g" permission on his own entry */
  146. rc = LDAP_SUCCESS;
  147. goto bailout;
  148. }
  149. /* if the requestor and the subject user are identical, let's grant it */
  150. if ( strcasecmp ( slapi_sdn_get_ndn(requestor_sdn), subjectdn ) == 0)
  151. {
  152. /* Requestor should see his own permission rights on any entry */
  153. rc = LDAP_SUCCESS;
  154. goto bailout;
  155. }
  156. aclutil_str_append ( errbuf, "get-effective-rights: requestor has no g permission on the entry" );
  157. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  158. "_ger_g_permission_granted: %s\n", *errbuf);
  159. rc = LDAP_INSUFFICIENT_ACCESS;
  160. bailout:
  161. if ( proxydn )
  162. {
  163. /* The ownership of proxydn has passed to requestor_sdn */
  164. slapi_sdn_free ( &requestor_sdn );
  165. }
  166. return rc;
  167. }
  168. static int
  169. _ger_parse_control (
  170. Slapi_PBlock *pb,
  171. char **subjectndn,
  172. int *iscritical,
  173. char **errbuf
  174. )
  175. {
  176. LDAPControl **requestcontrols;
  177. struct berval *subjectber;
  178. BerElement *ber;
  179. size_t subjectndnlen = 0;
  180. char *orig = NULL;
  181. char *normed = NULL;
  182. int rc = 0;
  183. if (NULL == subjectndn)
  184. {
  185. return LDAP_OPERATIONS_ERROR;
  186. }
  187. *subjectndn = NULL;
  188. /*
  189. * Get the control
  190. */
  191. slapi_pblock_get ( pb, SLAPI_REQCONTROLS, (void *) &requestcontrols );
  192. slapi_control_present ( requestcontrols,
  193. LDAP_CONTROL_GET_EFFECTIVE_RIGHTS,
  194. &subjectber,
  195. iscritical );
  196. if ( subjectber == NULL || subjectber->bv_val == NULL ||
  197. subjectber->bv_len == 0 )
  198. {
  199. aclutil_str_append ( errbuf, "get-effective-rights: missing subject" );
  200. slapi_log_error (SLAPI_LOG_FATAL, plugin_name, "%s\n", *errbuf );
  201. return LDAP_INVALID_SYNTAX;
  202. }
  203. if ( strncasecmp ( "dn:", subjectber->bv_val, 3 ) == 0 )
  204. {
  205. /*
  206. * This is a non-standard support to allow the subject being a plain
  207. * or base64 encoding string. Hence users using -J option in
  208. * ldapsearch don't have to do BER encoding for the subject.
  209. */
  210. orig = slapi_ch_malloc ( subjectber->bv_len + 1 );
  211. strncpy ( orig, subjectber->bv_val, subjectber->bv_len );
  212. *(orig + subjectber->bv_len) = '\0';
  213. }
  214. else
  215. {
  216. ber = ber_init (subjectber);
  217. if ( ber == NULL )
  218. {
  219. aclutil_str_append ( errbuf, "get-effective-rights: ber_init failed for the subject" );
  220. slapi_log_error (SLAPI_LOG_FATAL, plugin_name, "%s\n", *errbuf );
  221. return LDAP_OPERATIONS_ERROR;
  222. }
  223. /* "a" means to allocate storage as needed for octet string */
  224. if ( ber_scanf (ber, "a", &orig) == LBER_ERROR )
  225. {
  226. aclutil_str_append ( errbuf, "get-effective-rights: invalid ber tag in the subject" );
  227. slapi_log_error (SLAPI_LOG_FATAL, plugin_name, "%s\n", *errbuf );
  228. ber_free ( ber, 1 );
  229. return LDAP_INVALID_SYNTAX;
  230. }
  231. ber_free ( ber, 1 );
  232. }
  233. /*
  234. * The current implementation limits the subject to authorization ID
  235. * (see section 9 of RFC 2829) only. It also only supports the "dnAuthzId"
  236. * flavor, which looks like "dn:<DN>" where null <DN> is for anonymous.
  237. */
  238. subjectndnlen = orig ? strlen(orig) : 0;
  239. if ( NULL == orig || subjectndnlen < 3 || strncasecmp ( "dn:", orig, 3 ) != 0 )
  240. {
  241. aclutil_str_append ( errbuf, "get-effective-rights: subject is not dnAuthzId" );
  242. slapi_log_error (SLAPI_LOG_FATAL, plugin_name, "%s\n", *errbuf );
  243. slapi_ch_free_string(&orig);
  244. return LDAP_INVALID_SYNTAX;
  245. }
  246. /* memmove is safe for overlapping copy */
  247. rc = slapi_dn_normalize_ext(orig + 3, 0, &normed, &subjectndnlen);
  248. if (rc < 0) {
  249. aclutil_str_append ( errbuf, "get-effective-rights: failed to normalize dn: ");
  250. aclutil_str_append ( errbuf, orig);
  251. slapi_log_error (SLAPI_LOG_FATAL, plugin_name, "%s\n", *errbuf );
  252. slapi_ch_free_string(&orig);
  253. return LDAP_INVALID_SYNTAX;
  254. }
  255. if (rc == 0) { /* orig+3 is passed in; not terminated */
  256. *(normed + subjectndnlen) = '\0';
  257. *subjectndn = slapi_ch_strdup(normed);
  258. slapi_ch_free_string(&orig);
  259. } else {
  260. slapi_ch_free_string(&orig);
  261. *subjectndn = normed;
  262. }
  263. return LDAP_SUCCESS;
  264. }
  265. static void
  266. _ger_release_gerpb (
  267. Slapi_PBlock **gerpb,
  268. void **aclcb, /* original aclcb */
  269. Slapi_PBlock *pb /* original pb */
  270. )
  271. {
  272. if ( *gerpb )
  273. {
  274. slapi_pblock_destroy ( *gerpb );
  275. *gerpb = NULL;
  276. }
  277. /* Put the original aclcb back to pb */
  278. if ( *aclcb )
  279. {
  280. Connection *conn = NULL;
  281. slapi_pblock_get ( pb, SLAPI_CONNECTION, &conn );
  282. if (conn)
  283. {
  284. struct aclcb *geraclcb;
  285. geraclcb = (struct aclcb *) acl_get_ext ( ACL_EXT_CONNECTION, conn );
  286. acl_conn_ext_destructor ( geraclcb, NULL, NULL );
  287. acl_set_ext ( ACL_EXT_CONNECTION, conn, *aclcb );
  288. *aclcb = NULL;
  289. }
  290. }
  291. }
  292. static int
  293. _ger_new_gerpb (
  294. Slapi_PBlock *pb,
  295. Slapi_Entry *e,
  296. const char *subjectndn,
  297. Slapi_PBlock **gerpb,
  298. void **aclcb, /* original aclcb */
  299. char **errbuf
  300. )
  301. {
  302. Connection *conn;
  303. struct acl_cblock *geraclcb;
  304. Acl_PBlock *geraclpb;
  305. Operation *gerop;
  306. int rc = LDAP_SUCCESS;
  307. *aclcb = NULL;
  308. *gerpb = slapi_pblock_new ();
  309. if ( *gerpb == NULL )
  310. {
  311. rc = LDAP_NO_MEMORY;
  312. goto bailout;
  313. }
  314. {
  315. /* aclpb initialization needs the backend */
  316. Slapi_Backend *be;
  317. slapi_pblock_get ( pb, SLAPI_BACKEND, &be );
  318. slapi_pblock_set ( *gerpb, SLAPI_BACKEND, be );
  319. }
  320. {
  321. int isroot = slapi_dn_isroot ( subjectndn );
  322. slapi_pblock_set ( *gerpb, SLAPI_REQUESTOR_ISROOT, &isroot );
  323. }
  324. /* Save requestor's aclcb and set subjectdn's one */
  325. {
  326. slapi_pblock_get ( pb, SLAPI_CONNECTION, &conn );
  327. slapi_pblock_set ( *gerpb, SLAPI_CONNECTION, conn );
  328. /* Can't share the conn->aclcb because of different context */
  329. geraclcb = (struct acl_cblock *) acl_conn_ext_constructor ( NULL, NULL);
  330. if ( geraclcb == NULL )
  331. {
  332. rc = LDAP_NO_MEMORY;
  333. goto bailout;
  334. }
  335. slapi_sdn_set_ndn_byval ( geraclcb->aclcb_sdn, subjectndn );
  336. *aclcb = acl_get_ext ( ACL_EXT_CONNECTION, conn );
  337. acl_set_ext ( ACL_EXT_CONNECTION, conn, (void *) geraclcb );
  338. }
  339. {
  340. gerop = operation_new ( OP_FLAG_INTERNAL );
  341. if ( gerop == NULL )
  342. {
  343. rc = LDAP_NO_MEMORY;
  344. goto bailout;
  345. }
  346. /*
  347. * conn is a no-use parameter in the functions
  348. * chained down from factory_create_extension
  349. */
  350. gerop->o_extension = factory_create_extension ( get_operation_object_type(), (void *)gerop, (void *)conn );
  351. slapi_pblock_set ( *gerpb, SLAPI_OPERATION, gerop );
  352. slapi_sdn_set_dn_byval ( &gerop->o_sdn, subjectndn );
  353. geraclpb = acl_get_ext ( ACL_EXT_OPERATION, (void *)gerop);
  354. acl_init_aclpb ( *gerpb, geraclpb, subjectndn, 0 );
  355. geraclpb->aclpb_res_type |= ACLPB_EFFECTIVE_RIGHTS;
  356. }
  357. bailout:
  358. if ( rc != LDAP_SUCCESS )
  359. {
  360. _ger_release_gerpb ( gerpb, aclcb, pb );
  361. }
  362. return rc;
  363. }
  364. /*
  365. * Callers should have already allocated *gerstr to hold at least
  366. * "entryLevelRights: adnvxxx\n".
  367. */
  368. unsigned long
  369. _ger_get_entry_rights (
  370. Slapi_PBlock *gerpb,
  371. Slapi_Entry *e,
  372. const char *subjectndn,
  373. char **gerstr,
  374. size_t *gerstrsize,
  375. size_t *gerstrcap,
  376. char **errbuf
  377. )
  378. {
  379. unsigned long entryrights = 0;
  380. Slapi_RDN *rdn = NULL;
  381. char *rdntype = NULL;
  382. char *rdnvalue = NULL;
  383. _append_gerstr(gerstr, gerstrsize, gerstrcap, "entryLevelRights: ", NULL);
  384. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  385. "_ger_get_entry_rights: SLAPI_ACL_READ\n" );
  386. if (acl_access_allowed(gerpb, e, "*", NULL, SLAPI_ACL_READ) == LDAP_SUCCESS)
  387. {
  388. /* v - view e */
  389. entryrights |= SLAPI_ACL_READ;
  390. _append_gerstr(gerstr, gerstrsize, gerstrcap, "v", NULL);
  391. }
  392. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  393. "_ger_get_entry_rights: SLAPI_ACL_ADD\n" );
  394. if (acl_access_allowed(gerpb, e, NULL, NULL, SLAPI_ACL_ADD) == LDAP_SUCCESS)
  395. {
  396. /* a - add child entry below e */
  397. entryrights |= SLAPI_ACL_ADD;
  398. _append_gerstr(gerstr, gerstrsize, gerstrcap, "a", NULL);
  399. }
  400. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  401. "_ger_get_entry_rights: SLAPI_ACL_DELETE\n" );
  402. if (acl_access_allowed(gerpb, e, NULL, NULL, SLAPI_ACL_DELETE) == LDAP_SUCCESS)
  403. {
  404. /* d - delete e */
  405. entryrights |= SLAPI_ACL_DELETE;
  406. _append_gerstr(gerstr, gerstrsize, gerstrcap, "d", NULL);
  407. }
  408. /*
  409. * Some limitation/simplification applied here:
  410. * - The modrdn right requires the rights to delete the old rdn and
  411. * the new one. However we have no knowledge of what the new rdn
  412. * is going to be.
  413. * - In multi-valued RDN case, we check the right on
  414. * the first rdn type only for now.
  415. */
  416. rdn = slapi_rdn_new_dn ( slapi_entry_get_ndn (e) );
  417. slapi_rdn_get_first(rdn, &rdntype, &rdnvalue);
  418. if ( NULL != rdntype ) {
  419. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  420. "_ger_get_entry_rights: SLAPI_ACL_WRITE_DEL & _ADD %s\n", rdntype );
  421. if (acl_access_allowed(gerpb, e, rdntype, NULL,
  422. ACLPB_SLAPI_ACL_WRITE_DEL) == LDAP_SUCCESS &&
  423. acl_access_allowed(gerpb, e, rdntype, NULL,
  424. ACLPB_SLAPI_ACL_WRITE_ADD) == LDAP_SUCCESS)
  425. {
  426. /* n - rename e */
  427. entryrights |= SLAPI_ACL_WRITE;
  428. _append_gerstr(gerstr, gerstrsize, gerstrcap, "n", NULL);
  429. }
  430. }
  431. slapi_rdn_free ( &rdn );
  432. if ( entryrights == 0 )
  433. {
  434. _append_gerstr(gerstr, gerstrsize, gerstrcap, "none", NULL);
  435. }
  436. _append_gerstr(gerstr, gerstrsize, gerstrcap, "\n", NULL);
  437. return entryrights;
  438. }
  439. /*
  440. * *gerstr should point to a heap buffer since it may need
  441. * to expand dynamically.
  442. */
  443. unsigned long
  444. _ger_get_attr_rights (
  445. Slapi_PBlock *gerpb,
  446. Slapi_Entry *e,
  447. const char *subjectndn,
  448. char *type,
  449. char **gerstr,
  450. size_t *gerstrsize,
  451. size_t *gerstrcap,
  452. int isfirstattr,
  453. char **errbuf
  454. )
  455. {
  456. unsigned long attrrights = 0;
  457. if (!isfirstattr)
  458. {
  459. _append_gerstr(gerstr, gerstrsize, gerstrcap, ", ", NULL);
  460. }
  461. _append_gerstr(gerstr, gerstrsize, gerstrcap, type, ":");
  462. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  463. "_ger_get_attr_rights: SLAPI_ACL_READ %s\n", type );
  464. if (acl_access_allowed(gerpb, e, type, NULL, SLAPI_ACL_READ) == LDAP_SUCCESS)
  465. {
  466. /* r - read the values of type */
  467. attrrights |= SLAPI_ACL_READ;
  468. _append_gerstr(gerstr, gerstrsize, gerstrcap, "r", NULL);
  469. }
  470. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  471. "_ger_get_attr_rights: SLAPI_ACL_SEARCH %s\n", type );
  472. if (acl_access_allowed(gerpb, e, type, NULL, SLAPI_ACL_SEARCH) == LDAP_SUCCESS)
  473. {
  474. /* s - search the values of type */
  475. attrrights |= SLAPI_ACL_SEARCH;
  476. _append_gerstr(gerstr, gerstrsize, gerstrcap, "s", NULL);
  477. }
  478. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  479. "_ger_get_attr_rights: SLAPI_ACL_COMPARE %s\n", type );
  480. if (acl_access_allowed(gerpb, e, type, NULL, SLAPI_ACL_COMPARE) == LDAP_SUCCESS)
  481. {
  482. /* c - compare the values of type */
  483. attrrights |= SLAPI_ACL_COMPARE;
  484. _append_gerstr(gerstr, gerstrsize, gerstrcap, "c", NULL);
  485. }
  486. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  487. "_ger_get_attr_rights: SLAPI_ACL_WRITE_ADD %s\n", type );
  488. if (acl_access_allowed(gerpb, e, type, NULL, ACLPB_SLAPI_ACL_WRITE_ADD) == LDAP_SUCCESS)
  489. {
  490. /* w - add the values of type */
  491. attrrights |= ACLPB_SLAPI_ACL_WRITE_ADD;
  492. _append_gerstr(gerstr, gerstrsize, gerstrcap, "w", NULL);
  493. }
  494. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  495. "_ger_get_attr_rights: SLAPI_ACL_WRITE_DEL %s\n", type );
  496. if (acl_access_allowed(gerpb, e, type, NULL, ACLPB_SLAPI_ACL_WRITE_DEL) == LDAP_SUCCESS)
  497. {
  498. /* o - delete the values of type */
  499. attrrights |= ACLPB_SLAPI_ACL_WRITE_DEL;
  500. _append_gerstr(gerstr, gerstrsize, gerstrcap, "o", NULL);
  501. }
  502. /* If subjectdn has no general write right, check for self write */
  503. if ( 0 == (attrrights & (ACLPB_SLAPI_ACL_WRITE_DEL | ACLPB_SLAPI_ACL_WRITE_ADD)) )
  504. {
  505. struct berval val;
  506. val.bv_val = (char *)subjectndn;
  507. val.bv_len = strlen (subjectndn);
  508. if (acl_access_allowed(gerpb, e, type, &val, ACLPB_SLAPI_ACL_WRITE_ADD) == LDAP_SUCCESS)
  509. {
  510. /* W - add self to the attribute */
  511. attrrights |= ACLPB_SLAPI_ACL_WRITE_ADD;
  512. _append_gerstr(gerstr, gerstrsize, gerstrcap, "W", NULL);
  513. }
  514. if (acl_access_allowed(gerpb, e, type, &val, ACLPB_SLAPI_ACL_WRITE_DEL) == LDAP_SUCCESS)
  515. {
  516. /* O - delete self from the attribute */
  517. attrrights |= ACLPB_SLAPI_ACL_WRITE_DEL;
  518. _append_gerstr(gerstr, gerstrsize, gerstrcap, "O", NULL);
  519. }
  520. }
  521. if ( attrrights == 0 )
  522. {
  523. _append_gerstr(gerstr, gerstrsize, gerstrcap, "none", NULL);
  524. }
  525. return attrrights;
  526. }
  527. #define GER_GET_ATTR_RIGHTS(attrs) \
  528. for (thisattr = (attrs); thisattr && *thisattr; thisattr++) \
  529. { \
  530. _ger_get_attr_rights (gerpb, e, subjectndn, *thisattr, \
  531. gerstr, gerstrsize, gerstrcap, isfirstattr, errbuf); \
  532. isfirstattr = 0; \
  533. } \
  534. #define GER_GET_ATTR_RIGHTA_EXT(c, inattrs, exattrs); \
  535. for ( i = 0; attrs[i]; i++ ) \
  536. { \
  537. if ((c) != *attrs[i] && charray_inlist((inattrs), attrs[i]) && \
  538. !charray_inlist((exattrs), attrs[i])) \
  539. { \
  540. _ger_get_attr_rights ( gerpb, e, subjectndn, attrs[i], \
  541. gerstr, gerstrsize, gerstrcap, isfirstattr, errbuf ); \
  542. isfirstattr = 0; \
  543. } \
  544. }
  545. void
  546. _ger_get_attrs_rights (
  547. Slapi_PBlock *gerpb,
  548. Slapi_Entry *e,
  549. const char *subjectndn,
  550. char **attrs,
  551. char **gerstr,
  552. size_t *gerstrsize,
  553. size_t *gerstrcap,
  554. char **errbuf
  555. )
  556. {
  557. int isfirstattr = 1;
  558. /* gerstr was initially allocated with enough space for one more line */
  559. _append_gerstr(gerstr, gerstrsize, gerstrcap, "attributeLevelRights: ", NULL);
  560. if (attrs && *attrs)
  561. {
  562. int i = 0;
  563. char **allattrs = NULL;
  564. char **opattrs = NULL;
  565. char **myattrs = NULL;
  566. char **thisattr = NULL;
  567. int hasstar = charray_inlist(attrs, "*");
  568. int hasplus = charray_inlist(attrs, "+");
  569. Slapi_Attr *objclasses = NULL;
  570. Slapi_ValueSet *objclassvals = NULL;
  571. int isextensibleobj = 0;
  572. /* get all attrs available for the entry */
  573. slapi_entry_attr_find(e, "objectclass", &objclasses);
  574. if (NULL != objclasses) {
  575. Slapi_Value *v;
  576. slapi_attr_get_valueset(objclasses, &objclassvals);
  577. i = slapi_valueset_first_value(objclassvals, &v);
  578. if (-1 != i)
  579. {
  580. const char *ocname = NULL;
  581. allattrs = slapi_schema_list_objectclass_attributes(
  582. (const char *)v->bv.bv_val,
  583. SLAPI_OC_FLAG_REQUIRED|SLAPI_OC_FLAG_ALLOWED);
  584. /* check if this entry is an extensble object or not */
  585. ocname = slapi_value_get_string(v);
  586. if ( strcasecmp( ocname, "extensibleobject" ) == 0 )
  587. {
  588. isextensibleobj = 1;
  589. }
  590. /* add "aci" to the allattrs to adjust to do_search */
  591. charray_add(&allattrs, slapi_attr_syntax_normalize("aci"));
  592. while (-1 != i)
  593. {
  594. i = slapi_valueset_next_value(objclassvals, i, &v);
  595. if (-1 != i)
  596. {
  597. myattrs = slapi_schema_list_objectclass_attributes(
  598. (const char *)v->bv.bv_val,
  599. SLAPI_OC_FLAG_REQUIRED|SLAPI_OC_FLAG_ALLOWED);
  600. /* check if this entry is an extensble object or not */
  601. ocname = slapi_value_get_string(v);
  602. if ( strcasecmp( ocname, "extensibleobject" ) == 0 )
  603. {
  604. isextensibleobj = 1;
  605. }
  606. charray_merge_nodup(&allattrs, myattrs, 1/*copy_strs*/);
  607. charray_free(myattrs);
  608. }
  609. }
  610. }
  611. slapi_valueset_free(objclassvals);
  612. }
  613. /* get operational attrs */
  614. opattrs = slapi_schema_list_attribute_names(SLAPI_ATTR_FLAG_OPATTR);
  615. if (isextensibleobj)
  616. {
  617. for ( i = 0; attrs[i]; i++ )
  618. {
  619. _ger_get_attr_rights ( gerpb, e, subjectndn, attrs[i], gerstr,
  620. gerstrsize, gerstrcap, isfirstattr, errbuf );
  621. isfirstattr = 0;
  622. }
  623. }
  624. else
  625. {
  626. if (hasstar && hasplus)
  627. {
  628. GER_GET_ATTR_RIGHTS(allattrs);
  629. GER_GET_ATTR_RIGHTS(opattrs);
  630. }
  631. else if (hasstar)
  632. {
  633. GER_GET_ATTR_RIGHTS(allattrs);
  634. GER_GET_ATTR_RIGHTA_EXT('*', opattrs, allattrs);
  635. }
  636. else if (hasplus)
  637. {
  638. GER_GET_ATTR_RIGHTS(opattrs);
  639. GER_GET_ATTR_RIGHTA_EXT('+', allattrs, opattrs);
  640. }
  641. else
  642. {
  643. for ( i = 0; attrs[i]; i++ )
  644. {
  645. if (charray_inlist(allattrs, attrs[i]) ||
  646. charray_inlist(opattrs, attrs[i]) ||
  647. (0 == strcasecmp(attrs[i], "dn")) ||
  648. (0 == strcasecmp(attrs[i], "distinguishedName")))
  649. {
  650. _ger_get_attr_rights ( gerpb, e, subjectndn, attrs[i],
  651. gerstr, gerstrsize, gerstrcap, isfirstattr, errbuf );
  652. isfirstattr = 0;
  653. }
  654. else
  655. {
  656. /* if the attr does not belong to the entry,
  657. "<attr>:none" is returned */
  658. if (!isfirstattr)
  659. {
  660. _append_gerstr(gerstr, gerstrsize, gerstrcap, ", ", NULL);
  661. }
  662. _append_gerstr(gerstr, gerstrsize, gerstrcap, attrs[i], ":");
  663. _append_gerstr(gerstr, gerstrsize, gerstrcap, "none", NULL);
  664. isfirstattr = 0;
  665. }
  666. }
  667. }
  668. }
  669. charray_free(allattrs);
  670. charray_free(opattrs);
  671. }
  672. else
  673. {
  674. Slapi_Attr *prevattr = NULL, *attr;
  675. char *type;
  676. while ( slapi_entry_next_attr ( e, prevattr, &attr ) == 0 )
  677. {
  678. if ( ! slapi_attr_flag_is_set (attr, SLAPI_ATTR_FLAG_OPATTR) )
  679. {
  680. slapi_attr_get_type ( attr, &type );
  681. _ger_get_attr_rights ( gerpb, e, subjectndn, type, gerstr,
  682. gerstrsize, gerstrcap, isfirstattr, errbuf );
  683. isfirstattr = 0;
  684. }
  685. prevattr = attr;
  686. }
  687. }
  688. if ( isfirstattr )
  689. {
  690. /* not a single attribute was retrived or specified */
  691. _append_gerstr(gerstr, gerstrsize, gerstrcap, "*:none", NULL);
  692. }
  693. return;
  694. }
  695. /*
  696. * controlType = LDAP_CONTROL_GET_EFFECTIVE_RIGHTS;
  697. * criticality = n/a;
  698. * controlValue = OCTET STRING of BER encoding of the SEQUENCE of
  699. * ENUMERATED LDAP code
  700. */
  701. void
  702. _ger_set_response_control (
  703. Slapi_PBlock *pb,
  704. int iscritical,
  705. int rc
  706. )
  707. {
  708. LDAPControl **resultctrls = NULL;
  709. LDAPControl gerrespctrl;
  710. BerElement *ber = NULL;
  711. struct berval *berval = NULL;
  712. int found = 0;
  713. int i;
  714. if ( (ber = der_alloc ()) == NULL )
  715. {
  716. goto bailout;
  717. }
  718. /* begin sequence, enumeration, end sequence */
  719. ber_printf ( ber, "{e}", rc );
  720. if ( ber_flatten ( ber, &berval ) != LDAP_SUCCESS )
  721. {
  722. goto bailout;
  723. }
  724. gerrespctrl.ldctl_oid = LDAP_CONTROL_GET_EFFECTIVE_RIGHTS;
  725. gerrespctrl.ldctl_iscritical = iscritical;
  726. gerrespctrl.ldctl_value.bv_val = berval->bv_val;
  727. gerrespctrl.ldctl_value.bv_len = berval->bv_len;
  728. slapi_pblock_get ( pb, SLAPI_RESCONTROLS, &resultctrls );
  729. for (i = 0; resultctrls && resultctrls[i]; i++)
  730. {
  731. if (strcmp(resultctrls[i]->ldctl_oid, LDAP_CONTROL_GET_EFFECTIVE_RIGHTS) == 0)
  732. {
  733. /*
  734. * We get here if search returns more than one entry
  735. * and this is not the first entry.
  736. */
  737. ldap_control_free ( resultctrls[i] );
  738. resultctrls[i] = slapi_dup_control (&gerrespctrl);
  739. found = 1;
  740. break;
  741. }
  742. }
  743. if ( !found )
  744. {
  745. /* slapi_pblock_set() will dup the control */
  746. slapi_pblock_set ( pb, SLAPI_ADD_RESCONTROL, &gerrespctrl );
  747. }
  748. bailout:
  749. ber_free ( ber, 1 ); /* ber_free() checks for NULL param */
  750. ber_bvfree ( berval ); /* ber_bvfree() checks for NULL param */
  751. }
  752. int
  753. _ger_generate_template_entry (
  754. Slapi_PBlock *pb
  755. )
  756. {
  757. Slapi_Entry *e = NULL;
  758. char **gerattrs = NULL;
  759. char **attrs = NULL;
  760. char *templateentry = NULL;
  761. char *object = NULL;
  762. char *superior = NULL;
  763. char *p = NULL;
  764. char *dn = NULL;
  765. int siz = 0;
  766. int len = 0;
  767. int i = 0;
  768. int notfirst = 0;
  769. int rc = LDAP_SUCCESS;
  770. slapi_pblock_get( pb, SLAPI_SEARCH_GERATTRS, &gerattrs );
  771. if (NULL == gerattrs)
  772. {
  773. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  774. "Objectclass info is expected "
  775. "in the attr list, e.g., \"*@person\"\n");
  776. rc = LDAP_SUCCESS;
  777. goto bailout;
  778. }
  779. /* get the target dn where the template entry is located */
  780. slapi_pblock_get( pb, SLAPI_TARGET_DN, &dn );
  781. for (i = 0; gerattrs && gerattrs[i]; i++)
  782. {
  783. object = strchr(gerattrs[i], '@');
  784. if (NULL != object && '\0' != *(++object))
  785. {
  786. break;
  787. }
  788. }
  789. if (NULL == object)
  790. {
  791. rc = LDAP_SUCCESS; /* no objectclass info; ok to return */
  792. goto bailout;
  793. }
  794. attrs = slapi_schema_list_objectclass_attributes(
  795. (const char *)object, SLAPI_OC_FLAG_REQUIRED);
  796. if (NULL == attrs)
  797. {
  798. rc = LDAP_SUCCESS; /* bogus objectclass info; ok to return */
  799. goto bailout;
  800. }
  801. for (i = 0; attrs[i]; i++)
  802. {
  803. if (0 == strcasecmp(attrs[i], "objectclass"))
  804. {
  805. /* <*attrp>: <object>\n\0 */
  806. siz += strlen(attrs[i]) + 4 + strlen(object);
  807. }
  808. else
  809. {
  810. /* <*attrp>: (template_attribute)\n\0 */
  811. siz += strlen(attrs[i]) + 4 + 20;
  812. }
  813. }
  814. if (dn)
  815. {
  816. /* dn: cn=<template_name>,<dn>\n\0 */
  817. siz += 32 + strlen(object) + strlen(dn);
  818. }
  819. else
  820. {
  821. /* dn: cn=<template_name>\n\0 */
  822. siz += 32 + strlen(object);
  823. }
  824. templateentry = (char *)slapi_ch_malloc(siz);
  825. if (NULL != dn && strlen(dn) > 0)
  826. {
  827. PR_snprintf(templateentry, siz,
  828. "dn: cn=template_%s_objectclass,%s\n", object, dn);
  829. }
  830. else
  831. {
  832. PR_snprintf(templateentry, siz,
  833. "dn: cn=template_%s_objectclass\n", object);
  834. }
  835. for (--i; i >= 0; i--)
  836. {
  837. len = strlen(templateentry);
  838. p = templateentry + len;
  839. if (0 == strcasecmp(attrs[i], "objectclass"))
  840. {
  841. PR_snprintf(p, siz - len, "%s: %s\n", attrs[i], object);
  842. }
  843. else
  844. {
  845. PR_snprintf(p, siz - len, "%s: (template_attribute)\n", attrs[i]);
  846. }
  847. }
  848. charray_free(attrs);
  849. while ((superior = slapi_schema_get_superior_name(object)) &&
  850. (0 != strcasecmp(superior, "top")))
  851. {
  852. if (notfirst)
  853. {
  854. slapi_ch_free_string(&object);
  855. }
  856. notfirst = 1;
  857. object = superior;
  858. attrs = slapi_schema_list_objectclass_attributes(
  859. (const char *)superior, SLAPI_OC_FLAG_REQUIRED);
  860. for (i = 0; attrs && attrs[i]; i++)
  861. {
  862. if (0 == strcasecmp(attrs[i], "objectclass"))
  863. {
  864. /* <*attrp>: <object>\n\0 */
  865. siz += strlen(attrs[i]) + 4 + strlen(object);
  866. }
  867. }
  868. templateentry = (char *)slapi_ch_realloc(templateentry, siz);
  869. for (--i; i >= 0; i--)
  870. {
  871. len = strlen(templateentry);
  872. p = templateentry + len;
  873. if (0 == strcasecmp(attrs[i], "objectclass"))
  874. {
  875. PR_snprintf(p, siz - len, "%s: %s\n", attrs[i], object);
  876. }
  877. }
  878. charray_free(attrs);
  879. }
  880. if (notfirst)
  881. {
  882. slapi_ch_free_string(&object);
  883. }
  884. slapi_ch_free_string(&superior);
  885. siz += 18; /* objectclass: top\n\0 */
  886. len = strlen(templateentry);
  887. templateentry = (char *)slapi_ch_realloc(templateentry, siz);
  888. p = templateentry + len;
  889. PR_snprintf(p, siz - len, "objectclass: top\n");
  890. e = slapi_str2entry(templateentry, SLAPI_STR2ENTRY_NOT_WELL_FORMED_LDIF);
  891. /* set the template entry to send the result to clients */
  892. slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, e);
  893. bailout:
  894. slapi_ch_free_string(&templateentry);
  895. return rc;
  896. }
  897. int
  898. acl_get_effective_rights (
  899. Slapi_PBlock *pb,
  900. Slapi_Entry *e, /* target entry */
  901. char **attrs, /* Attribute of the entry */
  902. struct berval *val, /* value of attr. NOT USED */
  903. int access, /* requested access rights */
  904. char **errbuf
  905. )
  906. {
  907. Slapi_PBlock *gerpb = NULL;
  908. void *aclcb = NULL;
  909. char *subjectndn = NULL;
  910. char *gerstr = NULL;
  911. size_t gerstrsize = 0;
  912. size_t gerstrcap = 0;
  913. int iscritical = 1;
  914. int rc = LDAP_SUCCESS;
  915. *errbuf = '\0';
  916. if (NULL == e) /* create a template entry from SLAPI_SEARCH_GERATTRS */
  917. {
  918. rc = _ger_generate_template_entry ( pb );
  919. slapi_pblock_get ( pb, SLAPI_SEARCH_RESULT_ENTRY, &e );
  920. if ( rc != LDAP_SUCCESS || NULL == e )
  921. {
  922. goto bailout;
  923. }
  924. }
  925. /*
  926. * Get the subject
  927. */
  928. rc = _ger_parse_control (pb, &subjectndn, &iscritical, errbuf );
  929. if ( rc != LDAP_SUCCESS )
  930. {
  931. goto bailout;
  932. }
  933. /*
  934. * The requestor should have g permission on the entry
  935. * to get the effective rights.
  936. */
  937. rc = _ger_g_permission_granted (pb, e, subjectndn, errbuf);
  938. if ( rc != LDAP_SUCCESS )
  939. {
  940. goto bailout;
  941. }
  942. /*
  943. * Construct a new pb
  944. */
  945. rc = _ger_new_gerpb ( pb, e, subjectndn, &gerpb, &aclcb, errbuf );
  946. if ( rc != LDAP_SUCCESS )
  947. {
  948. goto bailout;
  949. }
  950. /* Get entry level effective rights */
  951. _ger_get_entry_rights ( gerpb, e, subjectndn, &gerstr, &gerstrsize, &gerstrcap, errbuf );
  952. /*
  953. * Attribute level effective rights may not be NULL
  954. * even if entry level's is.
  955. */
  956. _ger_get_attrs_rights ( gerpb, e, subjectndn, attrs, &gerstr, &gerstrsize, &gerstrcap, errbuf );
  957. bailout:
  958. /*
  959. * Now construct the response control
  960. */
  961. _ger_set_response_control ( pb, iscritical, rc );
  962. if ( rc != LDAP_SUCCESS )
  963. {
  964. gerstr = slapi_ch_smprintf("entryLevelRights: %d\nattributeLevelRights: *:%d", rc, rc );
  965. }
  966. slapi_log_error (SLAPI_LOG_ACLSUMMARY, plugin_name,
  967. "###### Effective Rights on Entry (%s) for Subject (%s) ######\n",
  968. e?slapi_entry_get_ndn(e):"null", subjectndn?subjectndn:"null");
  969. slapi_log_error (SLAPI_LOG_ACLSUMMARY, plugin_name, "%s\n", gerstr);
  970. /* Restore pb */
  971. _ger_release_gerpb ( &gerpb, &aclcb, pb );
  972. /*
  973. * General plugin uses SLAPI_RESULT_TEXT for error text. Here
  974. * SLAPI_PB_RESULT_TEXT is exclusively shared with add, dse and schema.
  975. * slapi_pblock_set() will free any previous data, and
  976. * pblock_done() will free SLAPI_PB_RESULT_TEXT.
  977. */
  978. slapi_pblock_set (pb, SLAPI_PB_RESULT_TEXT, gerstr);
  979. if ( !iscritical )
  980. {
  981. /*
  982. * If return code is not LDAP_SUCCESS, the server would
  983. * abort sending the data of the entry to the client.
  984. */
  985. rc = LDAP_SUCCESS;
  986. }
  987. slapi_ch_free ( (void **) &subjectndn );
  988. slapi_ch_free ( (void **) &gerstr );
  989. return rc;
  990. }