#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <string.h>
#include <sys/types.h>

extern int errno;

/* #define MYUID	102 */
#define MYUID	1000

static void setup_env(void);

main(argc, argv)
int argc;
char *argv[];
{
	char *shell, *getenv();
	char shell_basename[32];
	char *cp;
	extern int getopt();
	extern char *optarg;
	extern int optind, opterr;
	int errflg = 0;
	int lsflag = 0;				/* login shell flag */
	int c;
	char *cmdname = argv[0];

	if (getuid() != MYUID)
		exit(0);

	while ((c = getopt(argc, argv, "l")) != -1)
		switch(c) {
			case 'l': lsflag++;
				break;
			default: errflg++;
				break;
		}
	if (errflg) {
		fprintf(stderr, "usage: sx [-l]\n");
		fprintf(stderr, "usage: sx cmd [arg, ...]\n");
		exit(1);
	}

	if (setuid(0) || setgid(2)) {
		fprintf(stderr, "%s setuid failed - ", cmdname);
		perror("");
		exit(1);
	}

	setup_env();
	if (argc == optind) {
		/*
		 * no cmd supplied: run (login) shell
		 */
		shell_basename[0] = '\0';
		if ((shell = getenv("SHELL")) == NULL)
			shell = "/bin/sh";
		if (lsflag)
			strcpy(shell_basename, "-");
		if (cp = strrchr(shell, '/'))
			cp++;
		else
			cp = shell;
		strcpy(&shell_basename[strlen(shell_basename)], cp);

		execl(shell, shell_basename, (char *)0L);
		fprintf(stderr, "%s: cannot execute %s - ", cmdname, shell);
		perror("");
		exit(1);
	}

	while (optind-- >= 1) {
		++argv;
	}
	if (lsflag)
		fprintf(stderr, "%s: Warning: -l ignored\n", cmdname);
	execvp(argv[0], argv);
	fprintf(stderr, "%s: cannot execute %s: ", cmdname, argv[0]);
	perror("");
	exit (1);
}

static void
setup_env(void)
{
	char *getenv();
	if (getenv("HOME") == NULL)
		(void)putenv("HOME=/");
	(void)putenv("PS1=# ");
	(void)putenv("LOGNAME=root");
	(void)putenv("MAIL=/usr/mail/root");
#ifdef mips
	(void)putenv("VIS_SERVICES=/etc/vis.conf");
#endif
}
