s3:smbd: remove smbd_server_connection argument from session_claim()
[Samba/gebeck_regimport.git] / source3 / smbd / session.c
blobabb1e316bca67addd3b112ea2339cbdbc4686a2e
1 /*
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
29 #include "includes.h"
30 #include "smbd/smbd.h"
31 #include "smbd/globals.h"
32 #include "dbwrap/dbwrap.h"
33 #include "session.h"
34 #include "auth.h"
35 #include "../lib/tsocket/tsocket.h"
36 #include "../libcli/security/security.h"
37 #include "messages.h"
39 /********************************************************************
40 called when a session is created
41 ********************************************************************/
43 bool session_claim(struct smbXsrv_session *session)
45 struct user_struct *vuser = session->compat;
46 struct smbd_server_connection *sconn = session->connection->sconn;
47 struct server_id pid = messaging_server_id(sconn->msg_ctx);
48 TDB_DATA data;
49 int i = 0;
50 struct sessionid sessionid;
51 fstring keystr;
52 struct db_record *rec;
53 NTSTATUS status;
54 char *raddr;
56 vuser->session_keystr = NULL;
58 /* don't register sessions for the guest user - its just too
59 expensive to go through pam session code for browsing etc */
60 if (security_session_user_level(vuser->session_info, NULL) < SECURITY_USER) {
61 return True;
64 if (!sessionid_init()) {
65 return False;
68 ZERO_STRUCT(sessionid);
70 data.dptr = NULL;
71 data.dsize = 0;
73 if (lp_utmp()) {
75 for (i=1;i<MAX_SESSION_ID;i++) {
78 * This is very inefficient and needs fixing -- vl
81 struct server_id sess_pid;
82 TDB_DATA value;
84 snprintf(keystr, sizeof(keystr), "ID/%d", i);
86 rec = sessionid_fetch_record(NULL, keystr);
87 if (rec == NULL) {
88 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
89 return False;
92 value = dbwrap_record_get_value(rec);
94 if (value.dsize != sizeof(sessionid)) {
95 DEBUG(1, ("Re-using invalid record\n"));
96 break;
99 memcpy(&sess_pid,
100 ((char *)value.dptr)
101 + offsetof(struct sessionid, pid),
102 sizeof(sess_pid));
104 if (!process_exists(sess_pid)) {
105 DEBUG(5, ("%s has died -- re-using session\n",
106 procid_str_static(&sess_pid)));
107 break;
110 TALLOC_FREE(rec);
113 if (i == MAX_SESSION_ID) {
114 SMB_ASSERT(rec == NULL);
115 DEBUG(1,("session_claim: out of session IDs "
116 "(max is %d)\n", MAX_SESSION_ID));
117 return False;
120 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
121 SESSION_UTMP_TEMPLATE, i);
122 } else
124 snprintf(keystr, sizeof(keystr), "ID/%s/%llu",
125 procid_str_static(&pid),
126 (unsigned long long)vuser->vuid);
128 rec = sessionid_fetch_record(NULL, keystr);
129 if (rec == NULL) {
130 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
131 return False;
134 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
135 SESSION_TEMPLATE, (long unsigned int)getpid(),
136 (unsigned long long)vuser->vuid);
139 SMB_ASSERT(rec != NULL);
141 raddr = tsocket_address_inet_addr_string(session->connection->remote_address,
142 talloc_tos());
143 if (raddr == NULL) {
144 return false;
147 /* Make clear that we require the optional unix_token in the source3 code */
148 SMB_ASSERT(vuser->session_info->unix_token);
150 fstrcpy(sessionid.username, vuser->session_info->unix_info->unix_name);
151 fstrcpy(sessionid.hostname, sconn->remote_hostname);
152 sessionid.id_num = i; /* Only valid for utmp sessions */
153 sessionid.pid = pid;
154 sessionid.uid = vuser->session_info->unix_token->uid;
155 sessionid.gid = vuser->session_info->unix_token->gid;
156 fstrcpy(sessionid.remote_machine, get_remote_machine_name());
157 fstrcpy(sessionid.ip_addr_str, raddr);
158 sessionid.connect_start = time(NULL);
160 if (!smb_pam_claim_session(sessionid.username, sessionid.id_str,
161 sessionid.hostname)) {
162 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
163 sessionid.username, sessionid.id_str));
165 TALLOC_FREE(rec);
166 return False;
169 data.dptr = (uint8 *)&sessionid;
170 data.dsize = sizeof(sessionid);
172 status = dbwrap_record_store(rec, data, TDB_REPLACE);
174 TALLOC_FREE(rec);
176 if (!NT_STATUS_IS_OK(status)) {
177 DEBUG(1,("session_claim: unable to create session id "
178 "record: %s\n", nt_errstr(status)));
179 return False;
182 if (lp_utmp()) {
183 sys_utmp_claim(sessionid.username, sessionid.hostname,
184 sessionid.id_str, sessionid.id_num);
187 vuser->session_keystr = talloc_strdup(vuser, keystr);
188 if (!vuser->session_keystr) {
189 DEBUG(0, ("session_claim: talloc_strdup() failed for session_keystr\n"));
190 return False;
192 return True;
195 /********************************************************************
196 called when a session is destroyed
197 ********************************************************************/
199 void session_yield(struct smbXsrv_session *session)
201 struct user_struct *vuser = session->compat;
202 struct sessionid sessionid;
203 struct db_record *rec;
204 TDB_DATA value;
206 if (!vuser->session_keystr) {
207 return;
210 rec = sessionid_fetch_record(NULL, vuser->session_keystr);
211 if (rec == NULL) {
212 return;
215 value = dbwrap_record_get_value(rec);
217 if (value.dsize != sizeof(sessionid))
218 return;
220 memcpy(&sessionid, value.dptr, sizeof(sessionid));
222 if (lp_utmp()) {
223 sys_utmp_yield(sessionid.username, sessionid.hostname,
224 sessionid.id_str, sessionid.id_num);
227 smb_pam_close_session(sessionid.username, sessionid.id_str,
228 sessionid.hostname);
230 dbwrap_record_delete(rec);
232 TALLOC_FREE(rec);
235 /********************************************************************
236 ********************************************************************/
238 struct session_list {
239 TALLOC_CTX *mem_ctx;
240 int count;
241 struct sessionid *sessions;
244 static int gather_sessioninfo(const char *key, struct sessionid *session,
245 void *private_data)
247 struct session_list *sesslist = (struct session_list *)private_data;
249 sesslist->sessions = talloc_realloc(
250 sesslist->mem_ctx, sesslist->sessions, struct sessionid,
251 sesslist->count+1);
253 if (!sesslist->sessions) {
254 sesslist->count = 0;
255 return -1;
258 memcpy(&sesslist->sessions[sesslist->count], session,
259 sizeof(struct sessionid));
261 sesslist->count++;
263 DEBUG(7, ("gather_sessioninfo session from %s@%s\n",
264 session->username, session->remote_machine));
266 return 0;
269 /********************************************************************
270 ********************************************************************/
272 int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
274 struct session_list sesslist;
275 NTSTATUS status;
277 sesslist.mem_ctx = mem_ctx;
278 sesslist.count = 0;
279 sesslist.sessions = NULL;
281 status = sessionid_traverse_read(gather_sessioninfo, (void *) &sesslist);
282 if (!NT_STATUS_IS_OK(status)) {
283 DEBUG(3, ("Session traverse failed\n"));
284 SAFE_FREE(sesslist.sessions);
285 *session_list = NULL;
286 return 0;
289 *session_list = sesslist.sessions;
290 return sesslist.count;