1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*
Unix SMB/CIFS implementation.
session handling for utmp and PAM
Copyright (C) tridge@samba.org 2001
Copyright (C) abartlet@samba.org 2001
Copyright (C) Gerald (Jerry) Carter 2006
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* a "session" is claimed when we do a SessionSetupX operation
and is yielded when the corresponding vuid is destroyed.
sessions are used to populate utmp and PAM session structures
*/
#include "includes.h"
static TDB_CONTEXT *tdb;
/********************************************************************
********************************************************************/
BOOL session_init(void)
{
if (tdb)
return True;
tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
O_RDWR | O_CREAT, 0644);
if (!tdb) {
DEBUG(1,("session_init: failed to open sessionid tdb\n"));
return False;
}
return True;
}
/********************************************************************
called when a session is created
********************************************************************/
BOOL session_claim(user_struct *vuser)
{
int i = 0;
TDB_DATA data;
struct sockaddr sa;
struct in_addr *client_ip;
struct sessionid sessionid;
uint32 pid = (uint32)sys_getpid();
fstring keystr;
char * hostname;
int tdb_store_flag; /* If using utmp, we do an inital 'lock hold' store,
but we don't need this if we are just using the
(unique) pid/vuid combination */
vuser->session_keystr = NULL;
/* don't register sessions for the guest user - its just too
expensive to go through pam session code for browsing etc */
if (vuser->guest) {
return True;
}
if (!session_init())
return False;
ZERO_STRUCT(sessionid);
data.dptr = NULL;
data.dsize = 0;
if (lp_utmp()) {
for (i=1;i<MAX_SESSION_ID;i++) {
slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
if (tdb_store_bystring(tdb, keystr, data, TDB_INSERT) == 0) break;
}
if (i == MAX_SESSION_ID) {
DEBUG(1,("session_claim: out of session IDs (max is %d)\n",
MAX_SESSION_ID));
return False;
}
slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, SESSION_UTMP_TEMPLATE, i);
tdb_store_flag = TDB_MODIFY;
} else
{
slprintf(keystr, sizeof(keystr)-1, "ID/%lu/%u",
(long unsigned int)sys_getpid(),
vuser->vuid);
slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1,
SESSION_TEMPLATE, (long unsigned int)sys_getpid(),
vuser->vuid);
tdb_store_flag = TDB_REPLACE;
}
/* If 'hostname lookup' == yes, then do the DNS lookup. This is
needed because utmp and PAM both expect DNS names
client_name() handles this case internally.
*/
hostname = client_name();
if (strcmp(hostname, "UNKNOWN") == 0) {
hostname = client_addr();
}
fstrcpy(sessionid.username, vuser->user.unix_name);
fstrcpy(sessionid.hostname, hostname);
sessionid.id_num = i; /* Only valid for utmp sessions */
sessionid.pid = pid;
sessionid.uid = vuser->uid;
sessionid.gid = vuser->gid;
fstrcpy(sessionid.remote_machine, get_remote_machine_name());
fstrcpy(sessionid.ip_addr, client_addr());
sessionid.connect_start = time(NULL);
client_ip = client_inaddr(&sa);
if (!smb_pam_claim_session(sessionid.username, sessionid.id_str, sessionid.hostname)) {
DEBUG(1,("pam_session rejected the session for %s [%s]\n",
sessionid.username, sessionid.id_str));
if (tdb_store_flag == TDB_MODIFY) {
tdb_delete_bystring(tdb, keystr);
}
return False;
}
data.dptr = (uint8 *)&sessionid;
data.dsize = sizeof(sessionid);
if (tdb_store_bystring(tdb, keystr, data, tdb_store_flag) != 0) {
DEBUG(1,("session_claim: unable to create session id record\n"));
return False;
}
if (lp_utmp()) {
sys_utmp_claim(sessionid.username, sessionid.hostname,
client_ip,
sessionid.id_str, sessionid.id_num);
}
vuser->session_keystr = talloc_strdup(vuser, keystr);
if (!vuser->session_keystr) {
DEBUG(0, ("session_claim: talloc_strdup() failed for session_keystr\n"));
return False;
}
return True;
}
/********************************************************************
called when a session is destroyed
********************************************************************/
void session_yield(user_struct *vuser)
{
TDB_DATA dbuf;
struct sessionid sessionid;
struct in_addr *client_ip;
if (!tdb) return;
if (!vuser->session_keystr) {
return;
}
dbuf = tdb_fetch_bystring(tdb, vuser->session_keystr);
if (dbuf.dsize != sizeof(sessionid))
return;
memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
client_ip = interpret_addr2(sessionid.ip_addr);
SAFE_FREE(dbuf.dptr);
if (lp_utmp()) {
sys_utmp_yield(sessionid.username, sessionid.hostname,
client_ip,
sessionid.id_str, sessionid.id_num);
}
smb_pam_close_session(sessionid.username, sessionid.id_str, sessionid.hostname);
tdb_delete_bystring(tdb, vuser->session_keystr);
}
/********************************************************************
********************************************************************/
BOOL session_traverse(int (*fn)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *),
void *state)
{
if (!session_init()) {
DEBUG(3, ("No tdb opened\n"));
return False;
}
tdb_traverse(tdb, fn, state);
return True;
}
/********************************************************************
********************************************************************/
struct session_list {
TALLOC_CTX *mem_ctx;
int count;
struct sessionid *sessions;
};
static int gather_sessioninfo(TDB_CONTEXT *stdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
{
uint32 i;
struct session_list *sesslist = (struct session_list *) state;
const struct sessionid *current = (const struct sessionid *) dbuf.dptr;
i = sesslist->count;
sesslist->sessions = TALLOC_REALLOC_ARRAY(
sesslist->mem_ctx, sesslist->sessions, struct sessionid, i+1);
if (!sesslist->sessions) {
sesslist->count = 0;
return -1;
}
memcpy(&sesslist->sessions[i], current, sizeof(struct sessionid));
sesslist->count++;
DEBUG(7,("gather_sessioninfo session from %s@%s\n",
current->username, current->remote_machine));
return 0;
}
/********************************************************************
********************************************************************/
int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
{
struct session_list sesslist;
sesslist.mem_ctx = mem_ctx;
sesslist.count = 0;
sesslist.sessions = NULL;
if (!session_traverse(gather_sessioninfo, (void *) &sesslist)) {
DEBUG(3, ("Session traverse failed\n"));
SAFE_FREE(sesslist.sessions);
*session_list = NULL;
return 0;
}
*session_list = sesslist.sessions;
return sesslist.count;
}
|