acleffectiverights.c 30 KB

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