xref: /onnv-gate/usr/src/cmd/acct/wtmpfix.c (revision 428)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate 
230Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
240Sstevel@tonic-gate /*	  All Rights Reserved  	*/
250Sstevel@tonic-gate 
26*428Ssl108498 /*
27*428Ssl108498  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*428Ssl108498  * Use is subject to license terms.
29*428Ssl108498  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * wtmpfix - adjust wtmpx file and remove date changes.
340Sstevel@tonic-gate  *	wtmpfix <wtmpx1 >wtmpx2
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  *	code are added to really fix wtmpx if it is corrupted ..
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/param.h>
420Sstevel@tonic-gate #include "acctdef.h"
430Sstevel@tonic-gate #include <utmpx.h>
440Sstevel@tonic-gate #include <signal.h>
450Sstevel@tonic-gate #include <time.h>
460Sstevel@tonic-gate #include <ctype.h>
470Sstevel@tonic-gate #include <locale.h>
480Sstevel@tonic-gate #include <limits.h>
490Sstevel@tonic-gate #include <stdlib.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #define	MAXRUNTIME	3600	/* time out after 1 hour */
520Sstevel@tonic-gate #define	DAYEPOCH	(60 * 60 * 24)
530Sstevel@tonic-gate #define	wout(f, w)	 fwrite(w, sizeof (struct utmpx), 1, f);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate FILE	*Wtmpx, *Opw;
560Sstevel@tonic-gate FILE	*fp;
570Sstevel@tonic-gate char	Ofile[]	= "/tmp/wXXXXXX";
580Sstevel@tonic-gate static char time_buf[50];
590Sstevel@tonic-gate 
600Sstevel@tonic-gate struct	dtab
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	off_t	d_off1;		/* file offset start */
630Sstevel@tonic-gate 	off_t	d_off2;		/* file offset stop */
640Sstevel@tonic-gate 	time_t	d_adj;		/* time adjustment */
650Sstevel@tonic-gate 	struct dtab *d_ndp;	/* next record */
660Sstevel@tonic-gate };
670Sstevel@tonic-gate 
680Sstevel@tonic-gate struct	dtab	*Fdp;		/* list header */
690Sstevel@tonic-gate struct	dtab	*Ldp;		/* list trailer */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate time_t 	lastmonth, nextmonth;
720Sstevel@tonic-gate off_t	recno;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate struct	utmpx	Ut, Ut2;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate int	year, month;
770Sstevel@tonic-gate int 	ch;
780Sstevel@tonic-gate int	n;
790Sstevel@tonic-gate int	multimode;		/* multi user mode	 WHCC */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static int winp(FILE *, struct utmpx *);
820Sstevel@tonic-gate static void mkdtab(off_t);
830Sstevel@tonic-gate static void setdtab(off_t, struct utmpx *, struct utmpx *);
840Sstevel@tonic-gate static void adjust(off_t, struct utmpx *);
850Sstevel@tonic-gate static int invalid(char *);
86*428Ssl108498 static void intr(int) __NORETURN;
870Sstevel@tonic-gate static void scanfile(void);
880Sstevel@tonic-gate static int inrange(void);
890Sstevel@tonic-gate static void wabort(int);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate int
920Sstevel@tonic-gate main(int argc, char **argv)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	time_t tloc;
950Sstevel@tonic-gate 	struct tm *tmp;
960Sstevel@tonic-gate 	int fd;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
990Sstevel@tonic-gate 	setbuf(stdout, NULL);
1000Sstevel@tonic-gate 	alarm(MAXRUNTIME);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	if (signal(SIGALRM, wabort) == SIG_ERR) {
1030Sstevel@tonic-gate 		perror("signal");
1040Sstevel@tonic-gate 		return (1);
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 	if (signal(SIGINT, intr) == SIG_ERR) {
1070Sstevel@tonic-gate 		perror("signal");
1080Sstevel@tonic-gate 		return (1);
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	time(&tloc);
1120Sstevel@tonic-gate 	tmp = localtime(&tloc);
1130Sstevel@tonic-gate 	year = tmp->tm_year;
1140Sstevel@tonic-gate 	month = tmp->tm_mon + 1;
1150Sstevel@tonic-gate 	lastmonth = ((year + 1900 - 1970) * 365 +
1160Sstevel@tonic-gate 	    (month - 1) * 30) * DAYEPOCH;
1170Sstevel@tonic-gate 	nextmonth = ((year + 1900 - 1970) * 365 +
1180Sstevel@tonic-gate 	    (month + 1) * 30) * DAYEPOCH;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if (argc < 2) {
1210Sstevel@tonic-gate 		argv[argc] = "-";
1220Sstevel@tonic-gate 		argc++;
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if ((fd = mkstemp(Ofile)) == -1) {
1260Sstevel@tonic-gate 		fprintf(stderr, "cannot make temporary: %s\n", Ofile);
1270Sstevel@tonic-gate 		intr(0);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	if ((Opw = fdopen(fd, "w")) == NULL) {
1310Sstevel@tonic-gate 		fprintf(stderr, "cannot open temporary: %s\n", Ofile);
1320Sstevel@tonic-gate 		intr(0);
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	while (--argc > 0) {
1360Sstevel@tonic-gate 		argv++;
1370Sstevel@tonic-gate 		if (strcmp(*argv, "-") == 0)
1380Sstevel@tonic-gate 			Wtmpx = stdin;
1390Sstevel@tonic-gate 		else if ((Wtmpx = fopen(*argv, "r")) == NULL) {
1400Sstevel@tonic-gate 			fprintf(stderr, "Cannot open: %s\n", *argv);
1410Sstevel@tonic-gate 			intr(0);
1420Sstevel@tonic-gate 		}
1430Sstevel@tonic-gate 		scanfile();
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		if (Wtmpx != stdin)
1460Sstevel@tonic-gate 			fclose(Wtmpx);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 	fclose(Opw);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if ((Opw = fopen(Ofile, "r")) == NULL) {
1510Sstevel@tonic-gate 		fprintf(stderr, "Cannot read from temp: %s\n", Ofile);
1520Sstevel@tonic-gate 		intr(0);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 	recno = 0;
1550Sstevel@tonic-gate 	while (winp(Opw, &Ut)) {
1560Sstevel@tonic-gate 		adjust(recno, &Ut);
1570Sstevel@tonic-gate 		recno += sizeof (struct utmpx);
1580Sstevel@tonic-gate 		wout(stdout, &Ut);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	fclose(Opw);
1610Sstevel@tonic-gate 	unlink(Ofile);
1620Sstevel@tonic-gate 	return (0);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate static int
1660Sstevel@tonic-gate winp(FILE *f, struct utmpx *w)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	if (fread(w, sizeof (struct utmpx), 1, f) != 1)
1690Sstevel@tonic-gate 		return (0);
1700Sstevel@tonic-gate 	if ((w->ut_type >= EMPTY) && (w->ut_type <= UTMAXTYPE))
1710Sstevel@tonic-gate 		return (1);
1720Sstevel@tonic-gate 	else {
1730Sstevel@tonic-gate 		fprintf(stderr, "Bad file at offset %ld\n",
1740Sstevel@tonic-gate 			ftell(f) - sizeof (struct utmpx));
1750Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &w->ut_xtime);
1760Sstevel@tonic-gate 		fprintf(stderr, "%-12s %-8s %lu %s",
1770Sstevel@tonic-gate 			w->ut_line, w->ut_user, w->ut_xtime, time_buf);
1780Sstevel@tonic-gate 		intr(0);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 	/* NOTREACHED */
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static void
1840Sstevel@tonic-gate mkdtab(off_t p)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	struct dtab *dp;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	dp = Ldp;
1900Sstevel@tonic-gate 	if (dp == NULL) {
1910Sstevel@tonic-gate 		dp = calloc(sizeof (struct dtab), 1);
1920Sstevel@tonic-gate 		if (dp == NULL) {
1930Sstevel@tonic-gate 			fprintf(stderr, "out of core\n");
1940Sstevel@tonic-gate 			intr(0);
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 		Fdp = Ldp = dp;
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	dp->d_off1 = p;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate static void
2020Sstevel@tonic-gate setdtab(off_t p, struct utmpx *w1, struct utmpx *w2)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	struct dtab *dp;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if ((dp = Ldp) == NULL) {
2070Sstevel@tonic-gate 		fprintf(stderr, "no dtab\n");
2080Sstevel@tonic-gate 		intr(0);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 	dp->d_off2 = p;
2110Sstevel@tonic-gate 	dp->d_adj = w2->ut_xtime - w1->ut_xtime;
2120Sstevel@tonic-gate 	if ((Ldp = calloc(sizeof (struct dtab), 1)) == NULL) {
2130Sstevel@tonic-gate 		fprintf(stderr, "out of core\n");
2140Sstevel@tonic-gate 		intr(0);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 	Ldp->d_off1 = dp->d_off1;
2170Sstevel@tonic-gate 	dp->d_ndp = Ldp;
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate static void
2210Sstevel@tonic-gate adjust(off_t p, struct utmpx *w)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	off_t pp;
2250Sstevel@tonic-gate 	struct dtab *dp;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	pp = p;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	for (dp = Fdp; dp != NULL; dp = dp->d_ndp) {
2300Sstevel@tonic-gate 		if (dp->d_adj == 0)
2310Sstevel@tonic-gate 			continue;
2320Sstevel@tonic-gate 		if (pp >= dp->d_off1 && pp < dp->d_off2)
2330Sstevel@tonic-gate 			w->ut_xtime += dp->d_adj;
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate  *	invalid() determines whether the name field adheres to
2390Sstevel@tonic-gate  *	the criteria set forth in acctcon1.  If the name violates
2400Sstevel@tonic-gate  *	conventions, it returns a truth value meaning the name is
2410Sstevel@tonic-gate  *	invalid; if the name is okay, it returns false indicating
2420Sstevel@tonic-gate  *	the name is not invalid.
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate static int
2460Sstevel@tonic-gate invalid(char *name)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	int	i;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	for (i = 0; i < NSZ; i++) {
2510Sstevel@tonic-gate 		if (name[i] == '\0')
2520Sstevel@tonic-gate 			return (VALID);
2530Sstevel@tonic-gate 		if (! (isalnum(name[i]) || (name[i] == '$') ||
2540Sstevel@tonic-gate 		    (name[i] == ' ') || (name[i] == '.') ||
2550Sstevel@tonic-gate 		    (name[i] == '_') || (name[i] == '-'))) {
2560Sstevel@tonic-gate 			return (INVALID);
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 	return (VALID);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate static void
2630Sstevel@tonic-gate intr(int sig)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
2660Sstevel@tonic-gate 	unlink(Ofile);
2670Sstevel@tonic-gate 	exit(1);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate  * scanfile:
2720Sstevel@tonic-gate  * 1)  	reads the file, to see if the record is within reasonable
2730Sstevel@tonic-gate  * 	range; if not, then it will scan the file, delete foreign stuff.
2740Sstevel@tonic-gate  * 2)   enter setdtab if in multiuser mode
2750Sstevel@tonic-gate  * 3)   change bad login names to INVALID
2760Sstevel@tonic-gate  */
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate static void
2790Sstevel@tonic-gate scanfile()
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 	while ((n = fread(&Ut, sizeof (Ut), 1, Wtmpx)) > 0) {
2820Sstevel@tonic-gate 		if (n == 0) {
2830Sstevel@tonic-gate 			unlink(Ofile);
2840Sstevel@tonic-gate 			exit(0);
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 		if (!inrange()) {
2870Sstevel@tonic-gate 			for (;;) {
2880Sstevel@tonic-gate 				if (fseek(Wtmpx,
2890Sstevel@tonic-gate 				    -(off_t)sizeof (Ut), 1) != 0) {
2900Sstevel@tonic-gate 					perror("seek error\n");
2910Sstevel@tonic-gate 					exit(1);
2920Sstevel@tonic-gate 				}
2930Sstevel@tonic-gate 				if ((ch = getc(Wtmpx)) == EOF) {
2940Sstevel@tonic-gate 					perror("read\n");
2950Sstevel@tonic-gate 					exit(1);
2960Sstevel@tonic-gate 				}
2970Sstevel@tonic-gate 				fprintf(stderr, "checking offset %lo\n",
2980Sstevel@tonic-gate 				    ftell(Wtmpx));
2990Sstevel@tonic-gate 				if (fread(&Ut, sizeof (Ut), 1, Wtmpx) == 0) {
3000Sstevel@tonic-gate 					exit(1);
3010Sstevel@tonic-gate 				}
3020Sstevel@tonic-gate 				if (inrange())
3030Sstevel@tonic-gate 					break;
3040Sstevel@tonic-gate 			}
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 		/* Now we have a good utmpx record, do more processing */
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate #define	UTYPE	Ut.ut_type
3090Sstevel@tonic-gate #define	ULINE	Ut.ut_line
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 			if (recno == 0 || UTYPE == BOOT_TIME)
3120Sstevel@tonic-gate 				mkdtab(recno);
3130Sstevel@tonic-gate 			if (UTYPE == RUN_LVL) {
3140Sstevel@tonic-gate 				if (strncmp(ULINE, "run-level S", 11) == 0)
3150Sstevel@tonic-gate 					multimode = 0;
3160Sstevel@tonic-gate 				if (strncmp(ULINE, "run-level 2", 11) == 0)
3170Sstevel@tonic-gate 					multimode++;
3180Sstevel@tonic-gate 			}
3190Sstevel@tonic-gate 			if (invalid(Ut.ut_name)) {
3200Sstevel@tonic-gate 				fprintf(stderr,
3210Sstevel@tonic-gate 				    "wtmpfix: logname \"%*.*s\" changed "
3220Sstevel@tonic-gate 				    "to \"INVALID\"\n", OUTPUT_NSZ,
3230Sstevel@tonic-gate 				    OUTPUT_NSZ, Ut.ut_name);
3240Sstevel@tonic-gate 				(void) strncpy(Ut.ut_name, "INVALID", NSZ);
3250Sstevel@tonic-gate 			}
3260Sstevel@tonic-gate 			if (UTYPE == OLD_TIME) {
3270Sstevel@tonic-gate 				if (!winp(Wtmpx, &Ut2)) {
3280Sstevel@tonic-gate 					fprintf(stderr, "Input truncated at "
3290Sstevel@tonic-gate 					    "offset %ld\n", recno);
3300Sstevel@tonic-gate 					intr(0);
3310Sstevel@tonic-gate 				}
3320Sstevel@tonic-gate 				if (Ut2.ut_type != NEW_TIME) {
3330Sstevel@tonic-gate 					fprintf(stderr, "New date expected at "
3340Sstevel@tonic-gate 					    "offset %ld", recno);
3350Sstevel@tonic-gate 					intr(0);
3360Sstevel@tonic-gate 				}
3370Sstevel@tonic-gate 				if (multimode)  /* multiuser */
3380Sstevel@tonic-gate 					setdtab(recno, &Ut, &Ut2);
3390Sstevel@tonic-gate 				recno += (2 * sizeof (struct utmpx));
3400Sstevel@tonic-gate 				wout(Opw, &Ut);
3410Sstevel@tonic-gate 				wout(Opw, &Ut2);
3420Sstevel@tonic-gate 				continue;
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 			wout(Opw, &Ut);
3450Sstevel@tonic-gate 			recno += sizeof (struct utmpx);
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate static int
3500Sstevel@tonic-gate inrange()
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	if ((strcmp(Ut.ut_line, RUNLVL_MSG) == 0) ||
3530Sstevel@tonic-gate 	    (strcmp(Ut.ut_line, BOOT_MSG) == 0) ||
3540Sstevel@tonic-gate 	    (strcmp(Ut.ut_line, "acctg on") == 0) ||
3550Sstevel@tonic-gate 	    (strcmp(Ut.ut_line, OTIME_MSG) == 0) ||
3560Sstevel@tonic-gate 	    (strcmp(Ut.ut_line, NTIME_MSG) == 0))
3570Sstevel@tonic-gate 			return (1);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (Ut.ut_id != 0 &&
3600Sstevel@tonic-gate 		Ut.ut_xtime > 0 &&
3610Sstevel@tonic-gate 		Ut.ut_xtime > lastmonth &&
3620Sstevel@tonic-gate 		Ut.ut_xtime < nextmonth &&
3630Sstevel@tonic-gate 		Ut.ut_type >= EMPTY &&
3640Sstevel@tonic-gate 		Ut.ut_type <= UTMAXTYPE &&
3650Sstevel@tonic-gate 		Ut.ut_pid >= 0)
3660Sstevel@tonic-gate 		return (1);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	return (0);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate static void
3720Sstevel@tonic-gate wabort(int sig)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	fprintf(stderr, "give up\n");
3750Sstevel@tonic-gate 	exit(1);
3760Sstevel@tonic-gate }
377