1d89122ddd7a77258966a19478478687ff5d332f
[samba.git] / source3 / smbd / session.c
1 /* 
2    Unix SMB/CIFS implementation.
3    session handling for utmp and PAM
4
5    Copyright (C) tridge@samba.org       2001
6    Copyright (C) abartlet@samba.org     2001
7    Copyright (C) Gerald (Jerry) Carter  2006   
8    
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.
13    
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.
18    
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/>.
21 */
22
23 /* a "session" is claimed when we do a SessionSetupX operation
24    and is yielded when the corresponding vuid is destroyed.
25
26    sessions are used to populate utmp and PAM session structures
27 */
28
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"
38
39 /********************************************************************
40  called when a session is created
41 ********************************************************************/
42
43 bool session_claim(struct smbd_server_connection *sconn, user_struct *vuser)
44 {
45         struct server_id pid = messaging_server_id(sconn->msg_ctx);
46         TDB_DATA data;
47         int i = 0;
48         struct sessionid sessionid;
49         fstring keystr;
50         struct db_record *rec;
51         NTSTATUS status;
52         char *raddr;
53
54         vuser->session_keystr = NULL;
55
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) {
59                 return True;
60         }
61
62         if (!sessionid_init()) {
63                 return False;
64         }
65
66         ZERO_STRUCT(sessionid);
67
68         data.dptr = NULL;
69         data.dsize = 0;
70
71         if (lp_utmp()) {
72
73                 for (i=1;i<MAX_SESSION_ID;i++) {
74
75                         /*
76                          * This is very inefficient and needs fixing -- vl
77                          */
78
79                         struct server_id sess_pid;
80                         TDB_DATA value;
81
82                         snprintf(keystr, sizeof(keystr), "ID/%d", i);
83
84                         rec = sessionid_fetch_record(NULL, keystr);
85                         if (rec == NULL) {
86                                 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
87                                 return False;
88                         }
89
90                         value = dbwrap_record_get_value(rec);
91
92                         if (value.dsize != sizeof(sessionid)) {
93                                 DEBUG(1, ("Re-using invalid record\n"));
94                                 break;
95                         }
96
97                         memcpy(&sess_pid,
98                                ((char *)value.dptr)
99                                + offsetof(struct sessionid, pid),
100                                sizeof(sess_pid));
101
102                         if (!process_exists(sess_pid)) {
103                                 DEBUG(5, ("%s has died -- re-using session\n",
104                                           procid_str_static(&sess_pid)));
105                                 break;
106                         }
107
108                         TALLOC_FREE(rec);
109                 }
110
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));
115                         return False;
116                 }
117
118                 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
119                          SESSION_UTMP_TEMPLATE, i);
120         } else
121         {
122                 snprintf(keystr, sizeof(keystr), "ID/%s/%u",
123                          procid_str_static(&pid), vuser->vuid);
124
125                 rec = sessionid_fetch_record(NULL, keystr);
126                 if (rec == NULL) {
127                         DEBUG(1, ("Could not lock \"%s\"\n", keystr));
128                         return False;
129                 }
130
131                 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
132                          SESSION_TEMPLATE, (long unsigned int)sys_getpid(),
133                          vuser->vuid);
134         }
135
136         SMB_ASSERT(rec != NULL);
137
138         raddr = tsocket_address_inet_addr_string(sconn->remote_address,
139                                                  talloc_tos());
140         if (raddr == NULL) {
141                 return false;
142         }
143
144         /* Make clear that we require the optional unix_token in the source3 code */
145         SMB_ASSERT(vuser->session_info->unix_token);
146
147         fstrcpy(sessionid.username, vuser->session_info->unix_info->unix_name);
148         fstrcpy(sessionid.hostname, sconn->remote_hostname);
149         sessionid.id_num = i;  /* Only valid for utmp sessions */
150         sessionid.pid = pid;
151         sessionid.uid = vuser->session_info->unix_token->uid;
152         sessionid.gid = vuser->session_info->unix_token->gid;
153         fstrcpy(sessionid.remote_machine, get_remote_machine_name());
154         fstrcpy(sessionid.ip_addr_str, raddr);
155         sessionid.connect_start = time(NULL);
156
157         if (!smb_pam_claim_session(sessionid.username, sessionid.id_str,
158                                    sessionid.hostname)) {
159                 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
160                                 sessionid.username, sessionid.id_str));
161
162                 TALLOC_FREE(rec);
163                 return False;
164         }
165
166         data.dptr = (uint8 *)&sessionid;
167         data.dsize = sizeof(sessionid);
168
169         status = dbwrap_record_store(rec, data, TDB_REPLACE);
170
171         TALLOC_FREE(rec);
172
173         if (!NT_STATUS_IS_OK(status)) {
174                 DEBUG(1,("session_claim: unable to create session id "
175                          "record: %s\n", nt_errstr(status)));
176                 return False;
177         }
178
179         if (lp_utmp()) {
180                 sys_utmp_claim(sessionid.username, sessionid.hostname,
181                                sessionid.ip_addr_str,
182                                sessionid.id_str, sessionid.id_num);
183         }
184
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"));
188                 return False;
189         }
190         return True;
191 }
192
193 /********************************************************************
194  called when a session is destroyed
195 ********************************************************************/
196
197 void session_yield(user_struct *vuser)
198 {
199         struct sessionid sessionid;
200         struct db_record *rec;
201         TDB_DATA value;
202
203         if (!vuser->session_keystr) {
204                 return;
205         }
206
207         rec = sessionid_fetch_record(NULL, vuser->session_keystr);
208         if (rec == NULL) {
209                 return;
210         }
211
212         value = dbwrap_record_get_value(rec);
213
214         if (value.dsize != sizeof(sessionid))
215                 return;
216
217         memcpy(&sessionid, value.dptr, sizeof(sessionid));
218
219         if (lp_utmp()) {
220                 sys_utmp_yield(sessionid.username, sessionid.hostname, 
221                                sessionid.ip_addr_str,
222                                sessionid.id_str, sessionid.id_num);
223         }
224
225         smb_pam_close_session(sessionid.username, sessionid.id_str,
226                               sessionid.hostname);
227
228         dbwrap_record_delete(rec);
229
230         TALLOC_FREE(rec);
231 }
232
233 /********************************************************************
234 ********************************************************************/
235
236 struct session_list {
237         TALLOC_CTX *mem_ctx;
238         int count;
239         struct sessionid *sessions;
240 };
241
242 static int gather_sessioninfo(const char *key, struct sessionid *session,
243                               void *private_data)
244 {
245         struct session_list *sesslist = (struct session_list *)private_data;
246
247         sesslist->sessions = talloc_realloc(
248                 sesslist->mem_ctx, sesslist->sessions, struct sessionid,
249                 sesslist->count+1);
250
251         if (!sesslist->sessions) {
252                 sesslist->count = 0;
253                 return -1;
254         }
255
256         memcpy(&sesslist->sessions[sesslist->count], session,
257                sizeof(struct sessionid));
258
259         sesslist->count++;
260
261         DEBUG(7, ("gather_sessioninfo session from %s@%s\n",
262                   session->username, session->remote_machine));
263
264         return 0;
265 }
266
267 /********************************************************************
268 ********************************************************************/
269
270 int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
271 {
272         struct session_list sesslist;
273         NTSTATUS status;
274
275         sesslist.mem_ctx = mem_ctx;
276         sesslist.count = 0;
277         sesslist.sessions = NULL;
278
279         status = sessionid_traverse_read(gather_sessioninfo, (void *) &sesslist);
280         if (!NT_STATUS_IS_OK(status)) {
281                 DEBUG(3, ("Session traverse failed\n"));
282                 SAFE_FREE(sesslist.sessions);
283                 *session_list = NULL;
284                 return 0;
285         }
286
287         *session_list = sesslist.sessions;
288         return sesslist.count;
289 }