]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Backout the input exhaustion change, it loops. Still looking for
[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", 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         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_disconnect( conn, op,
84                                 LDAP_PROTOCOL_ERROR, "decoding error" );
85                         entry_free( e );
86                         return -1;
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,
93                                 NULL, "no values for type", NULL, 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_disconnect( conn, op,
109                         LDAP_PROTOCOL_ERROR, "decoding error" );
110                 return -1;
111         }
112
113         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
114                 entry_free( e );
115                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
116                 return rc;
117         } 
118
119         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d ADD dn=\"%s\"\n",
120             op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
121
122         /*
123          * We could be serving multiple database backends.  Select the
124          * appropriate one, or send a referral to our "referral server"
125          * if we don't hold it.
126          */
127         be = select_backend( e->e_ndn );
128         if ( be == NULL ) {
129                 entry_free( e );
130                 send_ldap_result( conn, op, LDAP_REFERRAL, NULL,
131                     NULL, default_referral, NULL );
132                 return rc;
133         }
134
135         /*
136          * do the add if 1 && (2 || 3)
137          * 1) there is an add function implemented in this backend;
138          * 2) this backend is master for what it holds;
139          * 3) it's a replica and the dn supplied is the updatedn.
140          */
141         if ( be->be_add ) {
142                 /* do the update here */
143                 if ( be->be_update_ndn == NULL ||
144                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
145                 {
146                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
147                                 global_lastmod == ON)) && be->be_update_ndn == NULL ) {
148
149                                 add_created_attrs( op, e );
150                         }
151                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
152                                 replog( be, LDAP_REQ_ADD, e->e_dn, e, 0 );
153                                 be_entry_release_w( be, e );
154                         }
155
156                 } else {
157                         entry_free( e );
158                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
159                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
160                 }
161         } else {
162             Debug( LDAP_DEBUG_ARGS, "    do_add: HHH\n", 0, 0, 0 );
163                 entry_free( e );
164                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
165                         NULL, "Function not implemented", NULL, NULL );
166         }
167
168         return rc;
169 }
170
171 static void
172 add_created_attrs( Operation *op, Entry *e )
173 {
174         char            buf[22];
175         struct berval   bv;
176         struct berval   *bvals[2];
177         Attribute       **a, **next;
178         Attribute       *tmp;
179         struct tm       *ltm;
180         time_t          currenttime;
181
182         Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
183
184         bvals[0] = &bv;
185         bvals[1] = NULL;
186
187         /* remove any attempts by the user to add these attrs */
188         for ( a = &e->e_attrs; *a != NULL; a = next ) {
189                 if ( oc_check_no_usermod_attr( (*a)->a_type ) ) {
190                         tmp = *a;
191                         *a = (*a)->a_next;
192                         attr_free( tmp );
193                         next = a;
194                 } else {
195                         next = &(*a)->a_next;
196                 }
197         }
198
199         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
200                 bv.bv_val = "<anonymous>";
201                 bv.bv_len = strlen( bv.bv_val );
202         } else {
203                 bv.bv_val = op->o_dn;
204                 bv.bv_len = strlen( bv.bv_val );
205         }
206         attr_merge( e, "creatorsname", bvals );
207
208         currenttime = slap_get_time();
209         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
210 #ifndef LDAP_LOCALTIME
211         ltm = gmtime( &currenttime );
212         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
213 #else
214         ltm = localtime( &currenttime );
215         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
216 #endif
217         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
218
219         bv.bv_val = buf;
220         bv.bv_len = strlen( bv.bv_val );
221         attr_merge( e, "createtimestamp", bvals );
222 }