Debian Codegraph @

src/ws/cockpit-desktop.in

raw
1 #!/bin/bash
2
3 #
4 # Copyright (C) 2018 Red Hat, Inc.
5 # SPDX-License-Identifier: LGPL-2.1-or-later
6
7
8 # Run a local bridge, web server, and browser for a particular Cockpit page.
9 # This is useful for integration into .desktop files, for systems which don't
10 # have cockpit.socket enabled. The web server and browser run in an unshared
11 # network namespace, and thus are totally isolated from everything else.
12 #
13 # Examples:
14 # cockpit-desktop /cockpit/@localhost/system/index.html
15 # cockpit-desktop network/firewall
16 # cockpit-desktop users
17 #
18 # As an experimental/demo feature, the bridge can also be started on a remote
19 # ssh host. The host name is given as (optional) second argument, which is
20 # passed verbatim to ssh.
21 #
22 # Example:
23 # cockpit-desktop system svr1
24 # cockpit-desktop / username@svr1
25 set -eu
26
27 # shellcheck disable=SC2034 # exec_prefix= is set because the default @libexecdir@ contains "${exec_prefix}"
28 exec_prefix="@prefix@"
29
30 # start browser in a temporary home dir, so that it does not interfere with your real one
31 BROWSER_HOME=$(mktemp --directory --tmpdir cockpit.desktop.XXXXXX)
32 export BROWSER_HOME
33
34 # find suitable browser, unless already set by $BROWSER
35 # We can't use xdg-open, it does too much magic behind the back to connect to
36 # existing instances (outside of our namespace) and does not allow us to reduce
37 # the UI, or pass options like chromium's --no-sandbox.
38 detect_browser()
39 {
40 [ -z "${BROWSER:-}" ] || return 0
41
42 # First choice, but it depends on gi.repository WebKit2, so check it
43 if @libexecdir@/cockpit-client --help >/dev/null 2>/dev/null; then
44 BROWSER="@libexecdir@/cockpit-client --disable-uniqueness --no-ui --external-ws"
45 return 0
46 fi
47
48 for browser in chromium-browser chromium google-chrome; do
49 if type $browser >/dev/null 2>&1; then
50 # need to disable sandboxing in user namespace, but that already isolates
51 # TODO: Find a way to disable the URL bar
52 BROWSER="$browser --no-sandbox --disable-infobars"
53 return 0
54 fi
55 done
56
57 # create firefox profile
58 FIREFOX_PROFILE="$BROWSER_HOME/profile"
59 mkdir "$FIREFOX_PROFILE"
60 cat <<EOF > "$FIREFOX_PROFILE/user.js"
61 user_pref("datareporting.policy.dataSubmissionEnabled", false);
62 user_pref("toolkit.telemetry.reportingpolicy.firstRun", false);
63 user_pref("browser.toolbars.bookmarks.visibility", "never");
64 EOF
65
66 if type firefox >/dev/null 2>&1; then
67 # TODO: Find a way to disable the URL bar
68 BROWSER="firefox --profile $FIREFOX_PROFILE --no-remote"
69 return 0
70 fi
71
72 for id in org.mozilla.firefox org.mozilla.Firefox; do
73 if flatpak info $id >/dev/null 2>&1; then
74 BROWSER="flatpak run --filesystem=$BROWSER_HOME $id --profile $FIREFOX_PROFILE --no-remote"
75 return 0
76 fi
77 done
78
79 # TODO: is there a simple way to use webkitgtk?
80 echo "No suitable browser found (Chromium/Chrome, or Firefox)" >&2
81 exit 1
82 }
83
84
85 if [ -z "${1:-}" ]; then
86 echo "Usage: $0 <Cockpit path> [ssh host]" >&2
87 exit 1
88 fi
89
90 # Expand the commandline argument into a url
91 case "$1" in
92 /*)
93 URL_PATH="$1"
94 ;;
95 */)
96 URL_PATH="/cockpit/@localhost/$1index.html"
97 ;;
98 */*)
99 URL_PATH="/cockpit/@localhost/$1.html"
100 ;;
101 *)
102 URL_PATH="/cockpit/@localhost/$1/index.html"
103 ;;
104 esac
105
106 detect_browser
107
108 # start the bridge; this needs to run in the normal user session/namespace
109 coproc ${2:+ssh "$2"} cockpit-bridge
110 trap "kill $COPROC_PID; wait $COPROC_PID || true" EXIT INT QUIT PIPE
111
112 # start ws and browser in a detached network namespace
113 SCRIPT='
114 set -eu
115 # new namespaces have lo down by default
116 ip link set lo up >&2
117
118 # forward parent stdin and stdout (from bridge) to cockpit-ws
119 # it pretty well does not matter which port we use in our own namespace, so use standard http
120 # disable /etc/cockpit/
121 XDG_CONFIG_DIRS="$BROWSER_HOME" COCKPIT_SUPERUSER="pkexec" '${COCKPIT_WS:-@libexecdir@/cockpit-ws}' -p 80 -a 127.0.0.90 --local-session=- <&0 >&1 &
122 WS_PID=$!
123 # ... and stop using that stdin/out for everything else
124 exec 0</dev/null
125 exec 1>&2
126
127 trap "set +e; kill $WS_PID; wait $WS_PID; rm -rf $BROWSER_HOME" EXIT INT QUIT PIPE
128
129 # if we have netcat, use it for waiting until ws is up
130 if type nc >/dev/null 2>&1; then
131 for retry in `seq 10`; do
132 nc -z 127.0.0.90 80 && break
133 sleep 0.5;
134 done
135 else
136 # otherwise, just wait a bit
137 sleep 3
138 fi
139
140 HOME="$BROWSER_HOME" '$BROWSER' http://127.0.0.90'"$URL_PATH"'
141 '
142 unshare --user --map-root-user --net /bin/bash -c "$SCRIPT" <&${COPROC[0]} >&${COPROC[1]}