s3-smbd: Added a change_to_user_by_session() function.
[Samba.git] / source3 / lib / sessionid_tdb.c
blobab54ec9ca5f8222ca0fcc611ebb7f3a644367818
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.h"
23 #include "session.h"
25 static struct db_context *session_db_ctx(void)
27 static struct db_context *session_db_ctx_ptr;
29 if (session_db_ctx_ptr != NULL) {
30 return session_db_ctx_ptr;
33 session_db_ctx_ptr = db_open(NULL, lock_path("sessionid.tdb"), 0,
34 TDB_CLEAR_IF_FIRST|TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
35 O_RDWR | O_CREAT, 0644);
36 return session_db_ctx_ptr;
39 bool sessionid_init(void)
41 if (session_db_ctx() == NULL) {
42 DEBUG(1,("session_init: failed to open sessionid tdb\n"));
43 return False;
46 return True;
49 struct db_record *sessionid_fetch_record(TALLOC_CTX *mem_ctx, const char *key)
51 struct db_context *db;
53 db = session_db_ctx();
54 if (db == NULL) {
55 return NULL;
57 return db->fetch_locked(db, mem_ctx, string_term_tdb_data(key));
60 struct sessionid_traverse_state {
61 int (*fn)(struct db_record *rec, const char *key,
62 struct sessionid *session, void *private_data);
63 void *private_data;
66 static int sessionid_traverse_fn(struct db_record *rec, void *private_data)
68 struct sessionid_traverse_state *state =
69 (struct sessionid_traverse_state *)private_data;
70 struct sessionid session;
72 if ((rec->key.dptr[rec->key.dsize-1] != '\0')
73 || (rec->value.dsize != sizeof(struct sessionid))) {
74 DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
75 return 0;
78 memcpy(&session, rec->value.dptr, sizeof(session));
80 return state->fn(rec, (char *)rec->key.dptr, &session,
81 state->private_data);
84 int sessionid_traverse(int (*fn)(struct db_record *rec, const char *key,
85 struct sessionid *session,
86 void *private_data),
87 void *private_data)
89 struct db_context *db;
90 struct sessionid_traverse_state state;
92 db = session_db_ctx();
93 if (db == NULL) {
94 return -1;
96 state.fn = fn;
97 state.private_data = private_data;
98 return db->traverse(db, sessionid_traverse_fn, &state);
101 struct sessionid_traverse_read_state {
102 int (*fn)(const char *key, struct sessionid *session,
103 void *private_data);
104 void *private_data;
107 static int sessionid_traverse_read_fn(struct db_record *rec,
108 void *private_data)
110 struct sessionid_traverse_read_state *state =
111 (struct sessionid_traverse_read_state *)private_data;
112 struct sessionid session;
114 if ((rec->key.dptr[rec->key.dsize-1] != '\0')
115 || (rec->value.dsize != sizeof(struct sessionid))) {
116 DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
117 return 0;
120 memcpy(&session, rec->value.dptr, sizeof(session));
122 return state->fn((char *)rec->key.dptr, &session,
123 state->private_data);
126 int sessionid_traverse_read(int (*fn)(const char *key,
127 struct sessionid *session,
128 void *private_data),
129 void *private_data)
131 struct db_context *db;
132 struct sessionid_traverse_read_state state;
134 db = session_db_ctx();
135 if (db == NULL) {
136 return -1;
138 state.fn = fn;
139 state.private_data = private_data;
140 return db->traverse(db, sessionid_traverse_read_fn, &state);