schemaparse.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* schemaparse.c - routines to support objectclass definitions */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #include "slap.h"
  46. /* global_oc and global_schema_csn are both protected by oc locks */
  47. struct objclass *global_oc;
  48. CSN *global_schema_csn = NULL; /* Timestamp for last update CSN. NULL = epoch */
  49. static int is_duplicate( char *target, char **list, int list_max );
  50. static void normalize_list( char **list );
  51. /* R/W lock used to protect the global objclass linked list. */
  52. static PRRWLock *oc_lock = NULL;
  53. /*
  54. * The oc_init_lock_callonce structure is used by NSPR to ensure
  55. * that oc_init_lock() is called at most once.
  56. */
  57. static PRCallOnceType oc_init_lock_callonce = { 0, 0, 0 };
  58. /* Create the objectclass read/write lock. Returns PRSuccess if successful */
  59. static PRStatus
  60. oc_init_lock( void )
  61. {
  62. if ( NULL == ( oc_lock = PR_NewRWLock( PR_RWLOCK_RANK_NONE,
  63. "objectclass rwlock" ))) {
  64. slapi_log_error( SLAPI_LOG_FATAL, "oc_init_lock",
  65. "PR_NewRWLock() for objectclass lock failed\n" );
  66. return PR_FAILURE;
  67. }
  68. return PR_SUCCESS;
  69. }
  70. void
  71. oc_lock_read( void )
  72. {
  73. if ( NULL != oc_lock ||
  74. PR_SUCCESS == PR_CallOnce( &oc_init_lock_callonce, oc_init_lock )) {
  75. PR_RWLock_Rlock( oc_lock );
  76. }
  77. }
  78. void
  79. oc_lock_write( void )
  80. {
  81. if ( NULL != oc_lock ||
  82. PR_SUCCESS == PR_CallOnce( &oc_init_lock_callonce, oc_init_lock )) {
  83. PR_RWLock_Wlock( oc_lock );
  84. }
  85. }
  86. void
  87. oc_unlock( void )
  88. {
  89. if ( oc_lock != NULL ) {
  90. PR_RWLock_Unlock( oc_lock );
  91. }
  92. }
  93. /*
  94. * Note: callers of g_get_global_oc_nolock() must hold a read or write lock
  95. */
  96. struct objclass* g_get_global_oc_nolock()
  97. {
  98. return global_oc;
  99. }
  100. /*
  101. * Note: callers of g_set_global_oc_nolock() must hold a write lock
  102. */
  103. void
  104. g_set_global_oc_nolock( struct objclass *newglobaloc )
  105. {
  106. global_oc = newglobaloc;
  107. }
  108. /*
  109. * Note: callers of g_get_global_schema_csn() must hold a read lock
  110. */
  111. const CSN *
  112. g_get_global_schema_csn()
  113. {
  114. return global_schema_csn;
  115. }
  116. /*
  117. * Note: callers of g_set_global_schema_csn() must hold a write lock.
  118. * csn is consumed.
  119. */
  120. void
  121. g_set_global_schema_csn(CSN *csn)
  122. {
  123. CSN *tmp = NULL;
  124. if (NULL != global_schema_csn)
  125. {
  126. tmp = global_schema_csn;
  127. }
  128. global_schema_csn = csn;
  129. if (NULL != tmp)
  130. {
  131. csn_free(&tmp);
  132. }
  133. }
  134. /*
  135. * There are two kinds of objectclasses:
  136. * Standard Objectclasses and User Defined Objectclasses
  137. *
  138. * Standard Objectclasses are the objectclasses which come with the Directory Server.
  139. * These objectclasses are always expected to be there and shouldn't be accidentally
  140. * changed by the end user. We dont' allow these objectclasses to be deleted, and the
  141. * admin CGIs will not allow the end user to change their definitions. However, we
  142. * will allow these objectclasses to be redefined via ldap_modify, by doing an LDAP_MOD_ADD.
  143. * The new definition will override the previous definition. The updated objectclass
  144. * will be written out the 00user.ldif and the original definition will stay
  145. * whereever it was originally defined. At startup, slapd will use the last definition
  146. * read as the real definition of an objectclass.
  147. *
  148. * User Defined ObjectClasses are objectclasses which were added to the Directory Server
  149. * by the end user. These objectclasses are also kept in 99user.ldif. These objectclasses
  150. * can be deleted by the end user.
  151. *
  152. * Every objectclass contains an array of attributes called oc_orig_required,
  153. * which are the required attributes for that objectclass which were not inherited from
  154. * any other objectclass. Likewise, there's also an array called oc_orig_allowed which
  155. * contains the allowed attributes which were not inherited from any other objectclass.
  156. *
  157. * The arrays oc_required and oc_allowed contain all the required and allowed attributes for
  158. * that objectclass, including the ones inherited from its parent and also the ones in
  159. * oc_orig_required and oc_orig_allowed.
  160. *
  161. * When an oc is updated, we go through the global list of objectclasses and see if
  162. * any ocs inherited from it. If so, we delete its oc_required and oc_allowed arrays,
  163. * copy the oc_orig_required and oc_orig_allowed arrays to oc_required and oc_allowed,
  164. * and then merge the parent's oc_required and oc_allowed onto oc_required and oc_allowed.
  165. *
  166. *
  167. */
  168. static int
  169. is_duplicate( char *target, char **list, int list_size ) {
  170. int i;
  171. for ( i = 0; i < list_size; i++ ) {
  172. if ( !strcasecmp( target, list[i] ) ) {
  173. return 1;
  174. }
  175. }
  176. return 0;
  177. }
  178. /*
  179. * Make normalized copies of all non-duplicate values in a list; free all old
  180. * values. The list is not resized.
  181. */
  182. static void
  183. normalize_list( char **list ) {
  184. int i, j;
  185. for ( i = 0, j = 0; list != NULL && list[i] != NULL; i++ ) {
  186. char *norm = slapi_attr_syntax_normalize( list[i] );
  187. char *save = list[i];
  188. if ( !is_duplicate( norm, list, j ) ) {
  189. list[j++] = norm;
  190. } else {
  191. slapi_ch_free((void **)&norm );
  192. }
  193. slapi_ch_free((void**)&save );
  194. }
  195. for ( ; j < i; j++ ) {
  196. list[j] = NULL;
  197. }
  198. }
  199. /*
  200. * normalize types contained in object class definitions. do this
  201. * after the whole config file is read so there is no order dependency
  202. * on inclusion of attributes and object classes.
  203. */
  204. void
  205. normalize_oc( void )
  206. {
  207. struct objclass *oc;
  208. oc_lock_write();
  209. for ( oc = g_get_global_oc_nolock(); oc != NULL; oc = oc->oc_next ) {
  210. LDAPDebug (LDAP_DEBUG_PARSE,
  211. "normalize_oc: normalizing '%s'\n", oc->oc_name, 0, 0);
  212. /* required attributes */
  213. normalize_list( oc->oc_required );
  214. normalize_list( oc->oc_orig_required );
  215. /* optional attributes */
  216. normalize_list( oc->oc_allowed );
  217. normalize_list( oc->oc_orig_allowed );
  218. }
  219. oc_unlock();
  220. }
  221. void
  222. normalize_oc_nolock( void )
  223. {
  224. struct objclass *oc;
  225. for ( oc = g_get_global_oc_nolock(); oc != NULL; oc = oc->oc_next ) {
  226. LDAPDebug (LDAP_DEBUG_PARSE,
  227. "normalize_oc: normalizing '%s'\n", oc->oc_name, 0, 0);
  228. /* required attributes */
  229. normalize_list( oc->oc_required );
  230. normalize_list( oc->oc_orig_required );
  231. /* optional attributes */
  232. normalize_list( oc->oc_allowed );
  233. normalize_list( oc->oc_orig_allowed );
  234. }
  235. }
  236. /*
  237. * oc_update_inheritance_nolock:
  238. * If an objectclass is redefined, we need to make sure that any objectclasses
  239. * which inherit from the redefined objectclass have their required and allowed
  240. * attributes updated.
  241. *
  242. * Every objectclass contains an array of attributes called oc_orig_required,
  243. * which are the required attributes for that objectclass which were not inherited from
  244. * any other objectclass. Likewise, there's also an array called oc_orig_allowed which
  245. * contains the allowed attributes which were not inherited from any other objectclass.
  246. *
  247. * The arrays oc_required and oc_allowed contain all the required and allowed attributes for
  248. * that objectclass, including the ones inherited from its parent and also the ones in
  249. * oc_orig_required and oc_orig_allowed.
  250. *
  251. * When an oc is updated, we go through the global list of objectclasses and see if
  252. * any ocs inherited from it. If so, we delete its oc_requried and oc_allowed arrays,
  253. * copy the oc_orig_required and oc_orig_allowed arrays to oc_required and oc_allowed,
  254. * and then merge the parent's oc_required and oc_allowed onto oc_required and oc_allowed.
  255. */
  256. void
  257. oc_update_inheritance_nolock( struct objclass *psuperior_oc )
  258. {
  259. struct objclass *oc;
  260. for ( oc = g_get_global_oc_nolock(); oc != NULL; oc = oc->oc_next ) {
  261. if ( oc->oc_superior &&
  262. (strcasecmp( oc->oc_superior, psuperior_oc->oc_name ) == 0) ) {
  263. if (oc->oc_required ) {
  264. charray_free (oc->oc_required);
  265. }
  266. if (oc->oc_allowed) {
  267. charray_free (oc->oc_allowed);
  268. }
  269. oc->oc_required = charray_dup ( oc->oc_orig_required );
  270. oc->oc_allowed = charray_dup ( oc->oc_orig_allowed );
  271. charray_merge ( &(oc->oc_required), psuperior_oc->oc_required, 1 );
  272. charray_merge ( &(oc->oc_allowed), psuperior_oc->oc_allowed, 1 );
  273. oc_update_inheritance_nolock ( oc );
  274. }
  275. }
  276. }