]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/add.c
699503aca8e25093a511afa326f6c733a8de9b66
[openldap] / servers / slapd / back-ldap / add.c
1 /* add.c - ldap backend add function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2000-2003 Pierangelo Masarati.
7  * Portions Copyright 1999-2003 Howard Chu.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "back-ldap.h"
33
34 int
35 ldap_back_add(
36     Operation   *op,
37     SlapReply   *rs )
38 {
39         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
40         struct ldapconn *lc;
41         int i, j;
42         Attribute *a;
43         LDAPMod **attrs;
44         struct berval mapped;
45         struct berval mdn = BER_BVNULL;
46         ber_int_t msgid;
47         dncookie dc;
48         int isupdate;
49         LDAPControl **ctrls = NULL;
50 #ifdef LDAP_BACK_PROXY_AUTHZ 
51         int rc = LDAP_SUCCESS;
52 #endif /* LDAP_BACK_PROXY_AUTHZ */
53
54 #ifdef NEW_LOGGING
55         LDAP_LOG( BACK_LDAP, ENTRY, "ldap_back_add: %s\n", op->o_req_dn.bv_val, 0, 0 );
56 #else /* !NEW_LOGGING */
57         Debug(LDAP_DEBUG_ARGS, "==> ldap_back_add: %s\n", op->o_req_dn.bv_val, 0, 0);
58 #endif /* !NEW_LOGGING */
59         
60         lc = ldap_back_getconn(op, rs);
61         if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
62                 return( -1 );
63         }
64
65         /*
66          * Rewrite the add dn, if needed
67          */
68         dc.rwmap = &li->rwmap;
69 #ifdef ENABLE_REWRITE
70         dc.conn = op->o_conn;
71         dc.rs = rs;
72         dc.ctx = "addDN";
73 #else
74         dc.tofrom = 1;
75         dc.normalized = 0;
76 #endif
77         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
78                 send_ldap_result( op, rs );
79                 return -1;
80         }
81
82         /* Count number of attributes in entry */
83         for (i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next)
84                 ;
85         
86         /* Create array of LDAPMods for ldap_add() */
87         attrs = (LDAPMod **)ch_malloc(sizeof(LDAPMod *)*i);
88
89 #ifdef ENABLE_REWRITE
90         dc.ctx = "addAttrDN";
91 #endif
92
93         isupdate = be_shadow_update( op );
94         for (i=0, a=op->oq_add.rs_e->e_attrs; a; a=a->a_next) {
95                 if ( !isupdate && a->a_desc->ad_type->sat_no_user_mod  ) {
96                         continue;
97                 }
98
99                 ldap_back_map(&li->rwmap.rwm_at, &a->a_desc->ad_cname, &mapped,
100                                 BACKLDAP_MAP);
101                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
102                         continue;
103                 }
104
105                 attrs[i] = (LDAPMod *)ch_malloc(sizeof(LDAPMod));
106                 if (attrs[i] == NULL) {
107                         continue;
108                 }
109
110                 attrs[i]->mod_op = LDAP_MOD_BVALUES;
111                 attrs[i]->mod_type = mapped.bv_val;
112
113                 if ( a->a_desc->ad_type->sat_syntax ==
114                         slap_schema.si_syn_distinguishedName ) {
115                         /*
116                          * FIXME: rewrite could fail; in this case
117                          * the operation should give up, right?
118                          */
119                         (void)ldap_dnattr_rewrite( &dc, a->a_vals );
120                 }
121
122                 for (j=0; a->a_vals[j].bv_val; j++);
123                 attrs[i]->mod_vals.modv_bvals = ch_malloc((j+1)*sizeof(struct berval *));
124                 for (j=0; a->a_vals[j].bv_val; j++)
125                         attrs[i]->mod_vals.modv_bvals[j] = &a->a_vals[j];
126                 attrs[i]->mod_vals.modv_bvals[j] = NULL;
127                 i++;
128         }
129         attrs[i] = NULL;
130
131         ctrls = op->o_ctrls;
132 #ifdef LDAP_BACK_PROXY_AUTHZ
133         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
134         if ( rc != LDAP_SUCCESS ) {
135                 goto cleanup;
136         }
137 #endif /* LDAP_BACK_PROXY_AUTHZ */
138
139         rs->sr_err = ldap_add_ext(lc->ld, mdn.bv_val, attrs,
140                         ctrls, NULL, &msgid);
141
142 #ifdef LDAP_BACK_PROXY_AUTHZ
143 cleanup:
144         if ( ctrls && ctrls != op->o_ctrls ) {
145                 free( ctrls[ 0 ] );
146                 free( ctrls );
147         } 
148 #endif /* LDAP_BACK_PROXY_AUTHZ */
149
150         for (--i; i>= 0; --i) {
151                 ch_free(attrs[i]->mod_vals.modv_bvals);
152                 ch_free(attrs[i]);
153         }
154         ch_free(attrs);
155         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
156                 free( mdn.bv_val );
157         }
158 #ifdef LDAP_BACK_PROXY_AUTHZ
159         if ( rc != LDAP_SUCCESS ) {
160                 send_ldap_result( op, rs );
161                 return -1;
162         }
163 #endif /* LDAP_BACK_PROXY_AUTHZ */
164         return ldap_back_op_result( lc, op, rs, msgid, 1 ) != LDAP_SUCCESS;
165 }
166