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