]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
441ac206847793868677295be81c8e7200f540b6
[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         dn = NULL;
87
88         Debug( LDAP_DEBUG_ARGS, "    do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
89
90         /* get the attrs */
91         e->e_attrs = NULL;
92         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
93             tag = ber_next_element( ber, &len, last ) ) {
94                 char            *type;
95                 struct berval   **vals;
96
97                 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
98                         send_ldap_disconnect( conn, op,
99                                 LDAP_PROTOCOL_ERROR, "decoding error" );
100                         entry_free( e );
101                         return -1;
102                 }
103
104                 if ( vals == NULL ) {
105                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
106                             0, 0 );
107                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
108                                 NULL, "no values for type", NULL, NULL );
109                         free( type );
110                         entry_free( e );
111                         return LDAP_PROTOCOL_ERROR;
112                 }
113
114                 attr_merge( e, type, vals );
115
116                 free( type );
117                 ber_bvecfree( vals );
118         }
119
120         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
121                 entry_free( e );
122                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
123                 send_ldap_disconnect( conn, op,
124                         LDAP_PROTOCOL_ERROR, "decoding error" );
125                 return -1;
126         }
127
128         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
129                 entry_free( e );
130                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
131                 return rc;
132         } 
133
134         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
135             op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
136
137         /*
138          * We could be serving multiple database backends.  Select the
139          * appropriate one, or send a referral to our "referral server"
140          * if we don't hold it.
141          */
142         be = select_backend( e->e_ndn );
143         if ( be == NULL ) {
144                 entry_free( e );
145                 send_ldap_result( conn, op, LDAP_REFERRAL, NULL,
146                     NULL, default_referral, NULL );
147                 return rc;
148         }
149
150         /*
151          * do the add if 1 && (2 || 3)
152          * 1) there is an add function implemented in this backend;
153          * 2) this backend is master for what it holds;
154          * 3) it's a replica and the dn supplied is the updatedn.
155          */
156         if ( be->be_add ) {
157                 /* do the update here */
158 #ifdef SLAPD_MULTIMASTER
159                 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
160                         global_lastmod == ON)) && (be->be_update_ndn == NULL ||
161                         strcmp( be->be_update_ndn, op->o_ndn )) )
162 #else
163                 if ( be->be_update_ndn == NULL ||
164                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
165 #endif
166                 {
167 #ifndef SLAPD_MULTIMASTER
168                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
169                                 global_lastmod == ON)) && be->be_update_ndn == NULL )
170 #endif
171                         {
172                                 rc = add_created_attrs( op, e );
173
174                                 if( rc != LDAP_SUCCESS ) {
175                                         entry_free( e );
176                                         send_ldap_result( conn, op, rc,
177                                                 NULL, "no-user-modification attribute type",
178                                                 NULL, NULL );
179                                         return rc;
180                                 }
181                         }
182
183                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
184 #ifdef SLAPD_MULTIMASTER
185                                 if (be->be_update_ndn == NULL ||
186                                         strcmp( be->be_update_ndn, op->o_ndn ))
187 #endif
188                                 {
189                                         replog( be, op, e->e_dn, e );
190                                 }
191                                 be_entry_release_w( be, e );
192                         }
193
194 #ifndef SLAPD_MULTIMASTER
195                 } else {
196                         entry_free( e );
197                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
198                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
199 #endif
200                 }
201         } else {
202             Debug( LDAP_DEBUG_ARGS, "    do_add: HHH\n", 0, 0, 0 );
203                 entry_free( e );
204                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
205                         NULL, "Function not implemented", NULL, NULL );
206         }
207
208         return rc;
209 }
210
211 static int
212 add_created_attrs( Operation *op, Entry *e )
213 {
214         char            buf[22];
215         struct berval   bv;
216         struct berval   *bvals[2];
217         Attribute       *a;
218         struct tm       *ltm;
219         time_t          currenttime;
220
221         Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
222
223         bvals[0] = &bv;
224         bvals[1] = NULL;
225
226         /* return error on any attempts by the user to add these attrs */
227         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
228                 if ( oc_check_no_usermod_attr( a->a_type ) ) {
229                         return LDAP_CONSTRAINT_VIOLATION;
230                 }
231         }
232
233         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
234                 bv.bv_val = "<anonymous>";
235                 bv.bv_len = strlen( bv.bv_val );
236         } else {
237                 bv.bv_val = op->o_dn;
238                 bv.bv_len = strlen( bv.bv_val );
239         }
240         attr_merge( e, "creatorsname", bvals );
241
242         currenttime = slap_get_time();
243         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
244 #ifndef LDAP_LOCALTIME
245         ltm = gmtime( &currenttime );
246         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
247 #else
248         ltm = localtime( &currenttime );
249         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
250 #endif
251         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
252
253         bv.bv_val = buf;
254         bv.bv_len = strlen( bv.bv_val );
255         attr_merge( e, "createtimestamp", bvals );
256
257         return LDAP_SUCCESS;
258 }