Skip to content

Commit

Permalink
Ditch fmt, log, path/filepath, and strings for ~17KB more sav…
Browse files Browse the repository at this point in the history
…ings

```console
$ stat --format '% 11n %s' gosu-before gosu-after
gosu-before 1495254
 gosu-after 1478001
```
  • Loading branch information
tianon committed Mar 20, 2024
1 parent a7a1ca6 commit 04fac5a
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package main // import "github.com/tianon/gosu"

import (
"fmt"
"log"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
)

Expand All @@ -18,67 +15,74 @@ func init() {
}

func version() string {
return fmt.Sprintf(`%s (%s on %s/%s; %s)`, Version, runtime.Version(), runtime.GOOS, runtime.GOARCH, runtime.Compiler)
// 1.17 (go1.18.2 on linux/amd64; gc)
return Version + ` (` + runtime.Version() + ` on ` + runtime.GOOS + `/` + runtime.GOARCH + `; ` + runtime.Compiler + `)`
}

func usage() string {
self := os.Args[0]
v := version()
t := `
Usage: {{ .Self }} user-spec command [args]
eg: {{ .Self }} tianon bash
{{ .Self }} nobody:root bash -c 'whoami && id'
{{ .Self }} 1000:1 id
Usage: ` + self + ` user-spec command [args]
eg: ` + self + ` tianon bash
` + self + ` nobody:root bash -c 'whoami && id'
` + self + ` 1000:1 id
{{ .Self }} version: {{ .Version }}
{{ .Self }} license: Apache-2.0 (full text at https://github.com/tianon/gosu)
` + self + ` version: ` + v + `
` + self + ` license: Apache-2.0 (full text at https://github.com/tianon/gosu)
`
t = strings.ReplaceAll(t, "{{ .Self }}", filepath.Base(os.Args[0]))
t = strings.ReplaceAll(t, "{{ .Version }}", version())
return t[1:]
}

func main() {
log.SetFlags(0) // no timestamps on our logs
func exit(code int, w io.Writer, ss ...string) {
for i, s := range ss {
if i > 0 {
w.Write([]byte{' '})
}
w.Write([]byte(s))
}
w.Write([]byte{'\n'})
os.Exit(code)
}

func main() {
if ok := os.Getenv("GOSU_PLEASE_LET_ME_BE_COMPLETELY_INSECURE_I_GET_TO_KEEP_ALL_THE_PIECES"); ok != "I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhäuser Gate. All those moments will be lost in time, like tears in rain. Time to die." {
if fi, err := os.Stat("/proc/self/exe"); err != nil {
log.Fatalf("error: %v", err)
} else if fi.Mode()&os.ModeSetuid != 0 {
exit(1, os.Stderr, "error:", err.Error())
} else if mode := fi.Mode(); mode&os.ModeSetuid != 0 {
// ... oh no
log.Fatalf("error: %q appears to be installed with the 'setuid' bit set, which is an *extremely* insecure and completely unsupported configuration! (what you want instead is likely 'sudo' or 'su')", os.Args[0])
} else if fi.Mode()&os.ModeSetgid != 0 {
exit(1, os.Stderr, "error:", os.Args[0], "appears to be installed with the 'setuid' bit set, which is an *extremely* insecure and completely unsupported configuration! (what you want instead is likely 'sudo' or 'su')")
} else if mode&os.ModeSetgid != 0 {
// ... oh no
log.Fatalf("error: %q appears to be installed with the 'setgid' bit set, which is not quite *as* insecure as 'setuid', but still not great, and definitely a completely unsupported configuration! (what you want instead is likely 'sudo' or 'su')", os.Args[0])
exit(1, os.Stderr, "error:", os.Args[0], "appears to be installed with the 'setgid' bit set, which is not quite *as* insecure as 'setuid', but still not great, and definitely a completely unsupported configuration! (what you want instead is likely 'sudo' or 'su')")
}
}

if len(os.Args) >= 2 {
switch os.Args[1] {
case "--help", "-h", "-?":
fmt.Println(usage())
os.Exit(0)
exit(0, os.Stdout, usage())
case "--version", "-v":
fmt.Println(version())
os.Exit(0)
exit(0, os.Stdout, version())
}
}
if len(os.Args) <= 2 {
log.Println(usage())
os.Exit(1)
exit(1, os.Stderr, usage())
}

// clear HOME so that SetupUser will set it
os.Unsetenv("HOME")

if err := SetupUser(os.Args[1]); err != nil {
log.Fatalf("error: failed switching to %q: %v", os.Args[1], err)
exit(1, os.Stderr, "error: failed switching to '"+os.Args[1]+"':", err.Error())
}

name, err := exec.LookPath(os.Args[2])
if err != nil {
log.Fatalf("error: %v", err)
exit(1, os.Stderr, "error:", err.Error())
}

if err = syscall.Exec(name, os.Args[2:], os.Environ()); err != nil {
log.Fatalf("error: exec failed: %v", err)
exit(1, os.Stderr, "error: exec failed:", err.Error())
}
}

0 comments on commit 04fac5a

Please sign in to comment.