1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Database interface wrapper
|
---|
4 | Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
|
---|
5 |
|
---|
6 | Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
|
---|
7 |
|
---|
8 | This program is free software; you can redistribute it and/or modify
|
---|
9 | it under the terms of the GNU General Public License as published by
|
---|
10 | the Free Software Foundation; either version 3 of the License, or
|
---|
11 | (at your option) any later version.
|
---|
12 |
|
---|
13 | This program is distributed in the hope that it will be useful,
|
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | GNU General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License
|
---|
19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "includes.h"
|
---|
23 | #ifdef CLUSTER_SUPPORT
|
---|
24 | #include "ctdb_private.h"
|
---|
25 | #endif
|
---|
26 | /*
|
---|
27 | * Fall back using fetch_locked if no genuine fetch operation is provided
|
---|
28 | */
|
---|
29 |
|
---|
30 | static int dbwrap_fallback_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
|
---|
31 | TDB_DATA key, TDB_DATA *data)
|
---|
32 | {
|
---|
33 | struct db_record *rec;
|
---|
34 |
|
---|
35 | if (!(rec = db->fetch_locked(db, mem_ctx, key))) {
|
---|
36 | return -1;
|
---|
37 | }
|
---|
38 |
|
---|
39 | data->dsize = rec->value.dsize;
|
---|
40 | data->dptr = talloc_move(mem_ctx, &rec->value.dptr);
|
---|
41 | TALLOC_FREE(rec);
|
---|
42 | return 0;
|
---|
43 | }
|
---|
44 |
|
---|
45 | bool db_is_local(const char *name)
|
---|
46 | {
|
---|
47 | #ifdef CLUSTER_SUPPORT
|
---|
48 | const char *sockname = lp_ctdbd_socket();
|
---|
49 |
|
---|
50 | if(!sockname || !*sockname) {
|
---|
51 | sockname = CTDB_PATH;
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (lp_clustering() && socket_exist(sockname)) {
|
---|
55 | const char *partname;
|
---|
56 | /* ctdb only wants the file part of the name */
|
---|
57 | partname = strrchr(name, '/');
|
---|
58 | if (partname) {
|
---|
59 | partname++;
|
---|
60 | } else {
|
---|
61 | partname = name;
|
---|
62 | }
|
---|
63 | /* allow ctdb for individual databases to be disabled */
|
---|
64 | if (lp_parm_bool(-1, "ctdb", partname, True)) {
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | #endif
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * If you need transaction support use db_open_trans()
|
---|
74 | */
|
---|
75 | struct db_context *db_open(TALLOC_CTX *mem_ctx,
|
---|
76 | const char *name,
|
---|
77 | int hash_size, int tdb_flags,
|
---|
78 | int open_flags, mode_t mode)
|
---|
79 | {
|
---|
80 | struct db_context *result = NULL;
|
---|
81 | #ifdef CLUSTER_SUPPORT
|
---|
82 | const char *sockname = lp_ctdbd_socket();
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | #ifdef CLUSTER_SUPPORT
|
---|
86 | if(!sockname || !*sockname) {
|
---|
87 | sockname = CTDB_PATH;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (lp_clustering() && socket_exist(sockname)) {
|
---|
91 | const char *partname;
|
---|
92 | /* ctdb only wants the file part of the name */
|
---|
93 | partname = strrchr(name, '/');
|
---|
94 | if (partname) {
|
---|
95 | partname++;
|
---|
96 | } else {
|
---|
97 | partname = name;
|
---|
98 | }
|
---|
99 | /* allow ctdb for individual databases to be disabled */
|
---|
100 | if (lp_parm_bool(-1, "ctdb", partname, True)) {
|
---|
101 | result = db_open_ctdb(mem_ctx, partname, hash_size,
|
---|
102 | tdb_flags, open_flags, mode);
|
---|
103 | if (result == NULL) {
|
---|
104 | DEBUG(0,("failed to attach to ctdb %s\n",
|
---|
105 | partname));
|
---|
106 | smb_panic("failed to attach to a ctdb "
|
---|
107 | "database");
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | if (result == NULL) {
|
---|
115 | result = db_open_tdb(mem_ctx, name, hash_size,
|
---|
116 | tdb_flags, open_flags, mode);
|
---|
117 | }
|
---|
118 |
|
---|
119 | if ((result != NULL) && (result->fetch == NULL)) {
|
---|
120 | result->fetch = dbwrap_fallback_fetch;
|
---|
121 | }
|
---|
122 |
|
---|
123 | return result;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * If you use this you can only modify with a transaction
|
---|
128 | */
|
---|
129 | struct db_context *db_open_trans(TALLOC_CTX *mem_ctx,
|
---|
130 | const char *name,
|
---|
131 | int hash_size, int tdb_flags,
|
---|
132 | int open_flags, mode_t mode)
|
---|
133 | {
|
---|
134 | bool use_tdb2 = lp_parm_bool(-1, "dbwrap", "use_tdb2", false);
|
---|
135 | #ifdef CLUSTER_SUPPORT
|
---|
136 | const char *sockname = lp_ctdbd_socket();
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | if (tdb_flags & TDB_CLEAR_IF_FIRST) {
|
---|
140 | DEBUG(0,("db_open_trans: called with TDB_CLEAR_IF_FIRST: %s\n",
|
---|
141 | name));
|
---|
142 | smb_panic("db_open_trans: called with TDB_CLEAR_IF_FIRST");
|
---|
143 | }
|
---|
144 |
|
---|
145 | #ifdef CLUSTER_SUPPORT
|
---|
146 | if(!sockname || !*sockname) {
|
---|
147 | sockname = CTDB_PATH;
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (lp_clustering() && socket_exist(sockname)) {
|
---|
151 | const char *partname;
|
---|
152 | /* ctdb only wants the file part of the name */
|
---|
153 | partname = strrchr(name, '/');
|
---|
154 | if (partname) {
|
---|
155 | partname++;
|
---|
156 | } else {
|
---|
157 | partname = name;
|
---|
158 | }
|
---|
159 | /* allow ctdb for individual databases to be disabled */
|
---|
160 | if (lp_parm_bool(-1, "ctdb", partname, true)) {
|
---|
161 | struct db_context *result = NULL;
|
---|
162 | result = db_open_ctdb(mem_ctx, partname, hash_size,
|
---|
163 | tdb_flags, open_flags, mode);
|
---|
164 | if (result == NULL) {
|
---|
165 | DEBUG(0,("failed to attach to ctdb %s\n",
|
---|
166 | partname));
|
---|
167 | smb_panic("failed to attach to a ctdb "
|
---|
168 | "database");
|
---|
169 | }
|
---|
170 | return result;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | if (use_tdb2) {
|
---|
176 | const char *partname;
|
---|
177 | /* tdb2 only wants the file part of the name */
|
---|
178 | partname = strrchr(name, '/');
|
---|
179 | if (partname) {
|
---|
180 | partname++;
|
---|
181 | } else {
|
---|
182 | partname = name;
|
---|
183 | }
|
---|
184 | /* allow ctdb for individual databases to be disabled */
|
---|
185 | if (lp_parm_bool(-1, "tdb2", partname, true)) {
|
---|
186 | return db_open_tdb2(mem_ctx, partname, hash_size,
|
---|
187 | tdb_flags, open_flags, mode);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | return db_open_tdb(mem_ctx, name, hash_size,
|
---|
192 | tdb_flags, open_flags, mode);
|
---|
193 | }
|
---|
194 |
|
---|
195 | NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
|
---|
196 | {
|
---|
197 | struct db_record *rec;
|
---|
198 | NTSTATUS status;
|
---|
199 |
|
---|
200 | rec = db->fetch_locked(db, talloc_tos(), string_term_tdb_data(key));
|
---|
201 | if (rec == NULL) {
|
---|
202 | return NT_STATUS_NO_MEMORY;
|
---|
203 | }
|
---|
204 | status = rec->delete_rec(rec);
|
---|
205 | TALLOC_FREE(rec);
|
---|
206 | return status;
|
---|
207 | }
|
---|
208 |
|
---|
209 | NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
|
---|
210 | TDB_DATA data, int flags)
|
---|
211 | {
|
---|
212 | struct db_record *rec;
|
---|
213 | NTSTATUS status;
|
---|
214 |
|
---|
215 | rec = db->fetch_locked(db, talloc_tos(), string_term_tdb_data(key));
|
---|
216 | if (rec == NULL) {
|
---|
217 | return NT_STATUS_NO_MEMORY;
|
---|
218 | }
|
---|
219 |
|
---|
220 | status = rec->store(rec, data, flags);
|
---|
221 | TALLOC_FREE(rec);
|
---|
222 | return status;
|
---|
223 | }
|
---|
224 |
|
---|
225 | TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
|
---|
226 | const char *key)
|
---|
227 | {
|
---|
228 | TDB_DATA result;
|
---|
229 |
|
---|
230 | if (db->fetch(db, mem_ctx, string_term_tdb_data(key), &result) == -1) {
|
---|
231 | return make_tdb_data(NULL, 0);
|
---|
232 | }
|
---|
233 |
|
---|
234 | return result;
|
---|
235 | }
|
---|