acleffectiverights.c 31 KB

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