]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
c245a54c62b3c67f537bd970be3a61fdfce4b828
[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 #ifdef  GET_CTRLS
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 #endif
153
154         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
155             conn->c_connid, op->o_opid, ndn, 0, 0 );
156
157         /*
158          * We could be serving multiple database backends.  Select the
159          * appropriate one, or send a referral to our "referral server"
160          * if we don't hold it.
161          */
162         if ( (be = select_backend( ndn )) == NULL ) {
163                 free( ndn );
164                 modlist_free( modlist );
165                 send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
166                     default_referral );
167                 return rc;
168         }
169
170         /* alias suffix if approp */
171         ndn = suffixAlias ( ndn, op, be );
172
173         /*
174          * do the modify if 1 && (2 || 3)
175          * 1) there is a modify function implemented in this backend;
176          * 2) this backend is master for what it holds;
177          * 3) it's a replica and the dn supplied is the update_ndn.
178          */
179         if ( be->be_modify ) {
180                 /* do the update here */
181                 if ( be->be_update_ndn == NULL ||
182                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
183                 {
184                         if ( (*be->be_modify)( be, conn, op, ndn, modlist ) == 0 ) {
185                                 replog( be, LDAP_REQ_MODIFY, ndn, modlist, 0 );
186                         }
187
188                 /* send a referral */
189                 } else {
190                         send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
191                             default_referral );
192                 }
193         } else {
194                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
195                     "Function not implemented" );
196         }
197
198         free( ndn );
199         modlist_free( modlist );
200         return rc;
201 }
202
203 static void
204 modlist_free(
205     LDAPModList *ml
206 )
207 {
208         LDAPModList *next;
209
210         for ( ; ml != NULL; ml = next ) {
211                 next = ml->ml_next;
212
213                 free( ml->ml_type );
214                 if ( ml->ml_bvalues != NULL )
215                         ber_bvecfree( ml->ml_bvalues );
216
217                 free( ml );
218         }
219 }