1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
* 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.
*
* $FreeBSD$
*
*/
/*
* 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, int);
/*
* NB: We cannot convert lastlog yet, but we don't need either.
*/
void
usage(void)
{
errx(EX_USAGE, "usage: cvt-wtmp [-f] [-n] /var/log/wtmp*");
}
int
main(int argc, char **argv)
{
int errs, i, nflag, forceflag, rv;
errs = nflag = forceflag = 0;
while ((i = getopt(argc, argv, "fn")) != -1)
switch (i)
{
case 'f':
forceflag++;
break;
case 'n':
nflag++;
break;
default:
errs++;
}
argc -= optind;
argv += optind;
if (argc <= 0 || errs)
usage();
for (;argc > 0; argc--, argv++)
convert(*argv, nflag, forceflag);
return 0;
}
void
convert(const char *name, int nflag, int forceflag)
{
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, shouldbe;
int old, new;
time_t now, early, *t;
struct tm tm;
struct utmp u;
struct outmp *ou;
enum { OLD, NEW } which = OLD; /* what we're defaulting to */
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;
memset(buf, 0, sizeof buf);
while (read(fd2, &buf[off], sizeof(time_t)) == sizeof(time_t))
{
t = (time_t *)&buf[off];
off += sizeof(time_t);
if (off < sizeof(struct outmp))
/* go on */
continue;
if (*t < early || *t > now)
{
/* unreasonable, collect another entry */
if (off > sizeof buf)
{
if (!forceflag)
{
(void) unlink(xname);
errx(EX_UNAVAILABLE, "I can't seem to make sense out of file \"%s\",\n"
"Could have forced using -f.",
name);
}
else
{
warnx("Record # %d in file \"%s\" seems bogus\n"
"(time: %d, previous time: %d, now: %d),\n"
"continuing anyway.",
old + new + 1, name, *t, early, now);
if (which == NEW)
{
(void)lseek(fd2, sizeof(struct utmp) - sizeof buf, SEEK_CUR);
goto write_new;
}
else
{
(void)lseek(fd2, sizeof(struct outmp) - sizeof buf, SEEK_CUR);
goto write_old;
}
}
}
continue;
}
/* time is reasonable, we seem to have collected a full entry */
if (off == sizeof(struct utmp))
{
/* new wtmp record */
which = NEW;
write_new:
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 */
which = OLD;
write_old:
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 (!forceflag)
{
warnx("Illegal record in file \"%s\", ignoring.", name);
off = 0;
continue;
}
else
{
warnx("Illegal record in file \"%s\", considering it %s one.",
name, (which == OLD? "an old": "a new"));
shouldbe = (which == OLD? sizeof(struct outmp): sizeof(struct utmp));
if (off < shouldbe)
(void)read(fd2, &buf[off], shouldbe - off);
else
(void)lseek(fd2, shouldbe - off, SEEK_CUR);
if (which == OLD)
goto write_old;
else
goto write_new;
}
}
off = 0;
/*
* Since the wtmp file is in chronologically acsending order, we
* can move the `early' time as we go. Allow for one hour
* time-of-day adjustments.
*/
early = *t - 3600;
memset(buf, 0, sizeof buf);
}
close(fd2);
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);
}
|