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