acleffectiverights.c 31 KB

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