s3-prefork: Allow better management of allowed_clients
[Samba/bjacke.git] / source3 / lib / sessionid_tdb.c
blobebc1b8af79632bf625e32ef2a115bb67e32f4ebb
1 /*
2 Unix SMB/CIFS implementation.
3 Low-level sessionid.tdb access functions
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 "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_open.h"
24 #include "session.h"
25 #include "util_tdb.h"
27 static struct db_context *session_db_ctx(void)
29 static struct db_context *session_db_ctx_ptr;
31 if (session_db_ctx_ptr != NULL) {
32 return session_db_ctx_ptr;
35 session_db_ctx_ptr = db_open(NULL, lock_path("sessionid.tdb"), 0,
36 TDB_CLEAR_IF_FIRST|TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
37 O_RDWR | O_CREAT, 0644);
38 return session_db_ctx_ptr;
41 bool sessionid_init(void)
43 if (session_db_ctx() == NULL) {
44 DEBUG(1,("session_init: failed to open sessionid tdb\n"));
45 return False;
48 return True;
51 struct db_record *sessionid_fetch_record(TALLOC_CTX *mem_ctx, const char *key)
53 struct db_context *db;
55 db = session_db_ctx();
56 if (db == NULL) {
57 return NULL;
59 return db->fetch_locked(db, mem_ctx, string_term_tdb_data(key));
62 struct sessionid_traverse_state {
63 int (*fn)(struct db_record *rec, const char *key,
64 struct sessionid *session, void *private_data);
65 void *private_data;
68 static int sessionid_traverse_fn(struct db_record *rec, void *private_data)
70 struct sessionid_traverse_state *state =
71 (struct sessionid_traverse_state *)private_data;
72 struct sessionid session;
74 if ((rec->key.dptr[rec->key.dsize-1] != '\0')
75 || (rec->value.dsize != sizeof(struct sessionid))) {
76 DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
77 return 0;
80 memcpy(&session, rec->value.dptr, sizeof(session));
82 return state->fn(rec, (char *)rec->key.dptr, &session,
83 state->private_data);
86 int sessionid_traverse(int (*fn)(struct db_record *rec, const char *key,
87 struct sessionid *session,
88 void *private_data),
89 void *private_data)
91 struct db_context *db;
92 struct sessionid_traverse_state state;
94 db = session_db_ctx();
95 if (db == NULL) {
96 return -1;
98 state.fn = fn;
99 state.private_data = private_data;
100 return db->traverse(db, sessionid_traverse_fn, &state);
103 struct sessionid_traverse_read_state {
104 int (*fn)(const char *key, struct sessionid *session,
105 void *private_data);
106 void *private_data;
109 static int sessionid_traverse_read_fn(struct db_record *rec,
110 void *private_data)
112 struct sessionid_traverse_read_state *state =
113 (struct sessionid_traverse_read_state *)private_data;
114 struct sessionid session;
116 if ((rec->key.dptr[rec->key.dsize-1] != '\0')
117 || (rec->value.dsize != sizeof(struct sessionid))) {
118 DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
119 return 0;
122 memcpy(&session, rec->value.dptr, sizeof(session));
124 return state->fn((char *)rec->key.dptr, &session,
125 state->private_data);
128 int sessionid_traverse_read(int (*fn)(const char *key,
129 struct sessionid *session,
130 void *private_data),
131 void *private_data)
133 struct db_context *db;
134 struct sessionid_traverse_read_state state;
136 db = session_db_ctx();
137 if (db == NULL) {
138 return -1;
140 state.fn = fn;
141 state.private_data = private_data;
142 return db->traverse(db, sessionid_traverse_read_fn, &state);