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