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