]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
016033742c4f869b2816773a770176d9460da009
[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 int
26 do_modify(
27     Connection  *conn,
28     Operation   *op
29 )
30 {
31         char            *ndn;
32         char            *last;
33         ber_tag_t       tag;
34         ber_len_t       len;
35         LDAPModList     *modlist;
36         LDAPModList     **modtail;
37 #ifdef LDAP_DEBUG
38         LDAPModList *tmp;
39 #endif
40         Backend         *be;
41         int rc;
42
43         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
44
45         if( op->o_bind_in_progress ) {
46                 Debug( LDAP_DEBUG_ANY, "do_modify: SASL bind in progress.\n",
47                         0, 0, 0 );
48                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
49                         NULL, "SASL bind in progress", NULL, NULL );
50                 return LDAP_SASL_BIND_IN_PROGRESS;
51         }
52
53         /*
54          * Parse the modify request.  It looks like this:
55          *
56          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
57          *              name    DistinguishedName,
58          *              mods    SEQUENCE OF SEQUENCE {
59          *                      operation       ENUMERATED {
60          *                              add     (0),
61          *                              delete  (1),
62          *                              replace (2)
63          *                      },
64          *                      modification    SEQUENCE {
65          *                              type    AttributeType,
66          *                              values  SET OF AttributeValue
67          *                      }
68          *              }
69          *      }
70          */
71
72         if ( ber_scanf( op->o_ber, "{a" /*}*/, &ndn ) == LBER_ERROR ) {
73                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
74                 send_ldap_disconnect( conn, op,
75                         LDAP_PROTOCOL_ERROR, "decoding error" );
76                 return -1;
77         }
78
79         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", ndn, 0, 0 );
80
81         (void) dn_normalize_case( ndn );
82
83         /* collect modifications & save for later */
84         modlist = NULL;
85         modtail = &modlist;
86
87         for ( tag = ber_first_element( op->o_ber, &len, &last );
88             tag != LBER_DEFAULT;
89             tag = ber_next_element( op->o_ber, &len, last ) )
90         {
91                 ber_int_t mop;
92
93                 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
94
95                 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
96                     &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
97                     == LBER_ERROR )
98                 {
99                         send_ldap_disconnect( conn, op,
100                                 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
101                         free( ndn );
102                         free( *modtail );
103                         *modtail = NULL;
104                         modlist_free( modlist );
105                         return -1;
106                 }
107
108                 (*modtail)->ml_op = mop;
109                 
110                 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
111                     (*modtail)->ml_op != LDAP_MOD_DELETE &&
112                     (*modtail)->ml_op != LDAP_MOD_REPLACE )
113                 {
114                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
115                             NULL, "unrecognized modify operation", NULL, NULL );
116                         free( ndn );
117                         modlist_free( modlist );
118                         return LDAP_PROTOCOL_ERROR;
119                 }
120
121                 if ( (*modtail)->ml_bvalues == NULL
122                         && (*modtail)->ml_op != LDAP_MOD_DELETE )
123                 {
124                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
125                             NULL, "unrecognized modify operation", NULL, NULL );
126                         free( ndn );
127                         modlist_free( modlist );
128                         return LDAP_PROTOCOL_ERROR;
129                 }
130                 attr_normalize( (*modtail)->ml_type );
131
132                 modtail = &(*modtail)->ml_next;
133         }
134         *modtail = NULL;
135
136 #ifdef LDAP_DEBUG
137         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
138         for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
139                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
140                         tmp->ml_op == LDAP_MOD_ADD
141                                 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
142                                         ? "delete" : "replace"), tmp->ml_type, 0 );
143         }
144 #endif
145
146         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
147                 free( ndn );
148                 modlist_free( modlist );
149                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
150                 return rc;
151         } 
152
153         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
154             op->o_connid, op->o_opid, ndn, 0, 0 );
155
156         /*
157          * We could be serving multiple database backends.  Select the
158          * appropriate one, or send a referral to our "referral server"
159          * if we don't hold it.
160          */
161         if ( (be = select_backend( ndn )) == NULL ) {
162                 free( ndn );
163                 modlist_free( modlist );
164                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
165                         NULL, NULL, default_referral, NULL );
166                 return rc;
167         }
168
169         /*
170          * do the modify if 1 && (2 || 3)
171          * 1) there is a modify function implemented in this backend;
172          * 2) this backend is master for what it holds;
173          * 3) it's a replica and the dn supplied is the update_ndn.
174          */
175         if ( be->be_modify ) {
176                 /* do the update here */
177                 if ( be->be_update_ndn == NULL ||
178                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
179                 {
180                         if ( (*be->be_modify)( be, conn, op, ndn, modlist ) == 0 ) {
181                                 replog( be, LDAP_REQ_MODIFY, ndn, modlist, 0 );
182                         }
183
184                 /* send a referral */
185                 } else {
186                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
187                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
188                 }
189         } else {
190                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
191                     NULL, "Function not implemented", NULL, NULL );
192         }
193
194         free( ndn );
195         modlist_free( modlist );
196         return rc;
197 }
198
199 static void
200 modlist_free(
201     LDAPModList *ml
202 )
203 {
204         LDAPModList *next;
205
206         for ( ; ml != NULL; ml = next ) {
207                 next = ml->ml_next;
208
209                 free( ml->ml_type );
210                 if ( ml->ml_bvalues != NULL )
211                         ber_bvecfree( ml->ml_bvalues );
212
213                 free( ml );
214         }
215 }