WHATSNEW: Start release notes for Samba 3.6.18.
[Samba.git] / source3 / lib / serverid.c
blob00dd6c4f8d208bad356171fbbff65a41bcc25bb4
1 /*
2 Unix SMB/CIFS implementation.
3 Implementation of a reliable server_exists()
4 Copyright (C) Volker Lendecke 2010
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.
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.
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "serverid.h"
23 #include "util_tdb.h"
24 #include "dbwrap.h"
25 #include "lib/util/tdb_wrap.h"
27 struct serverid_key {
28 pid_t pid;
29 uint32_t vnn;
32 struct serverid_data {
33 uint64_t unique_id;
34 uint32_t msg_flags;
37 bool serverid_parent_init(TALLOC_CTX *mem_ctx)
39 struct tdb_wrap *db;
42 * Open the tdb in the parent process (smbd) so that our
43 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
44 * work.
47 db = tdb_wrap_open(mem_ctx, lock_path("serverid.tdb"),
48 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDWR|O_CREAT,
49 0644);
50 if (db == NULL) {
51 DEBUG(1, ("could not open serverid.tdb: %s\n",
52 strerror(errno)));
53 return false;
55 return true;
58 static struct db_context *serverid_db(void)
60 static struct db_context *db;
62 if (db != NULL) {
63 return db;
65 db = db_open(NULL, lock_path("serverid.tdb"), 0,
66 TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDWR|O_CREAT, 0644);
67 return db;
70 static void serverid_fill_key(const struct server_id *id,
71 struct serverid_key *key)
73 ZERO_STRUCTP(key);
74 key->pid = id->pid;
75 key->vnn = id->vnn;
78 bool serverid_register(const struct server_id id, uint32_t msg_flags)
80 struct db_context *db;
81 struct serverid_key key;
82 struct serverid_data data;
83 struct db_record *rec;
84 TDB_DATA tdbkey, tdbdata;
85 NTSTATUS status;
86 bool ret = false;
88 db = serverid_db();
89 if (db == NULL) {
90 return false;
93 serverid_fill_key(&id, &key);
94 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
96 rec = db->fetch_locked(db, talloc_tos(), tdbkey);
97 if (rec == NULL) {
98 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
99 return false;
102 ZERO_STRUCT(data);
103 data.unique_id = id.unique_id;
104 data.msg_flags = msg_flags;
106 tdbdata = make_tdb_data((uint8_t *)&data, sizeof(data));
107 status = rec->store(rec, tdbdata, 0);
108 if (!NT_STATUS_IS_OK(status)) {
109 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
110 nt_errstr(status)));
111 goto done;
113 ret = true;
114 done:
115 TALLOC_FREE(rec);
116 return ret;
119 bool serverid_register_msg_flags(const struct server_id id, bool do_reg,
120 uint32_t msg_flags)
122 struct db_context *db;
123 struct serverid_key key;
124 struct serverid_data *data;
125 struct db_record *rec;
126 TDB_DATA tdbkey;
127 NTSTATUS status;
128 bool ret = false;
130 db = serverid_db();
131 if (db == NULL) {
132 return false;
135 serverid_fill_key(&id, &key);
136 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
138 rec = db->fetch_locked(db, talloc_tos(), tdbkey);
139 if (rec == NULL) {
140 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
141 return false;
144 if (rec->value.dsize != sizeof(struct serverid_data)) {
145 DEBUG(1, ("serverid record has unexpected size %d "
146 "(wanted %d)\n", (int)rec->value.dsize,
147 (int)sizeof(struct serverid_data)));
148 goto done;
151 data = (struct serverid_data *)rec->value.dptr;
153 if (do_reg) {
154 data->msg_flags |= msg_flags;
155 } else {
156 data->msg_flags &= ~msg_flags;
159 status = rec->store(rec, rec->value, 0);
160 if (!NT_STATUS_IS_OK(status)) {
161 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
162 nt_errstr(status)));
163 goto done;
165 ret = true;
166 done:
167 TALLOC_FREE(rec);
168 return ret;
171 bool serverid_deregister(struct server_id id)
173 struct db_context *db;
174 struct serverid_key key;
175 struct db_record *rec;
176 TDB_DATA tdbkey;
177 NTSTATUS status;
178 bool ret = false;
180 db = serverid_db();
181 if (db == NULL) {
182 return false;
185 serverid_fill_key(&id, &key);
186 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
188 rec = db->fetch_locked(db, talloc_tos(), tdbkey);
189 if (rec == NULL) {
190 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
191 return false;
194 status = rec->delete_rec(rec);
195 if (!NT_STATUS_IS_OK(status)) {
196 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
197 nt_errstr(status)));
198 goto done;
200 ret = true;
201 done:
202 TALLOC_FREE(rec);
203 return ret;
206 struct serverid_exists_state {
207 const struct server_id *id;
208 bool exists;
211 static int server_exists_parse(TDB_DATA key, TDB_DATA data, void *priv)
213 struct serverid_exists_state *state =
214 (struct serverid_exists_state *)priv;
216 if (data.dsize != sizeof(struct serverid_data)) {
217 return -1;
221 * Use memcmp, not direct compare. data.dptr might not be
222 * aligned.
224 state->exists = (memcmp(&state->id->unique_id, data.dptr,
225 sizeof(state->id->unique_id)) == 0);
226 return 0;
229 bool serverid_exists(const struct server_id *id)
231 struct db_context *db;
232 struct serverid_exists_state state;
233 struct serverid_key key;
234 TDB_DATA tdbkey;
236 if (procid_is_me(id)) {
237 return true;
240 if (!process_exists(*id)) {
241 return false;
244 if (id->unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
245 return true;
248 db = serverid_db();
249 if (db == NULL) {
250 return false;
253 serverid_fill_key(id, &key);
254 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
256 state.id = id;
257 state.exists = false;
259 if (db->parse_record(db, tdbkey, server_exists_parse, &state) == -1) {
260 return false;
262 return state.exists;
265 static bool serverid_rec_parse(const struct db_record *rec,
266 struct server_id *id, uint32_t *msg_flags)
268 struct serverid_key key;
269 struct serverid_data data;
271 if (rec->key.dsize != sizeof(key)) {
272 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
273 (int)rec->key.dsize));
274 return false;
276 if (rec->value.dsize != sizeof(data)) {
277 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
278 (int)rec->value.dsize));
279 return false;
282 memcpy(&key, rec->key.dptr, sizeof(key));
283 memcpy(&data, rec->value.dptr, sizeof(data));
285 id->pid = key.pid;
286 id->vnn = key.vnn;
287 id->unique_id = data.unique_id;
288 *msg_flags = data.msg_flags;
289 return true;
292 struct serverid_traverse_read_state {
293 int (*fn)(const struct server_id *id, uint32_t msg_flags,
294 void *private_data);
295 void *private_data;
298 static int serverid_traverse_read_fn(struct db_record *rec, void *private_data)
300 struct serverid_traverse_read_state *state =
301 (struct serverid_traverse_read_state *)private_data;
302 struct server_id id;
303 uint32_t msg_flags;
305 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
306 return 0;
308 return state->fn(&id, msg_flags,state->private_data);
311 bool serverid_traverse_read(int (*fn)(const struct server_id *id,
312 uint32_t msg_flags, void *private_data),
313 void *private_data)
315 struct db_context *db;
316 struct serverid_traverse_read_state state;
318 db = serverid_db();
319 if (db == NULL) {
320 return false;
322 state.fn = fn;
323 state.private_data = private_data;
324 return db->traverse_read(db, serverid_traverse_read_fn, &state);
327 struct serverid_traverse_state {
328 int (*fn)(struct db_record *rec, const struct server_id *id,
329 uint32_t msg_flags, void *private_data);
330 void *private_data;
333 static int serverid_traverse_fn(struct db_record *rec, void *private_data)
335 struct serverid_traverse_state *state =
336 (struct serverid_traverse_state *)private_data;
337 struct server_id id;
338 uint32_t msg_flags;
340 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
341 return 0;
343 return state->fn(rec, &id, msg_flags, state->private_data);
346 bool serverid_traverse(int (*fn)(struct db_record *rec,
347 const struct server_id *id,
348 uint32_t msg_flags, void *private_data),
349 void *private_data)
351 struct db_context *db;
352 struct serverid_traverse_state state;
354 db = serverid_db();
355 if (db == NULL) {
356 return false;
358 state.fn = fn;
359 state.private_data = private_data;
360 return db->traverse(db, serverid_traverse_fn, &state);
363 uint64_t serverid_get_random_unique_id(void)
365 uint64_t unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
367 while (unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
368 generate_random_buffer((uint8_t *)&unique_id,
369 sizeof(unique_id));
372 return unique_id;