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