Path: blob/main/crypto/heimdal/appl/login/utmp_login.c
96309 views
/*1* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "login_locl.h"3435RCSID("$Id$");3637/* try to put something useful from hostname into dst, dst_sz:38* full name, first component or address */3940void41shrink_hostname (const char *hostname,42char *dst, size_t dst_sz)43{44char local_hostname[MaxHostNameLen];45char *ld, *hd;46int ret;47struct addrinfo *ai;4849if (strlen(hostname) < dst_sz) {50strlcpy (dst, hostname, dst_sz);51return;52}53gethostname (local_hostname, sizeof(local_hostname));54hd = strchr (hostname, '.');55ld = strchr (local_hostname, '.');56if (hd != NULL && ld != NULL && strcmp(hd, ld) == 057&& hd - hostname < dst_sz) {58strlcpy (dst, hostname, dst_sz);59dst[hd - hostname] = '\0';60return;61}6263ret = getaddrinfo (hostname, NULL, NULL, &ai);64if (ret) {65strncpy (dst, hostname, dst_sz);66return;67}68ret = getnameinfo (ai->ai_addr, ai->ai_addrlen,69dst, dst_sz,70NULL, 0,71NI_NUMERICHOST);72freeaddrinfo (ai);73if (ret) {74strncpy (dst, hostname, dst_sz);75return;76}77}7879/* update utmp and wtmp - the BSD way */8081#if !defined(HAVE_UTMPX_H) || (defined(WTMP_FILE) && !defined(WTMPX_FILE))8283void84prepare_utmp (struct utmp *utmp, char *tty,85const char *username, const char *hostname)86{87char *ttyx = clean_ttyname (tty);8889memset(utmp, 0, sizeof(*utmp));90utmp->ut_time = time(NULL);91strncpy(utmp->ut_line, ttyx, sizeof(utmp->ut_line));92strncpy(utmp->ut_name, username, sizeof(utmp->ut_name));9394# ifdef HAVE_STRUCT_UTMP_UT_USER95strncpy(utmp->ut_user, username, sizeof(utmp->ut_user));96# endif9798# ifdef HAVE_STRUCT_UTMP_UT_ADDR99if (hostname[0]) {100struct hostent *he;101if ((he = gethostbyname(hostname)))102memcpy(&utmp->ut_addr, he->h_addr_list[0],103sizeof(utmp->ut_addr));104}105# endif106107# ifdef HAVE_STRUCT_UTMP_UT_HOST108shrink_hostname (hostname, utmp->ut_host, sizeof(utmp->ut_host));109# endif110111# ifdef HAVE_STRUCT_UTMP_UT_TYPE112utmp->ut_type = USER_PROCESS;113# endif114115# ifdef HAVE_STRUCT_UTMP_UT_PID116utmp->ut_pid = getpid();117# endif118119# ifdef HAVE_STRUCT_UTMP_UT_ID120strncpy(utmp->ut_id, make_id(ttyx), sizeof(utmp->ut_id));121# endif122}123#endif124125#ifdef HAVE_UTMPX_H126void utmp_login(char *tty, const char *username, const char *hostname)127{128return;129}130#else131132void utmp_login(char *tty, const char *username, const char *hostname)133{134struct utmp utmp;135int fd;136137prepare_utmp (&utmp, tty, username, hostname);138139#ifdef HAVE_SETUTENT140utmpname(_PATH_UTMP);141setutent();142pututline(&utmp);143endutent();144#else145146#ifdef HAVE_TTYSLOT147{148int ttyno;149ttyno = ttyslot();150if (ttyno > 0 && (fd = open(_PATH_UTMP, O_WRONLY, 0)) >= 0) {151lseek(fd, (long)(ttyno * sizeof(struct utmp)), SEEK_SET);152write(fd, &utmp, sizeof(struct utmp));153close(fd);154}155}156#endif /* HAVE_TTYSLOT */157#endif /* HAVE_SETUTENT */158159if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) {160write(fd, &utmp, sizeof(struct utmp));161close(fd);162}163}164165#endif /* !HAVE_UTMPX_H */166167168