acleffectiverights.c 26 KB

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