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/>.
21 #include "system/filesys.h"
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_open.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,
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"));
53 struct db_record
*sessionid_fetch_record(TALLOC_CTX
*mem_ctx
, const char *key
)
55 struct db_context
*db
;
57 db
= session_db_ctx();
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
);
70 static int sessionid_traverse_fn(struct db_record
*rec
, void *private_data
)
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"));
86 memcpy(&session
, value
.dptr
, sizeof(session
));
88 return state
->fn(rec
, (char *)key
.dptr
, &session
,
92 NTSTATUS
sessionid_traverse(int (*fn
)(struct db_record
*rec
, const char *key
,
93 struct sessionid
*session
,
97 struct db_context
*db
;
98 struct sessionid_traverse_state state
;
101 db
= session_db_ctx();
103 return NT_STATUS_UNSUCCESSFUL
;
106 state
.private_data
= private_data
;
107 status
= dbwrap_traverse(db
, sessionid_traverse_fn
, &state
, NULL
);
111 struct sessionid_traverse_read_state
{
112 int (*fn
)(const char *key
, struct sessionid
*session
,
117 static int sessionid_traverse_read_fn(struct smbXsrv_session_global0
*global
,
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
,
134 strncpy(session
.remote_machine
,
135 global
->channels
[0].remote_name
,
137 strncpy(session
.hostname
,
138 global
->channels
[0].remote_address
,
140 strncpy(session
.netbios_name
,
141 global
->channels
[0].remote_name
,
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
,
149 return state
->fn(NULL
, &session
, state
->private_data
);
152 NTSTATUS
sessionid_traverse_read(int (*fn
)(const char *key
,
153 struct sessionid
*session
,
157 struct sessionid_traverse_read_state state
;
161 state
.private_data
= private_data
;
162 status
= smbXsrv_session_global_traverse(sessionid_traverse_read_fn
,