]> git.sur5r.net Git - openldap/blob - servers/slapd/sessionlog.c
1. Session history support
[openldap] / servers / slapd / sessionlog.c
1 /* $OpenLDAP$ */
2 /*
3  * Session History Management Routines
4  */
5 /*
6  * Copyright 2003 The OpenLDAP Foundation, All Rights Reserved.
7  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8  */
9 /* Copyright (c) 2003 by International Business Machines, Inc.
10  *
11  * International Business Machines, Inc. (hereinafter called IBM) grants
12  * permission under its copyrights to use, copy, modify, and distribute this
13  * Software with or without fee, provided that the above copyright notice and
14  * all paragraphs of this notice appear in all copies, and that the name of IBM
15  * not be used in connection with the marketing of any product incorporating
16  * the Software or modifications thereof, without specific, written prior
17  * permission.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
20  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
22  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
23  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
24  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/socket.h>
33
34 #include "ldap_pvt.h"
35 #include "lutil.h"
36 #include "slap.h"
37 #include "lutil_ldap.h"
38
39 int
40 slap_send_session_log(
41         Operation *op,
42         Operation *sop,
43         SlapReply *rs
44 )
45 {
46         Entry e;
47         AttributeName   uuid_attr[2];
48         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
49         int             num_ctrls = 0;
50         struct slog_entry *slog_e;
51         int             result;
52         int             match;
53         const   char    *text;
54
55         uuid_attr[0].an_desc = NULL;
56         uuid_attr[0].an_oc = NULL;
57         uuid_attr[0].an_name.bv_len = 0;
58         uuid_attr[0].an_name.bv_val = NULL;
59         e.e_attrs = NULL;
60         e.e_id = 0;
61         e.e_name.bv_val = NULL;
62         e.e_name.bv_len = 0;
63         e.e_nname.bv_val = NULL;
64         e.e_nname.bv_len = 0;
65
66         for( num_ctrls = 0;
67                  num_ctrls < SLAP_MAX_RESPONSE_CONTROLS;
68                  num_ctrls++ ) {
69                 ctrls[num_ctrls] = NULL;
70         }
71         num_ctrls = 0;
72
73         LDAP_STAILQ_FOREACH( slog_e, &sop->o_sync_slog_list, sl_link ) {
74
75                 if ( op->o_sync_state.ctxcsn->bv_val == NULL ) {
76                         match = 1;
77                 } else {
78                         value_match( &match, slap_schema.si_ad_entryCSN,
79                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
80                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
81                                                 op->o_sync_state.ctxcsn, &slog_e->sl_csn, &text );
82                 }
83
84                 if ( match < 0 ) {
85                         rs->sr_err = slap_build_sync_state_ctrl_from_slog( op, rs, slog_e,
86                                                         LDAP_SYNC_DELETE, ctrls, num_ctrls++, 0, NULL );
87
88                         if ( rs->sr_err != LDAP_SUCCESS )
89                                 return rs->sr_err;
90
91                         if ( e.e_name.bv_val )
92                                 ch_free( e.e_name.bv_val );
93                         ber_dupbv( &e.e_name, &slog_e->sl_name );
94
95                         rs->sr_entry = &e;
96                         rs->sr_attrs = uuid_attr;
97                         rs->sr_ctrls = ctrls;
98                         result = send_search_entry( op, rs );
99                         ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
100                         ch_free( ctrls[--num_ctrls] );
101                         ctrls[num_ctrls] = NULL;
102                         rs->sr_ctrls = NULL;
103                 }
104         }
105 }
106
107 int
108 slap_add_session_log(
109         Operation *op,
110         Operation *sop,
111         Entry *e
112 )
113 {
114         struct slog_entry* slog_e;
115         Attribute *a;
116
117         slog_e = (struct slog_entry *) ch_calloc (1, sizeof( struct slog_entry ));
118         a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
119         ber_dupbv( &slog_e->sl_uuid, &a->a_vals[0] );
120         ber_dupbv( &slog_e->sl_name, &e->e_name );
121         ber_dupbv( &slog_e->sl_csn,  &op->o_sync_csn );
122         LDAP_STAILQ_INSERT_TAIL( &sop->o_sync_slog_list, slog_e, sl_link );
123         sop->o_sync_slog_len++;
124
125         while ( sop->o_sync_slog_len > sop->o_sync_slog_size ) {
126                 slog_e = LDAP_STAILQ_FIRST( &sop->o_sync_slog_list );
127                 if ( sop->o_sync_slog_omitcsn.bv_val ) {
128                         ch_free( sop->o_sync_slog_omitcsn.bv_val );
129                 }
130                 ber_dupbv( &sop->o_sync_slog_omitcsn, &slog_e->sl_csn );
131                 LDAP_STAILQ_REMOVE_HEAD( &sop->o_sync_slog_list, sl_link );
132                 ch_free( slog_e->sl_uuid.bv_val );
133                 ch_free( slog_e->sl_name.bv_val );
134                 ch_free( slog_e->sl_csn.bv_val );
135                 ch_free( slog_e );
136                 sop->o_sync_slog_len--;
137         }
138
139         return LDAP_SUCCESS;
140 }