/*
Unix SMB/CIFS implementation.
session handling for utmp and PAM
- Copyright (C) tridge@samba.org 2001
- Copyright (C) abartlet@pcug.org.au 2001
+
+ Copyright (C) tridge@samba.org 2001
+ Copyright (C) abartlet@samba.org 2001
+ Copyright (C) Gerald (Jerry) Carter 2006
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
+ the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* a "session" is claimed when we do a SessionSetupX operation
*/
#include "includes.h"
-
-extern fstring remote_machine;
-
-static TDB_CONTEXT *tdb;
-/* called when a session is created */
-BOOL session_claim(user_struct *vuser)
+#include "smbd/smbd.h"
+#include "smbd/globals.h"
+#include "dbwrap/dbwrap.h"
+#include "session.h"
+#include "auth.h"
+#include "../lib/tsocket/tsocket.h"
+#include "../libcli/security/security.h"
+#include "messages.h"
+
+/********************************************************************
+ called when a session is created
+********************************************************************/
+
+bool session_claim(struct smbXsrv_session *session)
{
- int i;
- TDB_DATA data;
- struct sessionid sessionid;
- uint32 pid = (uint32)sys_getpid();
- TDB_DATA key;
- fstring keystr;
- char * hostname;
-
- vuser->session_id = 0;
+ struct auth_session_info *session_info =
+ session->global->auth_session_info;
+ const char *username;
+ const char *hostname;
+ unsigned int id_num;
+ fstring id_str;
/* don't register sessions for the guest user - its just too
expensive to go through pam session code for browsing etc */
- if (vuser->guest) {
- return True;
+ if (security_session_user_level(session_info, NULL) < SECURITY_USER) {
+ return true;
}
- if (!tdb) {
- tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
- O_RDWR | O_CREAT, 0644);
- if (!tdb) {
- DEBUG(1,("session_claim: failed to open sessionid tdb\n"));
- return False;
- }
- }
+ id_num = session->global->session_global_id;
- ZERO_STRUCT(sessionid);
+ snprintf(id_str, sizeof(id_str), "smb/%u", id_num);
- data.dptr = NULL;
- data.dsize = 0;
+ /* Make clear that we require the optional unix_token in the source3 code */
+ SMB_ASSERT(session_info->unix_token);
- for (i=1;i<MAX_SESSION_ID;i++) {
- slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
- key.dptr = keystr;
- key.dsize = strlen(keystr)+1;
+ username = session_info->unix_info->unix_name;
+ hostname = session->global->channels[0].remote_name;
- if (tdb_store(tdb, key, data, TDB_INSERT) == 0) break;
+ if (!smb_pam_claim_session(username, id_str, hostname)) {
+ DEBUG(1,("pam_session rejected the session for %s [%s]\n",
+ username, id_str));
+ return false;
}
- if (i == MAX_SESSION_ID) {
- DEBUG(1,("session_claim: out of session IDs (max is %d)\n",
- MAX_SESSION_ID));
- return False;
+ if (lp_utmp()) {
+ sys_utmp_claim(username, hostname, id_str, id_num);
}
- /* If 'hostname lookup' == yes, then do the DNS lookup. This is
- needed becouse utmp and PAM both expect DNS names
-
- client_name() handles this case internally.
- */
+ return true;
+}
- hostname = client_name();
- if (strcmp(hostname, "UNKNOWN") == 0) {
- hostname = client_addr();
- }
+/********************************************************************
+ called when a session is destroyed
+********************************************************************/
- fstrcpy(sessionid.username, vuser->user.unix_name);
- fstrcpy(sessionid.hostname, hostname);
- slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, SESSION_TEMPLATE, i);
- sessionid.id_num = i;
- sessionid.pid = pid;
- sessionid.uid = vuser->uid;
- sessionid.gid = vuser->gid;
- fstrcpy(sessionid.remote_machine, remote_machine);
- fstrcpy(sessionid.ip_addr, client_addr());
-
- if (!smb_pam_claim_session(sessionid.username, sessionid.id_str, sessionid.hostname)) {
- DEBUG(1,("pam_session rejected the session for %s [%s]\n",
- sessionid.username, sessionid.id_str));
- tdb_delete(tdb, key);
- return False;
- }
+void session_yield(struct smbXsrv_session *session)
+{
+ struct auth_session_info *session_info =
+ session->global->auth_session_info;
+ const char *username;
+ const char *hostname;
+ unsigned int id_num;
+ fstring id_str = "";
- data.dptr = (char *)&sessionid;
- data.dsize = sizeof(sessionid);
- if (tdb_store(tdb, key, data, TDB_MODIFY) != 0) {
- DEBUG(1,("session_claim: unable to create session id record\n"));
- return False;
- }
+ id_num = session->global->session_global_id;
+
+ snprintf(id_str, sizeof(id_str), "smb/%u", id_num);
+
+ /* Make clear that we require the optional unix_token in the source3 code */
+ SMB_ASSERT(session_info->unix_token);
+
+ username = session_info->unix_info->unix_name;
+ hostname = session->global->channels[0].remote_name;
-#if WITH_UTMP
if (lp_utmp()) {
- sys_utmp_claim(sessionid.username, sessionid.hostname,
- sessionid.id_str, sessionid.id_num);
+ sys_utmp_yield(username, hostname, id_str, id_num);
}
-#endif
- vuser->session_id = i;
- return True;
+ smb_pam_close_session(username, id_str, hostname);
}
-/* called when a session is destroyed */
-void session_yield(user_struct *vuser)
-{
- TDB_DATA dbuf;
- struct sessionid sessionid;
- TDB_DATA key;
- fstring keystr;
-
- if (!tdb) return;
+/********************************************************************
+********************************************************************/
- if (vuser->session_id == 0) {
- return;
- }
+struct session_list {
+ TALLOC_CTX *mem_ctx;
+ int count;
+ const char *filter_user;
+ const char *filter_machine;
+ struct sessionid *sessions;
+};
- slprintf(keystr, sizeof(keystr)-1, "ID/%d", vuser->session_id);
+static int gather_sessioninfo(const char *key, struct sessionid *session,
+ void *private_data)
+{
+ struct session_list *sesslist = (struct session_list *)private_data;
- key.dptr = keystr;
- key.dsize = strlen(keystr)+1;
+ /* filter the session if required */
- dbuf = tdb_fetch(tdb, key);
+ if (sesslist->filter_user &&
+ (sesslist->filter_user[0] != '\0') &&
+ !strequal(session->username, sesslist->filter_user)) {
+ return 0;
+ }
- if (dbuf.dsize != sizeof(sessionid))
- return;
+ if (sesslist->filter_machine &&
+ (sesslist->filter_machine[0] != '\0') &&
+ !strequal(session->remote_machine,
+ sesslist->filter_machine)) {
+ return 0;
+ }
- memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
+ if (!process_exists(session->pid)) {
+ return 0;
+ }
- SAFE_FREE(dbuf.dptr);
+ sesslist->sessions = talloc_realloc(
+ sesslist->mem_ctx, sesslist->sessions, struct sessionid,
+ sesslist->count+1);
-#if WITH_UTMP
- if (lp_utmp()) {
- sys_utmp_yield(sessionid.username, sessionid.hostname,
- sessionid.id_str, sessionid.id_num);
+ if (!sesslist->sessions) {
+ sesslist->count = 0;
+ return -1;
}
-#endif
- smb_pam_close_session(sessionid.username, sessionid.id_str, sessionid.hostname);
+ memcpy(&sesslist->sessions[sesslist->count], session,
+ sizeof(struct sessionid));
- tdb_delete(tdb, key);
-}
+ sesslist->count++;
-static BOOL session_traverse(int (*fn)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *), void *state)
-{
- if (!tdb) {
- DEBUG(3, ("No tdb opened\n"));
- return False;
- }
+ DEBUG(7, ("gather_sessioninfo session from %s@%s\n",
+ session->username, session->remote_machine));
- tdb_traverse(tdb, fn, state);
- return True;
+ return 0;
}
-struct session_list {
- int count;
- struct sessionid *sessions;
-};
+/********************************************************************
+********************************************************************/
-static int gather_sessioninfo(TDB_CONTEXT *stdb, TDB_DATA kbuf, TDB_DATA dbuf,
- void *state)
+int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
{
- struct session_list *sesslist = (struct session_list *) state;
- const struct sessionid *current = (const struct sessionid *) dbuf.dptr;
+ struct session_list sesslist;
+ NTSTATUS status;
- sesslist->count += 1;
- sesslist->sessions = REALLOC(sesslist->sessions, sesslist->count *
- sizeof(struct sessionid));
+ sesslist.mem_ctx = mem_ctx;
+ sesslist.count = 0;
+ sesslist.filter_user = NULL;
+ sesslist.filter_machine = NULL;
+ sesslist.sessions = NULL;
- memcpy(&sesslist->sessions[sesslist->count - 1], current,
- sizeof(struct sessionid));
- DEBUG(7,("gather_sessioninfo session from %s@%s\n",
- current->username, current->remote_machine));
- return 0;
+ status = sessionid_traverse_read(gather_sessioninfo, (void *) &sesslist);
+ if (!NT_STATUS_IS_OK(status)) {
+ DBG_ERR("Session traverse failed: %s\n", nt_errstr(status));
+ TALLOC_FREE(sesslist.sessions);
+ *session_list = NULL;
+ return 0;
+ }
+
+ *session_list = sesslist.sessions;
+ return sesslist.count;
}
-int list_sessions(struct sessionid **session_list)
+/********************************************************************
+find the sessions that match the given username and machine
+********************************************************************/
+
+int find_sessions(TALLOC_CTX *mem_ctx, const char *username,
+ const char *machine, struct sessionid **session_list)
{
struct session_list sesslist;
+ NTSTATUS status;
+ sesslist.mem_ctx = mem_ctx;
sesslist.count = 0;
+ sesslist.filter_user = username;
+ sesslist.filter_machine = machine;
sesslist.sessions = NULL;
-
- if (!session_traverse(gather_sessioninfo, (void *) &sesslist)) {
- DEBUG(3, ("Session traverse failed\n"));
- SAFE_FREE(sesslist.sessions);
+
+ status = sessionid_traverse_read(gather_sessioninfo, (void *)&sesslist);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(3, ("Session traverse failed: %s\n", nt_errstr(status)));
+ TALLOC_FREE(sesslist.sessions);
*session_list = NULL;
return 0;
}
*session_list = sesslist.sessions;
return sesslist.count;
}
-