tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Copyright (C) 2005 Red Hat, Inc. */
 
#include <semanage/handle.h>
#include "semanage_store.h"
#include "semanage_conf.h"
#include "database.h"
#include "debug.h"
 
static int assert_init(semanage_handle_t * handle, dbase_config_t * dconfig)
{
 
   if (dconfig->dtable == NULL) {
 
       ERR(handle,
           "A direct or server connection is needed "
           "to use this function - please call "
           "the corresponding connect() method");
       return STATUS_ERR;
   }
 
   return STATUS_SUCCESS;
}
 
static int enter_ro(semanage_handle_t * handle, dbase_config_t * dconfig)
{
 
   if (assert_init(handle, dconfig) < 0)
       goto err;
 
   if (!handle->is_in_transaction &&
       handle->conf->store_type == SEMANAGE_CON_DIRECT) {
 
       if (semanage_get_active_lock(handle) < 0) {
           ERR(handle, "could not get the active lock");
           goto err;
       }
   }
 
   if (dconfig->dtable->cache(handle, dconfig->dbase) < 0)
       goto err;
 
   return STATUS_SUCCESS;
 
      err:
   ERR(handle, "could not enter read-only section");
   return STATUS_ERR;
}
 
static inline int exit_ro(semanage_handle_t * handle)
{
 
   int commit_num = handle->funcs->get_serial(handle);
 
   if (!handle->is_in_transaction &&
       handle->conf->store_type == SEMANAGE_CON_DIRECT)
       semanage_release_active_lock(handle);
 
   return commit_num;
}
 
static int enter_rw(semanage_handle_t * handle, dbase_config_t * dconfig)
{
 
   if (assert_init(handle, dconfig) < 0)
       goto err;
 
   if (!handle->is_in_transaction) {
       ERR(handle, "this operation requires a transaction");
       goto err;
   }
 
   if (dconfig->dtable->cache(handle, dconfig->dbase) < 0)
       goto err;
 
   return STATUS_SUCCESS;
 
      err:
   ERR(handle, "could not enter read-write section");
   return STATUS_ERR;
}
 
int dbase_modify(semanage_handle_t * handle,
        dbase_config_t * dconfig,
        const record_key_t * key, const record_t * data)
{
 
   if (enter_rw(handle, dconfig) < 0)
       return STATUS_ERR;
 
   if (dconfig->dtable->modify(handle, dconfig->dbase, key, data) < 0)
       return STATUS_ERR;
 
   return STATUS_SUCCESS;
}
 
int dbase_set(semanage_handle_t * handle,
         dbase_config_t * dconfig,
         const record_key_t * key, const record_t * data)
{
 
   if (enter_rw(handle, dconfig) < 0)
       return STATUS_ERR;
 
   if (dconfig->dtable->set(handle, dconfig->dbase, key, data) < 0)
       return STATUS_ERR;
 
   return STATUS_SUCCESS;
}
 
int dbase_del(semanage_handle_t * handle,
         dbase_config_t * dconfig, const record_key_t * key)
{
 
   if (enter_rw(handle, dconfig) < 0)
       return STATUS_ERR;
 
   if (dconfig->dtable->del(handle, dconfig->dbase, key) < 0)
       return STATUS_ERR;
 
   return STATUS_SUCCESS;
}
 
int dbase_query(semanage_handle_t * handle,
       dbase_config_t * dconfig,
       const record_key_t * key, record_t ** response)
{
 
   if (enter_ro(handle, dconfig) < 0)
       return STATUS_ERR;
 
   if (dconfig->dtable->query(handle, dconfig->dbase, key, response) < 0) {
       exit_ro(handle);
       return STATUS_ERR;
   }
 
   return exit_ro(handle);
}
 
int dbase_exists(semanage_handle_t * handle,
        dbase_config_t * dconfig,
        const record_key_t * key, int *response)
{
 
   if (enter_ro(handle, dconfig) < 0)
       return STATUS_ERR;
 
   if (dconfig->dtable->exists(handle, dconfig->dbase, key, response) < 0) {
       exit_ro(handle);
       return STATUS_ERR;
   }
 
   return exit_ro(handle);
}
 
int dbase_count(semanage_handle_t * handle,
       dbase_config_t * dconfig, unsigned int *response)
{
 
   if (enter_ro(handle, dconfig) < 0)
       return STATUS_ERR;
 
   if (dconfig->dtable->count(handle, dconfig->dbase, response) < 0) {
       exit_ro(handle);
       return STATUS_ERR;
   }
 
   return exit_ro(handle);
}
 
int dbase_iterate(semanage_handle_t * handle,
         dbase_config_t * dconfig,
         int (*fn) (const record_t * record,
                void *fn_arg), void *fn_arg)
{
 
   if (enter_ro(handle, dconfig) < 0)
       return STATUS_ERR;
 
   if (dconfig->dtable->iterate(handle, dconfig->dbase, fn, fn_arg) < 0) {
       exit_ro(handle);
       return STATUS_ERR;
   }
 
   return exit_ro(handle);
}
 
int dbase_list(semanage_handle_t * handle,
          dbase_config_t * dconfig,
          record_t *** records, unsigned int *count)
{
 
   if (enter_ro(handle, dconfig) < 0)
       return STATUS_ERR;
 
   if (dconfig->dtable->list(handle, dconfig->dbase, records, count) < 0) {
       exit_ro(handle);
       return STATUS_ERR;
   }
 
   return exit_ro(handle);
}