2 Unix SMB/CIFS implementation.
3 session handling for utmp and PAM
5 Copyright (C) tridge@samba.org 2001
6 Copyright (C) abartlet@samba.org 2001
7 Copyright (C) Gerald (Jerry) Carter 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* a "session" is claimed when we do a SessionSetupX operation
24 and is yielded when the corresponding vuid is destroyed.
26 sessions are used to populate utmp and PAM session structures
30 #include "smbd/smbd.h"
31 #include "smbd/globals.h"
32 #include "dbwrap/dbwrap.h"
35 #include "../lib/tsocket/tsocket.h"
36 #include "../libcli/security/security.h"
39 /********************************************************************
40 called when a session is created
41 ********************************************************************/
43 bool session_claim(struct smbd_server_connection
*sconn
, struct user_struct
*vuser
)
45 struct server_id pid
= messaging_server_id(sconn
->msg_ctx
);
48 struct sessionid sessionid
;
50 struct db_record
*rec
;
54 vuser
->session_keystr
= NULL
;
56 /* don't register sessions for the guest user - its just too
57 expensive to go through pam session code for browsing etc */
58 if (security_session_user_level(vuser
->session_info
, NULL
) < SECURITY_USER
) {
62 if (!sessionid_init()) {
66 ZERO_STRUCT(sessionid
);
73 for (i
=1;i
<MAX_SESSION_ID
;i
++) {
76 * This is very inefficient and needs fixing -- vl
79 struct server_id sess_pid
;
82 snprintf(keystr
, sizeof(keystr
), "ID/%d", i
);
84 rec
= sessionid_fetch_record(NULL
, keystr
);
86 DEBUG(1, ("Could not lock \"%s\"\n", keystr
));
90 value
= dbwrap_record_get_value(rec
);
92 if (value
.dsize
!= sizeof(sessionid
)) {
93 DEBUG(1, ("Re-using invalid record\n"));
99 + offsetof(struct sessionid
, pid
),
102 if (!process_exists(sess_pid
)) {
103 DEBUG(5, ("%s has died -- re-using session\n",
104 procid_str_static(&sess_pid
)));
111 if (i
== MAX_SESSION_ID
) {
112 SMB_ASSERT(rec
== NULL
);
113 DEBUG(1,("session_claim: out of session IDs "
114 "(max is %d)\n", MAX_SESSION_ID
));
118 snprintf(sessionid
.id_str
, sizeof(sessionid
.id_str
),
119 SESSION_UTMP_TEMPLATE
, i
);
122 snprintf(keystr
, sizeof(keystr
), "ID/%s/%llu",
123 procid_str_static(&pid
),
124 (unsigned long long)vuser
->vuid
);
126 rec
= sessionid_fetch_record(NULL
, keystr
);
128 DEBUG(1, ("Could not lock \"%s\"\n", keystr
));
132 snprintf(sessionid
.id_str
, sizeof(sessionid
.id_str
),
133 SESSION_TEMPLATE
, (long unsigned int)getpid(),
134 (unsigned long long)vuser
->vuid
);
137 SMB_ASSERT(rec
!= NULL
);
139 raddr
= tsocket_address_inet_addr_string(sconn
->remote_address
,
145 /* Make clear that we require the optional unix_token in the source3 code */
146 SMB_ASSERT(vuser
->session_info
->unix_token
);
148 fstrcpy(sessionid
.username
, vuser
->session_info
->unix_info
->unix_name
);
149 fstrcpy(sessionid
.hostname
, sconn
->remote_hostname
);
150 sessionid
.id_num
= i
; /* Only valid for utmp sessions */
152 sessionid
.uid
= vuser
->session_info
->unix_token
->uid
;
153 sessionid
.gid
= vuser
->session_info
->unix_token
->gid
;
154 fstrcpy(sessionid
.remote_machine
, get_remote_machine_name());
155 fstrcpy(sessionid
.ip_addr_str
, raddr
);
156 sessionid
.connect_start
= time(NULL
);
158 if (!smb_pam_claim_session(sessionid
.username
, sessionid
.id_str
,
159 sessionid
.hostname
)) {
160 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
161 sessionid
.username
, sessionid
.id_str
));
167 data
.dptr
= (uint8
*)&sessionid
;
168 data
.dsize
= sizeof(sessionid
);
170 status
= dbwrap_record_store(rec
, data
, TDB_REPLACE
);
174 if (!NT_STATUS_IS_OK(status
)) {
175 DEBUG(1,("session_claim: unable to create session id "
176 "record: %s\n", nt_errstr(status
)));
181 sys_utmp_claim(sessionid
.username
, sessionid
.hostname
,
182 sessionid
.id_str
, sessionid
.id_num
);
185 vuser
->session_keystr
= talloc_strdup(vuser
, keystr
);
186 if (!vuser
->session_keystr
) {
187 DEBUG(0, ("session_claim: talloc_strdup() failed for session_keystr\n"));
193 /********************************************************************
194 called when a session is destroyed
195 ********************************************************************/
197 void session_yield(struct user_struct
*vuser
)
199 struct sessionid sessionid
;
200 struct db_record
*rec
;
203 if (!vuser
->session_keystr
) {
207 rec
= sessionid_fetch_record(NULL
, vuser
->session_keystr
);
212 value
= dbwrap_record_get_value(rec
);
214 if (value
.dsize
!= sizeof(sessionid
))
217 memcpy(&sessionid
, value
.dptr
, sizeof(sessionid
));
220 sys_utmp_yield(sessionid
.username
, sessionid
.hostname
,
221 sessionid
.id_str
, sessionid
.id_num
);
224 smb_pam_close_session(sessionid
.username
, sessionid
.id_str
,
227 dbwrap_record_delete(rec
);
232 /********************************************************************
233 ********************************************************************/
235 struct session_list
{
238 struct sessionid
*sessions
;
241 static int gather_sessioninfo(const char *key
, struct sessionid
*session
,
244 struct session_list
*sesslist
= (struct session_list
*)private_data
;
246 sesslist
->sessions
= talloc_realloc(
247 sesslist
->mem_ctx
, sesslist
->sessions
, struct sessionid
,
250 if (!sesslist
->sessions
) {
255 memcpy(&sesslist
->sessions
[sesslist
->count
], session
,
256 sizeof(struct sessionid
));
260 DEBUG(7, ("gather_sessioninfo session from %s@%s\n",
261 session
->username
, session
->remote_machine
));
266 /********************************************************************
267 ********************************************************************/
269 int list_sessions(TALLOC_CTX
*mem_ctx
, struct sessionid
**session_list
)
271 struct session_list sesslist
;
274 sesslist
.mem_ctx
= mem_ctx
;
276 sesslist
.sessions
= NULL
;
278 status
= sessionid_traverse_read(gather_sessioninfo
, (void *) &sesslist
);
279 if (!NT_STATUS_IS_OK(status
)) {
280 DEBUG(3, ("Session traverse failed\n"));
281 SAFE_FREE(sesslist
.sessions
);
282 *session_list
= NULL
;
286 *session_list
= sesslist
.sessions
;
287 return sesslist
.count
;