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