2 Unix SMB/CIFS implementation.
4 Copyright (C) T.D.Lee@durham.ac.uk 1999
5 Heavily modified by Andrew Bartlett and Tridge, April 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /****************************************************************************
27 Reflect connection status in utmp/wtmp files.
28 T.D.Lee@durham.ac.uk September 1999
30 With grateful thanks since then to many who have helped port it to
31 different operating systems. The variety of OS quirks thereby
32 uncovered is amazing...
35 o Always attempt to use programmatic interface (pututline() etc.)
36 Indeed, at present only programmatic use is supported.
37 o The only currently supported programmatic interface to "wtmp{,x}"
38 is through "updwtmp*()" routines.
39 o The "x" (utmpx/wtmpx; HAVE_UTMPX_H) seems preferable.
40 o The HAVE_* items should identify supported features.
41 o If at all possible, avoid "if defined(MY-OS)" constructions.
43 OS observations and status:
44 Almost every OS seems to have its own quirks.
47 Tested on 2.6 and 2.7; should be OK on other flavours.
49 Apparently has utmpx.h but doesn't implement.
51 Has utmpx.h, but (e.g.) no "getutmpx()". (Is this like AIX ?)
53 utmpx.h seems not to set default filenames. non-x better.
55 Not tested. Appears to have "x".
57 Not tested. Appears to lack "x".
60 "updwtmp*()" routines seem absent, so no current wtmp* support.
61 Has "ut_addr": probably trivial to implement (although remember
62 that IPv6 is coming...).
65 No "putut*()" type of interface.
66 No "ut_type" and associated defines.
67 Write files directly. Alternatively use its login(3)/logout(3).
69 Not tested. Resembles FreeBSD, but no login()/logout().
72 Should "lastlog" files, if any, be updated?
73 BSD systems (SunOS 4, FreeBSD):
74 o Prominent mention on man pages.
75 System-V (e.g. Solaris 2):
76 o No mention on man pages, even under "man -k".
77 o Has a "/var/adm/lastlog" file, but pututxline() etc. seem
79 o Despite downplaying (above), nevertheless has <lastlog.h>.
80 So perhaps UN*X "lastlog" facility is intended for tty/terminal only?
83 Each connection requires a small number (starting at 0, working up)
84 to represent the line. This must be unique within and across all
85 smbd processes. It is the 'id_num' from Samba's session.c code.
87 The 4 byte 'ut_id' component is vital to distinguish connections,
88 of which there could be several hundred or even thousand.
89 Entries seem to be printable characters, with optional NULL pads.
91 We need to be distinct from other entries in utmp/wtmp.
93 Observed things: therefore avoid them. Add to this list please.
94 From Solaris 2.x (because that's what I have):
95 'sN' : run-levels; N: [0-9]
97 'CC' : arbitrary things; C: [a-z]
98 'rXNN' : rlogin; N: [0-9]; X: [0-9a-z]
99 'tXNN' : rlogin; N: [0-9]; X: [0-9a-z]
101 'ftpZ' : ftp (Z is the number 255, aka 0377, aka 0xff)
102 Mostly a record uses the same 'ut_id' in both "utmp" and "wtmp",
103 but differences have been seen.
105 Arbitrarily I have chosen to use a distinctive 'SM' for the
108 The remaining two bytes encode the session 'id_num' (see above).
109 Our caller (session.c) should note our 16-bit limitation.
111 ****************************************************************************/
119 /* BSD systems: some may need lastlog.h (SunOS 4), some may not (FreeBSD) */
120 /* Some System-V systems (e.g. Solaris 2) declare this too. */
121 #ifdef HAVE_LASTLOG_H
125 /****************************************************************************
126 Default paths to various {u,w}tmp{,x} files.
127 ****************************************************************************/
131 static const char *ux_pathname =
132 # if defined (UTMPX_FILE)
134 # elif defined (_UTMPX_FILE)
136 # elif defined (_PATH_UTMPX)
142 static const char *wx_pathname =
143 # if defined (WTMPX_FILE)
145 # elif defined (_WTMPX_FILE)
147 # elif defined (_PATH_WTMPX)
153 #endif /* HAVE_UTMPX_H */
155 static const char *ut_pathname =
156 # if defined (UTMP_FILE)
158 # elif defined (_UTMP_FILE)
160 # elif defined (_PATH_UTMP)
166 static const char *wt_pathname =
167 # if defined (WTMP_FILE)
169 # elif defined (_WTMP_FILE)
171 # elif defined (_PATH_WTMP)
177 /* BSD-like systems might want "lastlog" support. */
178 /* *** Not yet implemented */
179 #ifndef HAVE_PUTUTLINE /* see "pututline_my()" */
180 static const char *ll_pathname =
181 # if defined (_PATH_LASTLOG) /* what other names (if any?) */
185 # endif /* _PATH_LASTLOG */
186 #endif /* HAVE_PUTUTLINE */
189 * Get name of {u,w}tmp{,x} file.
190 * return: fname contains filename
191 * Possibly empty if this code not yet ported to this system.
193 * utmp{,x}: try "utmp dir", then default (a define)
194 * wtmp{,x}: try "wtmp dir", then "utmp dir", then default (a define)
196 static void uw_pathname(pstring fname, const char *uw_name, const char *uw_default)
200 pstrcpy(dirname, "");
202 /* For w-files, first look for explicit "wtmp dir" */
203 if (uw_name[0] == 'w') {
204 pstrcpy(dirname,lp_wtmpdir());
205 trim_string(dirname,"","/");
208 /* For u-files and non-explicit w-dir, look for "utmp dir" */
209 if (dirname == 0 || strlen(dirname) == 0) {
210 pstrcpy(dirname,lp_utmpdir());
211 trim_string(dirname,"","/");
214 /* If explicit directory above, use it */
215 if (dirname != 0 && strlen(dirname) != 0) {
216 pstrcpy(fname, dirname);
218 pstrcat(fname, uw_name);
222 /* No explicit directory: attempt to use default paths */
223 if (strlen(uw_default) == 0) {
224 /* No explicit setting, no known default.
225 * Has it yet been ported to this OS?
227 DEBUG(2,("uw_pathname: unable to determine pathname\n"));
229 pstrcpy(fname, uw_default);
232 #ifndef HAVE_PUTUTLINE
234 /****************************************************************************
235 Update utmp file directly. No subroutine interface: probably a BSD system.
236 ****************************************************************************/
238 static void pututline_my(pstring uname, struct utmp *u, BOOL claim)
240 DEBUG(1,("pututline_my: not yet implemented\n"));
241 /* BSD implementor: may want to consider (or not) adjusting "lastlog" */
243 #endif /* HAVE_PUTUTLINE */
247 /****************************************************************************
248 Update wtmp file directly. No subroutine interface: probably a BSD system.
249 Credit: Michail Vidiassov <master@iaas.msu.ru>
250 ****************************************************************************/
252 static void updwtmp_my(pstring wname, struct utmp *u, BOOL claim)
260 * may use empty ut_name to distinguish a logout record.
262 * May need "if defined(SUNOS4)" etc. around some of these,
263 * but try to avoid if possible.
266 * man page indicates ut_name and ut_host both NULL
268 * man page appears not to specify (hints non-NULL)
269 * A correspondent suggest at least ut_name should be NULL
271 memset((char *)&u->ut_name, '\0', sizeof(u->ut_name));
272 memset((char *)&u->ut_host, '\0', sizeof(u->ut_host));
274 /* Stolen from logwtmp function in libutil.
275 * May be more locking/blocking is needed?
277 if ((fd = open(wname, O_WRONLY|O_APPEND, 0)) < 0)
279 if (fstat(fd, &buf) == 0) {
280 if (write(fd, (char *)u, sizeof(struct utmp)) != sizeof(struct utmp))
281 (void) ftruncate(fd, buf.st_size);
285 #endif /* HAVE_UPDWTMP */
287 /****************************************************************************
288 Update via utmp/wtmp (not utmpx/wtmpx).
289 ****************************************************************************/
291 static void utmp_nox_update(struct utmp *u, BOOL claim)
293 pstring uname, wname;
294 #if defined(PUTUTLINE_RETURNS_UTMP)
296 #endif /* PUTUTLINE_RETURNS_UTMP */
298 uw_pathname(uname, "utmp", ut_pathname);
299 DEBUG(2,("utmp_nox_update: uname:%s\n", uname));
301 #ifdef HAVE_PUTUTLINE
302 if (strlen(uname) != 0) {
306 # if defined(PUTUTLINE_RETURNS_UTMP)
311 DEBUG(2,("utmp_nox_update: pututline() failed\n"));
314 # else /* PUTUTLINE_RETURNS_UTMP */
318 # endif /* PUTUTLINE_RETURNS_UTMP */
320 #else /* HAVE_PUTUTLINE */
321 if (strlen(uname) != 0) {
322 pututline_my(uname, u, claim);
324 #endif /* HAVE_PUTUTLINE */
326 uw_pathname(wname, "wtmp", wt_pathname);
327 DEBUG(2,("utmp_nox_update: wname:%s\n", wname));
328 if (strlen(wname) != 0) {
332 * updwtmp() and the newer updwtmpx() may be unsymmetrical.
333 * At least one OS, Solaris 2.x declares the former in the
334 * "utmpx" (latter) file and context.
335 * In the Solaris case this is irrelevant: it has both and
336 * we always prefer the "x" case, so doesn't come here.
337 * But are there other systems, with no "x", which lack
341 updwtmp_my(wname, u, claim);
342 #endif /* HAVE_UPDWTMP */
346 /****************************************************************************
347 Copy a string in the utmp structure.
348 ****************************************************************************/
350 static void utmp_strcpy(char *dest, const char *src, size_t n)
354 memset(dest, '\0', n);
358 memcpy(dest, src, n);
361 memcpy(dest, src, len);
365 /****************************************************************************
366 Update via utmpx/wtmpx (preferred) or via utmp/wtmp.
367 ****************************************************************************/
369 static void sys_utmp_update(struct utmp *u, const char *hostname, BOOL claim)
371 #if !defined(HAVE_UTMPX_H)
372 /* No utmpx stuff. Drop to non-x stuff */
373 utmp_nox_update(u, claim);
374 #elif !defined(HAVE_PUTUTXLINE)
375 /* Odd. Have utmpx.h but no "pututxline()". Drop to non-x stuff */
376 DEBUG(1,("utmp_update: have utmpx.h but no pututxline() function\n"));
377 utmp_nox_update(u, claim);
378 #elif !defined(HAVE_GETUTMPX)
379 /* Odd. Have utmpx.h but no "getutmpx()". Drop to non-x stuff */
380 DEBUG(1,("utmp_update: have utmpx.h but no getutmpx() function\n"));
381 utmp_nox_update(u, claim);
383 pstring uname, wname;
384 struct utmpx ux, *uxrc;
388 #if defined(HAVE_UX_UT_SYSLEN)
390 ux.ut_syslen = strlen(hostname) + 1; /* include end NULL */
394 utmp_strcpy(ux.ut_host, hostname, sizeof(ux.ut_host));
396 uw_pathname(uname, "utmpx", ux_pathname);
397 uw_pathname(wname, "wtmpx", wx_pathname);
398 DEBUG(2,("utmp_update: uname:%s wname:%s\n", uname, wname));
400 * Check for either uname or wname being empty.
401 * Some systems, such as Redhat 6, have a "utmpx.h" which doesn't
402 * define default filenames.
403 * Also, our local installation has not provided an override.
404 * Drop to non-x method. (E.g. RH6 has good defaults in "utmp.h".)
406 if ((strlen(uname) == 0) || (strlen(wname) == 0)) {
407 utmp_nox_update(u, claim);
411 uxrc = pututxline(&ux);
414 DEBUG(2,("utmp_update: pututxline() failed\n"));
417 updwtmpx(wname, &ux);
419 #endif /* HAVE_UTMPX_H */
422 #if defined(HAVE_UT_UT_ID)
423 /****************************************************************************
424 Encode the unique connection number into "ut_id".
425 ****************************************************************************/
427 static int ut_id_encode(int i, char *fourbyte)
430 const char *ut_id_encstr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
436 * Encode remaining 2 bytes from 'i'.
437 * 'ut_id_encstr' is the character set on which modulo arithmetic is done.
438 * Example: digits would produce the base-10 numbers from '001'.
440 nbase = strlen(ut_id_encstr);
442 fourbyte[3] = ut_id_encstr[i % nbase];
444 fourbyte[2] = ut_id_encstr[i % nbase];
447 return(i); /* 0: good; else overflow */
449 #endif /* defined(HAVE_UT_UT_ID) */
453 fill a system utmp structure given all the info we can gather
455 static BOOL sys_utmp_fill(struct utmp *u,
456 const char *username, const char *hostname,
457 struct in_addr *ipaddr,
458 const char *id_str, int id_num)
460 struct timeval timeval;
464 * Several (all?) systems seems to define one as the other.
465 * It is easier and clearer simply to let the following take its course,
466 * rather than to try to detect and optimise.
468 #if defined(HAVE_UT_UT_USER)
469 utmp_strcpy(u->ut_user, username, sizeof(u->ut_user));
470 #elif defined(HAVE_UT_UT_NAME)
471 utmp_strcpy(u->ut_name, username, sizeof(u->ut_name));
476 * If size limit proves troublesome, then perhaps use "ut_id_encode()".
478 if (strlen(id_str) > sizeof(u->ut_line)) {
479 DEBUG(1,("id_str [%s] is too long for %d char utmp field\n",
480 id_str, sizeof(u->ut_line)));
483 utmp_strcpy(u->ut_line, id_str, sizeof(u->ut_line));
485 #if defined(HAVE_UT_UT_PID)
486 u->ut_pid = sys_getpid();
491 * Some have one, some the other. Many have both, but defined (aliased).
492 * It is easier and clearer simply to let the following take its course.
493 * But note that we do the more precise ut_tv as the final assignment.
495 #if defined(HAVE_UT_UT_TIME)
496 gettimeofday(&timeval, NULL);
497 u->ut_time = timeval.tv_sec;
498 #elif defined(HAVE_UT_UT_TV)
499 gettimeofday(&timeval, NULL);
502 #error "with-utmp must have UT_TIME or UT_TV"
505 #if defined(HAVE_UT_UT_HOST)
506 utmp_strcpy(u->ut_host, hostname, sizeof(u->ut_host));
508 #if defined(HAVE_UT_UT_ADDR)
510 u->ut_addr = ipaddr->s_addr;
512 * "(unsigned long) ut_addr" apparently exists on at least HP-UX 10.20.
513 * Volunteer to implement, please ...
517 #if defined(HAVE_UT_UT_ID)
518 if (ut_id_encode(id_num, u->ut_id) != 0) {
519 DEBUG(1,("utmp_fill: cannot encode id %d\n", id_num));
527 /****************************************************************************
529 ****************************************************************************/
531 void sys_utmp_yield(const char *username, const char *hostname,
532 struct in_addr *ipaddr,
533 const char *id_str, int id_num)
539 #if defined(HAVE_UT_UT_EXIT)
540 u.ut_exit.e_termination = 0;
541 u.ut_exit.e_exit = 0;
544 #if defined(HAVE_UT_UT_TYPE)
545 u.ut_type = DEAD_PROCESS;
548 if (!sys_utmp_fill(&u, username, hostname, ipaddr, id_str, id_num)) return;
550 sys_utmp_update(&u, NULL, False);
553 /****************************************************************************
554 Claim a entry in whatever utmp system the OS uses.
555 ****************************************************************************/
557 void sys_utmp_claim(const char *username, const char *hostname,
558 struct in_addr *ipaddr,
559 const char *id_str, int id_num)
565 #if defined(HAVE_UT_UT_TYPE)
566 u.ut_type = USER_PROCESS;
569 if (!sys_utmp_fill(&u, username, hostname, ipaddr, id_str, id_num)) return;
571 sys_utmp_update(&u, hostname, True);
574 #else /* WITH_UTMP */
575 void dummy_utmp(void) {}