]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
Initial revision
[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
10 extern Entry    *dn2entry();
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, *e2;
28
29         matched = NULL;
30         if ( (e = dn2entry( be, dn, &matched )) == NULL ) {
31                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, "" );
32                 if ( matched != NULL ) {
33                         free( matched );
34                 }
35                 return( -1 );
36         }
37
38         if ( (pdn = dn_parent( be, dn )) != NULL ) {
39                 /* parent + rdn + separator(s) + null */
40                 newdn = (char *) ch_malloc( strlen( pdn ) + strlen( newrdn )
41                     + 3 );
42                 if ( dn_type( dn ) == DN_X500 ) {
43                         strcpy( newdn, newrdn );
44                         strcat( newdn, ", " );
45                         strcat( newdn, pdn );
46                 } else {
47                         strcpy( newdn, newrdn );
48                         p = strchr( newrdn, '\0' );
49                         p--;
50                         if ( *p != '.' && *p != '@' ) {
51                                 if ( (p = strpbrk( dn, ".@" )) != NULL ) {
52                                         sep[0] = *p;
53                                         sep[1] = '\0';
54                                         strcat( newdn, sep );
55                                 }
56                         }
57                         strcat( newdn, pdn );
58                 }
59         } else {
60                 newdn = strdup( newrdn );
61         }
62         (void) dn_normalize( newdn );
63
64         matched = NULL;
65         if ( (e2 = dn2entry( be, newdn, &matched )) != NULL ) {
66                 free( newdn );
67                 free( pdn );
68                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, NULL, NULL );
69                 cache_return_entry( &li->li_cache, e2 );
70                 cache_return_entry( &li->li_cache, e );
71                 return( -1 );
72         }
73         if ( matched != NULL ) {
74                 free( matched );
75         }
76
77         /* check for abandon */
78         pthread_mutex_lock( &op->o_abandonmutex );
79         if ( op->o_abandon ) {
80                 pthread_mutex_unlock( &op->o_abandonmutex );
81                 free( newdn );
82                 free( pdn );
83                 cache_return_entry( &li->li_cache, e2 );
84                 cache_return_entry( &li->li_cache, e );
85                 return( -1 );
86         }
87         pthread_mutex_unlock( &op->o_abandonmutex );
88
89         /* add new one */
90         if ( dn2id_add( be, newdn, e->e_id ) != 0 ) {
91                 free( newdn );
92                 free( pdn );
93                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
94                 cache_return_entry( &li->li_cache, e );
95                 return( -1 );
96         }
97
98         /* delete old one */
99         if ( dn2id_delete( be, dn ) != 0 ) {
100                 free( newdn );
101                 free( pdn );
102                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
103                 cache_return_entry( &li->li_cache, e );
104                 return( -1 );
105         }
106
107         (void) cache_delete_entry( &li->li_cache, e );
108         free( e->e_dn );
109         e->e_dn = newdn;
110
111         /* XXX
112          * At some point here we need to update the attribute values in
113          * the entry itself that were effected by this RDN change
114          * (respecting the value of the deleteoldrdn parameter).
115          *
116          * Since the code to do this has not yet been written, treat this
117          * omission as a (documented) bug.
118          */
119
120         /* id2entry index */
121         if ( id2entry_add( be, e ) != 0 ) {
122                 entry_free( e );
123
124                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
125                 return( -1 );
126         }
127         free( pdn );
128         cache_return_entry( &li->li_cache, e );
129         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
130
131         return( 0 );
132 }