Deleted Added
full compact
loginrec.c (149753) loginrec.c (157019)
1/*
2 * Copyright (c) 2000 Andre Lucas. All rights reserved.
3 * Portions copyright (c) 1998 Todd C. Miller
4 * Portions copyright (c) 1996 Jason Downs
5 * Portions copyright (c) 1996 Theo de Raadt
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * The btmp logging code is derived from login.c from util-linux and is under
30 * the the following license:
31 *
32 * Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms are permitted
36 * provided that the above copyright notice and this paragraph are
37 * duplicated in all such forms and that any documentation,
38 * advertising materials, and other materials related to such
39 * distribution and use acknowledge that the software was developed
40 * by the University of California, Berkeley. The name of the
41 * University may not be used to endorse or promote products derived
42 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
45 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 */
47
48
49/**
50 ** loginrec.c: platform-independent login recording and lastlog retrieval
51 **/
52
53/*
54 * The new login code explained
55 * ============================
56 *
57 * This code attempts to provide a common interface to login recording
58 * (utmp and friends) and last login time retrieval.
59 *
60 * Its primary means of achieving this is to use 'struct logininfo', a
61 * union of all the useful fields in the various different types of
62 * system login record structures one finds on UNIX variants.
63 *
64 * We depend on autoconf to define which recording methods are to be
65 * used, and which fields are contained in the relevant data structures
66 * on the local system. Many C preprocessor symbols affect which code
67 * gets compiled here.
68 *
69 * The code is designed to make it easy to modify a particular
70 * recording method, without affecting other methods nor requiring so
71 * many nested conditional compilation blocks as were commonplace in
72 * the old code.
73 *
74 * For login recording, we try to use the local system's libraries as
75 * these are clearly most likely to work correctly. For utmp systems
76 * this usually means login() and logout() or setutent() etc., probably
77 * in libutil, along with logwtmp() etc. On these systems, we fall back
78 * to writing the files directly if we have to, though this method
79 * requires very thorough testing so we do not corrupt local auditing
80 * information. These files and their access methods are very system
81 * specific indeed.
82 *
83 * For utmpx systems, the corresponding library functions are
84 * setutxent() etc. To the author's knowledge, all utmpx systems have
85 * these library functions and so no direct write is attempted. If such
86 * a system exists and needs support, direct analogues of the [uw]tmp
87 * code should suffice.
88 *
89 * Retrieving the time of last login ('lastlog') is in some ways even
90 * more problemmatic than login recording. Some systems provide a
91 * simple table of all users which we seek based on uid and retrieve a
92 * relatively standard structure. Others record the same information in
93 * a directory with a separate file, and others don't record the
94 * information separately at all. For systems in the latter category,
95 * we look backwards in the wtmp or wtmpx file for the last login entry
96 * for our user. Naturally this is slower and on busy systems could
97 * incur a significant performance penalty.
98 *
99 * Calling the new code
100 * --------------------
101 *
102 * In OpenSSH all login recording and retrieval is performed in
103 * login.c. Here you'll find working examples. Also, in the logintest.c
104 * program there are more examples.
105 *
106 * Internal handler calling method
107 * -------------------------------
108 *
109 * When a call is made to login_login() or login_logout(), both
110 * routines set a struct logininfo flag defining which action (log in,
111 * or log out) is to be taken. They both then call login_write(), which
112 * calls whichever of the many structure-specific handlers autoconf
113 * selects for the local system.
114 *
115 * The handlers themselves handle system data structure specifics. Both
116 * struct utmp and struct utmpx have utility functions (see
117 * construct_utmp*()) to try to make it simpler to add extra systems
118 * that introduce new features to either structure.
119 *
120 * While it may seem terribly wasteful to replicate so much similar
121 * code for each method, experience has shown that maintaining code to
122 * write both struct utmp and utmpx in one function, whilst maintaining
123 * support for all systems whether they have library support or not, is
124 * a difficult and time-consuming task.
125 *
126 * Lastlog support proceeds similarly. Functions login_get_lastlog()
127 * (and its OpenSSH-tuned friend login_get_lastlog_time()) call
128 * getlast_entry(), which tries one of three methods to find the last
129 * login time. It uses local system lastlog support if it can,
130 * otherwise it tries wtmp or wtmpx before giving up and returning 0,
131 * meaning "tilt".
132 *
133 * Maintenance
134 * -----------
135 *
136 * In many cases it's possible to tweak autoconf to select the correct
137 * methods for a particular platform, either by improving the detection
138 * code (best), or by presetting DISABLE_<method> or CONF_<method>_FILE
139 * symbols for the platform.
140 *
141 * Use logintest to check which symbols are defined before modifying
142 * configure.ac and loginrec.c. (You have to build logintest yourself
143 * with 'make logintest' as it's not built by default.)
144 *
145 * Otherwise, patches to the specific method(s) are very helpful!
146 */
147
148#include "includes.h"
149
150#include "ssh.h"
151#include "xmalloc.h"
152#include "loginrec.h"
153#include "log.h"
154#include "atomicio.h"
155#include "packet.h"
156#include "canohost.h"
157#include "auth.h"
158#include "buffer.h"
159
160#ifdef HAVE_UTIL_H
161# include <util.h>
162#endif
163
164#ifdef HAVE_LIBUTIL_H
165# include <libutil.h>
166#endif
167
1/*
2 * Copyright (c) 2000 Andre Lucas. All rights reserved.
3 * Portions copyright (c) 1998 Todd C. Miller
4 * Portions copyright (c) 1996 Jason Downs
5 * Portions copyright (c) 1996 Theo de Raadt
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * The btmp logging code is derived from login.c from util-linux and is under
30 * the the following license:
31 *
32 * Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms are permitted
36 * provided that the above copyright notice and this paragraph are
37 * duplicated in all such forms and that any documentation,
38 * advertising materials, and other materials related to such
39 * distribution and use acknowledge that the software was developed
40 * by the University of California, Berkeley. The name of the
41 * University may not be used to endorse or promote products derived
42 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
45 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 */
47
48
49/**
50 ** loginrec.c: platform-independent login recording and lastlog retrieval
51 **/
52
53/*
54 * The new login code explained
55 * ============================
56 *
57 * This code attempts to provide a common interface to login recording
58 * (utmp and friends) and last login time retrieval.
59 *
60 * Its primary means of achieving this is to use 'struct logininfo', a
61 * union of all the useful fields in the various different types of
62 * system login record structures one finds on UNIX variants.
63 *
64 * We depend on autoconf to define which recording methods are to be
65 * used, and which fields are contained in the relevant data structures
66 * on the local system. Many C preprocessor symbols affect which code
67 * gets compiled here.
68 *
69 * The code is designed to make it easy to modify a particular
70 * recording method, without affecting other methods nor requiring so
71 * many nested conditional compilation blocks as were commonplace in
72 * the old code.
73 *
74 * For login recording, we try to use the local system's libraries as
75 * these are clearly most likely to work correctly. For utmp systems
76 * this usually means login() and logout() or setutent() etc., probably
77 * in libutil, along with logwtmp() etc. On these systems, we fall back
78 * to writing the files directly if we have to, though this method
79 * requires very thorough testing so we do not corrupt local auditing
80 * information. These files and their access methods are very system
81 * specific indeed.
82 *
83 * For utmpx systems, the corresponding library functions are
84 * setutxent() etc. To the author's knowledge, all utmpx systems have
85 * these library functions and so no direct write is attempted. If such
86 * a system exists and needs support, direct analogues of the [uw]tmp
87 * code should suffice.
88 *
89 * Retrieving the time of last login ('lastlog') is in some ways even
90 * more problemmatic than login recording. Some systems provide a
91 * simple table of all users which we seek based on uid and retrieve a
92 * relatively standard structure. Others record the same information in
93 * a directory with a separate file, and others don't record the
94 * information separately at all. For systems in the latter category,
95 * we look backwards in the wtmp or wtmpx file for the last login entry
96 * for our user. Naturally this is slower and on busy systems could
97 * incur a significant performance penalty.
98 *
99 * Calling the new code
100 * --------------------
101 *
102 * In OpenSSH all login recording and retrieval is performed in
103 * login.c. Here you'll find working examples. Also, in the logintest.c
104 * program there are more examples.
105 *
106 * Internal handler calling method
107 * -------------------------------
108 *
109 * When a call is made to login_login() or login_logout(), both
110 * routines set a struct logininfo flag defining which action (log in,
111 * or log out) is to be taken. They both then call login_write(), which
112 * calls whichever of the many structure-specific handlers autoconf
113 * selects for the local system.
114 *
115 * The handlers themselves handle system data structure specifics. Both
116 * struct utmp and struct utmpx have utility functions (see
117 * construct_utmp*()) to try to make it simpler to add extra systems
118 * that introduce new features to either structure.
119 *
120 * While it may seem terribly wasteful to replicate so much similar
121 * code for each method, experience has shown that maintaining code to
122 * write both struct utmp and utmpx in one function, whilst maintaining
123 * support for all systems whether they have library support or not, is
124 * a difficult and time-consuming task.
125 *
126 * Lastlog support proceeds similarly. Functions login_get_lastlog()
127 * (and its OpenSSH-tuned friend login_get_lastlog_time()) call
128 * getlast_entry(), which tries one of three methods to find the last
129 * login time. It uses local system lastlog support if it can,
130 * otherwise it tries wtmp or wtmpx before giving up and returning 0,
131 * meaning "tilt".
132 *
133 * Maintenance
134 * -----------
135 *
136 * In many cases it's possible to tweak autoconf to select the correct
137 * methods for a particular platform, either by improving the detection
138 * code (best), or by presetting DISABLE_<method> or CONF_<method>_FILE
139 * symbols for the platform.
140 *
141 * Use logintest to check which symbols are defined before modifying
142 * configure.ac and loginrec.c. (You have to build logintest yourself
143 * with 'make logintest' as it's not built by default.)
144 *
145 * Otherwise, patches to the specific method(s) are very helpful!
146 */
147
148#include "includes.h"
149
150#include "ssh.h"
151#include "xmalloc.h"
152#include "loginrec.h"
153#include "log.h"
154#include "atomicio.h"
155#include "packet.h"
156#include "canohost.h"
157#include "auth.h"
158#include "buffer.h"
159
160#ifdef HAVE_UTIL_H
161# include <util.h>
162#endif
163
164#ifdef HAVE_LIBUTIL_H
165# include <libutil.h>
166#endif
167
168RCSID("$Id: loginrec.c,v 1.70 2005/07/17 07:26:44 djm Exp $");
169RCSID("$FreeBSD: head/crypto/openssh/loginrec.c 149753 2005-09-03 07:04:25Z des $");
168RCSID("$Id: loginrec.c,v 1.71 2005/11/22 08:55:13 dtucker Exp $");
169RCSID("$FreeBSD: head/crypto/openssh/loginrec.c 157019 2006-03-22 20:41:37Z des $");
170
171/**
172 ** prototypes for helper functions in this file
173 **/
174
175#if HAVE_UTMP_H
176void set_utmp_time(struct logininfo *li, struct utmp *ut);
177void construct_utmp(struct logininfo *li, struct utmp *ut);
178#endif
179
180#ifdef HAVE_UTMPX_H
181void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
182void construct_utmpx(struct logininfo *li, struct utmpx *ut);
183#endif
184
185int utmp_write_entry(struct logininfo *li);
186int utmpx_write_entry(struct logininfo *li);
187int wtmp_write_entry(struct logininfo *li);
188int wtmpx_write_entry(struct logininfo *li);
189int lastlog_write_entry(struct logininfo *li);
190int syslogin_write_entry(struct logininfo *li);
191
192int getlast_entry(struct logininfo *li);
193int lastlog_get_entry(struct logininfo *li);
194int wtmp_get_entry(struct logininfo *li);
195int wtmpx_get_entry(struct logininfo *li);
196
197extern Buffer loginmsg;
198
199/* pick the shortest string */
200#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
201
202/**
203 ** platform-independent login functions
204 **/
205
206/*
207 * login_login(struct logininfo *) - Record a login
208 *
209 * Call with a pointer to a struct logininfo initialised with
210 * login_init_entry() or login_alloc_entry()
211 *
212 * Returns:
213 * >0 if successful
214 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
215 */
216int
217login_login(struct logininfo *li)
218{
219 li->type = LTYPE_LOGIN;
220 return (login_write(li));
221}
222
223
224/*
225 * login_logout(struct logininfo *) - Record a logout
226 *
227 * Call as with login_login()
228 *
229 * Returns:
230 * >0 if successful
231 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
232 */
233int
234login_logout(struct logininfo *li)
235{
236 li->type = LTYPE_LOGOUT;
237 return (login_write(li));
238}
239
240/*
241 * login_get_lastlog_time(int) - Retrieve the last login time
242 *
243 * Retrieve the last login time for the given uid. Will try to use the
244 * system lastlog facilities if they are available, but will fall back
245 * to looking in wtmp/wtmpx if necessary
246 *
247 * Returns:
248 * 0 on failure, or if user has never logged in
249 * Time in seconds from the epoch if successful
250 *
251 * Useful preprocessor symbols:
252 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
253 * info
254 * USE_LASTLOG: If set, indicates the presence of system lastlog
255 * facilities. If this and DISABLE_LASTLOG are not set,
256 * try to retrieve lastlog information from wtmp/wtmpx.
257 */
258unsigned int
259login_get_lastlog_time(const int uid)
260{
261 struct logininfo li;
262
263 if (login_get_lastlog(&li, uid))
264 return (li.tv_sec);
265 else
266 return (0);
267}
268
269/*
270 * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
271 *
272 * Retrieve a logininfo structure populated (only partially) with
273 * information from the system lastlog data, or from wtmp/wtmpx if no
274 * system lastlog information exists.
275 *
276 * Note this routine must be given a pre-allocated logininfo.
277 *
278 * Returns:
279 * >0: A pointer to your struct logininfo if successful
280 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
281 */
282struct logininfo *
283login_get_lastlog(struct logininfo *li, const int uid)
284{
285 struct passwd *pw;
286
287 memset(li, '\0', sizeof(*li));
288 li->uid = uid;
289
290 /*
291 * If we don't have a 'real' lastlog, we need the username to
292 * reliably search wtmp(x) for the last login (see
293 * wtmp_get_entry().)
294 */
295 pw = getpwuid(uid);
296 if (pw == NULL)
297 fatal("%s: Cannot find account for uid %i", __func__, uid);
298
299 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
300 * username (XXX - so check for trunc!) */
301 strlcpy(li->username, pw->pw_name, sizeof(li->username));
302
303 if (getlast_entry(li))
304 return (li);
305 else
306 return (NULL);
307}
308
309
310/*
311 * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
312 * a logininfo structure
313 *
314 * This function creates a new struct logininfo, a data structure
315 * meant to carry the information required to portably record login info.
316 *
317 * Returns a pointer to a newly created struct logininfo. If memory
318 * allocation fails, the program halts.
319 */
320struct
321logininfo *login_alloc_entry(int pid, const char *username,
322 const char *hostname, const char *line)
323{
324 struct logininfo *newli;
325
326 newli = xmalloc(sizeof(*newli));
327 login_init_entry(newli, pid, username, hostname, line);
328 return (newli);
329}
330
331
332/* login_free_entry(struct logininfo *) - free struct memory */
333void
334login_free_entry(struct logininfo *li)
335{
336 xfree(li);
337}
338
339
340/* login_init_entry(struct logininfo *, int, char*, char*, char*)
341 * - initialise a struct logininfo
342 *
343 * Populates a new struct logininfo, a data structure meant to carry
344 * the information required to portably record login info.
345 *
346 * Returns: 1
347 */
348int
349login_init_entry(struct logininfo *li, int pid, const char *username,
350 const char *hostname, const char *line)
351{
352 struct passwd *pw;
353
354 memset(li, 0, sizeof(*li));
355
356 li->pid = pid;
357
358 /* set the line information */
359 if (line)
360 line_fullname(li->line, line, sizeof(li->line));
361
362 if (username) {
363 strlcpy(li->username, username, sizeof(li->username));
364 pw = getpwnam(li->username);
365 if (pw == NULL) {
366 fatal("%s: Cannot find user \"%s\"", __func__,
367 li->username);
368 }
369 li->uid = pw->pw_uid;
370 }
371
372 if (hostname)
373 strlcpy(li->hostname, hostname, sizeof(li->hostname));
374
375 return (1);
376}
377
378/*
379 * login_set_current_time(struct logininfo *) - set the current time
380 *
381 * Set the current time in a logininfo structure. This function is
382 * meant to eliminate the need to deal with system dependencies for
383 * time handling.
384 */
385void
386login_set_current_time(struct logininfo *li)
387{
388 struct timeval tv;
389
390 gettimeofday(&tv, NULL);
391
392 li->tv_sec = tv.tv_sec;
393 li->tv_usec = tv.tv_usec;
394}
395
396/* copy a sockaddr_* into our logininfo */
397void
398login_set_addr(struct logininfo *li, const struct sockaddr *sa,
399 const unsigned int sa_size)
400{
401 unsigned int bufsize = sa_size;
402
403 /* make sure we don't overrun our union */
404 if (sizeof(li->hostaddr) < sa_size)
405 bufsize = sizeof(li->hostaddr);
406
407 memcpy(&li->hostaddr.sa, sa, bufsize);
408}
409
410
411/**
412 ** login_write: Call low-level recording functions based on autoconf
413 ** results
414 **/
415int
416login_write(struct logininfo *li)
417{
418#ifndef HAVE_CYGWIN
419 if (geteuid() != 0) {
420 logit("Attempt to write login records by non-root user (aborting)");
421 return (1);
422 }
423#endif
424
425 /* set the timestamp */
426 login_set_current_time(li);
427#ifdef USE_LOGIN
428 syslogin_write_entry(li);
429#endif
430#ifdef USE_LASTLOG
431 if (li->type == LTYPE_LOGIN)
432 lastlog_write_entry(li);
433#endif
434#ifdef USE_UTMP
435 utmp_write_entry(li);
436#endif
437#ifdef USE_WTMP
438 wtmp_write_entry(li);
439#endif
440#ifdef USE_UTMPX
441 utmpx_write_entry(li);
442#endif
443#ifdef USE_WTMPX
444 wtmpx_write_entry(li);
445#endif
446#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
447 if (li->type == LTYPE_LOGIN &&
448 !sys_auth_record_login(li->username,li->hostname,li->line,
449 &loginmsg))
450 logit("Writing login record failed for %s", li->username);
451#endif
452#ifdef SSH_AUDIT_EVENTS
453 if (li->type == LTYPE_LOGIN)
454 audit_session_open(li->line);
455 else if (li->type == LTYPE_LOGOUT)
456 audit_session_close(li->line);
457#endif
458 return (0);
459}
460
461#ifdef LOGIN_NEEDS_UTMPX
462int
463login_utmp_only(struct logininfo *li)
464{
465 li->type = LTYPE_LOGIN;
466 login_set_current_time(li);
467# ifdef USE_UTMP
468 utmp_write_entry(li);
469# endif
470# ifdef USE_WTMP
471 wtmp_write_entry(li);
472# endif
473# ifdef USE_UTMPX
474 utmpx_write_entry(li);
475# endif
476# ifdef USE_WTMPX
477 wtmpx_write_entry(li);
478# endif
479 return (0);
480}
481#endif
482
483/**
484 ** getlast_entry: Call low-level functions to retrieve the last login
485 ** time.
486 **/
487
488/* take the uid in li and return the last login time */
489int
490getlast_entry(struct logininfo *li)
491{
492#ifdef USE_LASTLOG
493 return(lastlog_get_entry(li));
494#else /* !USE_LASTLOG */
495
496#if defined(DISABLE_LASTLOG)
497 /* On some systems we shouldn't even try to obtain last login
498 * time, e.g. AIX */
499 return (0);
500# elif defined(USE_WTMP) && \
501 (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
502 /* retrieve last login time from utmp */
503 return (wtmp_get_entry(li));
504# elif defined(USE_WTMPX) && \
505 (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
506 /* If wtmp isn't available, try wtmpx */
507 return (wtmpx_get_entry(li));
508# else
509 /* Give up: No means of retrieving last login time */
510 return (0);
511# endif /* DISABLE_LASTLOG */
512#endif /* USE_LASTLOG */
513}
514
515
516
517/*
518 * 'line' string utility functions
519 *
520 * These functions process the 'line' string into one of three forms:
521 *
522 * 1. The full filename (including '/dev')
523 * 2. The stripped name (excluding '/dev')
524 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
525 * /dev/pts/1 -> ts/1 )
526 *
527 * Form 3 is used on some systems to identify a .tmp.? entry when
528 * attempting to remove it. Typically both addition and removal is
529 * performed by one application - say, sshd - so as long as the choice
530 * uniquely identifies a terminal it's ok.
531 */
532
533
534/*
535 * line_fullname(): add the leading '/dev/' if it doesn't exist make
536 * sure dst has enough space, if not just copy src (ugh)
537 */
538char *
539line_fullname(char *dst, const char *src, u_int dstsize)
540{
541 memset(dst, '\0', dstsize);
542 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
543 strlcpy(dst, src, dstsize);
544 else {
545 strlcpy(dst, "/dev/", dstsize);
546 strlcat(dst, src, dstsize);
547 }
548 return (dst);
549}
550
551/* line_stripname(): strip the leading '/dev' if it exists, return dst */
552char *
553line_stripname(char *dst, const char *src, int dstsize)
554{
555 memset(dst, '\0', dstsize);
556 if (strncmp(src, "/dev/", 5) == 0)
557 strlcpy(dst, src + 5, dstsize);
558 else
559 strlcpy(dst, src, dstsize);
560 return (dst);
561}
562
563/*
564 * line_abbrevname(): Return the abbreviated (usually four-character)
565 * form of the line (Just use the last <dstsize> characters of the
566 * full name.)
567 *
568 * NOTE: use strncpy because we do NOT necessarily want zero
569 * termination
570 */
571char *
572line_abbrevname(char *dst, const char *src, int dstsize)
573{
574 size_t len;
575
576 memset(dst, '\0', dstsize);
577
578 /* Always skip prefix if present */
579 if (strncmp(src, "/dev/", 5) == 0)
580 src += 5;
581
582#ifdef WITH_ABBREV_NO_TTY
583 if (strncmp(src, "tty", 3) == 0)
584 src += 3;
585#endif
586
587 len = strlen(src);
588
589 if (len > 0) {
590 if (((int)len - dstsize) > 0)
591 src += ((int)len - dstsize);
592
593 /* note: _don't_ change this to strlcpy */
594 strncpy(dst, src, (size_t)dstsize);
595 }
596
597 return (dst);
598}
599
600/**
601 ** utmp utility functions
602 **
603 ** These functions manipulate struct utmp, taking system differences
604 ** into account.
605 **/
606
607#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
608
609/* build the utmp structure */
610void
611set_utmp_time(struct logininfo *li, struct utmp *ut)
612{
613# if defined(HAVE_TV_IN_UTMP)
614 ut->ut_tv.tv_sec = li->tv_sec;
615 ut->ut_tv.tv_usec = li->tv_usec;
616# elif defined(HAVE_TIME_IN_UTMP)
617 ut->ut_time = li->tv_sec;
618# endif
619}
620
621void
622construct_utmp(struct logininfo *li,
623 struct utmp *ut)
624{
625# ifdef HAVE_ADDR_V6_IN_UTMP
626 struct sockaddr_in6 *sa6;
627# endif
628
629 memset(ut, '\0', sizeof(*ut));
630
631 /* First fill out fields used for both logins and logouts */
632
633# ifdef HAVE_ID_IN_UTMP
634 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
635# endif
636
637# ifdef HAVE_TYPE_IN_UTMP
638 /* This is done here to keep utmp constants out of struct logininfo */
639 switch (li->type) {
640 case LTYPE_LOGIN:
641 ut->ut_type = USER_PROCESS;
642#ifdef _UNICOS
643 cray_set_tmpdir(ut);
644#endif
645 break;
646 case LTYPE_LOGOUT:
647 ut->ut_type = DEAD_PROCESS;
648#ifdef _UNICOS
649 cray_retain_utmp(ut, li->pid);
650#endif
651 break;
652 }
653# endif
654 set_utmp_time(li, ut);
655
656 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
657
658# ifdef HAVE_PID_IN_UTMP
659 ut->ut_pid = li->pid;
660# endif
661
662 /* If we're logging out, leave all other fields blank */
663 if (li->type == LTYPE_LOGOUT)
664 return;
665
666 /*
667 * These fields are only used when logging in, and are blank
668 * for logouts.
669 */
670
671 /* Use strncpy because we don't necessarily want null termination */
672 strncpy(ut->ut_name, li->username,
673 MIN_SIZEOF(ut->ut_name, li->username));
674# ifdef HAVE_HOST_IN_UTMP
675 realhostname_sa(ut->ut_host, sizeof ut->ut_host,
676 &li->hostaddr.sa, li->hostaddr.sa.sa_len);
677# endif
678# ifdef HAVE_ADDR_IN_UTMP
679 /* this is just a 32-bit IP address */
680 if (li->hostaddr.sa.sa_family == AF_INET)
681 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
682# endif
683# ifdef HAVE_ADDR_V6_IN_UTMP
684 /* this is just a 128-bit IPv6 address */
685 if (li->hostaddr.sa.sa_family == AF_INET6) {
686 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
687 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
688 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
689 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
690 ut->ut_addr_v6[1] = 0;
691 ut->ut_addr_v6[2] = 0;
692 ut->ut_addr_v6[3] = 0;
693 }
694 }
695# endif
696}
697#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
698
699/**
700 ** utmpx utility functions
701 **
702 ** These functions manipulate struct utmpx, accounting for system
703 ** variations.
704 **/
705
706#if defined(USE_UTMPX) || defined (USE_WTMPX)
707/* build the utmpx structure */
708void
709set_utmpx_time(struct logininfo *li, struct utmpx *utx)
710{
711# if defined(HAVE_TV_IN_UTMPX)
712 utx->ut_tv.tv_sec = li->tv_sec;
713 utx->ut_tv.tv_usec = li->tv_usec;
714# elif defined(HAVE_TIME_IN_UTMPX)
715 utx->ut_time = li->tv_sec;
716# endif
717}
718
719void
720construct_utmpx(struct logininfo *li, struct utmpx *utx)
721{
722# ifdef HAVE_ADDR_V6_IN_UTMP
723 struct sockaddr_in6 *sa6;
724# endif
725 memset(utx, '\0', sizeof(*utx));
726
727# ifdef HAVE_ID_IN_UTMPX
728 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
729# endif
730
731 /* this is done here to keep utmp constants out of loginrec.h */
732 switch (li->type) {
733 case LTYPE_LOGIN:
734 utx->ut_type = USER_PROCESS;
735 break;
736 case LTYPE_LOGOUT:
737 utx->ut_type = DEAD_PROCESS;
738 break;
739 }
740 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
741 set_utmpx_time(li, utx);
742 utx->ut_pid = li->pid;
743
744 /* strncpy(): Don't necessarily want null termination */
745 strncpy(utx->ut_name, li->username,
746 MIN_SIZEOF(utx->ut_name, li->username));
747
748 if (li->type == LTYPE_LOGOUT)
749 return;
750
751 /*
752 * These fields are only used when logging in, and are blank
753 * for logouts.
754 */
755
756# ifdef HAVE_HOST_IN_UTMPX
757 strncpy(utx->ut_host, li->hostname,
758 MIN_SIZEOF(utx->ut_host, li->hostname));
759# endif
760# ifdef HAVE_ADDR_IN_UTMPX
761 /* this is just a 32-bit IP address */
762 if (li->hostaddr.sa.sa_family == AF_INET)
763 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
764# endif
765# ifdef HAVE_ADDR_V6_IN_UTMP
766 /* this is just a 128-bit IPv6 address */
767 if (li->hostaddr.sa.sa_family == AF_INET6) {
768 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
769 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
770 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
771 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
772 ut->ut_addr_v6[1] = 0;
773 ut->ut_addr_v6[2] = 0;
774 ut->ut_addr_v6[3] = 0;
775 }
776 }
777# endif
778# ifdef HAVE_SYSLEN_IN_UTMPX
779 /* ut_syslen is the length of the utx_host string */
780 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
781# endif
782}
783#endif /* USE_UTMPX || USE_WTMPX */
784
785/**
786 ** Low-level utmp functions
787 **/
788
789/* FIXME: (ATL) utmp_write_direct needs testing */
790#ifdef USE_UTMP
791
792/* if we can, use pututline() etc. */
793# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
794 defined(HAVE_PUTUTLINE)
795# define UTMP_USE_LIBRARY
796# endif
797
798
799/* write a utmp entry with the system's help (pututline() and pals) */
800# ifdef UTMP_USE_LIBRARY
801static int
802utmp_write_library(struct logininfo *li, struct utmp *ut)
803{
804 setutent();
805 pututline(ut);
806# ifdef HAVE_ENDUTENT
807 endutent();
808# endif
809 return (1);
810}
811# else /* UTMP_USE_LIBRARY */
812
813/*
814 * Write a utmp entry direct to the file
815 * This is a slightly modification of code in OpenBSD's login.c
816 */
817static int
818utmp_write_direct(struct logininfo *li, struct utmp *ut)
819{
820 struct utmp old_ut;
821 register int fd;
822 int tty;
823
824 /* FIXME: (ATL) ttyslot() needs local implementation */
825
826#if defined(HAVE_GETTTYENT)
827 struct ttyent *ty;
828
829 tty=0;
830 setttyent();
831 while (NULL != (ty = getttyent())) {
832 tty++;
833 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
834 break;
835 }
836 endttyent();
837
838 if (NULL == ty) {
839 logit("%s: tty not found", __func__);
840 return (0);
841 }
842#else /* FIXME */
843
844 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
845
846#endif /* HAVE_GETTTYENT */
847
848 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
849 off_t pos, ret;
850
851 pos = (off_t)tty * sizeof(struct utmp);
852 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
853 logit("%s: lseek: %s", __func__, strerror(errno));
854 return (0);
855 }
856 if (ret != pos) {
857 logit("%s: Couldn't seek to tty %d slot in %s",
858 __func__, tty, UTMP_FILE);
859 return (0);
860 }
861 /*
862 * Prevent luser from zero'ing out ut_host.
863 * If the new ut_line is empty but the old one is not
864 * and ut_line and ut_name match, preserve the old ut_line.
865 */
866 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
867 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
868 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
869 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
870 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
871
872 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
873 logit("%s: lseek: %s", __func__, strerror(errno));
874 return (0);
875 }
876 if (ret != pos) {
877 logit("%s: Couldn't seek to tty %d slot in %s",
878 __func__, tty, UTMP_FILE);
879 return (0);
880 }
881 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
882 logit("%s: error writing %s: %s", __func__,
883 UTMP_FILE, strerror(errno));
884 }
885
886 close(fd);
887 return (1);
888 } else {
889 return (0);
890 }
891}
892# endif /* UTMP_USE_LIBRARY */
893
894static int
895utmp_perform_login(struct logininfo *li)
896{
897 struct utmp ut;
898
899 construct_utmp(li, &ut);
900# ifdef UTMP_USE_LIBRARY
901 if (!utmp_write_library(li, &ut)) {
902 logit("%s: utmp_write_library() failed", __func__);
903 return (0);
904 }
905# else
906 if (!utmp_write_direct(li, &ut)) {
907 logit("%s: utmp_write_direct() failed", __func__);
908 return (0);
909 }
910# endif
911 return (1);
912}
913
914
915static int
916utmp_perform_logout(struct logininfo *li)
917{
918 struct utmp ut;
919
920 construct_utmp(li, &ut);
921# ifdef UTMP_USE_LIBRARY
922 if (!utmp_write_library(li, &ut)) {
923 logit("%s: utmp_write_library() failed", __func__);
924 return (0);
925 }
926# else
927 if (!utmp_write_direct(li, &ut)) {
928 logit("%s: utmp_write_direct() failed", __func__);
929 return (0);
930 }
931# endif
932 return (1);
933}
934
935
936int
937utmp_write_entry(struct logininfo *li)
938{
939 switch(li->type) {
940 case LTYPE_LOGIN:
941 return (utmp_perform_login(li));
942
943 case LTYPE_LOGOUT:
944 return (utmp_perform_logout(li));
945
946 default:
947 logit("%s: invalid type field", __func__);
948 return (0);
949 }
950}
951#endif /* USE_UTMP */
952
953
954/**
955 ** Low-level utmpx functions
956 **/
957
958/* not much point if we don't want utmpx entries */
959#ifdef USE_UTMPX
960
961/* if we have the wherewithall, use pututxline etc. */
962# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
963 defined(HAVE_PUTUTXLINE)
964# define UTMPX_USE_LIBRARY
965# endif
966
967
968/* write a utmpx entry with the system's help (pututxline() and pals) */
969# ifdef UTMPX_USE_LIBRARY
970static int
971utmpx_write_library(struct logininfo *li, struct utmpx *utx)
972{
973 setutxent();
974 pututxline(utx);
975
976# ifdef HAVE_ENDUTXENT
977 endutxent();
978# endif
979 return (1);
980}
981
982# else /* UTMPX_USE_LIBRARY */
983
984/* write a utmp entry direct to the file */
985static int
986utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
987{
988 logit("%s: not implemented!", __func__);
989 return (0);
990}
991# endif /* UTMPX_USE_LIBRARY */
992
993static int
994utmpx_perform_login(struct logininfo *li)
995{
996 struct utmpx utx;
997
998 construct_utmpx(li, &utx);
999# ifdef UTMPX_USE_LIBRARY
1000 if (!utmpx_write_library(li, &utx)) {
1001 logit("%s: utmp_write_library() failed", __func__);
1002 return (0);
1003 }
1004# else
1005 if (!utmpx_write_direct(li, &ut)) {
1006 logit("%s: utmp_write_direct() failed", __func__);
1007 return (0);
1008 }
1009# endif
1010 return (1);
1011}
1012
1013
1014static int
1015utmpx_perform_logout(struct logininfo *li)
1016{
1017 struct utmpx utx;
1018
1019 construct_utmpx(li, &utx);
1020# ifdef HAVE_ID_IN_UTMPX
1021 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
1022# endif
1023# ifdef HAVE_TYPE_IN_UTMPX
1024 utx.ut_type = DEAD_PROCESS;
1025# endif
1026
1027# ifdef UTMPX_USE_LIBRARY
1028 utmpx_write_library(li, &utx);
1029# else
1030 utmpx_write_direct(li, &utx);
1031# endif
1032 return (1);
1033}
1034
1035int
1036utmpx_write_entry(struct logininfo *li)
1037{
1038 switch(li->type) {
1039 case LTYPE_LOGIN:
1040 return (utmpx_perform_login(li));
1041 case LTYPE_LOGOUT:
1042 return (utmpx_perform_logout(li));
1043 default:
1044 logit("%s: invalid type field", __func__);
1045 return (0);
1046 }
1047}
1048#endif /* USE_UTMPX */
1049
1050
1051/**
1052 ** Low-level wtmp functions
1053 **/
1054
1055#ifdef USE_WTMP
1056
1057/*
1058 * Write a wtmp entry direct to the end of the file
1059 * This is a slight modification of code in OpenBSD's logwtmp.c
1060 */
1061static int
1062wtmp_write(struct logininfo *li, struct utmp *ut)
1063{
1064 struct stat buf;
1065 int fd, ret = 1;
1066
1067 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
1068 logit("%s: problem writing %s: %s", __func__,
1069 WTMP_FILE, strerror(errno));
1070 return (0);
1071 }
1072 if (fstat(fd, &buf) == 0)
1073 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
1074 ftruncate(fd, buf.st_size);
1075 logit("%s: problem writing %s: %s", __func__,
1076 WTMP_FILE, strerror(errno));
1077 ret = 0;
1078 }
1079 close(fd);
1080 return (ret);
1081}
1082
1083static int
1084wtmp_perform_login(struct logininfo *li)
1085{
1086 struct utmp ut;
1087
1088 construct_utmp(li, &ut);
1089 return (wtmp_write(li, &ut));
1090}
1091
1092
1093static int
1094wtmp_perform_logout(struct logininfo *li)
1095{
1096 struct utmp ut;
1097
1098 construct_utmp(li, &ut);
1099 return (wtmp_write(li, &ut));
1100}
1101
1102
1103int
1104wtmp_write_entry(struct logininfo *li)
1105{
1106 switch(li->type) {
1107 case LTYPE_LOGIN:
1108 return (wtmp_perform_login(li));
1109 case LTYPE_LOGOUT:
1110 return (wtmp_perform_logout(li));
1111 default:
1112 logit("%s: invalid type field", __func__);
1113 return (0);
1114 }
1115}
1116
1117
1118/*
1119 * Notes on fetching login data from wtmp/wtmpx
1120 *
1121 * Logouts are usually recorded with (amongst other things) a blank
1122 * username on a given tty line. However, some systems (HP-UX is one)
1123 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1124 *
1125 * Since we're only looking for logins here, we know that the username
1126 * must be set correctly. On systems that leave it in, we check for
1127 * ut_type==USER_PROCESS (indicating a login.)
1128 *
1129 * Portability: Some systems may set something other than USER_PROCESS
1130 * to indicate a login process. I don't know of any as I write. Also,
1131 * it's possible that some systems may both leave the username in
1132 * place and not have ut_type.
1133 */
1134
1135/* return true if this wtmp entry indicates a login */
1136static int
1137wtmp_islogin(struct logininfo *li, struct utmp *ut)
1138{
1139 if (strncmp(li->username, ut->ut_name,
1140 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
1141# ifdef HAVE_TYPE_IN_UTMP
1142 if (ut->ut_type & USER_PROCESS)
1143 return (1);
1144# else
1145 return (1);
1146# endif
1147 }
1148 return (0);
1149}
1150
1151int
1152wtmp_get_entry(struct logininfo *li)
1153{
1154 struct stat st;
1155 struct utmp ut;
1156 int fd, found = 0;
1157
1158 /* Clear the time entries in our logininfo */
1159 li->tv_sec = li->tv_usec = 0;
1160
1161 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
1162 logit("%s: problem opening %s: %s", __func__,
1163 WTMP_FILE, strerror(errno));
1164 return (0);
1165 }
1166 if (fstat(fd, &st) != 0) {
1167 logit("%s: couldn't stat %s: %s", __func__,
1168 WTMP_FILE, strerror(errno));
1169 close(fd);
1170 return (0);
1171 }
1172
1173 /* Seek to the start of the last struct utmp */
1174 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
1175 /* Looks like we've got a fresh wtmp file */
1176 close(fd);
1177 return (0);
1178 }
1179
1180 while (!found) {
1181 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
1182 logit("%s: read of %s failed: %s", __func__,
1183 WTMP_FILE, strerror(errno));
1184 close (fd);
1185 return (0);
1186 }
1187 if ( wtmp_islogin(li, &ut) ) {
1188 found = 1;
1189 /*
1190 * We've already checked for a time in struct
1191 * utmp, in login_getlast()
1192 */
1193# ifdef HAVE_TIME_IN_UTMP
1194 li->tv_sec = ut.ut_time;
1195# else
1196# if HAVE_TV_IN_UTMP
1197 li->tv_sec = ut.ut_tv.tv_sec;
1198# endif
1199# endif
1200 line_fullname(li->line, ut.ut_line,
1201 MIN_SIZEOF(li->line, ut.ut_line));
1202# ifdef HAVE_HOST_IN_UTMP
1203 strlcpy(li->hostname, ut.ut_host,
1204 MIN_SIZEOF(li->hostname, ut.ut_host));
1205# endif
1206 continue;
1207 }
1208 /* Seek back 2 x struct utmp */
1209 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
1210 /* We've found the start of the file, so quit */
1211 close(fd);
1212 return (0);
1213 }
1214 }
1215
1216 /* We found an entry. Tidy up and return */
1217 close(fd);
1218 return (1);
1219}
1220# endif /* USE_WTMP */
1221
1222
1223/**
1224 ** Low-level wtmpx functions
1225 **/
1226
1227#ifdef USE_WTMPX
1228/*
1229 * Write a wtmpx entry direct to the end of the file
1230 * This is a slight modification of code in OpenBSD's logwtmp.c
1231 */
1232static int
1233wtmpx_write(struct logininfo *li, struct utmpx *utx)
1234{
1235#ifndef HAVE_UPDWTMPX
1236 struct stat buf;
1237 int fd, ret = 1;
1238
1239 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
1240 logit("%s: problem opening %s: %s", __func__,
1241 WTMPX_FILE, strerror(errno));
1242 return (0);
1243 }
1244
1245 if (fstat(fd, &buf) == 0)
1246 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
1247 ftruncate(fd, buf.st_size);
1248 logit("%s: problem writing %s: %s", __func__,
1249 WTMPX_FILE, strerror(errno));
1250 ret = 0;
1251 }
1252 close(fd);
1253
1254 return (ret);
1255#else
1256 updwtmpx(WTMPX_FILE, utx);
1257 return (1);
1258#endif
1259}
1260
1261
1262static int
1263wtmpx_perform_login(struct logininfo *li)
1264{
1265 struct utmpx utx;
1266
1267 construct_utmpx(li, &utx);
1268 return (wtmpx_write(li, &utx));
1269}
1270
1271
1272static int
1273wtmpx_perform_logout(struct logininfo *li)
1274{
1275 struct utmpx utx;
1276
1277 construct_utmpx(li, &utx);
1278 return (wtmpx_write(li, &utx));
1279}
1280
1281
1282int
1283wtmpx_write_entry(struct logininfo *li)
1284{
1285 switch(li->type) {
1286 case LTYPE_LOGIN:
1287 return (wtmpx_perform_login(li));
1288 case LTYPE_LOGOUT:
1289 return (wtmpx_perform_logout(li));
1290 default:
1291 logit("%s: invalid type field", __func__);
1292 return (0);
1293 }
1294}
1295
1296/* Please see the notes above wtmp_islogin() for information about the
1297 next two functions */
1298
1299/* Return true if this wtmpx entry indicates a login */
1300static int
1301wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1302{
1303 if (strncmp(li->username, utx->ut_name,
1304 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
1305# ifdef HAVE_TYPE_IN_UTMPX
1306 if (utx->ut_type == USER_PROCESS)
1307 return (1);
1308# else
1309 return (1);
1310# endif
1311 }
1312 return (0);
1313}
1314
1315
1316int
1317wtmpx_get_entry(struct logininfo *li)
1318{
1319 struct stat st;
1320 struct utmpx utx;
1321 int fd, found=0;
1322
1323 /* Clear the time entries */
1324 li->tv_sec = li->tv_usec = 0;
1325
1326 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
1327 logit("%s: problem opening %s: %s", __func__,
1328 WTMPX_FILE, strerror(errno));
1329 return (0);
1330 }
1331 if (fstat(fd, &st) != 0) {
1332 logit("%s: couldn't stat %s: %s", __func__,
1333 WTMPX_FILE, strerror(errno));
1334 close(fd);
1335 return (0);
1336 }
1337
1338 /* Seek to the start of the last struct utmpx */
1339 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
1340 /* probably a newly rotated wtmpx file */
1341 close(fd);
1342 return (0);
1343 }
1344
1345 while (!found) {
1346 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
1347 logit("%s: read of %s failed: %s", __func__,
1348 WTMPX_FILE, strerror(errno));
1349 close (fd);
1350 return (0);
1351 }
1352 /*
1353 * Logouts are recorded as a blank username on a particular
1354 * line. So, we just need to find the username in struct utmpx
1355 */
1356 if (wtmpx_islogin(li, &utx)) {
1357 found = 1;
1358# if defined(HAVE_TV_IN_UTMPX)
1359 li->tv_sec = utx.ut_tv.tv_sec;
1360# elif defined(HAVE_TIME_IN_UTMPX)
1361 li->tv_sec = utx.ut_time;
1362# endif
1363 line_fullname(li->line, utx.ut_line, sizeof(li->line));
1364# if defined(HAVE_HOST_IN_UTMPX)
1365 strlcpy(li->hostname, utx.ut_host,
1366 MIN_SIZEOF(li->hostname, utx.ut_host));
1367# endif
1368 continue;
1369 }
1370 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
1371 close(fd);
1372 return (0);
1373 }
1374 }
1375
1376 close(fd);
1377 return (1);
1378}
1379#endif /* USE_WTMPX */
1380
1381/**
1382 ** Low-level libutil login() functions
1383 **/
1384
1385#ifdef USE_LOGIN
1386static int
1387syslogin_perform_login(struct logininfo *li)
1388{
1389 struct utmp *ut;
1390
1391 ut = xmalloc(sizeof(*ut));
1392 construct_utmp(li, ut);
1393 login(ut);
1394 free(ut);
1395
1396 return (1);
1397}
1398
1399static int
1400syslogin_perform_logout(struct logininfo *li)
1401{
1402# ifdef HAVE_LOGOUT
1403 char line[UT_LINESIZE];
1404
1405 (void)line_stripname(line, li->line, sizeof(line));
1406
1407 if (!logout(line))
1408 logit("%s: logout() returned an error", __func__);
1409# ifdef HAVE_LOGWTMP
1410 else
1411 logwtmp(line, "", "");
1412# endif
1413 /* FIXME: (ATL - if the need arises) What to do if we have
1414 * login, but no logout? what if logout but no logwtmp? All
1415 * routines are in libutil so they should all be there,
1416 * but... */
1417# endif
1418 return (1);
1419}
1420
1421int
1422syslogin_write_entry(struct logininfo *li)
1423{
1424 switch (li->type) {
1425 case LTYPE_LOGIN:
1426 return (syslogin_perform_login(li));
1427 case LTYPE_LOGOUT:
1428 return (syslogin_perform_logout(li));
1429 default:
1430 logit("%s: Invalid type field", __func__);
1431 return (0);
1432 }
1433}
1434#endif /* USE_LOGIN */
1435
1436/* end of file log-syslogin.c */
1437
1438/**
1439 ** Low-level lastlog functions
1440 **/
1441
1442#ifdef USE_LASTLOG
1443#define LL_FILE 1
1444#define LL_DIR 2
1445#define LL_OTHER 3
1446
1447static void
1448lastlog_construct(struct logininfo *li, struct lastlog *last)
1449{
1450 /* clear the structure */
1451 memset(last, '\0', sizeof(*last));
1452
1453 line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
1454 strlcpy(last->ll_host, li->hostname,
1455 MIN_SIZEOF(last->ll_host, li->hostname));
1456 last->ll_time = li->tv_sec;
1457}
1458
1459static int
1460lastlog_filetype(char *filename)
1461{
1462 struct stat st;
1463
1464 if (stat(LASTLOG_FILE, &st) != 0) {
1465 logit("%s: Couldn't stat %s: %s", __func__,
1466 LASTLOG_FILE, strerror(errno));
1467 return (0);
1468 }
1469 if (S_ISDIR(st.st_mode))
1470 return (LL_DIR);
1471 else if (S_ISREG(st.st_mode))
1472 return (LL_FILE);
1473 else
1474 return (LL_OTHER);
1475}
1476
1477
1478/* open the file (using filemode) and seek to the login entry */
1479static int
1480lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1481{
1482 off_t offset;
1483 int type;
1484 char lastlog_file[1024];
1485
1486 type = lastlog_filetype(LASTLOG_FILE);
1487 switch (type) {
1488 case LL_FILE:
1489 strlcpy(lastlog_file, LASTLOG_FILE,
1490 sizeof(lastlog_file));
1491 break;
1492 case LL_DIR:
1493 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1494 LASTLOG_FILE, li->username);
1495 break;
1496 default:
1497 logit("%s: %.100s is not a file or directory!", __func__,
1498 LASTLOG_FILE);
1499 return (0);
1500 }
1501
1502 *fd = open(lastlog_file, filemode, 0600);
1503 if (*fd < 0) {
1504 debug("%s: Couldn't open %s: %s", __func__,
1505 lastlog_file, strerror(errno));
1506 return (0);
1507 }
1508
1509 if (type == LL_FILE) {
1510 /* find this uid's offset in the lastlog file */
1511 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
1512
1513 if (lseek(*fd, offset, SEEK_SET) != offset) {
1514 logit("%s: %s->lseek(): %s", __func__,
1515 lastlog_file, strerror(errno));
1516 return (0);
1517 }
1518 }
1519
1520 return (1);
1521}
1522
1523static int
1524lastlog_perform_login(struct logininfo *li)
1525{
1526 struct lastlog last;
1527 int fd;
1528
1529 /* create our struct lastlog */
1530 lastlog_construct(li, &last);
1531
1532 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
1533 return (0);
1534
1535 /* write the entry */
1536 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
1537 close(fd);
1538 logit("%s: Error writing to %s: %s", __func__,
1539 LASTLOG_FILE, strerror(errno));
1540 return (0);
1541 }
1542
1543 close(fd);
1544 return (1);
1545}
1546
1547int
1548lastlog_write_entry(struct logininfo *li)
1549{
1550 switch(li->type) {
1551 case LTYPE_LOGIN:
1552 return (lastlog_perform_login(li));
1553 default:
1554 logit("%s: Invalid type field", __func__);
1555 return (0);
1556 }
1557}
1558
1559static void
1560lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1561{
1562 line_fullname(li->line, last->ll_line, sizeof(li->line));
1563 strlcpy(li->hostname, last->ll_host,
1564 MIN_SIZEOF(li->hostname, last->ll_host));
1565 li->tv_sec = last->ll_time;
1566}
1567
1568int
1569lastlog_get_entry(struct logininfo *li)
1570{
1571 struct lastlog last;
1572 int fd, ret;
1573
1574 if (!lastlog_openseek(li, &fd, O_RDONLY))
1575 return (0);
1576
1577 ret = atomicio(read, fd, &last, sizeof(last));
1578 close(fd);
1579
1580 switch (ret) {
1581 case 0:
1582 memset(&last, '\0', sizeof(last));
1583 /* FALLTHRU */
1584 case sizeof(last):
1585 lastlog_populate_entry(li, &last);
1586 return (1);
1587 case -1:
1588 error("%s: Error reading from %s: %s", __func__,
1589 LASTLOG_FILE, strerror(errno));
1590 return (0);
1591 default:
1592 error("%s: Error reading from %s: Expecting %d, got %d",
170
171/**
172 ** prototypes for helper functions in this file
173 **/
174
175#if HAVE_UTMP_H
176void set_utmp_time(struct logininfo *li, struct utmp *ut);
177void construct_utmp(struct logininfo *li, struct utmp *ut);
178#endif
179
180#ifdef HAVE_UTMPX_H
181void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
182void construct_utmpx(struct logininfo *li, struct utmpx *ut);
183#endif
184
185int utmp_write_entry(struct logininfo *li);
186int utmpx_write_entry(struct logininfo *li);
187int wtmp_write_entry(struct logininfo *li);
188int wtmpx_write_entry(struct logininfo *li);
189int lastlog_write_entry(struct logininfo *li);
190int syslogin_write_entry(struct logininfo *li);
191
192int getlast_entry(struct logininfo *li);
193int lastlog_get_entry(struct logininfo *li);
194int wtmp_get_entry(struct logininfo *li);
195int wtmpx_get_entry(struct logininfo *li);
196
197extern Buffer loginmsg;
198
199/* pick the shortest string */
200#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
201
202/**
203 ** platform-independent login functions
204 **/
205
206/*
207 * login_login(struct logininfo *) - Record a login
208 *
209 * Call with a pointer to a struct logininfo initialised with
210 * login_init_entry() or login_alloc_entry()
211 *
212 * Returns:
213 * >0 if successful
214 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
215 */
216int
217login_login(struct logininfo *li)
218{
219 li->type = LTYPE_LOGIN;
220 return (login_write(li));
221}
222
223
224/*
225 * login_logout(struct logininfo *) - Record a logout
226 *
227 * Call as with login_login()
228 *
229 * Returns:
230 * >0 if successful
231 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
232 */
233int
234login_logout(struct logininfo *li)
235{
236 li->type = LTYPE_LOGOUT;
237 return (login_write(li));
238}
239
240/*
241 * login_get_lastlog_time(int) - Retrieve the last login time
242 *
243 * Retrieve the last login time for the given uid. Will try to use the
244 * system lastlog facilities if they are available, but will fall back
245 * to looking in wtmp/wtmpx if necessary
246 *
247 * Returns:
248 * 0 on failure, or if user has never logged in
249 * Time in seconds from the epoch if successful
250 *
251 * Useful preprocessor symbols:
252 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
253 * info
254 * USE_LASTLOG: If set, indicates the presence of system lastlog
255 * facilities. If this and DISABLE_LASTLOG are not set,
256 * try to retrieve lastlog information from wtmp/wtmpx.
257 */
258unsigned int
259login_get_lastlog_time(const int uid)
260{
261 struct logininfo li;
262
263 if (login_get_lastlog(&li, uid))
264 return (li.tv_sec);
265 else
266 return (0);
267}
268
269/*
270 * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
271 *
272 * Retrieve a logininfo structure populated (only partially) with
273 * information from the system lastlog data, or from wtmp/wtmpx if no
274 * system lastlog information exists.
275 *
276 * Note this routine must be given a pre-allocated logininfo.
277 *
278 * Returns:
279 * >0: A pointer to your struct logininfo if successful
280 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
281 */
282struct logininfo *
283login_get_lastlog(struct logininfo *li, const int uid)
284{
285 struct passwd *pw;
286
287 memset(li, '\0', sizeof(*li));
288 li->uid = uid;
289
290 /*
291 * If we don't have a 'real' lastlog, we need the username to
292 * reliably search wtmp(x) for the last login (see
293 * wtmp_get_entry().)
294 */
295 pw = getpwuid(uid);
296 if (pw == NULL)
297 fatal("%s: Cannot find account for uid %i", __func__, uid);
298
299 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
300 * username (XXX - so check for trunc!) */
301 strlcpy(li->username, pw->pw_name, sizeof(li->username));
302
303 if (getlast_entry(li))
304 return (li);
305 else
306 return (NULL);
307}
308
309
310/*
311 * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
312 * a logininfo structure
313 *
314 * This function creates a new struct logininfo, a data structure
315 * meant to carry the information required to portably record login info.
316 *
317 * Returns a pointer to a newly created struct logininfo. If memory
318 * allocation fails, the program halts.
319 */
320struct
321logininfo *login_alloc_entry(int pid, const char *username,
322 const char *hostname, const char *line)
323{
324 struct logininfo *newli;
325
326 newli = xmalloc(sizeof(*newli));
327 login_init_entry(newli, pid, username, hostname, line);
328 return (newli);
329}
330
331
332/* login_free_entry(struct logininfo *) - free struct memory */
333void
334login_free_entry(struct logininfo *li)
335{
336 xfree(li);
337}
338
339
340/* login_init_entry(struct logininfo *, int, char*, char*, char*)
341 * - initialise a struct logininfo
342 *
343 * Populates a new struct logininfo, a data structure meant to carry
344 * the information required to portably record login info.
345 *
346 * Returns: 1
347 */
348int
349login_init_entry(struct logininfo *li, int pid, const char *username,
350 const char *hostname, const char *line)
351{
352 struct passwd *pw;
353
354 memset(li, 0, sizeof(*li));
355
356 li->pid = pid;
357
358 /* set the line information */
359 if (line)
360 line_fullname(li->line, line, sizeof(li->line));
361
362 if (username) {
363 strlcpy(li->username, username, sizeof(li->username));
364 pw = getpwnam(li->username);
365 if (pw == NULL) {
366 fatal("%s: Cannot find user \"%s\"", __func__,
367 li->username);
368 }
369 li->uid = pw->pw_uid;
370 }
371
372 if (hostname)
373 strlcpy(li->hostname, hostname, sizeof(li->hostname));
374
375 return (1);
376}
377
378/*
379 * login_set_current_time(struct logininfo *) - set the current time
380 *
381 * Set the current time in a logininfo structure. This function is
382 * meant to eliminate the need to deal with system dependencies for
383 * time handling.
384 */
385void
386login_set_current_time(struct logininfo *li)
387{
388 struct timeval tv;
389
390 gettimeofday(&tv, NULL);
391
392 li->tv_sec = tv.tv_sec;
393 li->tv_usec = tv.tv_usec;
394}
395
396/* copy a sockaddr_* into our logininfo */
397void
398login_set_addr(struct logininfo *li, const struct sockaddr *sa,
399 const unsigned int sa_size)
400{
401 unsigned int bufsize = sa_size;
402
403 /* make sure we don't overrun our union */
404 if (sizeof(li->hostaddr) < sa_size)
405 bufsize = sizeof(li->hostaddr);
406
407 memcpy(&li->hostaddr.sa, sa, bufsize);
408}
409
410
411/**
412 ** login_write: Call low-level recording functions based on autoconf
413 ** results
414 **/
415int
416login_write(struct logininfo *li)
417{
418#ifndef HAVE_CYGWIN
419 if (geteuid() != 0) {
420 logit("Attempt to write login records by non-root user (aborting)");
421 return (1);
422 }
423#endif
424
425 /* set the timestamp */
426 login_set_current_time(li);
427#ifdef USE_LOGIN
428 syslogin_write_entry(li);
429#endif
430#ifdef USE_LASTLOG
431 if (li->type == LTYPE_LOGIN)
432 lastlog_write_entry(li);
433#endif
434#ifdef USE_UTMP
435 utmp_write_entry(li);
436#endif
437#ifdef USE_WTMP
438 wtmp_write_entry(li);
439#endif
440#ifdef USE_UTMPX
441 utmpx_write_entry(li);
442#endif
443#ifdef USE_WTMPX
444 wtmpx_write_entry(li);
445#endif
446#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
447 if (li->type == LTYPE_LOGIN &&
448 !sys_auth_record_login(li->username,li->hostname,li->line,
449 &loginmsg))
450 logit("Writing login record failed for %s", li->username);
451#endif
452#ifdef SSH_AUDIT_EVENTS
453 if (li->type == LTYPE_LOGIN)
454 audit_session_open(li->line);
455 else if (li->type == LTYPE_LOGOUT)
456 audit_session_close(li->line);
457#endif
458 return (0);
459}
460
461#ifdef LOGIN_NEEDS_UTMPX
462int
463login_utmp_only(struct logininfo *li)
464{
465 li->type = LTYPE_LOGIN;
466 login_set_current_time(li);
467# ifdef USE_UTMP
468 utmp_write_entry(li);
469# endif
470# ifdef USE_WTMP
471 wtmp_write_entry(li);
472# endif
473# ifdef USE_UTMPX
474 utmpx_write_entry(li);
475# endif
476# ifdef USE_WTMPX
477 wtmpx_write_entry(li);
478# endif
479 return (0);
480}
481#endif
482
483/**
484 ** getlast_entry: Call low-level functions to retrieve the last login
485 ** time.
486 **/
487
488/* take the uid in li and return the last login time */
489int
490getlast_entry(struct logininfo *li)
491{
492#ifdef USE_LASTLOG
493 return(lastlog_get_entry(li));
494#else /* !USE_LASTLOG */
495
496#if defined(DISABLE_LASTLOG)
497 /* On some systems we shouldn't even try to obtain last login
498 * time, e.g. AIX */
499 return (0);
500# elif defined(USE_WTMP) && \
501 (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
502 /* retrieve last login time from utmp */
503 return (wtmp_get_entry(li));
504# elif defined(USE_WTMPX) && \
505 (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
506 /* If wtmp isn't available, try wtmpx */
507 return (wtmpx_get_entry(li));
508# else
509 /* Give up: No means of retrieving last login time */
510 return (0);
511# endif /* DISABLE_LASTLOG */
512#endif /* USE_LASTLOG */
513}
514
515
516
517/*
518 * 'line' string utility functions
519 *
520 * These functions process the 'line' string into one of three forms:
521 *
522 * 1. The full filename (including '/dev')
523 * 2. The stripped name (excluding '/dev')
524 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
525 * /dev/pts/1 -> ts/1 )
526 *
527 * Form 3 is used on some systems to identify a .tmp.? entry when
528 * attempting to remove it. Typically both addition and removal is
529 * performed by one application - say, sshd - so as long as the choice
530 * uniquely identifies a terminal it's ok.
531 */
532
533
534/*
535 * line_fullname(): add the leading '/dev/' if it doesn't exist make
536 * sure dst has enough space, if not just copy src (ugh)
537 */
538char *
539line_fullname(char *dst, const char *src, u_int dstsize)
540{
541 memset(dst, '\0', dstsize);
542 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
543 strlcpy(dst, src, dstsize);
544 else {
545 strlcpy(dst, "/dev/", dstsize);
546 strlcat(dst, src, dstsize);
547 }
548 return (dst);
549}
550
551/* line_stripname(): strip the leading '/dev' if it exists, return dst */
552char *
553line_stripname(char *dst, const char *src, int dstsize)
554{
555 memset(dst, '\0', dstsize);
556 if (strncmp(src, "/dev/", 5) == 0)
557 strlcpy(dst, src + 5, dstsize);
558 else
559 strlcpy(dst, src, dstsize);
560 return (dst);
561}
562
563/*
564 * line_abbrevname(): Return the abbreviated (usually four-character)
565 * form of the line (Just use the last <dstsize> characters of the
566 * full name.)
567 *
568 * NOTE: use strncpy because we do NOT necessarily want zero
569 * termination
570 */
571char *
572line_abbrevname(char *dst, const char *src, int dstsize)
573{
574 size_t len;
575
576 memset(dst, '\0', dstsize);
577
578 /* Always skip prefix if present */
579 if (strncmp(src, "/dev/", 5) == 0)
580 src += 5;
581
582#ifdef WITH_ABBREV_NO_TTY
583 if (strncmp(src, "tty", 3) == 0)
584 src += 3;
585#endif
586
587 len = strlen(src);
588
589 if (len > 0) {
590 if (((int)len - dstsize) > 0)
591 src += ((int)len - dstsize);
592
593 /* note: _don't_ change this to strlcpy */
594 strncpy(dst, src, (size_t)dstsize);
595 }
596
597 return (dst);
598}
599
600/**
601 ** utmp utility functions
602 **
603 ** These functions manipulate struct utmp, taking system differences
604 ** into account.
605 **/
606
607#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
608
609/* build the utmp structure */
610void
611set_utmp_time(struct logininfo *li, struct utmp *ut)
612{
613# if defined(HAVE_TV_IN_UTMP)
614 ut->ut_tv.tv_sec = li->tv_sec;
615 ut->ut_tv.tv_usec = li->tv_usec;
616# elif defined(HAVE_TIME_IN_UTMP)
617 ut->ut_time = li->tv_sec;
618# endif
619}
620
621void
622construct_utmp(struct logininfo *li,
623 struct utmp *ut)
624{
625# ifdef HAVE_ADDR_V6_IN_UTMP
626 struct sockaddr_in6 *sa6;
627# endif
628
629 memset(ut, '\0', sizeof(*ut));
630
631 /* First fill out fields used for both logins and logouts */
632
633# ifdef HAVE_ID_IN_UTMP
634 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
635# endif
636
637# ifdef HAVE_TYPE_IN_UTMP
638 /* This is done here to keep utmp constants out of struct logininfo */
639 switch (li->type) {
640 case LTYPE_LOGIN:
641 ut->ut_type = USER_PROCESS;
642#ifdef _UNICOS
643 cray_set_tmpdir(ut);
644#endif
645 break;
646 case LTYPE_LOGOUT:
647 ut->ut_type = DEAD_PROCESS;
648#ifdef _UNICOS
649 cray_retain_utmp(ut, li->pid);
650#endif
651 break;
652 }
653# endif
654 set_utmp_time(li, ut);
655
656 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
657
658# ifdef HAVE_PID_IN_UTMP
659 ut->ut_pid = li->pid;
660# endif
661
662 /* If we're logging out, leave all other fields blank */
663 if (li->type == LTYPE_LOGOUT)
664 return;
665
666 /*
667 * These fields are only used when logging in, and are blank
668 * for logouts.
669 */
670
671 /* Use strncpy because we don't necessarily want null termination */
672 strncpy(ut->ut_name, li->username,
673 MIN_SIZEOF(ut->ut_name, li->username));
674# ifdef HAVE_HOST_IN_UTMP
675 realhostname_sa(ut->ut_host, sizeof ut->ut_host,
676 &li->hostaddr.sa, li->hostaddr.sa.sa_len);
677# endif
678# ifdef HAVE_ADDR_IN_UTMP
679 /* this is just a 32-bit IP address */
680 if (li->hostaddr.sa.sa_family == AF_INET)
681 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
682# endif
683# ifdef HAVE_ADDR_V6_IN_UTMP
684 /* this is just a 128-bit IPv6 address */
685 if (li->hostaddr.sa.sa_family == AF_INET6) {
686 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
687 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
688 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
689 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
690 ut->ut_addr_v6[1] = 0;
691 ut->ut_addr_v6[2] = 0;
692 ut->ut_addr_v6[3] = 0;
693 }
694 }
695# endif
696}
697#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
698
699/**
700 ** utmpx utility functions
701 **
702 ** These functions manipulate struct utmpx, accounting for system
703 ** variations.
704 **/
705
706#if defined(USE_UTMPX) || defined (USE_WTMPX)
707/* build the utmpx structure */
708void
709set_utmpx_time(struct logininfo *li, struct utmpx *utx)
710{
711# if defined(HAVE_TV_IN_UTMPX)
712 utx->ut_tv.tv_sec = li->tv_sec;
713 utx->ut_tv.tv_usec = li->tv_usec;
714# elif defined(HAVE_TIME_IN_UTMPX)
715 utx->ut_time = li->tv_sec;
716# endif
717}
718
719void
720construct_utmpx(struct logininfo *li, struct utmpx *utx)
721{
722# ifdef HAVE_ADDR_V6_IN_UTMP
723 struct sockaddr_in6 *sa6;
724# endif
725 memset(utx, '\0', sizeof(*utx));
726
727# ifdef HAVE_ID_IN_UTMPX
728 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
729# endif
730
731 /* this is done here to keep utmp constants out of loginrec.h */
732 switch (li->type) {
733 case LTYPE_LOGIN:
734 utx->ut_type = USER_PROCESS;
735 break;
736 case LTYPE_LOGOUT:
737 utx->ut_type = DEAD_PROCESS;
738 break;
739 }
740 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
741 set_utmpx_time(li, utx);
742 utx->ut_pid = li->pid;
743
744 /* strncpy(): Don't necessarily want null termination */
745 strncpy(utx->ut_name, li->username,
746 MIN_SIZEOF(utx->ut_name, li->username));
747
748 if (li->type == LTYPE_LOGOUT)
749 return;
750
751 /*
752 * These fields are only used when logging in, and are blank
753 * for logouts.
754 */
755
756# ifdef HAVE_HOST_IN_UTMPX
757 strncpy(utx->ut_host, li->hostname,
758 MIN_SIZEOF(utx->ut_host, li->hostname));
759# endif
760# ifdef HAVE_ADDR_IN_UTMPX
761 /* this is just a 32-bit IP address */
762 if (li->hostaddr.sa.sa_family == AF_INET)
763 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
764# endif
765# ifdef HAVE_ADDR_V6_IN_UTMP
766 /* this is just a 128-bit IPv6 address */
767 if (li->hostaddr.sa.sa_family == AF_INET6) {
768 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
769 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
770 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
771 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
772 ut->ut_addr_v6[1] = 0;
773 ut->ut_addr_v6[2] = 0;
774 ut->ut_addr_v6[3] = 0;
775 }
776 }
777# endif
778# ifdef HAVE_SYSLEN_IN_UTMPX
779 /* ut_syslen is the length of the utx_host string */
780 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
781# endif
782}
783#endif /* USE_UTMPX || USE_WTMPX */
784
785/**
786 ** Low-level utmp functions
787 **/
788
789/* FIXME: (ATL) utmp_write_direct needs testing */
790#ifdef USE_UTMP
791
792/* if we can, use pututline() etc. */
793# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
794 defined(HAVE_PUTUTLINE)
795# define UTMP_USE_LIBRARY
796# endif
797
798
799/* write a utmp entry with the system's help (pututline() and pals) */
800# ifdef UTMP_USE_LIBRARY
801static int
802utmp_write_library(struct logininfo *li, struct utmp *ut)
803{
804 setutent();
805 pututline(ut);
806# ifdef HAVE_ENDUTENT
807 endutent();
808# endif
809 return (1);
810}
811# else /* UTMP_USE_LIBRARY */
812
813/*
814 * Write a utmp entry direct to the file
815 * This is a slightly modification of code in OpenBSD's login.c
816 */
817static int
818utmp_write_direct(struct logininfo *li, struct utmp *ut)
819{
820 struct utmp old_ut;
821 register int fd;
822 int tty;
823
824 /* FIXME: (ATL) ttyslot() needs local implementation */
825
826#if defined(HAVE_GETTTYENT)
827 struct ttyent *ty;
828
829 tty=0;
830 setttyent();
831 while (NULL != (ty = getttyent())) {
832 tty++;
833 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
834 break;
835 }
836 endttyent();
837
838 if (NULL == ty) {
839 logit("%s: tty not found", __func__);
840 return (0);
841 }
842#else /* FIXME */
843
844 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
845
846#endif /* HAVE_GETTTYENT */
847
848 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
849 off_t pos, ret;
850
851 pos = (off_t)tty * sizeof(struct utmp);
852 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
853 logit("%s: lseek: %s", __func__, strerror(errno));
854 return (0);
855 }
856 if (ret != pos) {
857 logit("%s: Couldn't seek to tty %d slot in %s",
858 __func__, tty, UTMP_FILE);
859 return (0);
860 }
861 /*
862 * Prevent luser from zero'ing out ut_host.
863 * If the new ut_line is empty but the old one is not
864 * and ut_line and ut_name match, preserve the old ut_line.
865 */
866 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
867 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
868 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
869 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
870 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
871
872 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
873 logit("%s: lseek: %s", __func__, strerror(errno));
874 return (0);
875 }
876 if (ret != pos) {
877 logit("%s: Couldn't seek to tty %d slot in %s",
878 __func__, tty, UTMP_FILE);
879 return (0);
880 }
881 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
882 logit("%s: error writing %s: %s", __func__,
883 UTMP_FILE, strerror(errno));
884 }
885
886 close(fd);
887 return (1);
888 } else {
889 return (0);
890 }
891}
892# endif /* UTMP_USE_LIBRARY */
893
894static int
895utmp_perform_login(struct logininfo *li)
896{
897 struct utmp ut;
898
899 construct_utmp(li, &ut);
900# ifdef UTMP_USE_LIBRARY
901 if (!utmp_write_library(li, &ut)) {
902 logit("%s: utmp_write_library() failed", __func__);
903 return (0);
904 }
905# else
906 if (!utmp_write_direct(li, &ut)) {
907 logit("%s: utmp_write_direct() failed", __func__);
908 return (0);
909 }
910# endif
911 return (1);
912}
913
914
915static int
916utmp_perform_logout(struct logininfo *li)
917{
918 struct utmp ut;
919
920 construct_utmp(li, &ut);
921# ifdef UTMP_USE_LIBRARY
922 if (!utmp_write_library(li, &ut)) {
923 logit("%s: utmp_write_library() failed", __func__);
924 return (0);
925 }
926# else
927 if (!utmp_write_direct(li, &ut)) {
928 logit("%s: utmp_write_direct() failed", __func__);
929 return (0);
930 }
931# endif
932 return (1);
933}
934
935
936int
937utmp_write_entry(struct logininfo *li)
938{
939 switch(li->type) {
940 case LTYPE_LOGIN:
941 return (utmp_perform_login(li));
942
943 case LTYPE_LOGOUT:
944 return (utmp_perform_logout(li));
945
946 default:
947 logit("%s: invalid type field", __func__);
948 return (0);
949 }
950}
951#endif /* USE_UTMP */
952
953
954/**
955 ** Low-level utmpx functions
956 **/
957
958/* not much point if we don't want utmpx entries */
959#ifdef USE_UTMPX
960
961/* if we have the wherewithall, use pututxline etc. */
962# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
963 defined(HAVE_PUTUTXLINE)
964# define UTMPX_USE_LIBRARY
965# endif
966
967
968/* write a utmpx entry with the system's help (pututxline() and pals) */
969# ifdef UTMPX_USE_LIBRARY
970static int
971utmpx_write_library(struct logininfo *li, struct utmpx *utx)
972{
973 setutxent();
974 pututxline(utx);
975
976# ifdef HAVE_ENDUTXENT
977 endutxent();
978# endif
979 return (1);
980}
981
982# else /* UTMPX_USE_LIBRARY */
983
984/* write a utmp entry direct to the file */
985static int
986utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
987{
988 logit("%s: not implemented!", __func__);
989 return (0);
990}
991# endif /* UTMPX_USE_LIBRARY */
992
993static int
994utmpx_perform_login(struct logininfo *li)
995{
996 struct utmpx utx;
997
998 construct_utmpx(li, &utx);
999# ifdef UTMPX_USE_LIBRARY
1000 if (!utmpx_write_library(li, &utx)) {
1001 logit("%s: utmp_write_library() failed", __func__);
1002 return (0);
1003 }
1004# else
1005 if (!utmpx_write_direct(li, &ut)) {
1006 logit("%s: utmp_write_direct() failed", __func__);
1007 return (0);
1008 }
1009# endif
1010 return (1);
1011}
1012
1013
1014static int
1015utmpx_perform_logout(struct logininfo *li)
1016{
1017 struct utmpx utx;
1018
1019 construct_utmpx(li, &utx);
1020# ifdef HAVE_ID_IN_UTMPX
1021 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
1022# endif
1023# ifdef HAVE_TYPE_IN_UTMPX
1024 utx.ut_type = DEAD_PROCESS;
1025# endif
1026
1027# ifdef UTMPX_USE_LIBRARY
1028 utmpx_write_library(li, &utx);
1029# else
1030 utmpx_write_direct(li, &utx);
1031# endif
1032 return (1);
1033}
1034
1035int
1036utmpx_write_entry(struct logininfo *li)
1037{
1038 switch(li->type) {
1039 case LTYPE_LOGIN:
1040 return (utmpx_perform_login(li));
1041 case LTYPE_LOGOUT:
1042 return (utmpx_perform_logout(li));
1043 default:
1044 logit("%s: invalid type field", __func__);
1045 return (0);
1046 }
1047}
1048#endif /* USE_UTMPX */
1049
1050
1051/**
1052 ** Low-level wtmp functions
1053 **/
1054
1055#ifdef USE_WTMP
1056
1057/*
1058 * Write a wtmp entry direct to the end of the file
1059 * This is a slight modification of code in OpenBSD's logwtmp.c
1060 */
1061static int
1062wtmp_write(struct logininfo *li, struct utmp *ut)
1063{
1064 struct stat buf;
1065 int fd, ret = 1;
1066
1067 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
1068 logit("%s: problem writing %s: %s", __func__,
1069 WTMP_FILE, strerror(errno));
1070 return (0);
1071 }
1072 if (fstat(fd, &buf) == 0)
1073 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
1074 ftruncate(fd, buf.st_size);
1075 logit("%s: problem writing %s: %s", __func__,
1076 WTMP_FILE, strerror(errno));
1077 ret = 0;
1078 }
1079 close(fd);
1080 return (ret);
1081}
1082
1083static int
1084wtmp_perform_login(struct logininfo *li)
1085{
1086 struct utmp ut;
1087
1088 construct_utmp(li, &ut);
1089 return (wtmp_write(li, &ut));
1090}
1091
1092
1093static int
1094wtmp_perform_logout(struct logininfo *li)
1095{
1096 struct utmp ut;
1097
1098 construct_utmp(li, &ut);
1099 return (wtmp_write(li, &ut));
1100}
1101
1102
1103int
1104wtmp_write_entry(struct logininfo *li)
1105{
1106 switch(li->type) {
1107 case LTYPE_LOGIN:
1108 return (wtmp_perform_login(li));
1109 case LTYPE_LOGOUT:
1110 return (wtmp_perform_logout(li));
1111 default:
1112 logit("%s: invalid type field", __func__);
1113 return (0);
1114 }
1115}
1116
1117
1118/*
1119 * Notes on fetching login data from wtmp/wtmpx
1120 *
1121 * Logouts are usually recorded with (amongst other things) a blank
1122 * username on a given tty line. However, some systems (HP-UX is one)
1123 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1124 *
1125 * Since we're only looking for logins here, we know that the username
1126 * must be set correctly. On systems that leave it in, we check for
1127 * ut_type==USER_PROCESS (indicating a login.)
1128 *
1129 * Portability: Some systems may set something other than USER_PROCESS
1130 * to indicate a login process. I don't know of any as I write. Also,
1131 * it's possible that some systems may both leave the username in
1132 * place and not have ut_type.
1133 */
1134
1135/* return true if this wtmp entry indicates a login */
1136static int
1137wtmp_islogin(struct logininfo *li, struct utmp *ut)
1138{
1139 if (strncmp(li->username, ut->ut_name,
1140 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
1141# ifdef HAVE_TYPE_IN_UTMP
1142 if (ut->ut_type & USER_PROCESS)
1143 return (1);
1144# else
1145 return (1);
1146# endif
1147 }
1148 return (0);
1149}
1150
1151int
1152wtmp_get_entry(struct logininfo *li)
1153{
1154 struct stat st;
1155 struct utmp ut;
1156 int fd, found = 0;
1157
1158 /* Clear the time entries in our logininfo */
1159 li->tv_sec = li->tv_usec = 0;
1160
1161 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
1162 logit("%s: problem opening %s: %s", __func__,
1163 WTMP_FILE, strerror(errno));
1164 return (0);
1165 }
1166 if (fstat(fd, &st) != 0) {
1167 logit("%s: couldn't stat %s: %s", __func__,
1168 WTMP_FILE, strerror(errno));
1169 close(fd);
1170 return (0);
1171 }
1172
1173 /* Seek to the start of the last struct utmp */
1174 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
1175 /* Looks like we've got a fresh wtmp file */
1176 close(fd);
1177 return (0);
1178 }
1179
1180 while (!found) {
1181 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
1182 logit("%s: read of %s failed: %s", __func__,
1183 WTMP_FILE, strerror(errno));
1184 close (fd);
1185 return (0);
1186 }
1187 if ( wtmp_islogin(li, &ut) ) {
1188 found = 1;
1189 /*
1190 * We've already checked for a time in struct
1191 * utmp, in login_getlast()
1192 */
1193# ifdef HAVE_TIME_IN_UTMP
1194 li->tv_sec = ut.ut_time;
1195# else
1196# if HAVE_TV_IN_UTMP
1197 li->tv_sec = ut.ut_tv.tv_sec;
1198# endif
1199# endif
1200 line_fullname(li->line, ut.ut_line,
1201 MIN_SIZEOF(li->line, ut.ut_line));
1202# ifdef HAVE_HOST_IN_UTMP
1203 strlcpy(li->hostname, ut.ut_host,
1204 MIN_SIZEOF(li->hostname, ut.ut_host));
1205# endif
1206 continue;
1207 }
1208 /* Seek back 2 x struct utmp */
1209 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
1210 /* We've found the start of the file, so quit */
1211 close(fd);
1212 return (0);
1213 }
1214 }
1215
1216 /* We found an entry. Tidy up and return */
1217 close(fd);
1218 return (1);
1219}
1220# endif /* USE_WTMP */
1221
1222
1223/**
1224 ** Low-level wtmpx functions
1225 **/
1226
1227#ifdef USE_WTMPX
1228/*
1229 * Write a wtmpx entry direct to the end of the file
1230 * This is a slight modification of code in OpenBSD's logwtmp.c
1231 */
1232static int
1233wtmpx_write(struct logininfo *li, struct utmpx *utx)
1234{
1235#ifndef HAVE_UPDWTMPX
1236 struct stat buf;
1237 int fd, ret = 1;
1238
1239 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
1240 logit("%s: problem opening %s: %s", __func__,
1241 WTMPX_FILE, strerror(errno));
1242 return (0);
1243 }
1244
1245 if (fstat(fd, &buf) == 0)
1246 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
1247 ftruncate(fd, buf.st_size);
1248 logit("%s: problem writing %s: %s", __func__,
1249 WTMPX_FILE, strerror(errno));
1250 ret = 0;
1251 }
1252 close(fd);
1253
1254 return (ret);
1255#else
1256 updwtmpx(WTMPX_FILE, utx);
1257 return (1);
1258#endif
1259}
1260
1261
1262static int
1263wtmpx_perform_login(struct logininfo *li)
1264{
1265 struct utmpx utx;
1266
1267 construct_utmpx(li, &utx);
1268 return (wtmpx_write(li, &utx));
1269}
1270
1271
1272static int
1273wtmpx_perform_logout(struct logininfo *li)
1274{
1275 struct utmpx utx;
1276
1277 construct_utmpx(li, &utx);
1278 return (wtmpx_write(li, &utx));
1279}
1280
1281
1282int
1283wtmpx_write_entry(struct logininfo *li)
1284{
1285 switch(li->type) {
1286 case LTYPE_LOGIN:
1287 return (wtmpx_perform_login(li));
1288 case LTYPE_LOGOUT:
1289 return (wtmpx_perform_logout(li));
1290 default:
1291 logit("%s: invalid type field", __func__);
1292 return (0);
1293 }
1294}
1295
1296/* Please see the notes above wtmp_islogin() for information about the
1297 next two functions */
1298
1299/* Return true if this wtmpx entry indicates a login */
1300static int
1301wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1302{
1303 if (strncmp(li->username, utx->ut_name,
1304 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
1305# ifdef HAVE_TYPE_IN_UTMPX
1306 if (utx->ut_type == USER_PROCESS)
1307 return (1);
1308# else
1309 return (1);
1310# endif
1311 }
1312 return (0);
1313}
1314
1315
1316int
1317wtmpx_get_entry(struct logininfo *li)
1318{
1319 struct stat st;
1320 struct utmpx utx;
1321 int fd, found=0;
1322
1323 /* Clear the time entries */
1324 li->tv_sec = li->tv_usec = 0;
1325
1326 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
1327 logit("%s: problem opening %s: %s", __func__,
1328 WTMPX_FILE, strerror(errno));
1329 return (0);
1330 }
1331 if (fstat(fd, &st) != 0) {
1332 logit("%s: couldn't stat %s: %s", __func__,
1333 WTMPX_FILE, strerror(errno));
1334 close(fd);
1335 return (0);
1336 }
1337
1338 /* Seek to the start of the last struct utmpx */
1339 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
1340 /* probably a newly rotated wtmpx file */
1341 close(fd);
1342 return (0);
1343 }
1344
1345 while (!found) {
1346 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
1347 logit("%s: read of %s failed: %s", __func__,
1348 WTMPX_FILE, strerror(errno));
1349 close (fd);
1350 return (0);
1351 }
1352 /*
1353 * Logouts are recorded as a blank username on a particular
1354 * line. So, we just need to find the username in struct utmpx
1355 */
1356 if (wtmpx_islogin(li, &utx)) {
1357 found = 1;
1358# if defined(HAVE_TV_IN_UTMPX)
1359 li->tv_sec = utx.ut_tv.tv_sec;
1360# elif defined(HAVE_TIME_IN_UTMPX)
1361 li->tv_sec = utx.ut_time;
1362# endif
1363 line_fullname(li->line, utx.ut_line, sizeof(li->line));
1364# if defined(HAVE_HOST_IN_UTMPX)
1365 strlcpy(li->hostname, utx.ut_host,
1366 MIN_SIZEOF(li->hostname, utx.ut_host));
1367# endif
1368 continue;
1369 }
1370 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
1371 close(fd);
1372 return (0);
1373 }
1374 }
1375
1376 close(fd);
1377 return (1);
1378}
1379#endif /* USE_WTMPX */
1380
1381/**
1382 ** Low-level libutil login() functions
1383 **/
1384
1385#ifdef USE_LOGIN
1386static int
1387syslogin_perform_login(struct logininfo *li)
1388{
1389 struct utmp *ut;
1390
1391 ut = xmalloc(sizeof(*ut));
1392 construct_utmp(li, ut);
1393 login(ut);
1394 free(ut);
1395
1396 return (1);
1397}
1398
1399static int
1400syslogin_perform_logout(struct logininfo *li)
1401{
1402# ifdef HAVE_LOGOUT
1403 char line[UT_LINESIZE];
1404
1405 (void)line_stripname(line, li->line, sizeof(line));
1406
1407 if (!logout(line))
1408 logit("%s: logout() returned an error", __func__);
1409# ifdef HAVE_LOGWTMP
1410 else
1411 logwtmp(line, "", "");
1412# endif
1413 /* FIXME: (ATL - if the need arises) What to do if we have
1414 * login, but no logout? what if logout but no logwtmp? All
1415 * routines are in libutil so they should all be there,
1416 * but... */
1417# endif
1418 return (1);
1419}
1420
1421int
1422syslogin_write_entry(struct logininfo *li)
1423{
1424 switch (li->type) {
1425 case LTYPE_LOGIN:
1426 return (syslogin_perform_login(li));
1427 case LTYPE_LOGOUT:
1428 return (syslogin_perform_logout(li));
1429 default:
1430 logit("%s: Invalid type field", __func__);
1431 return (0);
1432 }
1433}
1434#endif /* USE_LOGIN */
1435
1436/* end of file log-syslogin.c */
1437
1438/**
1439 ** Low-level lastlog functions
1440 **/
1441
1442#ifdef USE_LASTLOG
1443#define LL_FILE 1
1444#define LL_DIR 2
1445#define LL_OTHER 3
1446
1447static void
1448lastlog_construct(struct logininfo *li, struct lastlog *last)
1449{
1450 /* clear the structure */
1451 memset(last, '\0', sizeof(*last));
1452
1453 line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
1454 strlcpy(last->ll_host, li->hostname,
1455 MIN_SIZEOF(last->ll_host, li->hostname));
1456 last->ll_time = li->tv_sec;
1457}
1458
1459static int
1460lastlog_filetype(char *filename)
1461{
1462 struct stat st;
1463
1464 if (stat(LASTLOG_FILE, &st) != 0) {
1465 logit("%s: Couldn't stat %s: %s", __func__,
1466 LASTLOG_FILE, strerror(errno));
1467 return (0);
1468 }
1469 if (S_ISDIR(st.st_mode))
1470 return (LL_DIR);
1471 else if (S_ISREG(st.st_mode))
1472 return (LL_FILE);
1473 else
1474 return (LL_OTHER);
1475}
1476
1477
1478/* open the file (using filemode) and seek to the login entry */
1479static int
1480lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1481{
1482 off_t offset;
1483 int type;
1484 char lastlog_file[1024];
1485
1486 type = lastlog_filetype(LASTLOG_FILE);
1487 switch (type) {
1488 case LL_FILE:
1489 strlcpy(lastlog_file, LASTLOG_FILE,
1490 sizeof(lastlog_file));
1491 break;
1492 case LL_DIR:
1493 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1494 LASTLOG_FILE, li->username);
1495 break;
1496 default:
1497 logit("%s: %.100s is not a file or directory!", __func__,
1498 LASTLOG_FILE);
1499 return (0);
1500 }
1501
1502 *fd = open(lastlog_file, filemode, 0600);
1503 if (*fd < 0) {
1504 debug("%s: Couldn't open %s: %s", __func__,
1505 lastlog_file, strerror(errno));
1506 return (0);
1507 }
1508
1509 if (type == LL_FILE) {
1510 /* find this uid's offset in the lastlog file */
1511 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
1512
1513 if (lseek(*fd, offset, SEEK_SET) != offset) {
1514 logit("%s: %s->lseek(): %s", __func__,
1515 lastlog_file, strerror(errno));
1516 return (0);
1517 }
1518 }
1519
1520 return (1);
1521}
1522
1523static int
1524lastlog_perform_login(struct logininfo *li)
1525{
1526 struct lastlog last;
1527 int fd;
1528
1529 /* create our struct lastlog */
1530 lastlog_construct(li, &last);
1531
1532 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
1533 return (0);
1534
1535 /* write the entry */
1536 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
1537 close(fd);
1538 logit("%s: Error writing to %s: %s", __func__,
1539 LASTLOG_FILE, strerror(errno));
1540 return (0);
1541 }
1542
1543 close(fd);
1544 return (1);
1545}
1546
1547int
1548lastlog_write_entry(struct logininfo *li)
1549{
1550 switch(li->type) {
1551 case LTYPE_LOGIN:
1552 return (lastlog_perform_login(li));
1553 default:
1554 logit("%s: Invalid type field", __func__);
1555 return (0);
1556 }
1557}
1558
1559static void
1560lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1561{
1562 line_fullname(li->line, last->ll_line, sizeof(li->line));
1563 strlcpy(li->hostname, last->ll_host,
1564 MIN_SIZEOF(li->hostname, last->ll_host));
1565 li->tv_sec = last->ll_time;
1566}
1567
1568int
1569lastlog_get_entry(struct logininfo *li)
1570{
1571 struct lastlog last;
1572 int fd, ret;
1573
1574 if (!lastlog_openseek(li, &fd, O_RDONLY))
1575 return (0);
1576
1577 ret = atomicio(read, fd, &last, sizeof(last));
1578 close(fd);
1579
1580 switch (ret) {
1581 case 0:
1582 memset(&last, '\0', sizeof(last));
1583 /* FALLTHRU */
1584 case sizeof(last):
1585 lastlog_populate_entry(li, &last);
1586 return (1);
1587 case -1:
1588 error("%s: Error reading from %s: %s", __func__,
1589 LASTLOG_FILE, strerror(errno));
1590 return (0);
1591 default:
1592 error("%s: Error reading from %s: Expecting %d, got %d",
1593 __func__, LASTLOG_FILE, sizeof(last), ret);
1593 __func__, LASTLOG_FILE, (int)sizeof(last), ret);
1594 return (0);
1595 }
1596
1597 /* NOTREACHED */
1598 return (0);
1599}
1600#endif /* USE_LASTLOG */
1601
1602#ifdef USE_BTMP
1603 /*
1604 * Logs failed login attempts in _PATH_BTMP if that exists.
1605 * The most common login failure is to give password instead of username.
1606 * So the _PATH_BTMP file checked for the correct permission, so that
1607 * only root can read it.
1608 */
1609
1610void
1611record_failed_login(const char *username, const char *hostname,
1612 const char *ttyn)
1613{
1614 int fd;
1615 struct utmp ut;
1616 struct sockaddr_storage from;
1594 return (0);
1595 }
1596
1597 /* NOTREACHED */
1598 return (0);
1599}
1600#endif /* USE_LASTLOG */
1601
1602#ifdef USE_BTMP
1603 /*
1604 * Logs failed login attempts in _PATH_BTMP if that exists.
1605 * The most common login failure is to give password instead of username.
1606 * So the _PATH_BTMP file checked for the correct permission, so that
1607 * only root can read it.
1608 */
1609
1610void
1611record_failed_login(const char *username, const char *hostname,
1612 const char *ttyn)
1613{
1614 int fd;
1615 struct utmp ut;
1616 struct sockaddr_storage from;
1617 size_t fromlen = sizeof(from);
1617 socklen_t fromlen = sizeof(from);
1618 struct sockaddr_in *a4;
1619 struct sockaddr_in6 *a6;
1620 time_t t;
1621 struct stat fst;
1622
1623 if (geteuid() != 0)
1624 return;
1625 if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) {
1626 debug("Unable to open the btmp file %s: %s", _PATH_BTMP,
1627 strerror(errno));
1628 return;
1629 }
1630 if (fstat(fd, &fst) < 0) {
1631 logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP,
1632 strerror(errno));
1633 goto out;
1634 }
1635 if((fst.st_mode & (S_IRWXG | S_IRWXO)) || (fst.st_uid != 0)){
1636 logit("Excess permission or bad ownership on file %s",
1637 _PATH_BTMP);
1638 goto out;
1639 }
1640
1641 memset(&ut, 0, sizeof(ut));
1642 /* strncpy because we don't necessarily want nul termination */
1643 strncpy(ut.ut_user, username, sizeof(ut.ut_user));
1644 strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line));
1645
1646 time(&t);
1647 ut.ut_time = t; /* ut_time is not always a time_t */
1648 ut.ut_type = LOGIN_PROCESS;
1649 ut.ut_pid = getpid();
1650
1651 /* strncpy because we don't necessarily want nul termination */
1652 strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
1653
1654 if (packet_connection_is_on_socket() &&
1655 getpeername(packet_get_connection_in(),
1656 (struct sockaddr *)&from, &fromlen) == 0) {
1657 ipv64_normalise_mapped(&from, &fromlen);
1658 if (from.ss_family == AF_INET) {
1659 a4 = (struct sockaddr_in *)&from;
1660 memcpy(&ut.ut_addr, &(a4->sin_addr),
1661 MIN_SIZEOF(ut.ut_addr, a4->sin_addr));
1662 }
1663#ifdef HAVE_ADDR_V6_IN_UTMP
1664 if (from.ss_family == AF_INET6) {
1665 a6 = (struct sockaddr_in6 *)&from;
1666 memcpy(&ut.ut_addr_v6, &(a6->sin6_addr),
1667 MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr));
1668 }
1669#endif
1670 }
1671
1672 if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut))
1673 error("Failed to write to %s: %s", _PATH_BTMP,
1674 strerror(errno));
1675
1676out:
1677 close(fd);
1678}
1679#endif /* USE_BTMP */
1618 struct sockaddr_in *a4;
1619 struct sockaddr_in6 *a6;
1620 time_t t;
1621 struct stat fst;
1622
1623 if (geteuid() != 0)
1624 return;
1625 if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) {
1626 debug("Unable to open the btmp file %s: %s", _PATH_BTMP,
1627 strerror(errno));
1628 return;
1629 }
1630 if (fstat(fd, &fst) < 0) {
1631 logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP,
1632 strerror(errno));
1633 goto out;
1634 }
1635 if((fst.st_mode & (S_IRWXG | S_IRWXO)) || (fst.st_uid != 0)){
1636 logit("Excess permission or bad ownership on file %s",
1637 _PATH_BTMP);
1638 goto out;
1639 }
1640
1641 memset(&ut, 0, sizeof(ut));
1642 /* strncpy because we don't necessarily want nul termination */
1643 strncpy(ut.ut_user, username, sizeof(ut.ut_user));
1644 strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line));
1645
1646 time(&t);
1647 ut.ut_time = t; /* ut_time is not always a time_t */
1648 ut.ut_type = LOGIN_PROCESS;
1649 ut.ut_pid = getpid();
1650
1651 /* strncpy because we don't necessarily want nul termination */
1652 strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
1653
1654 if (packet_connection_is_on_socket() &&
1655 getpeername(packet_get_connection_in(),
1656 (struct sockaddr *)&from, &fromlen) == 0) {
1657 ipv64_normalise_mapped(&from, &fromlen);
1658 if (from.ss_family == AF_INET) {
1659 a4 = (struct sockaddr_in *)&from;
1660 memcpy(&ut.ut_addr, &(a4->sin_addr),
1661 MIN_SIZEOF(ut.ut_addr, a4->sin_addr));
1662 }
1663#ifdef HAVE_ADDR_V6_IN_UTMP
1664 if (from.ss_family == AF_INET6) {
1665 a6 = (struct sockaddr_in6 *)&from;
1666 memcpy(&ut.ut_addr_v6, &(a6->sin6_addr),
1667 MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr));
1668 }
1669#endif
1670 }
1671
1672 if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut))
1673 error("Failed to write to %s: %s", _PATH_BTMP,
1674 strerror(errno));
1675
1676out:
1677 close(fd);
1678}
1679#endif /* USE_BTMP */