su.c (3645B)
1 /* see LICENSE file for copyright and license details */ 2 #ifndef _DEFAULT_SOURCE 3 #define _DEFAULT_SOURCE 4 #endif 5 6 #include "passwd.h" 7 #include "util.h" 8 #include "wexec.h" 9 10 #include <grp.h> 11 #include <limits.h> 12 #include <pwd.h> 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <string.h> 16 #include <termios.h> 17 #include <unistd.h> 18 19 static void 20 usage(void) 21 { 22 eprintf("usage: %s [-l] [-s shell] [-c command] [name]\n", argv0); 23 } 24 25 /* reads one line with echo off into a reusable static buffer */ 26 static char * 27 readpass(const char *prompt) 28 { 29 static char buf[128]; 30 struct termios old, raw; 31 int have_old; 32 size_t n; 33 34 fputs(prompt, stdout); 35 fflush(stdout); 36 37 have_old = tcgetattr(STDIN_FILENO, &old) == 0; 38 if (have_old) { 39 raw = old; 40 raw.c_lflag &= ~ECHO; 41 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); 42 } 43 44 if (!fgets(buf, sizeof(buf), stdin)) { 45 if (have_old) 46 tcsetattr(STDIN_FILENO, TCSAFLUSH, &old); 47 return NULL; 48 } 49 50 if (have_old) { 51 tcsetattr(STDIN_FILENO, TCSAFLUSH, &old); 52 fputc('\n', stdout); 53 } 54 55 n = strlen(buf); 56 if (n && buf[n - 1] == '\n') 57 buf[--n] = '\0'; 58 59 return buf; 60 } 61 62 // ?man su: become another user 63 // ?man arguments: [-l] [-s shell] [-c command] [name [arg ...]] 64 // ?man authenticate as name (default: root) and start a shell as them 65 // ?man with -l, also chdir to their home directory and run a login shell, 66 // ?man same as login would. with -c, the shell runs command and ends. 67 // ?man args after name go to the shell as they are, so su name -c cmd 68 // ?man runs cmd as on the BSDs and util-linux 69 int 70 main(int argc, char *argv[]) 71 { 72 struct passwd *pw; 73 char shellbuf[PATH_MAX]; 74 char *name, *pass, *base; 75 char **shargv; 76 char *shell = NULL, *command = NULL; 77 int i, n, lflag = 0; 78 uid_t uid; 79 80 ARGBEGIN 81 { 82 // ?man -l: start a login shell, chdir home like login does 83 case 'l': 84 lflag = 1; 85 break; 86 // ?man -s:shell run shell, not the one in the password database 87 case 's': 88 shell = EARGF(usage()); 89 break; 90 // ?man -c:command have the shell run command, and end 91 case 'c': 92 command = EARGF(usage()); 93 break; 94 default: 95 usage(); 96 } 97 ARGEND 98 99 name = argc > 0 ? argv[0] : "root"; 100 pw = getpwnam(name); 101 if (!pw) 102 eprintf("su: unknown user: %s\n", name); 103 104 uid = getuid(); 105 if (uid != 0) { 106 pass = readpass("Password: "); 107 if (!pass || pw_check(pw, pass) != 1) 108 eprintf("su: incorrect password\n"); 109 } 110 111 if (initgroups(pw->pw_name, pw->pw_gid) < 0) 112 weprintf("initgroups:"); 113 if (setgid(pw->pw_gid) < 0) 114 eprintf("setgid:"); 115 if (setuid(pw->pw_uid) < 0) 116 eprintf("setuid:"); 117 118 strlcpy(shellbuf, shell ? shell : pw->pw_shell[0] ? pw->pw_shell : "/bin/sh", sizeof(shellbuf)); 119 120 if (lflag) { 121 if (chdir(pw->pw_dir) < 0) 122 weprintf("chdir %s:", pw->pw_dir); 123 setenv("HOME", pw->pw_dir, 1); 124 } 125 setenv("USER", pw->pw_name, 1); 126 setenv("LOGNAME", pw->pw_name, 1); 127 setenv("SHELL", shellbuf, 1); 128 129 base = strrchr(shellbuf, '/'); 130 base = base ? base + 1 : shellbuf; 131 132 shargv = emalloc((size_t)(argc + 4) * sizeof *shargv); 133 if (lflag) { 134 shargv[0] = emalloc(strlen(base) + 2); 135 shargv[0][0] = '-'; 136 strcpy(shargv[0] + 1, base); 137 } else { 138 shargv[0] = estrdup(base); 139 } 140 n = 1; 141 if (command) { 142 shargv[n++] = "-c"; 143 shargv[n++] = command; 144 } 145 // a -c after the name is the shell's own, the way scripts written for 146 // other systems call su 147 for (i = 1; i < argc; i++) 148 shargv[n++] = argv[i]; 149 shargv[n] = NULL; 150 151 wexecv_self(shellbuf, shargv); 152 return 1; 153 }