s3:smbd: use session_global_id as session number for pam and utmp
[Samba/gebeck_regimport.git] / source3 / smbd / session.c
blob6b0263e12233eba376a325678acb06e8868e1bc5
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 struct sessionid sessionid;
50 fstring keystr;
51 struct db_record *rec;
52 NTSTATUS status;
53 char *raddr;
55 vuser->session_keystr = NULL;
57 /* don't register sessions for the guest user - its just too
58 expensive to go through pam session code for browsing etc */
59 if (security_session_user_level(vuser->session_info, NULL) < SECURITY_USER) {
60 return True;
63 if (!sessionid_init()) {
64 return False;
67 ZERO_STRUCT(sessionid);
69 sessionid.id_num = session->global->session_global_id;
71 data.dptr = NULL;
72 data.dsize = 0;
74 snprintf(keystr, sizeof(keystr), "ID/%u", sessionid.id_num);
75 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
76 "smb/%u", sessionid.id_num);
78 rec = sessionid_fetch_record(NULL, keystr);
79 if (rec == NULL) {
80 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
81 return False;
84 raddr = tsocket_address_inet_addr_string(session->connection->remote_address,
85 talloc_tos());
86 if (raddr == NULL) {
87 return false;
90 /* Make clear that we require the optional unix_token in the source3 code */
91 SMB_ASSERT(vuser->session_info->unix_token);
93 fstrcpy(sessionid.username, vuser->session_info->unix_info->unix_name);
94 fstrcpy(sessionid.hostname, sconn->remote_hostname);
95 sessionid.pid = pid;
96 sessionid.uid = vuser->session_info->unix_token->uid;
97 sessionid.gid = vuser->session_info->unix_token->gid;
98 fstrcpy(sessionid.remote_machine, get_remote_machine_name());
99 fstrcpy(sessionid.ip_addr_str, raddr);
100 sessionid.connect_start = time(NULL);
102 if (!smb_pam_claim_session(sessionid.username, sessionid.id_str,
103 sessionid.hostname)) {
104 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
105 sessionid.username, sessionid.id_str));
107 TALLOC_FREE(rec);
108 return False;
111 data.dptr = (uint8 *)&sessionid;
112 data.dsize = sizeof(sessionid);
114 status = dbwrap_record_store(rec, data, TDB_REPLACE);
116 TALLOC_FREE(rec);
118 if (!NT_STATUS_IS_OK(status)) {
119 DEBUG(1,("session_claim: unable to create session id "
120 "record: %s\n", nt_errstr(status)));
121 return False;
124 if (lp_utmp()) {
125 sys_utmp_claim(sessionid.username, sessionid.hostname,
126 sessionid.id_str, sessionid.id_num);
129 vuser->session_keystr = talloc_strdup(vuser, keystr);
130 if (!vuser->session_keystr) {
131 DEBUG(0, ("session_claim: talloc_strdup() failed for session_keystr\n"));
132 return False;
134 return True;
137 /********************************************************************
138 called when a session is destroyed
139 ********************************************************************/
141 void session_yield(struct smbXsrv_session *session)
143 struct user_struct *vuser = session->compat;
144 struct sessionid sessionid;
145 struct db_record *rec;
146 TDB_DATA value;
148 if (!vuser->session_keystr) {
149 return;
152 rec = sessionid_fetch_record(NULL, vuser->session_keystr);
153 if (rec == NULL) {
154 return;
157 value = dbwrap_record_get_value(rec);
159 if (value.dsize != sizeof(sessionid))
160 return;
162 memcpy(&sessionid, value.dptr, sizeof(sessionid));
164 if (lp_utmp()) {
165 sys_utmp_yield(sessionid.username, sessionid.hostname,
166 sessionid.id_str, sessionid.id_num);
169 smb_pam_close_session(sessionid.username, sessionid.id_str,
170 sessionid.hostname);
172 dbwrap_record_delete(rec);
174 TALLOC_FREE(rec);
177 /********************************************************************
178 ********************************************************************/
180 struct session_list {
181 TALLOC_CTX *mem_ctx;
182 int count;
183 struct sessionid *sessions;
186 static int gather_sessioninfo(const char *key, struct sessionid *session,
187 void *private_data)
189 struct session_list *sesslist = (struct session_list *)private_data;
191 sesslist->sessions = talloc_realloc(
192 sesslist->mem_ctx, sesslist->sessions, struct sessionid,
193 sesslist->count+1);
195 if (!sesslist->sessions) {
196 sesslist->count = 0;
197 return -1;
200 memcpy(&sesslist->sessions[sesslist->count], session,
201 sizeof(struct sessionid));
203 sesslist->count++;
205 DEBUG(7, ("gather_sessioninfo session from %s@%s\n",
206 session->username, session->remote_machine));
208 return 0;
211 /********************************************************************
212 ********************************************************************/
214 int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
216 struct session_list sesslist;
217 NTSTATUS status;
219 sesslist.mem_ctx = mem_ctx;
220 sesslist.count = 0;
221 sesslist.sessions = NULL;
223 status = sessionid_traverse_read(gather_sessioninfo, (void *) &sesslist);
224 if (!NT_STATUS_IS_OK(status)) {
225 DEBUG(3, ("Session traverse failed\n"));
226 SAFE_FREE(sesslist.sessions);
227 *session_list = NULL;
228 return 0;
231 *session_list = sesslist.sessions;
232 return sesslist.count;