blob: f8d7a42564c7d60d5629143147d28f10ea347776 (
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
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
|
#!/usr/bin/env ruby
require 'etc'
require 'fileutils'
require 'optparse'
require 'time'
RUNTIME_DIR = ENV['XDG_RUNTIME_DIR'] || "/run/user/#{Process.uid}"
CACHE_DIR = File.join(RUNTIME_DIR, "syca")
CACHE_FILE = File.join(CACHE_DIR, "timestamp")
CONFIG_FILE = File.join(CACHE_DIR, "config")
HELPER = "/usr/lib/syca-helper"
PAM_HELPER = "/usr/lib/syca-pam-auth.py"
# Default passwordless duration (minutes)
DEFAULT_MINUTES = 3
# Ensure user is in wheel group
def ensure_wheel_member
wheel_gid = Etc.getgrnam('wheel').gid rescue nil
if wheel_gid.nil?
puts "[syca] ERROR: group 'wheel' does not exist!"
exit 1
end
unless Process.groups.include?(wheel_gid) || Process.gid == wheel_gid
puts "[syca] Permission denied: user is not in wheel group"
exit 1
end
end
# Load passwordless duration from config
def load_config
if File.exist?(CONFIG_FILE)
Integer(File.read(CONFIG_FILE).strip) rescue DEFAULT_MINUTES
else
DEFAULT_MINUTES
end
end
# Save passwordless duration
def save_config(minutes)
FileUtils.mkdir_p(CACHE_DIR)
File.write(CONFIG_FILE, minutes.to_s)
end
# Save current timestamp
def save_timestamp
FileUtils.mkdir_p(CACHE_DIR)
File.write(CACHE_FILE, Time.now.to_i.to_s)
end
# Load cached timestamp
def load_timestamp
if File.exist?(CACHE_FILE)
Integer(File.read(CACHE_FILE).strip) rescue nil
else
nil
end
end
# Clear timestamp cache
def clear_cache
File.delete(CACHE_FILE) if File.exist?(CACHE_FILE)
end
# Disable passwordless mode
def disable_cache
clear_cache
File.delete(CONFIG_FILE) if File.exist?(CONFIG_FILE)
puts "[syca] Passwordless mode disabled."
end
# Check if cache is still valid
def cache_valid?
ts = load_timestamp
return false if ts.nil? || !File.exist?(CONFIG_FILE)
(Time.now.to_i - ts) < load_config * 60
end
# Seconds left in passwordless mode
def seconds_left
ts = load_timestamp
return 0 if ts.nil? || !File.exist?(CONFIG_FILE)
[(load_config * 60 - (Time.now.to_i - ts)), 0].max
end
# Run PAM authentication once
def pam_authenticate_once
system(PAM_HELPER)
end
# Loop until authentication succeeds
def pam_authenticate_loop
loop do
return true if pam_authenticate_once
puts "[syca] Incorrect password. Try again."
end
end
# Enable passwordless mode
def enable_cache(minutes)
save_config(minutes)
puts "[syca] Enabling passwordless mode for #{minutes} minutes..."
if pam_authenticate_loop
save_timestamp
puts "[syca] Passwordless mode enabled for #{minutes} minutes."
else
puts "[syca] Authentication failed. Cache not enabled."
end
end
# Run requested command (pipe-safe)
def run_command(cmd)
begin
if cache_valid? || (File.exist?(CONFIG_FILE) && pam_authenticate_loop) || (!File.exist?(CONFIG_FILE) && pam_authenticate_loop)
save_timestamp if !cache_valid?
pid = Process.spawn(HELPER, *cmd, in: STDIN, out: STDOUT, err: STDERR)
Process.wait(pid)
return $?.success?
else
false
end
rescue Interrupt
puts "\n[syca] Command cancelled by user."
false
end
end
# --- Main script ---
ensure_wheel_member
options = {}
parser = OptionParser.new do |o|
o.on("--enable N", Integer) { |v| options[:enable] = v }
o.on("--disable") { options[:disable] = true }
o.on("--status") { options[:status] = true }
end
parser.order! rescue nil
# Handle flags
if options[:enable]
enable_cache(options[:enable])
exit
end
if options[:disable]
disable_cache
exit
end
if options[:status]
if cache_valid?
puts "[syca] Passwordless mode active (#{seconds_left}s remaining)."
else
puts "[syca] Passwordless mode is NOT active."
end
exit
end
# No command provided
if ARGV.empty?
puts "Usage: syca <command>"
exit
end
# Run requested command
exit(run_command(ARGV) ? 0 : 1)
|