acleffectiverights.c 31 KB

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