| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Implementation of a reliable server_exists()
|
|---|
| 4 | Copyright (C) Volker Lendecke 2010
|
|---|
| 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 "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"
|
|---|
| 26 |
|
|---|
| 27 | struct serverid_key {
|
|---|
| 28 | pid_t pid;
|
|---|
| 29 | uint32_t vnn;
|
|---|
| 30 | };
|
|---|
| 31 |
|
|---|
| 32 | struct serverid_data {
|
|---|
| 33 | uint64_t unique_id;
|
|---|
| 34 | uint32_t msg_flags;
|
|---|
| 35 | };
|
|---|
| 36 |
|
|---|
| 37 | static struct db_context *db_ptr = NULL;
|
|---|
| 38 |
|
|---|
| 39 | static struct db_context *serverid_init(TALLOC_CTX *mem_ctx,
|
|---|
| 40 | bool readonly)
|
|---|
| 41 | {
|
|---|
| 42 | db_ptr = db_open(mem_ctx, lock_path("serverid.tdb"),
|
|---|
| 43 | 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
|
|---|
| 44 | readonly ? O_RDONLY : O_RDWR | O_CREAT,
|
|---|
| 45 | 0644);
|
|---|
| 46 | return db_ptr;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | bool serverid_parent_init(TALLOC_CTX *mem_ctx)
|
|---|
| 50 | {
|
|---|
| 51 | /*
|
|---|
| 52 | * Open the tdb in the parent process (smbd) so that our
|
|---|
| 53 | * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
|
|---|
| 54 | * work.
|
|---|
| 55 | */
|
|---|
| 56 |
|
|---|
| 57 | if (serverid_init(mem_ctx, false) == NULL) {
|
|---|
| 58 | DEBUG(1, ("could not open serverid.tdb: %s\n",
|
|---|
| 59 | strerror(errno)));
|
|---|
| 60 | return false;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | return true;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | bool serverid_init_readonly(TALLOC_CTX *mem_ctx)
|
|---|
| 67 | {
|
|---|
| 68 | if (serverid_init(mem_ctx, true) == NULL) {
|
|---|
| 69 | DEBUG(1, ("could not open serverid.tdb: %s\n",
|
|---|
| 70 | strerror(errno)));
|
|---|
| 71 | return false;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | return true;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | static struct db_context *serverid_db(void)
|
|---|
| 78 | {
|
|---|
| 79 | if (db_ptr != NULL) {
|
|---|
| 80 | return db_ptr;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | return serverid_init(NULL, false);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | static void serverid_fill_key(const struct server_id *id,
|
|---|
| 87 | struct serverid_key *key)
|
|---|
| 88 | {
|
|---|
| 89 | ZERO_STRUCTP(key);
|
|---|
| 90 | key->pid = id->pid;
|
|---|
| 91 | key->vnn = id->vnn;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | bool serverid_register(const struct server_id id, uint32_t msg_flags)
|
|---|
| 95 | {
|
|---|
| 96 | struct db_context *db;
|
|---|
| 97 | struct serverid_key key;
|
|---|
| 98 | struct serverid_data data;
|
|---|
| 99 | struct db_record *rec;
|
|---|
| 100 | TDB_DATA tdbkey, tdbdata;
|
|---|
| 101 | NTSTATUS status;
|
|---|
| 102 | bool ret = false;
|
|---|
| 103 |
|
|---|
| 104 | db = serverid_db();
|
|---|
| 105 | if (db == NULL) {
|
|---|
| 106 | return false;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | serverid_fill_key(&id, &key);
|
|---|
| 110 | tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
|
|---|
| 111 |
|
|---|
| 112 | rec = db->fetch_locked(db, talloc_tos(), tdbkey);
|
|---|
| 113 | if (rec == NULL) {
|
|---|
| 114 | DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
|
|---|
| 115 | return false;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | ZERO_STRUCT(data);
|
|---|
| 119 | data.unique_id = id.unique_id;
|
|---|
| 120 | data.msg_flags = msg_flags;
|
|---|
| 121 |
|
|---|
| 122 | tdbdata = make_tdb_data((uint8_t *)&data, sizeof(data));
|
|---|
| 123 | status = rec->store(rec, tdbdata, 0);
|
|---|
| 124 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 125 | DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
|
|---|
| 126 | nt_errstr(status)));
|
|---|
| 127 | goto done;
|
|---|
| 128 | }
|
|---|
| 129 | ret = true;
|
|---|
| 130 | done:
|
|---|
| 131 | TALLOC_FREE(rec);
|
|---|
| 132 | return ret;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | bool serverid_register_msg_flags(const struct server_id id, bool do_reg,
|
|---|
| 136 | uint32_t msg_flags)
|
|---|
| 137 | {
|
|---|
| 138 | struct db_context *db;
|
|---|
| 139 | struct serverid_key key;
|
|---|
| 140 | struct serverid_data *data;
|
|---|
| 141 | struct db_record *rec;
|
|---|
| 142 | TDB_DATA tdbkey;
|
|---|
| 143 | NTSTATUS status;
|
|---|
| 144 | bool ret = false;
|
|---|
| 145 |
|
|---|
| 146 | db = serverid_db();
|
|---|
| 147 | if (db == NULL) {
|
|---|
| 148 | return false;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | serverid_fill_key(&id, &key);
|
|---|
| 152 | tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
|
|---|
| 153 |
|
|---|
| 154 | rec = db->fetch_locked(db, talloc_tos(), tdbkey);
|
|---|
| 155 | if (rec == NULL) {
|
|---|
| 156 | DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
|
|---|
| 157 | return false;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | if (rec->value.dsize != sizeof(struct serverid_data)) {
|
|---|
| 161 | DEBUG(1, ("serverid record has unexpected size %d "
|
|---|
| 162 | "(wanted %d)\n", (int)rec->value.dsize,
|
|---|
| 163 | (int)sizeof(struct serverid_data)));
|
|---|
| 164 | goto done;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | data = (struct serverid_data *)rec->value.dptr;
|
|---|
| 168 |
|
|---|
| 169 | if (do_reg) {
|
|---|
| 170 | data->msg_flags |= msg_flags;
|
|---|
| 171 | } else {
|
|---|
| 172 | data->msg_flags &= ~msg_flags;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | status = rec->store(rec, rec->value, 0);
|
|---|
| 176 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 177 | DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
|
|---|
| 178 | nt_errstr(status)));
|
|---|
| 179 | goto done;
|
|---|
| 180 | }
|
|---|
| 181 | ret = true;
|
|---|
| 182 | done:
|
|---|
| 183 | TALLOC_FREE(rec);
|
|---|
| 184 | return ret;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | bool serverid_deregister(struct server_id id)
|
|---|
| 188 | {
|
|---|
| 189 | struct db_context *db;
|
|---|
| 190 | struct serverid_key key;
|
|---|
| 191 | struct db_record *rec;
|
|---|
| 192 | TDB_DATA tdbkey;
|
|---|
| 193 | NTSTATUS status;
|
|---|
| 194 | bool ret = false;
|
|---|
| 195 |
|
|---|
| 196 | db = serverid_db();
|
|---|
| 197 | if (db == NULL) {
|
|---|
| 198 | return false;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | serverid_fill_key(&id, &key);
|
|---|
| 202 | tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
|
|---|
| 203 |
|
|---|
| 204 | rec = db->fetch_locked(db, talloc_tos(), tdbkey);
|
|---|
| 205 | if (rec == NULL) {
|
|---|
| 206 | DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
|
|---|
| 207 | return false;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | status = rec->delete_rec(rec);
|
|---|
| 211 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 212 | DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
|
|---|
| 213 | nt_errstr(status)));
|
|---|
| 214 | goto done;
|
|---|
| 215 | }
|
|---|
| 216 | ret = true;
|
|---|
| 217 | done:
|
|---|
| 218 | TALLOC_FREE(rec);
|
|---|
| 219 | return ret;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | struct serverid_exists_state {
|
|---|
| 223 | const struct server_id *id;
|
|---|
| 224 | bool exists;
|
|---|
| 225 | };
|
|---|
| 226 |
|
|---|
| 227 | static int server_exists_parse(TDB_DATA key, TDB_DATA data, void *priv)
|
|---|
| 228 | {
|
|---|
| 229 | struct serverid_exists_state *state =
|
|---|
| 230 | (struct serverid_exists_state *)priv;
|
|---|
| 231 |
|
|---|
| 232 | if (data.dsize != sizeof(struct serverid_data)) {
|
|---|
| 233 | return -1;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /*
|
|---|
| 237 | * Use memcmp, not direct compare. data.dptr might not be
|
|---|
| 238 | * aligned.
|
|---|
| 239 | */
|
|---|
| 240 | state->exists = (memcmp(&state->id->unique_id, data.dptr,
|
|---|
| 241 | sizeof(state->id->unique_id)) == 0);
|
|---|
| 242 | return 0;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | bool serverid_exists(const struct server_id *id)
|
|---|
| 246 | {
|
|---|
| 247 | struct db_context *db;
|
|---|
| 248 | struct serverid_exists_state state;
|
|---|
| 249 | struct serverid_key key;
|
|---|
| 250 | TDB_DATA tdbkey;
|
|---|
| 251 |
|
|---|
| 252 | if (procid_is_me(id)) {
|
|---|
| 253 | return true;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | if (!process_exists(*id)) {
|
|---|
| 257 | return false;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | if (id->unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
|
|---|
| 261 | return true;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | db = serverid_db();
|
|---|
| 265 | if (db == NULL) {
|
|---|
| 266 | return false;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | serverid_fill_key(id, &key);
|
|---|
| 270 | tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
|
|---|
| 271 |
|
|---|
| 272 | state.id = id;
|
|---|
| 273 | state.exists = false;
|
|---|
| 274 |
|
|---|
| 275 | if (db->parse_record(db, tdbkey, server_exists_parse, &state) == -1) {
|
|---|
| 276 | return false;
|
|---|
| 277 | }
|
|---|
| 278 | return state.exists;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | static bool serverid_rec_parse(const struct db_record *rec,
|
|---|
| 282 | struct server_id *id, uint32_t *msg_flags)
|
|---|
| 283 | {
|
|---|
| 284 | struct serverid_key key;
|
|---|
| 285 | struct serverid_data data;
|
|---|
| 286 |
|
|---|
| 287 | if (rec->key.dsize != sizeof(key)) {
|
|---|
| 288 | DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
|
|---|
| 289 | (int)rec->key.dsize));
|
|---|
| 290 | return false;
|
|---|
| 291 | }
|
|---|
| 292 | if (rec->value.dsize != sizeof(data)) {
|
|---|
| 293 | DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
|
|---|
| 294 | (int)rec->value.dsize));
|
|---|
| 295 | return false;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | memcpy(&key, rec->key.dptr, sizeof(key));
|
|---|
| 299 | memcpy(&data, rec->value.dptr, sizeof(data));
|
|---|
| 300 |
|
|---|
| 301 | id->pid = key.pid;
|
|---|
| 302 | id->vnn = key.vnn;
|
|---|
| 303 | id->unique_id = data.unique_id;
|
|---|
| 304 | *msg_flags = data.msg_flags;
|
|---|
| 305 | return true;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | struct serverid_traverse_read_state {
|
|---|
| 309 | int (*fn)(const struct server_id *id, uint32_t msg_flags,
|
|---|
| 310 | void *private_data);
|
|---|
| 311 | void *private_data;
|
|---|
| 312 | };
|
|---|
| 313 |
|
|---|
| 314 | static int serverid_traverse_read_fn(struct db_record *rec, void *private_data)
|
|---|
| 315 | {
|
|---|
| 316 | struct serverid_traverse_read_state *state =
|
|---|
| 317 | (struct serverid_traverse_read_state *)private_data;
|
|---|
| 318 | struct server_id id;
|
|---|
| 319 | uint32_t msg_flags;
|
|---|
| 320 |
|
|---|
| 321 | if (!serverid_rec_parse(rec, &id, &msg_flags)) {
|
|---|
| 322 | return 0;
|
|---|
| 323 | }
|
|---|
| 324 | return state->fn(&id, msg_flags,state->private_data);
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | bool serverid_traverse_read(int (*fn)(const struct server_id *id,
|
|---|
| 328 | uint32_t msg_flags, void *private_data),
|
|---|
| 329 | void *private_data)
|
|---|
| 330 | {
|
|---|
| 331 | struct db_context *db;
|
|---|
| 332 | struct serverid_traverse_read_state state;
|
|---|
| 333 |
|
|---|
| 334 | db = serverid_db();
|
|---|
| 335 | if (db == NULL) {
|
|---|
| 336 | return false;
|
|---|
| 337 | }
|
|---|
| 338 | state.fn = fn;
|
|---|
| 339 | state.private_data = private_data;
|
|---|
| 340 | return db->traverse_read(db, serverid_traverse_read_fn, &state);
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | struct serverid_traverse_state {
|
|---|
| 344 | int (*fn)(struct db_record *rec, const struct server_id *id,
|
|---|
| 345 | uint32_t msg_flags, void *private_data);
|
|---|
| 346 | void *private_data;
|
|---|
| 347 | };
|
|---|
| 348 |
|
|---|
| 349 | static int serverid_traverse_fn(struct db_record *rec, void *private_data)
|
|---|
| 350 | {
|
|---|
| 351 | struct serverid_traverse_state *state =
|
|---|
| 352 | (struct serverid_traverse_state *)private_data;
|
|---|
| 353 | struct server_id id;
|
|---|
| 354 | uint32_t msg_flags;
|
|---|
| 355 |
|
|---|
| 356 | if (!serverid_rec_parse(rec, &id, &msg_flags)) {
|
|---|
| 357 | return 0;
|
|---|
| 358 | }
|
|---|
| 359 | return state->fn(rec, &id, msg_flags, state->private_data);
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | bool serverid_traverse(int (*fn)(struct db_record *rec,
|
|---|
| 363 | const struct server_id *id,
|
|---|
| 364 | uint32_t msg_flags, void *private_data),
|
|---|
| 365 | void *private_data)
|
|---|
| 366 | {
|
|---|
| 367 | struct db_context *db;
|
|---|
| 368 | struct serverid_traverse_state state;
|
|---|
| 369 |
|
|---|
| 370 | db = serverid_db();
|
|---|
| 371 | if (db == NULL) {
|
|---|
| 372 | return false;
|
|---|
| 373 | }
|
|---|
| 374 | state.fn = fn;
|
|---|
| 375 | state.private_data = private_data;
|
|---|
| 376 | return db->traverse(db, serverid_traverse_fn, &state);
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | uint64_t serverid_get_random_unique_id(void)
|
|---|
| 380 | {
|
|---|
| 381 | uint64_t unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
|
|---|
| 382 |
|
|---|
| 383 | while (unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
|
|---|
| 384 | generate_random_buffer((uint8_t *)&unique_id,
|
|---|
| 385 | sizeof(unique_id));
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | return unique_id;
|
|---|
| 389 | }
|
|---|