]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/add.c
Happy new year
[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 = { 0, NULL };
46         ber_int_t msgid;
47         dncookie dc;
48 #ifdef LDAP_BACK_PROXY_AUTHZ 
49         LDAPControl **ctrls = NULL;
50         int rc = LDAP_SUCCESS;
51 #endif /* LDAP_BACK_PROXY_AUTHZ */
52
53 #ifdef NEW_LOGGING
54         LDAP_LOG( BACK_LDAP, ENTRY, "ldap_back_add: %s\n", op->o_req_dn.bv_val, 0, 0 );
55 #else /* !NEW_LOGGING */
56         Debug(LDAP_DEBUG_ARGS, "==> ldap_back_add: %s\n", op->o_req_dn.bv_val, 0, 0);
57 #endif /* !NEW_LOGGING */
58         
59         lc = ldap_back_getconn(op, rs);
60         if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
61                 return( -1 );
62         }
63
64         /*
65          * Rewrite the add dn, if needed
66          */
67         dc.rwmap = &li->rwmap;
68 #ifdef ENABLE_REWRITE
69         dc.conn = op->o_conn;
70         dc.rs = rs;
71         dc.ctx = "addDn";
72 #else
73         dc.tofrom = 1;
74         dc.normalized = 0;
75 #endif
76         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
77                 send_ldap_result( op, rs );
78                 return -1;
79         }
80
81         /* Count number of attributes in entry */
82         for (i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next)
83                 ;
84         
85         /* Create array of LDAPMods for ldap_add() */
86         attrs = (LDAPMod **)ch_malloc(sizeof(LDAPMod *)*i);
87
88 #ifdef ENABLE_REWRITE
89         dc.ctx = "addDnAttr";
90 #endif
91         for (i=0, a=op->oq_add.rs_e->e_attrs; a; a=a->a_next) {
92                 if ( a->a_desc->ad_type->sat_no_user_mod  ) {
93                         continue;
94                 }
95
96                 ldap_back_map(&li->rwmap.rwm_at, &a->a_desc->ad_cname, &mapped,
97                                 BACKLDAP_MAP);
98                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
99                         continue;
100                 }
101
102                 attrs[i] = (LDAPMod *)ch_malloc(sizeof(LDAPMod));
103                 if (attrs[i] == NULL) {
104                         continue;
105                 }
106
107                 attrs[i]->mod_op = LDAP_MOD_BVALUES;
108                 attrs[i]->mod_type = mapped.bv_val;
109
110                 if ( a->a_desc->ad_type->sat_syntax ==
111                         slap_schema.si_syn_distinguishedName ) {
112                         /*
113                          * FIXME: rewrite could fail; in this case
114                          * the operation should give up, right?
115                          */
116                         (void)ldap_dnattr_rewrite( &dc, a->a_vals );
117                 }
118
119                 for (j=0; a->a_vals[j].bv_val; j++);
120                 attrs[i]->mod_vals.modv_bvals = ch_malloc((j+1)*sizeof(struct berval *));
121                 for (j=0; a->a_vals[j].bv_val; j++)
122                         attrs[i]->mod_vals.modv_bvals[j] = &a->a_vals[j];
123                 attrs[i]->mod_vals.modv_bvals[j] = NULL;
124                 i++;
125         }
126         attrs[i] = NULL;
127
128 #ifdef LDAP_BACK_PROXY_AUTHZ
129         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
130         if ( rc != LDAP_SUCCESS ) {
131                 goto cleanup;
132         }
133 #endif /* LDAP_BACK_PROXY_AUTHZ */
134
135         rs->sr_err = ldap_add_ext(lc->ld, mdn.bv_val, attrs,
136 #ifdef LDAP_BACK_PROXY_AUTHZ
137                         ctrls,
138 #else /* ! LDAP_BACK_PROXY_AUTHZ */
139                         op->o_ctrls,
140 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
141                         NULL, &msgid);
142
143 #ifdef LDAP_BACK_PROXY_AUTHZ
144 cleanup:
145         if ( ctrls && ctrls != op->o_ctrls ) {
146                 free( ctrls[ 0 ] );
147                 free( ctrls );
148         } 
149 #endif /* LDAP_BACK_PROXY_AUTHZ */
150
151         for (--i; i>= 0; --i) {
152                 ch_free(attrs[i]->mod_vals.modv_bvals);
153                 ch_free(attrs[i]);
154         }
155         ch_free(attrs);
156         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
157                 free( mdn.bv_val );
158         }
159 #ifdef LDAP_BACK_PROXY_AUTHZ
160         if ( rc != LDAP_SUCCESS ) {
161                 send_ldap_result( op, rs );
162                 return -1;
163         }
164 #endif /* LDAP_BACK_PROXY_AUTHZ */
165         return ldap_back_op_result( lc, op, rs, msgid, 1 ) != LDAP_SUCCESS;
166 }
167