summaryrefslogtreecommitdiff
path: root/source3/lib/conn_tdb.c
blob: e6f491bbfb782446e58a28dc26d9b058b48c2d50 (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
/* 
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

TDB_CONTEXT *conn_tdb_ctx(BOOL rw)
{
	static TDB_CONTEXT *tdb;

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

	if (rw) {
		tdb = tdb_open_log(lock_path("connections.tdb"), 0,
				   TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
				   O_RDWR | O_CREAT, 0644);
	}
	else {
		tdb = tdb_open_log(lock_path("connections.tdb"), 0,
				   TDB_DEFAULT, O_RDONLY, 0);
	}

	if (tdb == NULL) {
		DEBUG(0, ("Could not open connections.tdb: %s\n",
			  strerror(errno)));
	}

	return tdb;
}

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

static int conn_traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key,
			    TDB_DATA data, void *private_data)
{
	struct conn_traverse_state *state =
		(struct conn_traverse_state *)private_data;

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

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

int connections_traverse(int (*fn)(TDB_CONTEXT *tdb, TDB_DATA key,
				   TDB_DATA data, void *private_data),
			 void *private_data)
{
	TDB_CONTEXT *tdb = conn_tdb_ctx(True);

	if (tdb == NULL) {
		DEBUG(5, ("Could not open connections.tdb r/w, trying r/o\n"));
		tdb = conn_tdb_ctx(False);
	}

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

	return tdb_traverse(tdb, fn, private_data);
}

int connections_forall(int (*fn)(TDB_CONTEXT *tdb,
				 const struct connections_key *key,
				 const struct connections_data *data,
				 void *private_data),
		       void *private_data)
{
	struct conn_traverse_state state;

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

	return connections_traverse(conn_traverse_fn, (void *)&state);
}

BOOL connections_init(BOOL rw)
{
	return (conn_tdb_ctx(rw) != NULL);
}