]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/add.c
Fix prev commit, return generated passwd
[openldap] / servers / slapd / back-meta / add.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2003 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by the Howard Chu for inclusion
17  * in OpenLDAP Software and subsequently enhanced by Pierangelo
18  * Masarati.
19  */
20 /* This is an altered version */
21 /* Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
22  *
23  * This work has been developed to fulfill the requirements
24  * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
25  * to the OpenLDAP Foundation in the hope that it may be useful
26  * to the Open Source community, but WITHOUT ANY WARRANTY.
27  *
28  * Permission is granted to anyone to use this software for any purpose
29  * on any computer system, and to alter it and redistribute it, subject
30  * to the following restrictions:
31  * 
32  * 1. The author and SysNet s.n.c. are not responsible for the consequences
33  *    of use of this software, no matter how awful, even if they arise from 
34  *    flaws in it.
35  *
36  * 2. The origin of this software must not be misrepresented, either by
37  *    explicit claim or by omission.  Since few users ever read sources,
38  *    credits should appear in the documentation.
39  *
40  * 3. Altered versions must be plainly marked as such, and must not be
41  *    misrepresented as being the original software.  Since few users
42  *    ever read sources, credits should appear in the documentation.
43  *    SysNet s.n.c. cannot be responsible for the consequences of the
44  *    alterations.
45  * 
46  * 4. This notice may not be removed or altered.
47  * 
48  * 
49  * This software is based on the backend back-ldap, implemented
50  * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
51  * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
52  * contributors. The contribution of the original software to the present
53  * implementation is acknowledged in this copyright statement.
54  * 
55  * A special acknowledgement goes to Howard for the overall architecture
56  * (and for borrowing large pieces of code), and to Mark, who implemented
57  * from scratch the attribute/objectclass mapping.
58  * 
59  * The original copyright statement follows.
60  *
61  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
62  *
63  * Permission is granted to anyone to use this software for any purpose
64  * on any computer system, and to alter it and redistribute it, subject
65  * to the following restrictions:
66  *
67  * 1. The author is not responsible for the consequences of use of this
68  *    software, no matter how awful, even if they arise from flaws in it.
69  *
70  * 2. The origin of this software must not be misrepresented, either by
71  *    explicit claim or by omission.  Since few users ever read sources,
72  *    credits should appear in the documentation.
73  *
74  * 3. Altered versions must be plainly marked as such, and must not be
75  *    misrepresented as being the original software.  Since few users
76  *    ever read sources, credits should appear in the
77  *    documentation.
78  *
79  * 4. This notice may not be removed or altered.
80  *                
81  */
82
83
84 #include "portable.h"
85
86 #include <stdio.h>
87
88 #include <ac/string.h>
89 #include <ac/socket.h>
90
91 #include "slap.h"
92 #include "../back-ldap/back-ldap.h"
93 #include "back-meta.h"
94
95 int
96 meta_back_add( Operation *op, SlapReply *rs )
97 {
98         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
99         struct metaconn *lc;
100         int i, candidate = -1;
101         Attribute *a;
102         LDAPMod **attrs;
103         struct berval mdn = { 0, NULL }, mapped;
104         dncookie dc;
105
106 #ifdef NEW_LOGGING
107         LDAP_LOG( BACK_META, ENTRY, "meta_back_add: %s\n",
108                         op->o_req_dn.bv_val, 0, 0 );
109 #else /* !NEW_LOGGING */
110         Debug(LDAP_DEBUG_ARGS, "==> meta_back_add: %s\n",
111                         op->o_req_dn.bv_val, 0, 0 );
112 #endif /* !NEW_LOGGING */
113
114         /*
115          * get the current connection
116          */
117         lc = meta_back_getconn( op, rs, META_OP_REQUIRE_SINGLE,
118                         &op->o_req_ndn, &candidate );
119         if ( !lc ) {
120                 send_ldap_result( op, rs );
121         }
122
123         if ( !meta_back_dobind( lc, op )
124                         || !meta_back_is_valid( lc, candidate ) ) {
125                 rs->sr_err = LDAP_OTHER;
126                 send_ldap_result( op, rs );
127                 return -1;
128         }
129
130         /*
131          * Rewrite the add dn, if needed
132          */
133         dc.rwmap = &li->targets[ candidate ]->rwmap;
134         dc.conn = op->o_conn;
135         dc.rs = rs;
136         dc.ctx = "addDn";
137
138         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
139                 send_ldap_result( op, rs );
140                 return -1;
141         }
142
143         /* Count number of attributes in entry */
144         for ( i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next );
145         
146         /* Create array of LDAPMods for ldap_add() */
147         attrs = ch_malloc( sizeof( LDAPMod * )*i );
148
149         for ( i = 0, a = op->oq_add.rs_e->e_attrs; a; a = a->a_next ) {
150                 int j;
151
152                 if ( a->a_desc->ad_type->sat_no_user_mod  ) {
153                         continue;
154                 }
155
156                 ldap_back_map( &li->targets[ candidate ]->rwmap.rwm_at,
157                                 &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
158                 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
159                         continue;
160                 }
161
162                 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
163                 if ( attrs[ i ] == NULL ) {
164                         continue;
165                 }
166                 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
167                 attrs[ i ]->mod_type = mapped.bv_val;
168
169                 /*
170                  * FIXME: dn-valued attrs should be rewritten
171                  * to allow their use in ACLs at the back-ldap
172                  * level.
173                  */
174                 if ( strcmp( a->a_desc->ad_type->sat_syntax->ssyn_oid,
175                                         SLAPD_DN_SYNTAX ) == 0 ) {
176                         (void)ldap_dnattr_rewrite( &dc, a->a_vals );
177                 }
178
179                 for ( j = 0; a->a_vals[ j ].bv_val; j++ );
180                 attrs[ i ]->mod_vals.modv_bvals = ch_malloc((j+1)*sizeof(struct berval *));
181                 for ( j = 0; a->a_vals[ j ].bv_val; j++ ) {
182                         attrs[ i ]->mod_vals.modv_bvals[ j ] = &a->a_vals[ j ];
183                 }
184                 attrs[ i ]->mod_vals.modv_bvals[ j ] = NULL;
185                 i++;
186         }
187         attrs[ i ] = NULL;
188
189         ldap_add_s( lc->conns[ candidate ].ld, mdn.bv_val, attrs );
190         for ( --i; i >= 0; --i ) {
191                 free( attrs[ i ]->mod_vals.modv_bvals );
192                 free( attrs[ i ] );
193         }
194         free( attrs );
195         if ( mdn.bv_val != op->oq_add.rs_e->e_dn ) {
196                 free( mdn.bv_val );
197         }
198         return meta_back_op_result( lc, op, rs );
199 }
200