Revert "ldb: Remove Samba-specific symbols."
[Samba/ekacnet.git] / source3 / lib / sessionid_tdb.c
blob6efbafd46d5c100f4f2fc9612d52caae30ede9a3
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"
22 static struct db_context *session_db_ctx(void)
24 static struct db_context *session_db_ctx_ptr;
26 if (session_db_ctx_ptr != NULL) {
27 return session_db_ctx_ptr;
30 session_db_ctx_ptr = db_open(NULL, lock_path("sessionid.tdb"), 0,
31 TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
32 O_RDWR | O_CREAT, 0644);
33 return session_db_ctx_ptr;
36 bool sessionid_init(void)
38 if (session_db_ctx() == NULL) {
39 DEBUG(1,("session_init: failed to open sessionid tdb\n"));
40 return False;
43 return True;
46 struct db_record *sessionid_fetch_record(TALLOC_CTX *mem_ctx, const char *key)
48 struct db_context *db;
50 db = session_db_ctx();
51 if (db == NULL) {
52 return NULL;
54 return db->fetch_locked(db, mem_ctx, string_term_tdb_data(key));
57 struct sessionid_traverse_state {
58 int (*fn)(struct db_record *rec, const char *key,
59 struct sessionid *session, void *private_data);
60 void *private_data;
63 static int sessionid_traverse_fn(struct db_record *rec, void *private_data)
65 struct sessionid_traverse_state *state =
66 (struct sessionid_traverse_state *)private_data;
67 struct sessionid session;
69 if ((rec->key.dptr[rec->key.dsize-1] != '\0')
70 || (rec->value.dsize != sizeof(struct sessionid))) {
71 DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
72 return 0;
75 memcpy(&session, rec->value.dptr, sizeof(session));
77 return state->fn(rec, (char *)rec->key.dptr, &session,
78 state->private_data);
81 int sessionid_traverse(int (*fn)(struct db_record *rec, const char *key,
82 struct sessionid *session,
83 void *private_data),
84 void *private_data)
86 struct db_context *db;
87 struct sessionid_traverse_state state;
89 db = session_db_ctx();
90 if (db == NULL) {
91 return -1;
93 state.fn = fn;
94 state.private_data = private_data;
95 return db->traverse(db, sessionid_traverse_fn, &state);
98 struct sessionid_traverse_read_state {
99 int (*fn)(const char *key, struct sessionid *session,
100 void *private_data);
101 void *private_data;
104 static int sessionid_traverse_read_fn(struct db_record *rec,
105 void *private_data)
107 struct sessionid_traverse_read_state *state =
108 (struct sessionid_traverse_read_state *)private_data;
109 struct sessionid session;
111 if ((rec->key.dptr[rec->key.dsize-1] != '\0')
112 || (rec->value.dsize != sizeof(struct sessionid))) {
113 DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
114 return 0;
117 memcpy(&session, rec->value.dptr, sizeof(session));
119 return state->fn((char *)rec->key.dptr, &session,
120 state->private_data);
123 int sessionid_traverse_read(int (*fn)(const char *key,
124 struct sessionid *session,
125 void *private_data),
126 void *private_data)
128 struct db_context *db;
129 struct sessionid_traverse_read_state state;
131 db = session_db_ctx();
132 if (db == NULL) {
133 return -1;
135 state.fn = fn;
136 state.private_data = private_data;
137 return db->traverse(db, sessionid_traverse_read_fn, &state);