Path: utzoo!dciem!nrcaer!scs!spl1!laidbak!att!pacbell!ames!mailrus!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm From: tholm@uvicctr.UUCP (Terrence W. Holm) Newsgroups: comp.os.minix Subject: su(1) passing the environment Message-ID: <431@uvicctr.UUCP> Date: 2 Jun 88 00:34:53 GMT Article-I.D.: uvicctr.431 Reply-To: tholm@uvicctr.UUCP (Terrence W. Holm) Organization: University of Victoria, Victoria B.C. Canada Lines: 123 EFTH Minix report #15 - June 1988 - su(1) and the environment This is a modified su(1) that properly passes the environment. Note: putenv(3) must be installed (see EFTH report #2). A "man" page is included. echo x - su.1 gres '^X' '' > su.1 << '/' XNAME X su(1) - superimpose another user's shell X XSYNOPSIS X su [ username ] X XDESCRIPTION X This performs a log-in of another user, a new process is X used for the appropriate shell. Your current shell is not X destroyed, as occurs when using login(1). X X If "username" is not given it defaults to "root" (the super- X user). A password will be asked for if "username" requires one. X X The group and user id are changed for the new username. X The environment entries $HOME and $SHELL are set up according X to the new username's entry in the password file, but the X rest of the environment is passed untouched. The current X directory is not changed. X XFILES X /etc/passwd X XSEE ALSO X login(1) / echo x - su.c gres '^X' '' > su.c << '/' X/* su - become super-user Author: Patrick van Kleef */ X X/* Modified to set up HOME and SHELL in the environment, and pass */ X/* the rest of the environment to the new shell. 1987-Oct-7 EFTH */ X X#include "sgtty.h" X#include "stdio.h" X#include "pwd.h" X Xextern char **environ; X Xchar *malloc(); X X Xmain (argc, argv) Xint argc; Xchar *argv[]; X{ X register char *name; X char *crypt (); X char *shell = "/bin/sh"; X char *_home, *_shell; X int nr; X char password[14]; X struct sgttyb args; X register struct passwd *pwd; X struct passwd *getpwnam (); X X if (argc > 1) X name = argv[1]; X else X name = "root"; X X if ((pwd = getpwnam (name)) == 0) { X std_err("Unknown id: "); X std_err(name); X std_err("\n"); X exit (1); X } X X if (pwd->pw_passwd[0] != '\0' && getuid()!= 0) { X std_err("Password: "); X ioctl (0, TIOCGETP, &args); /* get parameters */ X args.sg_flags = args.sg_flags & (~ECHO); X ioctl (0, TIOCSETP, &args); X nr = read (0, password, 14); X password[nr - 1] = 0; X putc('\n',stderr); X args.sg_flags = args.sg_flags | ECHO; X ioctl (0, TIOCSETP, &args); X if (strcmp (pwd->pw_passwd, crypt (password, pwd->pw_passwd))) { X std_err("Sorry\n"); X exit (2); X } X } X setgid (pwd->pw_gid); X setuid (pwd->pw_uid); X if (pwd->pw_shell[0]) X shell = pwd->pw_shell; X X /* Set up HOME and SHELL in the environment */ X X _home = malloc( strlen(pwd->pw_dir) + 6 ); X strcpy( _home, "HOME=" ); X strcat( _home, pwd->pw_dir ); X putenv( _home ); X X _shell = malloc( strlen(shell) + 7 ); X strcpy( _shell, "SHELL=" ); X strcat( _shell, shell ); X putenv( _shell ); X X execle( shell, shell, (char *) 0, environ ); X std_err("No shell\n"); X exit (3); X} / -------------------------------------------------------------------- Edwin L. Froese uw-beaver!ubc-cs!mprg!handel!froese Terrence W. Holm {uw-beaver,ubc-cs}!uvicctr!sirius!tholm