]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Added connection initialisation and destruction notification. Now backends can regist...
[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, "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         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d ADD dn=\"%s\"\n",
98             conn->c_connid, op->o_opid, e->e_ndn, 0, 0 );
99
100         /*
101          * We could be serving multiple database backends.  Select the
102          * appropriate one, or send a referral to our "referral server"
103          * if we don't hold it.
104          */
105         be = select_backend( e->e_ndn );
106         if ( be == NULL ) {
107                 entry_free( e );
108                 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
109                     default_referral );
110                 return;
111         }
112
113         /*
114          * do the add if 1 && (2 || 3)
115          * 1) there is an add function implemented in this backend;
116          * 2) this backend is master for what it holds;
117          * 3) it's a replica and the dn supplied is the updatedn.
118          */
119         if ( be->be_add ) {
120                 /* do the update here */
121                 if ( be->be_update_ndn == NULL ||
122                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
123                 {
124                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
125                                 global_lastmod == ON)) && be->be_update_ndn == NULL ) {
126
127                                 add_created_attrs( op, e );
128                         }
129                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
130                                 replog( be, LDAP_REQ_ADD, e->e_dn, e, 0 );
131                                 be_entry_release_w( be, e );
132                         }
133
134                 } else {
135                         entry_free( e );
136                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
137                             default_referral );
138                 }
139         } else {
140             Debug( LDAP_DEBUG_ARGS, "    do_add: HHH\n", 0, 0, 0 );
141                 entry_free( e );
142                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
143                     "Function not implemented" );
144         }
145 }
146
147 static void
148 add_created_attrs( Operation *op, Entry *e )
149 {
150         char            buf[22];
151         struct berval   bv;
152         struct berval   *bvals[2];
153         Attribute       **a, **next;
154         Attribute       *tmp;
155         struct tm       *ltm;
156         time_t          currenttime;
157
158         Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
159
160         bvals[0] = &bv;
161         bvals[1] = NULL;
162
163         /* remove any attempts by the user to add these attrs */
164         for ( a = &e->e_attrs; *a != NULL; a = next ) {
165                 if ( strcasecmp( (*a)->a_type, "modifiersname" ) == 0 || 
166                         strcasecmp( (*a)->a_type, "modifytimestamp" ) == 0 ||
167                         strcasecmp( (*a)->a_type, "creatorsname" ) == 0 ||
168                         strcasecmp( (*a)->a_type, "createtimestamp" ) == 0 ) {
169                         tmp = *a;
170                         *a = (*a)->a_next;
171                         attr_free( tmp );
172                         next = a;
173                 } else {
174                         next = &(*a)->a_next;
175                 }
176         }
177
178         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
179                 bv.bv_val = "NULLDN";
180                 bv.bv_len = strlen( bv.bv_val );
181         } else {
182                 bv.bv_val = op->o_dn;
183                 bv.bv_len = strlen( bv.bv_val );
184         }
185         attr_merge( e, "creatorsname", bvals );
186
187         currenttime = slap_get_time();
188         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
189 #ifndef LDAP_LOCALTIME
190         ltm = gmtime( &currenttime );
191         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
192 #else
193         ltm = localtime( &currenttime );
194         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
195 #endif
196         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
197
198         bv.bv_val = buf;
199         bv.bv_len = strlen( bv.bv_val );
200         attr_merge( e, "createtimestamp", bvals );
201 }