Another item off my long-term todo list:
[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 = 0;
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         int tdb_store_flag;  /* If using utmp, we do an inital 'lock hold' store,
44                                 but we don't need this if we are just using the 
45                                 (unique) pid/vuid combination */
46
47         vuser->session_keystr = NULL;
48
49         /* don't register sessions for the guest user - its just too
50            expensive to go through pam session code for browsing etc */
51         if (vuser->guest) {
52                 return True;
53         }
54
55         if (!tdb) {
56                 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
57                                O_RDWR | O_CREAT, 0644);
58                 if (!tdb) {
59                         DEBUG(1,("session_claim: failed to open sessionid tdb\n"));
60                         return False;
61                 }
62         }
63
64         ZERO_STRUCT(sessionid);
65
66         data.dptr = NULL;
67         data.dsize = 0;
68
69 #if WITH_UTMP
70         if (lp_utmp()) {
71                 for (i=1;i<MAX_SESSION_ID;i++) {
72                         slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
73                         key.dptr = keystr;
74                         key.dsize = strlen(keystr)+1;
75                         
76                         if (tdb_store(tdb, key, data, TDB_INSERT) == 0) break;
77                 }
78                 
79                 if (i == MAX_SESSION_ID) {
80                         DEBUG(1,("session_claim: out of session IDs (max is %d)\n", 
81                                  MAX_SESSION_ID));
82                         return False;
83                 }
84                 slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, SESSION_UTMP_TEMPLATE, i);
85                 tdb_store_flag = TDB_MODIFY;
86         } else
87 #endif
88         {
89                 slprintf(keystr, sizeof(keystr)-1, "ID/%lu/%u", 
90                          (long unsigned int)sys_getpid(), 
91                          vuser->vuid);
92                 slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, 
93                          SESSION_TEMPLATE, (long unsigned int)sys_getpid(), 
94                          vuser->vuid);
95
96                 key.dptr = keystr;
97                 key.dsize = strlen(keystr)+1;
98                         
99                 tdb_store_flag = TDB_REPLACE;
100         }
101
102         /* If 'hostname lookup' == yes, then do the DNS lookup.  This is
103            needed becouse utmp and PAM both expect DNS names 
104            
105            client_name() handles this case internally.
106         */
107
108         hostname = client_name();
109         if (strcmp(hostname, "UNKNOWN") == 0) {
110                 hostname = client_addr();
111         }
112
113         fstrcpy(sessionid.username, vuser->user.unix_name);
114         fstrcpy(sessionid.hostname, hostname);
115         sessionid.id_num = i;  /* Only valid for utmp sessions */
116         sessionid.pid = pid;
117         sessionid.uid = vuser->uid;
118         sessionid.gid = vuser->gid;
119         fstrcpy(sessionid.remote_machine, remote_machine);
120         fstrcpy(sessionid.ip_addr, client_addr());
121
122         if (!smb_pam_claim_session(sessionid.username, sessionid.id_str, sessionid.hostname)) {
123                 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
124                                 sessionid.username, sessionid.id_str));
125                 if (tdb_store_flag == TDB_MODIFY) {
126                         tdb_delete(tdb, key);
127                 }
128                 return False;
129         }
130
131         data.dptr = (char *)&sessionid;
132         data.dsize = sizeof(sessionid);
133         if (tdb_store(tdb, key, data, tdb_store_flag) != 0) {
134                 DEBUG(1,("session_claim: unable to create session id record\n"));
135                 return False;
136         }
137
138 #if WITH_UTMP   
139         if (lp_utmp()) {
140                 sys_utmp_claim(sessionid.username, sessionid.hostname, 
141                                sessionid.id_str, sessionid.id_num);
142         }
143 #endif
144
145         vuser->session_keystr = strdup(keystr);
146         if (!vuser->session_keystr) {
147                 DEBUG(0, ("session_claim:  strdup() failed for session_keystr\n"));
148                 return False;
149         }
150         return True;
151 }
152
153 /* called when a session is destroyed */
154 void session_yield(user_struct *vuser)
155 {
156         TDB_DATA dbuf;
157         struct sessionid sessionid;
158         TDB_DATA key;           
159
160         if (!tdb) return;
161
162         if (!vuser->session_keystr) {
163                 return;
164         }
165
166         key.dptr = vuser->session_keystr;
167         key.dsize = strlen(vuser->session_keystr)+1;
168
169         dbuf = tdb_fetch(tdb, key);
170
171         if (dbuf.dsize != sizeof(sessionid))
172                 return;
173
174         memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
175
176         SAFE_FREE(dbuf.dptr);
177
178 #if WITH_UTMP   
179         if (lp_utmp()) {
180                 sys_utmp_yield(sessionid.username, sessionid.hostname, 
181                                sessionid.id_str, sessionid.id_num);
182         }
183 #endif
184
185         smb_pam_close_session(sessionid.username, sessionid.id_str, sessionid.hostname);
186
187         tdb_delete(tdb, key);
188 }
189
190 static BOOL session_traverse(int (*fn)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *), void *state)
191 {
192         if (!tdb) {
193                 DEBUG(3, ("No tdb opened\n"));
194                 return False;
195         }
196
197         tdb_traverse(tdb, fn, state);
198         return True;
199 }
200
201 struct session_list {
202         int count;
203         struct sessionid *sessions;
204 };
205
206 static int gather_sessioninfo(TDB_CONTEXT *stdb, TDB_DATA kbuf, TDB_DATA dbuf,
207                               void *state)
208 {
209         struct session_list *sesslist = (struct session_list *) state;
210         const struct sessionid *current = (const struct sessionid *) dbuf.dptr;
211
212         sesslist->count += 1;
213         sesslist->sessions = REALLOC(sesslist->sessions, sesslist->count * 
214                                       sizeof(struct sessionid));
215
216         memcpy(&sesslist->sessions[sesslist->count - 1], current, 
217                sizeof(struct sessionid));
218         DEBUG(7,("gather_sessioninfo session from %s@%s\n", 
219                  current->username, current->remote_machine));
220         return 0;
221 }
222
223 int list_sessions(struct sessionid **session_list)
224 {
225         struct session_list sesslist;
226
227         sesslist.count = 0;
228         sesslist.sessions = NULL;
229         
230         if (!session_traverse(gather_sessioninfo, (void *) &sesslist)) {
231                 DEBUG(3, ("Session traverse failed\n"));
232                 SAFE_FREE(sesslist.sessions);
233                 *session_list = NULL;
234                 return 0;
235         }
236
237         *session_list = sesslist.sessions;
238         return sesslist.count;
239 }
240