]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
d02c9f4501d15b6dafddf1f394afba0a91b0a2a7
[openldap] / servers / slapd / add.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  * Copyright (c) 1995 Regents of the University of Michigan.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * provided that this notice is preserved and that due credit is given
11  * to the University of Michigan at Ann Arbor. The name of the University
12  * may not be used to endorse or promote products derived from this
13  * software without specific prior written permission. This software
14  * is provided ``as is'' without express or implied warranty.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/time.h>
23 #include <ac/socket.h>
24
25 #include "slap.h"
26
27 static int      add_created_attrs(Operation *op, Entry *e);
28
29 int
30 do_add( Connection *conn, Operation *op )
31 {
32         BerElement      *ber = op->o_ber;
33         char            *dn, *ndn, *last;
34         ber_len_t       len;
35         ber_tag_t       tag;
36         Entry           *e;
37         Backend         *be;
38         int                     rc = LDAP_SUCCESS;
39
40         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
41
42         if( op->o_bind_in_progress ) {
43                 Debug( LDAP_DEBUG_ANY, "do_add: SASL bind in progress.\n", 0, 0, 0 );
44                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS, NULL,
45                     "SASL bind in progress", NULL, NULL );
46                 return LDAP_SASL_BIND_IN_PROGRESS;
47         }
48
49         /*
50          * Parse the add request.  It looks like this:
51          *
52          *      AddRequest := [APPLICATION 14] SEQUENCE {
53          *              name    DistinguishedName,
54          *              attrs   SEQUENCE OF SEQUENCE {
55          *                      type    AttributeType,
56          *                      values  SET OF AttributeValue
57          *              }
58          *      }
59          */
60
61         /* get the name */
62         if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
63                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
64                 send_ldap_disconnect( conn, op,
65                         LDAP_PROTOCOL_ERROR, "decoding error" );
66                 return -1;
67         }
68
69         ndn = ch_strdup( dn );
70
71         if ( dn_normalize_case( ndn ) == NULL ) {
72                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
73                 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
74                     "invalid DN", NULL, NULL );
75                 free( dn );
76                 free( ndn );
77                 return LDAP_INVALID_DN_SYNTAX;
78         }
79
80         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
81
82         e->e_dn = dn;
83         e->e_ndn = ndn;
84         e->e_private = NULL;
85
86         Debug( LDAP_DEBUG_ARGS, "    do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
87
88         /* get the attrs */
89         e->e_attrs = NULL;
90         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
91             tag = ber_next_element( ber, &len, last ) ) {
92                 char            *type;
93                 struct berval   **vals;
94
95                 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
96                         send_ldap_disconnect( conn, op,
97                                 LDAP_PROTOCOL_ERROR, "decoding error" );
98                         entry_free( e );
99                         return -1;
100                 }
101
102                 if ( vals == NULL ) {
103                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
104                             0, 0 );
105                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
106                                 NULL, "no values for type", NULL, NULL );
107                         free( type );
108                         entry_free( e );
109                         return LDAP_PROTOCOL_ERROR;
110                 }
111
112                 attr_merge( e, type, vals );
113
114                 free( type );
115                 ber_bvecfree( vals );
116         }
117
118         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
119                 entry_free( e );
120                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
121                 send_ldap_disconnect( conn, op,
122                         LDAP_PROTOCOL_ERROR, "decoding error" );
123                 return -1;
124         }
125
126         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
127                 entry_free( e );
128                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
129                 return rc;
130         } 
131
132         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
133             op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
134
135         /*
136          * We could be serving multiple database backends.  Select the
137          * appropriate one, or send a referral to our "referral server"
138          * if we don't hold it.
139          */
140         be = select_backend( e->e_ndn );
141         if ( be == NULL ) {
142                 entry_free( e );
143                 send_ldap_result( conn, op, LDAP_REFERRAL, NULL,
144                     NULL, default_referral, NULL );
145                 return rc;
146         }
147
148         if ( global_readonly || be->be_readonly ) {
149                 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
150                        0, 0, 0 );
151                 entry_free( e );
152                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
153                                   NULL, "database is read-only", NULL, NULL );
154                 return LDAP_UNWILLING_TO_PERFORM;
155         }
156
157         /*
158          * do the add if 1 && (2 || 3)
159          * 1) there is an add function implemented in this backend;
160          * 2) this backend is master for what it holds;
161          * 3) it's a replica and the dn supplied is the updatedn.
162          */
163         if ( be->be_add ) {
164                 /* do the update here */
165 #ifdef SLAPD_MULTIMASTER
166                 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
167                         global_lastmod == ON)) && (be->be_update_ndn == NULL ||
168                         strcmp( be->be_update_ndn, op->o_ndn )) )
169 #else
170                 if ( be->be_update_ndn == NULL ||
171                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
172 #endif
173                 {
174 #ifndef SLAPD_MULTIMASTER
175                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
176                                 global_lastmod == ON)) && be->be_update_ndn == NULL )
177 #endif
178                         {
179                                 rc = add_created_attrs( op, e );
180
181                                 if( rc != LDAP_SUCCESS ) {
182                                         entry_free( e );
183                                         send_ldap_result( conn, op, rc,
184                                                 NULL, "no-user-modification attribute type",
185                                                 NULL, NULL );
186                                         return rc;
187                                 }
188                         }
189
190                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
191 #ifdef SLAPD_MULTIMASTER
192                                 if (be->be_update_ndn == NULL ||
193                                         strcmp( be->be_update_ndn, op->o_ndn ))
194 #endif
195                                 {
196                                         replog( be, op, e->e_dn, e );
197                                 }
198                                 be_entry_release_w( be, e );
199                         }
200
201 #ifndef SLAPD_MULTIMASTER
202                 } else {
203                         entry_free( e );
204                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
205                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
206 #endif
207                 }
208         } else {
209             Debug( LDAP_DEBUG_ARGS, "    do_add: HHH\n", 0, 0, 0 );
210                 entry_free( e );
211                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
212                         NULL, "Function not implemented", NULL, NULL );
213         }
214
215         return rc;
216 }
217
218 static int
219 add_created_attrs( Operation *op, Entry *e )
220 {
221         char            buf[22];
222         struct berval   bv;
223         struct berval   *bvals[2];
224         Attribute       *a;
225         struct tm       *ltm;
226         time_t          currenttime;
227
228         Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
229
230         bvals[0] = &bv;
231         bvals[1] = NULL;
232
233         /* return error on any attempts by the user to add these attrs */
234         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
235                 if ( oc_check_no_usermod_attr( a->a_type ) ) {
236                         return LDAP_CONSTRAINT_VIOLATION;
237                 }
238         }
239
240         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
241                 bv.bv_val = "<anonymous>";
242                 bv.bv_len = strlen( bv.bv_val );
243         } else {
244                 bv.bv_val = op->o_dn;
245                 bv.bv_len = strlen( bv.bv_val );
246         }
247         attr_merge( e, "creatorsname", bvals );
248
249         currenttime = slap_get_time();
250         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
251 #ifndef LDAP_LOCALTIME
252         ltm = gmtime( &currenttime );
253         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
254 #else
255         ltm = localtime( &currenttime );
256         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
257 #endif
258         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
259
260         bv.bv_val = buf;
261         bv.bv_len = strlen( bv.bv_val );
262         attr_merge( e, "createtimestamp", bvals );
263
264         return LDAP_SUCCESS;
265 }