acleffectiverights.c 28 KB

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