3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
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.
21 #include <ac/string.h>
23 #include <ac/socket.h>
28 static int slap_mods2entry(
34 do_add( Connection *conn, Operation *op )
36 BerElement *ber = op->o_ber;
37 char *dn, *ndn, *last;
42 LDAPModList *modlist = NULL;
43 LDAPModList **modtail = &modlist;
44 Modifications *mods = NULL;
46 int rc = LDAP_SUCCESS;
48 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
51 * Parse the add request. It looks like this:
53 * AddRequest := [APPLICATION 14] SEQUENCE {
54 * name DistinguishedName,
55 * attrs SEQUENCE OF SEQUENCE {
57 * values SET OF AttributeValue
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" );
70 ndn = ch_strdup( dn );
72 if ( dn_normalize( 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 );
78 return LDAP_INVALID_DN_SYNTAX;
81 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
88 Debug( LDAP_DEBUG_ARGS, "do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
91 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
92 tag = ber_next_element( ber, &len, last ) )
94 LDAPModList *mod = (LDAPModList *) ch_malloc( sizeof(LDAPModList) );
95 mod->ml_op = LDAP_MOD_ADD;
98 rc = ber_scanf( ber, "{a{V}}", &mod->ml_type, &mod->ml_bvalues );
100 if ( rc == LBER_ERROR ) {
101 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
102 send_ldap_disconnect( conn, op,
103 LDAP_PROTOCOL_ERROR, "decoding error" );
109 if ( mod->ml_bvalues == NULL ) {
110 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
111 mod->ml_type, 0, 0 );
112 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
113 NULL, "no values for attribute type", NULL, NULL );
114 free( mod->ml_type );
120 modtail = &mod->ml_next;
123 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
124 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
125 send_ldap_disconnect( conn, op,
126 LDAP_PROTOCOL_ERROR, "decoding error" );
131 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
132 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
136 if ( modlist == NULL ) {
137 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
138 NULL, "no attributes provided", NULL, NULL );
142 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
143 op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
146 * We could be serving multiple database backends. Select the
147 * appropriate one, or send a referral to our "referral server"
148 * if we don't hold it.
150 be = select_backend( e->e_ndn );
152 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
153 NULL, NULL, default_referral, NULL );
157 /* make sure this backend recongizes critical controls */
158 rc = backend_check_controls( be, conn, op, &text ) ;
159 if( rc != LDAP_SUCCESS ) {
160 send_ldap_result( conn, op, rc,
161 NULL, text, NULL, NULL );
165 /* check for referrals */
166 rc = backend_check_referrals( be, conn, op, e->e_dn, e->e_ndn );
167 if ( rc != LDAP_SUCCESS ) {
171 if ( global_readonly || be->be_readonly ) {
172 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
174 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
175 NULL, "directory is read-only", NULL, NULL );
180 * do the add if 1 && (2 || 3)
181 * 1) there is an add function implemented in this backend;
182 * 2) this backend is master for what it holds;
183 * 3) it's a replica and the dn supplied is the updatedn.
186 /* do the update here */
187 #ifdef SLAPD_MULTIMASTER
188 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
189 global_lastmod == ON)) && (be->be_update_ndn == NULL ||
190 strcmp( be->be_update_ndn, op->o_ndn )) )
192 if ( be->be_update_ndn == NULL ||
193 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
196 int update = be->be_update_ndn != NULL;
198 rc = slap_modlist2mods( modlist, update, &mods, &text );
199 if( rc != LDAP_SUCCESS ) {
200 send_ldap_result( conn, op, rc,
201 NULL, text, NULL, NULL );
205 #ifndef SLAPD_MULTIMASTER
206 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
207 global_lastmod == ON)) && !update )
210 Modifications **modstail;
211 for( modstail = &mods;
213 modstail = &(*modstail)->sml_next )
215 assert( (*modstail)->sml_op == LDAP_MOD_ADD );
216 assert( (*modstail)->sml_desc != NULL );
218 rc = slap_mods_opattrs( op, modstail, &text );
219 if( rc != LDAP_SUCCESS ) {
220 send_ldap_result( conn, op, rc,
221 NULL, text, NULL, NULL );
226 rc = slap_mods2entry( mods, &e, &text );
227 if( rc != LDAP_SUCCESS ) {
228 send_ldap_result( conn, op, rc,
229 NULL, text, NULL, NULL );
233 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
234 #ifdef SLAPD_MULTIMASTER
235 if (be->be_update_ndn == NULL ||
236 strcmp( be->be_update_ndn, op->o_ndn ))
239 replog( be, op, e->e_dn, e );
241 be_entry_release_w( be, e );
245 #ifndef SLAPD_MULTIMASTER
247 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
248 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
252 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
253 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
254 NULL, "operation not supported within namingContext", NULL, NULL );
258 if( modlist != NULL ) {
259 slap_modlist_free( modlist );
262 slap_mods_free( mods );
271 static int slap_mods2entry(
276 Attribute **tail = &(*e)->e_attrs;
277 assert( *tail == NULL );
279 for( ; mods != NULL; mods = mods->sml_next ) {
282 assert( mods->sml_op == LDAP_MOD_ADD );
283 assert( mods->sml_desc != NULL );
285 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
288 #define SLURPD_FRIENDLY
289 #ifdef SLURPD_FRIENDLY
292 for( i=0; attr->a_vals[i]; i++ ) {
295 for( j=0; mods->sml_bvalues[j]; j++ ) {
300 attr->a_vals = ch_realloc( attr->a_vals,
301 sizeof( struct berval * ) * (i+j) );
303 /* should check for duplicates */
304 memcpy( &attr->a_vals[i], mods->sml_bvalues,
305 sizeof( struct berval * ) * j );
307 /* trim the mods array */
308 ch_free( mods->sml_bvalues );
309 mods->sml_bvalues = NULL;
313 *text = "attribute provided more than once";
314 return LDAP_TYPE_OR_VALUE_EXISTS;
318 attr = ch_calloc( 1, sizeof(Attribute) );
320 /* move ad to attr structure */
321 attr->a_desc = mods->sml_desc;
322 mods->sml_desc = NULL;
324 /* move values to attr structure */
325 /* should check for duplicates */
326 attr->a_vals = mods->sml_bvalues;
327 mods->sml_bvalues = NULL;
330 tail = &attr->a_next;