trying to get HEAD building again. If you want the code
[Samba.git] / source / smbd / utmp.c
blob9833a11f2d0fb4f7d06dc89f4b032076a3836dce
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 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.
22 #include "includes.h"
24 /****************************************************************************
25 Reflect connection status in utmp/wtmp files.
26 T.D.Lee@durham.ac.uk September 1999
28 With grateful thanks since then to many who have helped port it to
29 different operating systems. The variety of OS quirks thereby
30 uncovered is amazing...
32 Hints for porting:
33 o Always attempt to use programmatic interface (pututline() etc.)
34 Indeed, at present only programmatic use is supported.
35 o The only currently supported programmatic interface to "wtmp{,x}"
36 is through "updwtmp*()" routines.
37 o The "x" (utmpx/wtmpx; HAVE_UTMPX_H) seems preferable.
38 o The HAVE_* items should identify supported features.
39 o If at all possible, avoid "if defined(MY-OS)" constructions.
41 OS observations and status:
42 Almost every OS seems to have its own quirks.
44 Solaris 2.x:
45 Tested on 2.6 and 2.7; should be OK on other flavours.
46 AIX:
47 Apparently has utmpx.h but doesn't implement.
48 OSF:
49 Has utmpx.h, but (e.g.) no "getutmpx()". (Is this like AIX ?)
50 Redhat 6:
51 utmpx.h seems not to set default filenames. non-x better.
52 IRIX 6.5:
53 Not tested. Appears to have "x".
54 HP-UX 9.x:
55 Not tested. Appears to lack "x".
56 HP-UX 10.x:
57 Not tested.
58 "updwtmp*()" routines seem absent, so no current wtmp* support.
59 Has "ut_addr": probably trivial to implement (although remember
60 that IPv6 is coming...).
62 FreeBSD:
63 No "putut*()" type of interface.
64 No "ut_type" and associated defines.
65 Write files directly. Alternatively use its login(3)/logout(3).
66 SunOS 4:
67 Not tested. Resembles FreeBSD, but no login()/logout().
69 lastlog:
70 Should "lastlog" files, if any, be updated?
71 BSD systems (SunOS 4, FreeBSD):
72 o Prominent mention on man pages.
73 System-V (e.g. Solaris 2):
74 o No mention on man pages, even under "man -k".
75 o Has a "/var/adm/lastlog" file, but pututxline() etc. seem
76 not to touch it.
77 o Despite downplaying (above), nevertheless has <lastlog.h>.
78 So perhaps UN*X "lastlog" facility is intended for tty/terminal only?
80 Notes:
81 Each connection requires a small number (starting at 0, working up)
82 to represent the line. This must be unique within and across all
83 smbd processes. It is the 'id_num' from Samba's session.c code.
85 The 4 byte 'ut_id' component is vital to distinguish connections,
86 of which there could be several hundred or even thousand.
87 Entries seem to be printable characters, with optional NULL pads.
89 We need to be distinct from other entries in utmp/wtmp.
91 Observed things: therefore avoid them. Add to this list please.
92 From Solaris 2.x (because that's what I have):
93 'sN' : run-levels; N: [0-9]
94 'co' : console
95 'CC' : arbitrary things; C: [a-z]
96 'rXNN' : rlogin; N: [0-9]; X: [0-9a-z]
97 'tXNN' : rlogin; N: [0-9]; X: [0-9a-z]
98 '/NNN' : Solaris CDE
99 'ftpZ' : ftp (Z is the number 255, aka 0377, aka 0xff)
100 Mostly a record uses the same 'ut_id' in both "utmp" and "wtmp",
101 but differences have been seen.
103 Arbitrarily I have chosen to use a distinctive 'SM' for the
104 first two bytes.
106 The remaining two bytes encode the session 'id_num' (see above).
107 Our caller (session.c) should note our 16-bit limitation.
109 ****************************************************************************/
111 #ifndef WITH_UTMP
113 * Not WITH_UTMP? Simply supply dummy routines.
116 void sys_utmp_claim(const char *username, const char *hostname,
117 struct in_addr *ipaddr,
118 const char *id_str, int id_num)
121 void sys_utmp_yield(const char *username, const char *hostname,
122 struct in_addr *ipaddr,
123 const char *id_str, int id_num)
126 #else /* WITH_UTMP */
128 #include <utmp.h>
130 #ifdef HAVE_UTMPX_H
131 #include <utmpx.h>
132 #endif
134 /* BSD systems: some may need lastlog.h (SunOS 4), some may not (FreeBSD) */
135 /* Some System-V systems (e.g. Solaris 2) declare this too. */
136 #ifdef HAVE_LASTLOG_H
137 #include <lastlog.h>
138 #endif
140 /****************************************************************************
141 Default paths to various {u,w}tmp{,x} files.
142 ****************************************************************************/
144 #ifdef HAVE_UTMPX_H
146 static const char *ux_pathname =
147 # if defined (UTMPX_FILE)
148 UTMPX_FILE ;
149 # elif defined (_UTMPX_FILE)
150 _UTMPX_FILE ;
151 # elif defined (_PATH_UTMPX)
152 _PATH_UTMPX ;
153 # else
154 "" ;
155 # endif
157 static const char *wx_pathname =
158 # if defined (WTMPX_FILE)
159 WTMPX_FILE ;
160 # elif defined (_WTMPX_FILE)
161 _WTMPX_FILE ;
162 # elif defined (_PATH_WTMPX)
163 _PATH_WTMPX ;
164 # else
165 "" ;
166 # endif
168 #endif /* HAVE_UTMPX_H */
170 static const char *ut_pathname =
171 # if defined (UTMP_FILE)
172 UTMP_FILE ;
173 # elif defined (_UTMP_FILE)
174 _UTMP_FILE ;
175 # elif defined (_PATH_UTMP)
176 _PATH_UTMP ;
177 # else
178 "" ;
179 # endif
181 static const char *wt_pathname =
182 # if defined (WTMP_FILE)
183 WTMP_FILE ;
184 # elif defined (_WTMP_FILE)
185 _WTMP_FILE ;
186 # elif defined (_PATH_WTMP)
187 _PATH_WTMP ;
188 # else
189 "" ;
190 # endif
192 /* BSD-like systems might want "lastlog" support. */
193 /* *** Not yet implemented */
194 #ifndef HAVE_PUTUTLINE /* see "pututline_my()" */
195 static const char *ll_pathname =
196 # if defined (_PATH_LASTLOG) /* what other names (if any?) */
197 _PATH_LASTLOG ;
198 # else
199 "" ;
200 # endif /* _PATH_LASTLOG */
201 #endif /* HAVE_PUTUTLINE */
204 * Get name of {u,w}tmp{,x} file.
205 * return: fname contains filename
206 * Possibly empty if this code not yet ported to this system.
208 * utmp{,x}: try "utmp dir", then default (a define)
209 * wtmp{,x}: try "wtmp dir", then "utmp dir", then default (a define)
211 static void uw_pathname(pstring fname, const char *uw_name, const char *uw_default)
213 pstring dirname;
215 pstrcpy(dirname, "");
217 /* For w-files, first look for explicit "wtmp dir" */
218 if (uw_name[0] == 'w') {
219 pstrcpy(dirname,lp_wtmpdir());
220 trim_string(dirname,"","/");
223 /* For u-files and non-explicit w-dir, look for "utmp dir" */
224 if (dirname == 0 || strlen(dirname) == 0) {
225 pstrcpy(dirname,lp_utmpdir());
226 trim_string(dirname,"","/");
229 /* If explicit directory above, use it */
230 if (dirname != 0 && strlen(dirname) != 0) {
231 pstrcpy(fname, dirname);
232 pstrcat(fname, "/");
233 pstrcat(fname, uw_name);
234 return;
237 /* No explicit directory: attempt to use default paths */
238 if (strlen(uw_default) == 0) {
239 /* No explicit setting, no known default.
240 * Has it yet been ported to this OS?
242 DEBUG(2,("uw_pathname: unable to determine pathname\n"));
244 pstrcpy(fname, uw_default);
247 #ifndef HAVE_PUTUTLINE
249 /****************************************************************************
250 Update utmp file directly. No subroutine interface: probably a BSD system.
251 ****************************************************************************/
253 static void pututline_my(pstring uname, struct utmp *u, BOOL claim)
255 DEBUG(1,("pututline_my: not yet implemented\n"));
256 /* BSD implementor: may want to consider (or not) adjusting "lastlog" */
258 #endif /* HAVE_PUTUTLINE */
260 #ifndef HAVE_UPDWTMP
262 /****************************************************************************
263 Update wtmp file directly. No subroutine interface: probably a BSD system.
264 Credit: Michail Vidiassov <master@iaas.msu.ru>
265 ****************************************************************************/
267 static void updwtmp_my(pstring wname, struct utmp *u, BOOL claim)
269 int fd;
270 struct stat buf;
272 if (! claim) {
274 * BSD-like systems:
275 * may use empty ut_name to distinguish a logout record.
277 * May need "if defined(SUNOS4)" etc. around some of these,
278 * but try to avoid if possible.
280 * SunOS 4:
281 * man page indicates ut_name and ut_host both NULL
282 * FreeBSD 4.0:
283 * man page appears not to specify (hints non-NULL)
284 * A correspondent suggest at least ut_name should be NULL
286 memset((char *)&u->ut_name, '\0', sizeof(u->ut_name));
287 memset((char *)&u->ut_host, '\0', sizeof(u->ut_host));
289 /* Stolen from logwtmp function in libutil.
290 * May be more locking/blocking is needed?
292 if ((fd = open(wname, O_WRONLY|O_APPEND, 0)) < 0)
293 return;
294 if (fstat(fd, &buf) == 0) {
295 if (write(fd, (char *)u, sizeof(struct utmp)) != sizeof(struct utmp))
296 (void) ftruncate(fd, buf.st_size);
298 (void) close(fd);
300 #endif /* HAVE_UPDWTMP */
302 /****************************************************************************
303 Update via utmp/wtmp (not utmpx/wtmpx).
304 ****************************************************************************/
306 static void utmp_nox_update(struct utmp *u, BOOL claim)
308 pstring uname, wname;
309 #if defined(PUTUTLINE_RETURNS_UTMP)
310 struct utmp *urc;
311 #endif /* PUTUTLINE_RETURNS_UTMP */
313 uw_pathname(uname, "utmp", ut_pathname);
314 DEBUG(2,("utmp_nox_update: uname:%s\n", uname));
316 #ifdef HAVE_PUTUTLINE
317 if (strlen(uname) != 0) {
318 utmpname(uname);
321 # if defined(PUTUTLINE_RETURNS_UTMP)
322 setutent();
323 urc = pututline(u);
324 endutent();
325 if (urc == NULL) {
326 DEBUG(2,("utmp_nox_update: pututline() failed\n"));
327 return;
329 # else /* PUTUTLINE_RETURNS_UTMP */
330 setutent();
331 pututline(u);
332 endutent();
333 # endif /* PUTUTLINE_RETURNS_UTMP */
335 #else /* HAVE_PUTUTLINE */
336 if (strlen(uname) != 0) {
337 pututline_my(uname, u, claim);
339 #endif /* HAVE_PUTUTLINE */
341 uw_pathname(wname, "wtmp", wt_pathname);
342 DEBUG(2,("utmp_nox_update: wname:%s\n", wname));
343 if (strlen(wname) != 0) {
344 #ifdef HAVE_UPDWTMP
345 updwtmp(wname, u);
347 * updwtmp() and the newer updwtmpx() may be unsymmetrical.
348 * At least one OS, Solaris 2.x declares the former in the
349 * "utmpx" (latter) file and context.
350 * In the Solaris case this is irrelevant: it has both and
351 * we always prefer the "x" case, so doesn't come here.
352 * But are there other systems, with no "x", which lack
353 * updwtmp() perhaps?
355 #else
356 updwtmp_my(wname, u, claim);
357 #endif /* HAVE_UPDWTMP */
361 /****************************************************************************
362 Copy a string in the utmp structure.
363 ****************************************************************************/
365 static void utmp_strcpy(char *dest, const char *src, size_t n)
367 size_t len = 0;
369 memset(dest, '\0', n);
370 if (src)
371 len = strlen(src);
372 if (len >= n) {
373 memcpy(dest, src, n);
374 } else {
375 if (len)
376 memcpy(dest, src, len);
380 /****************************************************************************
381 Update via utmpx/wtmpx (preferred) or via utmp/wtmp.
382 ****************************************************************************/
384 static void sys_utmp_update(struct utmp *u, const char *hostname, BOOL claim)
386 #if !defined(HAVE_UTMPX_H)
387 /* No utmpx stuff. Drop to non-x stuff */
388 utmp_nox_update(u, claim);
389 #elif !defined(HAVE_PUTUTXLINE)
390 /* Odd. Have utmpx.h but no "pututxline()". Drop to non-x stuff */
391 DEBUG(1,("utmp_update: have utmpx.h but no pututxline() function\n"));
392 utmp_nox_update(u, claim);
393 #elif !defined(HAVE_GETUTMPX)
394 /* Odd. Have utmpx.h but no "getutmpx()". Drop to non-x stuff */
395 DEBUG(1,("utmp_update: have utmpx.h but no getutmpx() function\n"));
396 utmp_nox_update(u, claim);
397 #else
398 pstring uname, wname;
399 struct utmpx ux, *uxrc;
401 getutmpx(u, &ux);
403 #if defined(HAVE_UX_UT_SYSLEN)
404 if (hostname)
405 ux.ut_syslen = strlen(hostname) + 1; /* include end NULL */
406 else
407 ux.ut_syslen = 0;
408 #endif
409 utmp_strcpy(ux.ut_host, hostname, sizeof(ux.ut_host));
411 uw_pathname(uname, "utmpx", ux_pathname);
412 uw_pathname(wname, "wtmpx", wx_pathname);
413 DEBUG(2,("utmp_update: uname:%s wname:%s\n", uname, wname));
415 * Check for either uname or wname being empty.
416 * Some systems, such as Redhat 6, have a "utmpx.h" which doesn't
417 * define default filenames.
418 * Also, our local installation has not provided an override.
419 * Drop to non-x method. (E.g. RH6 has good defaults in "utmp.h".)
421 if ((strlen(uname) == 0) || (strlen(wname) == 0)) {
422 utmp_nox_update(u, claim);
423 } else {
424 utmpxname(uname);
425 setutxent();
426 uxrc = pututxline(&ux);
427 endutxent();
428 if (uxrc == NULL) {
429 DEBUG(2,("utmp_update: pututxline() failed\n"));
430 return;
432 updwtmpx(wname, &ux);
434 #endif /* HAVE_UTMPX_H */
437 #if defined(HAVE_UT_UT_ID)
438 /****************************************************************************
439 Encode the unique connection number into "ut_id".
440 ****************************************************************************/
442 static int ut_id_encode(int i, char *fourbyte)
444 int nbase;
445 const char *ut_id_encstr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
447 fourbyte[0] = 'S';
448 fourbyte[1] = 'M';
451 * Encode remaining 2 bytes from 'i'.
452 * 'ut_id_encstr' is the character set on which modulo arithmetic is done.
453 * Example: digits would produce the base-10 numbers from '001'.
455 nbase = strlen(ut_id_encstr);
457 fourbyte[3] = ut_id_encstr[i % nbase];
458 i /= nbase;
459 fourbyte[2] = ut_id_encstr[i % nbase];
460 i /= nbase;
462 return(i); /* 0: good; else overflow */
464 #endif /* defined(HAVE_UT_UT_ID) */
468 fill a system utmp structure given all the info we can gather
470 static BOOL sys_utmp_fill(struct utmp *u,
471 const char *username, const char *hostname,
472 struct in_addr *ipaddr,
473 const char *id_str, int id_num)
475 struct timeval timeval;
478 * ut_name, ut_user:
479 * Several (all?) systems seems to define one as the other.
480 * It is easier and clearer simply to let the following take its course,
481 * rather than to try to detect and optimise.
483 #if defined(HAVE_UT_UT_USER)
484 utmp_strcpy(u->ut_user, username, sizeof(u->ut_user));
485 #elif defined(HAVE_UT_UT_NAME)
486 utmp_strcpy(u->ut_name, username, sizeof(u->ut_name));
487 #endif
490 * ut_line:
491 * If size limit proves troublesome, then perhaps use "ut_id_encode()".
493 if (strlen(id_str) > sizeof(u->ut_line)) {
494 DEBUG(1,("id_str [%s] is too long for %d char utmp field\n",
495 id_str, sizeof(u->ut_line)));
496 return False;
498 utmp_strcpy(u->ut_line, id_str, sizeof(u->ut_line));
500 #if defined(HAVE_UT_UT_PID)
501 u->ut_pid = sys_getpid();
502 #endif
505 * ut_time, ut_tv:
506 * Some have one, some the other. Many have both, but defined (aliased).
507 * It is easier and clearer simply to let the following take its course.
508 * But note that we do the more precise ut_tv as the final assignment.
510 #if defined(HAVE_UT_UT_TIME)
511 gettimeofday(&timeval, NULL);
512 u->ut_time = timeval.tv_sec;
513 #elif defined(HAVE_UT_UT_TV)
514 gettimeofday(&timeval, NULL);
515 u->ut_tv = timeval;
516 #else
517 #error "with-utmp must have UT_TIME or UT_TV"
518 #endif
520 #if defined(HAVE_UT_UT_HOST)
521 utmp_strcpy(u->ut_host, hostname, sizeof(u->ut_host));
522 #endif
523 #if defined(HAVE_UT_UT_ADDR)
524 if (ipaddr)
525 u->ut_addr = ipaddr->s_addr;
527 * "(unsigned long) ut_addr" apparently exists on at least HP-UX 10.20.
528 * Volunteer to implement, please ...
530 #endif
532 #if defined(HAVE_UT_UT_ID)
533 if (ut_id_encode(id_num, u->ut_id) != 0) {
534 DEBUG(1,("utmp_fill: cannot encode id %d\n", id_num));
535 return False;
537 #endif
539 return True;
542 /****************************************************************************
543 Close a connection.
544 ****************************************************************************/
546 void sys_utmp_yield(const char *username, const char *hostname,
547 struct in_addr *ipaddr,
548 const char *id_str, int id_num)
550 struct utmp u;
552 ZERO_STRUCT(u);
554 #if defined(HAVE_UT_UT_EXIT)
555 u.ut_exit.e_termination = 0;
556 u.ut_exit.e_exit = 0;
557 #endif
559 #if defined(HAVE_UT_UT_TYPE)
560 u.ut_type = DEAD_PROCESS;
561 #endif
563 if (!sys_utmp_fill(&u, username, hostname, ipaddr, id_str, id_num)) return;
565 sys_utmp_update(&u, NULL, False);
568 /****************************************************************************
569 Claim a entry in whatever utmp system the OS uses.
570 ****************************************************************************/
572 void sys_utmp_claim(const char *username, const char *hostname,
573 struct in_addr *ipaddr,
574 const char *id_str, int id_num)
576 struct utmp u;
578 ZERO_STRUCT(u);
580 #if defined(HAVE_UT_UT_TYPE)
581 u.ut_type = USER_PROCESS;
582 #endif
584 if (!sys_utmp_fill(&u, username, hostname, ipaddr, id_str, id_num)) return;
586 sys_utmp_update(&u, hostname, True);
589 #endif /* WITH_UTMP */