]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
Should be in releng
[openldap] / servers / slapd / schema.c
1 /* schema.c - routines to manage schema definitions */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/ctype.h>
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "slap.h"
26 #include "ldap_pvt.h"
27 #include "lutil.h"
28
29
30 int
31 schema_info( Entry **entry, const char **text )
32 {
33         AttributeDescription *ad_structuralObjectClass
34                 = slap_schema.si_ad_structuralObjectClass;
35         AttributeDescription *ad_objectClass
36                 = slap_schema.si_ad_objectClass;
37         AttributeDescription *ad_createTimestamp
38                 = slap_schema.si_ad_createTimestamp;
39         AttributeDescription *ad_modifyTimestamp
40                 = slap_schema.si_ad_modifyTimestamp;
41
42         Entry           *e;
43         struct berval   vals[5];
44         struct berval   nvals[5];
45
46         e = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
47         if( e == NULL ) {
48                 /* Out of memory, do something about it */
49 #ifdef NEW_LOGGING
50                 LDAP_LOG( OPERATION, ERR, 
51                         "schema_info: SLAP_CALLOC failed - out of memory.\n", 0, 0,0 );
52 #else
53                 Debug( LDAP_DEBUG_ANY, 
54                         "schema_info: SLAP_CALLOC failed - out of memory.\n", 0, 0, 0 );
55 #endif
56                 *text = "out of memory";
57                 return LDAP_OTHER;
58         }
59
60         e->e_attrs = NULL;
61         /* backend-specific schema info should be created by the
62          * backend itself
63          */
64         ber_dupbv( &e->e_name, &global_schemadn );
65         ber_dupbv( &e->e_nname, &global_schemandn );
66         e->e_private = NULL;
67
68         vals[0].bv_val = "subentry";
69         vals[0].bv_len = sizeof("subentry")-1;
70         if( attr_merge_one( e, ad_structuralObjectClass, vals, vals ) )
71         {
72                 /* Out of memory, do something about it */
73                 entry_free( e );
74                 *text = "out of memory";
75                 return LDAP_OTHER;
76         }
77
78         vals[0].bv_val = "top";
79         vals[0].bv_len = sizeof("top")-1;
80         vals[1].bv_val = "subentry";
81         vals[1].bv_len = sizeof("subentry")-1;
82         vals[2].bv_val = "subschema";
83         vals[2].bv_len = sizeof("subschema")-1;
84         vals[3].bv_val = "extensibleObject";
85         vals[3].bv_len = sizeof("extensibleObject")-1;
86         vals[4].bv_val = NULL;
87         if( attr_merge( e, ad_objectClass, vals, vals ) )
88         {
89                 /* Out of memory, do something about it */
90                 entry_free( e );
91                 *text = "out of memory";
92                 return LDAP_OTHER;
93         }
94
95         {
96                 int rc;
97                 AttributeDescription *desc = NULL;
98                 struct berval rdn = global_schemadn;
99                 vals[0].bv_val = strchr( rdn.bv_val, '=' );
100
101                 if( vals[0].bv_val == NULL ) {
102                         *text = "improperly configured subschema subentry";
103                         return LDAP_OTHER;
104                 }
105
106                 vals[0].bv_val++;
107                 vals[0].bv_len = rdn.bv_len - (vals[0].bv_val - rdn.bv_val);
108                 rdn.bv_len -= vals[0].bv_len + 1;
109
110                 rc = slap_bv2ad( &rdn, &desc, text );
111
112                 if( rc != LDAP_SUCCESS ) {
113                         entry_free( e );
114                         *text = "improperly configured subschema subentry";
115                         return LDAP_OTHER;
116                 }
117
118                 nvals[0].bv_val = strchr( global_schemandn.bv_val, '=' );
119                 assert( nvals[0].bv_val );
120                 nvals[0].bv_val++;
121                 nvals[0].bv_len = global_schemandn.bv_len -
122                         (nvals[0].bv_val - global_schemandn.bv_val);
123
124                 if( attr_merge_one( e, desc, vals, nvals ) )
125                 {
126                         /* Out of memory, do something about it */
127                         entry_free( e );
128                         *text = "out of memory";
129                         return LDAP_OTHER;
130                 }
131         }
132
133         {
134                 struct          tm *ltm;
135 #ifdef HAVE_GMTIME_R
136                 struct          tm ltm_buf;
137 #endif
138                 char            timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
139
140                 /*
141                  * According to RFC 2251:
142
143    Servers SHOULD provide the attributes createTimestamp and
144    modifyTimestamp in subschema entries, in order to allow clients to
145    maintain their caches of schema information.
146
147                  * to be conservative, we declare schema created 
148                  * AND modified at server startup time ...
149                  */
150
151 #ifdef HAVE_GMTIME_R
152                 ltm = gmtime_r( &starttime, &ltm_buf );
153 #else
154                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
155                 ltm = gmtime( &starttime );
156 #endif /* HAVE_GMTIME_R */
157                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
158 #ifndef HAVE_GMTIME_R
159                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
160 #endif
161
162                 vals[0].bv_val = timebuf;
163                 vals[0].bv_len = strlen( timebuf );
164
165                 if( attr_merge_one( e, ad_createTimestamp, vals, vals ) )
166                 {
167                         /* Out of memory, do something about it */
168                         entry_free( e );
169                         *text = "out of memory";
170                         return LDAP_OTHER;
171                 }
172                 if( attr_merge_one( e, ad_modifyTimestamp, vals, vals ) )
173                 {
174                         /* Out of memory, do something about it */
175                         entry_free( e );
176                         *text = "out of memory";
177                         return LDAP_OTHER;
178                 }
179         }
180
181         if ( syn_schema_info( e ) 
182                 || mr_schema_info( e )
183                 || mru_schema_info( e )
184                 || at_schema_info( e )
185                 || oc_schema_info( e )
186                 || cr_schema_info( e ) )
187         {
188                 /* Out of memory, do something about it */
189                 entry_free( e );
190                 *text = "out of memory";
191                 return LDAP_OTHER;
192         }
193         
194         *entry = e;
195         return LDAP_SUCCESS;
196 }