summaryrefslogtreecommitdiffstats
path: root/source3/lib/conn_tdb.c
blob: 9b0a07a56c0a453afc486084010862b4674a9fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* 
   Unix SMB/CIFS implementation.
   Low-level connections.tdb access functions
   Copyright (C) Volker Lendecke 2007

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "system/filesys.h"
#include "smbd/globals.h"
#include "dbwrap/dbwrap.h"
#include "dbwrap/dbwrap_open.h"
#include "messages.h"

static struct db_context *connections_db_ctx(bool rw)
{
	static struct db_context *db_ctx;
	int open_flags;

	if (db_ctx != NULL) {
		return db_ctx;
	}

	open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;

	db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
			 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH|TDB_DEFAULT,
			 open_flags, 0644, DBWRAP_LOCK_ORDER_1);
	return db_ctx;
}

static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
						  TDB_DATA key)
{
	struct db_context *ctx = connections_db_ctx(True);

	if (ctx == NULL) {
		return NULL;
	}

	return dbwrap_fetch_locked(ctx, mem_ctx, key);
}

struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
					  connection_struct *conn,
					  const char *name)
{
	struct connections_key ckey;
	TDB_DATA key;

	ZERO_STRUCT(ckey);
	ckey.pid = messaging_server_id(conn->sconn->msg_ctx);
	ckey.cnum = conn->cnum;
	strlcpy(ckey.name, name, sizeof(ckey.name));

	key.dsize = sizeof(ckey);
	key.dptr = (uint8 *)&ckey;

	return connections_fetch_record(mem_ctx, key);
}

struct conn_traverse_state {
	int (*fn)(struct db_record *rec,
		  const struct connections_key *key,
		  const struct connections_data *data,
		  void *private_data);
	void *private_data;
};

static int conn_traverse_fn(struct db_record *rec, void *private_data)
{
	TDB_DATA key;
	TDB_DATA value;
	struct conn_traverse_state *state =
		(struct conn_traverse_state *)private_data;

	key = dbwrap_record_get_key(rec);
	value = dbwrap_record_get_value(rec);

	if ((key.dsize != sizeof(struct connections_key))
	    || (value.dsize != sizeof(struct connections_data))) {
		return 0;
	}

	return state->fn(rec, (const struct connections_key *)key.dptr,
			 (const struct connections_data *)value.dptr,
			 state->private_data);
}

int connections_traverse(int (*fn)(struct db_record *rec,
				   void *private_data),
			 void *private_data)
{
	NTSTATUS status;
	int count;
	struct db_context *ctx = connections_db_ctx(False);

	if (ctx == NULL) {
		return -1;
	}

	status = dbwrap_traverse(ctx, fn, private_data, &count);
	if (!NT_STATUS_IS_OK(status)) {
		return -1;
	}

	return count;
}

int connections_forall(int (*fn)(struct db_record *rec,
				 const struct connections_key *key,
				 const struct connections_data *data,
				 void *private_data),
		       void *private_data)
{
	struct db_context *ctx;
	struct conn_traverse_state state;
	NTSTATUS status;
	int count;

	ctx = connections_db_ctx(true);
	if (ctx == NULL) {
		return -1;
	}

	state.fn = fn;
	state.private_data = private_data;

	status = dbwrap_traverse(ctx, conn_traverse_fn, (void *)&state, &count);
	if (!NT_STATUS_IS_OK(status)) {
		return -1;
	}

	return count;
}

struct conn_traverse_read_state {
	int (*fn)(const struct connections_key *key,
		  const struct connections_data *data,
		  void *private_data);
	void *private_data;
};

static int connections_forall_read_fn(struct db_record *rec,
				      void *private_data)
{
	TDB_DATA key;
	TDB_DATA value;
	struct conn_traverse_read_state *state =
		(struct conn_traverse_read_state *)private_data;

	key = dbwrap_record_get_key(rec);
	value = dbwrap_record_get_value(rec);

	if ((key.dsize != sizeof(struct connections_key))
	    || (value.dsize != sizeof(struct connections_data))) {
		return 0;
	}
	return state->fn((const struct connections_key *)key.dptr,
			 (const struct connections_data *)value.dptr,
			 state->private_data);
}

int connections_forall_read(int (*fn)(const struct connections_key *key,
				      const struct connections_data *data,
				      void *private_data),
			    void *private_data)
{
	struct db_context *ctx;
	struct conn_traverse_read_state state;
	NTSTATUS status;
	int count;

	ctx = connections_db_ctx(false);
	if (ctx == NULL) {
		return -1;
	}

	state.fn = fn;
	state.private_data = private_data;

	status = dbwrap_traverse_read(ctx, connections_forall_read_fn,
				      (void *)&state, &count);

	if (!NT_STATUS_IS_OK(status)) {
		return -1;
	}

	return count;
}

bool connections_init(bool rw)
{
	return (connections_db_ctx(rw) != NULL);
}