]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
DNS SRV default referral handling
[openldap] / servers / slapd / add.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 #include <ac/string.h>
22 #include <ac/time.h>
23 #include <ac/socket.h>
24
25 #include "ldap_pvt.h"
26 #include "slap.h"
27
28 static int slap_mods2entry(
29         Modifications *mods,
30         Entry **e,
31         int repl_user,
32         const char **text,
33         char *textbuf, size_t textlen );
34
35 int
36 do_add( Connection *conn, Operation *op )
37 {
38         BerElement      *ber = op->o_ber;
39         char            *last;
40         struct berval dn = { 0, NULL };
41         ber_len_t       len;
42         ber_tag_t       tag;
43         Entry           *e;
44         Backend         *be;
45         Modifications   *modlist = NULL;
46         Modifications   **modtail = &modlist;
47         Modifications   tmp;
48         const char *text;
49         int                     rc = LDAP_SUCCESS;
50         int     manageDSAit;
51
52 #ifdef NEW_LOGGING
53         LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", conn->c_connid,0,0 );
54 #else
55         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
56 #endif
57         /*
58          * Parse the add request.  It looks like this:
59          *
60          *      AddRequest := [APPLICATION 14] SEQUENCE {
61          *              name    DistinguishedName,
62          *              attrs   SEQUENCE OF SEQUENCE {
63          *                      type    AttributeType,
64          *                      values  SET OF AttributeValue
65          *              }
66          *      }
67          */
68
69         /* get the name */
70         if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
71 #ifdef NEW_LOGGING
72                 LDAP_LOG( OPERATION, ERR, 
73                         "do_add: conn %d ber_scanf failed\n", conn->c_connid,0,0 );
74 #else
75                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
76 #endif
77                 send_ldap_disconnect( conn, op,
78                         LDAP_PROTOCOL_ERROR, "decoding error" );
79                 return -1;
80         }
81
82         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
83
84         rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
85
86         if( rc != LDAP_SUCCESS ) {
87 #ifdef NEW_LOGGING
88                 LDAP_LOG( OPERATION, ERR, 
89                         "do_add: conn %d invalid dn (%s)\n", conn->c_connid, dn.bv_val, 0 );
90 #else
91                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
92 #endif
93                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
94                             "invalid DN", NULL, NULL );
95                 goto done;
96         }
97
98 #ifdef NEW_LOGGING
99         LDAP_LOG( OPERATION, ARGS, 
100                 "do_add: conn %d  dn (%s)\n", conn->c_connid, e->e_dn, 0 );
101 #else
102         Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
103 #endif
104
105         /* get the attrs */
106         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
107             tag = ber_next_element( ber, &len, last ) )
108         {
109                 Modifications *mod;
110                 ber_tag_t rtag;
111
112                 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
113
114                 if ( rtag == LBER_ERROR ) {
115 #ifdef NEW_LOGGING
116                         LDAP_LOG( OPERATION, ERR, 
117                                    "do_add: conn %d      decoding error \n", conn->c_connid, 0, 0 );
118 #else
119                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
120 #endif
121                         send_ldap_disconnect( conn, op,
122                                 LDAP_PROTOCOL_ERROR, "decoding error" );
123                         rc = -1;
124                         goto done;
125                 }
126
127                 if ( tmp.sml_bvalues == NULL ) {
128 #ifdef NEW_LOGGING
129                         LDAP_LOG( OPERATION, INFO, 
130                                 "do_add: conn %d         no values for type %s\n",
131                                 conn->c_connid, tmp.sml_type.bv_val, 0 );
132 #else
133                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
134                                 tmp.sml_type.bv_val, 0, 0 );
135 #endif
136                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
137                                 NULL, "no values for attribute type", NULL, NULL );
138                         goto done;
139                 }
140                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
141                 
142                 mod->sml_op = LDAP_MOD_ADD;
143                 mod->sml_next = NULL;
144                 mod->sml_desc = NULL;
145                 mod->sml_type = tmp.sml_type;
146                 mod->sml_bvalues = tmp.sml_bvalues;
147
148                 *modtail = mod;
149                 modtail = &mod->sml_next;
150         }
151
152         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
153 #ifdef NEW_LOGGING
154                 LDAP_LOG( OPERATION, ERR, 
155                         "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
156 #else
157                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
158 #endif
159                 send_ldap_disconnect( conn, op,
160                         LDAP_PROTOCOL_ERROR, "decoding error" );
161                 rc = -1;
162                 goto done;
163         }
164
165         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
166 #ifdef NEW_LOGGING
167                 LDAP_LOG( OPERATION, INFO, 
168                         "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
169 #else
170                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
171 #endif
172                 goto done;
173         } 
174
175         if ( modlist == NULL ) {
176                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
177                         NULL, "no attributes provided", NULL, NULL );
178                 goto done;
179         }
180
181         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
182             op->o_connid, op->o_opid, e->e_dn, 0, 0 );
183
184         if( e->e_nname.bv_len == 0 ) {
185                 /* protocolError may be a more appropriate error */
186                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
187                         NULL, "root DSE already exists",
188                         NULL, NULL );
189                 goto done;
190
191         } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
192                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
193                         NULL, "subschema subentry already exists",
194                         NULL, NULL );
195                 goto done;
196         }
197
198         manageDSAit = get_manageDSAit( op );
199
200         /*
201          * We could be serving multiple database backends.  Select the
202          * appropriate one, or send a referral to our "referral server"
203          * if we don't hold it.
204          */
205         be = select_backend( &e->e_nname, manageDSAit, 0 );
206         if ( be == NULL ) {
207                 BerVarray ref = referral_rewrite( default_referral,
208                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
209
210                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
211                         NULL, NULL, ref ? ref : default_referral, NULL );
212
213                 if ( ref ) ber_bvarray_free( ref );
214                 goto done;
215         }
216
217         /* check restrictions */
218         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
219         if( rc != LDAP_SUCCESS ) {
220                 send_ldap_result( conn, op, rc,
221                         NULL, text, NULL, NULL );
222                 goto done;
223         }
224
225         /* check for referrals */
226         rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
227         if ( rc != LDAP_SUCCESS ) {
228                 goto done;
229         }
230
231         /*
232          * do the add if 1 && (2 || 3)
233          * 1) there is an add function implemented in this backend;
234          * 2) this backend is master for what it holds;
235          * 3) it's a replica and the dn supplied is the updatedn.
236          */
237         if ( be->be_add ) {
238                 /* do the update here */
239                 int repl_user = be_isupdate(be, &op->o_ndn );
240 #ifndef SLAPD_MULTIMASTER
241                 if ( !be->be_update_ndn.bv_len || repl_user )
242 #endif
243                 {
244                         int update = be->be_update_ndn.bv_len;
245                         char textbuf[SLAP_TEXT_BUFLEN];
246                         size_t textlen = sizeof textbuf;
247
248                         rc = slap_mods_check( modlist, update, &text,
249                                 textbuf, textlen );
250
251                         if( rc != LDAP_SUCCESS ) {
252                                 send_ldap_result( conn, op, rc,
253                                         NULL, text, NULL, NULL );
254                                 goto done;
255                         }
256
257                         if ( !repl_user ) {
258                                 for( modtail = &modlist;
259                                         *modtail != NULL;
260                                         modtail = &(*modtail)->sml_next )
261                                 {
262                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
263                                         assert( (*modtail)->sml_desc != NULL );
264                                 }
265                                 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
266                                         textbuf, textlen );
267                                 if( rc != LDAP_SUCCESS ) {
268                                         send_ldap_result( conn, op, rc,
269                                                 NULL, text, NULL, NULL );
270                                         goto done;
271                                 }
272                         }
273
274                         rc = slap_mods2entry( modlist, &e, repl_user, &text,
275                                 textbuf, textlen );
276                         if( rc != LDAP_SUCCESS ) {
277                                 send_ldap_result( conn, op, rc,
278                                         NULL, text, NULL, NULL );
279                                 goto done;
280                         }
281
282                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
283 #ifdef SLAPD_MULTIMASTER
284                                 if ( !repl_user )
285 #endif
286                                 {
287                                         replog( be, op, &e->e_name, &e->e_nname, e );
288                                 }
289                                 be_entry_release_w( be, conn, op, e );
290                                 e = NULL;
291                         }
292
293 #ifndef SLAPD_MULTIMASTER
294                 } else {
295                         BerVarray defref = be->be_update_refs
296                                 ? be->be_update_refs : default_referral;
297                         BerVarray ref = referral_rewrite( defref,
298                                 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
299
300                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
301                                 ref ? ref : defref, NULL );
302
303                         if ( ref ) ber_bvarray_free( ref );
304 #endif
305                 }
306         } else {
307 #ifdef NEW_LOGGING
308             LDAP_LOG( OPERATION, INFO, 
309                        "do_add: conn %d  no backend support\n", conn->c_connid, 0, 0 );
310 #else
311             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
312 #endif
313             send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
314                               NULL, "operation not supported within namingContext", NULL, NULL );
315         }
316
317 done:
318         if( modlist != NULL ) {
319                 slap_mods_free( modlist );
320         }
321         if( e != NULL ) {
322                 entry_free( e );
323         }
324
325         return rc;
326 }
327
328 static int slap_mods2entry(
329         Modifications *mods,
330         Entry **e,
331         int repl_user,
332         const char **text,
333         char *textbuf, size_t textlen )
334 {
335         Attribute **tail = &(*e)->e_attrs;
336         assert( *tail == NULL );
337
338         *text = textbuf;
339
340         for( ; mods != NULL; mods = mods->sml_next ) {
341                 Attribute *attr;
342
343                 assert( mods->sml_op == LDAP_MOD_ADD );
344                 assert( mods->sml_desc != NULL );
345
346                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
347
348                 if( attr != NULL ) {
349 #define SLURPD_FRIENDLY
350 #ifdef SLURPD_FRIENDLY
351                         ber_len_t i,j;
352
353                         if( !repl_user ) {
354                                 snprintf( textbuf, textlen,
355                                         "attribute '%s' provided more than once",
356                                         mods->sml_desc->ad_cname.bv_val );
357                                 return LDAP_TYPE_OR_VALUE_EXISTS;
358                         }
359
360                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
361                                 /* count them */
362                         }
363                         for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
364                                 /* count them */
365                         }
366                         j++;    /* NULL */
367                         
368                         attr->a_vals = ch_realloc( attr->a_vals,
369                                 sizeof( struct berval ) * (i+j) );
370
371                         /* should check for duplicates */
372
373                         AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
374                                 sizeof( struct berval ) * j );
375
376                         /* trim the mods array */
377                         ch_free( mods->sml_bvalues );
378                         mods->sml_bvalues = NULL;
379
380                         continue;
381 #else
382                         snprintf( textbuf, textlen,
383                                 "attribute '%s' provided more than once",
384                                 mods->sml_desc->ad_cname.bv_val );
385                         return LDAP_TYPE_OR_VALUE_EXISTS;
386 #endif
387                 }
388
389                 if( mods->sml_bvalues[1].bv_val != NULL ) {
390                         /* check for duplicates */
391                         int             i, j;
392                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
393
394                         /* check if the values we're adding already exist */
395                         if( mr == NULL || !mr->smr_match ) {
396                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
397                                         /* test asserted values against themselves */
398                                         for( j = 0; j < i; j++ ) {
399                                                 if ( bvmatch( &mods->sml_bvalues[i],
400                                                         &mods->sml_bvalues[j] ) ) {
401                                                         /* value exists already */
402                                                         snprintf( textbuf, textlen,
403                                                                 "%s: value #%d provided more than once",
404                                                                 mods->sml_desc->ad_cname.bv_val, j );
405                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
406                                                 }
407                                         }
408                                 }
409
410                         } else {
411                                 int             rc;
412                                 const char      *text = NULL;
413                                 char            textbuf[ SLAP_TEXT_BUFLEN ]  = { '\0' };
414                                 
415                                 rc = modify_check_duplicates( mods->sml_desc, mr,
416                                                 NULL, mods->sml_bvalues,
417                                                 &text, textbuf, sizeof( textbuf ) );
418
419                                 if ( rc != LDAP_SUCCESS ) {
420                                         return rc;
421                                 }
422                         }
423                 }
424
425                 attr = ch_calloc( 1, sizeof(Attribute) );
426
427                 /* move ad to attr structure */
428                 attr->a_desc = mods->sml_desc;
429                 mods->sml_desc = NULL;
430
431                 /* move values to attr structure */
432                 /*      should check for duplicates */
433                 attr->a_vals = mods->sml_bvalues;
434                 mods->sml_bvalues = NULL;
435
436                 *tail = attr;
437                 tail = &attr->a_next;
438         }
439
440         return LDAP_SUCCESS;
441 }