summaryrefslogtreecommitdiffstats
path: root/pkgs/simple/su2.nix
blob: a9a92b10d4f535581ac79966de68fcc345c5b8db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{ pkgs }:

pkgs.writeDashBin "su2" ''
  # usage: su2 [xauth=auto|disable|trusted|untrusted] user=USER [-- COMMAND [ARGS...]]

  set -efu

  xauth=''${xauth-auto}
  while test $# -gt 0; do
    case $1 in
      xauth=auto|xauth=disable|xauth=trusted|xauth=untrusted)
        xauth=''${1#xauth=}
        shift
        ;;
      user=*)
        user=''${1#user=}
        shift
        ;;
      --)
        shift
        break
        ;;
      *)
        echo "$0: error: bad argument $1" >&2
        exit 1
    esac
  done

  if test "$xauth" = auto; then
    xauth=disable
    if test "$user" != root && test -z "''${SSH_CLIENT-}"; then
      case ''${DISPLAY-} in
        :8)
          xauth=untrusted
          ;;
        :11)
          xauth=trusted
          ;;
      esac
    fi
  fi

  case $user in
    root)
      case $xauth in
        trusted)
          ${pkgs.xauth-user}/bin/xauth-user "$user" --trusted
          ;;
        untrusted)
          ${pkgs.xauth-user}/bin/xauth-user "$user"
          ;;
        disable)
          :
          ;;
        *)
          echo "$0: error: bad xauth value: $xauth" >&2
          exit 2
      esac
      if test -n "''${SSH_CLIENT-}"; then
        exec /run/wrappers/bin/su - -c "env SSH_CLIENT='$SSH_CLIENT' '$SHELL'"
      else
        case $TERM in alacritty)
          alacritty msg config 'colors.primary.background="#230000"'
          alacritty msg config 'colors.primary.foreground="#e0c0c0"'
          alacritty msg config 'colors.normal.black="#800000"'
        esac
        exec /run/wrappers/bin/su -
      fi
      ;;
    *)
      INIT=$(
        case $xauth in
          trusted|untrusted)
            XCOOKIE=$(
              umask 0077
              tmpfile=$XDG_RUNTIME_DIR/su2.$$.xauth
              trusted=$xauth
              trap '${pkgs.coreutils}/bin/rm "$tmpfile"' EXIT
              ${pkgs.coreutils}/bin/touch "$tmpfile"
              ${pkgs.xauth}/bin/xauth -f "$tmpfile" generate "$DISPLAY" . "$trusted" timeout 86400
              ${pkgs.xauth}/bin/xauth -f "$tmpfile" extract - "$DISPLAY" | ${pkgs.coreutils}/bin/base64
            )
            export XCOOKIE
            ;;
          disable)
            :
            ;;
          *)
            echo "$0: error: bad xauth value: $xauth" >&2
            exit 2
        esac
        export SU2PID=$$
        ${pkgs.jq}/bin/jq -nr '
          def to_shell_vars(x):
            x | to_entries | map(
              select(.value != null) |
              "\(.key)=\(.value|@sh)"
            ) | join(" ");

          env | [
            "export \(to_shell_vars({
              COLORTERM,
              DISPLAY,
              SSH_CLIENT,
            }))",
            "\(to_shell_vars({
              SU2PID,
              XCOOKIE,
            }));"
          ] | join(";")
        '
      )
      export INIT

      exec machinectl shell -E INIT "$user"@.host ${pkgs.writeDash "login" ''
        eval "$INIT"
        unset INIT
        if test "''${XCOOKIE+x}" = x; then
          echo "$XCOOKIE" | ${pkgs.coreutils}/bin/base64 -d | ${pkgs.xauth}/bin/xauth merge -
          unset XCOOKIE
        fi
        if test $# = 0; then
          set -- "$SHELL"
        fi
        # dtach-list finds su2 sessions by this process name.
        exec ${pkgs.otherutils}/bin/exec -n "(su2 $SU2PID $1)" "$@"
      ''} "$@"
      ;;
  esac
''