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