dropbear: update to 2015.67
[tomato.git] / release / src / router / dropbear / loginrec.c
blobd6ec75fea0319280251525adfc6b3f8fcf7d32be
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;
308 /* copy a sockaddr_* into our logininfo */
309 void
310 login_set_addr(struct logininfo *li, const struct sockaddr *sa,
311 const unsigned int sa_size)
313 unsigned int bufsize = sa_size;
315 /* make sure we don't overrun our union */
316 if (sizeof(li->hostaddr) < sa_size)
317 bufsize = sizeof(li->hostaddr);
319 memcpy((void *)&(li->hostaddr.sa), (const void *)sa, bufsize);
324 ** login_write: Call low-level recording functions based on autoconf
325 ** results
328 login_write (struct logininfo *li)
330 #ifndef HAVE_CYGWIN
331 if ((int)geteuid() != 0) {
332 return 1;
334 #endif
336 /* set the timestamp */
337 login_set_current_time(li);
338 #ifdef USE_LOGIN
339 syslogin_write_entry(li);
340 #endif
341 #ifdef USE_LASTLOG
342 if (li->type == LTYPE_LOGIN) {
343 lastlog_write_entry(li);
345 #endif
346 #ifdef USE_UTMP
347 utmp_write_entry(li);
348 #endif
349 #ifdef USE_WTMP
350 wtmp_write_entry(li);
351 #endif
352 #ifdef USE_UTMPX
353 utmpx_write_entry(li);
354 #endif
355 #ifdef USE_WTMPX
356 wtmpx_write_entry(li);
357 #endif
358 return 0;
361 #ifdef LOGIN_NEEDS_UTMPX
363 login_utmp_only(struct logininfo *li)
365 li->type = LTYPE_LOGIN;
366 login_set_current_time(li);
367 # ifdef USE_UTMP
368 utmp_write_entry(li);
369 # endif
370 # ifdef USE_WTMP
371 wtmp_write_entry(li);
372 # endif
373 # ifdef USE_UTMPX
374 utmpx_write_entry(li);
375 # endif
376 # ifdef USE_WTMPX
377 wtmpx_write_entry(li);
378 # endif
379 return 0;
381 #endif
386 * 'line' string utility functions
388 * These functions process the 'line' string into one of three forms:
390 * 1. The full filename (including '/dev')
391 * 2. The stripped name (excluding '/dev')
392 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
393 * /dev/pts/1 -> ts/1 )
395 * Form 3 is used on some systems to identify a .tmp.? entry when
396 * attempting to remove it. Typically both addition and removal is
397 * performed by one application - say, sshd - so as long as the choice
398 * uniquely identifies a terminal it's ok.
402 /* line_fullname(): add the leading '/dev/' if it doesn't exist make
403 * sure dst has enough space, if not just copy src (ugh) */
404 char *
405 line_fullname(char *dst, const char *src, size_t dstsize)
407 memset(dst, '\0', dstsize);
408 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5))) {
409 strlcpy(dst, src, dstsize);
410 } else {
411 strlcpy(dst, "/dev/", dstsize);
412 strlcat(dst, src, dstsize);
414 return dst;
417 /* line_stripname(): strip the leading '/dev' if it exists, return dst */
418 char *
419 line_stripname(char *dst, const char *src, size_t dstsize)
421 memset(dst, '\0', dstsize);
422 if (strncmp(src, "/dev/", 5) == 0)
423 strlcpy(dst, src + 5, dstsize);
424 else
425 strlcpy(dst, src, dstsize);
426 return dst;
429 /* line_abbrevname(): Return the abbreviated (usually four-character)
430 * form of the line (Just use the last <dstsize> characters of the
431 * full name.)
433 * NOTE: use strncpy because we do NOT necessarily want zero
434 * termination */
435 char *
436 line_abbrevname(char *dst, const char *src, size_t dstsize)
438 size_t len;
440 memset(dst, '\0', dstsize);
442 /* Always skip prefix if present */
443 if (strncmp(src, "/dev/", 5) == 0)
444 src += 5;
446 #ifdef WITH_ABBREV_NO_TTY
447 if (strncmp(src, "tty", 3) == 0)
448 src += 3;
449 #endif
451 len = strlen(src);
453 if (len > 0) {
454 if (((int)len - dstsize) > 0)
455 src += ((int)len - dstsize);
457 /* note: _don't_ change this to strlcpy */
458 strncpy(dst, src, (size_t)dstsize);
461 return dst;
465 ** utmp utility functions
467 ** These functions manipulate struct utmp, taking system differences
468 ** into account.
471 #if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
473 /* build the utmp structure */
474 void
475 set_utmp_time(struct logininfo *li, struct utmp *ut)
477 # ifdef HAVE_STRUCT_UTMP_UT_TV
478 ut->ut_tv.tv_sec = li->tv_sec;
479 ut->ut_tv.tv_usec = li->tv_usec;
480 # else
481 # ifdef HAVE_STRUCT_UTMP_UT_TIME
482 ut->ut_time = li->tv_sec;
483 # endif
484 # endif
487 void
488 construct_utmp(struct logininfo *li,
489 struct utmp *ut)
491 # ifdef HAVE_ADDR_V6_IN_UTMP
492 struct sockaddr_in6 *sa6;
493 # endif
494 memset(ut, '\0', sizeof(*ut));
496 /* First fill out fields used for both logins and logouts */
498 # ifdef HAVE_STRUCT_UTMP_UT_ID
499 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
500 # endif
502 # ifdef HAVE_STRUCT_UTMP_UT_TYPE
503 /* This is done here to keep utmp constants out of struct logininfo */
504 switch (li->type) {
505 case LTYPE_LOGIN:
506 ut->ut_type = USER_PROCESS;
507 #ifdef _UNICOS
508 cray_set_tmpdir(ut);
509 #endif
510 break;
511 case LTYPE_LOGOUT:
512 ut->ut_type = DEAD_PROCESS;
513 #ifdef _UNICOS
514 cray_retain_utmp(ut, li->pid);
515 #endif
516 break;
518 # endif
519 set_utmp_time(li, ut);
521 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
523 # ifdef HAVE_STRUCT_UTMP_UT_PID
524 ut->ut_pid = li->pid;
525 # endif
527 /* If we're logging out, leave all other fields blank */
528 if (li->type == LTYPE_LOGOUT)
529 return;
532 * These fields are only used when logging in, and are blank
533 * for logouts.
536 /* Use strncpy because we don't necessarily want null termination */
537 strncpy(ut->ut_name, li->username, MIN_SIZEOF(ut->ut_name, li->username));
538 # ifdef HAVE_STRUCT_UTMP_UT_HOST
539 strncpy(ut->ut_host, li->hostname, MIN_SIZEOF(ut->ut_host, li->hostname));
540 # endif
541 # ifdef HAVE_STRUCT_UTMP_UT_ADDR
542 /* this is just a 32-bit IP address */
543 if (li->hostaddr.sa.sa_family == AF_INET)
544 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
545 # endif
546 # ifdef HAVE_ADDR_V6_IN_UTMP
547 /* this is just a 128-bit IPv6 address */
548 if (li->hostaddr.sa.sa_family == AF_INET6) {
549 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
550 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
551 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
552 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
553 ut->ut_addr_v6[1] = 0;
554 ut->ut_addr_v6[2] = 0;
555 ut->ut_addr_v6[3] = 0;
558 # endif
560 #endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
563 ** utmpx utility functions
565 ** These functions manipulate struct utmpx, accounting for system
566 ** variations.
569 #if defined(USE_UTMPX) || defined (USE_WTMPX)
570 /* build the utmpx structure */
571 void
572 set_utmpx_time(struct logininfo *li, struct utmpx *utx)
574 # ifdef HAVE_STRUCT_UTMPX_UT_TV
575 utx->ut_tv.tv_sec = li->tv_sec;
576 utx->ut_tv.tv_usec = li->tv_usec;
577 # else /* HAVE_STRUCT_UTMPX_UT_TV */
578 # ifdef HAVE_STRUCT_UTMPX_UT_TIME
579 utx->ut_time = li->tv_sec;
580 # endif /* HAVE_STRUCT_UTMPX_UT_TIME */
581 # endif /* HAVE_STRUCT_UTMPX_UT_TV */
584 void
585 construct_utmpx(struct logininfo *li, struct utmpx *utx)
587 # ifdef HAVE_ADDR_V6_IN_UTMP
588 struct sockaddr_in6 *sa6;
589 # endif
590 memset(utx, '\0', sizeof(*utx));
591 # ifdef HAVE_STRUCT_UTMPX_UT_ID
592 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
593 # endif
595 /* this is done here to keep utmp constants out of loginrec.h */
596 switch (li->type) {
597 case LTYPE_LOGIN:
598 utx->ut_type = USER_PROCESS;
599 break;
600 case LTYPE_LOGOUT:
601 utx->ut_type = DEAD_PROCESS;
602 break;
604 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
605 set_utmpx_time(li, utx);
606 utx->ut_pid = li->pid;
607 /* strncpy(): Don't necessarily want null termination */
608 strncpy(utx->ut_name, li->username, MIN_SIZEOF(utx->ut_name, li->username));
610 if (li->type == LTYPE_LOGOUT)
611 return;
614 * These fields are only used when logging in, and are blank
615 * for logouts.
618 # ifdef HAVE_STRUCT_UTMPX_UT_HOST
619 strncpy(utx->ut_host, li->hostname, MIN_SIZEOF(utx->ut_host, li->hostname));
620 # endif
621 # ifdef HAVE_STRUCT_UTMPX_UT_ADDR
622 /* this is just a 32-bit IP address */
623 if (li->hostaddr.sa.sa_family == AF_INET)
624 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
625 # endif
626 # ifdef HAVE_ADDR_V6_IN_UTMP
627 /* this is just a 128-bit IPv6 address */
628 if (li->hostaddr.sa.sa_family == AF_INET6) {
629 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
630 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
631 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
632 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
633 ut->ut_addr_v6[1] = 0;
634 ut->ut_addr_v6[2] = 0;
635 ut->ut_addr_v6[3] = 0;
638 # endif
639 # ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN
640 /* ut_syslen is the length of the utx_host string */
641 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
642 # endif
644 #endif /* USE_UTMPX || USE_WTMPX */
647 ** Low-level utmp functions
650 /* FIXME: (ATL) utmp_write_direct needs testing */
651 #ifdef USE_UTMP
653 /* if we can, use pututline() etc. */
654 # if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
655 defined(HAVE_PUTUTLINE)
656 # define UTMP_USE_LIBRARY
657 # endif
660 /* write a utmp entry with the system's help (pututline() and pals) */
661 # ifdef UTMP_USE_LIBRARY
662 static int
663 utmp_write_library(struct logininfo *li, struct utmp *ut)
665 setutent();
666 pututline(ut);
668 # ifdef HAVE_ENDUTENT
669 endutent();
670 # endif
671 return 1;
673 # else /* UTMP_USE_LIBRARY */
675 /* write a utmp entry direct to the file */
676 /* This is a slightly modification of code in OpenBSD's login.c */
677 static int
678 utmp_write_direct(struct logininfo *li, struct utmp *ut)
680 struct utmp old_ut;
681 register int fd;
682 int tty;
684 /* FIXME: (ATL) ttyslot() needs local implementation */
686 #if defined(HAVE_GETTTYENT)
687 register struct ttyent *ty;
689 tty=0;
691 setttyent();
692 while ((struct ttyent *)0 != (ty = getttyent())) {
693 tty++;
694 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
695 break;
697 endttyent();
699 if((struct ttyent *)0 == ty) {
700 dropbear_log(LOG_WARNING, "utmp_write_entry: tty not found");
701 return(1);
703 #else /* FIXME */
705 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
707 #endif /* HAVE_GETTTYENT */
709 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
710 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
712 * Prevent luser from zero'ing out ut_host.
713 * If the new ut_line is empty but the old one is not
714 * and ut_line and ut_name match, preserve the old ut_line.
716 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
717 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
718 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
719 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0)) {
720 (void)memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
723 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
724 if (atomicio(write, fd, ut, sizeof(*ut)) != sizeof(*ut))
725 dropbear_log(LOG_WARNING, "utmp_write_direct: error writing %s: %s",
726 UTMP_FILE, strerror(errno));
728 (void)close(fd);
729 return 1;
730 } else {
731 return 0;
734 # endif /* UTMP_USE_LIBRARY */
736 static int
737 utmp_perform_login(struct logininfo *li)
739 struct utmp ut;
741 construct_utmp(li, &ut);
742 # ifdef UTMP_USE_LIBRARY
743 if (!utmp_write_library(li, &ut)) {
744 dropbear_log(LOG_WARNING, "utmp_perform_login: utmp_write_library() failed");
745 return 0;
747 # else
748 if (!utmp_write_direct(li, &ut)) {
749 dropbear_log(LOG_WARNING, "utmp_perform_login: utmp_write_direct() failed");
750 return 0;
752 # endif
753 return 1;
757 static int
758 utmp_perform_logout(struct logininfo *li)
760 struct utmp ut;
762 construct_utmp(li, &ut);
763 # ifdef UTMP_USE_LIBRARY
764 if (!utmp_write_library(li, &ut)) {
765 dropbear_log(LOG_WARNING, "utmp_perform_logout: utmp_write_library() failed");
766 return 0;
768 # else
769 if (!utmp_write_direct(li, &ut)) {
770 dropbear_log(LOG_WARNING, "utmp_perform_logout: utmp_write_direct() failed");
771 return 0;
773 # endif
774 return 1;
779 utmp_write_entry(struct logininfo *li)
781 switch(li->type) {
782 case LTYPE_LOGIN:
783 return utmp_perform_login(li);
785 case LTYPE_LOGOUT:
786 return utmp_perform_logout(li);
788 default:
789 dropbear_log(LOG_WARNING, "utmp_write_entry: invalid type field");
790 return 0;
793 #endif /* USE_UTMP */
797 ** Low-level utmpx functions
800 /* not much point if we don't want utmpx entries */
801 #ifdef USE_UTMPX
803 /* if we have the wherewithall, use pututxline etc. */
804 # if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
805 defined(HAVE_PUTUTXLINE)
806 # define UTMPX_USE_LIBRARY
807 # endif
810 /* write a utmpx entry with the system's help (pututxline() and pals) */
811 # ifdef UTMPX_USE_LIBRARY
812 static int
813 utmpx_write_library(struct logininfo *li, struct utmpx *utx)
815 setutxent();
816 pututxline(utx);
818 # ifdef HAVE_ENDUTXENT
819 endutxent();
820 # endif
821 return 1;
824 # else /* UTMPX_USE_LIBRARY */
826 /* write a utmp entry direct to the file */
827 static int
828 utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
830 dropbear_log(LOG_WARNING, "utmpx_write_direct: not implemented!");
831 return 0;
833 # endif /* UTMPX_USE_LIBRARY */
835 static int
836 utmpx_perform_login(struct logininfo *li)
838 struct utmpx utx;
840 construct_utmpx(li, &utx);
841 # ifdef UTMPX_USE_LIBRARY
842 if (!utmpx_write_library(li, &utx)) {
843 dropbear_log(LOG_WARNING, "utmpx_perform_login: utmp_write_library() failed");
844 return 0;
846 # else
847 if (!utmpx_write_direct(li, &ut)) {
848 dropbear_log(LOG_WARNING, "utmpx_perform_login: utmp_write_direct() failed");
849 return 0;
851 # endif
852 return 1;
856 static int
857 utmpx_perform_logout(struct logininfo *li)
859 struct utmpx utx;
861 construct_utmpx(li, &utx);
862 # ifdef HAVE_STRUCT_UTMPX_UT_ID
863 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
864 # endif
865 # ifdef HAVE_STRUCT_UTMPX_UT_TYPE
866 utx.ut_type = DEAD_PROCESS;
867 # endif
869 # ifdef UTMPX_USE_LIBRARY
870 utmpx_write_library(li, &utx);
871 # else
872 utmpx_write_direct(li, &utx);
873 # endif
874 return 1;
878 utmpx_write_entry(struct logininfo *li)
880 switch(li->type) {
881 case LTYPE_LOGIN:
882 return utmpx_perform_login(li);
883 case LTYPE_LOGOUT:
884 return utmpx_perform_logout(li);
885 default:
886 dropbear_log(LOG_WARNING, "utmpx_write_entry: invalid type field");
887 return 0;
890 #endif /* USE_UTMPX */
894 ** Low-level wtmp functions
897 #ifdef USE_WTMP
899 /* write a wtmp entry direct to the end of the file */
900 /* This is a slight modification of code in OpenBSD's logwtmp.c */
901 static int
902 wtmp_write(struct logininfo *li, struct utmp *ut)
904 struct stat buf;
905 int fd, ret = 1;
907 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
908 dropbear_log(LOG_WARNING, "wtmp_write: problem writing %s: %s",
909 WTMP_FILE, strerror(errno));
910 return 0;
912 if (fstat(fd, &buf) == 0)
913 if (atomicio(write, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
914 ftruncate(fd, buf.st_size);
915 dropbear_log(LOG_WARNING, "wtmp_write: problem writing %s: %s",
916 WTMP_FILE, strerror(errno));
917 ret = 0;
919 (void)close(fd);
920 return ret;
923 static int
924 wtmp_perform_login(struct logininfo *li)
926 struct utmp ut;
928 construct_utmp(li, &ut);
929 return wtmp_write(li, &ut);
933 static int
934 wtmp_perform_logout(struct logininfo *li)
936 struct utmp ut;
938 construct_utmp(li, &ut);
939 return wtmp_write(li, &ut);
944 wtmp_write_entry(struct logininfo *li)
946 switch(li->type) {
947 case LTYPE_LOGIN:
948 return wtmp_perform_login(li);
949 case LTYPE_LOGOUT:
950 return wtmp_perform_logout(li);
951 default:
952 dropbear_log(LOG_WARNING, "wtmp_write_entry: invalid type field");
953 return 0;
958 /* Notes on fetching login data from wtmp/wtmpx
960 * Logouts are usually recorded with (amongst other things) a blank
961 * username on a given tty line. However, some systems (HP-UX is one)
962 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
964 * Since we're only looking for logins here, we know that the username
965 * must be set correctly. On systems that leave it in, we check for
966 * ut_type==USER_PROCESS (indicating a login.)
968 * Portability: Some systems may set something other than USER_PROCESS
969 * to indicate a login process. I don't know of any as I write. Also,
970 * it's possible that some systems may both leave the username in
971 * place and not have ut_type.
974 /* return true if this wtmp entry indicates a login */
975 static int
976 wtmp_islogin(struct logininfo *li, struct utmp *ut)
978 if (strncmp(li->username, ut->ut_name,
979 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
980 # ifdef HAVE_STRUCT_UTMP_UT_TYPE
981 if (ut->ut_type & USER_PROCESS)
982 return 1;
983 # else
984 return 1;
985 # endif
987 return 0;
991 wtmp_get_entry(struct logininfo *li)
993 struct stat st;
994 struct utmp ut;
995 int fd, found=0;
997 /* Clear the time entries in our logininfo */
998 li->tv_sec = li->tv_usec = 0;
1000 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
1001 dropbear_log(LOG_WARNING, "wtmp_get_entry: problem opening %s: %s",
1002 WTMP_FILE, strerror(errno));
1003 return 0;
1005 if (fstat(fd, &st) != 0) {
1006 dropbear_log(LOG_WARNING, "wtmp_get_entry: couldn't stat %s: %s",
1007 WTMP_FILE, strerror(errno));
1008 close(fd);
1009 return 0;
1012 /* Seek to the start of the last struct utmp */
1013 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
1014 /* Looks like we've got a fresh wtmp file */
1015 close(fd);
1016 return 0;
1019 while (!found) {
1020 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
1021 dropbear_log(LOG_WARNING, "wtmp_get_entry: read of %s failed: %s",
1022 WTMP_FILE, strerror(errno));
1023 close (fd);
1024 return 0;
1026 if ( wtmp_islogin(li, &ut) ) {
1027 found = 1;
1028 /* We've already checked for a time in struct
1029 * utmp, in login_getlast(). */
1030 # ifdef HAVE_STRUCT_UTMP_UT_TIME
1031 li->tv_sec = ut.ut_time;
1032 # else
1033 # if HAVE_STRUCT_UTMP_UT_TV
1034 li->tv_sec = ut.ut_tv.tv_sec;
1035 # endif
1036 # endif
1037 line_fullname(li->line, ut.ut_line,
1038 MIN_SIZEOF(li->line, ut.ut_line));
1039 # ifdef HAVE_STRUCT_UTMP_UT_HOST
1040 strlcpy(li->hostname, ut.ut_host,
1041 MIN_SIZEOF(li->hostname, ut.ut_host));
1042 # endif
1043 continue;
1045 /* Seek back 2 x struct utmp */
1046 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
1047 /* We've found the start of the file, so quit */
1048 close (fd);
1049 return 0;
1053 /* We found an entry. Tidy up and return */
1054 close(fd);
1055 return 1;
1057 # endif /* USE_WTMP */
1061 ** Low-level wtmpx functions
1064 #ifdef USE_WTMPX
1065 /* write a wtmpx entry direct to the end of the file */
1066 /* This is a slight modification of code in OpenBSD's logwtmp.c */
1067 static int
1068 wtmpx_write(struct logininfo *li, struct utmpx *utx)
1070 struct stat buf;
1071 int fd, ret = 1;
1073 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
1074 dropbear_log(LOG_WARNING, "wtmpx_write: problem opening %s: %s",
1075 WTMPX_FILE, strerror(errno));
1076 return 0;
1079 if (fstat(fd, &buf) == 0)
1080 if (atomicio(write, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
1081 ftruncate(fd, buf.st_size);
1082 dropbear_log(LOG_WARNING, "wtmpx_write: problem writing %s: %s",
1083 WTMPX_FILE, strerror(errno));
1084 ret = 0;
1086 (void)close(fd);
1088 return ret;
1092 static int
1093 wtmpx_perform_login(struct logininfo *li)
1095 struct utmpx utx;
1097 construct_utmpx(li, &utx);
1098 return wtmpx_write(li, &utx);
1102 static int
1103 wtmpx_perform_logout(struct logininfo *li)
1105 struct utmpx utx;
1107 construct_utmpx(li, &utx);
1108 return wtmpx_write(li, &utx);
1113 wtmpx_write_entry(struct logininfo *li)
1115 switch(li->type) {
1116 case LTYPE_LOGIN:
1117 return wtmpx_perform_login(li);
1118 case LTYPE_LOGOUT:
1119 return wtmpx_perform_logout(li);
1120 default:
1121 dropbear_log(LOG_WARNING, "wtmpx_write_entry: invalid type field");
1122 return 0;
1126 /* Please see the notes above wtmp_islogin() for information about the
1127 next two functions */
1129 /* Return true if this wtmpx entry indicates a login */
1130 static int
1131 wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1133 if ( strncmp(li->username, utx->ut_name,
1134 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
1135 # ifdef HAVE_STRUCT_UTMPX_UT_TYPE
1136 if (utx->ut_type == USER_PROCESS)
1137 return 1;
1138 # else
1139 return 1;
1140 # endif
1142 return 0;
1147 wtmpx_get_entry(struct logininfo *li)
1149 struct stat st;
1150 struct utmpx utx;
1151 int fd, found=0;
1153 /* Clear the time entries */
1154 li->tv_sec = li->tv_usec = 0;
1156 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
1157 dropbear_log(LOG_WARNING, "wtmpx_get_entry: problem opening %s: %s",
1158 WTMPX_FILE, strerror(errno));
1159 return 0;
1161 if (fstat(fd, &st) != 0) {
1162 dropbear_log(LOG_WARNING, "wtmpx_get_entry: couldn't stat %s: %s",
1163 WTMPX_FILE, strerror(errno));
1164 close(fd);
1165 return 0;
1168 /* Seek to the start of the last struct utmpx */
1169 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
1170 /* probably a newly rotated wtmpx file */
1171 close(fd);
1172 return 0;
1175 while (!found) {
1176 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
1177 dropbear_log(LOG_WARNING, "wtmpx_get_entry: read of %s failed: %s",
1178 WTMPX_FILE, strerror(errno));
1179 close (fd);
1180 return 0;
1182 /* Logouts are recorded as a blank username on a particular line.
1183 * So, we just need to find the username in struct utmpx */
1184 if ( wtmpx_islogin(li, &utx) ) {
1185 found = 1;
1186 # ifdef HAVE_STRUCT_UTMPX_UT_TV
1187 li->tv_sec = utx.ut_tv.tv_sec;
1188 # else
1189 # ifdef HAVE_STRUCT_UTMPX_UT_TIME
1190 li->tv_sec = utx.ut_time;
1191 # endif
1192 # endif
1193 line_fullname(li->line, utx.ut_line, sizeof(li->line));
1194 # ifdef HAVE_STRUCT_UTMPX_UT_HOST
1195 strlcpy(li->hostname, utx.ut_host,
1196 MIN_SIZEOF(li->hostname, utx.ut_host));
1197 # endif
1198 continue;
1200 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
1201 close (fd);
1202 return 0;
1206 close(fd);
1207 return 1;
1209 #endif /* USE_WTMPX */
1212 ** Low-level libutil login() functions
1215 #ifdef USE_LOGIN
1216 static int
1217 syslogin_perform_login(struct logininfo *li)
1219 struct utmp *ut;
1221 if (! (ut = (struct utmp *)malloc(sizeof(*ut)))) {
1222 dropbear_log(LOG_WARNING, "syslogin_perform_login: couldn't malloc()");
1223 return 0;
1225 construct_utmp(li, ut);
1226 login(ut);
1227 free(ut);
1229 return 1;
1232 static int
1233 syslogin_perform_logout(struct logininfo *li)
1235 # ifdef HAVE_LOGOUT
1236 char line[8];
1238 (void)line_stripname(line, li->line, sizeof(line));
1240 if (!logout(line)) {
1241 dropbear_log(LOG_WARNING, "syslogin_perform_logout: logout(%s) returned an error: %s", line, strerror(errno));
1242 # ifdef HAVE_LOGWTMP
1243 } else {
1244 logwtmp(line, "", "");
1245 # endif
1247 /* FIXME: (ATL - if the need arises) What to do if we have
1248 * login, but no logout? what if logout but no logwtmp? All
1249 * routines are in libutil so they should all be there,
1250 * but... */
1251 # endif
1252 return 1;
1256 syslogin_write_entry(struct logininfo *li)
1258 switch (li->type) {
1259 case LTYPE_LOGIN:
1260 return syslogin_perform_login(li);
1261 case LTYPE_LOGOUT:
1262 return syslogin_perform_logout(li);
1263 default:
1264 dropbear_log(LOG_WARNING, "syslogin_write_entry: Invalid type field");
1265 return 0;
1268 #endif /* USE_LOGIN */
1270 /* end of file log-syslogin.c */
1273 ** Low-level lastlog functions
1276 #ifdef USE_LASTLOG
1277 #define LL_FILE 1
1278 #define LL_DIR 2
1279 #define LL_OTHER 3
1281 static void
1282 lastlog_construct(struct logininfo *li, struct lastlog *last)
1284 /* clear the structure */
1285 memset(last, '\0', sizeof(*last));
1287 (void)line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
1288 strlcpy(last->ll_host, li->hostname,
1289 MIN_SIZEOF(last->ll_host, li->hostname));
1290 last->ll_time = li->tv_sec;
1293 static int
1294 lastlog_filetype(char *filename)
1296 struct stat st;
1298 if (stat(filename, &st) != 0) {
1299 dropbear_log(LOG_WARNING, "lastlog_perform_login: Couldn't stat %s: %s", filename,
1300 strerror(errno));
1301 return 0;
1303 if (S_ISDIR(st.st_mode))
1304 return LL_DIR;
1305 else if (S_ISREG(st.st_mode))
1306 return LL_FILE;
1307 else
1308 return LL_OTHER;
1312 /* open the file (using filemode) and seek to the login entry */
1313 static int
1314 lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1316 off_t offset;
1317 int type;
1318 char lastlog_file[1024];
1320 type = lastlog_filetype(LASTLOG_FILE);
1321 switch (type) {
1322 case LL_FILE:
1323 strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file));
1324 break;
1325 case LL_DIR:
1326 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1327 LASTLOG_FILE, li->username);
1328 break;
1329 default:
1330 dropbear_log(LOG_WARNING, "lastlog_openseek: %.100s is not a file or directory!",
1331 LASTLOG_FILE);
1332 return 0;
1335 *fd = open(lastlog_file, filemode, 0600);
1336 if ( *fd < 0) {
1337 dropbear_log(LOG_INFO, "lastlog_openseek: Couldn't open %s: %s",
1338 lastlog_file, strerror(errno));
1339 return 0;
1342 if (type == LL_FILE) {
1343 /* find this uid's offset in the lastlog file */
1344 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
1346 if ( lseek(*fd, offset, SEEK_SET) != offset ) {
1347 dropbear_log(LOG_WARNING, "lastlog_openseek: %s->lseek(): %s",
1348 lastlog_file, strerror(errno));
1349 return 0;
1353 return 1;
1356 static int
1357 lastlog_perform_login(struct logininfo *li)
1359 struct lastlog last;
1360 int fd;
1362 /* create our struct lastlog */
1363 lastlog_construct(li, &last);
1365 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
1366 return(0);
1368 /* write the entry */
1369 if (atomicio(write, fd, &last, sizeof(last)) != sizeof(last)) {
1370 close(fd);
1371 dropbear_log(LOG_WARNING, "lastlog_write_filemode: Error writing to %s: %s",
1372 LASTLOG_FILE, strerror(errno));
1373 return 0;
1376 close(fd);
1377 return 1;
1381 lastlog_write_entry(struct logininfo *li)
1383 switch(li->type) {
1384 case LTYPE_LOGIN:
1385 return lastlog_perform_login(li);
1386 default:
1387 dropbear_log(LOG_WARNING, "lastlog_write_entry: Invalid type field");
1388 return 0;
1392 #endif /* USE_LASTLOG */