| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Low-level connections.tdb access functions
|
|---|
| 4 | Copyright (C) Volker Lendecke 2007
|
|---|
| 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 |
|
|---|
| 22 | static struct db_context *connections_db_ctx(bool rw)
|
|---|
| 23 | {
|
|---|
| 24 | static struct db_context *db_ctx;
|
|---|
| 25 |
|
|---|
| 26 | if (db_ctx != NULL) {
|
|---|
| 27 | return db_ctx;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | if (rw) {
|
|---|
| 31 | db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
|
|---|
| 32 | TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
|
|---|
| 33 | O_RDWR | O_CREAT, 0644);
|
|---|
| 34 | }
|
|---|
| 35 | else {
|
|---|
| 36 | db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
|
|---|
| 37 | TDB_CLEAR_IF_FIRST|TDB_DEFAULT, O_RDONLY, 0);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | return db_ctx;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
|
|---|
| 44 | TDB_DATA key)
|
|---|
| 45 | {
|
|---|
| 46 | struct db_context *ctx = connections_db_ctx(True);
|
|---|
| 47 |
|
|---|
| 48 | if (ctx == NULL) {
|
|---|
| 49 | return NULL;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | return ctx->fetch_locked(ctx, mem_ctx, key);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
|
|---|
| 56 | connection_struct *conn,
|
|---|
| 57 | const char *name)
|
|---|
| 58 | {
|
|---|
| 59 | struct connections_key ckey;
|
|---|
| 60 | TDB_DATA key;
|
|---|
| 61 |
|
|---|
| 62 | ZERO_STRUCT(ckey);
|
|---|
| 63 | ckey.pid = procid_self();
|
|---|
| 64 | ckey.cnum = conn ? conn->cnum : -1;
|
|---|
| 65 | strlcpy(ckey.name, name, sizeof(ckey.name));
|
|---|
| 66 |
|
|---|
| 67 | key.dsize = sizeof(ckey);
|
|---|
| 68 | key.dptr = (uint8 *)&ckey;
|
|---|
| 69 |
|
|---|
| 70 | return connections_fetch_record(mem_ctx, key);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | struct conn_traverse_state {
|
|---|
| 74 | int (*fn)(struct db_record *rec,
|
|---|
| 75 | const struct connections_key *key,
|
|---|
| 76 | const struct connections_data *data,
|
|---|
| 77 | void *private_data);
|
|---|
| 78 | void *private_data;
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | static int conn_traverse_fn(struct db_record *rec, void *private_data)
|
|---|
| 82 | {
|
|---|
| 83 | struct conn_traverse_state *state =
|
|---|
| 84 | (struct conn_traverse_state *)private_data;
|
|---|
| 85 |
|
|---|
| 86 | if ((rec->key.dsize != sizeof(struct connections_key))
|
|---|
| 87 | || (rec->value.dsize != sizeof(struct connections_data))) {
|
|---|
| 88 | return 0;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | return state->fn(rec, (const struct connections_key *)rec->key.dptr,
|
|---|
| 92 | (const struct connections_data *)rec->value.dptr,
|
|---|
| 93 | state->private_data);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | int connections_traverse(int (*fn)(struct db_record *rec,
|
|---|
| 97 | void *private_data),
|
|---|
| 98 | void *private_data)
|
|---|
| 99 | {
|
|---|
| 100 | struct db_context *ctx = connections_db_ctx(False);
|
|---|
| 101 |
|
|---|
| 102 | if (ctx == NULL) {
|
|---|
| 103 | return -1;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | return ctx->traverse(ctx, fn, private_data);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | int connections_forall(int (*fn)(struct db_record *rec,
|
|---|
| 110 | const struct connections_key *key,
|
|---|
| 111 | const struct connections_data *data,
|
|---|
| 112 | void *private_data),
|
|---|
| 113 | void *private_data)
|
|---|
| 114 | {
|
|---|
| 115 | struct conn_traverse_state state;
|
|---|
| 116 |
|
|---|
| 117 | state.fn = fn;
|
|---|
| 118 | state.private_data = private_data;
|
|---|
| 119 |
|
|---|
| 120 | return connections_traverse(conn_traverse_fn, (void *)&state);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | bool connections_init(bool rw)
|
|---|
| 124 | {
|
|---|
| 125 | return (connections_db_ctx(rw) != NULL);
|
|---|
| 126 | }
|
|---|