2 Unix SMB/Netbios implementation.
4 session handling for utmp and PAM
5 Copyright (C) tridge@samba.org 2001
6 Copyright (C) abartlet@pcug.org.au 2001
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.
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.
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.
23 /* a "session" is claimed when we do a SessionSetupX operation
24 and is yielded when the corresponding vuid is destroyed.
26 sessions are used to populate utmp and PAM session structures
31 extern fstring remote_machine;
33 static TDB_CONTEXT *tdb;
34 /* called when a session is created */
35 BOOL session_claim(uint16 vuid)
37 user_struct *vuser = get_valid_user_struct(vuid);
40 struct sessionid sessionid;
43 uint32 pid = (uint32)sys_getpid();
48 vuser->session_id = 0;
50 /* don't register sessions for the guest user - its just too
51 expensive to go through pam session code for browsing etc */
52 if (strequal(vuser->user.unix_name,lp_guestaccount(-1))) {
57 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST|USE_TDB_MMAP_FLAG,
58 O_RDWR | O_CREAT, 0644);
60 DEBUG(1,("session_claim: failed to open sessionid tdb\n"));
65 ZERO_STRUCT(sessionid);
70 for (i=1;i<MAX_SESSION_ID;i++) {
71 slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
73 key.dsize = strlen(keystr)+1;
75 if (tdb_store(tdb, key, data, TDB_INSERT) == 0) break;
78 if (i == MAX_SESSION_ID) {
79 DEBUG(1,("session_claim: out of session IDs (max is %d)\n",
84 hostname = client_name();
85 if (strequal(hostname,"UNKNOWN"))
86 hostname = client_addr();
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);
93 sessionid.uid = vuser->uid;
94 sessionid.gid = vuser->gid;
95 fstrcpy(sessionid.remote_machine, remote_machine);
96 fstrcpy(sessionid.ip_addr, client_addr());
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);
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"));
114 sys_utmp_claim(sessionid.username, sessionid.hostname,
115 sessionid.id_str, sessionid.id_num);
119 vuser->session_id = i;
123 /* called when a session is destroyed */
124 void session_yield(uint16 vuid)
126 user_struct *vuser = get_valid_user_struct(vuid);
128 struct sessionid sessionid;
134 if (vuser->session_id == 0) {
138 slprintf(keystr, sizeof(keystr)-1, "ID/%d", vuser->session_id);
141 key.dsize = strlen(keystr)+1;
143 dbuf = tdb_fetch(tdb, key);
145 if (dbuf.dsize != sizeof(sessionid))
148 memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
150 safe_free(dbuf.dptr);
155 sys_utmp_yield(sessionid.username, sessionid.hostname,
156 sessionid.id_str, sessionid.id_num);
160 smb_pam_close_session(sessionid.username, sessionid.id_str, sessionid.hostname);
162 tdb_delete(tdb, key);