]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
8b006652f3e4217a17dc88e184ba48b684ce4d67
[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-2004 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                 /* Out of memory, do something about it */
72                 entry_free( e );
73                 *text = "out of memory";
74                 return LDAP_OTHER;
75         }
76
77         vals[0].bv_val = "top";
78         vals[0].bv_len = sizeof("top")-1;
79         vals[1].bv_val = "subentry";
80         vals[1].bv_len = sizeof("subentry")-1;
81         vals[2].bv_val = "subschema";
82         vals[2].bv_len = sizeof("subschema")-1;
83         vals[3].bv_val = "extensibleObject";
84         vals[3].bv_len = sizeof("extensibleObject")-1;
85         vals[4].bv_val = NULL;
86         if( attr_merge( e, ad_objectClass, vals, vals ) ) {
87                 /* Out of memory, do something about it */
88                 entry_free( e );
89                 *text = "out of memory";
90                 return LDAP_OTHER;
91         }
92
93         {
94                 int rc;
95                 AttributeDescription *desc = NULL;
96                 struct berval rdn = global_schemadn;
97                 vals[0].bv_val = strchr( rdn.bv_val, '=' );
98
99                 if( vals[0].bv_val == NULL ) {
100                         *text = "improperly configured subschema subentry";
101                         return LDAP_OTHER;
102                 }
103
104                 vals[0].bv_val++;
105                 vals[0].bv_len = rdn.bv_len - (vals[0].bv_val - rdn.bv_val);
106                 rdn.bv_len -= vals[0].bv_len + 1;
107
108                 rc = slap_bv2ad( &rdn, &desc, text );
109
110                 if( rc != LDAP_SUCCESS ) {
111                         entry_free( e );
112                         *text = "improperly configured subschema subentry";
113                         return LDAP_OTHER;
114                 }
115
116                 nvals[0].bv_val = strchr( global_schemandn.bv_val, '=' );
117                 assert( nvals[0].bv_val );
118                 nvals[0].bv_val++;
119                 nvals[0].bv_len = global_schemandn.bv_len -
120                         (nvals[0].bv_val - global_schemandn.bv_val);
121
122                 if( attr_merge_one( e, desc, vals, nvals ) ) {
123                         /* Out of memory, do something about it */
124                         entry_free( e );
125                         *text = "out of memory";
126                         return LDAP_OTHER;
127                 }
128         }
129
130         {
131                 struct          tm *ltm;
132 #ifdef HAVE_GMTIME_R
133                 struct          tm ltm_buf;
134 #endif
135                 char            timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
136
137                 /*
138                  * According to RFC 2251:
139
140    Servers SHOULD provide the attributes createTimestamp and
141    modifyTimestamp in subschema entries, in order to allow clients to
142    maintain their caches of schema information.
143
144                  * to be conservative, we declare schema created 
145                  * AND modified at server startup time ...
146                  */
147
148 #ifdef HAVE_GMTIME_R
149                 ltm = gmtime_r( &starttime, &ltm_buf );
150 #else
151                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
152                 ltm = gmtime( &starttime );
153 #endif /* HAVE_GMTIME_R */
154                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
155 #ifndef HAVE_GMTIME_R
156                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
157 #endif
158
159                 vals[0].bv_val = timebuf;
160                 vals[0].bv_len = strlen( timebuf );
161
162                 if( attr_merge_one( e, ad_createTimestamp, vals, vals ) ) {
163                         /* Out of memory, do something about it */
164                         entry_free( e );
165                         *text = "out of memory";
166                         return LDAP_OTHER;
167                 }
168                 if( attr_merge_one( e, ad_modifyTimestamp, vals, vals ) ) {
169                         /* Out of memory, do something about it */
170                         entry_free( e );
171                         *text = "out of memory";
172                         return LDAP_OTHER;
173                 }
174         }
175
176         if ( syn_schema_info( e ) 
177                 || mr_schema_info( e )
178                 || mru_schema_info( e )
179                 || at_schema_info( e )
180                 || oc_schema_info( e )
181                 || cr_schema_info( e ) )
182         {
183                 /* Out of memory, do something about it */
184                 entry_free( e );
185                 *text = "out of memory";
186                 return LDAP_OTHER;
187         }
188         
189         *entry = e;
190         return LDAP_SUCCESS;
191 }