]> git.sur5r.net Git - openldap/blob - servers/slapd/sessionlog.c
component cleanup
[openldap] / servers / slapd / sessionlog.c
1 /* sessionlog.c -- Session History Management Routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2003 IBM Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "ldap_pvt.h"
26 #include "lutil.h"
27 #include "slap.h"
28 #include "lutil_ldap.h"
29
30 int
31 slap_send_session_log(
32         Operation *op,
33         Operation *sop,
34         SlapReply *rs
35 )
36 {
37         Entry e;
38         AttributeName   uuid_attr[2];
39         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
40         int             num_ctrls = 0;
41         struct slog_entry *slog_e;
42         int             result;
43         int             match;
44         const   char    *text;
45
46         uuid_attr[0].an_desc = NULL;
47         uuid_attr[0].an_oc = NULL;
48         uuid_attr[0].an_oc_exclude = 0;
49         uuid_attr[0].an_name.bv_len = 0;
50         uuid_attr[0].an_name.bv_val = NULL;
51         e.e_attrs = NULL;
52         e.e_id = 0;
53         e.e_name.bv_val = NULL;
54         e.e_name.bv_len = 0;
55         e.e_nname.bv_val = NULL;
56         e.e_nname.bv_len = 0;
57
58         for( num_ctrls = 0;
59                  num_ctrls < SLAP_MAX_RESPONSE_CONTROLS;
60                  num_ctrls++ ) {
61                 ctrls[num_ctrls] = NULL;
62         }
63         num_ctrls = 0;
64
65         LDAP_STAILQ_FOREACH( slog_e, &sop->o_sync_slog_list, sl_link ) {
66
67                 if ( op->o_sync_state.ctxcsn->bv_val == NULL ) {
68                         match = 1;
69                 } else {
70                         value_match( &match, slap_schema.si_ad_entryCSN,
71                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
72                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
73                                                 op->o_sync_state.ctxcsn, &slog_e->sl_csn, &text );
74                 }
75
76                 if ( match < 0 ) {
77                         rs->sr_err = slap_build_sync_state_ctrl_from_slog( op, rs, slog_e,
78                                                         LDAP_SYNC_DELETE, ctrls, num_ctrls++, 0, NULL );
79
80                         if ( rs->sr_err != LDAP_SUCCESS )
81                                 return rs->sr_err;
82
83                         if ( e.e_name.bv_val )
84                                 ch_free( e.e_name.bv_val );
85                         ber_dupbv( &e.e_name, &slog_e->sl_name );
86
87                         rs->sr_entry = &e;
88                         rs->sr_attrs = uuid_attr;
89                         rs->sr_ctrls = ctrls;
90                         rs->sr_flags = 0;
91                         result = send_search_entry( op, rs );
92                         slap_sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val, op->o_tmpmemctx );
93                         slap_sl_free( ctrls[--num_ctrls], op->o_tmpmemctx );
94                         ctrls[num_ctrls] = NULL;
95                         rs->sr_ctrls = NULL;
96                 }
97         }
98         return LDAP_SUCCESS;
99 }
100
101 int
102 slap_add_session_log(
103         Operation *op,
104         Operation *sop,
105         Entry *e
106 )
107 {
108         struct slog_entry* slog_e;
109         Attribute *a;
110
111         slog_e = (struct slog_entry *) ch_calloc (1, sizeof( struct slog_entry ));
112         a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
113         ber_dupbv( &slog_e->sl_uuid, &a->a_nvals[0] );
114         ber_dupbv( &slog_e->sl_name, &e->e_name );
115         ber_dupbv( &slog_e->sl_csn,  &op->o_sync_csn );
116         LDAP_STAILQ_INSERT_TAIL( &sop->o_sync_slog_list, slog_e, sl_link );
117         sop->o_sync_slog_len++;
118
119         while ( sop->o_sync_slog_len > sop->o_sync_slog_size ) {
120                 slog_e = LDAP_STAILQ_FIRST( &sop->o_sync_slog_list );
121                 if ( sop->o_sync_slog_omitcsn.bv_val ) {
122                         ch_free( sop->o_sync_slog_omitcsn.bv_val );
123                 }
124                 ber_dupbv( &sop->o_sync_slog_omitcsn, &slog_e->sl_csn );
125                 LDAP_STAILQ_REMOVE_HEAD( &sop->o_sync_slog_list, sl_link );
126                 ch_free( slog_e->sl_uuid.bv_val );
127                 ch_free( slog_e->sl_name.bv_val );
128                 ch_free( slog_e->sl_csn.bv_val );
129                 ch_free( slog_e );
130                 sop->o_sync_slog_len--;
131         }
132
133         return LDAP_SUCCESS;
134 }