2 Unix SMB/Netbios implementation.
5 Copyright (C) T.D.Lee@durham.ac.uk 1999
6 Heavily modified by Andrew Bartlett and Tridge, April 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /****************************************************************************
28 Reflect connection status in utmp/wtmp files.
29 T.D.Lee@durham.ac.uk September 1999
31 With grateful thanks since then to many who have helped port it to
32 different operating systems. The variety of OS quirks thereby
33 uncovered is amazing...
36 o Always attempt to use programmatic interface (pututline() etc.)
37 Indeed, at present only programmatic use is supported.
38 o The only currently supported programmatic interface to "wtmp{,x}"
39 is through "updwtmp*()" routines.
40 o The "x" (utmpx/wtmpx; HAVE_UTMPX_H) seems preferable.
41 o The HAVE_* items should identify supported features.
42 o If at all possible, avoid "if defined(MY-OS)" constructions.
44 OS observations and status:
45 Almost every OS seems to have its own quirks.
48 Tested on 2.6 and 2.7; should be OK on other flavours.
50 Apparently has utmpx.h but doesn't implement.
52 Has utmpx.h, but (e.g.) no "getutmpx()". (Is this like AIX ?)
54 utmpx.h seems not to set default filenames. non-x better.
56 Not tested. Appears to have "x".
58 Not tested. Appears to lack "x".
61 "updwtmp*()" routines seem absent, so no current wtmp* support.
62 Has "ut_addr": probably trivial to implement (although remember
63 that IPv6 is coming...).
66 No "putut*()" type of interface.
67 No "ut_type" and associated defines.
68 Write files directly. Alternatively use its login(3)/logout(3).
70 Not tested. Resembles FreeBSD, but no login()/logout().
73 Should "lastlog" files, if any, be updated?
74 BSD systems (SunOS 4, FreeBSD):
75 o Prominent mention on man pages.
76 System-V (e.g. Solaris 2):
77 o No mention on man pages, even under "man -k".
78 o Has a "/var/adm/lastlog" file, but pututxline() etc. seem
80 o Despite downplaying (above), nevertheless has <lastlog.h>.
81 So perhaps UN*X "lastlog" facility is intended for tty/terminal only?
84 Each connection requires a small number (starting at 0, working up)
85 to represent the line (unum). This must be unique within and across
88 The 4 byte 'ut_id' component is vital to distinguish connections,
89 of which there could be several hundered or even thousand.
90 Entries seem to be printable characters, with optional NULL pads.
92 We need to be distinct from other entries in utmp/wtmp.
94 Observed things: therefore avoid them. Add to this list please.
95 From Solaris 2.x (because that's what I have):
96 'sN' : run-levels; N: [0-9]
98 'CC' : arbitrary things; C: [a-z]
99 'rXNN' : rlogin; N: [0-9]; X: [0-9a-z]
100 'tXNN' : rlogin; N: [0-9]; X: [0-9a-z]
102 'ftpZ' : ftp (Z is the number 255, aka 0377, aka 0xff)
103 Mostly a record uses the same 'ut_id' in both "utmp" and "wtmp",
104 but differences have been seen.
106 Arbitrarily I have chosen to use a distinctive 'SM' for the
109 The remaining two encode the "unum" (see above).
111 For "utmp consolidate" the suggestion was made to encode the pid into
112 those remaining two bytes (16 bits). But recent UNIX (e.g Solaris 8)
113 is migrating to pids > 16 bits, so we ought not to do this.
115 ****************************************************************************/
123 /* BSD systems: some may need lastlog.h (SunOS 4), some may not (FreeBSD) */
124 /* Some System-V systems (e.g. Solaris 2) declare this too. */
125 #ifdef HAVE_LASTLOG_H
129 /****************************************************************************
130 Obtain/release a small number (0 upwards) unique within and across smbds.
131 ****************************************************************************/
133 * Need a "small" number to represent this connection, unique within this
134 * smbd and across all smbds.
137 * Start at 0, hunt up for free, unique number "unum" by attempting to
138 * store it as a key in a tdb database:
139 * key: unum data: pid+conn
140 * Also store its inverse, ready for yield function:
141 * key: pid+conn data: unum
144 * Find key: pid+conn; data is unum; delete record
145 * Find key: unum ; delete record.
148 * The claim algorithm (a "for" loop attempting to store numbers in a tdb
149 * database) will be increasingly inefficient with larger numbers of
150 * connections. Is it possible to write a suitable primitive within tdb?
152 * However, by also storing the inverse key/data pair, we at least make
153 * the yield algorithm efficient.
156 /****************************************************************************
157 Default paths to various {u,w}tmp{,x} files.
158 ****************************************************************************/
162 static const char *ux_pathname
=
163 # if defined (UTMPX_FILE)
165 # elif defined (_UTMPX_FILE)
167 # elif defined (_PATH_UTMPX)
173 static const char *wx_pathname
=
174 # if defined (WTMPX_FILE)
176 # elif defined (_WTMPX_FILE)
178 # elif defined (_PATH_WTMPX)
184 #endif /* HAVE_UTMPX_H */
186 static const char *ut_pathname
=
187 # if defined (UTMP_FILE)
189 # elif defined (_UTMP_FILE)
191 # elif defined (_PATH_UTMP)
197 static const char *wt_pathname
=
198 # if defined (WTMP_FILE)
200 # elif defined (_WTMP_FILE)
202 # elif defined (_PATH_WTMP)
208 /* BSD-like systems might want "lastlog" support. */
209 /* *** Not yet implemented */
210 #ifndef HAVE_PUTUTLINE /* see "pututline_my()" */
211 static const char *ll_pathname
=
212 # if defined (_PATH_LASTLOG) /* what other names (if any?) */
216 # endif /* _PATH_LASTLOG */
217 #endif /* HAVE_PUTUTLINE */
220 * Get name of {u,w}tmp{,x} file.
221 * return: fname contains filename
222 * Possibly empty if this code not yet ported to this system.
224 * utmp{,x}: try "utmp dir", then default (a define)
225 * wtmp{,x}: try "wtmp dir", then "utmp dir", then default (a define)
227 static void uw_pathname(pstring fname
, const char *uw_name
, const char *uw_default
)
231 pstrcpy(dirname
, "");
233 /* For w-files, first look for explicit "wtmp dir" */
234 if (uw_name
[0] == 'w') {
235 pstrcpy(dirname
,lp_wtmpdir());
236 trim_string(dirname
,"","/");
239 /* For u-files and non-explicit w-dir, look for "utmp dir" */
240 if (dirname
== 0 || strlen(dirname
) == 0) {
241 pstrcpy(dirname
,lp_utmpdir());
242 trim_string(dirname
,"","/");
245 /* If explicit directory above, use it */
246 if (dirname
!= 0 && strlen(dirname
) != 0) {
247 pstrcpy(fname
, dirname
);
249 pstrcat(fname
, uw_name
);
253 /* No explicit directory: attempt to use default paths */
254 if (strlen(uw_default
) == 0) {
255 /* No explicit setting, no known default.
256 * Has it yet been ported to this OS?
258 DEBUG(2,("uw_pathname: unable to determine pathname\n"));
260 pstrcpy(fname
, uw_default
);
263 #ifndef HAVE_PUTUTLINE
265 /****************************************************************************
266 Update utmp file directly. No subroutine interface: probably a BSD system.
267 ****************************************************************************/
269 static void pututline_my(pstring uname
, struct utmp
*u
, BOOL claim
)
271 DEBUG(1,("pututline_my: not yet implemented\n"));
272 /* BSD implementor: may want to consider (or not) adjusting "lastlog" */
274 #endif /* HAVE_PUTUTLINE */
278 /****************************************************************************
279 Update wtmp file directly. No subroutine interface: probably a BSD system.
280 Credit: Michail Vidiassov <master@iaas.msu.ru>
281 ****************************************************************************/
283 static void updwtmp_my(pstring wname
, struct utmp
*u
, BOOL claim
)
291 * may use empty ut_name to distinguish a logout record.
293 * May need "if defined(SUNOS4)" etc. around some of these,
294 * but try to avoid if possible.
297 * man page indicates ut_name and ut_host both NULL
299 * man page appears not to specify (hints non-NULL)
300 * A correspondent suggest at least ut_name should be NULL
302 memset((char *)&u
->ut_name
, '\0', sizeof(u
->ut_name
));
303 memset((char *)&u
->ut_host
, '\0', sizeof(u
->ut_host
));
305 /* Stolen from logwtmp function in libutil.
306 * May be more locking/blocking is needed?
308 if ((fd
= open(wname
, O_WRONLY
|O_APPEND
, 0)) < 0)
310 if (fstat(fd
, &buf
) == 0) {
311 if (write(fd
, (char *)u
, sizeof(struct utmp
)) != sizeof(struct utmp
))
312 (void) ftruncate(fd
, buf
.st_size
);
316 #endif /* HAVE_UPDWTMP */
318 /****************************************************************************
319 Update via utmp/wtmp (not utmpx/wtmpx).
320 ****************************************************************************/
322 static void utmp_nox_update(struct utmp
*u
, BOOL claim
)
324 pstring uname
, wname
;
325 #if defined(PUTUTLINE_RETURNS_UTMP)
327 #endif /* PUTUTLINE_RETURNS_UTMP */
329 uw_pathname(uname
, "utmp", ut_pathname
);
330 DEBUG(2,("utmp_nox_update: uname:%s\n", uname
));
332 #ifdef HAVE_PUTUTLINE
333 if (strlen(uname
) != 0) {
337 # if defined(PUTUTLINE_RETURNS_UTMP)
342 DEBUG(2,("utmp_nox_update: pututline() failed\n"));
345 # else /* PUTUTLINE_RETURNS_UTMP */
349 # endif /* PUTUTLINE_RETURNS_UTMP */
351 #else /* HAVE_PUTUTLINE */
352 if (strlen(uname
) != 0) {
353 pututline_my(uname
, u
, claim
);
355 #endif /* HAVE_PUTUTLINE */
357 uw_pathname(wname
, "wtmp", wt_pathname
);
358 DEBUG(2,("utmp_nox_update: wname:%s\n", wname
));
359 if (strlen(wname
) != 0) {
363 * updwtmp() and the newer updwtmpx() may be unsymmetrical.
364 * At least one OS, Solaris 2.x declares the former in the
365 * "utmpx" (latter) file and context.
366 * In the Solaris case this is irrelevant: it has both and
367 * we always prefer the "x" case, so doesn't come here.
368 * But are there other systems, with no "x", which lack
372 updwtmp_my(wname
, u
, claim
);
373 #endif /* HAVE_UPDWTMP */
377 /****************************************************************************
378 Copy a string in the utmp structure.
379 ****************************************************************************/
381 static void utmp_strcpy(char *dest
, const char *src
, size_t n
)
385 memset(dest
, '\0', n
);
389 memcpy(dest
, src
, n
);
392 memcpy(dest
, src
, len
);
396 /****************************************************************************
397 Update via utmpx/wtmpx (preferred) or via utmp/wtmp.
398 ****************************************************************************/
400 static void sys_utmp_update(struct utmp
*u
, const char *hostname
, BOOL claim
)
402 #if !defined(HAVE_UTMPX_H)
403 /* No utmpx stuff. Drop to non-x stuff */
404 utmp_nox_update(u
, claim
);
405 #elif !defined(HAVE_PUTUTXLINE)
406 /* Odd. Have utmpx.h but no "pututxline()". Drop to non-x stuff */
407 DEBUG(1,("utmp_update: have utmpx.h but no pututxline() function\n"));
408 utmp_nox_update(u
, claim
);
409 #elif !defined(HAVE_GETUTMPX)
410 /* Odd. Have utmpx.h but no "getutmpx()". Drop to non-x stuff */
411 DEBUG(1,("utmp_update: have utmpx.h but no getutmpx() function\n"));
412 utmp_nox_update(u
, claim
);
414 pstring uname
, wname
;
415 struct utmpx ux
, *uxrc
;
419 #if defined(HAVE_UX_UT_SYSLEN)
421 ux
.ut_syslen
= strlen(hostname
) + 1; /* include end NULL */
425 utmp_strcpy(ux
.ut_host
, hostname
, sizeof(ux
.ut_host
));
427 uw_pathname(uname
, "utmpx", ux_pathname
);
428 uw_pathname(wname
, "wtmpx", wx_pathname
);
429 DEBUG(2,("utmp_update: uname:%s wname:%s\n", uname
, wname
));
431 * Check for either uname or wname being empty.
432 * Some systems, such as Redhat 6, have a "utmpx.h" which doesn't
433 * define default filenames.
434 * Also, our local installation has not provided an override.
435 * Drop to non-x method. (E.g. RH6 has good defaults in "utmp.h".)
437 if ((strlen(uname
) == 0) || (strlen(wname
) == 0)) {
438 utmp_nox_update(u
, claim
);
442 uxrc
= pututxline(&ux
);
445 DEBUG(2,("utmp_update: pututxline() failed\n"));
448 updwtmpx(wname
, &ux
);
450 #endif /* HAVE_UTMPX_H */
453 #if defined(HAVE_UT_UT_ID)
454 /****************************************************************************
455 Encode the unique connection number into "ut_id".
456 ****************************************************************************/
458 static int ut_id_encode(int i
, char *fourbyte
)
461 char *ut_id_encstr
= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
467 * Encode remaining 2 bytes from 'i'.
468 * 'ut_id_encstr' is the character set on which modulo arithmetic is done.
469 * Example: digits would produce the base-10 numbers from '001'.
471 nbase
= strlen(ut_id_encstr
);
473 fourbyte
[3] = ut_id_encstr
[i
% nbase
];
475 fourbyte
[2] = ut_id_encstr
[i
% nbase
];
478 return(i
); /* 0: good; else overflow */
480 #endif /* defined(HAVE_UT_UT_ID) */
484 fill a system utmp structure given all the info we can gather
486 static BOOL
sys_utmp_fill(struct utmp
*u
,
487 const char *username
, const char *hostname
,
488 const char *id_str
, int id_num
)
490 struct timeval timeval
;
494 * Several (all?) systems seems to define one as the other.
495 * It is easier and clearer simply to let the following take its course,
496 * rather than to try to detect and optimise.
498 #if defined(HAVE_UT_UT_USER)
499 utmp_strcpy(u
->ut_user
, username
, sizeof(u
->ut_user
));
500 #elif defined(HAVE_UT_UT_NAME)
501 utmp_strcpy(u
->ut_name
, username
, sizeof(u
->ut_name
));
506 * If size limit proves troublesome, then perhaps use "ut_id_encode()".
508 * Temporary variable "line_tmp" avoids trouble:
509 * o with unwanted trailing NULL if ut_line full;
510 * o with overflow if ut_line would be more than full.
512 if (strlen(id_str
) > sizeof(u
->ut_line
)) {
513 DEBUG(1,("id_str [%s] is too long for %d char utmp field\n",
514 id_str
, sizeof(u
->ut_line
)));
517 utmp_strcpy(u
->ut_line
, id_str
, sizeof(u
->ut_line
));
519 #if defined(HAVE_UT_UT_PID)
520 u
->ut_pid
= sys_getpid();
525 * Some have one, some the other. Many have both, but defined (aliased).
526 * It is easier and clearer simply to let the following take its course.
527 * But note that we do the more precise ut_tv as the final assignment.
529 #if defined(HAVE_UT_UT_TIME)
530 gettimeofday(&timeval
, NULL
);
531 u
->ut_time
= timeval
.tv_sec
;
532 #elif defined(HAVE_UT_UT_TV)
533 gettimeofday(&timeval
, NULL
);
536 #error "with-utmp must have UT_TIME or UT_TV"
539 #if defined(HAVE_UT_UT_HOST)
540 utmp_strcpy(u
->ut_host
, hostname
, sizeof(u
->ut_host
));
543 #if defined(HAVE_UT_UT_ADDR)
545 * "(unsigned long) ut_addr" apparently exists on at least HP-UX 10.20.
546 * Volunteer to implement, please ...
550 #if defined(HAVE_UT_UT_ID)
551 if (ut_id_encode(id_num
, u
->ut_id
) != 0) {
552 DEBUG(1,("utmp_fill: cannot encode id %d\n", id_num
));
560 /****************************************************************************
562 ****************************************************************************/
564 void sys_utmp_yield(const char *username
, const char *hostname
,
565 const char *id_str
, int id_num
)
571 #if defined(HAVE_UT_UT_EXIT)
572 u
.ut_exit
.e_termination
= 0;
573 u
.ut_exit
.e_exit
= 0;
576 #if defined(HAVE_UT_UT_TYPE)
577 u
.ut_type
= DEAD_PROCESS
;
580 if (!sys_utmp_fill(&u
, username
, hostname
, id_str
, id_num
)) return;
582 sys_utmp_update(&u
, NULL
, False
);
585 /****************************************************************************
586 Claim a entry in whatever utmp system the OS uses.
587 ****************************************************************************/
589 void sys_utmp_claim(const char *username
, const char *hostname
,
590 const char *id_str
, int id_num
)
596 #if defined(HAVE_UT_UT_TYPE)
597 u
.ut_type
= USER_PROCESS
;
600 if (!sys_utmp_fill(&u
, username
, hostname
, id_str
, id_num
)) return;
602 sys_utmp_update(&u
, hostname
, True
);
605 #else /* WITH_UTMP */
606 void dummy_utmp(void) {}