When possible, store the IP address of the connecting client, not just the
authorAndrew Bartlett <abartlet@samba.org>
Thu, 24 Apr 2003 09:52:29 +0000 (09:52 +0000)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 24 Apr 2003 09:52:29 +0000 (09:52 +0000)
hostname.

This makes 'last -i' show the IP.

Thanks to Philip Anderson <pza@australia.op.org> for the idea.

Andrew Bartlett
(This used to be commit 107731c080da1e3e4e13e966f8b79bfd2692a952)

source3/lib/util_sock.c
source3/smbd/session.c
source3/smbd/utmp.c

index 5460bf57b89eecc3ce644b206b0a73183fd759ad..2439cd8eacb01ea9230f9c414892811a2e7bdd05 100644 (file)
@@ -764,6 +764,19 @@ char *client_addr(void)
        return get_socket_addr(client_fd);
 }
 
+struct in_addr *client_inaddr(struct sockaddr *sa)
+{
+       struct sockaddr_in *sockin = (struct sockaddr_in *) (sa);
+       int     length = sizeof(*sa);
+       
+       if (getpeername(client_fd, sa, &length) < 0) {
+               DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
+               return NULL;
+       }
+       
+       return &sockin->sin_addr;
+}
+
 /*******************************************************************
  matchname - determine if host name matches IP address. Used to
  confirm a hostname lookup to prevent spoof attacks
index 54b7a24b070cfca590f0fcfd7cca647d652a3769..2478b34d6e5ef954b33407c9ae572604d6d82259 100644 (file)
@@ -33,6 +33,8 @@ BOOL session_claim(user_struct *vuser)
 {
        int i = 0;
        TDB_DATA data;
+       struct sockaddr sa;
+       struct in_addr *client_ip;
        struct sessionid sessionid;
        uint32 pid = (uint32)sys_getpid();
        TDB_DATA key;           
@@ -117,6 +119,8 @@ BOOL session_claim(user_struct *vuser)
        fstrcpy(sessionid.remote_machine, get_remote_machine_name());
        fstrcpy(sessionid.ip_addr, client_addr());
 
+       client_ip = client_inaddr(&sa);
+
        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));
@@ -136,6 +140,7 @@ BOOL session_claim(user_struct *vuser)
 #if WITH_UTMP  
        if (lp_utmp()) {
                sys_utmp_claim(sessionid.username, sessionid.hostname, 
+                              client_ip,
                               sessionid.id_str, sessionid.id_num);
        }
 #endif
@@ -153,7 +158,8 @@ void session_yield(user_struct *vuser)
 {
        TDB_DATA dbuf;
        struct sessionid sessionid;
-       TDB_DATA key;           
+       struct in_addr client_ip;
+       TDB_DATA key;
 
        if (!tdb) return;
 
@@ -171,11 +177,14 @@ void session_yield(user_struct *vuser)
 
        memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
 
+       inet_pton(AF_INET, sessionid.ip_addr, &client_ip);
+
        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
index 6c12cfac626837174121ade8857e6689823a799b..84ec364654940e7d2c471f85fd5e83c9718e8fbd 100644 (file)
@@ -484,6 +484,7 @@ static int ut_id_encode(int i, char *fourbyte)
 */
 static BOOL sys_utmp_fill(struct utmp *u,
                          const char *username, const char *hostname,
+                         struct in_addr *ipaddr,
                          const char *id_str, int id_num)
 {                        
        struct timeval timeval;
@@ -538,8 +539,9 @@ static BOOL sys_utmp_fill(struct utmp *u,
 #if defined(HAVE_UT_UT_HOST)
        utmp_strcpy(u->ut_host, hostname, sizeof(u->ut_host));
 #endif
-
 #if defined(HAVE_UT_UT_ADDR)
+       if (ipaddr)
+               u->ut_addr = ipaddr->s_addr;
        /*
         * "(unsigned long) ut_addr" apparently exists on at least HP-UX 10.20.
         * Volunteer to implement, please ...
@@ -561,6 +563,7 @@ static BOOL sys_utmp_fill(struct utmp *u,
 ****************************************************************************/
 
 void sys_utmp_yield(const char *username, const char *hostname, 
+                   struct in_addr *ipaddr,
                    const char *id_str, int id_num)
 {
        struct utmp u;
@@ -576,7 +579,7 @@ void sys_utmp_yield(const char *username, const char *hostname,
        u.ut_type = DEAD_PROCESS;
 #endif
 
-       if (!sys_utmp_fill(&u, username, hostname, id_str, id_num)) return;
+       if (!sys_utmp_fill(&u, username, hostname, ipaddr, id_str, id_num)) return;
 
        sys_utmp_update(&u, NULL, False);
 }
@@ -586,6 +589,7 @@ void sys_utmp_yield(const char *username, const char *hostname,
 ****************************************************************************/
 
 void sys_utmp_claim(const char *username, const char *hostname, 
+                   struct in_addr *ipaddr,
                    const char *id_str, int id_num)
 {
        struct utmp u;
@@ -596,7 +600,7 @@ void sys_utmp_claim(const char *username, const char *hostname,
        u.ut_type = USER_PROCESS;
 #endif
 
-       if (!sys_utmp_fill(&u, username, hostname, id_str, id_num)) return;
+       if (!sys_utmp_fill(&u, username, hostname, ipaddr, id_str, id_num)) return;
 
        sys_utmp_update(&u, hostname, True);
 }