s3-talloc Change TALLOC_REALLOC_ARRAY() to talloc_realloc()
[samba.git] / source3 / smbd / session.c
index 6f9d62edfdb920c2711ea6cc2294a43befa72864..12d4818e90ca8c548c5e2b5572911ea459e1fe82 100644 (file)
@@ -1,12 +1,14 @@
 /* 
    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,
@@ -15,8 +17,7 @@
    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"
+#include "smbd/smbd.h"
+#include "smbd/globals.h"
+#include "dbwrap.h"
+#include "session.h"
+#include "auth.h"
 
-extern fstring remote_machine;
+/********************************************************************
+ called when a session is created
+********************************************************************/
 
-static TDB_CONTEXT *tdb;
-/* called when a session is created */
-BOOL session_claim(user_struct *vuser)
+bool session_claim(struct smbd_server_connection *sconn, user_struct *vuser)
 {
-       int i;
+       struct server_id pid = sconn_server_id(sconn);
        TDB_DATA data;
+       int i = 0;
        struct sessionid sessionid;
-       uint32 pid = (uint32)sys_getpid();
-       TDB_DATA key;           
        fstring keystr;
-       char * hostname;
+       struct db_record *rec;
+       NTSTATUS status;
 
-       vuser->session_id = 0;
+       vuser->session_keystr = NULL;
 
        /* don't register sessions for the guest user - its just too
           expensive to go through pam session code for browsing etc */
-       if (vuser->guest) {
+       if (vuser->session_info->guest) {
                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;
-               }
+       if (!sessionid_init()) {
+               return False;
        }
 
        ZERO_STRUCT(sessionid);
@@ -63,114 +64,213 @@ BOOL session_claim(user_struct *vuser)
        data.dptr = NULL;
        data.dsize = 0;
 
-       for (i=1;i<MAX_SESSION_ID;i++) {
-               slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
-               key.dptr = keystr;
-               key.dsize = strlen(keystr)+1;
+       if (lp_utmp()) {
 
-               if (tdb_store(tdb, key, data, TDB_INSERT) == 0) break;
-       }
+               for (i=1;i<MAX_SESSION_ID;i++) {
 
-       if (i == MAX_SESSION_ID) {
-               DEBUG(1,("session_claim: out of session IDs (max is %d)\n", 
-                        MAX_SESSION_ID));
-               return False;
+                       /*
+                        * This is very inefficient and needs fixing -- vl
+                        */
+
+                       struct server_id sess_pid;
+
+                       snprintf(keystr, sizeof(keystr), "ID/%d", i);
+
+                       rec = sessionid_fetch_record(NULL, keystr);
+                       if (rec == NULL) {
+                               DEBUG(1, ("Could not lock \"%s\"\n", keystr));
+                               return False;
+                       }
+
+                       if (rec->value.dsize != sizeof(sessionid)) {
+                               DEBUG(1, ("Re-using invalid record\n"));
+                               break;
+                       }
+
+                       memcpy(&sess_pid,
+                              ((char *)rec->value.dptr)
+                              + offsetof(struct sessionid, pid),
+                              sizeof(sess_pid));
+
+                       if (!process_exists(sess_pid)) {
+                               DEBUG(5, ("%s has died -- re-using session\n",
+                                         procid_str_static(&sess_pid)));
+                               break;
+                       }
+
+                       TALLOC_FREE(rec);
+               }
+
+               if (i == MAX_SESSION_ID) {
+                       SMB_ASSERT(rec == NULL);
+                       DEBUG(1,("session_claim: out of session IDs "
+                                "(max is %d)\n", MAX_SESSION_ID));
+                       return False;
+               }
+
+               snprintf(sessionid.id_str, sizeof(sessionid.id_str),
+                        SESSION_UTMP_TEMPLATE, i);
+       } else
+       {
+               snprintf(keystr, sizeof(keystr), "ID/%s/%u",
+                        procid_str_static(&pid), vuser->vuid);
+
+               rec = sessionid_fetch_record(NULL, keystr);
+               if (rec == NULL) {
+                       DEBUG(1, ("Could not lock \"%s\"\n", keystr));
+                       return False;
+               }
+
+               snprintf(sessionid.id_str, sizeof(sessionid.id_str),
+                        SESSION_TEMPLATE, (long unsigned int)sys_getpid(),
+                        vuser->vuid);
        }
 
+       SMB_ASSERT(rec != NULL);
+
        /* If 'hostname lookup' == yes, then do the DNS lookup.  This is
-           needed becouse utmp and PAM both expect DNS names */
+           needed because utmp and PAM both expect DNS names
 
-       if (lp_hostname_lookups()) {
-               hostname = client_name();
-       } else {
-               hostname = client_addr();
-       }
+          client_name() handles this case internally.
+       */
 
-       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;
+       fstrcpy(sessionid.username, vuser->session_info->unix_name);
+       fstrcpy(sessionid.hostname, sconn->client_id.name);
+       sessionid.id_num = i;  /* Only valid for utmp sessions */
        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)) {
+       sessionid.uid = vuser->session_info->utok.uid;
+       sessionid.gid = vuser->session_info->utok.gid;
+       fstrcpy(sessionid.remote_machine, get_remote_machine_name());
+       fstrcpy(sessionid.ip_addr_str, sconn->client_id.addr);
+       sessionid.connect_start = time(NULL);
+
+       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);
+
+               TALLOC_FREE(rec);
                return False;
        }
 
-       data.dptr = (char *)&sessionid;
+       data.dptr = (uint8 *)&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"));
+
+       status = rec->store(rec, data, TDB_REPLACE);
+
+       TALLOC_FREE(rec);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1,("session_claim: unable to create session id "
+                        "record: %s\n", nt_errstr(status)));
                return False;
        }
 
-#if WITH_UTMP  
        if (lp_utmp()) {
-               sys_utmp_claim(sessionid.username, sessionid.hostname, 
+               sys_utmp_claim(sessionid.username, sessionid.hostname,
+                              sessionid.ip_addr_str,
                               sessionid.id_str, sessionid.id_num);
        }
-#endif
 
-       vuser->session_id = i;
+       vuser->session_keystr = talloc_strdup(vuser, keystr);
+       if (!vuser->session_keystr) {
+               DEBUG(0, ("session_claim:  talloc_strdup() failed for session_keystr\n"));
+               return False;
+       }
        return True;
 }
 
-/* called when a session is destroyed */
+/********************************************************************
+ 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;
+       struct db_record *rec;
 
-       if (vuser->session_id == 0) {
+       if (!vuser->session_keystr) {
                return;
        }
 
-       slprintf(keystr, sizeof(keystr)-1, "ID/%d", vuser->session_id);
-
-       key.dptr = keystr;
-       key.dsize = strlen(keystr)+1;
-
-       dbuf = tdb_fetch(tdb, key);
-
-       if (dbuf.dsize != sizeof(sessionid))
+       rec = sessionid_fetch_record(NULL, vuser->session_keystr);
+       if (rec == NULL) {
                return;
+       }
 
-       memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
+       if (rec->value.dsize != sizeof(sessionid))
+               return;
 
-       SAFE_FREE(dbuf.dptr);
+       memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
 
-#if WITH_UTMP  
        if (lp_utmp()) {
                sys_utmp_yield(sessionid.username, sessionid.hostname, 
+                              sessionid.ip_addr_str,
                               sessionid.id_str, sessionid.id_num);
        }
-#endif
 
-       smb_pam_close_session(sessionid.username, sessionid.id_str, sessionid.hostname);
+       smb_pam_close_session(sessionid.username, sessionid.id_str,
+                             sessionid.hostname);
+
+       rec->delete_rec(rec);
 
-       tdb_delete(tdb, key);
+       TALLOC_FREE(rec);
 }
 
-BOOL session_traverse(int (*fn)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *), void *state)
+/********************************************************************
+********************************************************************/
+
+struct session_list {
+       TALLOC_CTX *mem_ctx;
+       int count;
+       struct sessionid *sessions;
+};
+
+static int gather_sessioninfo(const char *key, struct sessionid *session,
+                             void *private_data)
 {
-  if (!tdb) {
-    DEBUG(3, ("No tdb opened\n"));
-    return False;
-  }
+       struct session_list *sesslist = (struct session_list *)private_data;
+
+       sesslist->sessions = talloc_realloc(
+               sesslist->mem_ctx, sesslist->sessions, struct sessionid,
+               sesslist->count+1);
+
+       if (!sesslist->sessions) {
+               sesslist->count = 0;
+               return -1;
+       }
 
-  tdb_traverse(tdb, fn, state);
-  return True;
+       memcpy(&sesslist->sessions[sesslist->count], session,
+              sizeof(struct sessionid));
+
+       sesslist->count++;
+
+       DEBUG(7, ("gather_sessioninfo session from %s@%s\n",
+                 session->username, session->remote_machine));
+
+       return 0;
 }
 
+/********************************************************************
+********************************************************************/
 
+int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
+{
+       struct session_list sesslist;
+       int ret;
+
+       sesslist.mem_ctx = mem_ctx;
+       sesslist.count = 0;
+       sesslist.sessions = NULL;
+
+       ret = sessionid_traverse_read(gather_sessioninfo, (void *) &sesslist);
+       if (ret == -1) {
+               DEBUG(3, ("Session traverse failed\n"));
+               SAFE_FREE(sesslist.sessions);
+               *session_list = NULL;
+               return 0;
+       }
 
+       *session_list = sesslist.sessions;
+       return sesslist.count;
+}