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