summaryrefslogtreecommitdiffstats
path: root/source/smbd/utmp.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/smbd/utmp.c')
-rw-r--r--source/smbd/utmp.c96
1 files changed, 54 insertions, 42 deletions
diff --git a/source/smbd/utmp.c b/source/smbd/utmp.c
index a521d0113d4..92e001cd036 100644
--- a/source/smbd/utmp.c
+++ b/source/smbd/utmp.c
@@ -1,5 +1,6 @@
/*
- Unix SMB/CIFS implementation.
+ Unix SMB/Netbios implementation.
+ Version 2.0
utmp routines
Copyright (C) T.D.Lee@durham.ac.uk 1999
Heavily modified by Andrew Bartlett and Tridge, April 2001
@@ -21,6 +22,8 @@
#include "includes.h"
+#ifdef WITH_UTMP
+
/****************************************************************************
Reflect connection status in utmp/wtmp files.
T.D.Lee@durham.ac.uk September 1999
@@ -79,11 +82,11 @@ lastlog:
Notes:
Each connection requires a small number (starting at 0, working up)
- to represent the line. This must be unique within and across all
- smbd processes. It is the 'id_num' from Samba's session.c code.
+ to represent the line (unum). This must be unique within and across
+ all smbd processes.
The 4 byte 'ut_id' component is vital to distinguish connections,
- of which there could be several hundred or even thousand.
+ of which there could be several hundered or even thousand.
Entries seem to be printable characters, with optional NULL pads.
We need to be distinct from other entries in utmp/wtmp.
@@ -103,27 +106,13 @@ Notes:
Arbitrarily I have chosen to use a distinctive 'SM' for the
first two bytes.
- The remaining two bytes encode the session 'id_num' (see above).
- Our caller (session.c) should note our 16-bit limitation.
-
-****************************************************************************/
-
-#ifndef WITH_UTMP
-/*
- * Not WITH_UTMP? Simply supply dummy routines.
- */
+ The remaining two encode the "unum" (see above).
-void sys_utmp_claim(const char *username, const char *hostname,
- struct in_addr *ipaddr,
- const char *id_str, int id_num)
-{}
+ For "utmp consolidate" the suggestion was made to encode the pid into
+ those remaining two bytes (16 bits). But recent UNIX (e.g Solaris 8)
+ is migrating to pids > 16 bits, so we ought not to do this.
-void sys_utmp_yield(const char *username, const char *hostname,
- struct in_addr *ipaddr,
- const char *id_str, int id_num)
-{}
-
-#else /* WITH_UTMP */
+****************************************************************************/
#include <utmp.h>
@@ -138,6 +127,33 @@ void sys_utmp_yield(const char *username, const char *hostname,
#endif
/****************************************************************************
+ Obtain/release a small number (0 upwards) unique within and across smbds.
+****************************************************************************/
+/*
+ * Need a "small" number to represent this connection, unique within this
+ * smbd and across all smbds.
+ *
+ * claim:
+ * Start at 0, hunt up for free, unique number "unum" by attempting to
+ * store it as a key in a tdb database:
+ * key: unum data: pid+conn
+ * Also store its inverse, ready for yield function:
+ * key: pid+conn data: unum
+ *
+ * yield:
+ * Find key: pid+conn; data is unum; delete record
+ * Find key: unum ; delete record.
+ *
+ * Comment:
+ * The claim algorithm (a "for" loop attempting to store numbers in a tdb
+ * database) will be increasingly inefficient with larger numbers of
+ * connections. Is it possible to write a suitable primitive within tdb?
+ *
+ * However, by also storing the inverse key/data pair, we at least make
+ * the yield algorithm efficient.
+ */
+
+/****************************************************************************
Default paths to various {u,w}tmp{,x} files.
****************************************************************************/
@@ -217,13 +233,13 @@ static void uw_pathname(pstring fname, const char *uw_name, const char *uw_defau
/* For w-files, first look for explicit "wtmp dir" */
if (uw_name[0] == 'w') {
pstrcpy(dirname,lp_wtmpdir());
- trim_char(dirname,'\0','/');
+ trim_string(dirname,"","/");
}
/* For u-files and non-explicit w-dir, look for "utmp dir" */
if (dirname == 0 || strlen(dirname) == 0) {
pstrcpy(dirname,lp_utmpdir());
- trim_char(dirname,'\0','/');
+ trim_string(dirname,"","/");
}
/* If explicit directory above, use it */
@@ -283,12 +299,8 @@ static void updwtmp_my(pstring wname, struct utmp *u, BOOL claim)
* man page appears not to specify (hints non-NULL)
* A correspondent suggest at least ut_name should be NULL
*/
-#if defined(HAVE_UT_UT_NAME)
memset((char *)&u->ut_name, '\0', sizeof(u->ut_name));
-#endif
-#if defined(HAVE_UT_UT_HOST)
memset((char *)&u->ut_host, '\0', sizeof(u->ut_host));
-#endif
}
/* Stolen from logwtmp function in libutil.
* May be more locking/blocking is needed?
@@ -410,9 +422,7 @@ static void sys_utmp_update(struct utmp *u, const char *hostname, BOOL claim)
else
ux.ut_syslen = 0;
#endif
-#if defined(HAVE_UT_UT_HOST)
utmp_strcpy(ux.ut_host, hostname, sizeof(ux.ut_host));
-#endif
uw_pathname(uname, "utmpx", ux_pathname);
uw_pathname(wname, "wtmpx", wx_pathname);
@@ -448,7 +458,7 @@ static void sys_utmp_update(struct utmp *u, const char *hostname, BOOL claim)
static int ut_id_encode(int i, char *fourbyte)
{
int nbase;
- const char *ut_id_encstr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ char *ut_id_encstr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
fourbyte[0] = 'S';
fourbyte[1] = 'M';
@@ -475,7 +485,6 @@ 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;
@@ -495,10 +504,14 @@ static BOOL sys_utmp_fill(struct utmp *u,
/*
* ut_line:
* If size limit proves troublesome, then perhaps use "ut_id_encode()".
+ *
+ * Temporary variable "line_tmp" avoids trouble:
+ * o with unwanted trailing NULL if ut_line full;
+ * o with overflow if ut_line would be more than full.
*/
if (strlen(id_str) > sizeof(u->ut_line)) {
- DEBUG(1,("id_str [%s] is too long for %lu char utmp field\n",
- id_str, (unsigned long)sizeof(u->ut_line)));
+ DEBUG(1,("id_str [%s] is too long for %d char utmp field\n",
+ id_str, sizeof(u->ut_line)));
return False;
}
utmp_strcpy(u->ut_line, id_str, sizeof(u->ut_line));
@@ -526,9 +539,8 @@ 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 ...
@@ -550,7 +562,6 @@ 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;
@@ -566,7 +577,7 @@ void sys_utmp_yield(const char *username, const char *hostname,
u.ut_type = DEAD_PROCESS;
#endif
- if (!sys_utmp_fill(&u, username, hostname, ipaddr, id_str, id_num)) return;
+ if (!sys_utmp_fill(&u, username, hostname, id_str, id_num)) return;
sys_utmp_update(&u, NULL, False);
}
@@ -576,7 +587,6 @@ 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;
@@ -587,9 +597,11 @@ void sys_utmp_claim(const char *username, const char *hostname,
u.ut_type = USER_PROCESS;
#endif
- if (!sys_utmp_fill(&u, username, hostname, ipaddr, id_str, id_num)) return;
+ if (!sys_utmp_fill(&u, username, hostname, id_str, id_num)) return;
sys_utmp_update(&u, hostname, True);
}
-#endif /* WITH_UTMP */
+#else /* WITH_UTMP */
+ void dummy_utmp(void) {}
+#endif