Repository navigation
Expand file tree
/
Copy pathunlock_daemon.py
More file actions
executable file
·424 lines (356 loc) · 13.5 KB
/
Copy pathunlock_daemon.py
File metadata and controls
executable file
·424 lines (356 loc) · 13.5 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
#!/usr/bin/env python3
"""
Apple Watch Unlock Daemon for Linux
====================================
Unlocks your Linux session when your Apple Watch is detected nearby and unlocked.
Features:
- IRK verification (only YOUR Watch)
- Status flag detection (AUTO_UNLOCK_ON)
- RSSI proximity check
- Auto-lock when Watch moves away (optional)
- Systemd service integration
Usage:
sudo ./unlock_daemon.py
Or as a service:
sudo systemctl enable --now apple-watch-unlock
"""
import sys
import re
import subprocess
import logging
import os
import select
from datetime import datetime
from typing import Optional
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
# ============================================================
# CONFIGURATION
# ============================================================
# Your Apple Watch IRK (from macOS Keychain)
IRK_ENV_VAR = "WATCH_UNLOCK_LINUX_IRK"
IRK_FILE = os.environ.get("WATCH_UNLOCK_LINUX_IRK_FILE", "/etc/watch-unlock-linux/irk")
IRK_SOURCE: Optional[str] = None
IRK: Optional[bytes] = None
# RSSI thresholds (adjust based on your environment)
# Your data: close=-66, far=-90
RSSI_UNLOCK_THRESHOLD = -80 # Must be closer than this to unlock
RSSI_LOCK_THRESHOLD = -85 # Lock when Watch is farther than this
# Status flags
FLAG_WATCH_LOCKED = 0x20
FLAG_AUTO_UNLOCK_ON = 0x80
# Timing
UNLOCK_DEBOUNCE_SECONDS = 0 # React immediately to unlock signal
LOCK_GRACE_PERIOD_SECONDS = 30 # Seconds before locking when Watch moves away
PRESENCE_TIMEOUT_SECONDS = 60 # Lock if no Watch signal for this long
# Features
ENABLE_AUTO_LOCK = True # Lock when Watch moves away
# Logging
LOG_FILE = "/var/log/apple-watch-unlock.log"
LOG_LEVEL = logging.INFO
# ============================================================
# Setup Logging
# ============================================================
logging.basicConfig(
level=LOG_LEVEL,
format='%(asctime)s [%(levelname)s] %(message)s',
handlers=[
logging.StreamHandler(sys.stderr),
# logging.FileHandler(LOG_FILE), # Uncomment when running as service
]
)
log = logging.getLogger(__name__)
# ============================================================
# IRK Verification
# ============================================================
def parse_irk_hex(value: str, source: str) -> Optional[bytes]:
hex_str = value.strip().replace(" ", "").replace(":", "")
if not hex_str:
log.error("Empty IRK in %s", source)
return None
try:
irk = bytes.fromhex(hex_str)
except ValueError:
log.error("Invalid IRK hex in %s", source)
return None
if len(irk) != 16:
log.error("Invalid IRK length in %s: expected 16 bytes, got %d", source, len(irk))
return None
return irk
def load_irk() -> Optional[bytes]:
global IRK_SOURCE
env_value = os.environ.get(IRK_ENV_VAR)
if env_value:
IRK_SOURCE = f"env:{IRK_ENV_VAR}"
return parse_irk_hex(env_value, IRK_SOURCE)
try:
with open(IRK_FILE, "r", encoding="ascii") as handle:
value = handle.read().strip()
except FileNotFoundError:
log.error("IRK file not found at %s; set %s or create the file.", IRK_FILE, IRK_ENV_VAR)
return None
except Exception as e:
log.error("Failed to read IRK file %s: %s", IRK_FILE, e)
return None
IRK_SOURCE = f"file:{IRK_FILE}"
return parse_irk_hex(value, IRK_SOURCE)
def ah(irk: bytes, prand: bytes) -> bytes:
"""Bluetooth address hash function."""
r_padded = bytes(13) + prand
cipher = Cipher(algorithms.AES(irk), modes.ECB(), backend=default_backend())
enc = cipher.encryptor()
return (enc.update(r_padded) + enc.finalize())[-3:]
def is_my_watch(mac_str: str) -> bool:
"""Verify MAC address belongs to MY Watch using IRK."""
try:
if not IRK:
return False
mac = bytes.fromhex(mac_str.replace(":", ""))
if (mac[0] >> 6) != 0b01:
return False
return mac[3:6] == ah(IRK, mac[0:3])
except:
return False
# ============================================================
# Session Control
# ============================================================
def list_sessions() -> list[tuple[str, Optional[str]]]:
"""Return list of (session_id, seat) tuples."""
try:
result = subprocess.run(
["loginctl", "list-sessions", "--no-legend"],
capture_output=True, text=True, check=True
)
except Exception as e:
log.error("Failed to list sessions: %s", e)
return []
sessions = []
for line in result.stdout.strip().split('\n'):
parts = line.split()
if not parts:
continue
session_id = parts[0]
seat = parts[3] if len(parts) >= 4 else None
sessions.append((session_id, seat))
return sessions
def list_seats() -> list[str]:
"""Return list of seat names."""
try:
result = subprocess.run(
["loginctl", "list-seats", "--no-legend"],
capture_output=True, text=True, check=True
)
except Exception as e:
log.debug("Failed to list seats: %s", e)
return []
seats = []
for line in result.stdout.strip().split('\n'):
parts = line.split()
if parts:
seats.append(parts[0])
return seats
def get_active_session_for_seat(seat: str) -> Optional[str]:
"""Return active session id for a seat, if available."""
try:
result = subprocess.run(
["loginctl", "show-seat", seat, "-p", "ActiveSession"],
capture_output=True, text=True, check=True
)
except Exception as e:
log.debug("Failed to query seat %s: %s", seat, e)
return None
value = result.stdout.strip().split("=", 1)[-1]
if value and value != "n/a":
return value
return None
def get_active_session_id() -> Optional[str]:
seats = list_seats()
if not seats:
seats = ["seat0"]
for seat in seats:
session_id = get_active_session_for_seat(seat)
if session_id:
return session_id
return None
def get_session_id() -> Optional[str]:
"""Get current user's login session ID."""
session_id = get_active_session_id()
if session_id:
return session_id
sessions = list_sessions()
for session_id, seat in sessions:
if seat and seat != "-":
return session_id
if sessions:
return sessions[0][0]
return None
def is_session_locked() -> bool:
"""Check if the current session is locked."""
try:
session_id = get_session_id()
if not session_id:
return False
result = subprocess.run(
["loginctl", "show-session", session_id, "-p", "LockedHint"],
capture_output=True, text=True, check=True
)
return "LockedHint=yes" in result.stdout
except Exception as e:
log.error(f"Failed to check lock state: {e}")
return False
def unlock_session():
"""Unlock the current session."""
try:
session_id = get_session_id()
if not session_id:
log.error("No session ID found")
return False
log.info(f"[UNLOCK] Session {session_id} unlocked via Apple Watch")
subprocess.run(
["loginctl", "unlock-session", session_id],
check=True
)
return True
except Exception as e:
log.error(f"Failed to unlock session: {e}")
return False
def lock_session():
"""Lock the current session."""
try:
session_id = get_session_id()
if not session_id:
return False
log.info(f"[LOCK] Session {session_id} locked")
subprocess.run(
["loginctl", "lock-session", session_id],
check=True
)
return True
except Exception as e:
log.error(f"Failed to lock session: {e}")
return False
# ============================================================
# State Machine
# ============================================================
class WatchState:
def __init__(self):
self.last_seen: Optional[datetime] = None
self.last_rssi: Optional[int] = None
self.last_unlock_time: Optional[datetime] = None
self.leaving_since: Optional[datetime] = None
self.auto_lock_paused: bool = True # Start paused until we see the watch
self.last_flags: Optional[int] = None
state = WatchState()
def handle_watch_detection(rssi: int, status_flags: int):
global state
now = datetime.now()
if state.auto_lock_paused:
log.info("[RESUME] Watch signal detected, auto-lock monitoring active")
state.auto_lock_paused = False
state.last_seen = now
state.last_rssi = rssi
state.last_flags = status_flags
has_au_on = bool(status_flags & FLAG_AUTO_UNLOCK_ON)
has_locked = bool(status_flags & FLAG_WATCH_LOCKED)
is_on_wrist = has_au_on or has_locked
if not is_on_wrist:
state.leaving_since = None
return
if has_au_on and not has_locked and rssi >= RSSI_UNLOCK_THRESHOLD:
state.leaving_since = None
if is_session_locked():
if state.last_unlock_time is None or \
(now - state.last_unlock_time).seconds >= UNLOCK_DEBOUNCE_SECONDS:
if unlock_session():
state.last_unlock_time = now
return
if ENABLE_AUTO_LOCK and is_on_wrist and rssi < RSSI_LOCK_THRESHOLD:
if state.leaving_since is None:
state.leaving_since = now
status = "unlocked" if has_au_on else "locked"
log.info(f"[AWAY] Watch on wrist ({status}) and far (RSSI: {rssi}), grace period started")
elif (now - state.leaving_since).seconds >= LOCK_GRACE_PERIOD_SECONDS:
if not is_session_locked():
log.info(f"[LOCK] Auto-locking session (RSSI: {rssi})")
lock_session()
state.leaving_since = None
return
if rssi >= RSSI_LOCK_THRESHOLD and state.leaving_since is not None:
log.info(f"[RETURN] Watch returned (RSSI: {rssi}), cancelling lock")
state.leaving_since = None
def check_presence_timeout():
"""Pause auto-lock if Watch hasn't been seen for a while (watch probably off/stored)."""
if not ENABLE_AUTO_LOCK or state.last_seen is None:
return
if state.auto_lock_paused:
return # Already paused
elapsed = (datetime.now() - state.last_seen).seconds
if elapsed >= PRESENCE_TIMEOUT_SECONDS:
log.info(f"[PAUSE] Watch not seen for {elapsed}s, pausing auto-lock (watch probably off)")
state.auto_lock_paused = True
state.leaving_since = None # Don't lock, just pause
# ============================================================
# Startup
# ============================================================
def print_startup_info():
log.info("Apple Watch Unlock Daemon started")
if IRK_SOURCE:
log.info(f" IRK source: {IRK_SOURCE}")
else:
log.info(" IRK source: unknown")
log.info(f" Unlock threshold: {RSSI_UNLOCK_THRESHOLD} dBm")
log.info(f" Lock threshold: {RSSI_LOCK_THRESHOLD} dBm")
log.info(f" Auto-lock: {'enabled' if ENABLE_AUTO_LOCK else 'disabled'}")
# ============================================================
# Main Loop
# ============================================================
def main():
global IRK
IRK = load_irk()
if not IRK:
log.error("IRK is not configured; exiting.")
return 1
print_startup_info()
log.info("Waiting for Apple Watch...")
mac = rssi = None
try:
while True:
ready, _, _ = select.select([sys.stdin], [], [], 1.0)
if ready:
line = sys.stdin.readline()
if not line:
break
# Extract MAC
m = re.search(r'Address:\s*([0-9A-Fa-f:]{17})', line)
if m:
mac = m.group(1)
# Extract RSSI
r = re.search(r'RSSI:\s*(-?\d+)', line)
if r:
rssi = int(r.group(1))
# Parse Nearby Info
hx = re.search(r'4c 00 10 (0[56]) ([0-9a-f]{2}) ([0-9a-f]{2})', line.lower())
if hx and mac and rssi is not None:
status_flags = int(hx.group(3), 16)
if is_my_watch(mac):
has_au_on = bool(status_flags & FLAG_AUTO_UNLOCK_ON)
has_locked = bool(status_flags & FLAG_WATCH_LOCKED)
if has_au_on and not has_locked:
status = "on-wrist/unlocked"
elif has_locked:
status = "on-wrist/locked"
else:
status = "off-wrist"
log.debug(f"[DETECT] Watch {mac} | RSSI:{rssi} | {status} | flags:0x{status_flags:02X}")
handle_watch_detection(rssi, status_flags)
mac = rssi = None
# Periodic presence check
check_presence_timeout()
except KeyboardInterrupt:
log.info("Daemon stopped by user")
except Exception as e:
log.error(f"Daemon error: {e}")
raise
return 0
if __name__ == "__main__":
sys.exit(main())