summaryrefslogtreecommitdiff
path: root/usr/src/cmd/acct/wtmpfix.c
blob: 56ad06c2ad1f5fdc058f10674a9ee5ccd6f20caf (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"
/*
 * wtmpfix - adjust wtmpx file and remove date changes.
 *	wtmpfix <wtmpx1 >wtmpx2
 *
 *	code are added to really fix wtmpx if it is corrupted ..
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include "acctdef.h"
#include <utmpx.h>
#include <signal.h>
#include <time.h>
#include <ctype.h>
#include <locale.h>
#include <limits.h>
#include <stdlib.h>

#define	MAXRUNTIME	3600	/* time out after 1 hour */
#define	DAYEPOCH	(60 * 60 * 24)
#define	wout(f, w)	 fwrite(w, sizeof (struct utmpx), 1, f);

FILE	*Wtmpx, *Opw;
FILE	*fp;
char	Ofile[]	= "/tmp/wXXXXXX";
static char time_buf[50];

struct	dtab
{
	off_t	d_off1;		/* file offset start */
	off_t	d_off2;		/* file offset stop */
	time_t	d_adj;		/* time adjustment */
	struct dtab *d_ndp;	/* next record */
};

struct	dtab	*Fdp;		/* list header */
struct	dtab	*Ldp;		/* list trailer */

time_t 	lastmonth, nextmonth;
off_t	recno;

struct	utmpx	Ut, Ut2;

int	year, month;
int 	ch;
int	n;
int	multimode;		/* multi user mode	 WHCC */

static int winp(FILE *, struct utmpx *);
static void mkdtab(off_t);
static void setdtab(off_t, struct utmpx *, struct utmpx *);
static void adjust(off_t, struct utmpx *);
static int invalid(char *);
static void intr(int) __NORETURN;
static void scanfile(void);
static int inrange(void);
static void wabort(int);

int
main(int argc, char **argv)
{
	time_t tloc;
	struct tm *tmp;
	int fd;

	(void) setlocale(LC_ALL, "");
	setbuf(stdout, NULL);
	alarm(MAXRUNTIME);

	if (signal(SIGALRM, wabort) == SIG_ERR) {
		perror("signal");
		return (1);
	}
	if (signal(SIGINT, intr) == SIG_ERR) {
		perror("signal");
		return (1);
	}

	time(&tloc);
	tmp = localtime(&tloc);
	year = tmp->tm_year;
	month = tmp->tm_mon + 1;
	lastmonth = ((year + 1900 - 1970) * 365 +
	    (month - 1) * 30) * DAYEPOCH;
	nextmonth = ((year + 1900 - 1970) * 365 +
	    (month + 1) * 30) * DAYEPOCH;

	if (argc < 2) {
		argv[argc] = "-";
		argc++;
	}

	if ((fd = mkstemp(Ofile)) == -1) {
		fprintf(stderr, "cannot make temporary: %s\n", Ofile);
		intr(0);
	}

	if ((Opw = fdopen(fd, "w")) == NULL) {
		fprintf(stderr, "cannot open temporary: %s\n", Ofile);
		intr(0);
	}

	while (--argc > 0) {
		argv++;
		if (strcmp(*argv, "-") == 0)
			Wtmpx = stdin;
		else if ((Wtmpx = fopen(*argv, "r")) == NULL) {
			fprintf(stderr, "Cannot open: %s\n", *argv);
			intr(0);
		}
		scanfile();

		if (Wtmpx != stdin)
			fclose(Wtmpx);
	}
	fclose(Opw);

	if ((Opw = fopen(Ofile, "r")) == NULL) {
		fprintf(stderr, "Cannot read from temp: %s\n", Ofile);
		intr(0);
	}
	recno = 0;
	while (winp(Opw, &Ut)) {
		adjust(recno, &Ut);
		recno += sizeof (struct utmpx);
		wout(stdout, &Ut);
	}
	fclose(Opw);
	unlink(Ofile);
	return (0);
}

static int
winp(FILE *f, struct utmpx *w)
{
	if (fread(w, sizeof (struct utmpx), 1, f) != 1)
		return (0);
	if ((w->ut_type >= EMPTY) && (w->ut_type <= UTMAXTYPE))
		return (1);
	else {
		fprintf(stderr, "Bad file at offset %ld\n",
			ftell(f) - sizeof (struct utmpx));
		cftime(time_buf, DATE_FMT, &w->ut_xtime);
		fprintf(stderr, "%-12s %-8s %lu %s",
			w->ut_line, w->ut_user, w->ut_xtime, time_buf);
		intr(0);
	}
	/* NOTREACHED */
}

static void
mkdtab(off_t p)
{

	struct dtab *dp;

	dp = Ldp;
	if (dp == NULL) {
		dp = calloc(sizeof (struct dtab), 1);
		if (dp == NULL) {
			fprintf(stderr, "out of core\n");
			intr(0);
		}
		Fdp = Ldp = dp;
	}
	dp->d_off1 = p;
}

static void
setdtab(off_t p, struct utmpx *w1, struct utmpx *w2)
{
	struct dtab *dp;

	if ((dp = Ldp) == NULL) {
		fprintf(stderr, "no dtab\n");
		intr(0);
	}
	dp->d_off2 = p;
	dp->d_adj = w2->ut_xtime - w1->ut_xtime;
	if ((Ldp = calloc(sizeof (struct dtab), 1)) == NULL) {
		fprintf(stderr, "out of core\n");
		intr(0);
	}
	Ldp->d_off1 = dp->d_off1;
	dp->d_ndp = Ldp;
}

static void
adjust(off_t p, struct utmpx *w)
{

	off_t pp;
	struct dtab *dp;

	pp = p;

	for (dp = Fdp; dp != NULL; dp = dp->d_ndp) {
		if (dp->d_adj == 0)
			continue;
		if (pp >= dp->d_off1 && pp < dp->d_off2)
			w->ut_xtime += dp->d_adj;
	}
}

/*
 *	invalid() determines whether the name field adheres to
 *	the criteria set forth in acctcon1.  If the name violates
 *	conventions, it returns a truth value meaning the name is
 *	invalid; if the name is okay, it returns false indicating
 *	the name is not invalid.
 */

static int
invalid(char *name)
{
	int	i;

	for (i = 0; i < NSZ; i++) {
		if (name[i] == '\0')
			return (VALID);
		if (! (isalnum(name[i]) || (name[i] == '$') ||
		    (name[i] == ' ') || (name[i] == '.') ||
		    (name[i] == '_') || (name[i] == '-'))) {
			return (INVALID);
		}
	}
	return (VALID);
}

static void
intr(int sig)
{
	signal(SIGINT, SIG_IGN);
	unlink(Ofile);
	exit(1);
}

/*
 * scanfile:
 * 1)  	reads the file, to see if the record is within reasonable
 * 	range; if not, then it will scan the file, delete foreign stuff.
 * 2)   enter setdtab if in multiuser mode
 * 3)   change bad login names to INVALID
 */

static void
scanfile()
{
	while ((n = fread(&Ut, sizeof (Ut), 1, Wtmpx)) > 0) {
		if (n == 0) {
			unlink(Ofile);
			exit(0);
		}
		if (!inrange()) {
			for (;;) {
				if (fseek(Wtmpx,
				    -(off_t)sizeof (Ut), 1) != 0) {
					perror("seek error\n");
					exit(1);
				}
				if ((ch = getc(Wtmpx)) == EOF) {
					perror("read\n");
					exit(1);
				}
				fprintf(stderr, "checking offset %lo\n",
				    ftell(Wtmpx));
				if (fread(&Ut, sizeof (Ut), 1, Wtmpx) == 0) {
					exit(1);
				}
				if (inrange())
					break;
			}
		}
		/* Now we have a good utmpx record, do more processing */

#define	UTYPE	Ut.ut_type
#define	ULINE	Ut.ut_line

			if (recno == 0 || UTYPE == BOOT_TIME)
				mkdtab(recno);
			if (UTYPE == RUN_LVL) {
				if (strncmp(ULINE, "run-level S", 11) == 0)
					multimode = 0;
				if (strncmp(ULINE, "run-level 2", 11) == 0)
					multimode++;
			}
			if (invalid(Ut.ut_name)) {
				fprintf(stderr,
				    "wtmpfix: logname \"%*.*s\" changed "
				    "to \"INVALID\"\n", OUTPUT_NSZ,
				    OUTPUT_NSZ, Ut.ut_name);
				(void) strncpy(Ut.ut_name, "INVALID", NSZ);
			}
			if (UTYPE == OLD_TIME) {
				if (!winp(Wtmpx, &Ut2)) {
					fprintf(stderr, "Input truncated at "
					    "offset %ld\n", recno);
					intr(0);
				}
				if (Ut2.ut_type != NEW_TIME) {
					fprintf(stderr, "New date expected at "
					    "offset %ld", recno);
					intr(0);
				}
				if (multimode)  /* multiuser */
					setdtab(recno, &Ut, &Ut2);
				recno += (2 * sizeof (struct utmpx));
				wout(Opw, &Ut);
				wout(Opw, &Ut2);
				continue;
			}
			wout(Opw, &Ut);
			recno += sizeof (struct utmpx);
	}
}

static int
inrange()
{
	if ((strcmp(Ut.ut_line, RUNLVL_MSG) == 0) ||
	    (strcmp(Ut.ut_line, BOOT_MSG) == 0) ||
	    (strcmp(Ut.ut_line, "acctg on") == 0) ||
	    (strcmp(Ut.ut_line, OTIME_MSG) == 0) ||
	    (strcmp(Ut.ut_line, NTIME_MSG) == 0))
			return (1);

	if (Ut.ut_id != 0 &&
		Ut.ut_xtime > 0 &&
		Ut.ut_xtime > lastmonth &&
		Ut.ut_xtime < nextmonth &&
		Ut.ut_type >= EMPTY &&
		Ut.ut_type <= UTMAXTYPE &&
		Ut.ut_pid >= 0)
		return (1);

	return (0);
}

static void
wabort(int sig)
{
	fprintf(stderr, "give up\n");
	exit(1);
}