rootdse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /* rootdse.c - routines to manage the root DSE */
  39. #include <stdio.h>
  40. #include "slap.h"
  41. #include "fe.h"
  42. /* XXXmcs: why not look at the NO-USER-MODIFICATION flag instead? */
  43. static char *readonly_attributes[] = {
  44. "namingcontexts",
  45. "nsBackendSuffix",
  46. "subschemasubentry",
  47. "supportedldapversion",
  48. "supportedcontrol",
  49. "supportedextension",
  50. "supportedsaslmechanisms",
  51. "dataversion",
  52. "ref",
  53. "vendorName",
  54. "vendorVersion",
  55. ATTR_NETSCAPEMDSUFFIX,
  56. NULL
  57. };
  58. static char *writable_attributes[] = {
  59. "copiedfrom",
  60. "copyingfrom",
  61. "aci",
  62. NULL
  63. };
  64. /*
  65. * function: is_readonly_attr
  66. * args: attr - candidate attribute name
  67. * returns: 0 if this attribute may be written to the root DSE
  68. * 1 if it may not be written.
  69. * notes: should probably be integrated into syntax AVL tree, so that
  70. * this list can be configured at runtime.
  71. */
  72. static int
  73. rootdse_is_readonly_attr( char *attr )
  74. {
  75. int i;
  76. if ( NULL == attr ) {
  77. return 1; /* I guess. It's not really an attribute at all */
  78. }
  79. if ( NULL == readonly_attributes ) {
  80. return 0;
  81. }
  82. /*
  83. * optimization: check for attributes we're likely to be writing
  84. * frequently.
  85. */
  86. for ( i = 0; NULL != writable_attributes[ i ]; i++ ) {
  87. if ( strncasecmp( attr, writable_attributes[ i ],
  88. strlen( writable_attributes[ i ])) == 0 ) {
  89. return 0;
  90. }
  91. }
  92. for ( i = 0; NULL != readonly_attributes[ i ]; i++ ) {
  93. if ( strncasecmp( attr, readonly_attributes[ i ],
  94. strlen( readonly_attributes[ i ])) == 0 ) {
  95. return 1;
  96. }
  97. }
  98. return 0;
  99. }
  100. /*
  101. * Handle a read operation on the root DSE (the entry with DN "");
  102. * Note: we're copying a lot of attributes here. It might be better
  103. * to keep track of which we really need to free, and arrange that
  104. * the others are unlinked from the attribute list of the entry
  105. * before calling slapi_entry_free().
  106. */
  107. int
  108. read_root_dse( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *entryAfter, int *returncode, char *returntext, void *arg )
  109. {
  110. int i;
  111. struct berval *vals[2];
  112. struct berval val;
  113. struct berval **bvals;
  114. Slapi_Backend *be;
  115. char *cookie = NULL;
  116. char **strs;
  117. void *node;
  118. Slapi_DN *sdn;
  119. /*
  120. * Check that we were doing a base search on the root dse.
  121. */
  122. {
  123. int scope;
  124. slapi_pblock_get( pb, SLAPI_SEARCH_SCOPE, &scope );
  125. if(scope!=LDAP_SCOPE_BASE)
  126. {
  127. *returncode= LDAP_NO_SUCH_OBJECT;
  128. return SLAPI_DSE_CALLBACK_ERROR;
  129. }
  130. }
  131. vals[0] = &val;
  132. vals[1] = NULL;
  133. /* loop through backend suffixes to get namingcontexts attr */
  134. attrlist_delete( &e->e_attrs, "namingcontexts");
  135. sdn = slapi_get_first_suffix(&node, 0);
  136. while (sdn)
  137. {
  138. val.bv_val = (char*)slapi_sdn_get_dn(sdn); /* jcm: had to cast away const */
  139. val.bv_len = strlen( val.bv_val );
  140. attrlist_merge( &e->e_attrs, "namingcontexts", vals );
  141. sdn = slapi_get_next_suffix(&node, 0);
  142. }
  143. attrlist_delete( &e->e_attrs, "nsBackendSuffix");
  144. for (be = slapi_get_first_backend(&cookie); be != NULL;
  145. be = slapi_get_next_backend(cookie)) {
  146. char * base;
  147. char * be_name;
  148. const Slapi_DN *be_suffix;
  149. if (slapi_be_private(be)) continue;
  150. /* tolerate a backend under construction containing no suffix */
  151. if ((be_suffix = slapi_be_getsuffix(be, 0)) == NULL) continue;
  152. if ((base = (char *)slapi_sdn_get_dn(be_suffix)) == NULL) continue;
  153. if ((be_name = slapi_be_get_name(be)) == NULL) continue;
  154. val.bv_len = strlen(base)+strlen(be_name)+1;
  155. val.bv_val = slapi_ch_malloc(val.bv_len+1);
  156. sprintf(val.bv_val, "%s:%s", be_name, base);
  157. attrlist_merge(&e->e_attrs, "nsBackendSuffix", vals);
  158. slapi_ch_free((void **) &val.bv_val);
  159. }
  160. slapi_ch_free((void **)&cookie);
  161. /* schema entry */
  162. val.bv_val = SLAPD_SCHEMA_DN;
  163. val.bv_len = sizeof( SLAPD_SCHEMA_DN ) - 1;
  164. attrlist_replace( &e->e_attrs, "subschemasubentry", vals );
  165. /* supported extended operations */
  166. attrlist_delete( &e->e_attrs, "supportedExtension");
  167. if (( strs = slapi_get_supported_extended_ops_copy()) != NULL ) {
  168. for ( i = 0; strs[i] != NULL; ++i ) {
  169. val.bv_val = strs[i];
  170. val.bv_len = strlen( strs[i] );
  171. attrlist_merge( &e->e_attrs, "supportedExtension", vals );
  172. }
  173. charray_free(strs);
  174. }
  175. /* supported controls */
  176. attrlist_delete( &e->e_attrs, "supportedControl");
  177. if ( slapi_get_supported_controls_copy( &strs, NULL ) == 0
  178. && strs != NULL ) {
  179. for ( i = 0; strs[i] != NULL; ++i ) {
  180. val.bv_val = strs[i];
  181. val.bv_len = strlen( strs[i] );
  182. attrlist_merge( &e->e_attrs, "supportedControl", vals );
  183. }
  184. charray_free(strs);
  185. }
  186. /* supported sasl mechanisms */
  187. attrlist_delete( &e->e_attrs, "supportedSASLMechanisms");
  188. if (( strs = ids_sasl_listmech (pb)) != NULL ) {
  189. for ( i = 0; strs[i] != NULL; ++i ) {
  190. val.bv_val = strs[i];
  191. val.bv_len = strlen( strs[i] );
  192. attrlist_merge( &e->e_attrs, "supportedSASLMechanisms", vals );
  193. }
  194. charray_free(strs);
  195. }
  196. /* supported LDAP versions */
  197. val.bv_val = "2";
  198. val.bv_len = 1;
  199. attrlist_replace( &e->e_attrs, "supportedldapversion", vals );
  200. val.bv_val = "3";
  201. val.bv_len = 1;
  202. attrlist_merge( &e->e_attrs, "supportedldapversion", vals );
  203. /* superior references (ref attribute) */
  204. attrlist_delete( &e->e_attrs, "ref");
  205. if (( bvals = g_get_default_referral()) != NULL ) {
  206. for ( i = 0; bvals[i] != NULL; ++i ) {
  207. val.bv_val = bvals[i]->bv_val;
  208. val.bv_len = bvals[i]->bv_len;
  209. attrlist_merge( &e->e_attrs, "ref", vals );
  210. }
  211. }
  212. /* RFC 3045 attributes: vendorName and vendorVersion */
  213. val.bv_val = SLAPD_VENDOR_NAME;
  214. val.bv_len = strlen( val.bv_val );
  215. attrlist_replace( &e->e_attrs, "vendorName", vals );
  216. val.bv_val = slapd_get_version_value();
  217. val.bv_len = strlen( val.bv_val );
  218. attrlist_replace( &e->e_attrs, "vendorVersion", vals );
  219. slapi_ch_free( (void **)&val.bv_val );
  220. /* Server Data Version */
  221. if (( val.bv_val = (char*)get_server_dataversion()) != NULL ) { /* jcm cast away const */
  222. val.bv_len = strlen( val.bv_val );
  223. attrlist_replace( &e->e_attrs, attr_dataversion, vals );
  224. }
  225. /* machine data suffix
  226. * this has been added in 4.0 for replication purpose
  227. * and since 5.0 is now unused by the core server,
  228. * however some functionalities of the console framework 5.01
  229. * still depend on this
  230. */
  231. if (( val.bv_val = get_config_DN()) != NULL ) {
  232. val.bv_len = strlen( val.bv_val );
  233. attrlist_replace( &e->e_attrs, ATTR_NETSCAPEMDSUFFIX, vals );
  234. }
  235. #ifdef notdef
  236. /* XXXggood testing - print the size of the changelog db */
  237. {
  238. unsigned int clsize;
  239. clsize = get_changelog_size();
  240. sprintf( buf, "%u", clsize );
  241. val.bv_val = buf;
  242. val.bv_len = strlen( buf );
  243. attrlist_replace( &e->e_attrs, "changelogsize", vals );
  244. slapi_ch_free((void**)&val.bv_val );
  245. }
  246. #endif /* notdef */
  247. /* vlvsearch is list of dns to VLV Search Specifications */
  248. attrlist_delete( &e->e_attrs, "vlvsearch");
  249. cookie = NULL;
  250. be = slapi_get_first_backend(&cookie);
  251. while (be)
  252. {
  253. if(!be->be_private)
  254. {
  255. /* Generate searches to find the VLV Search Specifications */
  256. int r;
  257. Slapi_PBlock *resultpb= NULL;
  258. Slapi_Entry** entry = NULL;
  259. Slapi_DN dn;
  260. slapi_sdn_init(&dn);
  261. be_getconfigdn(be,&dn);
  262. resultpb= slapi_search_internal( slapi_sdn_get_ndn(&dn), LDAP_SCOPE_ONELEVEL, "objectclass=vlvsearch", NULL, NULL, 1);
  263. slapi_sdn_done(&dn);
  264. slapi_pblock_get( resultpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entry );
  265. slapi_pblock_get( resultpb, SLAPI_PLUGIN_INTOP_RESULT, &r );
  266. if(r==LDAP_SUCCESS)
  267. {
  268. for (; *entry; ++entry)
  269. {
  270. val.bv_val = slapi_entry_get_dn(*entry);
  271. val.bv_len = strlen( val.bv_val );
  272. attrlist_merge( &e->e_attrs, "vlvsearch", vals );
  273. }
  274. }
  275. slapi_free_search_results_internal(resultpb);
  276. slapi_pblock_destroy(resultpb);
  277. }
  278. be = slapi_get_next_backend (cookie);
  279. }
  280. slapi_ch_free ((void **)&cookie);
  281. *returncode= LDAP_SUCCESS;
  282. return SLAPI_DSE_CALLBACK_OK;
  283. }
  284. /*
  285. * Handle a modification request on the root DSE. Only certain
  286. * attributes are writable. If an attempt to modify an attribute
  287. * which is not allowed, return LDAP_UNWILLING_TO_PERFORM, unless
  288. * the modification is disallowed because of ACL checking, in which
  289. * case LDAP_INSUFFICIENT_ACCESS is returned.
  290. */
  291. int
  292. modify_root_dse( Slapi_PBlock *pb, Slapi_Entry *entryBefore, Slapi_Entry *e, int *returncode, char *returntext, void *arg )
  293. {
  294. LDAPMod **mods;
  295. /*
  296. * Make a pass through the attributes and check them
  297. * to make sure none are computed. Also check for
  298. * reflected attributes and reject those as well.
  299. * Eventually, some reflected attributes might be
  300. * writable, but for now, none are.
  301. */
  302. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  303. if ( NULL != mods )
  304. {
  305. int i;
  306. for ( i = 0; NULL != mods[ i ]; i++ )
  307. {
  308. if ( rootdse_is_readonly_attr( mods[ i ]->mod_type ))
  309. {
  310. /* The modification is disallowed */
  311. *returncode = LDAP_UNWILLING_TO_PERFORM;
  312. strcpy(returntext,"Modification of these root DSE attributes not allowed");
  313. return SLAPI_DSE_CALLBACK_ERROR;
  314. }
  315. }
  316. }
  317. *returncode= LDAP_SUCCESS;
  318. return SLAPI_DSE_CALLBACK_OK; /* success -- apply the changes */
  319. }