dropbear 2016.73
[tomato.git] / release / src / router / dropbear / loginrec.c
blob1b8b1432256ee2db46ab8a03145d9cb668c6827f
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
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.
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.
28 /**
29 ** loginrec.c: platform-independent login recording and lastlog retrieval
30 **/
32 /* For now lastlog code has been removed as it wasn't being used by Dropbear. */
35 The new login code explained
36 ============================
38 This code attempts to provide a common interface to login recording
39 (utmp and friends) and last login time retrieval.
41 Its primary means of achieving this is to use 'struct logininfo', a
42 union of all the useful fields in the various different types of
43 system login record structures one finds on UNIX variants.
45 We depend on autoconf to define which recording methods are to be
46 used, and which fields are contained in the relevant data structures
47 on the local system. Many C preprocessor symbols affect which code
48 gets compiled here.
50 The code is designed to make it easy to modify a particular
51 recording method, without affecting other methods nor requiring so
52 many nested conditional compilation blocks as were commonplace in
53 the old code.
55 For login recording, we try to use the local system's libraries as
56 these are clearly most likely to work correctly. For utmp systems
57 this usually means login() and logout() or setutent() etc., probably
58 in libutil, along with logwtmp() etc. On these systems, we fall back
59 to writing the files directly if we have to, though this method
60 requires very thorough testing so we do not corrupt local auditing
61 information. These files and their access methods are very system
62 specific indeed.
64 For utmpx systems, the corresponding library functions are
65 setutxent() etc. To the author's knowledge, all utmpx systems have
66 these library functions and so no direct write is attempted. If such
67 a system exists and needs support, direct analogues of the [uw]tmp
68 code should suffice.
70 Retrieving the time of last login ('lastlog') is in some ways even
71 more problemmatic than login recording. Some systems provide a
72 simple table of all users which we seek based on uid and retrieve a
73 relatively standard structure. Others record the same information in
74 a directory with a separate file, and others don't record the
75 information separately at all. For systems in the latter category,
76 we look backwards in the wtmp or wtmpx file for the last login entry
77 for our user. Naturally this is slower and on busy systems could
78 incur a significant performance penalty.
80 Calling the new code
81 --------------------
83 In OpenSSH all login recording and retrieval is performed in
84 login.c. Here you'll find working examples. Also, in the logintest.c
85 program there are more examples.
87 Internal handler calling method
88 -------------------------------
90 When a call is made to login_login() or login_logout(), both
91 routines set a struct logininfo flag defining which action (log in,
92 or log out) is to be taken. They both then call login_write(), which
93 calls whichever of the many structure-specific handlers autoconf
94 selects for the local system.
96 The handlers themselves handle system data structure specifics. Both
97 struct utmp and struct utmpx have utility functions (see
98 construct_utmp*()) to try to make it simpler to add extra systems
99 that introduce new features to either structure.
101 While it may seem terribly wasteful to replicate so much similar
102 code for each method, experience has shown that maintaining code to
103 write both struct utmp and utmpx in one function, whilst maintaining
104 support for all systems whether they have library support or not, is
105 a difficult and time-consuming task.
107 Lastlog support proceeds similarly. Functions login_get_lastlog()
108 (and its OpenSSH-tuned friend login_get_lastlog_time()) call
109 getlast_entry(), which tries one of three methods to find the last
110 login time. It uses local system lastlog support if it can,
111 otherwise it tries wtmp or wtmpx before giving up and returning 0,
112 meaning "tilt".
114 Maintenance
115 -----------
117 In many cases it's possible to tweak autoconf to select the correct
118 methods for a particular platform, either by improving the detection
119 code (best), or by presetting DISABLE_<method> or CONF_<method>_FILE
120 symbols for the platform.
122 Use logintest to check which symbols are defined before modifying
123 configure.ac and loginrec.c. (You have to build logintest yourself
124 with 'make logintest' as it's not built by default.)
126 Otherwise, patches to the specific method(s) are very helpful!
131 ** TODO:
132 ** homegrown ttyslot()
133 ** test, test, test
135 ** Platform status:
136 ** ----------------
138 ** Known good:
139 ** Linux (Redhat 6.2, Debian)
140 ** Solaris
141 ** HP-UX 10.20 (gcc only)
142 ** IRIX
143 ** NeXT - M68k/HPPA/Sparc (4.2/3.3)
145 ** Testing required: Please send reports!
146 ** NetBSD
147 ** HP-UX 11
148 ** AIX
150 ** Platforms with known problems:
151 ** Some variants of Slackware Linux
156 #include "includes.h"
157 #include "loginrec.h"
158 #include "dbutil.h"
159 #include "atomicio.h"
162 ** prototypes for helper functions in this file
165 #if HAVE_UTMP_H
166 void set_utmp_time(struct logininfo *li, struct utmp *ut);
167 void construct_utmp(struct logininfo *li, struct utmp *ut);
168 #endif
170 #ifdef HAVE_UTMPX_H
171 void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
172 void construct_utmpx(struct logininfo *li, struct utmpx *ut);
173 #endif
175 int utmp_write_entry(struct logininfo *li);
176 int utmpx_write_entry(struct logininfo *li);
177 int wtmp_write_entry(struct logininfo *li);
178 int wtmpx_write_entry(struct logininfo *li);
179 int lastlog_write_entry(struct logininfo *li);
180 int syslogin_write_entry(struct logininfo *li);
182 int wtmp_get_entry(struct logininfo *li);
183 int wtmpx_get_entry(struct logininfo *li);
185 /* pick the shortest string */
186 #define MIN_SIZEOF(s1,s2) ( sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2) )
189 ** platform-independent login functions
192 /* login_login(struct logininfo *) -Record a login
194 * Call with a pointer to a struct logininfo initialised with
195 * login_init_entry() or login_alloc_entry()
197 * Returns:
198 * >0 if successful
199 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
202 login_login (struct logininfo *li)
204 li->type = LTYPE_LOGIN;
205 return login_write(li);
209 /* login_logout(struct logininfo *) - Record a logout
211 * Call as with login_login()
213 * Returns:
214 * >0 if successful
215 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
218 login_logout(struct logininfo *li)
220 li->type = LTYPE_LOGOUT;
221 return login_write(li);
225 /* login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
226 * a logininfo structure
228 * This function creates a new struct logininfo, a data structure
229 * meant to carry the information required to portably record login info.
231 * Returns a pointer to a newly created struct logininfo. If memory
232 * allocation fails, the program halts.
234 struct
235 logininfo *login_alloc_entry(int pid, const char *username,
236 const char *hostname, const char *line)
238 struct logininfo *newli;
240 newli = (struct logininfo *) m_malloc (sizeof(*newli));
241 (void)login_init_entry(newli, pid, username, hostname, line);
242 return newli;
246 /* login_free_entry(struct logininfo *) - free struct memory */
247 void
248 login_free_entry(struct logininfo *li)
250 m_free(li);
254 /* login_init_entry(struct logininfo *, int, char*, char*, char*)
255 * - initialise a struct logininfo
257 * Populates a new struct logininfo, a data structure meant to carry
258 * the information required to portably record login info.
260 * Returns: 1
263 login_init_entry(struct logininfo *li, int pid, const char *username,
264 const char *hostname, const char *line)
266 struct passwd *pw;
268 memset(li, 0, sizeof(*li));
270 li->pid = pid;
272 /* set the line information */
273 if (line)
274 line_fullname(li->line, line, sizeof(li->line));
276 if (username) {
277 strlcpy(li->username, username, sizeof(li->username));
278 pw = getpwnam(li->username);
279 if (pw == NULL)
280 dropbear_exit("login_init_entry: Cannot find user \"%s\"",
281 li->username);
282 li->uid = pw->pw_uid;
285 if (hostname)
286 strlcpy(li->hostname, hostname, sizeof(li->hostname));
288 return 1;
291 /* login_set_current_time(struct logininfo *) - set the current time
293 * Set the current time in a logininfo structure. This function is
294 * meant to eliminate the need to deal with system dependencies for
295 * time handling.
297 void
298 login_set_current_time(struct logininfo *li)
300 struct timeval tv;
302 gettimeofday(&tv, NULL);
304 li->tv_sec = tv.tv_sec;
305 li->tv_usec = tv.tv_usec;
309 ** login_write: Call low-level recording functions based on autoconf
310 ** results
313 login_write (struct logininfo *li)
315 #ifndef HAVE_CYGWIN
316 if ((int)geteuid() != 0) {
317 return 1;
319 #endif
321 /* set the timestamp */
322 login_set_current_time(li);
323 #ifdef USE_LOGIN
324 syslogin_write_entry(li);
325 #endif
326 #ifdef USE_LASTLOG
327 if (li->type == LTYPE_LOGIN) {
328 lastlog_write_entry(li);
330 #endif
331 #ifdef USE_UTMP
332 utmp_write_entry(li);
333 #endif
334 #ifdef USE_WTMP
335 wtmp_write_entry(li);
336 #endif
337 #ifdef USE_UTMPX
338 utmpx_write_entry(li);
339 #endif
340 #ifdef USE_WTMPX
341 wtmpx_write_entry(li);
342 #endif
343 return 0;
346 #ifdef LOGIN_NEEDS_UTMPX
348 login_utmp_only(struct logininfo *li)
350 li->type = LTYPE_LOGIN;
351 login_set_current_time(li);
352 # ifdef USE_UTMP
353 utmp_write_entry(li);
354 # endif
355 # ifdef USE_WTMP
356 wtmp_write_entry(li);
357 # endif
358 # ifdef USE_UTMPX
359 utmpx_write_entry(li);
360 # endif
361 # ifdef USE_WTMPX
362 wtmpx_write_entry(li);
363 # endif
364 return 0;
366 #endif
371 * 'line' string utility functions
373 * These functions process the 'line' string into one of three forms:
375 * 1. The full filename (including '/dev')
376 * 2. The stripped name (excluding '/dev')
377 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
378 * /dev/pts/1 -> ts/1 )
380 * Form 3 is used on some systems to identify a .tmp.? entry when
381 * attempting to remove it. Typically both addition and removal is
382 * performed by one application - say, sshd - so as long as the choice
383 * uniquely identifies a terminal it's ok.
387 /* line_fullname(): add the leading '/dev/' if it doesn't exist make
388 * sure dst has enough space, if not just copy src (ugh) */
389 char *
390 line_fullname(char *dst, const char *src, size_t dstsize)
392 memset(dst, '\0', dstsize);
393 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5))) {
394 strlcpy(dst, src, dstsize);
395 } else {
396 strlcpy(dst, "/dev/", dstsize);
397 strlcat(dst, src, dstsize);
399 return dst;
402 /* line_stripname(): strip the leading '/dev' if it exists, return dst */
403 char *
404 line_stripname(char *dst, const char *src, size_t dstsize)
406 memset(dst, '\0', dstsize);
407 if (strncmp(src, "/dev/", 5) == 0)
408 strlcpy(dst, src + 5, dstsize);
409 else
410 strlcpy(dst, src, dstsize);
411 return dst;
414 /* line_abbrevname(): Return the abbreviated (usually four-character)
415 * form of the line (Just use the last <dstsize> characters of the
416 * full name.)
418 * NOTE: use strncpy because we do NOT necessarily want zero
419 * termination */
420 char *
421 line_abbrevname(char *dst, const char *src, size_t dstsize)
423 size_t len;
425 memset(dst, '\0', dstsize);
427 /* Always skip prefix if present */
428 if (strncmp(src, "/dev/", 5) == 0)
429 src += 5;
431 #ifdef WITH_ABBREV_NO_TTY
432 if (strncmp(src, "tty", 3) == 0)
433 src += 3;
434 #endif
436 len = strlen(src);
438 if (len > 0) {
439 if (((int)len - dstsize) > 0)
440 src += ((int)len - dstsize);
442 /* note: _don't_ change this to strlcpy */
443 strncpy(dst, src, (size_t)dstsize);
446 return dst;
450 ** utmp utility functions
452 ** These functions manipulate struct utmp, taking system differences
453 ** into account.
456 #if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
458 /* build the utmp structure */
459 void
460 set_utmp_time(struct logininfo *li, struct utmp *ut)
462 # ifdef HAVE_STRUCT_UTMP_UT_TV
463 ut->ut_tv.tv_sec = li->tv_sec;
464 ut->ut_tv.tv_usec = li->tv_usec;
465 # else
466 # ifdef HAVE_STRUCT_UTMP_UT_TIME
467 ut->ut_time = li->tv_sec;
468 # endif
469 # endif
472 void
473 construct_utmp(struct logininfo *li,
474 struct utmp *ut)
476 # ifdef HAVE_ADDR_V6_IN_UTMP
477 struct sockaddr_in6 *sa6;
478 # endif
479 memset(ut, '\0', sizeof(*ut));
481 /* First fill out fields used for both logins and logouts */
483 # ifdef HAVE_STRUCT_UTMP_UT_ID
484 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
485 # endif
487 # ifdef HAVE_STRUCT_UTMP_UT_TYPE
488 /* This is done here to keep utmp constants out of struct logininfo */
489 switch (li->type) {
490 case LTYPE_LOGIN:
491 ut->ut_type = USER_PROCESS;
492 #ifdef _UNICOS
493 cray_set_tmpdir(ut);
494 #endif
495 break;
496 case LTYPE_LOGOUT:
497 ut->ut_type = DEAD_PROCESS;
498 #ifdef _UNICOS
499 cray_retain_utmp(ut, li->pid);
500 #endif
501 break;
503 # endif
504 set_utmp_time(li, ut);
506 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
508 # ifdef HAVE_STRUCT_UTMP_UT_PID
509 ut->ut_pid = li->pid;
510 # endif
512 /* If we're logging out, leave all other fields blank */
513 if (li->type == LTYPE_LOGOUT)
514 return;
517 * These fields are only used when logging in, and are blank
518 * for logouts.
521 /* Use strncpy because we don't necessarily want null termination */
522 strncpy(ut->ut_name, li->username, MIN_SIZEOF(ut->ut_name, li->username));
523 # ifdef HAVE_STRUCT_UTMP_UT_HOST
524 strncpy(ut->ut_host, li->hostname, MIN_SIZEOF(ut->ut_host, li->hostname));
525 # endif
526 # ifdef HAVE_STRUCT_UTMP_UT_ADDR
527 /* this is just a 32-bit IP address */
528 if (li->hostaddr.sa.sa_family == AF_INET)
529 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
530 # endif
531 # ifdef HAVE_ADDR_V6_IN_UTMP
532 /* this is just a 128-bit IPv6 address */
533 if (li->hostaddr.sa.sa_family == AF_INET6) {
534 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
535 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
536 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
537 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
538 ut->ut_addr_v6[1] = 0;
539 ut->ut_addr_v6[2] = 0;
540 ut->ut_addr_v6[3] = 0;
543 # endif
545 #endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
548 ** utmpx utility functions
550 ** These functions manipulate struct utmpx, accounting for system
551 ** variations.
554 #if defined(USE_UTMPX) || defined (USE_WTMPX)
555 /* build the utmpx structure */
556 void
557 set_utmpx_time(struct logininfo *li, struct utmpx *utx)
559 # ifdef HAVE_STRUCT_UTMPX_UT_TV
560 utx->ut_tv.tv_sec = li->tv_sec;
561 utx->ut_tv.tv_usec = li->tv_usec;
562 # else /* HAVE_STRUCT_UTMPX_UT_TV */
563 # ifdef HAVE_STRUCT_UTMPX_UT_TIME
564 utx->ut_time = li->tv_sec;
565 # endif /* HAVE_STRUCT_UTMPX_UT_TIME */
566 # endif /* HAVE_STRUCT_UTMPX_UT_TV */
569 void
570 construct_utmpx(struct logininfo *li, struct utmpx *utx)
572 # ifdef HAVE_ADDR_V6_IN_UTMP
573 struct sockaddr_in6 *sa6;
574 # endif
575 memset(utx, '\0', sizeof(*utx));
576 # ifdef HAVE_STRUCT_UTMPX_UT_ID
577 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
578 # endif
580 /* this is done here to keep utmp constants out of loginrec.h */
581 switch (li->type) {
582 case LTYPE_LOGIN:
583 utx->ut_type = USER_PROCESS;
584 break;
585 case LTYPE_LOGOUT:
586 utx->ut_type = DEAD_PROCESS;
587 break;
589 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
590 set_utmpx_time(li, utx);
591 utx->ut_pid = li->pid;
592 /* strncpy(): Don't necessarily want null termination */
593 strncpy(utx->ut_name, li->username, MIN_SIZEOF(utx->ut_name, li->username));
595 if (li->type == LTYPE_LOGOUT)
596 return;
599 * These fields are only used when logging in, and are blank
600 * for logouts.
603 # ifdef HAVE_STRUCT_UTMPX_UT_HOST
604 strncpy(utx->ut_host, li->hostname, MIN_SIZEOF(utx->ut_host, li->hostname));
605 # endif
606 # ifdef HAVE_STRUCT_UTMPX_UT_ADDR
607 /* this is just a 32-bit IP address */
608 if (li->hostaddr.sa.sa_family == AF_INET)
609 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
610 # endif
611 # ifdef HAVE_ADDR_V6_IN_UTMP
612 /* this is just a 128-bit IPv6 address */
613 if (li->hostaddr.sa.sa_family == AF_INET6) {
614 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
615 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
616 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
617 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
618 ut->ut_addr_v6[1] = 0;
619 ut->ut_addr_v6[2] = 0;
620 ut->ut_addr_v6[3] = 0;
623 # endif
624 # ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN
625 /* ut_syslen is the length of the utx_host string */
626 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
627 # endif
629 #endif /* USE_UTMPX || USE_WTMPX */
632 ** Low-level utmp functions
635 /* FIXME: (ATL) utmp_write_direct needs testing */
636 #ifdef USE_UTMP
638 /* if we can, use pututline() etc. */
639 # if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
640 defined(HAVE_PUTUTLINE)
641 # define UTMP_USE_LIBRARY
642 # endif
645 /* write a utmp entry with the system's help (pututline() and pals) */
646 # ifdef UTMP_USE_LIBRARY
647 static int
648 utmp_write_library(struct logininfo *li, struct utmp *ut)
650 setutent();
651 pututline(ut);
653 # ifdef HAVE_ENDUTENT
654 endutent();
655 # endif
656 return 1;
658 # else /* UTMP_USE_LIBRARY */
660 /* write a utmp entry direct to the file */
661 /* This is a slightly modification of code in OpenBSD's login.c */
662 static int
663 utmp_write_direct(struct logininfo *li, struct utmp *ut)
665 struct utmp old_ut;
666 register int fd;
667 int tty;
669 /* FIXME: (ATL) ttyslot() needs local implementation */
671 #if defined(HAVE_GETTTYENT)
672 register struct ttyent *ty;
674 tty=0;
676 setttyent();
677 while ((struct ttyent *)0 != (ty = getttyent())) {
678 tty++;
679 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
680 break;
682 endttyent();
684 if((struct ttyent *)0 == ty) {
685 dropbear_log(LOG_WARNING, "utmp_write_entry: tty not found");
686 return(1);
688 #else /* FIXME */
690 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
692 #endif /* HAVE_GETTTYENT */
694 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
695 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
697 * Prevent luser from zero'ing out ut_host.
698 * If the new ut_line is empty but the old one is not
699 * and ut_line and ut_name match, preserve the old ut_line.
701 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
702 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
703 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
704 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0)) {
705 (void)memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
708 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
709 if (atomicio(write, fd, ut, sizeof(*ut)) != sizeof(*ut))
710 dropbear_log(LOG_WARNING, "utmp_write_direct: error writing %s: %s",
711 UTMP_FILE, strerror(errno));
713 (void)close(fd);
714 return 1;
715 } else {
716 return 0;
719 # endif /* UTMP_USE_LIBRARY */
721 static int
722 utmp_perform_login(struct logininfo *li)
724 struct utmp ut;
726 construct_utmp(li, &ut);
727 # ifdef UTMP_USE_LIBRARY
728 if (!utmp_write_library(li, &ut)) {
729 dropbear_log(LOG_WARNING, "utmp_perform_login: utmp_write_library() failed");
730 return 0;
732 # else
733 if (!utmp_write_direct(li, &ut)) {
734 dropbear_log(LOG_WARNING, "utmp_perform_login: utmp_write_direct() failed");
735 return 0;
737 # endif
738 return 1;
742 static int
743 utmp_perform_logout(struct logininfo *li)
745 struct utmp ut;
747 construct_utmp(li, &ut);
748 # ifdef UTMP_USE_LIBRARY
749 if (!utmp_write_library(li, &ut)) {
750 dropbear_log(LOG_WARNING, "utmp_perform_logout: utmp_write_library() failed");
751 return 0;
753 # else
754 if (!utmp_write_direct(li, &ut)) {
755 dropbear_log(LOG_WARNING, "utmp_perform_logout: utmp_write_direct() failed");
756 return 0;
758 # endif
759 return 1;
764 utmp_write_entry(struct logininfo *li)
766 switch(li->type) {
767 case LTYPE_LOGIN:
768 return utmp_perform_login(li);
770 case LTYPE_LOGOUT:
771 return utmp_perform_logout(li);
773 default:
774 dropbear_log(LOG_WARNING, "utmp_write_entry: invalid type field");
775 return 0;
778 #endif /* USE_UTMP */
782 ** Low-level utmpx functions
785 /* not much point if we don't want utmpx entries */
786 #ifdef USE_UTMPX
788 /* if we have the wherewithall, use pututxline etc. */
789 # if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
790 defined(HAVE_PUTUTXLINE)
791 # define UTMPX_USE_LIBRARY
792 # endif
795 /* write a utmpx entry with the system's help (pututxline() and pals) */
796 # ifdef UTMPX_USE_LIBRARY
797 static int
798 utmpx_write_library(struct logininfo *li, struct utmpx *utx)
800 setutxent();
801 pututxline(utx);
803 # ifdef HAVE_ENDUTXENT
804 endutxent();
805 # endif
806 return 1;
809 # else /* UTMPX_USE_LIBRARY */
811 /* write a utmp entry direct to the file */
812 static int
813 utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
815 dropbear_log(LOG_WARNING, "utmpx_write_direct: not implemented!");
816 return 0;
818 # endif /* UTMPX_USE_LIBRARY */
820 static int
821 utmpx_perform_login(struct logininfo *li)
823 struct utmpx utx;
825 construct_utmpx(li, &utx);
826 # ifdef UTMPX_USE_LIBRARY
827 if (!utmpx_write_library(li, &utx)) {
828 dropbear_log(LOG_WARNING, "utmpx_perform_login: utmp_write_library() failed");
829 return 0;
831 # else
832 if (!utmpx_write_direct(li, &ut)) {
833 dropbear_log(LOG_WARNING, "utmpx_perform_login: utmp_write_direct() failed");
834 return 0;
836 # endif
837 return 1;
841 static int
842 utmpx_perform_logout(struct logininfo *li)
844 struct utmpx utx;
846 construct_utmpx(li, &utx);
847 # ifdef HAVE_STRUCT_UTMPX_UT_ID
848 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
849 # endif
850 # ifdef HAVE_STRUCT_UTMPX_UT_TYPE
851 utx.ut_type = DEAD_PROCESS;
852 # endif
854 # ifdef UTMPX_USE_LIBRARY
855 utmpx_write_library(li, &utx);
856 # else
857 utmpx_write_direct(li, &utx);
858 # endif
859 return 1;
863 utmpx_write_entry(struct logininfo *li)
865 switch(li->type) {
866 case LTYPE_LOGIN:
867 return utmpx_perform_login(li);
868 case LTYPE_LOGOUT:
869 return utmpx_perform_logout(li);
870 default:
871 dropbear_log(LOG_WARNING, "utmpx_write_entry: invalid type field");
872 return 0;
875 #endif /* USE_UTMPX */
879 ** Low-level wtmp functions
882 #ifdef USE_WTMP
884 /* write a wtmp entry direct to the end of the file */
885 /* This is a slight modification of code in OpenBSD's logwtmp.c */
886 static int
887 wtmp_write(struct logininfo *li, struct utmp *ut)
889 struct stat buf;
890 int fd, ret = 1;
892 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
893 dropbear_log(LOG_WARNING, "wtmp_write: problem writing %s: %s",
894 WTMP_FILE, strerror(errno));
895 return 0;
897 if (fstat(fd, &buf) == 0)
898 if (atomicio(write, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
899 ftruncate(fd, buf.st_size);
900 dropbear_log(LOG_WARNING, "wtmp_write: problem writing %s: %s",
901 WTMP_FILE, strerror(errno));
902 ret = 0;
904 (void)close(fd);
905 return ret;
908 static int
909 wtmp_perform_login(struct logininfo *li)
911 struct utmp ut;
913 construct_utmp(li, &ut);
914 return wtmp_write(li, &ut);
918 static int
919 wtmp_perform_logout(struct logininfo *li)
921 struct utmp ut;
923 construct_utmp(li, &ut);
924 return wtmp_write(li, &ut);
929 wtmp_write_entry(struct logininfo *li)
931 switch(li->type) {
932 case LTYPE_LOGIN:
933 return wtmp_perform_login(li);
934 case LTYPE_LOGOUT:
935 return wtmp_perform_logout(li);
936 default:
937 dropbear_log(LOG_WARNING, "wtmp_write_entry: invalid type field");
938 return 0;
943 /* Notes on fetching login data from wtmp/wtmpx
945 * Logouts are usually recorded with (amongst other things) a blank
946 * username on a given tty line. However, some systems (HP-UX is one)
947 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
949 * Since we're only looking for logins here, we know that the username
950 * must be set correctly. On systems that leave it in, we check for
951 * ut_type==USER_PROCESS (indicating a login.)
953 * Portability: Some systems may set something other than USER_PROCESS
954 * to indicate a login process. I don't know of any as I write. Also,
955 * it's possible that some systems may both leave the username in
956 * place and not have ut_type.
959 /* return true if this wtmp entry indicates a login */
960 static int
961 wtmp_islogin(struct logininfo *li, struct utmp *ut)
963 if (strncmp(li->username, ut->ut_name,
964 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
965 # ifdef HAVE_STRUCT_UTMP_UT_TYPE
966 if (ut->ut_type & USER_PROCESS)
967 return 1;
968 # else
969 return 1;
970 # endif
972 return 0;
976 wtmp_get_entry(struct logininfo *li)
978 struct stat st;
979 struct utmp ut;
980 int fd, found=0;
982 /* Clear the time entries in our logininfo */
983 li->tv_sec = li->tv_usec = 0;
985 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
986 dropbear_log(LOG_WARNING, "wtmp_get_entry: problem opening %s: %s",
987 WTMP_FILE, strerror(errno));
988 return 0;
990 if (fstat(fd, &st) != 0) {
991 dropbear_log(LOG_WARNING, "wtmp_get_entry: couldn't stat %s: %s",
992 WTMP_FILE, strerror(errno));
993 close(fd);
994 return 0;
997 /* Seek to the start of the last struct utmp */
998 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
999 /* Looks like we've got a fresh wtmp file */
1000 close(fd);
1001 return 0;
1004 while (!found) {
1005 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
1006 dropbear_log(LOG_WARNING, "wtmp_get_entry: read of %s failed: %s",
1007 WTMP_FILE, strerror(errno));
1008 close (fd);
1009 return 0;
1011 if ( wtmp_islogin(li, &ut) ) {
1012 found = 1;
1013 /* We've already checked for a time in struct
1014 * utmp, in login_getlast(). */
1015 # ifdef HAVE_STRUCT_UTMP_UT_TIME
1016 li->tv_sec = ut.ut_time;
1017 # else
1018 # if HAVE_STRUCT_UTMP_UT_TV
1019 li->tv_sec = ut.ut_tv.tv_sec;
1020 # endif
1021 # endif
1022 line_fullname(li->line, ut.ut_line,
1023 MIN_SIZEOF(li->line, ut.ut_line));
1024 # ifdef HAVE_STRUCT_UTMP_UT_HOST
1025 strlcpy(li->hostname, ut.ut_host,
1026 MIN_SIZEOF(li->hostname, ut.ut_host));
1027 # endif
1028 continue;
1030 /* Seek back 2 x struct utmp */
1031 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
1032 /* We've found the start of the file, so quit */
1033 close (fd);
1034 return 0;
1038 /* We found an entry. Tidy up and return */
1039 close(fd);
1040 return 1;
1042 # endif /* USE_WTMP */
1046 ** Low-level wtmpx functions
1049 #ifdef USE_WTMPX
1050 /* write a wtmpx entry direct to the end of the file */
1051 /* This is a slight modification of code in OpenBSD's logwtmp.c */
1052 static int
1053 wtmpx_write(struct logininfo *li, struct utmpx *utx)
1055 struct stat buf;
1056 int fd, ret = 1;
1058 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
1059 dropbear_log(LOG_WARNING, "wtmpx_write: problem opening %s: %s",
1060 WTMPX_FILE, strerror(errno));
1061 return 0;
1064 if (fstat(fd, &buf) == 0)
1065 if (atomicio(write, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
1066 ftruncate(fd, buf.st_size);
1067 dropbear_log(LOG_WARNING, "wtmpx_write: problem writing %s: %s",
1068 WTMPX_FILE, strerror(errno));
1069 ret = 0;
1071 (void)close(fd);
1073 return ret;
1077 static int
1078 wtmpx_perform_login(struct logininfo *li)
1080 struct utmpx utx;
1082 construct_utmpx(li, &utx);
1083 return wtmpx_write(li, &utx);
1087 static int
1088 wtmpx_perform_logout(struct logininfo *li)
1090 struct utmpx utx;
1092 construct_utmpx(li, &utx);
1093 return wtmpx_write(li, &utx);
1098 wtmpx_write_entry(struct logininfo *li)
1100 switch(li->type) {
1101 case LTYPE_LOGIN:
1102 return wtmpx_perform_login(li);
1103 case LTYPE_LOGOUT:
1104 return wtmpx_perform_logout(li);
1105 default:
1106 dropbear_log(LOG_WARNING, "wtmpx_write_entry: invalid type field");
1107 return 0;
1111 /* Please see the notes above wtmp_islogin() for information about the
1112 next two functions */
1114 /* Return true if this wtmpx entry indicates a login */
1115 static int
1116 wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1118 if ( strncmp(li->username, utx->ut_name,
1119 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
1120 # ifdef HAVE_STRUCT_UTMPX_UT_TYPE
1121 if (utx->ut_type == USER_PROCESS)
1122 return 1;
1123 # else
1124 return 1;
1125 # endif
1127 return 0;
1132 wtmpx_get_entry(struct logininfo *li)
1134 struct stat st;
1135 struct utmpx utx;
1136 int fd, found=0;
1138 /* Clear the time entries */
1139 li->tv_sec = li->tv_usec = 0;
1141 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
1142 dropbear_log(LOG_WARNING, "wtmpx_get_entry: problem opening %s: %s",
1143 WTMPX_FILE, strerror(errno));
1144 return 0;
1146 if (fstat(fd, &st) != 0) {
1147 dropbear_log(LOG_WARNING, "wtmpx_get_entry: couldn't stat %s: %s",
1148 WTMPX_FILE, strerror(errno));
1149 close(fd);
1150 return 0;
1153 /* Seek to the start of the last struct utmpx */
1154 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
1155 /* probably a newly rotated wtmpx file */
1156 close(fd);
1157 return 0;
1160 while (!found) {
1161 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
1162 dropbear_log(LOG_WARNING, "wtmpx_get_entry: read of %s failed: %s",
1163 WTMPX_FILE, strerror(errno));
1164 close (fd);
1165 return 0;
1167 /* Logouts are recorded as a blank username on a particular line.
1168 * So, we just need to find the username in struct utmpx */
1169 if ( wtmpx_islogin(li, &utx) ) {
1170 found = 1;
1171 # ifdef HAVE_STRUCT_UTMPX_UT_TV
1172 li->tv_sec = utx.ut_tv.tv_sec;
1173 # else
1174 # ifdef HAVE_STRUCT_UTMPX_UT_TIME
1175 li->tv_sec = utx.ut_time;
1176 # endif
1177 # endif
1178 line_fullname(li->line, utx.ut_line, sizeof(li->line));
1179 # ifdef HAVE_STRUCT_UTMPX_UT_HOST
1180 strlcpy(li->hostname, utx.ut_host,
1181 MIN_SIZEOF(li->hostname, utx.ut_host));
1182 # endif
1183 continue;
1185 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
1186 close (fd);
1187 return 0;
1191 close(fd);
1192 return 1;
1194 #endif /* USE_WTMPX */
1197 ** Low-level libutil login() functions
1200 #ifdef USE_LOGIN
1201 static int
1202 syslogin_perform_login(struct logininfo *li)
1204 struct utmp *ut;
1206 if (! (ut = (struct utmp *)malloc(sizeof(*ut)))) {
1207 dropbear_log(LOG_WARNING, "syslogin_perform_login: couldn't malloc()");
1208 return 0;
1210 construct_utmp(li, ut);
1211 login(ut);
1212 free(ut);
1214 return 1;
1217 static int
1218 syslogin_perform_logout(struct logininfo *li)
1220 # ifdef HAVE_LOGOUT
1221 char line[8];
1223 (void)line_stripname(line, li->line, sizeof(line));
1225 if (!logout(line)) {
1226 dropbear_log(LOG_WARNING, "syslogin_perform_logout: logout(%s) returned an error: %s", line, strerror(errno));
1227 # ifdef HAVE_LOGWTMP
1228 } else {
1229 logwtmp(line, "", "");
1230 # endif
1232 /* FIXME: (ATL - if the need arises) What to do if we have
1233 * login, but no logout? what if logout but no logwtmp? All
1234 * routines are in libutil so they should all be there,
1235 * but... */
1236 # endif
1237 return 1;
1241 syslogin_write_entry(struct logininfo *li)
1243 switch (li->type) {
1244 case LTYPE_LOGIN:
1245 return syslogin_perform_login(li);
1246 case LTYPE_LOGOUT:
1247 return syslogin_perform_logout(li);
1248 default:
1249 dropbear_log(LOG_WARNING, "syslogin_write_entry: Invalid type field");
1250 return 0;
1253 #endif /* USE_LOGIN */
1255 /* end of file log-syslogin.c */
1258 ** Low-level lastlog functions
1261 #ifdef USE_LASTLOG
1262 #define LL_FILE 1
1263 #define LL_DIR 2
1264 #define LL_OTHER 3
1266 static void
1267 lastlog_construct(struct logininfo *li, struct lastlog *last)
1269 /* clear the structure */
1270 memset(last, '\0', sizeof(*last));
1272 (void)line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
1273 strlcpy(last->ll_host, li->hostname,
1274 MIN_SIZEOF(last->ll_host, li->hostname));
1275 last->ll_time = li->tv_sec;
1278 static int
1279 lastlog_filetype(char *filename)
1281 struct stat st;
1283 if (stat(filename, &st) != 0) {
1284 dropbear_log(LOG_WARNING, "lastlog_perform_login: Couldn't stat %s: %s", filename,
1285 strerror(errno));
1286 return 0;
1288 if (S_ISDIR(st.st_mode))
1289 return LL_DIR;
1290 else if (S_ISREG(st.st_mode))
1291 return LL_FILE;
1292 else
1293 return LL_OTHER;
1297 /* open the file (using filemode) and seek to the login entry */
1298 static int
1299 lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1301 off_t offset;
1302 int type;
1303 char lastlog_file[1024];
1305 type = lastlog_filetype(LASTLOG_FILE);
1306 switch (type) {
1307 case LL_FILE:
1308 strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file));
1309 break;
1310 case LL_DIR:
1311 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1312 LASTLOG_FILE, li->username);
1313 break;
1314 default:
1315 dropbear_log(LOG_WARNING, "lastlog_openseek: %.100s is not a file or directory!",
1316 LASTLOG_FILE);
1317 return 0;
1320 *fd = open(lastlog_file, filemode, 0600);
1321 if ( *fd < 0) {
1322 dropbear_log(LOG_INFO, "lastlog_openseek: Couldn't open %s: %s",
1323 lastlog_file, strerror(errno));
1324 return 0;
1327 if (type == LL_FILE) {
1328 /* find this uid's offset in the lastlog file */
1329 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
1331 if ( lseek(*fd, offset, SEEK_SET) != offset ) {
1332 dropbear_log(LOG_WARNING, "lastlog_openseek: %s->lseek(): %s",
1333 lastlog_file, strerror(errno));
1334 return 0;
1338 return 1;
1341 static int
1342 lastlog_perform_login(struct logininfo *li)
1344 struct lastlog last;
1345 int fd;
1347 /* create our struct lastlog */
1348 lastlog_construct(li, &last);
1350 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
1351 return(0);
1353 /* write the entry */
1354 if (atomicio(write, fd, &last, sizeof(last)) != sizeof(last)) {
1355 close(fd);
1356 dropbear_log(LOG_WARNING, "lastlog_write_filemode: Error writing to %s: %s",
1357 LASTLOG_FILE, strerror(errno));
1358 return 0;
1361 close(fd);
1362 return 1;
1366 lastlog_write_entry(struct logininfo *li)
1368 switch(li->type) {
1369 case LTYPE_LOGIN:
1370 return lastlog_perform_login(li);
1371 default:
1372 dropbear_log(LOG_WARNING, "lastlog_write_entry: Invalid type field");
1373 return 0;
1377 #endif /* USE_LASTLOG */