
#include	"tron.h"

ckpass()
{
	register short lc, g;
	struct stat statb;
	FILE *fp;
	static char kbuf[256];
	register char *p;
	short fd;

	if((fp = fopen(PASSWD, "r")) == NULL){
		fprintf(stdout, "*TRON: No %s\n", PASSWD);
		return(1);
	}
	if((fd = creat(PTMP, 0600)) == -1){
		fprintf(stdout, "*TRON: Can't creat %s\n", PTMP);
		fclose(fp);
		return(1);
	}
	lc = 0;
	while(fgets(kbuf, 255, fp)){
		p = kbuf;
		lc++;
		while(*p && *p != ':')p++;
		if(*p++ == 0){
lp:
			fprintf(stdout, "**TRON: Bad passwd line %d: %s",
				lc, kbuf);
			continue;
		}
		while(*p && *p != ':')p++;
		if(*p++ == 0)goto lp;
		while(*p && *p != ':')p++;
		if(*p++ == 0)goto lp;
		g = atoi(p);
		while(*p && *p != ':')p++;
		if(*p++ == 0)goto lp;
		while(*p && *p != ':')p++;
		if(*p++ == 0)goto lp;
		while(*p && *p != ':')p++;
		if(*p++ == 0)goto lp;
		while(*p)p++;
		if(g == 0 || g == 2 || g == 3 || g > 20000 || g < 0)
			write(fd, kbuf, p - kbuf);
	}
	close(fd);
	fclose(fp);
	if(stat(PDATA, &statb))
		fprintf(stdout, "*TRON: No %s\n", PDATA);
	else	{
		sig2 = signal(2, 1);
		if((lc = fork()) == -1)
			fprintf(stdout, "**TRON: Can not fork for ckpasswd()\n");
		else	if(lc == 0){
				signal(2, 0);
				signal(3, 0);
				close(1);
				creat(PDIFF, 0600);
				execl("/usr/bin/diff", "diff", PDATA, PTMP, 0);
				fprintf(stdout, "***TRON: Can't execute diff\n");
				/* Yes, I know it goes to PDIFF, but wait... */
				exit(1);
			}
			else	{
				while((g = wait(0)) != lc && g != -1);
				if(stat(PDIFF, &statb) == 0 &&
					statb.st_size){
					fprintf(stdout, "**TRON: new/old prived accts changed\n");
					if((lc = open(PDIFF, 0)) != -1){
						fflush(stdout);
						while((g = read(lc, kbuf, 256)) > 0)
							write(1, kbuf, g);
						close(lc);
					}
				}
				unlink(PDIFF);
			}
		signal(2, sig2);
	}
	unlink(PDATA);
	link(PTMP, PDATA);
	unlink(PTMP);
	return(0);
}
