]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
6be8038bdfd7257c6b749bb0ea539fc246a96e0a
[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, NULL,
49                     "SASL bind in progress" );
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_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL, "" );
75                 return rc;
76         }
77
78         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", ndn, 0, 0 );
79
80         (void) dn_normalize_case( ndn );
81
82         /* collect modifications & save for later */
83         modlist = NULL;
84         modtail = &modlist;
85
86         for ( tag = ber_first_element( op->o_ber, &len, &last );
87             tag != LBER_DEFAULT;
88             tag = ber_next_element( op->o_ber, &len, last ) )
89         {
90                 ber_int_t mop;
91
92                 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
93
94                 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
95                     &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
96                     == LBER_ERROR )
97                 {
98                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
99                             "decoding error" );
100                         free( ndn );
101                         free( *modtail );
102                         *modtail = NULL;
103                         modlist_free( modlist );
104                         return rc;
105                 }
106
107                 (*modtail)->ml_op = mop;
108                 
109                 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
110                     (*modtail)->ml_op != LDAP_MOD_DELETE &&
111                     (*modtail)->ml_op != LDAP_MOD_REPLACE )
112                 {
113                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
114                             "unrecognized modify operation" );
115                         free( ndn );
116                         modlist_free( modlist );
117                         return rc;
118                 }
119
120                 if ( (*modtail)->ml_bvalues == NULL
121                         && (*modtail)->ml_op != LDAP_MOD_DELETE )
122                 {
123                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
124                             "no values given" );
125                         free( ndn );
126                         modlist_free( modlist );
127                         return rc;
128                 }
129                 attr_normalize( (*modtail)->ml_type );
130
131                 modtail = &(*modtail)->ml_next;
132         }
133         *modtail = NULL;
134
135 #ifdef LDAP_DEBUG
136         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
137         for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
138                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
139                         tmp->ml_op == LDAP_MOD_ADD
140                                 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
141                                         ? "delete" : "replace"), tmp->ml_type, 0 );
142         }
143 #endif
144
145         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
146                 free( ndn );
147                 modlist_free( modlist );
148                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
149                 return rc;
150         } 
151
152         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
153             conn->c_connid, op->o_opid, ndn, 0, 0 );
154
155         /*
156          * We could be serving multiple database backends.  Select the
157          * appropriate one, or send a referral to our "referral server"
158          * if we don't hold it.
159          */
160         if ( (be = select_backend( ndn )) == NULL ) {
161                 free( ndn );
162                 modlist_free( modlist );
163                 send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
164                     default_referral );
165                 return rc;
166         }
167
168         /* alias suffix if approp */
169         ndn = suffixAlias ( ndn, op, be );
170
171         /*
172          * do the modify if 1 && (2 || 3)
173          * 1) there is a modify function implemented in this backend;
174          * 2) this backend is master for what it holds;
175          * 3) it's a replica and the dn supplied is the update_ndn.
176          */
177         if ( be->be_modify ) {
178                 /* do the update here */
179                 if ( be->be_update_ndn == NULL ||
180                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
181                 {
182                         if ( (*be->be_modify)( be, conn, op, ndn, modlist ) == 0 ) {
183                                 replog( be, LDAP_REQ_MODIFY, ndn, modlist, 0 );
184                         }
185
186                 /* send a referral */
187                 } else {
188                         send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
189                             default_referral );
190                 }
191         } else {
192                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
193                     "Function not implemented" );
194         }
195
196         free( ndn );
197         modlist_free( modlist );
198         return rc;
199 }
200
201 static void
202 modlist_free(
203     LDAPModList *ml
204 )
205 {
206         LDAPModList *next;
207
208         for ( ; ml != NULL; ml = next ) {
209                 next = ml->ml_next;
210
211                 free( ml->ml_type );
212                 if ( ml->ml_bvalues != NULL )
213                         ber_bvecfree( ml->ml_bvalues );
214
215                 free( ml );
216         }
217 }