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 | #include "dbwrap.h"
|
---|
24 | #include "util_tdb.h"
|
---|
25 | #ifdef CLUSTER_SUPPORT
|
---|
26 | #include "ctdb_private.h"
|
---|
27 | #endif
|
---|
28 | /*
|
---|
29 | * Fall back using fetch_locked if no genuine fetch operation is provided
|
---|
30 | */
|
---|
31 |
|
---|
32 | static int dbwrap_fallback_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
|
---|
33 | TDB_DATA key, TDB_DATA *data)
|
---|
34 | {
|
---|
35 | struct db_record *rec;
|
---|
36 |
|
---|
37 | if (!(rec = db->fetch_locked(db, mem_ctx, key))) {
|
---|
38 | return -1;
|
---|
39 | }
|
---|
40 |
|
---|
41 | data->dsize = rec->value.dsize;
|
---|
42 | data->dptr = talloc_move(mem_ctx, &rec->value.dptr);
|
---|
43 | TALLOC_FREE(rec);
|
---|
44 | return 0;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * Fall back using fetch if no genuine parse operation is provided
|
---|
49 | */
|
---|
50 |
|
---|
51 | static int dbwrap_fallback_parse_record(struct db_context *db, TDB_DATA key,
|
---|
52 | int (*parser)(TDB_DATA key,
|
---|
53 | TDB_DATA data,
|
---|
54 | void *private_data),
|
---|
55 | void *private_data)
|
---|
56 | {
|
---|
57 | TDB_DATA data;
|
---|
58 | int res;
|
---|
59 |
|
---|
60 | res = db->fetch(db, talloc_tos(), key, &data);
|
---|
61 | if (res != 0) {
|
---|
62 | return res;
|
---|
63 | }
|
---|
64 |
|
---|
65 | res = parser(key, data, private_data);
|
---|
66 | TALLOC_FREE(data.dptr);
|
---|
67 | return res;
|
---|
68 | }
|
---|
69 |
|
---|
70 | bool db_is_local(const char *name)
|
---|
71 | {
|
---|
72 | #ifdef CLUSTER_SUPPORT
|
---|
73 | const char *sockname = lp_ctdbd_socket();
|
---|
74 |
|
---|
75 | if(!sockname || !*sockname) {
|
---|
76 | sockname = CTDB_PATH;
|
---|
77 | }
|
---|
78 |
|
---|
79 | if (lp_clustering() && socket_exist(sockname)) {
|
---|
80 | const char *partname;
|
---|
81 | /* ctdb only wants the file part of the name */
|
---|
82 | partname = strrchr(name, '/');
|
---|
83 | if (partname) {
|
---|
84 | partname++;
|
---|
85 | } else {
|
---|
86 | partname = name;
|
---|
87 | }
|
---|
88 | /* allow ctdb for individual databases to be disabled */
|
---|
89 | if (lp_parm_bool(-1, "ctdb", partname, True)) {
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | #endif
|
---|
94 | return true;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * open a database
|
---|
99 | */
|
---|
100 | struct db_context *db_open(TALLOC_CTX *mem_ctx,
|
---|
101 | const char *name,
|
---|
102 | int hash_size, int tdb_flags,
|
---|
103 | int open_flags, mode_t mode)
|
---|
104 | {
|
---|
105 | struct db_context *result = NULL;
|
---|
106 | #ifdef CLUSTER_SUPPORT
|
---|
107 | const char *sockname = lp_ctdbd_socket();
|
---|
108 |
|
---|
109 | if(!sockname || !*sockname) {
|
---|
110 | sockname = CTDB_PATH;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (lp_clustering()) {
|
---|
114 | const char *partname;
|
---|
115 |
|
---|
116 | if (!socket_exist(sockname)) {
|
---|
117 | DEBUG(1, ("ctdb socket does not exist - is ctdb not "
|
---|
118 | "running?\n"));
|
---|
119 | return NULL;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /* ctdb only wants the file part of the name */
|
---|
123 | partname = strrchr(name, '/');
|
---|
124 | if (partname) {
|
---|
125 | partname++;
|
---|
126 | } else {
|
---|
127 | partname = name;
|
---|
128 | }
|
---|
129 | /* allow ctdb for individual databases to be disabled */
|
---|
130 | if (lp_parm_bool(-1, "ctdb", partname, True)) {
|
---|
131 | result = db_open_ctdb(mem_ctx, partname, hash_size,
|
---|
132 | tdb_flags, open_flags, mode);
|
---|
133 | if (result == NULL) {
|
---|
134 | DEBUG(0,("failed to attach to ctdb %s\n",
|
---|
135 | partname));
|
---|
136 | if (errno == 0) {
|
---|
137 | errno = EIO;
|
---|
138 | }
|
---|
139 | return NULL;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | if (result == NULL) {
|
---|
147 | result = db_open_tdb(mem_ctx, name, hash_size,
|
---|
148 | tdb_flags, open_flags, mode);
|
---|
149 | }
|
---|
150 |
|
---|
151 | if ((result != NULL) && (result->fetch == NULL)) {
|
---|
152 | result->fetch = dbwrap_fallback_fetch;
|
---|
153 | }
|
---|
154 | if ((result != NULL) && (result->parse_record == NULL)) {
|
---|
155 | result->parse_record = dbwrap_fallback_parse_record;
|
---|
156 | }
|
---|
157 |
|
---|
158 | return result;
|
---|
159 | }
|
---|
160 |
|
---|
161 | NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key)
|
---|
162 | {
|
---|
163 | struct db_record *rec;
|
---|
164 | NTSTATUS status;
|
---|
165 |
|
---|
166 | rec = db->fetch_locked(db, talloc_tos(), key);
|
---|
167 | if (rec == NULL) {
|
---|
168 | return NT_STATUS_NO_MEMORY;
|
---|
169 | }
|
---|
170 | status = rec->delete_rec(rec);
|
---|
171 | TALLOC_FREE(rec);
|
---|
172 | return status;
|
---|
173 | }
|
---|
174 |
|
---|
175 | NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key,
|
---|
176 | TDB_DATA data, int flags)
|
---|
177 | {
|
---|
178 | struct db_record *rec;
|
---|
179 | NTSTATUS status;
|
---|
180 |
|
---|
181 | rec = db->fetch_locked(db, talloc_tos(), key);
|
---|
182 | if (rec == NULL) {
|
---|
183 | return NT_STATUS_NO_MEMORY;
|
---|
184 | }
|
---|
185 |
|
---|
186 | status = rec->store(rec, data, flags);
|
---|
187 | TALLOC_FREE(rec);
|
---|
188 | return status;
|
---|
189 | }
|
---|
190 |
|
---|
191 | TDB_DATA dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
|
---|
192 | TDB_DATA key)
|
---|
193 | {
|
---|
194 | TDB_DATA result;
|
---|
195 |
|
---|
196 | if (db->fetch(db, mem_ctx, key, &result) == -1) {
|
---|
197 | return make_tdb_data(NULL, 0);
|
---|
198 | }
|
---|
199 |
|
---|
200 | return result;
|
---|
201 | }
|
---|
202 |
|
---|
203 | NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
|
---|
204 | {
|
---|
205 | return dbwrap_delete(db, string_term_tdb_data(key));
|
---|
206 | }
|
---|
207 |
|
---|
208 | NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
|
---|
209 | TDB_DATA data, int flags)
|
---|
210 | {
|
---|
211 | return dbwrap_store(db, string_term_tdb_data(key), data, flags);
|
---|
212 | }
|
---|
213 |
|
---|
214 | TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
|
---|
215 | const char *key)
|
---|
216 | {
|
---|
217 | return dbwrap_fetch(db, mem_ctx, string_term_tdb_data(key));
|
---|
218 | }
|
---|
219 |
|
---|