2 Unix SMB/CIFS implementation.
3 session handling for utmp and PAM
5 Copyright (C) tridge@samba.org 2001
6 Copyright (C) abartlet@samba.org 2001
7 Copyright (C) Gerald (Jerry) Carter 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* a "session" is claimed when we do a SessionSetupX operation
25 and is yielded when the corresponding vuid is destroyed.
27 sessions are used to populate utmp and PAM session structures
32 static TDB_CONTEXT
*tdb
;
34 /********************************************************************
35 ********************************************************************/
37 BOOL
session_init(void)
42 tdb
= tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST
|TDB_DEFAULT
,
43 O_RDWR
| O_CREAT
, 0644);
45 DEBUG(1,("session_init: failed to open sessionid tdb\n"));
52 /********************************************************************
53 called when a session is created
54 ********************************************************************/
56 BOOL
session_claim(user_struct
*vuser
)
61 struct in_addr
*client_ip
;
62 struct sessionid sessionid
;
63 uint32 pid
= (uint32
)sys_getpid();
67 int tdb_store_flag
; /* If using utmp, we do an inital 'lock hold' store,
68 but we don't need this if we are just using the
69 (unique) pid/vuid combination */
71 vuser
->session_keystr
= NULL
;
73 /* don't register sessions for the guest user - its just too
74 expensive to go through pam session code for browsing etc */
82 ZERO_STRUCT(sessionid
);
88 for (i
=1;i
<MAX_SESSION_ID
;i
++) {
89 slprintf(keystr
, sizeof(keystr
)-1, "ID/%d", i
);
91 key
.dsize
= strlen(keystr
)+1;
93 if (tdb_store(tdb
, key
, data
, TDB_INSERT
) == 0) break;
96 if (i
== MAX_SESSION_ID
) {
97 DEBUG(1,("session_claim: out of session IDs (max is %d)\n",
101 slprintf(sessionid
.id_str
, sizeof(sessionid
.id_str
)-1, SESSION_UTMP_TEMPLATE
, i
);
102 tdb_store_flag
= TDB_MODIFY
;
105 slprintf(keystr
, sizeof(keystr
)-1, "ID/%lu/%u",
106 (long unsigned int)sys_getpid(),
108 slprintf(sessionid
.id_str
, sizeof(sessionid
.id_str
)-1,
109 SESSION_TEMPLATE
, (long unsigned int)sys_getpid(),
113 key
.dsize
= strlen(keystr
)+1;
115 tdb_store_flag
= TDB_REPLACE
;
118 /* If 'hostname lookup' == yes, then do the DNS lookup. This is
119 needed because utmp and PAM both expect DNS names
121 client_name() handles this case internally.
124 hostname
= client_name();
125 if (strcmp(hostname
, "UNKNOWN") == 0) {
126 hostname
= client_addr();
129 fstrcpy(sessionid
.username
, vuser
->user
.unix_name
);
130 fstrcpy(sessionid
.hostname
, hostname
);
131 sessionid
.id_num
= i
; /* Only valid for utmp sessions */
133 sessionid
.uid
= vuser
->uid
;
134 sessionid
.gid
= vuser
->gid
;
135 fstrcpy(sessionid
.remote_machine
, get_remote_machine_name());
136 fstrcpy(sessionid
.ip_addr
, client_addr());
137 sessionid
.connect_start
= time(NULL
);
139 client_ip
= client_inaddr(&sa
);
141 if (!smb_pam_claim_session(sessionid
.username
, sessionid
.id_str
, sessionid
.hostname
)) {
142 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
143 sessionid
.username
, sessionid
.id_str
));
144 if (tdb_store_flag
== TDB_MODIFY
) {
145 tdb_delete(tdb
, key
);
150 data
.dptr
= (char *)&sessionid
;
151 data
.dsize
= sizeof(sessionid
);
152 if (tdb_store(tdb
, key
, data
, tdb_store_flag
) != 0) {
153 DEBUG(1,("session_claim: unable to create session id record\n"));
158 sys_utmp_claim(sessionid
.username
, sessionid
.hostname
,
160 sessionid
.id_str
, sessionid
.id_num
);
163 vuser
->session_keystr
= SMB_STRDUP(keystr
);
164 if (!vuser
->session_keystr
) {
165 DEBUG(0, ("session_claim: strdup() failed for session_keystr\n"));
171 /********************************************************************
172 called when a session is destroyed
173 ********************************************************************/
175 void session_yield(user_struct
*vuser
)
178 struct sessionid sessionid
;
179 struct in_addr
*client_ip
;
184 if (!vuser
->session_keystr
) {
188 key
.dptr
= vuser
->session_keystr
;
189 key
.dsize
= strlen(vuser
->session_keystr
)+1;
191 dbuf
= tdb_fetch(tdb
, key
);
193 if (dbuf
.dsize
!= sizeof(sessionid
))
196 memcpy(&sessionid
, dbuf
.dptr
, sizeof(sessionid
));
198 client_ip
= interpret_addr2(sessionid
.ip_addr
);
200 SAFE_FREE(dbuf
.dptr
);
203 sys_utmp_yield(sessionid
.username
, sessionid
.hostname
,
205 sessionid
.id_str
, sessionid
.id_num
);
208 smb_pam_close_session(sessionid
.username
, sessionid
.id_str
, sessionid
.hostname
);
210 tdb_delete(tdb
, key
);
213 /********************************************************************
214 ********************************************************************/
216 BOOL
session_traverse(int (*fn
)(TDB_CONTEXT
*, TDB_DATA
, TDB_DATA
, void *),
219 if (!session_init()) {
220 DEBUG(3, ("No tdb opened\n"));
224 tdb_traverse(tdb
, fn
, state
);
228 /********************************************************************
229 ********************************************************************/
231 struct session_list
{
233 struct sessionid
*sessions
;
236 static int gather_sessioninfo(TDB_CONTEXT
*stdb
, TDB_DATA kbuf
, TDB_DATA dbuf
, void *state
)
239 struct session_list
*sesslist
= (struct session_list
*) state
;
240 const struct sessionid
*current
= (const struct sessionid
*) dbuf
.dptr
;
244 sesslist
->sessions
= SMB_REALLOC_ARRAY(sesslist
->sessions
, struct sessionid
, i
+1);
245 if (!sesslist
->sessions
) {
250 memcpy(&sesslist
->sessions
[i
], current
, sizeof(struct sessionid
));
253 DEBUG(7,("gather_sessioninfo session from %s@%s\n",
254 current
->username
, current
->remote_machine
));
259 /********************************************************************
260 ********************************************************************/
262 int list_sessions(struct sessionid
**session_list
)
264 struct session_list sesslist
;
267 sesslist
.sessions
= NULL
;
269 if (!session_traverse(gather_sessioninfo
, (void *) &sesslist
)) {
270 DEBUG(3, ("Session traverse failed\n"));
271 SAFE_FREE(sesslist
.sessions
);
272 *session_list
= NULL
;
276 *session_list
= sesslist
.sessions
;
277 return sesslist
.count
;