s3:lib: implement sessionid_traverse_read with smb2srv_session_traverse_read
[Samba/gebeck_regimport.git] / source3 / lib / sessionid_tdb.c
blobfafdb9a8751eaef4674110043b7705a9cc17d0f3
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"
26 #include "smbd/globals.h"
28 static struct db_context *session_db_ctx(void)
30 static struct db_context *session_db_ctx_ptr;
32 if (session_db_ctx_ptr != NULL) {
33 return session_db_ctx_ptr;
36 session_db_ctx_ptr = db_open(NULL, lock_path("sessionid.tdb"), 0,
37 TDB_CLEAR_IF_FIRST|TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
38 O_RDWR | O_CREAT, 0644,
39 DBWRAP_LOCK_ORDER_1);
40 return session_db_ctx_ptr;
43 bool sessionid_init(void)
45 if (session_db_ctx() == NULL) {
46 DEBUG(1,("session_init: failed to open sessionid tdb\n"));
47 return False;
50 return True;
53 struct db_record *sessionid_fetch_record(TALLOC_CTX *mem_ctx, const char *key)
55 struct db_context *db;
57 db = session_db_ctx();
58 if (db == NULL) {
59 return NULL;
61 return dbwrap_fetch_locked(db, mem_ctx, string_term_tdb_data(key));
64 struct sessionid_traverse_state {
65 int (*fn)(struct db_record *rec, const char *key,
66 struct sessionid *session, void *private_data);
67 void *private_data;
70 static int sessionid_traverse_fn(struct db_record *rec, void *private_data)
72 TDB_DATA key;
73 TDB_DATA value;
74 struct sessionid_traverse_state *state =
75 (struct sessionid_traverse_state *)private_data;
76 struct sessionid session;
78 key = dbwrap_record_get_key(rec);
79 value = dbwrap_record_get_value(rec);
80 if ((key.dptr[key.dsize-1] != '\0')
81 || (value.dsize != sizeof(struct sessionid))) {
82 DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
83 return 0;
86 memcpy(&session, value.dptr, sizeof(session));
88 return state->fn(rec, (char *)key.dptr, &session,
89 state->private_data);
92 NTSTATUS sessionid_traverse(int (*fn)(struct db_record *rec, const char *key,
93 struct sessionid *session,
94 void *private_data),
95 void *private_data)
97 struct db_context *db;
98 struct sessionid_traverse_state state;
99 NTSTATUS status;
101 db = session_db_ctx();
102 if (db == NULL) {
103 return NT_STATUS_UNSUCCESSFUL;
105 state.fn = fn;
106 state.private_data = private_data;
107 status = dbwrap_traverse(db, sessionid_traverse_fn, &state, NULL);
108 return status;
111 struct sessionid_traverse_read_state {
112 int (*fn)(const char *key, struct sessionid *session,
113 void *private_data);
114 void *private_data;
117 static int sessionid_traverse_read_fn(struct smbXsrv_session_global0 *global,
118 void *private_data)
120 struct sessionid_traverse_read_state *state =
121 (struct sessionid_traverse_read_state *)private_data;
122 struct auth_session_info *session_info = global->auth_session_info;
123 struct sessionid session = {
124 .uid = session_info->unix_token->uid,
125 .gid = session_info->unix_token->gid,
126 .id_num = global->session_global_id,
127 .connect_start = nt_time_to_unix(global->creation_time),
128 .pid = global->channels[0].server_id,
131 strncpy(session.username,
132 session_info->unix_info->unix_name,
133 sizeof(fstring)-1);
134 strncpy(session.remote_machine,
135 global->channels[0].remote_name,
136 sizeof(fstring)-1);
137 strncpy(session.hostname,
138 global->channels[0].remote_address,
139 sizeof(fstring)-1);
140 strncpy(session.netbios_name,
141 global->channels[0].remote_name,
142 sizeof(fstring)-1);
143 snprintf(session.id_str, sizeof(fstring)-1,
144 "smb/%u", global->session_global_id);
145 strncpy(session.ip_addr_str,
146 global->channels[0].remote_address,
147 sizeof(fstring)-1);
149 return state->fn(NULL, &session, state->private_data);
152 NTSTATUS sessionid_traverse_read(int (*fn)(const char *key,
153 struct sessionid *session,
154 void *private_data),
155 void *private_data)
157 struct sessionid_traverse_read_state state;
158 NTSTATUS status;
160 state.fn = fn;
161 state.private_data = private_data;
162 status = smbXsrv_session_global_traverse(sessionid_traverse_read_fn,
163 &state);
165 return status;