acleffectiverights.c 28 KB

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