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 #if defined(WITH_PAM) || defined(WITH_UTMP)
33 static TDB_CONTEXT
*tdb
;
42 /* called when a session is created */
43 BOOL
session_claim(uint16 vuid
)
45 user_struct
*vuser
= get_valid_user_struct(vuid
);
48 struct sessionid sessionid
;
51 uint32 pid
= (uint32
)sys_getpid();
55 vuser
->session_id
= 0;
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))) {
64 tdb
= tdb_open(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST
,
65 O_RDWR
| O_CREAT
, 0644);
67 DEBUG(1,("session_claim: failed to open sessionid tdb\n"));
72 ZERO_STRUCT(sessionid
);
77 for (i
=1;i
<MAX_SESSION_ID
;i
++) {
78 slprintf(keystr
, sizeof(keystr
)-1, "ID/%d", i
);
80 key
.dsize
= strlen(keystr
)+1;
82 if (tdb_store(tdb
, key
, data
, TDB_INSERT
) == 0) break;
85 if (i
== MAX_SESSION_ID
) {
86 DEBUG(1,("session_claim: out of session IDs (max is %d)\n",
91 fstrcpy(sessionid
.username
, vuser
->user
.unix_name
);
93 fstrcpy(sessionid
.hostname
, lp_utmp_hostname());
96 extern fstring remote_machine
;
97 fstrcpy(sessionid
.hostname
, remote_machine
);
100 slprintf(sessionid
.id_str
, sizeof(sessionid
.id_str
)-1, SESSION_TEMPLATE
, i
);
101 sessionid
.id_num
= i
;
104 dlen
= tdb_pack(dbuf
, sizeof(dbuf
), "fffdd",
105 sessionid
.username
, sessionid
.hostname
, sessionid
.id_str
,
106 sessionid
.id_num
, sessionid
.pid
);
110 if (tdb_store(tdb
, key
, data
, TDB_MODIFY
) != 0) {
111 DEBUG(1,("session_claim: unable to create session id record\n"));
116 if (!pam_session(True
, sessionid
.username
, sessionid
.id_str
)) {
117 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
118 sessionid
.username
, sessionid
.id_str
));
119 tdb_delete(tdb
, key
);
126 sys_utmp_claim(sessionid
.username
, sessionid
.hostname
,
127 sessionid
.id_str
, sessionid
.id_num
);
131 vuser
->session_id
= i
;
135 /* called when a session is destroyed */
136 void session_yield(uint16 vuid
)
138 user_struct
*vuser
= get_valid_user_struct(vuid
);
140 struct sessionid sessionid
;
146 if (vuser
->session_id
== 0) {
150 slprintf(keystr
, sizeof(keystr
)-1, "ID/%d", vuser
->session_id
);
153 key
.dsize
= strlen(keystr
)+1;
155 data
= tdb_fetch(tdb
, key
);
156 if (data
.dptr
== NULL
) {
160 tdb_unpack(data
.dptr
, data
.dsize
, "fffdd",
161 &sessionid
.username
, &sessionid
.hostname
, &sessionid
.id_str
,
162 &sessionid
.id_num
, &sessionid
.pid
);
166 sys_utmp_yield(sessionid
.username
, sessionid
.hostname
,
167 sessionid
.id_str
, sessionid
.id_num
);
172 pam_session(False
, sessionid
.username
, sessionid
.id_str
);
175 tdb_delete(tdb
, key
);
179 /* null functions - no session support needed */
180 BOOL
session_claim(uint16 vuid
) { return True
; }
181 void session_yield(uint16 vuid
) {}