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