s4:ldb-samba/ldb_wrap.c - fix indentation and trailing whitespaces
[Samba.git] / source3 / lib / sessionid_tdb.c
blobfe67681d189802b14c8ec7f8bfae225227877098
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 "dbwrap.h"
23 static struct db_context *session_db_ctx(void)
25 static struct db_context *session_db_ctx_ptr;
27 if (session_db_ctx_ptr != NULL) {
28 return session_db_ctx_ptr;
31 session_db_ctx_ptr = db_open(NULL, lock_path("sessionid.tdb"), 0,
32 TDB_CLEAR_IF_FIRST|TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
33 O_RDWR | O_CREAT, 0644);
34 return session_db_ctx_ptr;
37 bool sessionid_init(void)
39 if (session_db_ctx() == NULL) {
40 DEBUG(1,("session_init: failed to open sessionid tdb\n"));
41 return False;
44 return True;
47 struct db_record *sessionid_fetch_record(TALLOC_CTX *mem_ctx, const char *key)
49 struct db_context *db;
51 db = session_db_ctx();
52 if (db == NULL) {
53 return NULL;
55 return db->fetch_locked(db, mem_ctx, string_term_tdb_data(key));
58 struct sessionid_traverse_state {
59 int (*fn)(struct db_record *rec, const char *key,
60 struct sessionid *session, void *private_data);
61 void *private_data;
64 static int sessionid_traverse_fn(struct db_record *rec, void *private_data)
66 struct sessionid_traverse_state *state =
67 (struct sessionid_traverse_state *)private_data;
68 struct sessionid session;
70 if ((rec->key.dptr[rec->key.dsize-1] != '\0')
71 || (rec->value.dsize != sizeof(struct sessionid))) {
72 DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
73 return 0;
76 memcpy(&session, rec->value.dptr, sizeof(session));
78 return state->fn(rec, (char *)rec->key.dptr, &session,
79 state->private_data);
82 int sessionid_traverse(int (*fn)(struct db_record *rec, const char *key,
83 struct sessionid *session,
84 void *private_data),
85 void *private_data)
87 struct db_context *db;
88 struct sessionid_traverse_state state;
90 db = session_db_ctx();
91 if (db == NULL) {
92 return -1;
94 state.fn = fn;
95 state.private_data = private_data;
96 return db->traverse(db, sessionid_traverse_fn, &state);
99 struct sessionid_traverse_read_state {
100 int (*fn)(const char *key, struct sessionid *session,
101 void *private_data);
102 void *private_data;
105 static int sessionid_traverse_read_fn(struct db_record *rec,
106 void *private_data)
108 struct sessionid_traverse_read_state *state =
109 (struct sessionid_traverse_read_state *)private_data;
110 struct sessionid session;
112 if ((rec->key.dptr[rec->key.dsize-1] != '\0')
113 || (rec->value.dsize != sizeof(struct sessionid))) {
114 DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
115 return 0;
118 memcpy(&session, rec->value.dptr, sizeof(session));
120 return state->fn((char *)rec->key.dptr, &session,
121 state->private_data);
124 int sessionid_traverse_read(int (*fn)(const char *key,
125 struct sessionid *session,
126 void *private_data),
127 void *private_data)
129 struct db_context *db;
130 struct sessionid_traverse_read_state state;
132 db = session_db_ctx();
133 if (db == NULL) {
134 return -1;
136 state.fn = fn;
137 state.private_data = private_data;
138 return db->traverse(db, sessionid_traverse_read_fn, &state);