s4-smbtorture: add ndr test for nbt_netlogon_packet to avoid future regressions.
[Samba/gebeck_regimport.git] / source3 / smbd / session.c
blob0797d1b41f26c947b4b225abea9b1d73f0387e00
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"
38 /********************************************************************
39 called when a session is created
40 ********************************************************************/
42 bool session_claim(struct smbd_server_connection *sconn, user_struct *vuser)
44 struct server_id pid = sconn_server_id(sconn);
45 TDB_DATA data;
46 int i = 0;
47 struct sessionid sessionid;
48 fstring keystr;
49 struct db_record *rec;
50 NTSTATUS status;
51 char *raddr;
53 vuser->session_keystr = NULL;
55 /* don't register sessions for the guest user - its just too
56 expensive to go through pam session code for browsing etc */
57 if (security_session_user_level(vuser->session_info, NULL) < SECURITY_USER) {
58 return True;
61 if (!sessionid_init()) {
62 return False;
65 ZERO_STRUCT(sessionid);
67 data.dptr = NULL;
68 data.dsize = 0;
70 if (lp_utmp()) {
72 for (i=1;i<MAX_SESSION_ID;i++) {
75 * This is very inefficient and needs fixing -- vl
78 struct server_id sess_pid;
79 TDB_DATA value;
81 snprintf(keystr, sizeof(keystr), "ID/%d", i);
83 rec = sessionid_fetch_record(NULL, keystr);
84 if (rec == NULL) {
85 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
86 return False;
89 value = dbwrap_record_get_value(rec);
91 if (value.dsize != sizeof(sessionid)) {
92 DEBUG(1, ("Re-using invalid record\n"));
93 break;
96 memcpy(&sess_pid,
97 ((char *)value.dptr)
98 + offsetof(struct sessionid, pid),
99 sizeof(sess_pid));
101 if (!process_exists(sess_pid)) {
102 DEBUG(5, ("%s has died -- re-using session\n",
103 procid_str_static(&sess_pid)));
104 break;
107 TALLOC_FREE(rec);
110 if (i == MAX_SESSION_ID) {
111 SMB_ASSERT(rec == NULL);
112 DEBUG(1,("session_claim: out of session IDs "
113 "(max is %d)\n", MAX_SESSION_ID));
114 return False;
117 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
118 SESSION_UTMP_TEMPLATE, i);
119 } else
121 snprintf(keystr, sizeof(keystr), "ID/%s/%u",
122 procid_str_static(&pid), vuser->vuid);
124 rec = sessionid_fetch_record(NULL, keystr);
125 if (rec == NULL) {
126 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
127 return False;
130 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
131 SESSION_TEMPLATE, (long unsigned int)sys_getpid(),
132 vuser->vuid);
135 SMB_ASSERT(rec != NULL);
137 raddr = tsocket_address_inet_addr_string(sconn->remote_address,
138 talloc_tos());
139 if (raddr == NULL) {
140 return false;
143 /* Make clear that we require the optional unix_token in the source3 code */
144 SMB_ASSERT(vuser->session_info->unix_token);
146 fstrcpy(sessionid.username, vuser->session_info->unix_info->unix_name);
147 fstrcpy(sessionid.hostname, sconn->remote_hostname);
148 sessionid.id_num = i; /* Only valid for utmp sessions */
149 sessionid.pid = pid;
150 sessionid.uid = vuser->session_info->unix_token->uid;
151 sessionid.gid = vuser->session_info->unix_token->gid;
152 fstrcpy(sessionid.remote_machine, get_remote_machine_name());
153 fstrcpy(sessionid.ip_addr_str, raddr);
154 sessionid.connect_start = time(NULL);
156 if (!smb_pam_claim_session(sessionid.username, sessionid.id_str,
157 sessionid.hostname)) {
158 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
159 sessionid.username, sessionid.id_str));
161 TALLOC_FREE(rec);
162 return False;
165 data.dptr = (uint8 *)&sessionid;
166 data.dsize = sizeof(sessionid);
168 status = dbwrap_record_store(rec, data, TDB_REPLACE);
170 TALLOC_FREE(rec);
172 if (!NT_STATUS_IS_OK(status)) {
173 DEBUG(1,("session_claim: unable to create session id "
174 "record: %s\n", nt_errstr(status)));
175 return False;
178 if (lp_utmp()) {
179 sys_utmp_claim(sessionid.username, sessionid.hostname,
180 sessionid.ip_addr_str,
181 sessionid.id_str, sessionid.id_num);
184 vuser->session_keystr = talloc_strdup(vuser, keystr);
185 if (!vuser->session_keystr) {
186 DEBUG(0, ("session_claim: talloc_strdup() failed for session_keystr\n"));
187 return False;
189 return True;
192 /********************************************************************
193 called when a session is destroyed
194 ********************************************************************/
196 void session_yield(user_struct *vuser)
198 struct sessionid sessionid;
199 struct db_record *rec;
200 TDB_DATA value;
202 if (!vuser->session_keystr) {
203 return;
206 rec = sessionid_fetch_record(NULL, vuser->session_keystr);
207 if (rec == NULL) {
208 return;
211 value = dbwrap_record_get_value(rec);
213 if (value.dsize != sizeof(sessionid))
214 return;
216 memcpy(&sessionid, value.dptr, sizeof(sessionid));
218 if (lp_utmp()) {
219 sys_utmp_yield(sessionid.username, sessionid.hostname,
220 sessionid.ip_addr_str,
221 sessionid.id_str, sessionid.id_num);
224 smb_pam_close_session(sessionid.username, sessionid.id_str,
225 sessionid.hostname);
227 dbwrap_record_delete(rec);
229 TALLOC_FREE(rec);
232 /********************************************************************
233 ********************************************************************/
235 struct session_list {
236 TALLOC_CTX *mem_ctx;
237 int count;
238 struct sessionid *sessions;
241 static int gather_sessioninfo(const char *key, struct sessionid *session,
242 void *private_data)
244 struct session_list *sesslist = (struct session_list *)private_data;
246 sesslist->sessions = talloc_realloc(
247 sesslist->mem_ctx, sesslist->sessions, struct sessionid,
248 sesslist->count+1);
250 if (!sesslist->sessions) {
251 sesslist->count = 0;
252 return -1;
255 memcpy(&sesslist->sessions[sesslist->count], session,
256 sizeof(struct sessionid));
258 sesslist->count++;
260 DEBUG(7, ("gather_sessioninfo session from %s@%s\n",
261 session->username, session->remote_machine));
263 return 0;
266 /********************************************************************
267 ********************************************************************/
269 int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
271 struct session_list sesslist;
272 NTSTATUS status;
274 sesslist.mem_ctx = mem_ctx;
275 sesslist.count = 0;
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;
283 return 0;
286 *session_list = sesslist.sessions;
287 return sesslist.count;