/*
* sucmd prog arg...
*
*	Become a superuser
*
* written: 	23.7.90		Wolfgang Schwabl @ AUI
*/

#include	"V.h"
#include	"sys_pwd.h"

#define		UID	0	/* ID of Superuser */
#define		EUID	0

char *known[]= {		/* this is a NULL - terminated list */
	"super1",		/* of trusted superusers	    */
	"super2",
	"super3",
	"super4",
	NULL
};

trusted_user()		/* Is the real user a trustworthy user ? */
{
	register int trusted;    	/* boolean: Is the user trusted ? */
	register struct passwd *pw;	/* a pointer to the password record */

	trusted = 0;

	if ((pw=getpwuid(getuid())) != (struct passwd *)0) {
		register char *s;
	 	register int i;
		for (i=0; (s=known[i]) != NULL; i++) {
			if (strcmp(s,pw->pw_name) == 0) {
				trusted = 1;
			}
		}
	} else {
		printf("Your UID %d is unknown\n",getuid());
	}
	return(trusted);
}


main()
{
	register struct passwd *pw;	/* a pointer to the password record */

	/* check for the trustworthiness of the caller */
	if (trusted_user()) {
		/* give all privileges to a trustworthy user */
		if (setreuid(UID,EUID) < 0) {
			perror("setreuid");
			return(1);
		}
        /* change the new users's HOME directory */
		if ((pw=getpwuid(getuid())) != (struct passwd *)0) {
		    if (setenv("HOME",pw->pw_dir,1) < 0) {
			    perror("setenv");
			    return(1);
		    }
		} else {
		    printf("The UID %d is unknown\n",getuid());
        }
		execl("/bin/csh","ncsh",NULL);
		printf("Sorry, could not exec a shell\n");
		return(2);
	}
	printf("Sorry, you are an unknown user to me\n");
	return(3);
}
