Don't resolve the hostname in smbd as we can pause for a long time while
[samba.git] / source3 / smbd / session.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4    session handling for utmp and PAM
5    Copyright (C) tridge@samba.org 2001
6    Copyright (C) abartlet@pcug.org.au 2001
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 extern fstring remote_machine;
32
33 static TDB_CONTEXT *tdb;
34 /* called when a session is created */
35 BOOL session_claim(user_struct *vuser)
36 {
37         int i;
38         TDB_DATA data;
39         struct sessionid sessionid;
40         uint32 pid = (uint32)sys_getpid();
41         TDB_DATA key;           
42         fstring keystr;
43         char * hostname;
44
45         vuser->session_id = 0;
46
47         /* don't register sessions for the guest user - its just too
48            expensive to go through pam session code for browsing etc */
49         if (vuser->guest) {
50                 return True;
51         }
52
53         if (!tdb) {
54                 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
55                                O_RDWR | O_CREAT, 0644);
56                 if (!tdb) {
57                         DEBUG(1,("session_claim: failed to open sessionid tdb\n"));
58                         return False;
59                 }
60         }
61
62         ZERO_STRUCT(sessionid);
63
64         data.dptr = NULL;
65         data.dsize = 0;
66
67         for (i=1;i<MAX_SESSION_ID;i++) {
68                 slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
69                 key.dptr = keystr;
70                 key.dsize = strlen(keystr)+1;
71
72                 if (tdb_store(tdb, key, data, TDB_INSERT) == 0) break;
73         }
74
75         if (i == MAX_SESSION_ID) {
76                 DEBUG(1,("session_claim: out of session IDs (max is %d)\n", 
77                          MAX_SESSION_ID));
78                 return False;
79         }
80
81         /* Don't resolve the hostname in smbd as we can pause for a long
82            time while waiting for DNS timeouts to occur.  The correct
83            place to do this is in the code that displays the session
84            information. */
85
86         hostname = client_addr();
87
88         fstrcpy(sessionid.username, vuser->user.unix_name);
89         fstrcpy(sessionid.hostname, hostname);
90         slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, SESSION_TEMPLATE, i);
91         sessionid.id_num = i;
92         sessionid.pid = pid;
93         sessionid.uid = vuser->uid;
94         sessionid.gid = vuser->gid;
95         fstrcpy(sessionid.remote_machine, remote_machine);
96         fstrcpy(sessionid.ip_addr, client_addr());
97
98         if (!smb_pam_claim_session(sessionid.username, sessionid.id_str, sessionid.hostname)) {
99                 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
100                                 sessionid.username, sessionid.id_str));
101                 tdb_delete(tdb, key);
102                 return False;
103         }
104
105         data.dptr = (char *)&sessionid;
106         data.dsize = sizeof(sessionid);
107         if (tdb_store(tdb, key, data, TDB_MODIFY) != 0) {
108                 DEBUG(1,("session_claim: unable to create session id record\n"));
109                 return False;
110         }
111
112 #if WITH_UTMP   
113         if (lp_utmp()) {
114                 sys_utmp_claim(sessionid.username, sessionid.hostname, 
115                                sessionid.id_str, sessionid.id_num);
116         }
117 #endif
118
119         vuser->session_id = i;
120         return True;
121 }
122
123 /* called when a session is destroyed */
124 void session_yield(user_struct *vuser)
125 {
126         TDB_DATA dbuf;
127         struct sessionid sessionid;
128         TDB_DATA key;           
129         fstring keystr;
130
131         if (!tdb) return;
132
133         if (vuser->session_id == 0) {
134                 return;
135         }
136
137         slprintf(keystr, sizeof(keystr)-1, "ID/%d", vuser->session_id);
138
139         key.dptr = keystr;
140         key.dsize = strlen(keystr)+1;
141
142         dbuf = tdb_fetch(tdb, key);
143
144         if (dbuf.dsize != sizeof(sessionid))
145                 return;
146
147         memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
148
149         SAFE_FREE(dbuf.dptr);
150
151 #if WITH_UTMP   
152         if (lp_utmp()) {
153                 sys_utmp_yield(sessionid.username, sessionid.hostname, 
154                                sessionid.id_str, sessionid.id_num);
155         }
156 #endif
157
158         smb_pam_close_session(sessionid.username, sessionid.id_str, sessionid.hostname);
159
160         tdb_delete(tdb, key);
161 }
162
163 BOOL session_traverse(int (*fn)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *), void *state)
164 {
165   if (!tdb) {
166     DEBUG(3, ("No tdb opened\n"));
167     return False;
168   }
169
170   tdb_traverse(tdb, fn, state);
171   return True;
172 }
173
174
175