Line data Source code
1 : /*
2 : * Map names to server_ids
3 : *
4 : * Copyright Volker Lendecke <vl@samba.org> 2014
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include "replace.h"
21 : #include "system/filesys.h"
22 : #include "lib/util/server_id.h"
23 : #include "lib/util/server_id_db.h"
24 : #include "lib/tdb_wrap/tdb_wrap.h"
25 : #include "lib/util/strv.h"
26 : #include "lib/util/util_tdb.h"
27 : #include "lib/util/samba_util.h"
28 :
29 30755 : static TDB_DATA talloc_tdb_data(void *ptr)
30 : {
31 30755 : return (TDB_DATA) { .dptr = ptr, .dsize = talloc_get_size(ptr) };
32 : }
33 :
34 : struct server_id_db {
35 : struct server_id pid;
36 : struct tdb_wrap *tdb;
37 : char *names;
38 : };
39 :
40 : static int server_id_db_destructor(struct server_id_db *db);
41 :
42 418534 : struct server_id_db *server_id_db_init(TALLOC_CTX *mem_ctx,
43 : struct server_id pid,
44 : const char *base_path,
45 : int hash_size, int tdb_flags)
46 418534 : {
47 17401 : struct server_id_db *db;
48 418534 : size_t pathlen = strlen(base_path) + 11;
49 418534 : char path[pathlen];
50 :
51 418534 : db = talloc(mem_ctx, struct server_id_db);
52 418534 : if (db == NULL) {
53 0 : return NULL;
54 : }
55 418534 : db->pid = pid;
56 418534 : db->names = NULL;
57 :
58 418534 : snprintf(path, pathlen, "%s/names.tdb", base_path);
59 :
60 418534 : db->tdb = tdb_wrap_open(db, path, hash_size, tdb_flags,
61 : O_RDWR|O_CREAT, 0660);
62 418534 : if (db->tdb == NULL) {
63 0 : TALLOC_FREE(db);
64 0 : return NULL;
65 : }
66 :
67 418534 : talloc_set_destructor(db, server_id_db_destructor);
68 :
69 418534 : return db;
70 : }
71 :
72 90590 : void server_id_db_reinit(struct server_id_db *db, struct server_id pid)
73 : {
74 90590 : db->pid = pid;
75 90590 : TALLOC_FREE(db->names);
76 90590 : }
77 :
78 0 : struct server_id server_id_db_pid(struct server_id_db *db)
79 : {
80 0 : return db->pid;
81 : }
82 :
83 493993 : static int server_id_db_destructor(struct server_id_db *db)
84 : {
85 493993 : char *name = NULL;
86 :
87 525847 : while ((name = strv_next(db->names, name)) != NULL) {
88 31854 : server_id_db_remove(db, name);
89 : }
90 :
91 493993 : return 0;
92 : }
93 :
94 32139 : int server_id_db_add(struct server_id_db *db, const char *name)
95 : {
96 32139 : struct tdb_context *tdb = db->tdb->tdb;
97 198 : TDB_DATA key;
98 198 : char *n;
99 198 : int ret;
100 :
101 32139 : n = strv_find(db->names, name);
102 32139 : if (n != NULL) {
103 0 : return EEXIST;
104 : }
105 :
106 32139 : ret = strv_add(db, &db->names, name);
107 32139 : if (ret != 0) {
108 0 : return ret;
109 : }
110 :
111 32139 : key = string_term_tdb_data(name);
112 :
113 32139 : {
114 32139 : size_t idlen = server_id_str_buf_unique(db->pid, NULL, 0);
115 32139 : char idbuf[idlen];
116 :
117 32139 : server_id_str_buf_unique(db->pid, idbuf, idlen);
118 :
119 32337 : ret = tdb_append(
120 : tdb, key,
121 32139 : (TDB_DATA) { .dptr = (uint8_t *)idbuf, .dsize = idlen });
122 : }
123 :
124 32139 : if (ret != 0) {
125 0 : enum TDB_ERROR err = tdb_error(tdb);
126 0 : strv_delete(&db->names, strv_find(db->names, name));
127 0 : return map_unix_error_from_tdb(err);
128 : }
129 :
130 31941 : return 0;
131 : }
132 :
133 32863 : int server_id_db_prune_name(struct server_id_db *db, const char *name,
134 : struct server_id server)
135 32863 : {
136 32863 : struct tdb_context *tdb = db->tdb->tdb;
137 32863 : size_t idbuf_len = server_id_str_buf_unique(server, NULL, 0);
138 32863 : char idbuf[idbuf_len];
139 262 : TDB_DATA key;
140 262 : uint8_t *data;
141 262 : size_t datalen;
142 262 : char *ids, *id;
143 262 : int ret;
144 :
145 32863 : key = string_term_tdb_data(name);
146 32863 : server_id_str_buf_unique(server, idbuf, idbuf_len);
147 :
148 32863 : ret = tdb_chainlock(tdb, key);
149 32863 : if (ret == -1) {
150 0 : enum TDB_ERROR err = tdb_error(tdb);
151 0 : return map_unix_error_from_tdb(err);
152 : }
153 :
154 32863 : ret = tdb_fetch_talloc(tdb, key, db, &data);
155 32863 : if (ret != 0) {
156 701 : tdb_chainunlock(tdb, key);
157 701 : return ret;
158 : }
159 :
160 32162 : datalen = talloc_get_size(data);
161 32162 : if ((datalen == 0) || (data[datalen-1] != '\0')) {
162 0 : tdb_chainunlock(tdb, key);
163 0 : TALLOC_FREE(data);
164 0 : return EINVAL;
165 : }
166 :
167 32162 : ids = (char *)data;
168 :
169 32162 : id = strv_find(ids, idbuf);
170 32162 : if (id == NULL) {
171 103 : tdb_chainunlock(tdb, key);
172 103 : TALLOC_FREE(data);
173 103 : return ENOENT;
174 : }
175 :
176 32059 : strv_delete(&ids, id);
177 :
178 32059 : if (talloc_get_size(ids) == 0) {
179 1304 : ret = tdb_delete(tdb, key);
180 : } else {
181 30755 : ret = tdb_store(tdb, key, talloc_tdb_data(ids), TDB_MODIFY);
182 : }
183 32059 : TALLOC_FREE(data);
184 :
185 32059 : tdb_chainunlock(tdb, key);
186 :
187 32059 : if (ret == -1) {
188 0 : enum TDB_ERROR err = tdb_error(tdb);
189 0 : return map_unix_error_from_tdb(err);
190 : }
191 :
192 31861 : return 0;
193 : }
194 :
195 31912 : int server_id_db_remove(struct server_id_db *db, const char *name)
196 : {
197 188 : char *n;
198 188 : int ret;
199 :
200 31912 : n = strv_find(db->names, name);
201 31912 : if (n == NULL) {
202 0 : return ENOENT;
203 : }
204 :
205 31912 : ret = server_id_db_prune_name(db, name, db->pid);
206 31912 : if (ret != 0) {
207 740 : return ret;
208 : }
209 :
210 31108 : strv_delete(&db->names, n);
211 31108 : return 0;
212 : }
213 :
214 757741 : int server_id_db_lookup(struct server_id_db *db, const char *name,
215 : TALLOC_CTX *mem_ctx, unsigned *pnum_servers,
216 : struct server_id **pservers)
217 : {
218 757741 : struct tdb_context *tdb = db->tdb->tdb;
219 18708 : TDB_DATA key;
220 18708 : uint8_t *data;
221 18708 : size_t datalen;
222 18708 : char *ids, *id;
223 18708 : unsigned num_servers;
224 18708 : struct server_id *servers;
225 18708 : int i, ret;
226 :
227 757741 : key = string_term_tdb_data(name);
228 :
229 757741 : ret = tdb_fetch_talloc(tdb, key, mem_ctx, &data);
230 757741 : if (ret != 0) {
231 706272 : return ret;
232 : }
233 :
234 33137 : datalen = talloc_get_size(data);
235 33137 : if ((datalen == 0) || (data[datalen-1] != '\0')) {
236 0 : TALLOC_FREE(data);
237 0 : return EINVAL;
238 : }
239 :
240 33137 : ids = (char *)data;
241 33137 : num_servers = strv_count(ids);
242 :
243 33137 : servers = talloc_array(mem_ctx, struct server_id, num_servers);
244 33137 : if (servers == NULL) {
245 0 : TALLOC_FREE(data);
246 0 : return ENOMEM;
247 : }
248 :
249 32761 : i = 0;
250 :
251 66364 : for (id = ids; id != NULL; id = strv_next(ids, id)) {
252 33227 : servers[i++] = server_id_from_string(NONCLUSTER_VNN, id);
253 : }
254 :
255 33137 : TALLOC_FREE(data);
256 :
257 33137 : *pnum_servers = num_servers;
258 33137 : *pservers = servers;
259 :
260 33137 : return 0;
261 : }
262 :
263 21099 : bool server_id_db_lookup_one(struct server_id_db *db, const char *name,
264 : struct server_id *server)
265 : {
266 354 : int ret;
267 354 : unsigned num_servers;
268 354 : struct server_id *servers;
269 :
270 21099 : ret = server_id_db_lookup(db, name, db, &num_servers, &servers);
271 21099 : if (ret != 0) {
272 2 : return false;
273 : }
274 21097 : if (num_servers == 0) {
275 0 : TALLOC_FREE(servers);
276 0 : return false;
277 : }
278 21097 : *server = servers[0];
279 21097 : TALLOC_FREE(servers);
280 21097 : return true;
281 : }
282 :
283 : struct server_id_db_traverse_state {
284 : TALLOC_CTX *mem_ctx;
285 : int (*fn)(const char *name,
286 : unsigned num_servers,
287 : const struct server_id *servers,
288 : void *private_data);
289 : void *private_data;
290 : };
291 :
292 22348 : static int server_id_db_traverse_fn(struct tdb_context *tdb,
293 : TDB_DATA key, TDB_DATA data,
294 : void *private_data)
295 : {
296 22348 : struct server_id_db_traverse_state *state = private_data;
297 1155 : const char *name;
298 1155 : char *ids, *id;
299 1155 : unsigned num_servers;
300 1155 : struct server_id *servers;
301 1155 : int i, ret;
302 :
303 22348 : if (key.dsize == 0) {
304 0 : return 0;
305 : }
306 22348 : if (key.dptr[key.dsize-1] != '\0') {
307 0 : return 0;
308 : }
309 22348 : name = (const char *)key.dptr;
310 :
311 22348 : ids = (char *)talloc_memdup(state->mem_ctx, data.dptr, data.dsize);
312 22348 : if (ids == NULL) {
313 0 : return 0;
314 : }
315 :
316 22348 : num_servers = strv_count(ids);
317 22348 : servers = talloc_array(ids, struct server_id, num_servers);
318 :
319 22348 : i = 0;
320 :
321 50069 : for (id = ids; id != NULL; id = strv_next(ids, id)) {
322 26566 : servers[i++] = server_id_from_string(NONCLUSTER_VNN, id);
323 : }
324 :
325 22348 : ret = state->fn(name, num_servers, servers, state->private_data);
326 :
327 22348 : TALLOC_FREE(ids);
328 :
329 22348 : return ret;
330 : }
331 :
332 1031 : int server_id_db_traverse_read(struct server_id_db *db,
333 : int (*fn)(const char *name,
334 : unsigned num_servers,
335 : const struct server_id *servers,
336 : void *private_data),
337 : void *private_data)
338 : {
339 60 : struct server_id_db_traverse_state state;
340 60 : int ret;
341 :
342 1091 : state = (struct server_id_db_traverse_state) {
343 : .fn = fn, .private_data = private_data,
344 1031 : .mem_ctx = talloc_new(db)
345 : };
346 :
347 1031 : if (state.mem_ctx == NULL) {
348 0 : return ENOMEM;
349 : }
350 :
351 1031 : ret = tdb_traverse_read(db->tdb->tdb, server_id_db_traverse_fn,
352 : &state);
353 1031 : TALLOC_FREE(state.mem_ctx);
354 971 : return ret;
355 : }
|