1
0

acleffectiverights.c 30 KB

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