r21108: Send sys_notify_watch through the VFS, FAM is next
[ira/wip.git] / source3 / smbd / session.c
index ac06b9872dd04e513069e39b2dbc1c86a466fc9c..bcb840a3fe40b68a61405bf4a99de892503910a6 100644 (file)
@@ -1,8 +1,10 @@
 /* 
    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
 #include "includes.h"
 
 static TDB_CONTEXT *tdb;
-/* called when a session is created */
+
+/********************************************************************
+********************************************************************/
+
+BOOL session_init(void)
+{
+       if (tdb)
+               return True;
+
+       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_init: failed to open sessionid tdb\n"));
+               return False;
+       }
+
+       return True;
+}
+
+/********************************************************************
+ called when a session is created
+********************************************************************/
+
 BOOL session_claim(user_struct *vuser)
 {
        int i = 0;
@@ -52,21 +76,14 @@ BOOL session_claim(user_struct *vuser)
                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 (!session_init())
+               return False;
 
        ZERO_STRUCT(sessionid);
 
        data.dptr = NULL;
        data.dsize = 0;
 
-#if WITH_UTMP
        if (lp_utmp()) {
                for (i=1;i<MAX_SESSION_ID;i++) {
                        slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
@@ -84,7 +101,6 @@ BOOL session_claim(user_struct *vuser)
                slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, SESSION_UTMP_TEMPLATE, i);
                tdb_store_flag = TDB_MODIFY;
        } else
-#endif
        {
                slprintf(keystr, sizeof(keystr)-1, "ID/%lu/%u", 
                         (long unsigned int)sys_getpid(), 
@@ -100,7 +116,7 @@ BOOL session_claim(user_struct *vuser)
        }
 
        /* 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 
           
           client_name() handles this case internally.
        */
@@ -118,6 +134,7 @@ BOOL session_claim(user_struct *vuser)
        sessionid.gid = vuser->gid;
        fstrcpy(sessionid.remote_machine, get_remote_machine_name());
        fstrcpy(sessionid.ip_addr, client_addr());
+       sessionid.connect_start = time(NULL);
 
        client_ip = client_inaddr(&sa);
 
@@ -137,15 +154,13 @@ BOOL session_claim(user_struct *vuser)
                return False;
        }
 
-#if WITH_UTMP  
        if (lp_utmp()) {
                sys_utmp_claim(sessionid.username, sessionid.hostname, 
                               client_ip,
                               sessionid.id_str, sessionid.id_num);
        }
-#endif
 
-       vuser->session_keystr = strdup(keystr);
+       vuser->session_keystr = SMB_STRDUP(keystr);
        if (!vuser->session_keystr) {
                DEBUG(0, ("session_claim:  strdup() failed for session_keystr\n"));
                return False;
@@ -153,7 +168,10 @@ BOOL session_claim(user_struct *vuser)
        return True;
 }
 
-/* called when a session is destroyed */
+/********************************************************************
+ called when a session is destroyed
+********************************************************************/
+
 void session_yield(user_struct *vuser)
 {
        TDB_DATA dbuf;
@@ -181,22 +199,24 @@ void session_yield(user_struct *vuser)
 
        SAFE_FREE(dbuf.dptr);
 
-#if WITH_UTMP  
        if (lp_utmp()) {
                sys_utmp_yield(sessionid.username, sessionid.hostname, 
                               client_ip,
                               sessionid.id_str, sessionid.id_num);
        }
-#endif
 
        smb_pam_close_session(sessionid.username, sessionid.id_str, sessionid.hostname);
 
        tdb_delete(tdb, key);
 }
 
-static BOOL session_traverse(int (*fn)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *), void *state)
+/********************************************************************
+********************************************************************/
+
+BOOL session_traverse(int (*fn)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *),
+                     void *state)
 {
-       if (!tdb) {
+       if (!session_init()) {
                DEBUG(3, ("No tdb opened\n"));
                return False;
        }
@@ -205,28 +225,40 @@ static BOOL session_traverse(int (*fn)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *
        return True;
 }
 
+/********************************************************************
+********************************************************************/
+
 struct session_list {
        int count;
        struct sessionid *sessions;
 };
 
-static int gather_sessioninfo(TDB_CONTEXT *stdb, TDB_DATA kbuf, TDB_DATA dbuf,
-                             void *state)
+static int gather_sessioninfo(TDB_CONTEXT *stdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
 {
+       uint32 i;       
        struct session_list *sesslist = (struct session_list *) state;
        const struct sessionid *current = (const struct sessionid *) dbuf.dptr;
 
-       sesslist->count += 1;
-       sesslist->sessions = REALLOC(sesslist->sessions, sesslist->count * 
-                                     sizeof(struct sessionid));
+       i = sesslist->count;
+       
+       sesslist->sessions = SMB_REALLOC_ARRAY(sesslist->sessions, struct sessionid, i+1);
+       if (!sesslist->sessions) {
+               sesslist->count = 0;
+               return -1;
+       }
+
+       memcpy(&sesslist->sessions[i], current, sizeof(struct sessionid));
+       sesslist->count++;
 
-       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;
 }
 
+/********************************************************************
+********************************************************************/
+
 int list_sessions(struct sessionid **session_list)
 {
        struct session_list sesslist;
@@ -244,4 +276,3 @@ int list_sessions(struct sessionid **session_list)
        *session_list = sesslist.sessions;
        return sesslist.count;
 }
-