@@ -0,0 +1,339 @@
1+diff --git a/gale-install.in b/gale-install.in
2+index 50e8ad8..eec0ed2 100644
3+--- a/gale-install.in
4++++ b/gale-install.in
5+@@ -29,22 +29,78 @@ testkey_stdin() {
6+ gkinfo -x 2>/dev/null | qgrep "^Public key: <$1>"
7+ }
8+
9+-if [ -n "$GALE_SYS_DIR" ]; then
10+- SYS_DIR="$GALE_SYS_DIR"
11+-elif [ -n "$sysconfdir" ]; then
12+- SYS_DIR="$sysconfdir/gale"
13++INST_SYS_DIR="$sysconfdir/gale"
14++
15++if [ `id -u` -eq 0 ]; then
16++ is_root=yes
17++ SYS_DIR=/etc/gale
18++else
19++ is_root=no
20++ SYS_DIR="$HOME/.gale"
21++fi
22++
23++if [ -f /etc/NIXOS ]; then
24++ is_nixos=yes
25++else
26++ is_nixos=no
27++fi
28++
29++if [ -u /var/setuid-wrappers/gksign ]; then
30++ cat <<EOM
31++
32++Gale appears to have already been set up via the NixOS module system (check
33++/etc/nixos/configuration.nix).
34++
35++EOM
36++
37++ exit 0
38++fi
39++
40++if [ $is_root = yes ]; then
41++ cat <<EOM
42++
43++You are running gale-install as root, so I'm going to assume you want to set up
44++Gale for system-wide use. Unfortunately, Gale expects to be able to run the
45++"gksign" program via setuid, and we can't change gksign's permissions once it's
46++already been installed because the Nix store is immutable by design.
47++
48++EOM
49++
50++ if [ $is_nixos = no ]; then
51++ cat <<EOM
52++
53++This isn't a NixOS system. Gale can only be installed for a single user on
54++non-NixOS systems. If you want a system-wide installation, you'll have to
55++install Gale the old-fashioned way. Sorry.
56++
57++EOM
58++
59++ exit 1
60++ fi
61++ cat <<EOM
62++
63++This script will generate a domain key for you, and after you get it signed, you
64++can finish the setup process by configuring Gale via the NixOS module system
65++(i.e. /etc/nixos/configuration.nix).
66++
67++EOM
68++
69+ else
70+- echo "Error: cannot locate Gale system configuration directory."
71+- exit 1
72++ cat <<EOM
73++
74++Hi. You are running gale-install as a normal user, so I'm going to assume you
75++want to set up Gale for your own (i.e. non-system-wide) use.
76++
77++EOM
78++
79+ fi
80+
81+-CONF="$SYS_DIR/conf"
82+ umask 022
83+-PATH="$bindir:$sbindir:$PATH:/usr/ucb"
84++PATH="$bindir:$sbindir:$PATH"
85+ export PATH
86+
87+-if [ -f "$CONF" ]; then
88+- exec 3<"$CONF"
89++if [ -n "$SYS_DIR" ] && [ -f "$SYS_DIR/conf" ]; then
90++ exec 3<"$SYS_DIR/conf"
91+
92+ while read var value <&3 ; do
93+ case "$var" in
94+@@ -58,52 +114,14 @@ if [ -f "$CONF" ]; then
95+ exec 3<&-
96+ fi
97+
98+-USER="`whoami`"
99+-if [ -z "$USER" ]; then
100+- echo "Error: cannot determine your username."
101+- exit 1
102+-fi
103+-
104+-# -----------------------------------------------------------------------------
105+-# check directory structure
106+-
107+-if [ ! -d "$SYS_DIR" ]; then
108+- echo "Error: Invalid SYS_DIR: \"$SYS_DIR\"."
109+- exit 1
110+-fi
111+-
112+ # -----------------------------------------------------------------------------
113+-# get settings for important variables: user, domain, server
114++# get settings for important variables: domain, server
115+
116+-[ -n "$CONF_GALE_USER" ] && GALE_USER="$CONF_GALE_USER"
117+ [ -n "$CONF_GALE_DOMAIN" ] && GALE_DOMAIN="$CONF_GALE_DOMAIN"
118+ [ -n "$CONF_GALE_SERVER" ] && GALE_SERVER="$CONF_GALE_SERVER"
119+
120+-if [ -z "$GALE_USER" ]; then
121+-cat << EOM
122+-
123+-Hi. You need to denote a user to own the Gale domain secret key. You must
124+-trust this user with Gale authentication for your domain; the "gksign" program
125+-will run as this user. I recommend using a special "gale" user; if you don't
126+-have administrative privileges here, you'll probably have to use your own
127+-account. I do not recommend the use of "root".
128+-
129+-No harm done if you stop this script now to set up such a user.
130+-
131+-EOM
132+-
133+- printf "Enter the Gale username: "
134+- read GALE_USER
135+- if [ -z "$GALE_USER" ]; then
136+- echo "Error: Invalid username or no home dir: \"$GALE_USER\"."
137+- exit 1
138+- fi
139+-else
140+- echo "Using \"$GALE_USER\" as the Gale owner."
141+-fi
142+-
143+ if [ -z "$GALE_DOMAIN" ] ; then
144+-cat << EOM
145++ cat << EOM
146+
147+ You need to identify the name of your Gale domain for two purposes:
148+
149+@@ -140,97 +158,56 @@ else
150+ fi
151+
152+ # -----------------------------------------------------------------------------
153+-# create configuration file
154+-
155+-if [ ! -f "$CONF" ]; then
156+-cat > "$CONF" <<EOM
157+-# $CONF -- created by Gale installer; edit to suit.
158+-EOM
159++# create configuration file (if this is a single-user setup)
160++
161++if [ $is_root = no ]; then
162++ CONF="$SYS_DIR/conf"
163++
164++ if [ ! -d "$SYS_DIR" ]; then
165++ run mkdir -m 0700 -p "$SYS_DIR"
166++ run mkdir -m 0700 -p "$SYS_DIR/auth"
167++ run mkdir -m 0755 -p "$SYS_DIR/auth/trusted"
168++ run cp -f "$INST_SYS_DIR/auth/trusted/ROOT" "$SYS_DIR/auth/trusted"
169++ run mkdir -m 0700 -p "$SYS_DIR/auth/private"
170++ run mkdir -p "$SYS_DIR/auth/local" "$SYS_DIR/auth/cache"
171++ fi
172+
173+-cat <<EOM
174++ if [ ! -f "$CONF" ]; then
175++ cat <<EOM
176+
177+ *** Creating "$CONF".
178+ Examine and edit this file to your taste and local needs.
179+ If you want to recreate it from scratch, remove it and re-run this.
180+-EOM
181+-fi
182+-
183+-[ -n "$CONF_GALE_USER" ] || cat >> "$CONF" <<EOM
184+
185+-# The user who owns the domain secret key. (Used in installation and upgrade)
186+-GALE_USER $GALE_USER
187+ EOM
188+
189+-[ -n "$CONF_GALE_DOMAIN" ] || cat >> "$CONF" << EOM
190++ cat > "$CONF" <<EOF
191++# $CONF -- created by Gale installer; edit to suit.
192+
193+ # The authentication domain to use. (Mandatory)
194+ GALE_DOMAIN $GALE_DOMAIN
195+-EOM
196+-
197+-# -----------------------------------------------------------------------------
198+-# make gksign setuid
199++EOF
200+
201+-gksign="$sbindir/gksign"
202+-readlink="`which readlink 2>/dev/null`"
203+-[ -x "$readlink" ] && gksignlink="`"$readlink" "$gksign" 2>/dev/null`"
204+-[ -f "$gksignlink" ] && gksign="$gksignlink"
205+-
206+-echo ""
207+-if copy chown "$GALE_USER" "$gksign" ; then
208+- :
209+-else
210+- echo "*** We need to chown $GALE_USER '$gksign'."
211+- echo " Please run this script as a user that can do so,"
212+- echo " or do so yourself and re-run this script."
213+- exit 1
214++ fi
215+ fi
216+-run chmod 4755 "$gksign"
217+
218+-# -----------------------------------------------------------------------------
219+-# create a domain, if necessary
220++if [ $is_root = no ]; then
221++ GALE_SYS_DIR="$SYS_DIR"
222++ export GALE_SYS_DIR
223+
224+-echo ""
225+-if test -u "$gksign" || copy chmod u+s "$gksign" ; then
226+- :
227++ testkey "$GALE_DOMAIN" && exit 0
228++ echo "*** You lack a signed key for your domain, \"$GALE_DOMAIN\"."
229++ GALE="$SYS_DIR"
230+ else
231+- echo "*** We need to chmod u+s '$gksign'."
232+- echo " Please run this script as a user that can do so,"
233+- echo " or do so yourself and re-run this script."
234+- exit 1
235+-fi
236+-
237+-testkey "$GALE_DOMAIN" && exit 0
238+-echo "*** You lack a signed key for your domain, \"$GALE_DOMAIN\"."
239+-
240+-if [ "x$GALE_USER" != "x$USER" ]; then
241+-cat <<EOM
242+-Become user "$GALE_USER" (you are now "$USER") and run this script again
243+-to create a new domain; contact your domain administrator if you wish to
244+-become part of an existing domain.
245+-EOM
246+- exit 1
247+-fi
248+-
249+-if [ -z "$HOME" ]; then
250+- cd
251+- HOME="`pwd`"
252+- echo "Warning: \$HOME not set, assuming \"$HOME\"."
253+-fi
254+-
255+-GALE="$HOME/.gale"
256+-mkdir -p "$GALE"
257+-if [ ! -w "$GALE" ]; then
258+- echo "Error: Cannot create \"$GALE\"."
259+- exit 1
260++ GALE="`pwd`"
261+ fi
262+
263+ unsigned="$GALE/$GALE_DOMAIN.unsigned"
264+ signer="`echo "$GALE_DOMAIN" | sed 's%^[^.@:/]*[.@:/]%%'`"
265+ [ -z "$signer" ] && signer="ROOT"
266+
267+-if [ -f "$unsigned" ]; then
268+-
269+-cat << EOM
270++if [ $is_root = no ] && [ -f "$unsigned" ]; then
271++ cat << EOM
272+
273+ Okay, so we've already been here. Last time, I created the file
274+ "$unsigned" for you to have signed.
275+@@ -275,16 +252,12 @@ EOM
276+ exit 1
277+ fi
278+
279+-cat << EOM
280+-
281+-The domain should be properly configured now. Assuming users can access a
282+-version of "gksign" setuid to "$GALE_USER" (this user), they should be
283+-able to start running clients and generating IDs for themselves.
284++ cat << EOM
285+
286+-The installation process is complete!
287++The domain should be properly configured now. The installation process is
288++complete!
289+
290+ EOM
291+-
292+ exit 0
293+ fi
294+
295+@@ -300,7 +273,10 @@ printf "Enter the description: "
296+ read descr
297+
298+ echo "We will generate the key now. Have patience."
299+-gkgen -r "$GALE/auth/private/$GALE_DOMAIN.gpri" \
300++if [ $is_root = no ]; then
301++ install_gpri=auth/private/
302++fi
303++gkgen -r "$GALE/${install_gpri}$GALE_DOMAIN.gpri" \
304+ -u "$unsigned" "$GALE_DOMAIN" /"$descr" || exit 1
305+
306+ cat << EOM
307+@@ -311,8 +284,29 @@ the owner of the "$signer" domain through appropriate means.
308+ Take care to preseve the file's binary data; you may need to uuencode it.
309+
310+ Assuming they trust you with your subdomain, they should pass the key through
311+-"gksign" as a filter, returning the signed output to you. When you have this
312+-signed key file available, re-run this script, and we will move on to the
313+-next step.
314++"gksign" as a filter, returning the signed output to you.
315++
316++EOM
317++
318++if [ $is_nixos = yes ]; then
319++ cat <<EOM
320++
321++When you have this signed key file available, you can finish the setup process
322++by placing the private key and signed public key in a directory (they will be
323++copied) and setting "services.gale.keyPath" in /etc/nixos/configuration.nix.
324+
325+ EOM
326++else
327++ cat <<EOM
328++
329++When you have this signed key file available, re-run this script, and we will
330++move on to the next step.
331++
332++*** IMPORTANT ***
333++You must set GALE_SYS_DIR="$GALE_SYS_DIR" in your environment before re-running
334++this script and before running any Gale programs once setup is finished! Make
335++sure to set it now, and consider setting it via your shell initialization files
336++so that you don't forget to do it next login.
337++
338++EOM
339++fi