]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
26c96709aed041d384cd3dd9a4588f6c5cab1638
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include "slap.h"
8 #include "back-ldbm.h"
9 #include "proto-back-ldbm.h"
10
11 extern char     *dn_parent();
12
13 int
14 ldbm_back_modrdn(
15     Backend     *be,
16     Connection  *conn,
17     Operation   *op,
18     char        *dn,
19     char        *newrdn,
20     int         deleteoldrdn
21 )
22 {
23         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
24         char            *matched;
25         char            *pdn, *newdn, *p;
26         char            sep[2];
27         Entry           *e;
28
29         matched = NULL;
30
31         /* get entry with writer lock */
32         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
33                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, "" );
34                 if ( matched != NULL ) {
35                         free( matched );
36                 }
37                 return( -1 );
38         }
39
40         if ( (pdn = dn_parent( be, dn )) != NULL ) {
41                 /* parent + rdn + separator(s) + null */
42                 newdn = (char *) ch_malloc( strlen( pdn ) + strlen( newrdn )
43                     + 3 );
44                 if ( dn_type( dn ) == DN_X500 ) {
45                         strcpy( newdn, newrdn );
46                         strcat( newdn, ", " );
47                         strcat( newdn, pdn );
48                 } else {
49                         strcpy( newdn, newrdn );
50                         p = strchr( newrdn, '\0' );
51                         p--;
52                         if ( *p != '.' && *p != '@' ) {
53                                 if ( (p = strpbrk( dn, ".@" )) != NULL ) {
54                                         sep[0] = *p;
55                                         sep[1] = '\0';
56                                         strcat( newdn, sep );
57                                 }
58                         }
59                         strcat( newdn, pdn );
60                 }
61         } else {
62                 newdn = strdup( newrdn );
63         }
64         (void) dn_normalize( newdn );
65
66         /* get entry with writer lock */
67         if ( (dn2id ( be, newdn ) ) != NOID ) {
68                 free( newdn );
69                 free( pdn );
70                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, NULL, NULL );
71                 goto error_return;
72         }
73
74         /* check for abandon */
75         pthread_mutex_lock( &op->o_abandonmutex );
76         if ( op->o_abandon ) {
77                 pthread_mutex_unlock( &op->o_abandonmutex );
78                 free( newdn );
79                 free( pdn );
80                 goto error_return;
81         }
82         pthread_mutex_unlock( &op->o_abandonmutex );
83
84         /* add new one */
85         if ( dn2id_add( be, newdn, e->e_id ) != 0 ) {
86                 free( newdn );
87                 free( pdn );
88                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
89                 goto error_return;
90         }
91
92         /* delete old one */
93         if ( dn2id_delete( be, dn ) != 0 ) {
94                 free( newdn );
95                 free( pdn );
96                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
97                 goto error_return;
98         }
99
100         (void) cache_delete_entry( &li->li_cache, e );
101         free( e->e_dn );
102         e->e_dn = newdn;
103
104         /* XXX
105          * At some point here we need to update the attribute values in
106          * the entry itself that were effected by this RDN change
107          * (respecting the value of the deleteoldrdn parameter).
108          *
109          * Since the code to do this has not yet been written, treat this
110          * omission as a (documented) bug.
111          */
112
113         /* id2entry index */
114         if ( id2entry_add( be, e ) != 0 ) {
115                 entry_free( e );
116                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
117                 goto error_return;
118         }
119         free( pdn );
120
121         /* free entry and writer lock */
122         cache_return_entry_w( &li->li_cache, e );
123         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
124
125         return( 0 );
126
127 error_return:
128         /* free entry and writer lock */
129         cache_return_entry_w( &li->li_cache, e );
130         return( -1 );
131 }