2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Copyright 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of Julianne F. Haugh nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "defines.h"
|
2008-01-05 18:53:22 +05:30
|
|
|
#include "prototypes.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#include <utmp.h>
|
|
|
|
|
|
|
|
#if HAVE_UTMPX_H
|
|
|
|
#include <utmpx.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#if HAVE_UTMPX_H
|
2007-10-07 17:15:14 +05:30
|
|
|
struct utmpx utxent;
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:15:14 +05:30
|
|
|
struct utmp utent;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
extern struct utmp *getutent ();
|
|
|
|
extern struct utmp *getutline ();
|
|
|
|
extern void setutent ();
|
|
|
|
extern void endutent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#define NO_UTENT \
|
2007-10-07 17:17:01 +05:30
|
|
|
_("No utmp entry. You must exec \"login\" from the lowest level \"sh\"")
|
2007-10-07 17:14:02 +05:30
|
|
|
#define NO_TTY \
|
2007-10-07 17:17:01 +05:30
|
|
|
_("Unable to determine your tty name.")
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* checkutmp - see if utmp file is correct for this process
|
|
|
|
*
|
|
|
|
* System V is very picky about the contents of the utmp file
|
|
|
|
* and requires that a slot for the current process exist.
|
|
|
|
* The utmp file is scanned for an entry with the same process
|
|
|
|
* ID. If no entry exists the process exits with a message.
|
|
|
|
*
|
|
|
|
* The "picky" flag is for network and other logins that may
|
|
|
|
* use special flags. It allows the pid checks to be overridden.
|
|
|
|
* This means that getty should never invoke login with any
|
|
|
|
* command line flags.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
#if defined(__linux__) /* XXX */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
void checkutmp (int picky)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
struct utmp *ut;
|
2007-10-07 17:15:23 +05:30
|
|
|
pid_t pid = getpid ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
setutent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/* First, try to find a valid utmp entry for this process. */
|
2007-10-07 17:15:23 +05:30
|
|
|
while ((ut = getutent ()))
|
2007-10-07 17:14:02 +05:30
|
|
|
if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
|
2007-10-07 17:15:23 +05:30
|
|
|
(ut->ut_type == LOGIN_PROCESS
|
|
|
|
|| ut->ut_type == USER_PROCESS))
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
/* If there is one, just use it, otherwise create a new one. */
|
|
|
|
if (ut) {
|
|
|
|
utent = *ut;
|
|
|
|
} else {
|
|
|
|
if (picky) {
|
2007-10-07 17:15:23 +05:30
|
|
|
puts (NO_UTENT);
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
line = ttyname (0);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!line) {
|
2007-10-07 17:15:23 +05:30
|
|
|
puts (NO_TTY);
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
if (strncmp (line, "/dev/", 5) == 0)
|
2007-10-07 17:14:02 +05:30
|
|
|
line += 5;
|
2007-10-07 17:15:23 +05:30
|
|
|
memset ((void *) &utent, 0, sizeof utent);
|
2007-10-07 17:14:02 +05:30
|
|
|
utent.ut_type = LOGIN_PROCESS;
|
|
|
|
utent.ut_pid = pid;
|
2007-10-07 17:15:23 +05:30
|
|
|
strncpy (utent.ut_line, line, sizeof utent.ut_line);
|
2007-10-07 17:14:02 +05:30
|
|
|
/* XXX - assumes /dev/tty?? */
|
2007-10-07 17:16:07 +05:30
|
|
|
strncpy (utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
|
2007-10-07 17:15:23 +05:30
|
|
|
strcpy (utent.ut_user, "LOGIN");
|
|
|
|
utent.ut_time = time (NULL);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(LOGIN_PROCESS)
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
void checkutmp (int picky)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
struct utmp *ut;
|
2007-10-07 17:15:23 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#if HAVE_UTMPX_H
|
|
|
|
struct utmpx *utx;
|
|
|
|
#endif
|
2007-10-07 17:15:23 +05:30
|
|
|
pid_t pid = getpid ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#if HAVE_UTMPX_H
|
2007-10-07 17:15:23 +05:30
|
|
|
setutxent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:15:23 +05:30
|
|
|
setutent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (picky) {
|
|
|
|
#if HAVE_UTMPX_H
|
2007-10-07 17:15:23 +05:30
|
|
|
while ((utx = getutxent ()))
|
2007-10-07 17:14:02 +05:30
|
|
|
if (utx->ut_pid == pid)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (utx)
|
|
|
|
utxent = *utx;
|
|
|
|
#endif
|
2007-10-07 17:15:23 +05:30
|
|
|
while ((ut = getutent ()))
|
2007-10-07 17:14:02 +05:30
|
|
|
if (ut->ut_pid == pid)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ut)
|
|
|
|
utent = *ut;
|
|
|
|
|
|
|
|
#if HAVE_UTMPX_H
|
2007-10-07 17:15:23 +05:30
|
|
|
endutxent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:15:23 +05:30
|
|
|
endutent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (!ut) {
|
2007-10-07 17:15:23 +05:30
|
|
|
puts (NO_UTENT);
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifndef UNIXPC
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is no ut_line value in this record, fill
|
|
|
|
* it in by getting the TTY name and stuffing it in
|
|
|
|
* the structure. The UNIX/PC is broken in this regard
|
|
|
|
* and needs help ...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (utent.ut_line[0] == '\0')
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* !UNIXPC */
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
if (!(line = ttyname (0))) {
|
|
|
|
puts (NO_TTY);
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
if (strncmp (line, "/dev/", 5) == 0)
|
2007-10-07 17:14:02 +05:30
|
|
|
line += 5;
|
2007-10-07 17:16:07 +05:30
|
|
|
strncpy (utent.ut_line, line, sizeof utent.ut_line);
|
2007-10-07 17:14:02 +05:30
|
|
|
#if HAVE_UTMPX_H
|
2007-10-07 17:16:07 +05:30
|
|
|
strncpy (utxent.ut_line, line, sizeof utxent.ut_line);
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} else {
|
2007-10-07 17:15:23 +05:30
|
|
|
if (!(line = ttyname (0))) {
|
|
|
|
puts (NO_TTY);
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
if (strncmp (line, "/dev/", 5) == 0)
|
2007-10-07 17:14:02 +05:30
|
|
|
line += 5;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
strncpy (utent.ut_line, line, sizeof utent.ut_line);
|
|
|
|
if ((ut = getutline (&utent)))
|
|
|
|
strncpy (utent.ut_id, ut->ut_id, sizeof ut->ut_id);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
strcpy (utent.ut_user, "LOGIN");
|
|
|
|
utent.ut_pid = getpid ();
|
2007-10-07 17:14:02 +05:30
|
|
|
utent.ut_type = LOGIN_PROCESS;
|
2007-10-07 17:15:23 +05:30
|
|
|
utent.ut_time = time (NULL);
|
2007-10-07 17:14:02 +05:30
|
|
|
#if HAVE_UTMPX_H
|
2007-10-07 17:15:23 +05:30
|
|
|
strncpy (utxent.ut_line, line, sizeof utxent.ut_line);
|
|
|
|
if ((utx = getutxline (&utxent)))
|
2007-10-07 17:16:07 +05:30
|
|
|
strncpy (utxent.ut_id, utx->ut_id, sizeof utxent.ut_id);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
strcpy (utxent.ut_user, "LOGIN");
|
2007-10-07 17:14:02 +05:30
|
|
|
utxent.ut_pid = utent.ut_pid;
|
|
|
|
utxent.ut_type = utent.ut_type;
|
2007-10-07 17:15:23 +05:30
|
|
|
if (sizeof (utxent.ut_tv) == sizeof (struct timeval))
|
2007-10-07 17:16:07 +05:30
|
|
|
gettimeofday ((struct timeval *) &utxent.ut_tv, NULL);
|
2007-10-07 17:15:23 +05:30
|
|
|
else {
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
utxent.ut_tv.tv_sec = tv.tv_sec;
|
|
|
|
utxent.ut_tv.tv_usec = tv.tv_usec;
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
utent.ut_time = utxent.ut_tv.tv_sec;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:34 +05:30
|
|
|
#endif
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some systems already have updwtmp() and possibly updwtmpx(). Others
|
|
|
|
* don't, so we re-implement these functions if necessary. --marekm
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HAVE_UPDWTMP
|
2007-10-07 17:15:23 +05:30
|
|
|
static void updwtmp (const char *filename, const struct utmp *ut)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
fd = open (filename, O_APPEND | O_WRONLY, 0);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (fd >= 0) {
|
2007-10-07 17:15:23 +05:30
|
|
|
write (fd, (const char *) ut, sizeof (*ut));
|
|
|
|
close (fd);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* ! HAVE_UPDWTMP */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#ifdef HAVE_UTMPX_H
|
|
|
|
#ifndef HAVE_UPDWTMPX
|
2007-10-07 17:15:23 +05:30
|
|
|
static void updwtmpx (const char *filename, const struct utmpx *utx)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
fd = open (filename, O_APPEND | O_WRONLY, 0);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (fd >= 0) {
|
2007-10-07 17:15:23 +05:30
|
|
|
write (fd, (const char *) utx, sizeof (*utx));
|
|
|
|
close (fd);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* ! HAVE_UPDWTMPX */
|
|
|
|
#endif /* ! HAVE_UTMPX_H */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* setutmp - put a USER_PROCESS entry in the utmp file
|
|
|
|
*
|
|
|
|
* setutmp changes the type of the current utmp entry to
|
|
|
|
* USER_PROCESS. the wtmp file will be updated as well.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
#if defined(__linux__) /* XXX */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
void setutmp (const char *name, const char *line, const char *host)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
utent.ut_type = USER_PROCESS;
|
2007-10-07 17:15:23 +05:30
|
|
|
strncpy (utent.ut_user, name, sizeof utent.ut_user);
|
|
|
|
utent.ut_time = time (NULL);
|
2007-10-07 17:14:02 +05:30
|
|
|
/* other fields already filled in by checkutmp above */
|
2007-10-07 17:15:23 +05:30
|
|
|
setutent ();
|
|
|
|
pututline (&utent);
|
|
|
|
endutent ();
|
|
|
|
updwtmp (_WTMP_FILE, &utent);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#elif HAVE_UTMPX_H
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
void setutmp (const char *name, const char *line, const char *host)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
struct utmp *utmp, utline;
|
|
|
|
struct utmpx *utmpx, utxline;
|
|
|
|
pid_t pid = getpid ();
|
|
|
|
int found_utmpx = 0, found_utmp = 0;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* The canonical device name doesn't include "/dev/"; skip it
|
|
|
|
* if it is already there.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (strncmp (line, "/dev/", 5) == 0)
|
|
|
|
line += 5;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update utmpx. We create an empty entry in case there is
|
|
|
|
* no matching entry in the utmpx file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
setutxent ();
|
|
|
|
setutent ();
|
|
|
|
|
|
|
|
while (utmpx = getutxent ()) {
|
|
|
|
if (utmpx->ut_pid == pid) {
|
|
|
|
found_utmpx = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (utmp = getutent ()) {
|
|
|
|
if (utmp->ut_pid == pid) {
|
|
|
|
found_utmp = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the entry matching `pid' cannot be found, create a new
|
|
|
|
* entry with the device name in it.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (!found_utmpx) {
|
2007-10-07 17:14:02 +05:30
|
|
|
memset ((void *) &utxline, 0, sizeof utxline);
|
|
|
|
strncpy (utxline.ut_line, line, sizeof utxline.ut_line);
|
|
|
|
utxline.ut_pid = getpid ();
|
|
|
|
} else {
|
|
|
|
utxline = *utmpx;
|
|
|
|
if (strncmp (utxline.ut_line, "/dev/", 5) == 0) {
|
|
|
|
memmove (utxline.ut_line, utxline.ut_line + 5,
|
2007-10-07 17:15:23 +05:30
|
|
|
sizeof utxline.ut_line - 5);
|
2007-10-07 17:14:02 +05:30
|
|
|
utxline.ut_line[sizeof utxline.ut_line - 5] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
if (!found_utmp) {
|
2007-10-07 17:14:02 +05:30
|
|
|
memset ((void *) &utline, 0, sizeof utline);
|
|
|
|
strncpy (utline.ut_line, utxline.ut_line,
|
2007-10-07 17:15:23 +05:30
|
|
|
sizeof utline.ut_line);
|
2007-10-07 17:14:02 +05:30
|
|
|
utline.ut_pid = utxline.ut_pid;
|
|
|
|
} else {
|
|
|
|
utline = *utmp;
|
|
|
|
if (strncmp (utline.ut_line, "/dev/", 5) == 0) {
|
|
|
|
memmove (utline.ut_line, utline.ut_line + 5,
|
2007-10-07 17:15:23 +05:30
|
|
|
sizeof utline.ut_line - 5);
|
2007-10-07 17:14:02 +05:30
|
|
|
utline.ut_line[sizeof utline.ut_line - 5] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in the fields in the utmpx entry and write it out. Do
|
|
|
|
* the utmp entry at the same time to make sure things don't
|
|
|
|
* get messed up.
|
|
|
|
*/
|
|
|
|
|
|
|
|
strncpy (utxline.ut_user, name, sizeof utxline.ut_user);
|
|
|
|
strncpy (utline.ut_user, name, sizeof utline.ut_user);
|
|
|
|
|
|
|
|
utline.ut_type = utxline.ut_type = USER_PROCESS;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (sizeof (utxline.ut_tv) == sizeof (struct timeval))
|
|
|
|
gettimeofday ((struct timeval *) &utxline.ut_tv, NULL);
|
|
|
|
else {
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
utxline.ut_tv.tv_sec = tv.tv_sec;
|
|
|
|
utxline.ut_tv.tv_usec = tv.tv_usec;
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
utline.ut_time = utxline.ut_tv.tv_sec;
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
strncpy (utxline.ut_host, host ? host : "", sizeof utxline.ut_host);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
pututxline (&utxline);
|
|
|
|
pututline (&utline);
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
updwtmpx (_WTMP_FILE "x", &utxline);
|
|
|
|
updwtmp (_WTMP_FILE, &utline);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
utxent = utxline;
|
|
|
|
utent = utline;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|