acleffectiverights.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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. /*
  561. * If it's stated attribute list is given,
  562. * the first attr in the list should not be empty.
  563. * Otherwise, it's considered the list is not given.
  564. */
  565. if (attrs && *attrs && (strlen(*attrs) > 0))
  566. {
  567. int i = 0;
  568. char **allattrs = NULL;
  569. char **opattrs = NULL;
  570. char **myattrs = NULL;
  571. char **thisattr = NULL;
  572. int hasstar = charray_inlist(attrs, "*");
  573. int hasplus = charray_inlist(attrs, "+");
  574. Slapi_Attr *objclasses = NULL;
  575. Slapi_ValueSet *objclassvals = NULL;
  576. int isextensibleobj = 0;
  577. /* get all attrs available for the entry */
  578. slapi_entry_attr_find(e, "objectclass", &objclasses);
  579. if (NULL != objclasses) {
  580. Slapi_Value *v;
  581. slapi_attr_get_valueset(objclasses, &objclassvals);
  582. i = slapi_valueset_first_value(objclassvals, &v);
  583. if (-1 != i)
  584. {
  585. const char *ocname = NULL;
  586. allattrs = slapi_schema_list_objectclass_attributes(
  587. (const char *)v->bv.bv_val,
  588. SLAPI_OC_FLAG_REQUIRED|SLAPI_OC_FLAG_ALLOWED);
  589. /* check if this entry is an extensble object or not */
  590. ocname = slapi_value_get_string(v);
  591. if ( strcasecmp( ocname, "extensibleobject" ) == 0 )
  592. {
  593. isextensibleobj = 1;
  594. }
  595. /* add "aci" to the allattrs to adjust to do_search */
  596. charray_add(&allattrs, slapi_attr_syntax_normalize("aci"));
  597. while (-1 != i)
  598. {
  599. i = slapi_valueset_next_value(objclassvals, i, &v);
  600. if (-1 != i)
  601. {
  602. myattrs = slapi_schema_list_objectclass_attributes(
  603. (const char *)v->bv.bv_val,
  604. SLAPI_OC_FLAG_REQUIRED|SLAPI_OC_FLAG_ALLOWED);
  605. /* check if this entry is an extensble object or not */
  606. ocname = slapi_value_get_string(v);
  607. if ( strcasecmp( ocname, "extensibleobject" ) == 0 )
  608. {
  609. isextensibleobj = 1;
  610. }
  611. charray_merge_nodup(&allattrs, myattrs, 1/*copy_strs*/);
  612. charray_free(myattrs);
  613. }
  614. }
  615. }
  616. slapi_valueset_free(objclassvals);
  617. }
  618. /* get operational attrs */
  619. opattrs = slapi_schema_list_attribute_names(SLAPI_ATTR_FLAG_OPATTR);
  620. if (isextensibleobj)
  621. {
  622. for ( i = 0; attrs[i]; i++ )
  623. {
  624. if ('\0' == *attrs[i]) {
  625. continue; /* skip an empty attr */
  626. }
  627. _ger_get_attr_rights ( gerpb, e, subjectndn, attrs[i], gerstr,
  628. gerstrsize, gerstrcap, isfirstattr, errbuf );
  629. isfirstattr = 0;
  630. }
  631. }
  632. else
  633. {
  634. if (hasstar && hasplus)
  635. {
  636. GER_GET_ATTR_RIGHTS(allattrs);
  637. GER_GET_ATTR_RIGHTS(opattrs);
  638. }
  639. else if (hasstar)
  640. {
  641. GER_GET_ATTR_RIGHTS(allattrs);
  642. GER_GET_ATTR_RIGHTA_EXT('*', opattrs, allattrs);
  643. }
  644. else if (hasplus)
  645. {
  646. GER_GET_ATTR_RIGHTS(opattrs);
  647. GER_GET_ATTR_RIGHTA_EXT('+', allattrs, opattrs);
  648. }
  649. else
  650. {
  651. for ( i = 0; attrs[i]; i++ )
  652. {
  653. if ('\0' == *attrs[i]) {
  654. continue; /* skip an empty attr */
  655. }
  656. if (charray_inlist(allattrs, attrs[i]) ||
  657. charray_inlist(opattrs, attrs[i]) ||
  658. (0 == strcasecmp(attrs[i], "dn")) ||
  659. (0 == strcasecmp(attrs[i], "distinguishedName")))
  660. {
  661. _ger_get_attr_rights ( gerpb, e, subjectndn, attrs[i],
  662. gerstr, gerstrsize, gerstrcap, isfirstattr, errbuf );
  663. isfirstattr = 0;
  664. }
  665. else
  666. {
  667. /* if the attr does not belong to the entry,
  668. "<attr>:none" is returned */
  669. if (!isfirstattr)
  670. {
  671. _append_gerstr(gerstr, gerstrsize, gerstrcap, ", ", NULL);
  672. }
  673. _append_gerstr(gerstr, gerstrsize, gerstrcap, attrs[i], ":");
  674. _append_gerstr(gerstr, gerstrsize, gerstrcap, "none", NULL);
  675. isfirstattr = 0;
  676. }
  677. }
  678. }
  679. }
  680. charray_free(allattrs);
  681. charray_free(opattrs);
  682. }
  683. else
  684. {
  685. Slapi_Attr *prevattr = NULL, *attr;
  686. char *type;
  687. while ( slapi_entry_next_attr ( e, prevattr, &attr ) == 0 )
  688. {
  689. if ( ! slapi_attr_flag_is_set (attr, SLAPI_ATTR_FLAG_OPATTR) )
  690. {
  691. slapi_attr_get_type ( attr, &type );
  692. _ger_get_attr_rights ( gerpb, e, subjectndn, type, gerstr,
  693. gerstrsize, gerstrcap, isfirstattr, errbuf );
  694. isfirstattr = 0;
  695. }
  696. prevattr = attr;
  697. }
  698. }
  699. if ( isfirstattr )
  700. {
  701. /* not a single attribute was retrived or specified */
  702. _append_gerstr(gerstr, gerstrsize, gerstrcap, "*:none", NULL);
  703. }
  704. return;
  705. }
  706. /*
  707. * controlType = LDAP_CONTROL_GET_EFFECTIVE_RIGHTS;
  708. * criticality = n/a;
  709. * controlValue = OCTET STRING of BER encoding of the SEQUENCE of
  710. * ENUMERATED LDAP code
  711. */
  712. void
  713. _ger_set_response_control (
  714. Slapi_PBlock *pb,
  715. int iscritical,
  716. int rc
  717. )
  718. {
  719. LDAPControl **resultctrls = NULL;
  720. LDAPControl gerrespctrl;
  721. BerElement *ber = NULL;
  722. struct berval *berval = NULL;
  723. int found = 0;
  724. int i;
  725. if ( (ber = der_alloc ()) == NULL )
  726. {
  727. goto bailout;
  728. }
  729. /* begin sequence, enumeration, end sequence */
  730. ber_printf ( ber, "{e}", rc );
  731. if ( ber_flatten ( ber, &berval ) != LDAP_SUCCESS )
  732. {
  733. goto bailout;
  734. }
  735. gerrespctrl.ldctl_oid = LDAP_CONTROL_GET_EFFECTIVE_RIGHTS;
  736. gerrespctrl.ldctl_iscritical = iscritical;
  737. gerrespctrl.ldctl_value.bv_val = berval->bv_val;
  738. gerrespctrl.ldctl_value.bv_len = berval->bv_len;
  739. slapi_pblock_get ( pb, SLAPI_RESCONTROLS, &resultctrls );
  740. for (i = 0; resultctrls && resultctrls[i]; i++)
  741. {
  742. if (strcmp(resultctrls[i]->ldctl_oid, LDAP_CONTROL_GET_EFFECTIVE_RIGHTS) == 0)
  743. {
  744. /*
  745. * We get here if search returns more than one entry
  746. * and this is not the first entry.
  747. */
  748. ldap_control_free ( resultctrls[i] );
  749. resultctrls[i] = slapi_dup_control (&gerrespctrl);
  750. found = 1;
  751. break;
  752. }
  753. }
  754. if ( !found )
  755. {
  756. /* slapi_pblock_set() will dup the control */
  757. slapi_pblock_set ( pb, SLAPI_ADD_RESCONTROL, &gerrespctrl );
  758. }
  759. bailout:
  760. ber_free ( ber, 1 ); /* ber_free() checks for NULL param */
  761. ber_bvfree ( berval ); /* ber_bvfree() checks for NULL param */
  762. }
  763. int
  764. _ger_generate_template_entry (
  765. Slapi_PBlock *pb
  766. )
  767. {
  768. Slapi_Entry *e = NULL;
  769. char **gerattrs = NULL;
  770. char **attrs = NULL;
  771. char **allowedattrs = NULL;
  772. char *templateentry = NULL;
  773. char *object = NULL;
  774. char *superior = NULL;
  775. char *p = NULL;
  776. char *dn = NULL;
  777. char *dntype = NULL;
  778. int siz = 0;
  779. int len = 0;
  780. int i = 0;
  781. int notfirst = 0;
  782. int rc = LDAP_SUCCESS;
  783. slapi_pblock_get( pb, SLAPI_SEARCH_GERATTRS, &gerattrs );
  784. if (NULL == gerattrs)
  785. {
  786. slapi_log_error (SLAPI_LOG_FATAL, plugin_name,
  787. "Objectclass info is expected "
  788. "in the attr list, e.g., \"*@person\"\n");
  789. rc = LDAP_SUCCESS;
  790. goto bailout;
  791. }
  792. /* get the target dn where the template entry is located */
  793. slapi_pblock_get( pb, SLAPI_TARGET_DN, &dn );
  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. if (dn)
  843. {
  844. /* dn: <attr>=<template_name>,<dn>\n\0 */
  845. if (dntype) {
  846. siz += strlen(dntype) + 30 + strlen(object) + strlen(dn);
  847. } else {
  848. siz += strlen(attrs[0]) + 30 + strlen(object) + strlen(dn);
  849. }
  850. }
  851. else
  852. {
  853. /* dn: <attr>=<template_name>\n\0 */
  854. if (dntype) {
  855. siz += strlen(dntype) + 30 + strlen(object);
  856. } else {
  857. siz += strlen(attrs[0]) + 30 + strlen(object);
  858. }
  859. }
  860. templateentry = (char *)slapi_ch_malloc(siz);
  861. if (NULL != dn && strlen(dn) > 0)
  862. {
  863. PR_snprintf(templateentry, siz,
  864. "dn: %s=template_%s_objectclass,%s\n",
  865. dntype?dntype:attrs[0], object, dn);
  866. }
  867. else
  868. {
  869. PR_snprintf(templateentry, siz,
  870. "dn: %s=template_%s_objectclass\n",
  871. dntype?dntype:attrs[0], object);
  872. }
  873. for (--i; i >= 0; i--)
  874. {
  875. len = strlen(templateentry);
  876. p = templateentry + len;
  877. if (0 == strcasecmp(attrs[i], "objectclass"))
  878. {
  879. PR_snprintf(p, siz - len, "%s: %s\n", attrs[i], object);
  880. }
  881. else
  882. {
  883. PR_snprintf(p, siz - len, "%s: (template_attribute)\n", attrs[i]);
  884. }
  885. }
  886. charray_free(attrs);
  887. while ((superior = slapi_schema_get_superior_name(object)) &&
  888. (0 != strcasecmp(superior, "top")))
  889. {
  890. if (notfirst)
  891. {
  892. slapi_ch_free_string(&object);
  893. }
  894. notfirst = 1;
  895. object = superior;
  896. attrs = slapi_schema_list_objectclass_attributes(
  897. (const char *)superior, SLAPI_OC_FLAG_REQUIRED);
  898. for (i = 0; attrs && attrs[i]; i++)
  899. {
  900. if (0 == strcasecmp(attrs[i], "objectclass"))
  901. {
  902. /* <*attrp>: <object>\n\0 */
  903. siz += strlen(attrs[i]) + 4 + strlen(object);
  904. }
  905. }
  906. templateentry = (char *)slapi_ch_realloc(templateentry, siz);
  907. for (--i; i >= 0; i--)
  908. {
  909. len = strlen(templateentry);
  910. p = templateentry + len;
  911. if (0 == strcasecmp(attrs[i], "objectclass"))
  912. {
  913. PR_snprintf(p, siz - len, "%s: %s\n", attrs[i], object);
  914. }
  915. }
  916. charray_free(attrs);
  917. }
  918. if (notfirst)
  919. {
  920. slapi_ch_free_string(&object);
  921. }
  922. slapi_ch_free_string(&superior);
  923. siz += 18; /* objectclass: top\n\0 */
  924. len = strlen(templateentry);
  925. templateentry = (char *)slapi_ch_realloc(templateentry, siz);
  926. p = templateentry + len;
  927. PR_snprintf(p, siz - len, "objectclass: top\n");
  928. e = slapi_str2entry(templateentry, SLAPI_STR2ENTRY_NOT_WELL_FORMED_LDIF);
  929. /* set the template entry to send the result to clients */
  930. slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, e);
  931. bailout:
  932. slapi_ch_free_string(&templateentry);
  933. return rc;
  934. }
  935. int
  936. acl_get_effective_rights (
  937. Slapi_PBlock *pb,
  938. Slapi_Entry *e, /* target entry */
  939. char **attrs, /* Attribute of the entry */
  940. struct berval *val, /* value of attr. NOT USED */
  941. int access, /* requested access rights */
  942. char **errbuf
  943. )
  944. {
  945. Slapi_PBlock *gerpb = NULL;
  946. void *aclcb = NULL;
  947. char *subjectndn = NULL;
  948. char *gerstr = NULL;
  949. size_t gerstrsize = 0;
  950. size_t gerstrcap = 0;
  951. int iscritical = 1;
  952. int rc = LDAP_SUCCESS;
  953. *errbuf = '\0';
  954. if (NULL == e) /* create a template entry from SLAPI_SEARCH_GERATTRS */
  955. {
  956. rc = _ger_generate_template_entry ( pb );
  957. slapi_pblock_get ( pb, SLAPI_SEARCH_RESULT_ENTRY, &e );
  958. if ( rc != LDAP_SUCCESS || NULL == e )
  959. {
  960. goto bailout;
  961. }
  962. }
  963. /*
  964. * Get the subject
  965. */
  966. rc = _ger_parse_control (pb, &subjectndn, &iscritical, errbuf );
  967. if ( rc != LDAP_SUCCESS )
  968. {
  969. goto bailout;
  970. }
  971. /*
  972. * The requestor should have g permission on the entry
  973. * to get the effective rights.
  974. */
  975. rc = _ger_g_permission_granted (pb, e, subjectndn, errbuf);
  976. if ( rc != LDAP_SUCCESS )
  977. {
  978. goto bailout;
  979. }
  980. /*
  981. * Construct a new pb
  982. */
  983. rc = _ger_new_gerpb ( pb, e, subjectndn, &gerpb, &aclcb, errbuf );
  984. if ( rc != LDAP_SUCCESS )
  985. {
  986. goto bailout;
  987. }
  988. /* Get entry level effective rights */
  989. _ger_get_entry_rights ( gerpb, e, subjectndn, &gerstr, &gerstrsize, &gerstrcap, errbuf );
  990. /*
  991. * Attribute level effective rights may not be NULL
  992. * even if entry level's is.
  993. */
  994. _ger_get_attrs_rights ( gerpb, e, subjectndn, attrs, &gerstr, &gerstrsize, &gerstrcap, errbuf );
  995. bailout:
  996. /*
  997. * Now construct the response control
  998. */
  999. _ger_set_response_control ( pb, iscritical, rc );
  1000. if ( rc != LDAP_SUCCESS )
  1001. {
  1002. gerstr = slapi_ch_smprintf("entryLevelRights: %d\nattributeLevelRights: *:%d", rc, rc );
  1003. }
  1004. slapi_log_error (SLAPI_LOG_ACLSUMMARY, plugin_name,
  1005. "###### Effective Rights on Entry (%s) for Subject (%s) ######\n",
  1006. e?slapi_entry_get_ndn(e):"null", subjectndn?subjectndn:"null");
  1007. slapi_log_error (SLAPI_LOG_ACLSUMMARY, plugin_name, "%s\n", gerstr);
  1008. /* Restore pb */
  1009. _ger_release_gerpb ( &gerpb, &aclcb, pb );
  1010. /*
  1011. * General plugin uses SLAPI_RESULT_TEXT for error text. Here
  1012. * SLAPI_PB_RESULT_TEXT is exclusively shared with add, dse and schema.
  1013. * slapi_pblock_set() will free any previous data, and
  1014. * pblock_done() will free SLAPI_PB_RESULT_TEXT.
  1015. */
  1016. slapi_pblock_set (pb, SLAPI_PB_RESULT_TEXT, gerstr);
  1017. if ( !iscritical )
  1018. {
  1019. /*
  1020. * If return code is not LDAP_SUCCESS, the server would
  1021. * abort sending the data of the entry to the client.
  1022. */
  1023. rc = LDAP_SUCCESS;
  1024. }
  1025. slapi_ch_free ( (void **) &subjectndn );
  1026. slapi_ch_free ( (void **) &gerstr );
  1027. return rc;
  1028. }