Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 15 Dec 1996 15:31:55 +0100 (MET)
From:      J Wunsch <j@uriah.heep.sax.de>
To:        freebsd-current@FreeBSD.org (FreeBSD-current users)
Subject:   cvt-wtmp
Message-ID:  <199612151431.PAA24147@uriah.heep.sax.de>

next in thread | raw e-mail | index | archive | help

--ELM850660315-20318-0_
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

For the curious, here is it.  I'm still awaiting Jordan's proposal on
the upgrade suite framework :), but since i'm afraid this will take
quite a bit longer to complete, and people need to convert their
wtmp's right now, i wrote the tool.  (Yes, i've just upgraded my
machine, or what do you believe why i wrote it? :)

I might check it into CVS under tools/ until the upgrade framework is
ready for prime-time.  Objections?  (If it's only in -current, but not
in 2.2, it won't get a CVS tag anytime soon, so we could even
repository copy it later.)

-- 
cheers, J"org

joerg_wunsch@uriah.heep.sax.de -- http://www.sax.de/~joerg/ -- NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)

--ELM850660315-20318-0_
Content-Type: text/plain
Content-Disposition: attachment; filename=cvt-wtmp.c
Content-Description: /tmp/cvt-wtmp.c
Content-Transfer-Encoding: 7bit

/*
 * Copyright (c) 1996 Joerg Wunsch
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``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 THE DEVELOPERS 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.
 */

/*
 * Heuristics to convert old wtmp format to new one.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>

#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <utmp.h>


#define	OUT_NAMESIZE	8
#define	OUT_LINESIZE	8
#define	OUT_HOSTSIZE	16

struct olastlog {
	time_t	ll_time;
	char	ll_line[OUT_LINESIZE];
	char	ll_host[OUT_HOSTSIZE];
};

struct outmp {
	char	ut_line[OUT_LINESIZE];
	char	ut_name[OUT_NAMESIZE];
	char	ut_host[OUT_HOSTSIZE];
	long	ut_time;
};

void	usage(void);
void	convert(const char *, int);

/*
 * NB: We cannot convert lastlog yet, but we don't need either.
 */

void
usage(void)
{
  errx(EX_USAGE, "usage: cvt-wtmp [-n] /var/log/wtmp*");
}


int
main(int argc, char **argv)
{
  int errs, i, nflag, rv;

  errs = nflag = 0;
  while ((i = getopt(argc, argv, "n")) != EOF)
    switch (i)
      {
      case 'n':
	nflag++;
	break;

      default:
	errs++;
      }
  argc -= optind;
  argv += optind;

  if (argc <= 0 || errs)
    usage();

  for (;argc > 0; argc--, argv++)
    convert(*argv, nflag);

  return 0;
}

void
convert(const char *name, int nflag)
{
  struct stat sb;
  struct timeval tv[2];
  char xname[1024], yname[1024];
  unsigned char buf[128];	/* large enough to hold one wtmp record */
  int fd1, fd2;
  size_t off;
  int old, new;
  time_t now, early, t;
  struct tm tm;
  struct utmp u;
  struct outmp *ou;

  if (stat(name, &sb) == -1)
    {
      warn("Cannot stat file \"%s\", continuing.", name);
      return;
    }

  now = time(NULL);
  /* some point in time very early, before 386BSD 0.0 */
  tm.tm_sec = 0; tm.tm_min = 0; tm.tm_hour = 0;
  tm.tm_mday = 1; tm.tm_mon = 2; tm.tm_year = 92;
  tm.tm_isdst = 0;
  early = mktime(&tm);

  tv[0].tv_sec = sb.st_atimespec.tv_sec;
  tv[0].tv_usec = sb.st_atimespec.tv_nsec / 1000;
  tv[1].tv_sec = sb.st_mtimespec.tv_sec;
  tv[1].tv_usec = sb.st_mtimespec.tv_nsec / 1000;

  /* unzipping is handled best externally */
  if (strlen(name) > 3 && memcmp(&name[strlen(name) - 3], ".gz", 3) == 0)
    {
      warnx("Cannot handle gzipped files, ignoring \"%s\".", name);
      return;
    }

  (void) snprintf(xname, sizeof xname, "%s.new", name);
  if (!nflag && (fd1 = open(xname, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
    err(EX_CANTCREAT, "Can't create new wtmp file");

  if ((fd2 = open(name, O_RDONLY, 0)) == -1)
    err(EX_UNAVAILABLE, "input file magically disappeared, i'm confused");

  old = new = 0; off = 0;

  while (read(fd2, &buf[off], sizeof(time_t)) == sizeof(time_t))
    {
      t = *((time_t *)&buf[off]);
      off += sizeof(time_t);
      if (t < early || t > now)
	{
	  /* unreasonable, collect another entry */
	  if (off > sizeof buf)
	    {
	      (void) unlink(xname);
	      errx(EX_UNAVAILABLE, "I can't seem to make sense out of file \"%s\"",
		   name);
	    }
	  continue;
	}
      /* time is reasonable, we seem to have collected a full entry */
      if (off == sizeof(struct utmp))
	{
	  /* new wtmp record */
	  new++;
	  if (!nflag)
	    {
	      if (write(fd1, buf, sizeof(struct utmp)) != sizeof(struct utmp))
		err(EX_IOERR, "writing file \"%s\"", xname);
	    }
	}
      else if (off == sizeof(struct outmp))
	{
	  /* old fart */
	  old++;
	  if (!nflag)
	    {
	      ou = (struct outmp *)buf;
	      memset(&u, 0, sizeof u);
	      memcpy(&u.ut_line, ou->ut_line, OUT_LINESIZE);
	      memcpy(&u.ut_name, ou->ut_name, OUT_NAMESIZE);
	      memcpy(&u.ut_host, ou->ut_host, OUT_HOSTSIZE);
	      memcpy(&u.ut_time, &ou->ut_time, sizeof u.ut_time);
	      if (write(fd1, &u, sizeof(struct utmp)) != sizeof(struct utmp))
		err(EX_IOERR, "writing file \"%s\"", xname);
	    }
	}
      else
	{
	  if (off < sizeof(struct outmp))
	    /* try again */
	    continue;
	  warnx("Illegal record in file \"%s\", ignoring.", name);
	  off = 0;
	  continue;
	}
      off = 0;
    }
  close(fd2);

  /*
   * Since the wtmp file is chronologically increasing, we can move
   * the `early' time as we go.  Allow for one hour time-of-day
   * adjustments.
   */
  early = t - 3600;

  printf("File \"%s\": %d old and %d new records found.\n",
	 name, old, new);

  if (nflag)
    return;

  (void) close(fd1);
  (void) snprintf(yname, sizeof yname, "%s.bak", name);

  if (rename(name, yname) == -1)
    err(EX_OSERR, "Cannot rename \"%s\" to \"%s\"", name, yname);
  
  if (rename(xname, name) == -1)
    err(EX_OSERR, "Cannot rename \"%s\" to \"%s\"", xname, name);

  if (utimes(name, tv) == -1)
    warn("Cannot adjust access and modification times for \"%s\"", name);
}

--ELM850660315-20318-0_--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199612151431.PAA24147>