-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlastpass-dmenu
More file actions
executable file
·127 lines (114 loc) · 3.33 KB
/
lastpass-dmenu
File metadata and controls
executable file
·127 lines (114 loc) · 3.33 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
#!/usr/bin/env bash
# by Christopher Peterson
# https://chrispeterson.info
######
# Argpargse
######
read -r -d '' helpoutput <<EOF
Usage:
${0} [OPTIONS]... [copy|type]
Pop-up LastPass desktop quick-search. Puts the selection into the
clipboard or has xdotool type it out for you.
Options:
-n, --notes
When this option is set, this script will try to return the secure note
from the selection instead of the password. Lastpass-cli itself does
not support "attached" notes, so only the actual note field is
returned.
Default: disabled
Positional arguments:
[copy|type]
Specify what to do with the selected password/note: copy to the
clipboard, or have xdotool type it for you.
Examples
Put the password into the clipboard:
${0} copy
Have the script type out the password for you:
${0} type
Copy any secure notes in the selection:
${0} --notes copy
Security
Please make copy mode more secure by limiting X selection requests on the
clipboard. Lpass-cli can do this by setting the environment variable
\`LPASS_CLIPBOARD_COMMAND\` as per lastpass-cli documentation.
xclip can do this by limiting the number of paste requests that can come
from this selection:
export LPASS_CLIPBOARD_COMMAND="xclip -selection clipboard -in -l 1"
xsel cannot limit paste requests, but can time out the selection (ms):
export LPASS_CLIPBOARD_COMMAND="xsel -t 5000 --input --clipboard"
EOF
if [ $# -eq 0 ]; then
echo "${helpoutput}"
exit 0
fi
# Before running through `getopts`, translate out convenient long-versions
# within $@ because we're using bash built-in getopts which does not support
# long args
for opt in "$@"; do
shift
case "${opt}" in
'--notes') set -- "$@" '-n' ;;
'--help') set -- "$@" '-h' ;;
*) set -- "$@" "${opt}" ;;
esac
done
# Defaults
lpass_field='password'
# Back to the beginning now and get our opts
OPTIND=1
while getopts ':nh' opt; do
case "${opt}" in
h)
echo "${helpoutput}"
exit 0
;;
n)
lpass_field='notes'
;;
*)
echo "Invalid option ${OPTARG}" >&2
echo "${helpoutput}" >&2
exit 1
;;
esac
done
shift $(( OPTIND - 1 ))
subcommand="${1}"
if [ "${subcommand}" != 'type' ] && [ "${subcommand}" != 'copy' ]; then
echo "Incorrect subcommand. Must be 'type' or 'copy'" >&2
echo
echo "${helpoutput}" >&2
exit 1
fi
######
# Main
######
# List all entries in LastPass vault into dmenu formatted as follows
# Folder/subfolder/Name of Site [username at site] [id: id for lookup]
IFS=$'\n' entries=($(lpass ls --sync auto --long |
cut -d ' ' -f 3- |
sed 's/\[username: /[/' |
sed 's/\(.*\)\(\[.*\]\) \(\[.*\]\)/\1 \3 \2/')
)
if ! lpass status; then
echo 'Lastpass-cli returning failed status.' <&2
# shellcheck disable=SC2016
echo 'Please run `lpass login [your_username]`' >&2
exit 1
fi
# Get selection from user via dmenu
selid=$(
printf '%s\n' "${entries[@]}" |
dmenu -i -p 'LastPass: ' -l 7 |
sed 's/^.*\[id: \([0-9]\{1,\}\)\].*$/\1/'
)
if [ -z "${selid}" ]; then
echo 'Nothing selected. Exiting.' >&2
exit 0
fi
if [ "${subcommand}" == 'type' ]; then
lpass show "--${lpass_field}" "${selid}" | xargs xdotool type
else
# Result to clipboard
lpass show --clip "--${lpass_field}" "${selid}"
fi