]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Fix -USLAPD_RLOOKUPS
[openldap] / servers / slapd / add.c
1 /*
2  * Copyright (c) 1995 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/string.h>
18 #include <ac/time.h>
19 #include <ac/socket.h>
20
21 #include "slap.h"
22
23 static void     add_created_attrs(Operation *op, Entry *e);
24
25 int
26 do_add( Connection *conn, Operation *op )
27 {
28         BerElement      *ber = op->o_ber;
29         char            *dn, *ndn, *last;
30         ber_len_t       len;
31         ber_tag_t       tag;
32         Entry           *e;
33         Backend         *be;
34         int                     rc = LDAP_SUCCESS;
35
36         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
37
38         if( op->o_bind_in_progress ) {
39                 Debug( LDAP_DEBUG_ANY, "do_add: SASL bind in progress.\n", 0, 0, 0 );
40                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS, NULL,
41                     "SASL bind in progress", NULL, NULL );
42                 return LDAP_SASL_BIND_IN_PROGRESS;
43         }
44
45         /*
46          * Parse the add request.  It looks like this:
47          *
48          *      AddRequest := [APPLICATION 14] SEQUENCE {
49          *              name    DistinguishedName,
50          *              attrs   SEQUENCE OF SEQUENCE {
51          *                      type    AttributeType,
52          *                      values  SET OF AttributeValue
53          *              }
54          *      }
55          */
56
57         /* get the name */
58         if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
59                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
60                 send_ldap_disconnect( conn, op,
61                         LDAP_PROTOCOL_ERROR, "decoding error" );
62                 return -1;
63         }
64
65         ndn = ch_strdup( dn );
66
67         if ( dn_normalize_case( ndn ) == NULL ) {
68                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
69                 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
70                     "invalid DN", NULL, NULL );
71                 free( dn );
72                 free( ndn );
73                 return LDAP_INVALID_DN_SYNTAX;
74         }
75
76         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
77
78         e->e_dn = dn;
79         e->e_ndn = ndn;
80         e->e_private = NULL;
81
82         dn = NULL;
83
84         Debug( LDAP_DEBUG_ARGS, "    do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
85
86         /* get the attrs */
87         e->e_attrs = NULL;
88         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
89             tag = ber_next_element( ber, &len, last ) ) {
90                 char            *type;
91                 struct berval   **vals;
92
93                 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
94                         send_ldap_disconnect( conn, op,
95                                 LDAP_PROTOCOL_ERROR, "decoding error" );
96                         entry_free( e );
97                         return -1;
98                 }
99
100                 if ( vals == NULL ) {
101                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
102                             0, 0 );
103                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
104                                 NULL, "no values for type", NULL, NULL );
105                         free( type );
106                         entry_free( e );
107                         return LDAP_PROTOCOL_ERROR;
108                 }
109
110                 attr_merge( e, type, vals );
111
112                 free( type );
113                 ber_bvecfree( vals );
114         }
115
116         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
117                 entry_free( e );
118                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
119                 send_ldap_disconnect( conn, op,
120                         LDAP_PROTOCOL_ERROR, "decoding error" );
121                 return -1;
122         }
123
124         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
125                 entry_free( e );
126                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
127                 return rc;
128         } 
129
130         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d ADD dn=\"%s\"\n",
131             op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
132
133         /*
134          * We could be serving multiple database backends.  Select the
135          * appropriate one, or send a referral to our "referral server"
136          * if we don't hold it.
137          */
138         be = select_backend( e->e_ndn );
139         if ( be == NULL ) {
140                 entry_free( e );
141                 send_ldap_result( conn, op, LDAP_REFERRAL, NULL,
142                     NULL, default_referral, NULL );
143                 return rc;
144         }
145
146         /*
147          * do the add if 1 && (2 || 3)
148          * 1) there is an add function implemented in this backend;
149          * 2) this backend is master for what it holds;
150          * 3) it's a replica and the dn supplied is the updatedn.
151          */
152         if ( be->be_add ) {
153                 /* do the update here */
154                 if ( be->be_update_ndn == NULL ||
155                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
156                 {
157                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
158                                 global_lastmod == ON)) && be->be_update_ndn == NULL ) {
159
160                                 add_created_attrs( op, e );
161                         }
162                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
163                                 replog( be, LDAP_REQ_ADD, e->e_dn, e, 0 );
164                                 be_entry_release_w( be, e );
165                         }
166
167                 } else {
168                         entry_free( e );
169                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
170                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
171                 }
172         } else {
173             Debug( LDAP_DEBUG_ARGS, "    do_add: HHH\n", 0, 0, 0 );
174                 entry_free( e );
175                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
176                         NULL, "Function not implemented", NULL, NULL );
177         }
178
179         return rc;
180 }
181
182 static void
183 add_created_attrs( Operation *op, Entry *e )
184 {
185         char            buf[22];
186         struct berval   bv;
187         struct berval   *bvals[2];
188         Attribute       **a, **next;
189         Attribute       *tmp;
190         struct tm       *ltm;
191         time_t          currenttime;
192
193         Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
194
195         bvals[0] = &bv;
196         bvals[1] = NULL;
197
198         /* remove any attempts by the user to add these attrs */
199         for ( a = &e->e_attrs; *a != NULL; a = next ) {
200                 if ( oc_check_no_usermod_attr( (*a)->a_type ) ) {
201                         tmp = *a;
202                         *a = (*a)->a_next;
203                         attr_free( tmp );
204                         next = a;
205                 } else {
206                         next = &(*a)->a_next;
207                 }
208         }
209
210         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
211                 bv.bv_val = "<anonymous>";
212                 bv.bv_len = strlen( bv.bv_val );
213         } else {
214                 bv.bv_val = op->o_dn;
215                 bv.bv_len = strlen( bv.bv_val );
216         }
217         attr_merge( e, "creatorsname", bvals );
218
219         currenttime = slap_get_time();
220         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
221 #ifndef LDAP_LOCALTIME
222         ltm = gmtime( &currenttime );
223         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
224 #else
225         ltm = localtime( &currenttime );
226         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
227 #endif
228         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
229
230         bv.bv_val = buf;
231         bv.bv_len = strlen( bv.bv_val );
232         attr_merge( e, "createtimestamp", bvals );
233 }