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