/*
    Pirate Bulletin Board System
    Copyright (C) 1990, Edward Luke

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


    Email address:
    lush@Athena.EE.MsState.EDU
*/
#include <stdio.h>
#include <utmp.h>
#include <pwd.h> 

char *ttyname() ,*rindex() ;

#ifndef SYSV
struct utmp *
invis()
{
	static struct utmp data ;
	FILE *fp ;
	int flag = 0 ;
	char *name, *tp ;
	struct passwd *pp ;
	
    tp = ttyname(0) ;
    if(!tp)
      return NULL ;
    tp = rindex(tp,'/') + 1 ;
	pp = getpwuid(getuid()) ;
	if(!pp)
	{
		fprintf(stderr,"You Don't exist!\n") ;
		exit(0) ;
	}
	name = pp->pw_name ;
	
	if((fp = fopen("/etc/utmp","r+")) == NULL)
		exit(0) ;
	while(read(fileno(fp),&data,sizeof(struct utmp))>0)
	{
		if(!strcmp(tp,data.ut_line))
		{
            struct utmp nildata ;
			flag = 1 ;
            bcopy(&data, &nildata, sizeof(nildata)) ;
#ifdef INVISIBLE
			bzero(nildata.ut_name,8) ;
			fseek(fp,(long)(ftell(fp) - sizeof(struct utmp)),0) ;
			if(write(fileno(fp),&nildata,sizeof(struct utmp)) != sizeof(struct utmp))
				/* NIL IF STATEMENT */ ;
#endif
            fclose(fp) ;
            return &data ;
		}
	}
	fclose(fp) ;
    return NULL ;
}
#endif
char *env[] = {
	"TERM=xxxxxxxxxxxxxxxxxxxxxxxxxxx",
	NULL} ;

main() 
{
    int uid ;

    uid = getuid() ;
    if(uid == BBSUID) { /* bbs uid */
        struct utmp *whee ;
        char hid[17] ;

#ifndef SYSV
        whee = invis() ;
#endif
        chroot(BBSHOME) ;
        setuid(uid) ;
#ifndef SYSV
        if(whee) {
            strncpy(hid, whee->ut_host, 16) ;
            hid[16] = '\0' ;
            execl("/bin/bbs","bbs","h", hid, NULL) ;
        }
        else
#endif
          execle("/bin/bbs","bbs", NULL, env) ;
        printf("execl failed\n") ;
    }
    setuid(uid) ;
    printf("UID DOES NOT MATCH\n") ;
    exit(-1) ;
}

