]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
Happy new year
[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
36         e = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
37         if( e == NULL ) {
38                 /* Out of memory, do something about it */
39 #ifdef NEW_LOGGING
40                 LDAP_LOG( OPERATION, ERR, 
41                         "schema_info: SLAP_CALLOC failed - out of memory.\n", 0, 0,0 );
42 #else
43                 Debug( LDAP_DEBUG_ANY, 
44                         "schema_info: SLAP_CALLOC failed - out of memory.\n", 0, 0, 0 );
45 #endif
46                 *text = "out of memory";
47                 return LDAP_OTHER;
48         }
49
50         e->e_attrs = NULL;
51         /* backend-specific schema info should be created by the
52          * backend itself
53          */
54         ber_dupbv( &e->e_name, &global_schemadn );
55         ber_dupbv( &e->e_nname, &global_schemandn );
56         e->e_private = NULL;
57
58         vals[0].bv_val = "subentry";
59         vals[0].bv_len = sizeof("subentry")-1;
60         if( attr_merge_one( e, ad_structuralObjectClass, vals ) ) {
61                 /* Out of memory, do something about it */
62                 entry_free( e );
63                 *text = "out of memory";
64                 return LDAP_OTHER;
65         }
66
67         vals[0].bv_val = "top";
68         vals[0].bv_len = sizeof("top")-1;
69         vals[1].bv_val = "subentry";
70         vals[1].bv_len = sizeof("subentry")-1;
71         vals[2].bv_val = "subschema";
72         vals[2].bv_len = sizeof("subschema")-1;
73         vals[3].bv_val = "extensibleObject";
74         vals[3].bv_len = sizeof("extensibleObject")-1;
75         vals[4].bv_val = NULL;
76         if( attr_merge( e, ad_objectClass, vals ) ) {
77                 /* Out of memory, do something about it */
78                 entry_free( e );
79                 *text = "out of memory";
80                 return LDAP_OTHER;
81         }
82
83         {
84                 int rc;
85                 AttributeDescription *desc = NULL;
86                 struct berval rdn = global_schemadn;
87                 vals[0].bv_val = strchr( rdn.bv_val, '=' );
88
89                 if( vals[0].bv_val == NULL ) {
90                         *text = "improperly configured subschema subentry";
91                         return LDAP_OTHER;
92                 }
93
94                 vals[0].bv_val++;
95                 vals[0].bv_len = rdn.bv_len - (vals[0].bv_val - rdn.bv_val);
96                 rdn.bv_len -= vals[0].bv_len + 1;
97
98                 rc = slap_bv2ad( &rdn, &desc, text );
99
100                 if( rc != LDAP_SUCCESS ) {
101                         entry_free( e );
102                         *text = "improperly configured subschema subentry";
103                         return LDAP_OTHER;
104                 }
105
106                 if( attr_merge_one( e, desc, vals ) ) {
107                         /* Out of memory, do something about it */
108                         entry_free( e );
109                         *text = "out of memory";
110                         return LDAP_OTHER;
111                 }
112         }
113
114         {
115                 struct          tm *ltm;
116                 char            timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
117
118                 /*
119                  * According to RFC 2251:
120
121    Servers SHOULD provide the attributes createTimestamp and
122    modifyTimestamp in subschema entries, in order to allow clients to
123    maintain their caches of schema information.
124
125                  * to be conservative, we declare schema created 
126                  * AND modified at server startup time ...
127                  */
128
129                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
130                 ltm = gmtime( &starttime );
131                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
132                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
133
134                 vals[0].bv_val = timebuf;
135                 vals[0].bv_len = strlen( timebuf );
136
137                 if( attr_merge_one( e, ad_createTimestamp, vals ) ) {
138                         /* Out of memory, do something about it */
139                         entry_free( e );
140                         *text = "out of memory";
141                         return LDAP_OTHER;
142                 }
143                 if( attr_merge_one( e, ad_modifyTimestamp, vals ) ) {
144                         /* Out of memory, do something about it */
145                         entry_free( e );
146                         *text = "out of memory";
147                         return LDAP_OTHER;
148                 }
149         }
150
151         if ( syn_schema_info( e ) 
152                 || mr_schema_info( e )
153                 || mru_schema_info( e )
154                 || at_schema_info( e )
155                 || oc_schema_info( e )
156                 || cr_schema_info( e ) )
157         {
158                 /* Out of memory, do something about it */
159                 entry_free( e );
160                 *text = "out of memory";
161                 return LDAP_OTHER;
162         }
163         
164         *entry = e;
165         return LDAP_SUCCESS;
166 }