9b8d11cc65207ec6ca5c61b0adade358f94153f9
[amitay/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.h"
33 #include "session.h"
34 #include "auth.h"
35 #include "../lib/tsocket/tsocket.h"
36
37 /********************************************************************
38  called when a session is created
39 ********************************************************************/
40
41 bool session_claim(struct smbd_server_connection *sconn, user_struct *vuser)
42 {
43         struct server_id pid = sconn_server_id(sconn);
44         TDB_DATA data;
45         int i = 0;
46         struct sessionid sessionid;
47         fstring keystr;
48         struct db_record *rec;
49         NTSTATUS status;
50         char *raddr;
51
52         vuser->session_keystr = NULL;
53
54         /* don't register sessions for the guest user - its just too
55            expensive to go through pam session code for browsing etc */
56         if (vuser->session_info->unix_info->guest) {
57                 return True;
58         }
59
60         if (!sessionid_init()) {
61                 return False;
62         }
63
64         ZERO_STRUCT(sessionid);
65
66         data.dptr = NULL;
67         data.dsize = 0;
68
69         if (lp_utmp()) {
70
71                 for (i=1;i<MAX_SESSION_ID;i++) {
72
73                         /*
74                          * This is very inefficient and needs fixing -- vl
75                          */
76
77                         struct server_id sess_pid;
78
79                         snprintf(keystr, sizeof(keystr), "ID/%d", i);
80
81                         rec = sessionid_fetch_record(NULL, keystr);
82                         if (rec == NULL) {
83                                 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
84                                 return False;
85                         }
86
87                         if (rec->value.dsize != sizeof(sessionid)) {
88                                 DEBUG(1, ("Re-using invalid record\n"));
89                                 break;
90                         }
91
92                         memcpy(&sess_pid,
93                                ((char *)rec->value.dptr)
94                                + offsetof(struct sessionid, pid),
95                                sizeof(sess_pid));
96
97                         if (!process_exists(sess_pid)) {
98                                 DEBUG(5, ("%s has died -- re-using session\n",
99                                           procid_str_static(&sess_pid)));
100                                 break;
101                         }
102
103                         TALLOC_FREE(rec);
104                 }
105
106                 if (i == MAX_SESSION_ID) {
107                         SMB_ASSERT(rec == NULL);
108                         DEBUG(1,("session_claim: out of session IDs "
109                                  "(max is %d)\n", MAX_SESSION_ID));
110                         return False;
111                 }
112
113                 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
114                          SESSION_UTMP_TEMPLATE, i);
115         } else
116         {
117                 snprintf(keystr, sizeof(keystr), "ID/%s/%u",
118                          procid_str_static(&pid), vuser->vuid);
119
120                 rec = sessionid_fetch_record(NULL, keystr);
121                 if (rec == NULL) {
122                         DEBUG(1, ("Could not lock \"%s\"\n", keystr));
123                         return False;
124                 }
125
126                 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
127                          SESSION_TEMPLATE, (long unsigned int)sys_getpid(),
128                          vuser->vuid);
129         }
130
131         SMB_ASSERT(rec != NULL);
132
133         raddr = tsocket_address_inet_addr_string(sconn->remote_address,
134                                                  talloc_tos());
135         if (raddr == NULL) {
136                 return false;
137         }
138
139         /* Make clear that we require the optional unix_token in the source3 code */
140         SMB_ASSERT(vuser->session_info->unix_token);
141
142         fstrcpy(sessionid.username, vuser->session_info->unix_info->unix_name);
143         fstrcpy(sessionid.hostname, sconn->remote_hostname);
144         sessionid.id_num = i;  /* Only valid for utmp sessions */
145         sessionid.pid = pid;
146         sessionid.uid = vuser->session_info->unix_token->uid;
147         sessionid.gid = vuser->session_info->unix_token->gid;
148         fstrcpy(sessionid.remote_machine, get_remote_machine_name());
149         fstrcpy(sessionid.ip_addr_str, raddr);
150         sessionid.connect_start = time(NULL);
151
152         if (!smb_pam_claim_session(sessionid.username, sessionid.id_str,
153                                    sessionid.hostname)) {
154                 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
155                                 sessionid.username, sessionid.id_str));
156
157                 TALLOC_FREE(rec);
158                 return False;
159         }
160
161         data.dptr = (uint8 *)&sessionid;
162         data.dsize = sizeof(sessionid);
163
164         status = rec->store(rec, data, TDB_REPLACE);
165
166         TALLOC_FREE(rec);
167
168         if (!NT_STATUS_IS_OK(status)) {
169                 DEBUG(1,("session_claim: unable to create session id "
170                          "record: %s\n", nt_errstr(status)));
171                 return False;
172         }
173
174         if (lp_utmp()) {
175                 sys_utmp_claim(sessionid.username, sessionid.hostname,
176                                sessionid.ip_addr_str,
177                                sessionid.id_str, sessionid.id_num);
178         }
179
180         vuser->session_keystr = talloc_strdup(vuser, keystr);
181         if (!vuser->session_keystr) {
182                 DEBUG(0, ("session_claim:  talloc_strdup() failed for session_keystr\n"));
183                 return False;
184         }
185         return True;
186 }
187
188 /********************************************************************
189  called when a session is destroyed
190 ********************************************************************/
191
192 void session_yield(user_struct *vuser)
193 {
194         struct sessionid sessionid;
195         struct db_record *rec;
196
197         if (!vuser->session_keystr) {
198                 return;
199         }
200
201         rec = sessionid_fetch_record(NULL, vuser->session_keystr);
202         if (rec == NULL) {
203                 return;
204         }
205
206         if (rec->value.dsize != sizeof(sessionid))
207                 return;
208
209         memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
210
211         if (lp_utmp()) {
212                 sys_utmp_yield(sessionid.username, sessionid.hostname, 
213                                sessionid.ip_addr_str,
214                                sessionid.id_str, sessionid.id_num);
215         }
216
217         smb_pam_close_session(sessionid.username, sessionid.id_str,
218                               sessionid.hostname);
219
220         rec->delete_rec(rec);
221
222         TALLOC_FREE(rec);
223 }
224
225 /********************************************************************
226 ********************************************************************/
227
228 struct session_list {
229         TALLOC_CTX *mem_ctx;
230         int count;
231         struct sessionid *sessions;
232 };
233
234 static int gather_sessioninfo(const char *key, struct sessionid *session,
235                               void *private_data)
236 {
237         struct session_list *sesslist = (struct session_list *)private_data;
238
239         sesslist->sessions = talloc_realloc(
240                 sesslist->mem_ctx, sesslist->sessions, struct sessionid,
241                 sesslist->count+1);
242
243         if (!sesslist->sessions) {
244                 sesslist->count = 0;
245                 return -1;
246         }
247
248         memcpy(&sesslist->sessions[sesslist->count], session,
249                sizeof(struct sessionid));
250
251         sesslist->count++;
252
253         DEBUG(7, ("gather_sessioninfo session from %s@%s\n",
254                   session->username, session->remote_machine));
255
256         return 0;
257 }
258
259 /********************************************************************
260 ********************************************************************/
261
262 int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
263 {
264         struct session_list sesslist;
265         int ret;
266
267         sesslist.mem_ctx = mem_ctx;
268         sesslist.count = 0;
269         sesslist.sessions = NULL;
270
271         ret = sessionid_traverse_read(gather_sessioninfo, (void *) &sesslist);
272         if (ret < 0) {
273                 DEBUG(3, ("Session traverse failed\n"));
274                 SAFE_FREE(sesslist.sessions);
275                 *session_list = NULL;
276                 return 0;
277         }
278
279         *session_list = sesslist.sessions;
280         return sesslist.count;
281 }