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 static TDB_CONTEXT
*tdb
;
31 /* called when a session is created */
32 BOOL
session_claim(user_struct
*vuser
)
37 struct in_addr
*client_ip
;
38 struct sessionid sessionid
;
39 uint32 pid
= (uint32
)sys_getpid();
43 int tdb_store_flag
; /* If using utmp, we do an inital 'lock hold' store,
44 but we don't need this if we are just using the
45 (unique) pid/vuid combination */
47 vuser
->session_keystr
= NULL
;
49 /* don't register sessions for the guest user - its just too
50 expensive to go through pam session code for browsing etc */
56 tdb
= tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST
|TDB_DEFAULT
,
57 O_RDWR
| O_CREAT
, 0644);
59 DEBUG(1,("session_claim: failed to open sessionid tdb\n"));
64 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",
83 slprintf(sessionid
.id_str
, sizeof(sessionid
.id_str
)-1, SESSION_UTMP_TEMPLATE
, i
);
84 tdb_store_flag
= TDB_MODIFY
;
87 slprintf(keystr
, sizeof(keystr
)-1, "ID/%lu/%u",
88 (long unsigned int)sys_getpid(),
90 slprintf(sessionid
.id_str
, sizeof(sessionid
.id_str
)-1,
91 SESSION_TEMPLATE
, (long unsigned int)sys_getpid(),
95 key
.dsize
= strlen(keystr
)+1;
97 tdb_store_flag
= TDB_REPLACE
;
100 /* If 'hostname lookup' == yes, then do the DNS lookup. This is
101 needed because utmp and PAM both expect DNS names
103 client_name() handles this case internally.
106 hostname
= client_name();
107 if (strcmp(hostname
, "UNKNOWN") == 0) {
108 hostname
= client_addr();
111 fstrcpy(sessionid
.username
, vuser
->user
.unix_name
);
112 fstrcpy(sessionid
.hostname
, hostname
);
113 sessionid
.id_num
= i
; /* Only valid for utmp sessions */
115 sessionid
.uid
= vuser
->uid
;
116 sessionid
.gid
= vuser
->gid
;
117 fstrcpy(sessionid
.remote_machine
, get_remote_machine_name());
118 fstrcpy(sessionid
.ip_addr
, client_addr());
120 client_ip
= client_inaddr(&sa
);
122 if (!smb_pam_claim_session(sessionid
.username
, sessionid
.id_str
, sessionid
.hostname
)) {
123 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
124 sessionid
.username
, sessionid
.id_str
));
125 if (tdb_store_flag
== TDB_MODIFY
) {
126 tdb_delete(tdb
, key
);
131 data
.dptr
= (char *)&sessionid
;
132 data
.dsize
= sizeof(sessionid
);
133 if (tdb_store(tdb
, key
, data
, tdb_store_flag
) != 0) {
134 DEBUG(1,("session_claim: unable to create session id record\n"));
139 sys_utmp_claim(sessionid
.username
, sessionid
.hostname
,
141 sessionid
.id_str
, sessionid
.id_num
);
144 vuser
->session_keystr
= strdup(keystr
);
145 if (!vuser
->session_keystr
) {
146 DEBUG(0, ("session_claim: strdup() failed for session_keystr\n"));
152 /* called when a session is destroyed */
153 void session_yield(user_struct
*vuser
)
156 struct sessionid sessionid
;
157 struct in_addr
*client_ip
;
162 if (!vuser
->session_keystr
) {
166 key
.dptr
= vuser
->session_keystr
;
167 key
.dsize
= strlen(vuser
->session_keystr
)+1;
169 dbuf
= tdb_fetch(tdb
, key
);
171 if (dbuf
.dsize
!= sizeof(sessionid
))
174 memcpy(&sessionid
, dbuf
.dptr
, sizeof(sessionid
));
176 client_ip
= interpret_addr2(sessionid
.ip_addr
);
178 SAFE_FREE(dbuf
.dptr
);
181 sys_utmp_yield(sessionid
.username
, sessionid
.hostname
,
183 sessionid
.id_str
, sessionid
.id_num
);
186 smb_pam_close_session(sessionid
.username
, sessionid
.id_str
, sessionid
.hostname
);
188 tdb_delete(tdb
, key
);
191 static BOOL
session_traverse(int (*fn
)(TDB_CONTEXT
*, TDB_DATA
, TDB_DATA
, void *), void *state
)
194 DEBUG(3, ("No tdb opened\n"));
198 tdb_traverse(tdb
, fn
, state
);
202 struct session_list
{
204 struct sessionid
*sessions
;
207 static int gather_sessioninfo(TDB_CONTEXT
*stdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
210 struct session_list
*sesslist
= (struct session_list
*) state
;
211 const struct sessionid
*current
= (const struct sessionid
*) dbuf
.dptr
;
213 sesslist
->count
+= 1;
214 sesslist
->sessions
= REALLOC(sesslist
->sessions
, sesslist
->count
*
215 sizeof(struct sessionid
));
217 memcpy(&sesslist
->sessions
[sesslist
->count
- 1], current
,
218 sizeof(struct sessionid
));
219 DEBUG(7,("gather_sessioninfo session from %s@%s\n",
220 current
->username
, current
->remote_machine
));
224 int list_sessions(struct sessionid
**session_list
)
226 struct session_list sesslist
;
229 sesslist
.sessions
= NULL
;
231 if (!session_traverse(gather_sessioninfo
, (void *) &sesslist
)) {
232 DEBUG(3, ("Session traverse failed\n"));
233 SAFE_FREE(sesslist
.sessions
);
234 *session_list
= NULL
;
238 *session_list
= sesslist
.sessions
;
239 return sesslist
.count
;