acleffectiverights.c 28 KB

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