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