new files for head
[kai/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 #if defined(WITH_PAM) || defined(WITH_UTMP)
32
33 static TDB_CONTEXT *tdb;
34 struct sessionid {
35         fstring username;
36         fstring hostname;
37         fstring id_str;
38         uint32  id_num;
39         uint32  pid;
40 };
41
42 /* called when a session is created */
43 BOOL session_claim(uint16 vuid)
44 {
45         user_struct *vuser = get_valid_user_struct(vuid);
46         int i;
47         TDB_DATA data;
48         struct sessionid sessionid;
49         pstring dbuf;
50         int dlen;
51         uint32 pid = (uint32)sys_getpid();
52         TDB_DATA key;           
53         fstring keystr;
54
55         vuser->session_id = 0;
56
57         /* don't register sessions for the guest user - its just too
58            expensive to go through pam session code for browsing etc */
59         if (strequal(vuser->user.unix_name,lp_guestaccount(-1))) {
60                 return True;
61         }
62
63         if (!tdb) {
64                 tdb = tdb_open(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST, 
65                                O_RDWR | O_CREAT, 0644);
66                 if (!tdb) {
67                         DEBUG(1,("session_claim: failed to open sessionid tdb\n"));
68                         return False;
69                 }
70         }
71
72         ZERO_STRUCT(sessionid);
73
74         data.dptr = NULL;
75         data.dsize = 0;
76
77         for (i=1;i<MAX_SESSION_ID;i++) {
78                 slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
79                 key.dptr = keystr;
80                 key.dsize = strlen(keystr)+1;
81
82                 if (tdb_store(tdb, key, data, TDB_INSERT) == 0) break;
83         }
84
85         if (i == MAX_SESSION_ID) {
86                 DEBUG(1,("session_claim: out of session IDs (max is %d)\n", 
87                          MAX_SESSION_ID));
88                 return False;
89         }
90
91         fstrcpy(sessionid.username, vuser->user.unix_name);
92         fstrcpy(sessionid.hostname, lp_utmp_hostname());
93         slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, SESSION_TEMPLATE, i);
94         sessionid.id_num = i;
95         sessionid.pid = pid;
96
97         dlen = tdb_pack(dbuf, sizeof(dbuf), "fffdd",
98                         sessionid.username, sessionid.hostname, sessionid.id_str,
99                         sessionid.id_num, sessionid.pid);
100
101         data.dptr = dbuf;
102         data.dsize = dlen;
103         if (tdb_store(tdb, key, data, TDB_MODIFY) != 0) {
104                 DEBUG(1,("session_claim: unable to create session id record\n"));
105                 return False;
106         }
107
108 #if WITH_PAM
109         if (!pam_session(True, sessionid.username, sessionid.id_str)) {
110                 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
111                          sessionid.username, sessionid.id_str));
112                 tdb_delete(tdb, key);
113                 return False;
114         }
115 #endif
116
117 #if WITH_UTMP   
118         if (lp_utmp()) {
119                 sys_utmp_claim(sessionid.username, sessionid.hostname, 
120                                sessionid.id_str, sessionid.id_num);
121         }
122 #endif
123
124         vuser->session_id = i;
125         return True;
126 }
127
128 /* called when a session is destroyed */
129 void session_yield(uint16 vuid)
130 {
131         user_struct *vuser = get_valid_user_struct(vuid);
132         TDB_DATA data;
133         struct sessionid sessionid;
134         TDB_DATA key;           
135         fstring keystr;
136
137         if (!tdb) return;
138
139         if (vuser->session_id == 0) {
140                 return;
141         }
142
143         slprintf(keystr, sizeof(keystr)-1, "ID/%d", vuser->session_id);
144
145         key.dptr = keystr;
146         key.dsize = strlen(keystr)+1;
147
148         data = tdb_fetch(tdb, key);
149         if (data.dptr == NULL) {
150                 return;
151         }
152
153         tdb_unpack(data.dptr, data.dsize, "fffdd",
154                    &sessionid.username, &sessionid.hostname, &sessionid.id_str,
155                    &sessionid.id_num, &sessionid.pid);
156
157 #if WITH_UTMP   
158         if (lp_utmp()) {
159                 sys_utmp_yield(sessionid.username, sessionid.hostname, 
160                                sessionid.id_str, sessionid.id_num);
161         }
162 #endif
163
164 #if WITH_PAM
165         pam_session(False, sessionid.username, sessionid.id_str);
166 #endif
167
168         tdb_delete(tdb, key);
169 }
170
171 #else
172  /* null functions - no session support needed */
173  BOOL session_claim(uint16 vuid) { return True; }
174  void session_yield(uint16 vuid) {} 
175 #endif