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