Initial import from FreeBSD RELENG_4:
[games.git] / tools / 3.0-upgrade / cvt-wtmp.c
1 /*
2  * Copyright (c) 1996 Joerg Wunsch
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
25  *
26  * $FreeBSD: src/tools/3.0-upgrade/cvt-wtmp.c,v 1.6 1999/08/28 00:54:20 peter Exp $
27  *
28  */
29
30 /*
31  * Heuristics to convert old wtmp format to new one.
32  */
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37
38 #include <err.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sysexits.h>
44 #include <unistd.h>
45 #include <utmp.h>
46
47
48 #define OUT_NAMESIZE    8
49 #define OUT_LINESIZE    8
50 #define OUT_HOSTSIZE    16
51
52 struct olastlog {
53         time_t  ll_time;
54         char    ll_line[OUT_LINESIZE];
55         char    ll_host[OUT_HOSTSIZE];
56 };
57
58 struct outmp {
59         char    ut_line[OUT_LINESIZE];
60         char    ut_name[OUT_NAMESIZE];
61         char    ut_host[OUT_HOSTSIZE];
62         long    ut_time;
63 };
64
65 void    usage(void);
66 void    convert(const char *, int, int);
67
68 /*
69  * NB: We cannot convert lastlog yet, but we don't need either.
70  */
71
72 void
73 usage(void)
74 {
75   errx(EX_USAGE, "usage: cvt-wtmp [-f] [-n] /var/log/wtmp*");
76 }
77
78
79 int
80 main(int argc, char **argv)
81 {
82   int errs, i, nflag, forceflag, rv;
83
84   errs = nflag = forceflag = 0;
85   while ((i = getopt(argc, argv, "fn")) != -1)
86     switch (i)
87       {
88       case 'f':
89         forceflag++;
90         break;
91
92       case 'n':
93         nflag++;
94         break;
95
96       default:
97         errs++;
98       }
99   argc -= optind;
100   argv += optind;
101
102   if (argc <= 0 || errs)
103     usage();
104
105   for (;argc > 0; argc--, argv++)
106     convert(*argv, nflag, forceflag);
107
108   return 0;
109 }
110
111 void
112 convert(const char *name, int nflag, int forceflag)
113 {
114   struct stat sb;
115   struct timeval tv[2];
116   char xname[1024], yname[1024];
117   unsigned char buf[128];       /* large enough to hold one wtmp record */
118   int fd1, fd2;
119   size_t off, shouldbe;
120   int old, new;
121   time_t now, early, *t;
122   struct tm tm;
123   struct utmp u;
124   struct outmp *ou;
125   enum { OLD, NEW } which = OLD; /* what we're defaulting to */
126
127   if (stat(name, &sb) == -1)
128     {
129       warn("Cannot stat file \"%s\", continuing.", name);
130       return;
131     }
132
133   now = time(NULL);
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;
137   tm.tm_isdst = 0;
138   early = mktime(&tm);
139
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;
144
145   /* unzipping is handled best externally */
146   if (strlen(name) > 3 && memcmp(&name[strlen(name) - 3], ".gz", 3) == 0)
147     {
148       warnx("Cannot handle gzipped files, ignoring \"%s\".", name);
149       return;
150     }
151
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");
155
156   if ((fd2 = open(name, O_RDONLY, 0)) == -1)
157     err(EX_UNAVAILABLE, "input file magically disappeared, i'm confused");
158
159   old = new = 0; off = 0;
160   memset(buf, 0, sizeof buf);
161
162   while (read(fd2, &buf[off], sizeof(time_t)) == sizeof(time_t))
163     {
164       t = (time_t *)&buf[off];
165       off += sizeof(time_t);
166       if (off < sizeof(struct outmp))
167         /* go on */
168         continue;
169       if (*t < early || *t > now)
170         {
171           /* unreasonable, collect another entry */
172           if (off > sizeof buf)
173             {
174               if (!forceflag)
175                 {
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.",
179                      name);
180                 }
181               else
182                 {
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);
187                   if (which == NEW)
188                     {
189                       (void)lseek(fd2, sizeof(struct utmp) - sizeof buf, SEEK_CUR);
190                       goto write_new;
191                     }
192                   else
193                     {
194                       (void)lseek(fd2, sizeof(struct outmp) - sizeof buf, SEEK_CUR);
195                       goto write_old;
196                     }
197                 }
198             }
199           continue;
200         }
201       /* time is reasonable, we seem to have collected a full entry */
202       if (off == sizeof(struct utmp))
203         {
204           /* new wtmp record */
205           which = NEW;
206         write_new:
207           new++;
208           if (!nflag)
209             {
210               if (write(fd1, buf, sizeof(struct utmp)) != sizeof(struct utmp))
211                 err(EX_IOERR, "writing file \"%s\"", xname);
212             }
213         }
214       else if (off == sizeof(struct outmp))
215         {
216           /* old fart */
217           which = OLD;
218         write_old:
219           old++;
220           if (!nflag)
221             {
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);
230             }
231         }
232       else
233         {
234           if (!forceflag)
235             {
236               warnx("Illegal record in file \"%s\", ignoring.", name);
237               off = 0;
238               continue;
239             }
240           else
241             {
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));
245               if (off < shouldbe)
246                 (void)read(fd2, &buf[off], shouldbe - off);
247               else
248                 (void)lseek(fd2, shouldbe - off, SEEK_CUR);
249               if (which == OLD)
250                 goto write_old;
251               else
252                 goto write_new;
253             }
254         }
255       off = 0;
256       /*
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.
260        */
261       early = *t - 3600;
262       memset(buf, 0, sizeof buf);
263     }
264   close(fd2);
265
266   printf("File \"%s\": %d old and %d new records found.\n",
267          name, old, new);
268
269   if (nflag)
270     return;
271
272   (void) close(fd1);
273   (void) snprintf(yname, sizeof yname, "%s.bak", name);
274
275   if (rename(name, yname) == -1)
276     err(EX_OSERR, "Cannot rename \"%s\" to \"%s\"", name, yname);
277   
278   if (rename(xname, name) == -1)
279     err(EX_OSERR, "Cannot rename \"%s\" to \"%s\"", xname, name);
280
281   if (utimes(name, tv) == -1)
282     warn("Cannot adjust access and modification times for \"%s\"", name);
283 }
284