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