Blame login/login.c

Packit 6c4009
/* Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <utmp.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Return the result of ttyname in the buffer pointed to by TTY, which should
Packit 6c4009
   be of length BUF_LEN.  If it is too long to fit in this buffer, a
Packit 6c4009
   sufficiently long buffer is allocated using malloc, and returned in TTY.
Packit 6c4009
   0 is returned upon success, -1 otherwise.  */
Packit 6c4009
static int
Packit 6c4009
tty_name (int fd, char **tty, size_t buf_len)
Packit 6c4009
{
Packit 6c4009
  int rv;			/* Return value.  0 = success.  */
Packit 6c4009
  char *buf = *tty;		/* Buffer for ttyname, initially the user's. */
Packit 6c4009
Packit 6c4009
  for (;;)
Packit 6c4009
    {
Packit 6c4009
      char *new_buf;
Packit 6c4009
Packit 6c4009
      if (buf_len)
Packit 6c4009
	{
Packit 6c4009
	  rv = ttyname_r (fd, buf, buf_len);
Packit 6c4009
Packit 6c4009
	  if (rv != 0 || memchr (buf, '\0', buf_len))
Packit 6c4009
	    /* We either got an error, or we succeeded and the
Packit 6c4009
	       returned name fit in the buffer.  */
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  /* Try again with a longer buffer.  */
Packit 6c4009
	  buf_len += buf_len;	/* Double it */
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	/* No initial buffer; start out by mallocing one.  */
Packit 6c4009
	buf_len = 128;		/* First time guess.  */
Packit 6c4009
Packit 6c4009
      if (buf != *tty)
Packit 6c4009
	/* We've already malloced another buffer at least once.  */
Packit 6c4009
	new_buf = realloc (buf, buf_len);
Packit 6c4009
      else
Packit 6c4009
	new_buf = malloc (buf_len);
Packit 6c4009
      if (! new_buf)
Packit 6c4009
	{
Packit 6c4009
	  rv = -1;
Packit 6c4009
	  __set_errno (ENOMEM);
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
      buf = new_buf;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (rv == 0)
Packit 6c4009
    *tty = buf;		/* Return buffer to the user.  */
Packit 6c4009
  else if (buf != *tty)
Packit 6c4009
    free (buf);		/* Free what we malloced when returning an error.  */
Packit 6c4009
Packit 6c4009
  return rv;
Packit 6c4009
}
Packit 6c4009

Packit 6c4009
void
Packit 6c4009
login (const struct utmp *ut)
Packit 6c4009
{
Packit 6c4009
#ifdef PATH_MAX
Packit 6c4009
  char _tty[PATH_MAX + UT_LINESIZE];
Packit 6c4009
#else
Packit 6c4009
  char _tty[512 + UT_LINESIZE];
Packit 6c4009
#endif
Packit 6c4009
  char *tty = _tty;
Packit 6c4009
  int found_tty;
Packit 6c4009
  const char *ttyp;
Packit 6c4009
  struct utmp copy = *ut;
Packit 6c4009
Packit 6c4009
  /* Fill in those fields we supply.  */
Packit 6c4009
  copy.ut_type = USER_PROCESS;
Packit 6c4009
  copy.ut_pid = getpid ();
Packit 6c4009
Packit 6c4009
  /* Seek tty.  */
Packit 6c4009
  found_tty = tty_name (STDIN_FILENO, &tty, sizeof (_tty));
Packit 6c4009
  if (found_tty < 0)
Packit 6c4009
    found_tty = tty_name (STDOUT_FILENO, &tty, sizeof (_tty));
Packit 6c4009
  if (found_tty < 0)
Packit 6c4009
    found_tty = tty_name (STDERR_FILENO, &tty, sizeof (_tty));
Packit 6c4009
Packit 6c4009
  if (found_tty >= 0)
Packit 6c4009
    {
Packit 6c4009
      /* We only want to insert the name of the tty without path.
Packit 6c4009
	 But take care of name like /dev/pts/3.  */
Packit 6c4009
      if (strncmp (tty, "/dev/", 5) == 0)
Packit 6c4009
	ttyp = tty + 5;		/* Skip the "/dev/".  */
Packit 6c4009
      else
Packit 6c4009
	ttyp = basename (tty);
Packit 6c4009
Packit 6c4009
      /* Position to record for this tty.  */
Packit 6c4009
      strncpy (copy.ut_line, ttyp, UT_LINESIZE);
Packit 6c4009
Packit 6c4009
      /* Tell that we want to use the UTMP file.  */
Packit 6c4009
      if (utmpname (_PATH_UTMP) == 0)
Packit 6c4009
	{
Packit 6c4009
	  /* Open UTMP file.  */
Packit 6c4009
	  setutent ();
Packit 6c4009
Packit 6c4009
	  /* Write the entry.  */
Packit 6c4009
	  pututline (©);
Packit 6c4009
Packit 6c4009
	  /* Close UTMP file.  */
Packit 6c4009
	  endutent ();
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (tty != _tty)
Packit 6c4009
	free (tty);		/* Free buffer malloced by tty_name.  */
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    /* We provide a default value so that the output does not contain
Packit 6c4009
       an random bytes in this field.  */
Packit 6c4009
    strncpy (copy.ut_line, "???", UT_LINESIZE);
Packit 6c4009
Packit 6c4009
  /* Update the WTMP file.  Here we have to add a new entry.  */
Packit 6c4009
  updwtmp (_PATH_WTMP, ©);
Packit 6c4009
}