xref: /titanic_54/usr/src/cmd/acct/wtmpfix.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1993-2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * wtmpfix - adjust wtmpx file and remove date changes.
34*7c478bd9Sstevel@tonic-gate  *	wtmpfix <wtmpx1 >wtmpx2
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  *	code are added to really fix wtmpx if it is corrupted ..
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <stdio.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
42*7c478bd9Sstevel@tonic-gate #include "acctdef.h"
43*7c478bd9Sstevel@tonic-gate #include <utmpx.h>
44*7c478bd9Sstevel@tonic-gate #include <signal.h>
45*7c478bd9Sstevel@tonic-gate #include <time.h>
46*7c478bd9Sstevel@tonic-gate #include <ctype.h>
47*7c478bd9Sstevel@tonic-gate #include <locale.h>
48*7c478bd9Sstevel@tonic-gate #include <limits.h>
49*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #define	MAXRUNTIME	3600	/* time out after 1 hour */
52*7c478bd9Sstevel@tonic-gate #define	DAYEPOCH	(60 * 60 * 24)
53*7c478bd9Sstevel@tonic-gate #define	wout(f, w)	 fwrite(w, sizeof (struct utmpx), 1, f);
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate FILE	*Wtmpx, *Opw;
56*7c478bd9Sstevel@tonic-gate FILE	*fp;
57*7c478bd9Sstevel@tonic-gate char	Ofile[]	= "/tmp/wXXXXXX";
58*7c478bd9Sstevel@tonic-gate static char time_buf[50];
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate struct	dtab
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate 	off_t	d_off1;		/* file offset start */
63*7c478bd9Sstevel@tonic-gate 	off_t	d_off2;		/* file offset stop */
64*7c478bd9Sstevel@tonic-gate 	time_t	d_adj;		/* time adjustment */
65*7c478bd9Sstevel@tonic-gate 	struct dtab *d_ndp;	/* next record */
66*7c478bd9Sstevel@tonic-gate };
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate struct	dtab	*Fdp;		/* list header */
69*7c478bd9Sstevel@tonic-gate struct	dtab	*Ldp;		/* list trailer */
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate time_t 	lastmonth, nextmonth;
72*7c478bd9Sstevel@tonic-gate off_t	recno;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate struct	utmpx	Ut, Ut2;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate int	year, month;
77*7c478bd9Sstevel@tonic-gate int 	ch;
78*7c478bd9Sstevel@tonic-gate int	n;
79*7c478bd9Sstevel@tonic-gate int	multimode;		/* multi user mode	 WHCC */
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate static int winp(FILE *, struct utmpx *);
82*7c478bd9Sstevel@tonic-gate static void mkdtab(off_t);
83*7c478bd9Sstevel@tonic-gate static void setdtab(off_t, struct utmpx *, struct utmpx *);
84*7c478bd9Sstevel@tonic-gate static void adjust(off_t, struct utmpx *);
85*7c478bd9Sstevel@tonic-gate static int invalid(char *);
86*7c478bd9Sstevel@tonic-gate static void intr(int);
87*7c478bd9Sstevel@tonic-gate static void scanfile(void);
88*7c478bd9Sstevel@tonic-gate static int inrange(void);
89*7c478bd9Sstevel@tonic-gate static void wabort(int);
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate int
92*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate 	time_t tloc;
95*7c478bd9Sstevel@tonic-gate 	struct tm *tmp;
96*7c478bd9Sstevel@tonic-gate 	int fd;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
99*7c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
100*7c478bd9Sstevel@tonic-gate 	alarm(MAXRUNTIME);
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	if (signal(SIGALRM, wabort) == SIG_ERR) {
103*7c478bd9Sstevel@tonic-gate 		perror("signal");
104*7c478bd9Sstevel@tonic-gate 		return (1);
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, intr) == SIG_ERR) {
107*7c478bd9Sstevel@tonic-gate 		perror("signal");
108*7c478bd9Sstevel@tonic-gate 		return (1);
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	time(&tloc);
112*7c478bd9Sstevel@tonic-gate 	tmp = localtime(&tloc);
113*7c478bd9Sstevel@tonic-gate 	year = tmp->tm_year;
114*7c478bd9Sstevel@tonic-gate 	month = tmp->tm_mon + 1;
115*7c478bd9Sstevel@tonic-gate 	lastmonth = ((year + 1900 - 1970) * 365 +
116*7c478bd9Sstevel@tonic-gate 	    (month - 1) * 30) * DAYEPOCH;
117*7c478bd9Sstevel@tonic-gate 	nextmonth = ((year + 1900 - 1970) * 365 +
118*7c478bd9Sstevel@tonic-gate 	    (month + 1) * 30) * DAYEPOCH;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	if (argc < 2) {
121*7c478bd9Sstevel@tonic-gate 		argv[argc] = "-";
122*7c478bd9Sstevel@tonic-gate 		argc++;
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	if ((fd = mkstemp(Ofile)) == -1) {
126*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "cannot make temporary: %s\n", Ofile);
127*7c478bd9Sstevel@tonic-gate 		intr(0);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if ((Opw = fdopen(fd, "w")) == NULL) {
131*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "cannot open temporary: %s\n", Ofile);
132*7c478bd9Sstevel@tonic-gate 		intr(0);
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	while (--argc > 0) {
136*7c478bd9Sstevel@tonic-gate 		argv++;
137*7c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-") == 0)
138*7c478bd9Sstevel@tonic-gate 			Wtmpx = stdin;
139*7c478bd9Sstevel@tonic-gate 		else if ((Wtmpx = fopen(*argv, "r")) == NULL) {
140*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Cannot open: %s\n", *argv);
141*7c478bd9Sstevel@tonic-gate 			intr(0);
142*7c478bd9Sstevel@tonic-gate 		}
143*7c478bd9Sstevel@tonic-gate 		scanfile();
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 		if (Wtmpx != stdin)
146*7c478bd9Sstevel@tonic-gate 			fclose(Wtmpx);
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 	fclose(Opw);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	if ((Opw = fopen(Ofile, "r")) == NULL) {
151*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Cannot read from temp: %s\n", Ofile);
152*7c478bd9Sstevel@tonic-gate 		intr(0);
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 	recno = 0;
155*7c478bd9Sstevel@tonic-gate 	while (winp(Opw, &Ut)) {
156*7c478bd9Sstevel@tonic-gate 		adjust(recno, &Ut);
157*7c478bd9Sstevel@tonic-gate 		recno += sizeof (struct utmpx);
158*7c478bd9Sstevel@tonic-gate 		wout(stdout, &Ut);
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 	fclose(Opw);
161*7c478bd9Sstevel@tonic-gate 	unlink(Ofile);
162*7c478bd9Sstevel@tonic-gate 	return (0);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate static int
166*7c478bd9Sstevel@tonic-gate winp(FILE *f, struct utmpx *w)
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate 	if (fread(w, sizeof (struct utmpx), 1, f) != 1)
169*7c478bd9Sstevel@tonic-gate 		return (0);
170*7c478bd9Sstevel@tonic-gate 	if ((w->ut_type >= EMPTY) && (w->ut_type <= UTMAXTYPE))
171*7c478bd9Sstevel@tonic-gate 		return (1);
172*7c478bd9Sstevel@tonic-gate 	else {
173*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Bad file at offset %ld\n",
174*7c478bd9Sstevel@tonic-gate 			ftell(f) - sizeof (struct utmpx));
175*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &w->ut_xtime);
176*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%-12s %-8s %lu %s",
177*7c478bd9Sstevel@tonic-gate 			w->ut_line, w->ut_user, w->ut_xtime, time_buf);
178*7c478bd9Sstevel@tonic-gate 		intr(0);
179*7c478bd9Sstevel@tonic-gate 	}
180*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate static void
184*7c478bd9Sstevel@tonic-gate mkdtab(off_t p)
185*7c478bd9Sstevel@tonic-gate {
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	struct dtab *dp;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	dp = Ldp;
190*7c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
191*7c478bd9Sstevel@tonic-gate 		dp = calloc(sizeof (struct dtab), 1);
192*7c478bd9Sstevel@tonic-gate 		if (dp == NULL) {
193*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "out of core\n");
194*7c478bd9Sstevel@tonic-gate 			intr(0);
195*7c478bd9Sstevel@tonic-gate 		}
196*7c478bd9Sstevel@tonic-gate 		Fdp = Ldp = dp;
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 	dp->d_off1 = p;
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate static void
202*7c478bd9Sstevel@tonic-gate setdtab(off_t p, struct utmpx *w1, struct utmpx *w2)
203*7c478bd9Sstevel@tonic-gate {
204*7c478bd9Sstevel@tonic-gate 	struct dtab *dp;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	if ((dp = Ldp) == NULL) {
207*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "no dtab\n");
208*7c478bd9Sstevel@tonic-gate 		intr(0);
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 	dp->d_off2 = p;
211*7c478bd9Sstevel@tonic-gate 	dp->d_adj = w2->ut_xtime - w1->ut_xtime;
212*7c478bd9Sstevel@tonic-gate 	if ((Ldp = calloc(sizeof (struct dtab), 1)) == NULL) {
213*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "out of core\n");
214*7c478bd9Sstevel@tonic-gate 		intr(0);
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 	Ldp->d_off1 = dp->d_off1;
217*7c478bd9Sstevel@tonic-gate 	dp->d_ndp = Ldp;
218*7c478bd9Sstevel@tonic-gate }
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate static void
221*7c478bd9Sstevel@tonic-gate adjust(off_t p, struct utmpx *w)
222*7c478bd9Sstevel@tonic-gate {
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	off_t pp;
225*7c478bd9Sstevel@tonic-gate 	struct dtab *dp;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	pp = p;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	for (dp = Fdp; dp != NULL; dp = dp->d_ndp) {
230*7c478bd9Sstevel@tonic-gate 		if (dp->d_adj == 0)
231*7c478bd9Sstevel@tonic-gate 			continue;
232*7c478bd9Sstevel@tonic-gate 		if (pp >= dp->d_off1 && pp < dp->d_off2)
233*7c478bd9Sstevel@tonic-gate 			w->ut_xtime += dp->d_adj;
234*7c478bd9Sstevel@tonic-gate 	}
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate /*
238*7c478bd9Sstevel@tonic-gate  *	invalid() determines whether the name field adheres to
239*7c478bd9Sstevel@tonic-gate  *	the criteria set forth in acctcon1.  If the name violates
240*7c478bd9Sstevel@tonic-gate  *	conventions, it returns a truth value meaning the name is
241*7c478bd9Sstevel@tonic-gate  *	invalid; if the name is okay, it returns false indicating
242*7c478bd9Sstevel@tonic-gate  *	the name is not invalid.
243*7c478bd9Sstevel@tonic-gate  */
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate static int
246*7c478bd9Sstevel@tonic-gate invalid(char *name)
247*7c478bd9Sstevel@tonic-gate {
248*7c478bd9Sstevel@tonic-gate 	int	i;
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < NSZ; i++) {
251*7c478bd9Sstevel@tonic-gate 		if (name[i] == '\0')
252*7c478bd9Sstevel@tonic-gate 			return (VALID);
253*7c478bd9Sstevel@tonic-gate 		if (! (isalnum(name[i]) || (name[i] == '$') ||
254*7c478bd9Sstevel@tonic-gate 		    (name[i] == ' ') || (name[i] == '.') ||
255*7c478bd9Sstevel@tonic-gate 		    (name[i] == '_') || (name[i] == '-'))) {
256*7c478bd9Sstevel@tonic-gate 			return (INVALID);
257*7c478bd9Sstevel@tonic-gate 		}
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 	return (VALID);
260*7c478bd9Sstevel@tonic-gate }
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate static void
263*7c478bd9Sstevel@tonic-gate intr(int sig)
264*7c478bd9Sstevel@tonic-gate {
265*7c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
266*7c478bd9Sstevel@tonic-gate 	unlink(Ofile);
267*7c478bd9Sstevel@tonic-gate 	exit(1);
268*7c478bd9Sstevel@tonic-gate }
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate /*
271*7c478bd9Sstevel@tonic-gate  * scanfile:
272*7c478bd9Sstevel@tonic-gate  * 1)  	reads the file, to see if the record is within reasonable
273*7c478bd9Sstevel@tonic-gate  * 	range; if not, then it will scan the file, delete foreign stuff.
274*7c478bd9Sstevel@tonic-gate  * 2)   enter setdtab if in multiuser mode
275*7c478bd9Sstevel@tonic-gate  * 3)   change bad login names to INVALID
276*7c478bd9Sstevel@tonic-gate  */
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate static void
279*7c478bd9Sstevel@tonic-gate scanfile()
280*7c478bd9Sstevel@tonic-gate {
281*7c478bd9Sstevel@tonic-gate 	while ((n = fread(&Ut, sizeof (Ut), 1, Wtmpx)) > 0) {
282*7c478bd9Sstevel@tonic-gate 		if (n == 0) {
283*7c478bd9Sstevel@tonic-gate 			unlink(Ofile);
284*7c478bd9Sstevel@tonic-gate 			exit(0);
285*7c478bd9Sstevel@tonic-gate 		}
286*7c478bd9Sstevel@tonic-gate 		if (!inrange()) {
287*7c478bd9Sstevel@tonic-gate 			for (;;) {
288*7c478bd9Sstevel@tonic-gate 				if (fseek(Wtmpx,
289*7c478bd9Sstevel@tonic-gate 				    -(off_t)sizeof (Ut), 1) != 0) {
290*7c478bd9Sstevel@tonic-gate 					perror("seek error\n");
291*7c478bd9Sstevel@tonic-gate 					exit(1);
292*7c478bd9Sstevel@tonic-gate 				}
293*7c478bd9Sstevel@tonic-gate 				if ((ch = getc(Wtmpx)) == EOF) {
294*7c478bd9Sstevel@tonic-gate 					perror("read\n");
295*7c478bd9Sstevel@tonic-gate 					exit(1);
296*7c478bd9Sstevel@tonic-gate 				}
297*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "checking offset %lo\n",
298*7c478bd9Sstevel@tonic-gate 				    ftell(Wtmpx));
299*7c478bd9Sstevel@tonic-gate 				if (fread(&Ut, sizeof (Ut), 1, Wtmpx) == 0) {
300*7c478bd9Sstevel@tonic-gate 					exit(1);
301*7c478bd9Sstevel@tonic-gate 				}
302*7c478bd9Sstevel@tonic-gate 				if (inrange())
303*7c478bd9Sstevel@tonic-gate 					break;
304*7c478bd9Sstevel@tonic-gate 			}
305*7c478bd9Sstevel@tonic-gate 		}
306*7c478bd9Sstevel@tonic-gate 		/* Now we have a good utmpx record, do more processing */
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate #define	UTYPE	Ut.ut_type
309*7c478bd9Sstevel@tonic-gate #define	ULINE	Ut.ut_line
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 			if (recno == 0 || UTYPE == BOOT_TIME)
312*7c478bd9Sstevel@tonic-gate 				mkdtab(recno);
313*7c478bd9Sstevel@tonic-gate 			if (UTYPE == RUN_LVL) {
314*7c478bd9Sstevel@tonic-gate 				if (strncmp(ULINE, "run-level S", 11) == 0)
315*7c478bd9Sstevel@tonic-gate 					multimode = 0;
316*7c478bd9Sstevel@tonic-gate 				if (strncmp(ULINE, "run-level 2", 11) == 0)
317*7c478bd9Sstevel@tonic-gate 					multimode++;
318*7c478bd9Sstevel@tonic-gate 			}
319*7c478bd9Sstevel@tonic-gate 			if (invalid(Ut.ut_name)) {
320*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
321*7c478bd9Sstevel@tonic-gate 				    "wtmpfix: logname \"%*.*s\" changed "
322*7c478bd9Sstevel@tonic-gate 				    "to \"INVALID\"\n", OUTPUT_NSZ,
323*7c478bd9Sstevel@tonic-gate 				    OUTPUT_NSZ, Ut.ut_name);
324*7c478bd9Sstevel@tonic-gate 				(void) strncpy(Ut.ut_name, "INVALID", NSZ);
325*7c478bd9Sstevel@tonic-gate 			}
326*7c478bd9Sstevel@tonic-gate 			if (UTYPE == OLD_TIME) {
327*7c478bd9Sstevel@tonic-gate 				if (!winp(Wtmpx, &Ut2)) {
328*7c478bd9Sstevel@tonic-gate 					fprintf(stderr, "Input truncated at "
329*7c478bd9Sstevel@tonic-gate 					    "offset %ld\n", recno);
330*7c478bd9Sstevel@tonic-gate 					intr(0);
331*7c478bd9Sstevel@tonic-gate 				}
332*7c478bd9Sstevel@tonic-gate 				if (Ut2.ut_type != NEW_TIME) {
333*7c478bd9Sstevel@tonic-gate 					fprintf(stderr, "New date expected at "
334*7c478bd9Sstevel@tonic-gate 					    "offset %ld", recno);
335*7c478bd9Sstevel@tonic-gate 					intr(0);
336*7c478bd9Sstevel@tonic-gate 				}
337*7c478bd9Sstevel@tonic-gate 				if (multimode)  /* multiuser */
338*7c478bd9Sstevel@tonic-gate 					setdtab(recno, &Ut, &Ut2);
339*7c478bd9Sstevel@tonic-gate 				recno += (2 * sizeof (struct utmpx));
340*7c478bd9Sstevel@tonic-gate 				wout(Opw, &Ut);
341*7c478bd9Sstevel@tonic-gate 				wout(Opw, &Ut2);
342*7c478bd9Sstevel@tonic-gate 				continue;
343*7c478bd9Sstevel@tonic-gate 			}
344*7c478bd9Sstevel@tonic-gate 			wout(Opw, &Ut);
345*7c478bd9Sstevel@tonic-gate 			recno += sizeof (struct utmpx);
346*7c478bd9Sstevel@tonic-gate 	}
347*7c478bd9Sstevel@tonic-gate }
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate static int
350*7c478bd9Sstevel@tonic-gate inrange()
351*7c478bd9Sstevel@tonic-gate {
352*7c478bd9Sstevel@tonic-gate 	if ((strcmp(Ut.ut_line, RUNLVL_MSG) == 0) ||
353*7c478bd9Sstevel@tonic-gate 	    (strcmp(Ut.ut_line, BOOT_MSG) == 0) ||
354*7c478bd9Sstevel@tonic-gate 	    (strcmp(Ut.ut_line, "acctg on") == 0) ||
355*7c478bd9Sstevel@tonic-gate 	    (strcmp(Ut.ut_line, OTIME_MSG) == 0) ||
356*7c478bd9Sstevel@tonic-gate 	    (strcmp(Ut.ut_line, NTIME_MSG) == 0))
357*7c478bd9Sstevel@tonic-gate 			return (1);
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	if (Ut.ut_id != 0 &&
360*7c478bd9Sstevel@tonic-gate 		Ut.ut_xtime > 0 &&
361*7c478bd9Sstevel@tonic-gate 		Ut.ut_xtime > lastmonth &&
362*7c478bd9Sstevel@tonic-gate 		Ut.ut_xtime < nextmonth &&
363*7c478bd9Sstevel@tonic-gate 		Ut.ut_type >= EMPTY &&
364*7c478bd9Sstevel@tonic-gate 		Ut.ut_type <= UTMAXTYPE &&
365*7c478bd9Sstevel@tonic-gate 		Ut.ut_pid >= 0)
366*7c478bd9Sstevel@tonic-gate 		return (1);
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	return (0);
369*7c478bd9Sstevel@tonic-gate }
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate static void
372*7c478bd9Sstevel@tonic-gate wabort(int sig)
373*7c478bd9Sstevel@tonic-gate {
374*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "give up\n");
375*7c478bd9Sstevel@tonic-gate 	exit(1);
376*7c478bd9Sstevel@tonic-gate }
377