| 1 |
/* |
| 2 |
Copyright (C) 1999-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* |
| 20 |
* Copyright (c) 1995 Wietse Venema. All rights reserved. |
| 21 |
* |
| 22 |
* Individual files may be covered by other copyrights (as noted in |
| 23 |
* the file itself.) |
| 24 |
* |
| 25 |
* This material was originally written and compiled by Wietse Venema |
| 26 |
* at Eindhoven University of Technology, The Netherlands, in 1990, |
| 27 |
* 1991, 1992, 1993, 1994 and 1995. |
| 28 |
* |
| 29 |
* Redistribution and use in source and binary forms are permitted |
| 30 |
* provided that this entire copyright notice is duplicated in all |
| 31 |
* such copies. |
| 32 |
* |
| 33 |
* This software is provided "as is" and without any expressed or |
| 34 |
* implied warranties, including, without limitation, the implied |
| 35 |
* warranties of merchantibility and fitness for any particular |
| 36 |
* purpose. |
| 37 |
*/ |
| 38 |
|
| 39 |
/* Written by Wietse Venema. With port to GNU Inetutils done by Alain |
| 40 |
Magloire. Reorganized to cope with full variation between utmpx |
| 41 |
and utmp, with different API sets, by Mats Erik Andersson. */ |
| 42 |
|
| 43 |
#include <config.h> |
| 44 |
|
| 45 |
#include <sys/types.h> |
| 46 |
|
| 47 |
#include <sys/time.h> |
| 48 |
#include <time.h> |
| 49 |
#ifdef HAVE_UTMPX_H |
| 50 |
# ifndef __USE_GNU |
| 51 |
# define __USE_GNU 1 |
| 52 |
# endif |
| 53 |
# include <utmpx.h> |
| 54 |
#else /* !HAVE_UTMPX_H */ |
| 55 |
# ifdef HAVE_UTIL_H |
| 56 |
# include <util.h> |
| 57 |
# endif |
| 58 |
# ifdef HAVE_LIBUTIL_H |
| 59 |
# include <libutil.h> |
| 60 |
# endif |
| 61 |
# include <utmp.h> |
| 62 |
#endif |
| 63 |
#include <string.h> |
| 64 |
#include <unistd.h> |
| 65 |
|
| 66 |
/* utmp_init - update utmp and wtmp before login */ |
| 67 |
|
| 68 |
void |
| 69 |
utmp_init (char *line, char *user, char *id, char *host) |
| 70 |
{ |
| 71 |
#ifdef HAVE_UTMPX_H |
| 72 |
struct utmpx utx; |
| 73 |
struct timeval tv; |
| 74 |
#else /* !HAVE_UTMPX_H */ |
| 75 |
struct utmp utx; |
| 76 |
# if defined HAVE_STRUCT_UTMP_UT_TV |
| 77 |
struct timeval tv; |
| 78 |
# endif |
| 79 |
#endif |
| 80 |
|
| 81 |
memset ((char *) &utx, 0, sizeof (utx)); |
| 82 |
#if defined HAVE_STRUCT_UTMP_UT_ID || defined HAVE_STRUCT_UTMPX_UT_ID |
| 83 |
strncpy (utx.ut_id, id, sizeof (utx.ut_id)); |
| 84 |
#endif |
| 85 |
#if defined HAVE_STRUCT_UTMP_UT_USER || defined HAVE_STRUCT_UTMPX_UT_USER |
| 86 |
strncpy (utx.ut_user, user, sizeof (utx.ut_user)); |
| 87 |
#elif defined HAVE_STRUCT_UTMP_UT_NAME || defined HAVE_STRUCT_UTMPX_UT_NAME |
| 88 |
strncpy (utx.ut_name, user, sizeof (utx.ut_name)); |
| 89 |
#endif |
| 90 |
#if defined HAVE_STRUCT_UTMP_UT_HOST || defined HAVE_STRUCT_UTMPX_UT_HOST |
| 91 |
strncpy (utx.ut_host, host, sizeof (utx.ut_host)); |
| 92 |
# ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN /* Only utmpx. */ |
| 93 |
if (strlen (host) < sizeof (utx.ut_host)) |
| 94 |
utx.ut_syslen = strlen (host) + 1; |
| 95 |
else |
| 96 |
{ |
| 97 |
utx.ut_host[sizeof (utx.ut_host) - 1] = '\0'; |
| 98 |
utx.ut_syslen = sizeof (utx.ut_host); |
| 99 |
} |
| 100 |
# endif |
| 101 |
#endif /* UT_HOST */ |
| 102 |
#if defined HAVE_STRUCT_UTMP_UT_LINE || defined HAVE_STRUCT_UTMPX_UT_LINE |
| 103 |
strncpy (utx.ut_line, line, sizeof (utx.ut_line)); |
| 104 |
#endif |
| 105 |
|
| 106 |
#if defined HAVE_STRUCT_UTMP_UT_PID || defined HAVE_STRUCT_UTMPX_UT_PID |
| 107 |
utx.ut_pid = getpid (); |
| 108 |
#endif |
| 109 |
#if defined HAVE_STRUCT_UTMP_UT_TYPE || defined HAVE_STRUCT_UTMPX_UT_TYPE |
| 110 |
utx.ut_type = LOGIN_PROCESS; |
| 111 |
#endif |
| 112 |
#if defined HAVE_STRUCT_UTMP_UT_TV || defined HAVE_STRUCT_UTMPX_UT_TV |
| 113 |
gettimeofday (&tv, 0); |
| 114 |
utx.ut_tv.tv_sec = tv.tv_sec; |
| 115 |
utx.ut_tv.tv_usec = tv.tv_usec; |
| 116 |
#else |
| 117 |
time (&(utx.ut_time)); |
| 118 |
#endif |
| 119 |
|
| 120 |
/* Prefer utmpx over utmp, and attempt to |
| 121 |
* use pututxline/pututline for writing the |
| 122 |
* initial entry. Then apply whatever |
| 123 |
* wtmp updating that happens to be available. |
| 124 |
* |
| 125 |
* That failing, fall back to loginx/login. |
| 126 |
* This is made in order than we are granted |
| 127 |
* LOGIN_PROCESS type and stay unbound by |
| 128 |
* any tty sensing of stdin, stdout, or stderr, |
| 129 |
* like GNU libc would do in login(). |
| 130 |
*/ |
| 131 |
#ifdef HAVE_UTMPX_H |
| 132 |
# ifdef HAVE_PUTUTXLINE |
| 133 |
setutxent (); |
| 134 |
pututxline (&utx); |
| 135 |
/* Some systems perform wtmp updating |
| 136 |
* already in calling pututxline(). |
| 137 |
*/ |
| 138 |
# ifdef HAVE_UPDWTMPX |
| 139 |
updwtmpx (PATH_WTMPX, &utx); |
| 140 |
# elif defined HAVE_LOGWTMPX |
| 141 |
logwtmpx (line, user, id, 0, LOGIN_PROCESS); |
| 142 |
# endif |
| 143 |
/* wtmp updating */ |
| 144 |
endutxent (); |
| 145 |
# elif defined HAVE_LOGINX /* !HAVE_PUTUTXLINE */ |
| 146 |
loginx (&utx, 0, LOGIN_PROCESS); |
| 147 |
# endif/* HAVE_LOGINX && !HAVE_PUTUTXLINE */ |
| 148 |
|
| 149 |
#else /* !HAVE_UTMPX_H */ |
| 150 |
# ifdef HAVE_PUTUTLINE |
| 151 |
setutent (); |
| 152 |
pututline (&utx); |
| 153 |
# ifdef HAVE_UPDWTMP |
| 154 |
updwtmp (PATH_WTMP, &utx); |
| 155 |
# elif defined HAVE_LOGWTMP /* !HAVE_UPDWTMP */ |
| 156 |
logwtmp (line, user, id); |
| 157 |
# endif |
| 158 |
/* wtmp updating */ |
| 159 |
endutent (); |
| 160 |
# elif defined HAVE_LOGIN /* !HAVE_PUTUTLINE */ |
| 161 |
(void) id; /* Silence warnings. */ |
| 162 |
login (&utx); |
| 163 |
# endif/* HAVE_LOGIN && !HAVE_PUTUTLINE */ |
| 164 |
#endif /* !HAVE_UTMPX_H */ |
| 165 |
} |
| 166 |
|
| 167 |
/* utmp_ptsid - generate utmp id for pseudo terminal */ |
| 168 |
|
| 169 |
char * |
| 170 |
utmp_ptsid (char *line, char *tag) |
| 171 |
{ |
| 172 |
static char buf[5]; |
| 173 |
|
| 174 |
strncpy (buf, tag, 2); |
| 175 |
strncpy (buf + 2, line + strlen (line) - 2, 2); |
| 176 |
return (buf); |
| 177 |
} |