c7cdf41fb5b0ed19c4feb1512e6ea68252d91fa7
[samba.git] / source / 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
31 /********************************************************************
32 ********************************************************************/
33
34 static struct db_context *session_db_ctx(void)
35 {
36         static struct db_context *ctx;
37
38         if (ctx)
39                 return ctx;
40
41         ctx = db_open(NULL, lock_path("sessionid.tdb"), 0,
42                       TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
43                       O_RDWR | O_CREAT, 0644);
44         return ctx;
45 }
46
47 BOOL session_init(void)
48 {
49         if (session_db_ctx() == NULL) {
50                 DEBUG(1,("session_init: failed to open sessionid tdb\n"));
51                 return False;
52         }
53
54         return True;
55 }
56
57 /********************************************************************
58  called when a session is created
59 ********************************************************************/
60
61 BOOL session_claim(user_struct *vuser)
62 {
63         TDB_DATA key, data;
64         int i = 0;
65         struct sessionid sessionid;
66         struct server_id pid = procid_self();
67         fstring keystr;
68         char * hostname;
69         struct db_context *ctx;
70         struct db_record *rec;
71         NTSTATUS status;
72
73         vuser->session_keystr = NULL;
74
75         /* don't register sessions for the guest user - its just too
76            expensive to go through pam session code for browsing etc */
77         if (vuser->guest) {
78                 return True;
79         }
80
81         if (!(ctx = session_db_ctx())) {
82                 return False;
83         }
84
85         ZERO_STRUCT(sessionid);
86
87         data.dptr = NULL;
88         data.dsize = 0;
89
90         if (lp_utmp()) {
91
92                 for (i=1;i<MAX_SESSION_ID;i++) {
93
94                         /*
95                          * This is very inefficient and needs fixing -- vl
96                          */
97
98                         struct server_id sess_pid;
99
100                         snprintf(keystr, sizeof(keystr), "ID/%d", i);
101                         key = string_term_tdb_data(keystr);
102
103                         rec = ctx->fetch_locked(ctx, NULL, key);
104
105                         if (rec == NULL) {
106                                 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
107                                 return False;
108                         }
109
110                         if (rec->value.dsize != sizeof(sessionid)) {
111                                 DEBUG(1, ("Re-using invalid record\n"));
112                                 break;
113                         }
114
115                         sess_pid = ((struct sessionid *)rec->value.dptr)->pid;
116
117                         if (!process_exists(sess_pid)) {
118                                 DEBUG(5, ("%s has died -- re-using session\n",
119                                           procid_str_static(&sess_pid)));
120                                 break;
121                         }
122
123                         TALLOC_FREE(rec);
124                 }
125
126                 if (i == MAX_SESSION_ID) {
127                         SMB_ASSERT(rec == NULL);
128                         DEBUG(1,("session_claim: out of session IDs "
129                                  "(max is %d)\n", MAX_SESSION_ID));
130                         return False;
131                 }
132
133                 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
134                          SESSION_UTMP_TEMPLATE, i);
135         } else
136         {
137                 snprintf(keystr, sizeof(keystr), "ID/%s/%u",
138                          procid_str_static(&pid), vuser->vuid);
139                 key = string_term_tdb_data(keystr);
140
141                 rec = ctx->fetch_locked(ctx, NULL, key);
142
143                 if (rec == NULL) {
144                         DEBUG(1, ("Could not lock \"%s\"\n", keystr));
145                         return False;
146                 }
147
148                 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
149                          SESSION_TEMPLATE, (long unsigned int)sys_getpid(),
150                          vuser->vuid);
151         }
152
153         SMB_ASSERT(rec != NULL);
154
155         /* If 'hostname lookup' == yes, then do the DNS lookup.  This is
156            needed because utmp and PAM both expect DNS names
157
158            client_name() handles this case internally.
159         */
160
161         hostname = client_name();
162         if (strcmp(hostname, "UNKNOWN") == 0) {
163                 hostname = client_addr();
164         }
165
166         fstrcpy(sessionid.username, vuser->user.unix_name);
167         fstrcpy(sessionid.hostname, hostname);
168         sessionid.id_num = i;  /* Only valid for utmp sessions */
169         sessionid.pid = pid;
170         sessionid.uid = vuser->uid;
171         sessionid.gid = vuser->gid;
172         fstrcpy(sessionid.remote_machine, get_remote_machine_name());
173         fstrcpy(sessionid.ip_addr_str, client_addr());
174         sessionid.connect_start = time(NULL);
175
176         if (!smb_pam_claim_session(sessionid.username, sessionid.id_str,
177                                    sessionid.hostname)) {
178                 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
179                                 sessionid.username, sessionid.id_str));
180
181                 TALLOC_FREE(rec);
182                 return False;
183         }
184
185         data.dptr = (uint8 *)&sessionid;
186         data.dsize = sizeof(sessionid);
187
188         status = rec->store(rec, data, TDB_REPLACE);
189
190         TALLOC_FREE(rec);
191
192         if (!NT_STATUS_IS_OK(status)) {
193                 DEBUG(1,("session_claim: unable to create session id "
194                          "record: %s\n", nt_errstr(status)));
195                 return False;
196         }
197
198         if (lp_utmp()) {
199                 sys_utmp_claim(sessionid.username, sessionid.hostname,
200                                sessionid.ip_addr_str,
201                                sessionid.id_str, sessionid.id_num);
202         }
203
204         TALLOC_FREE(rec);
205
206         vuser->session_keystr = talloc_strdup(vuser, keystr);
207         if (!vuser->session_keystr) {
208                 DEBUG(0, ("session_claim:  talloc_strdup() failed for "
209                           "session_keystr\n"));
210                 return False;
211         }
212         return True;
213 }
214
215 /********************************************************************
216  called when a session is destroyed
217 ********************************************************************/
218
219 void session_yield(user_struct *vuser)
220 {
221         TDB_DATA key;
222         struct sessionid sessionid;
223         struct db_context *ctx;
224         struct db_record *rec;
225
226         if (!(ctx = session_db_ctx())) return;
227
228         if (!vuser->session_keystr) {
229                 return;
230         }
231
232         key = string_term_tdb_data(vuser->session_keystr);
233
234         if (!(rec = ctx->fetch_locked(ctx, NULL, key))) {
235                 return;
236         }
237
238         if (rec->value.dsize != sizeof(sessionid))
239                 return;
240
241         memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
242
243         if (lp_utmp()) {
244                 sys_utmp_yield(sessionid.username, sessionid.hostname, 
245                                sessionid.ip_addr_str,
246                                sessionid.id_str, sessionid.id_num);
247         }
248
249         smb_pam_close_session(sessionid.username, sessionid.id_str,
250                               sessionid.hostname);
251
252         rec->delete_rec(rec);
253
254         TALLOC_FREE(rec);
255 }
256
257 /********************************************************************
258 ********************************************************************/
259
260 static BOOL session_traverse(int (*fn)(struct db_record *db,
261                                        void *private_data),
262                              void *private_data)
263 {
264         struct db_context *ctx;
265
266         if (!(ctx = session_db_ctx())) {
267                 DEBUG(3, ("No tdb opened\n"));
268                 return False;
269         }
270
271         ctx->traverse_read(ctx, fn, private_data);
272         return True;
273 }
274
275 /********************************************************************
276 ********************************************************************/
277
278 struct session_list {
279         TALLOC_CTX *mem_ctx;
280         int count;
281         struct sessionid *sessions;
282 };
283
284 static int gather_sessioninfo(struct db_record *rec, void *state)
285 {
286         struct session_list *sesslist = (struct session_list *) state;
287         const struct sessionid *current =
288                 (const struct sessionid *) rec->value.dptr;
289
290         sesslist->sessions = TALLOC_REALLOC_ARRAY(
291                 sesslist->mem_ctx, sesslist->sessions, struct sessionid,
292                 sesslist->count+1);
293
294         if (!sesslist->sessions) {
295                 sesslist->count = 0;
296                 return -1;
297         }
298
299         memcpy(&sesslist->sessions[sesslist->count], current,
300                sizeof(struct sessionid));
301
302         sesslist->count++;
303
304         DEBUG(7,("gather_sessioninfo session from %s@%s\n", 
305                  current->username, current->remote_machine));
306
307         return 0;
308 }
309
310 /********************************************************************
311 ********************************************************************/
312
313 int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
314 {
315         struct session_list sesslist;
316
317         sesslist.mem_ctx = mem_ctx;
318         sesslist.count = 0;
319         sesslist.sessions = NULL;
320         
321         if (!session_traverse(gather_sessioninfo, (void *) &sesslist)) {
322                 DEBUG(3, ("Session traverse failed\n"));
323                 SAFE_FREE(sesslist.sessions);
324                 *session_list = NULL;
325                 return 0;
326         }
327
328         *session_list = sesslist.sessions;
329         return sesslist.count;
330 }