-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
454 lines (364 loc) · 11.9 KB
/
Copy pathinstall.sh
File metadata and controls
454 lines (364 loc) · 11.9 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/bin/bash
set -e
CONFIG_DIR="/etc/siglatch"
KEY_FILE="$CONFIG_DIR/server_priv.pem"
CONFIG_FILE="$CONFIG_DIR/server.conf"
DEFAULT_CONFIG="./default_config/server.conf"
function is_macos() {
[[ "$(uname)" == "Darwin" ]]
}
function do_chown() {
if is_macos; then
chown root "$1"
else
chown root:root "$1"
fi
}
construct_install_dirs() {
echo "📦 Creating Siglatch install directories..."
local base="/etc/siglatch"
local scripts_dir="$base/scripts"
local log_dir="/var/log/siglatch"
local state_dir="/var/lib/siglatch"
# Create base directories
mkdir -p "$scripts_dir"
mkdir -p "$log_dir"
mkdir -p "$state_dir"
# Create symlinks inside /etc/siglatch
ln -snf "$log_dir" "$base/logs"
ln -snf "$state_dir" "$base/state"
# Copy core daemon binary
cp ./siglatchd "$base/" || {
echo "❌ Failed to copy siglatchd"
return 1
}
# Place `knocker` into system binary path
# Recommend: /usr/local/bin (not /etc)
cp ./knocker /usr/local/bin/knocker || {
echo "❌ Failed to install knocker"
return 1
}
chmod +x /usr/local/bin/knocker
echo "✅ Install directories and binaries set up successfully."
}
validate_key() {
echo "🔍 Validating private key at $KEY_FILE..."
if [ ! -f "$KEY_FILE" ]; then
echo "❌ Key file does not exist: $KEY_FILE"
return 1
fi
# Check if it's a valid RSA private key
if openssl rsa -in "$KEY_FILE" -check -noout > /dev/null 2>&1; then
echo "✅ RSA private key is valid."
# Show fingerprint or key info
openssl rsa -in "$KEY_FILE" -pubout -outform PEM | openssl rsa -pubin -pubout -text -noout | grep 'Modulus\|Exponent'
else
echo "❌ Invalid RSA private key or wrong format."
return 1
fi
}
validate_permissions() {
echo "🔍 Validating permissions on $KEY_FILE..."
if [ ! -f "$KEY_FILE" ]; then
echo "❌ Key file does not exist: $KEY_FILE"
return 1
fi
# Check permissions (should be 600)
perms=$(stat -f "%Lp" "$KEY_FILE" 2>/dev/null || stat -c "%a" "$KEY_FILE")
if [ "$perms" != "600" ]; then
echo "❌ Incorrect permissions: expected 600, got $perms"
return 1
fi
# Check ownership
owner=$(stat -f "%Su" "$KEY_FILE" 2>/dev/null || stat -c "%U" "$KEY_FILE")
group=$(stat -f "%Sg" "$KEY_FILE" 2>/dev/null || stat -c "%G" "$KEY_FILE")
if [ "$owner" != "root" ]; then
echo "❌ File must be owned by root. Found owner: $owner"
return 1
fi
if is_macos && [ "$group" != "wheel" ]; then
echo "❌ Group should be 'wheel' on macOS. Found: $group"
return 1
elif ! is_macos && [ "$group" != "root" ]; then
echo "❌ Group should be 'root' on Linux. Found: $group"
return 1
fi
echo "✅ Permissions and ownership are valid."
}
function install_config() {
echo "📄 Installing default config..."
cp "$DEFAULT_CONFIG" "$CONFIG_FILE"
}
function generate_key() {
echo "🔑 Generating RSA private key..."
openssl genpkey -algorithm RSA -out "$KEY_FILE" -pkeyopt rsa_keygen_bits:2048
chmod 600 "$KEY_FILE"
do_chown "$KEY_FILE"
}
create_userhmac() {
local username="$1"
local force="$2"
if [ -z "$username" ]; then
echo "❌ Usage: create_userhmac <username> [--force]"
exit 1
fi
local hmac_file="$CONFIG_DIR/keys/${username}.hmac.key"
if [ -f "$hmac_file" ]; then
if [ "$force" != "--force" ]; then
echo "⚠️ HMAC key already exists for user '$username'. Use --force to overwrite."
exit 1
else
echo "⚠️ Overwriting existing HMAC key for '$username'..."
fi
fi
echo "🔐 Generating HMAC key for user '$username'..."
# Generate random 32-byte HMAC key
openssl rand -out "$hmac_file" 32
chmod 600 "$hmac_file"
do_chown "$hmac_file"
echo "✅ HMAC key created:"
echo " 🔒 HMAC Key: $hmac_file"
}
export_userhmac() {
local username="$1"
if [ -z "$username" ]; then
echo "❌ Usage: export_userhmac <username>"
exit 1
fi
local hmac_file="$CONFIG_DIR/keys/${username}.hmac.key"
if [ ! -f "$hmac_file" ]; then
echo "❌ HMAC key for user '$username' does not exist."
exit 1
fi
echo "📤 Exporting HMAC key for user '$username':"
cat "$hmac_file" | xxd -p
}
show_help() {
cat <<EOF
🔐 siglatch install script — secure daemon key + config bootstrap
Usage:
sudo ./install.sh <command> [options]
Core Commands:
install One-time install. Fails if already present.
reinstall Replaces config and regenerates server key.
new_key Generates a new server private key.
repair_key Fixes permissions and validates existing key.
help Show this help message.
Key Management:
export_pubkey Output the server's public key (for client config)
create_userkey <user> Generate a new RSA keypair for a user
export_userkey <user> Output a user's private key (e.g. for email)
install_userkey <user> [file] [--overwrite]
Install a user public key from file or prompt
install_ip_auth installs the ip auth scripts into siglatch installation
create_userhmac <user> [--force]
Generate a new random HMAC key for a user
export_userhmac <user>
Output a user's HMAC key (hex encoded for client use)
Configuration Note:
- After installing a user key, be sure to update your config with an entry like:
[user:your_username_here]
enabled = yes
key_file = /etc/siglatch/keys/root.key
hmac_file = /etc/siglatch/keys/root.hmac.key
actions = grant_ip, run_script
Notes:
- This script must be run as root.
- Config will be created in /etc/siglatch/
- Keys are stored in /etc/siglatch/keys/
- All PEM and HMAC files must be mode 600 and root-owned
EOF
exit 0
}
do_repair_key() {
if [ -f "$KEY_FILE" ]; then
echo "🔧 Repairing permissions for existing key..."
chmod 600 "$KEY_FILE"
do_chown "$KEY_FILE"
echo "✅ Key permissions repaired."
validate_permissions
validate_key
else
echo "❌ No key found to repair at $KEY_FILE."
exit 1
fi
}
do_new_key() {
echo "🗝️ Forcing new private key generation..."
generate_key
validate_permissions
validate_key
}
do_reinstall() {
echo "♻️ Reinstalling config and regenerating key..."
mkdir -p "$CONFIG_DIR"
construct_install_dirs
install_config
install_ipauth_scripts
generate_key
validate_permissions
validate_key
}
do_install() {
if [ -d "$CONFIG_DIR" ]; then
echo "⚠️ $CONFIG_DIR already exists. Use reinstall or other options."
show_help
exit 1
fi
echo "📁 Creating $CONFIG_DIR..."
mkdir -p "$CONFIG_DIR"
construct_install_dirs
install_config
install_ipauth_scripts
generate_key
validate_permissions
validate_key
}
export_pubkey() {
echo "🔓 Exporting server public key..."
if [ ! -f "$KEY_FILE" ]; then
echo "❌ Server private key not found at $KEY_FILE"
exit 1
fi
if ! openssl rsa -in "$KEY_FILE" -pubout 2>/dev/null; then
echo "❌ Failed to extract public key. Check key format and permissions."
exit 1
fi
}
create_userkey() {
local username="$1"
local force="$2"
if [ -z "$username" ]; then
echo "❌ Usage: create_userkey <username> [--force]"
exit 1
fi
local priv_file="$CONFIG_DIR/keys/${username}.private.pem"
local pub_file="$CONFIG_DIR/keys/${username}.pub.pem"
# Check for existing keys
if [ -f "$priv_file" ] || [ -f "$pub_file" ]; then
if [ "$force" != "--force" ]; then
echo "⚠️ Key files already exist for user '$username'. Use --force to overwrite."
exit 1
else
echo "⚠️ Overwriting existing keys for '$username'..."
fi
fi
echo "🔐 Generating keypair for user '$username'..."
# Generate private key
openssl genpkey -algorithm RSA -out "$priv_file" -pkeyopt rsa_keygen_bits:2048
chmod 600 "$priv_file"
do_chown "$priv_file"
# Extract public key
openssl rsa -in "$priv_file" -pubout -out "$pub_file"
chmod 600 "$pub_file"
do_chown "$pub_file"
echo "✅ Keypair created:"
echo " 🔑 Private: $priv_file"
echo " 📢 Public : $pub_file"
}
export_userkey() {
local username="$1"
if [ -z "$username" ]; then
echo "❌ Usage: export_userkey <username>"
exit 1
fi
local priv_file="$CONFIG_DIR/keys/${username}.private.pem"
if [ ! -f "$priv_file" ]; then
echo "❌ No private key found for user '$username' at $priv_file"
exit 1
fi
echo "🔐 Exporting private key for '$username'..."
cat "$priv_file"
}
install_userkey() {
local username="$1"
local arg2="$2"
local arg3="$3"
if [ -z "$username" ]; then
echo "❌ Usage: install_userkey <username> [file] [--overwrite]"
exit 1
fi
# Normalize args
local source=""
local flag=""
if [[ "$arg2" == "--overwrite" ]]; then
flag="--overwrite"
elif [[ -n "$arg2" ]]; then
source="$arg2"
[[ "$arg3" == "--overwrite" ]] && flag="--overwrite"
fi
local dest="$CONFIG_DIR/keys/${username}.pub.pem"
local priv="$CONFIG_DIR/keys/${username}.private.pem"
if [ -f "$dest" ] && [ "$flag" != "--overwrite" ]; then
echo "⚠️ Key already exists for '$username' at $dest. Use --overwrite to replace it."
exit 1
fi
echo "📥 Installing user key for '$username'..."
# Always delete private key (policy)
if [ -f "$priv" ]; then
echo "⚠️ Deleting existing private key: $priv"
rm -f "$priv"
fi
if [ -n "$source" ] && [ -f "$source" ]; then
echo "📄 Reading key from file: $source"
cp "$source" "$dest"
else
echo "📋 No file provided. Paste the PEM-formatted public key below, then press Ctrl+D:"
cat > "$dest"
fi
if ! grep -q "BEGIN PUBLIC KEY" "$dest"; then
echo "❌ File does not appear to be a valid PEM public key."
rm -f "$dest"
exit 1
fi
chmod 600 "$dest"
do_chown "$dest"
echo "✅ Installed public key to: $dest"
}
install_ipauth_scripts() {
echo "🔧 Installing Siglatch ipAuth scripts..."
# Define base paths
local src_dir="./scripts"
local config_src="./default_config/ipAuth"
local target_base="/etc/siglatch/scripts"
local config_target="$target_base/ipAuth/config"
# Copy script directory
mkdir -p "$target_base"
cp -r "$src_dir"/* "$target_base/" || {
echo "❌ Failed to copy scripts"
return 1
}
# Ensure config directory exists
mkdir -p "$config_target"
# Copy default config files
cp "$config_src"/* "$config_target/" || {
echo "❌ Failed to copy config files"
return 1
}
echo "✅ Scripts and config installed to /etc/siglatch/scripts/ipAuth"
}
echo "🔐 Installing siglatch system configuration..."
if [[ $EUID -ne 0 ]]; then
echo "❌ This script must be run as root." >&2
exit 1
fi
main() {
case "$1" in
repair_key) do_repair_key ;;
new_key) do_new_key ;;
reinstall) do_reinstall ;;
install_ip_auth) install_ipauth_scripts ;;
install) do_install ;;
export_pubkey) export_pubkey ;;
create_userkey) create_userkey "$2" "$3" ;;
export_userkey) export_userkey "$2" ;;
install_userkey) install_userkey "$2" "$3" "$4" ;;
create_userhmac) create_userhmac "$2" "$3" ;; # NEW
export_userhmac) export_userhmac "$2" ;; # NEW
help) show_help ;;
"") show_help ;;
*) echo "❌ Unknown option: $1"; show_help ;;
esac
}
main "$@"
echo "✅ siglatch setup complete."