This repository has no description
1.7 kB
77 lines
1#!/usr/bin/env bash
2# pinentry-rofi: A rofi-based pinentry for SSH TPM authentication
3# Reads SSH_CLIENT_PID to extract SSH command context
4set -efu
5
6# Extract SSH args from client PID
7# Groups flags with their arguments on the same line
8get_ssh_args() {
9 local pid="${SSH_CLIENT_PID:-}"
10 [[ -z "$pid" ]] && return 1
11 [[ ! -r "/proc/$pid/cmdline" ]] && return 1
12
13 # Read command line (null-separated) into array
14 local -a args=()
15 while IFS= read -r -d '' arg; do
16 args+=("$arg")
17 done < "/proc/$pid/cmdline"
18
19 # Check first arg is ssh
20 [[ "${args[0]}" == "ssh" || "${args[0]}" == */ssh ]] || return 1
21
22 # Output args, keeping flags and their values on the same line
23 local i=1
24 local line=""
25 while [[ $i -lt ${#args[@]} ]]; do
26 local arg="${args[$i]}"
27 if [[ "$arg" == -* ]]; then
28 # Print previous line if exists
29 [[ -n "$line" ]] && echo "$line"
30 line="$arg"
31 else
32 # Append to current line
33 if [[ -n "$line" ]]; then
34 line="$line $arg"
35 else
36 line="$arg"
37 fi
38 fi
39 ((i++))
40 done
41 # Print last line
42 [[ -n "$line" ]] && echo "$line"
43}
44
45# Get SSH args
46ssh_args=""
47if ssh_args=$(get_ssh_args 2>/dev/null); then
48 : # got args
49fi
50
51# Build the rofi message
52mesg_args=()
53if [[ -n "$ssh_args" ]]; then
54 mesg="<b>SSH:</b>
55$ssh_args"
56 mesg_args=(-mesg "$mesg")
57fi
58
59# Determine theme
60theme_file="/var/theme/config/rofi-theme"
61theme_args=()
62if [[ -r "$theme_file" ]]; then
63 theme=$(cat "$theme_file")
64 theme_args=(-theme "$theme")
65fi
66
67# Use rofi in password mode
68result=$(rofi -dmenu \
69 -password \
70 -p "PIN" \
71 -lines 0 \
72 -no-fixed-num-lines \
73 "${mesg_args[@]}" \
74 "${theme_args[@]}" \
75 < /dev/null 2>/dev/null) || exit 1
76
77echo "$result"