s3:smbd: change blocking.c to use fsp_fnum_dbg() for fsp->fnum logging.
[Samba/gebeck_regimport.git] / source3 / smbd / utmp.c
blob34b77616b6a61d9e0af6ed06414c5efe86245155
1 /*
2 Unix SMB/CIFS implementation.
3 utmp routines
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
25 /****************************************************************************
26 Reflect connection status in utmp/wtmp files.
27 T.D.Lee@durham.ac.uk September 1999
29 With grateful thanks since then to many who have helped port it to
30 different operating systems. The variety of OS quirks thereby
31 uncovered is amazing...
33 Hints for porting:
34 o Always attempt to use programmatic interface (pututline() etc.)
35 Indeed, at present only programmatic use is supported.
36 o The only currently supported programmatic interface to "wtmp{,x}"
37 is through "updwtmp*()" routines.
38 o The "x" (utmpx/wtmpx; HAVE_UTMPX_H) seems preferable.
39 o The HAVE_* items should identify supported features.
40 o If at all possible, avoid "if defined(MY-OS)" constructions.
42 OS observations and status:
43 Almost every OS seems to have its own quirks.
45 Solaris 2.x:
46 Tested on 2.6 and 2.7; should be OK on other flavours.
47 AIX:
48 Apparently has utmpx.h but doesn't implement.
49 OSF:
50 Has utmpx.h, but (e.g.) no "getutmpx()". (Is this like AIX ?)
51 Redhat 6:
52 utmpx.h seems not to set default filenames. non-x better.
53 IRIX 6.5:
54 Not tested. Appears to have "x".
55 HP-UX 9.x:
56 Not tested. Appears to lack "x".
57 HP-UX 10.x:
58 Not tested.
59 "updwtmp*()" routines seem absent, so no current wtmp* support.
60 Has "ut_addr": probably trivial to implement (although remember
61 that IPv6 is coming...).
63 FreeBSD:
64 No "putut*()" type of interface.
65 No "ut_type" and associated defines.
66 Write files directly. Alternatively use its login(3)/logout(3).
67 SunOS 4:
68 Not tested. Resembles FreeBSD, but no login()/logout().
70 lastlog:
71 Should "lastlog" files, if any, be updated?
72 BSD systems (SunOS 4, FreeBSD):
73 o Prominent mention on man pages.
74 System-V (e.g. Solaris 2):
75 o No mention on man pages, even under "man -k".
76 o Has a "/var/adm/lastlog" file, but pututxline() etc. seem
77 not to touch it.
78 o Despite downplaying (above), nevertheless has <lastlog.h>.
79 So perhaps UN*X "lastlog" facility is intended for tty/terminal only?
81 Notes:
82 Each connection requires a small number (starting at 0, working up)
83 to represent the line. This must be unique within and across all
84 smbd processes. It is the 'id_num' from Samba's session.c code.
86 The 4 byte 'ut_id' component is vital to distinguish connections,
87 of which there could be several hundred or even thousand.
88 Entries seem to be printable characters, with optional NULL pads.
90 We need to be distinct from other entries in utmp/wtmp.
92 Observed things: therefore avoid them. Add to this list please.
93 From Solaris 2.x (because that's what I have):
94 'sN' : run-levels; N: [0-9]
95 'co' : console
96 'CC' : arbitrary things; C: [a-z]
97 'rXNN' : rlogin; N: [0-9]; X: [0-9a-z]
98 'tXNN' : rlogin; N: [0-9]; X: [0-9a-z]
99 '/NNN' : Solaris CDE
100 'ftpZ' : ftp (Z is the number 255, aka 0377, aka 0xff)
101 Mostly a record uses the same 'ut_id' in both "utmp" and "wtmp",
102 but differences have been seen.
104 Arbitrarily I have chosen to use a distinctive 'SM' for the
105 first two bytes.
107 The remaining two bytes encode the session 'id_num' (see above).
108 Our caller (session.c) should note our 16-bit limitation.
110 ****************************************************************************/
112 #ifndef WITH_UTMP
114 * Not WITH_UTMP? Simply supply dummy routines.
117 void sys_utmp_claim(const char *username, const char *hostname,
118 const char *ip_addr_str,
119 const char *id_str, int id_num)
122 void sys_utmp_yield(const char *username, const char *hostname,
123 const char *ip_addr_str,
124 const char *id_str, int id_num)
127 #else /* WITH_UTMP */
129 #ifdef HAVE_UTMP_H
130 #include <utmp.h>
131 #endif
133 #ifdef HAVE_UTMPX_H
134 #include <utmpx.h>
135 #endif
137 /* BSD systems: some may need lastlog.h (SunOS 4), some may not (FreeBSD) */
138 /* Some System-V systems (e.g. Solaris 2) declare this too. */
139 #ifdef HAVE_LASTLOG_H
140 #include <lastlog.h>
141 #endif
143 /****************************************************************************
144 Default paths to various {u,w}tmp{,x} files.
145 ****************************************************************************/
147 #ifdef HAVE_UTMPX_H
149 static const char * const ux_pathname =
150 # if defined (UTMPX_FILE)
151 UTMPX_FILE ;
152 # elif defined (_UTMPX_FILE)
153 _UTMPX_FILE ;
154 # elif defined (_PATH_UTMPX)
155 _PATH_UTMPX ;
156 # else
157 "" ;
158 # endif
160 static const char * const wx_pathname =
161 # if defined (WTMPX_FILE)
162 WTMPX_FILE ;
163 # elif defined (_WTMPX_FILE)
164 _WTMPX_FILE ;
165 # elif defined (_PATH_WTMPX)
166 _PATH_WTMPX ;
167 # else
168 "" ;
169 # endif
171 #endif /* HAVE_UTMPX_H */
173 static const char * const ut_pathname =
174 # if defined (UTMP_FILE)
175 UTMP_FILE ;
176 # elif defined (_UTMP_FILE)
177 _UTMP_FILE ;
178 # elif defined (_PATH_UTMP)
179 _PATH_UTMP ;
180 # else
181 "" ;
182 # endif
184 static const char * const wt_pathname =
185 # if defined (WTMP_FILE)
186 WTMP_FILE ;
187 # elif defined (_WTMP_FILE)
188 _WTMP_FILE ;
189 # elif defined (_PATH_WTMP)
190 _PATH_WTMP ;
191 # else
192 "" ;
193 # endif
195 /* BSD-like systems might want "lastlog" support. */
196 #if 0 /* *** Not yet implemented */
197 #ifndef HAVE_PUTUTLINE /* see "pututline_my()" */
198 static const char *ll_pathname =
199 # if defined (_PATH_LASTLOG) /* what other names (if any?) */
200 _PATH_LASTLOG ;
201 # else
202 "" ;
203 # endif /* _PATH_LASTLOG */
204 #endif /* HAVE_PUTUTLINE */
205 #endif
208 * Get name of {u,w}tmp{,x} file.
209 * return: fname contains filename
210 * Possibly empty if this code not yet ported to this system.
212 * utmp{,x}: try "utmp dir", then default (a define)
213 * wtmp{,x}: try "wtmp dir", then "utmp dir", then default (a define)
215 static char *uw_pathname(TALLOC_CTX *ctx,
216 const char *uw_name,
217 const char *uw_default)
219 char *dirname = NULL;
221 /* For w-files, first look for explicit "wtmp dir" */
222 if (uw_name[0] == 'w') {
223 dirname = talloc_strdup(ctx, lp_wtmpdir());
224 if (!dirname) {
225 return NULL;
227 trim_char(dirname,'\0','/');
230 /* For u-files and non-explicit w-dir, look for "utmp dir" */
231 if ((dirname == NULL) || (strlen(dirname) == 0)) {
232 dirname = talloc_strdup(ctx, lp_utmpdir());
233 if (!dirname) {
234 return NULL;
236 trim_char(dirname,'\0','/');
239 /* If explicit directory above, use it */
240 if (dirname && strlen(dirname) != 0) {
241 return talloc_asprintf(ctx,
242 "%s/%s",
243 dirname,
244 uw_name);
247 /* No explicit directory: attempt to use default paths */
248 if (strlen(uw_default) == 0) {
249 /* No explicit setting, no known default.
250 * Has it yet been ported to this OS?
252 DEBUG(2,("uw_pathname: unable to determine pathname\n"));
254 return talloc_strdup(ctx, uw_default);
257 #ifndef HAVE_PUTUTLINE
258 /****************************************************************************
259 Update utmp file directly. No subroutine interface: probably a BSD system.
260 ****************************************************************************/
262 static void pututline_my(const char *uname, struct utmp *u, bool claim)
264 DEBUG(1,("pututline_my: not yet implemented\n"));
265 /* BSD implementor: may want to consider (or not) adjusting "lastlog" */
267 #endif /* HAVE_PUTUTLINE */
269 #ifndef HAVE_UPDWTMP
271 /****************************************************************************
272 Update wtmp file directly. No subroutine interface: probably a BSD system.
273 Credit: Michail Vidiassov <master@iaas.msu.ru>
274 ****************************************************************************/
276 static void updwtmp_my(const char *wname, struct utmp *u, bool claim)
278 int fd;
279 struct stat buf;
281 if (! claim) {
283 * BSD-like systems:
284 * may use empty ut_name to distinguish a logout record.
286 * May need "if defined(SUNOS4)" etc. around some of these,
287 * but try to avoid if possible.
289 * SunOS 4:
290 * man page indicates ut_name and ut_host both NULL
291 * FreeBSD 4.0:
292 * man page appears not to specify (hints non-NULL)
293 * A correspondent suggest at least ut_name should be NULL
295 #if defined(HAVE_UT_UT_NAME)
296 memset((char *)&u->ut_name, '\0', sizeof(u->ut_name));
297 #endif
298 #if defined(HAVE_UT_UT_HOST)
299 memset((char *)&u->ut_host, '\0', sizeof(u->ut_host));
300 #endif
302 /* Stolen from logwtmp function in libutil.
303 * May be more locking/blocking is needed?
305 if ((fd = open(wname, O_WRONLY|O_APPEND, 0)) < 0)
306 return;
307 if (fstat(fd, &buf) == 0) {
308 if (write(fd, (char *)u, sizeof(struct utmp)) != sizeof(struct utmp))
309 (void) ftruncate(fd, buf.st_size);
311 (void) close(fd);
313 #endif /* HAVE_UPDWTMP */
315 /****************************************************************************
316 Update via utmp/wtmp (not utmpx/wtmpx).
317 ****************************************************************************/
319 static void utmp_nox_update(struct utmp *u, bool claim)
321 char *uname = NULL;
322 char *wname = NULL;
323 #if defined(PUTUTLINE_RETURNS_UTMP)
324 struct utmp *urc;
325 #endif /* PUTUTLINE_RETURNS_UTMP */
327 uname = uw_pathname(talloc_tos(), "utmp", ut_pathname);
328 if (!uname) {
329 return;
331 DEBUG(2,("utmp_nox_update: uname:%s\n", uname));
333 #ifdef HAVE_PUTUTLINE
334 if (strlen(uname) != 0) {
335 utmpname(uname);
338 # if defined(PUTUTLINE_RETURNS_UTMP)
339 setutent();
340 urc = pututline(u);
341 endutent();
342 if (urc == NULL) {
343 DEBUG(2,("utmp_nox_update: pututline() failed\n"));
344 return;
346 # else /* PUTUTLINE_RETURNS_UTMP */
347 setutent();
348 pututline(u);
349 endutent();
350 # endif /* PUTUTLINE_RETURNS_UTMP */
352 #else /* HAVE_PUTUTLINE */
353 if (strlen(uname) != 0) {
354 pututline_my(uname, u, claim);
356 #endif /* HAVE_PUTUTLINE */
358 wname = uw_pathname(talloc_tos(), "wtmp", wt_pathname);
359 if (!wname) {
360 return;
362 DEBUG(2,("utmp_nox_update: wname:%s\n", wname));
363 if (strlen(wname) != 0) {
364 #ifdef HAVE_UPDWTMP
365 updwtmp(wname, u);
367 * updwtmp() and the newer updwtmpx() may be unsymmetrical.
368 * At least one OS, Solaris 2.x declares the former in the
369 * "utmpx" (latter) file and context.
370 * In the Solaris case this is irrelevant: it has both and
371 * we always prefer the "x" case, so doesn't come here.
372 * But are there other systems, with no "x", which lack
373 * updwtmp() perhaps?
375 #else
376 updwtmp_my(wname, u, claim);
377 #endif /* HAVE_UPDWTMP */
381 /****************************************************************************
382 Copy a string in the utmp structure.
383 ****************************************************************************/
385 static void utmp_strcpy(char *dest, const char *src, size_t n)
387 size_t len = 0;
389 memset(dest, '\0', n);
390 if (src)
391 len = strlen(src);
392 if (len >= n) {
393 memcpy(dest, src, n);
394 } else {
395 if (len)
396 memcpy(dest, src, len);
400 /****************************************************************************
401 Update via utmpx/wtmpx (preferred) or via utmp/wtmp.
402 ****************************************************************************/
404 static void sys_utmp_update(struct utmp *u, const char *hostname, bool claim)
406 #if !defined(HAVE_UTMPX_H)
407 /* No utmpx stuff. Drop to non-x stuff */
408 utmp_nox_update(u, claim);
409 #elif !defined(HAVE_PUTUTXLINE)
410 /* Odd. Have utmpx.h but no "pututxline()". Drop to non-x stuff */
411 DEBUG(1,("utmp_update: have utmpx.h but no pututxline() function\n"));
412 utmp_nox_update(u, claim);
413 #elif !defined(HAVE_GETUTMPX)
414 /* Odd. Have utmpx.h but no "getutmpx()". Drop to non-x stuff */
415 DEBUG(1,("utmp_update: have utmpx.h but no getutmpx() function\n"));
416 utmp_nox_update(u, claim);
417 #elif !defined(HAVE_UPDWTMPX)
418 /* Have utmpx.h but no "updwtmpx()". Drop to non-x stuff */
419 DEBUG(1,("utmp_update: have utmpx.h but no updwtmpx() function\n"));
420 utmp_nox_update(u, claim);
421 #else
422 char *uname = NULL;
423 char *wname = NULL;
424 struct utmpx ux, *uxrc;
426 getutmpx(u, &ux);
428 #if defined(HAVE_UX_UT_SYSLEN)
429 if (hostname)
430 ux.ut_syslen = strlen(hostname) + 1; /* include end NULL */
431 else
432 ux.ut_syslen = 0;
433 #endif
434 #if defined(HAVE_UT_UT_HOST)
435 utmp_strcpy(ux.ut_host, hostname, sizeof(ux.ut_host));
436 #endif
438 uname = uw_pathname(talloc_tos(), "utmpx", ux_pathname);
439 wname = uw_pathname(talloc_tos(), "wtmpx", wx_pathname);
440 if (uname && wname) {
441 DEBUG(2,("utmp_update: uname:%s wname:%s\n", uname, wname));
445 * Check for either uname or wname being empty.
446 * Some systems, such as Redhat 6, have a "utmpx.h" which doesn't
447 * define default filenames.
448 * Also, our local installation has not provided an override.
449 * Drop to non-x method. (E.g. RH6 has good defaults in "utmp.h".)
451 if (!uname || !wname || (strlen(uname) == 0) || (strlen(wname) == 0)) {
452 utmp_nox_update(u, claim);
453 } else {
454 utmpxname(uname);
455 setutxent();
456 uxrc = pututxline(&ux);
457 endutxent();
458 if (uxrc == NULL) {
459 DEBUG(2,("utmp_update: pututxline() failed\n"));
460 return;
462 updwtmpx(wname, &ux);
464 #endif /* HAVE_UTMPX_H */
467 #if defined(HAVE_UT_UT_ID)
468 /****************************************************************************
469 Encode the unique connection number into "ut_id".
470 ****************************************************************************/
472 static int ut_id_encode(int i, char *fourbyte)
474 int nbase;
475 const char *ut_id_encstr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
477 fourbyte[0] = 'S';
478 fourbyte[1] = 'M';
481 * Encode remaining 2 bytes from 'i'.
482 * 'ut_id_encstr' is the character set on which modulo arithmetic is done.
483 * Example: digits would produce the base-10 numbers from '001'.
485 nbase = strlen(ut_id_encstr);
487 fourbyte[3] = ut_id_encstr[i % nbase];
488 i /= nbase;
489 fourbyte[2] = ut_id_encstr[i % nbase];
490 i /= nbase;
492 return(i); /* 0: good; else overflow */
494 #endif /* defined(HAVE_UT_UT_ID) */
498 fill a system utmp structure given all the info we can gather
500 static bool sys_utmp_fill(struct utmp *u,
501 const char *username, const char *hostname,
502 const char *ip_addr_str,
503 const char *id_str, int id_num)
505 struct timeval timeval;
508 * ut_name, ut_user:
509 * Several (all?) systems seems to define one as the other.
510 * It is easier and clearer simply to let the following take its course,
511 * rather than to try to detect and optimise.
513 #if defined(HAVE_UT_UT_USER)
514 utmp_strcpy(u->ut_user, username, sizeof(u->ut_user));
515 #elif defined(HAVE_UT_UT_NAME)
516 utmp_strcpy(u->ut_name, username, sizeof(u->ut_name));
517 #endif
520 * ut_line:
521 * If size limit proves troublesome, then perhaps use "ut_id_encode()".
523 if (strlen(id_str) > sizeof(u->ut_line)) {
524 DEBUG(1,("id_str [%s] is too long for %lu char utmp field\n",
525 id_str, (unsigned long)sizeof(u->ut_line)));
526 return False;
528 utmp_strcpy(u->ut_line, id_str, sizeof(u->ut_line));
530 #if defined(HAVE_UT_UT_PID)
531 u->ut_pid = getpid();
532 #endif
535 * ut_time, ut_tv:
536 * Some have one, some the other. Many have both, but defined (aliased).
537 * It is easier and clearer simply to let the following take its course.
538 * But note that we do the more precise ut_tv as the final assignment.
540 #if defined(HAVE_UT_UT_TIME)
541 GetTimeOfDay(&timeval);
542 u->ut_time = timeval.tv_sec;
543 #elif defined(HAVE_UT_UT_TV)
544 GetTimeOfDay(&timeval);
545 u->ut_tv = timeval;
546 #else
547 #error "with-utmp must have UT_TIME or UT_TV"
548 #endif
550 #if defined(HAVE_UT_UT_HOST)
551 utmp_strcpy(u->ut_host, hostname, sizeof(u->ut_host));
552 #endif
553 #if defined(HAVE_IPV6) && defined(HAVE_UT_UT_ADDR_V6)
554 memset(&u->ut_addr_v6, '\0', sizeof(u->ut_addr_v6));
555 if (ip_addr_str) {
556 struct in6_addr addr;
557 if (inet_pton(AF_INET6, ip_addr_str, &addr) > 0) {
558 memcpy(&u->ut_addr_v6, &addr, sizeof(addr));
561 #elif defined(HAVE_UT_UT_ADDR)
562 memset(&u->ut_addr, '\0', sizeof(u->ut_addr));
563 if (ip_addr_str) {
564 struct in_addr addr;
565 if (inet_pton(AF_INET, ip_addr_str, &addr) > 0) {
566 memcpy(&u->ut_addr, &addr, sizeof(addr));
570 * "(unsigned long) ut_addr" apparently exists on at least HP-UX 10.20.
571 * Volunteer to implement, please ...
573 #endif
575 #if defined(HAVE_UT_UT_ID)
576 if (ut_id_encode(id_num, u->ut_id) != 0) {
577 DEBUG(1,("utmp_fill: cannot encode id %d\n", id_num));
578 return False;
580 #endif
582 return True;
585 /****************************************************************************
586 Close a connection.
587 ****************************************************************************/
589 void sys_utmp_yield(const char *username, const char *hostname,
590 const char *ip_addr_str,
591 const char *id_str, int id_num)
593 struct utmp u;
595 ZERO_STRUCT(u);
597 #if defined(HAVE_UT_UT_EXIT)
598 u.ut_exit.e_termination = 0;
599 u.ut_exit.e_exit = 0;
600 #endif
602 #if defined(HAVE_UT_UT_TYPE)
603 u.ut_type = DEAD_PROCESS;
604 #endif
606 if (!sys_utmp_fill(&u, username, hostname, ip_addr_str, id_str, id_num))
607 return;
609 sys_utmp_update(&u, NULL, False);
612 /****************************************************************************
613 Claim a entry in whatever utmp system the OS uses.
614 ****************************************************************************/
616 void sys_utmp_claim(const char *username, const char *hostname,
617 const char *ip_addr_str,
618 const char *id_str, int id_num)
620 struct utmp u;
622 ZERO_STRUCT(u);
624 #if defined(HAVE_UT_UT_TYPE)
625 u.ut_type = USER_PROCESS;
626 #endif
628 if (!sys_utmp_fill(&u, username, hostname, ip_addr_str, id_str, id_num))
629 return;
631 sys_utmp_update(&u, hostname, True);
634 #endif /* WITH_UTMP */