[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / smbd / session.c
blobbcb840a3fe40b68a61405bf4a99de892503910a6
1 /*
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
30 #include "includes.h"
32 static TDB_CONTEXT *tdb;
34 /********************************************************************
35 ********************************************************************/
37 BOOL session_init(void)
39 if (tdb)
40 return True;
42 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
43 O_RDWR | O_CREAT, 0644);
44 if (!tdb) {
45 DEBUG(1,("session_init: failed to open sessionid tdb\n"));
46 return False;
49 return True;
52 /********************************************************************
53 called when a session is created
54 ********************************************************************/
56 BOOL session_claim(user_struct *vuser)
58 int i = 0;
59 TDB_DATA data;
60 struct sockaddr sa;
61 struct in_addr *client_ip;
62 struct sessionid sessionid;
63 uint32 pid = (uint32)sys_getpid();
64 TDB_DATA key;
65 fstring keystr;
66 char * hostname;
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 */
75 if (vuser->guest) {
76 return True;
79 if (!session_init())
80 return False;
82 ZERO_STRUCT(sessionid);
84 data.dptr = NULL;
85 data.dsize = 0;
87 if (lp_utmp()) {
88 for (i=1;i<MAX_SESSION_ID;i++) {
89 slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
90 key.dptr = keystr;
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",
98 MAX_SESSION_ID));
99 return False;
101 slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, SESSION_UTMP_TEMPLATE, i);
102 tdb_store_flag = TDB_MODIFY;
103 } else
105 slprintf(keystr, sizeof(keystr)-1, "ID/%lu/%u",
106 (long unsigned int)sys_getpid(),
107 vuser->vuid);
108 slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1,
109 SESSION_TEMPLATE, (long unsigned int)sys_getpid(),
110 vuser->vuid);
112 key.dptr = keystr;
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 */
132 sessionid.pid = pid;
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);
147 return False;
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"));
154 return False;
157 if (lp_utmp()) {
158 sys_utmp_claim(sessionid.username, sessionid.hostname,
159 client_ip,
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"));
166 return False;
168 return True;
171 /********************************************************************
172 called when a session is destroyed
173 ********************************************************************/
175 void session_yield(user_struct *vuser)
177 TDB_DATA dbuf;
178 struct sessionid sessionid;
179 struct in_addr *client_ip;
180 TDB_DATA key;
182 if (!tdb) return;
184 if (!vuser->session_keystr) {
185 return;
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))
194 return;
196 memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
198 client_ip = interpret_addr2(sessionid.ip_addr);
200 SAFE_FREE(dbuf.dptr);
202 if (lp_utmp()) {
203 sys_utmp_yield(sessionid.username, sessionid.hostname,
204 client_ip,
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 *),
217 void *state)
219 if (!session_init()) {
220 DEBUG(3, ("No tdb opened\n"));
221 return False;
224 tdb_traverse(tdb, fn, state);
225 return True;
228 /********************************************************************
229 ********************************************************************/
231 struct session_list {
232 int count;
233 struct sessionid *sessions;
236 static int gather_sessioninfo(TDB_CONTEXT *stdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
238 uint32 i;
239 struct session_list *sesslist = (struct session_list *) state;
240 const struct sessionid *current = (const struct sessionid *) dbuf.dptr;
242 i = sesslist->count;
244 sesslist->sessions = SMB_REALLOC_ARRAY(sesslist->sessions, struct sessionid, i+1);
245 if (!sesslist->sessions) {
246 sesslist->count = 0;
247 return -1;
250 memcpy(&sesslist->sessions[i], current, sizeof(struct sessionid));
251 sesslist->count++;
253 DEBUG(7,("gather_sessioninfo session from %s@%s\n",
254 current->username, current->remote_machine));
256 return 0;
259 /********************************************************************
260 ********************************************************************/
262 int list_sessions(struct sessionid **session_list)
264 struct session_list sesslist;
266 sesslist.count = 0;
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;
273 return 0;
276 *session_list = sesslist.sessions;
277 return sesslist.count;