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