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
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.
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.
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.
22 /* a "session" is claimed when we do a SessionSetupX operation
23 and is yielded when the corresponding vuid is destroyed.
25 sessions are used to populate utmp and PAM session structures
30 extern fstring remote_machine;
32 static TDB_CONTEXT *tdb;
33 /* called when a session is created */
34 BOOL session_claim(user_struct *vuser)
38 struct sessionid sessionid;
39 uint32 pid = (uint32)sys_getpid();
44 vuser->session_id = 0;
46 /* don't register sessions for the guest user - its just too
47 expensive to go through pam session code for browsing etc */
53 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
54 O_RDWR | O_CREAT, 0644);
56 DEBUG(1,("session_claim: failed to open sessionid tdb\n"));
61 ZERO_STRUCT(sessionid);
66 for (i=1;i<MAX_SESSION_ID;i++) {
67 slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
69 key.dsize = strlen(keystr)+1;
71 if (tdb_store(tdb, key, data, TDB_INSERT) == 0) break;
74 if (i == MAX_SESSION_ID) {
75 DEBUG(1,("session_claim: out of session IDs (max is %d)\n",
80 /* Don't resolve the hostname in smbd as we can pause for a long
81 time while waiting for DNS timeouts to occur. The correct
82 place to do this is in the code that displays the session
85 hostname = client_addr();
87 fstrcpy(sessionid.username, vuser->user.unix_name);
88 fstrcpy(sessionid.hostname, hostname);
89 slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, SESSION_TEMPLATE, i);
92 sessionid.uid = vuser->uid;
93 sessionid.gid = vuser->gid;
94 fstrcpy(sessionid.remote_machine, remote_machine);
95 fstrcpy(sessionid.ip_addr, client_addr());
97 if (!smb_pam_claim_session(sessionid.username, sessionid.id_str, sessionid.hostname)) {
98 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
99 sessionid.username, sessionid.id_str));
100 tdb_delete(tdb, key);
104 data.dptr = (char *)&sessionid;
105 data.dsize = sizeof(sessionid);
106 if (tdb_store(tdb, key, data, TDB_MODIFY) != 0) {
107 DEBUG(1,("session_claim: unable to create session id record\n"));
113 sys_utmp_claim(sessionid.username, sessionid.hostname,
114 sessionid.id_str, sessionid.id_num);
118 vuser->session_id = i;
122 /* called when a session is destroyed */
123 void session_yield(user_struct *vuser)
126 struct sessionid sessionid;
132 if (vuser->session_id == 0) {
136 slprintf(keystr, sizeof(keystr)-1, "ID/%d", vuser->session_id);
139 key.dsize = strlen(keystr)+1;
141 dbuf = tdb_fetch(tdb, key);
143 if (dbuf.dsize != sizeof(sessionid))
146 memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
148 SAFE_FREE(dbuf.dptr);
152 sys_utmp_yield(sessionid.username, sessionid.hostname,
153 sessionid.id_str, sessionid.id_num);
157 smb_pam_close_session(sessionid.username, sessionid.id_str, sessionid.hostname);
159 tdb_delete(tdb, key);
162 BOOL session_traverse(int (*fn)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *), void *state)
165 DEBUG(3, ("No tdb opened\n"));
169 tdb_traverse(tdb, fn, state);