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