Fix up some unused variables and functions, fix up formatting
[samba.git] / source / 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(uint16 vuid)
36 {
37         user_struct *vuser = get_valid_user_struct(vuid);
38         int i;
39         TDB_DATA data;
40         struct sessionid sessionid;
41         uint32 pid = (uint32)sys_getpid();
42         TDB_DATA key;           
43         fstring keystr;
44         char * hostname;
45
46         vuser->session_id = 0;
47
48         /* don't register sessions for the guest user - its just too
49            expensive to go through pam session code for browsing etc */
50         if (strequal(vuser->user.unix_name,lp_guestaccount(-1))) {
51                 return True;
52         }
53
54         if (!tdb) {
55                 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST|USE_TDB_MMAP_FLAG, 
56                                O_RDWR | O_CREAT, 0644);
57                 if (!tdb) {
58                         DEBUG(1,("session_claim: failed to open sessionid tdb\n"));
59                         return False;
60                 }
61         }
62
63         ZERO_STRUCT(sessionid);
64
65         data.dptr = NULL;
66         data.dsize = 0;
67
68         for (i=1;i<MAX_SESSION_ID;i++) {
69                 slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
70                 key.dptr = keystr;
71                 key.dsize = strlen(keystr)+1;
72
73                 if (tdb_store(tdb, key, data, TDB_INSERT) == 0) break;
74         }
75
76         if (i == MAX_SESSION_ID) {
77                 DEBUG(1,("session_claim: out of session IDs (max is %d)\n", 
78                          MAX_SESSION_ID));
79                 return False;
80         }
81
82         hostname = client_name();
83         if (strequal(hostname,"UNKNOWN"))
84                 hostname = client_addr();
85
86         fstrcpy(sessionid.username, vuser->user.unix_name);
87         fstrcpy(sessionid.hostname, hostname);
88         slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, SESSION_TEMPLATE, i);
89         sessionid.id_num = i;
90         sessionid.pid = pid;
91         sessionid.uid = vuser->uid;
92         sessionid.gid = vuser->gid;
93         fstrcpy(sessionid.remote_machine, remote_machine);
94         fstrcpy(sessionid.ip_addr, client_addr());
95
96         if (!smb_pam_claim_session(sessionid.username, sessionid.id_str, sessionid.hostname)) {
97                 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
98                                 sessionid.username, sessionid.id_str));
99                 tdb_delete(tdb, key);
100                 return False;
101         }
102
103         data.dptr = (char *)&sessionid;
104         data.dsize = sizeof(sessionid);
105         if (tdb_store(tdb, key, data, TDB_MODIFY) != 0) {
106                 DEBUG(1,("session_claim: unable to create session id record\n"));
107                 return False;
108         }
109
110 #if WITH_UTMP   
111         if (lp_utmp()) {
112                 sys_utmp_claim(sessionid.username, sessionid.hostname, 
113                                sessionid.id_str, sessionid.id_num);
114         }
115 #endif
116
117         vuser->session_id = i;
118         return True;
119 }
120
121 /* called when a session is destroyed */
122 void session_yield(uint16 vuid)
123 {
124         user_struct *vuser = get_valid_user_struct(vuid);
125         TDB_DATA dbuf;
126         struct sessionid sessionid;
127         TDB_DATA key;           
128         fstring keystr;
129
130         if (!tdb) return;
131
132         if (vuser->session_id == 0) {
133                 return;
134         }
135
136         slprintf(keystr, sizeof(keystr)-1, "ID/%d", vuser->session_id);
137
138         key.dptr = keystr;
139         key.dsize = strlen(keystr)+1;
140
141         dbuf = tdb_fetch(tdb, key);
142
143         if (dbuf.dsize != sizeof(sessionid))
144                 return;
145
146         memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
147
148         safe_free(dbuf.dptr);
149         dbuf.dptr = NULL;
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