]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Send LDAP_SASL_BIND_IN_PROGRESS if o_bind_in_progress is true.
[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, *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" );
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_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
61                     "decoding error" );
62                 return LDAP_PROTOCOL_ERROR;
63         }
64
65         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
66
67         e->e_dn = dn;
68         e->e_ndn = dn_normalize_case( ch_strdup( dn ) );
69         e->e_private = NULL;
70
71         dn = NULL;
72
73         Debug( LDAP_DEBUG_ARGS, "    do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
74
75         /* get the attrs */
76         e->e_attrs = NULL;
77         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
78             tag = ber_next_element( ber, &len, last ) ) {
79                 char            *type;
80                 struct berval   **vals;
81
82                 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
83                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
84                             NULL, "decoding error" );
85                         entry_free( e );
86                         return LDAP_PROTOCOL_ERROR;
87                 }
88
89                 if ( vals == NULL ) {
90                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
91                             0, 0 );
92                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
93                             NULL );
94                         free( type );
95                         entry_free( e );
96                         return LDAP_PROTOCOL_ERROR;
97                 }
98
99                 attr_merge( e, type, vals );
100
101                 free( type );
102                 ber_bvecfree( vals );
103         }
104
105         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
106                 entry_free( e );
107                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
108                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
109                     "decoding error" );
110                 return LDAP_PROTOCOL_ERROR;
111         }
112
113 #ifdef GET_CTRLS
114         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
115                 entry_free( e );
116                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
117                 return rc;
118         } 
119 #endif
120
121         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d ADD dn=\"%s\"\n",
122             conn->c_connid, op->o_opid, e->e_ndn, 0, 0 );
123
124         /*
125          * We could be serving multiple database backends.  Select the
126          * appropriate one, or send a referral to our "referral server"
127          * if we don't hold it.
128          */
129         be = select_backend( e->e_ndn );
130         if ( be == NULL ) {
131                 entry_free( e );
132                 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
133                     default_referral );
134                 return rc;
135         }
136
137         /*
138          * do the add if 1 && (2 || 3)
139          * 1) there is an add function implemented in this backend;
140          * 2) this backend is master for what it holds;
141          * 3) it's a replica and the dn supplied is the updatedn.
142          */
143         if ( be->be_add ) {
144                 /* do the update here */
145                 if ( be->be_update_ndn == NULL ||
146                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
147                 {
148                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
149                                 global_lastmod == ON)) && be->be_update_ndn == NULL ) {
150
151                                 add_created_attrs( op, e );
152                         }
153                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
154                                 replog( be, LDAP_REQ_ADD, e->e_dn, e, 0 );
155                                 be_entry_release_w( be, e );
156                         }
157
158                 } else {
159                         entry_free( e );
160                         send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
161                             default_referral );
162                 }
163         } else {
164             Debug( LDAP_DEBUG_ARGS, "    do_add: HHH\n", 0, 0, 0 );
165                 entry_free( e );
166                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
167                     "Function not implemented" );
168         }
169
170         return rc;
171 }
172
173 static void
174 add_created_attrs( Operation *op, Entry *e )
175 {
176         char            buf[22];
177         struct berval   bv;
178         struct berval   *bvals[2];
179         Attribute       **a, **next;
180         Attribute       *tmp;
181         struct tm       *ltm;
182         time_t          currenttime;
183
184         Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
185
186         bvals[0] = &bv;
187         bvals[1] = NULL;
188
189         /* remove any attempts by the user to add these attrs */
190         for ( a = &e->e_attrs; *a != NULL; a = next ) {
191                 if ( oc_check_operational( (*a)->a_type ) ) {
192                         tmp = *a;
193                         *a = (*a)->a_next;
194                         attr_free( tmp );
195                         next = a;
196                 } else {
197                         next = &(*a)->a_next;
198                 }
199         }
200
201         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
202                 bv.bv_val = "NULLDN";
203                 bv.bv_len = strlen( bv.bv_val );
204         } else {
205                 bv.bv_val = op->o_dn;
206                 bv.bv_len = strlen( bv.bv_val );
207         }
208         attr_merge( e, "creatorsname", bvals );
209
210         currenttime = slap_get_time();
211         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
212 #ifndef LDAP_LOCALTIME
213         ltm = gmtime( &currenttime );
214         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
215 #else
216         ltm = localtime( &currenttime );
217         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
218 #endif
219         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
220
221         bv.bv_val = buf;
222         bv.bv_len = strlen( bv.bv_val );
223         attr_merge( e, "createtimestamp", bvals );
224 }