acleffectiverights.c 30 KB

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