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