2 * Copyright (c) 1996 Joerg Wunsch
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Heuristics to convert old wtmp format to new one.
34 #include <sys/types.h>
48 #define OUT_NAMESIZE 8
49 #define OUT_LINESIZE 8
50 #define OUT_HOSTSIZE 16
54 char ll_line[OUT_LINESIZE];
55 char ll_host[OUT_HOSTSIZE];
59 char ut_line[OUT_LINESIZE];
60 char ut_name[OUT_NAMESIZE];
61 char ut_host[OUT_HOSTSIZE];
66 void convert(const char *, int, int);
69 * NB: We cannot convert lastlog yet, but we don't need either.
75 errx(EX_USAGE, "usage: cvt-wtmp [-f] [-n] /var/log/wtmp*");
80 main(int argc, char **argv)
82 int errs, i, nflag, forceflag, rv;
84 errs = nflag = forceflag = 0;
85 while ((i = getopt(argc, argv, "fn")) != -1)
102 if (argc <= 0 || errs)
105 for (;argc > 0; argc--, argv++)
106 convert(*argv, nflag, forceflag);
112 convert(const char *name, int nflag, int forceflag)
115 struct timeval tv[2];
116 char xname[1024], yname[1024];
117 unsigned char buf[128]; /* large enough to hold one wtmp record */
119 size_t off, shouldbe;
121 time_t now, early, *t;
125 enum { OLD, NEW } which = OLD; /* what we're defaulting to */
127 if (stat(name, &sb) == -1)
129 warn("Cannot stat file \"%s\", continuing.", name);
134 /* some point in time very early, before 386BSD 0.0 */
135 tm.tm_sec = 0; tm.tm_min = 0; tm.tm_hour = 0;
136 tm.tm_mday = 1; tm.tm_mon = 2; tm.tm_year = 92;
140 tv[0].tv_sec = sb.st_atimespec.tv_sec;
141 tv[0].tv_usec = sb.st_atimespec.tv_nsec / 1000;
142 tv[1].tv_sec = sb.st_mtimespec.tv_sec;
143 tv[1].tv_usec = sb.st_mtimespec.tv_nsec / 1000;
145 /* unzipping is handled best externally */
146 if (strlen(name) > 3 && memcmp(&name[strlen(name) - 3], ".gz", 3) == 0)
148 warnx("Cannot handle gzipped files, ignoring \"%s\".", name);
152 (void) snprintf(xname, sizeof xname, "%s.new", name);
153 if (!nflag && (fd1 = open(xname, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
154 err(EX_CANTCREAT, "Can't create new wtmp file");
156 if ((fd2 = open(name, O_RDONLY, 0)) == -1)
157 err(EX_UNAVAILABLE, "input file magically disappeared, i'm confused");
159 old = new = 0; off = 0;
160 memset(buf, 0, sizeof buf);
162 while (read(fd2, &buf[off], sizeof(time_t)) == sizeof(time_t))
164 t = (time_t *)&buf[off];
165 off += sizeof(time_t);
166 if (off < sizeof(struct outmp))
169 if (*t < early || *t > now)
171 /* unreasonable, collect another entry */
172 if (off > sizeof buf)
176 (void) unlink(xname);
177 errx(EX_UNAVAILABLE, "I can't seem to make sense out of file \"%s\",\n"
178 "Could have forced using -f.",
183 warnx("Record # %d in file \"%s\" seems bogus\n"
184 "(time: %d, previous time: %d, now: %d),\n"
185 "continuing anyway.",
186 old + new + 1, name, *t, early, now);
189 (void)lseek(fd2, sizeof(struct utmp) - sizeof buf, SEEK_CUR);
194 (void)lseek(fd2, sizeof(struct outmp) - sizeof buf, SEEK_CUR);
201 /* time is reasonable, we seem to have collected a full entry */
202 if (off == sizeof(struct utmp))
204 /* new wtmp record */
210 if (write(fd1, buf, sizeof(struct utmp)) != sizeof(struct utmp))
211 err(EX_IOERR, "writing file \"%s\"", xname);
214 else if (off == sizeof(struct outmp))
222 ou = (struct outmp *)buf;
223 memset(&u, 0, sizeof u);
224 memcpy(&u.ut_line, ou->ut_line, OUT_LINESIZE);
225 memcpy(&u.ut_name, ou->ut_name, OUT_NAMESIZE);
226 memcpy(&u.ut_host, ou->ut_host, OUT_HOSTSIZE);
227 memcpy(&u.ut_time, &ou->ut_time, sizeof u.ut_time);
228 if (write(fd1, &u, sizeof(struct utmp)) != sizeof(struct utmp))
229 err(EX_IOERR, "writing file \"%s\"", xname);
236 warnx("Illegal record in file \"%s\", ignoring.", name);
242 warnx("Illegal record in file \"%s\", considering it %s one.",
243 name, (which == OLD? "an old": "a new"));
244 shouldbe = (which == OLD? sizeof(struct outmp): sizeof(struct utmp));
246 (void)read(fd2, &buf[off], shouldbe - off);
248 (void)lseek(fd2, shouldbe - off, SEEK_CUR);
257 * Since the wtmp file is in chronologically acsending order, we
258 * can move the `early' time as we go. Allow for one hour
259 * time-of-day adjustments.
262 memset(buf, 0, sizeof buf);
266 printf("File \"%s\": %d old and %d new records found.\n",
273 (void) snprintf(yname, sizeof yname, "%s.bak", name);
275 if (rename(name, yname) == -1)
276 err(EX_OSERR, "Cannot rename \"%s\" to \"%s\"", name, yname);
278 if (rename(xname, name) == -1)
279 err(EX_OSERR, "Cannot rename \"%s\" to \"%s\"", xname, name);
281 if (utimes(name, tv) == -1)
282 warn("Cannot adjust access and modification times for \"%s\"", name);