Commit 3dd28f4e authored by jan.koester's avatar jan.koester
Browse files

su: -c COMMAND, unknown options rejected; the builtin keeps root's shell

"su jan -c id" failed with "su: user id does not exist": su knew no
-c and took every word as the user name, the last one winning. Now
-c COMMAND / --command=COMMAND runs "SHELL -c COMMAND" as the user
(before or after the user name), -h/--help shows the usage, and an
unknown option, a missing command or a second user name is an error
instead of a user name.

The fresh builtin ran su right in the shell for root, so the switched
user's shell replaced root's (and "su USER -c ..." ended a root
script); it now always runs su in a child the shell waits for.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent a2e343e2
Loading
Loading
Loading
Loading
+11 −7
Changes for commands/auth/su.cpp: 11 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -37,24 +37,28 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

namespace fresh { namespace cmd {

    // The builtin: as root it switches right here (auth::suMain()); anyone
    // else can't read /etc/shadow.yaml or setuid(), so the setuid-root /bin/su
    // (FRESH_SU_PATH) does it, in a child the shell waits for -- leaving the
    // su'd shell returns to this one.
    // The builtin runs su in a child the shell waits for -- leaving the su'd
    // shell (or the end of "su USER -c COMMAND") returns to this one, also
    // for root, whose shell must not be replaced. Root switches in that
    // child itself (auth::suMain()); anyone else can't read
    // /etc/shadow.yaml or setuid(), so the setuid-root /bin/su
    // (FRESH_SU_PATH) does it.
    class SuCommand : public ShellCommand {
    public:
        SuCommand():ShellCommand("su"){}

        int runCommand(int argc,const char **argv){
            if(geteuid()==0)
                return auth::suMain(argc,argv);

            pid_t pid=fork();
            if(pid<0){
                std::cerr << "su: fork: " << strerror(errno) << std::endl;
                return 1;
            }
            if(pid==0){
                if(geteuid()==0){ // suMain() returns only on errors and for --help
                    int r=auth::suMain(argc,argv);
                    std::cout.flush();
                    _exit(r);
                }
                execv(FRESH_SU_PATH,const_cast<char **>(argv));
                std::cerr << "su: exec " << FRESH_SU_PATH << ": " << strerror(errno) << std::endl;
                _exit(127);
+46 −8
Changes for commands/auth/suimpl.h: 46 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -71,19 +71,52 @@ namespace fresh { namespace auth {
        return false;
    }

    // "su [-|-l|--login] [USERNAME]" (default USERNAME: root). Root
    // switching to any user needs no password (matches real su); anyone
    // else must be in group wheel and is prompted, verified the same way
    // login verifies. "-"/"-l" additionally chdir's to the target's home
    inline void suUsage(std::ostream &out){
        out << "usage: su [-|-l|--login] [-c COMMAND|--command=COMMAND] [USERNAME]" << std::endl
            << "  starts USERNAME's shell (default root); -c runs COMMAND in it instead" << std::endl;
    }

    // "su [-|-l|--login] [-c COMMAND] [USERNAME]" (default USERNAME: root).
    // Root switching to any user needs no password (matches real su);
    // anyone else must be in group wheel and is prompted, verified the same
    // way login verifies. "-"/"-l" additionally chdir's to the target's home
    // and resets HOME/USER/LOGNAME/SHELL, i.e. a login shell rather than
    // just a privilege swap that keeps the caller's own environment/cwd.
    // -c COMMAND runs "SHELL -c COMMAND" instead of an interactive shell.
    // Options may come before or after the user name; anything else that
    // starts with '-' is an error -- never taken for a user name.
    inline int suMain(int argc,const char **argv){
        bool loginShell=false;
        std::string target="root";
        bool loginShell=false,haveTarget=false,haveCommand=false;
        std::string target="root",command;
        for(int i=1;i<argc;++i){
            std::string a=argv[i];
            if(a=="-" || a=="-l" || a=="--login") loginShell=true;
            else target=a;
            else if(a=="-c" || a=="--command"){
                if(i+1>=argc){
                    std::cerr << "su: " << a << " needs a command" << std::endl;
                    suUsage(std::cerr);
                    return 1;
                }
                command=argv[++i];
                haveCommand=true;
            }else if(a.rfind("--command=",0)==0){
                command=a.substr(10);
                haveCommand=true;
            }else if(a=="-h" || a=="--help"){
                suUsage(std::cout);
                return 0;
            }else if(a.size()>1 && a[0]=='-'){
                std::cerr << "su: unknown option " << a << std::endl;
                suUsage(std::cerr);
                return 1;
            }else if(haveTarget){
                std::cerr << "su: one user name only (" << target << ", " << a << ")" << std::endl;
                suUsage(std::cerr);
                return 1;
            }else{
                target=a;
                haveTarget=true;
            }
        }

        bool callerIsRoot=ids::isRoot(ids::ruid());
@@ -129,7 +162,12 @@ namespace fresh { namespace auth {
        size_t slash=shell.find_last_of('/');
        std::string base=(slash==std::string::npos) ? shell : shell.substr(slash+1);
        std::string argv0=(loginShell ? "-" : "")+base;
        char *newArgv[2]={const_cast<char *>(argv0.c_str()),nullptr};
        std::string dashC="-c";
        char *newArgv[4]={const_cast<char *>(argv0.c_str()),nullptr,nullptr,nullptr};
        if(haveCommand){
            newArgv[1]=const_cast<char *>(dashC.c_str());
            newArgv[2]=const_cast<char *>(command.c_str());
        }

        execv(shell.c_str(),newArgv);
        std::cerr << "su: exec " << shell << ": " << strerror(errno) << std::endl;