aboutsummaryrefslogtreecommitdiff
path: root/bin/,passmenu
blob: b25c12940d9fb14700db2636b72b15f6f6e67d7b (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
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
#!/usr/bin/env bash

VERSION="23.1.4"

set -eu

FLAG_AUTOTYPE=0
FLAG_COPY=0
FLAG_FILEISUSER=0
FLAG_ONLYPASSWORD=0
FLAG_SQUASH=0
FLAG_TYPE=0
FLAG_HELP=0

CMD_COPY="${CMD_COPY:-"wl-copy"}"
CMD_TYPE="${CMD_TYPE:-"wtype -"}"

# We expect to find these fields in pass(1)'s output
PASS_FIELD_USERNAME="${PASS_FIELD_USERNAME:-"username"}"

WOFI_PASS_AUTOTYPE="${WOFI_PASS_AUTOTYPE:-":username :tab :password"}"
WOFI_PASS_DELAY=${WOFI_PASS_DELAY:-2}
WOFI_PASS_AUTO_ENTER="${WOFI_PASS_AUTO_ENTER:-"false"}"

function _wofi() {
    fuzzel -d "${@}"
}

function _pass() {
    pass "${@}"
}

function _trim() {
    local var="${*}"
    # remove leading whitespace characters
    var="${var#"${var%%[![:space:]]*}"}"
    # remove trailing whitespace characters
    var="${var%"${var##*[![:space:]]}"}"
    printf '%s' "${var}"
}

function generate_otp() {
    local -r passname="${1}"
    pass otp "${passname}" | tail -n1 | { IFS= read -r pass; printf %s "${pass}"; }
}

function parse_fields() {
    local field_list
    local has_username=0
    local -r passname="${1}"
    mapfile -t entries < <(_pass show "${passname}" | tail -n +2 )
    local line=""
    local fields=()

    for entry in "${entries[@]}"; do
        if [[ "${entry}" == "autotype_always" ]]; then
            fields+=("${entry}")
        else
            field="$(echo "${entry}" | cut -d: -f1 -s)"
            [ -n "${field}" ] && fields+=("${field}")
        fi
    done

    field_list+=("password")
    if [ ${FLAG_FILEISUSER} -eq 1 ]; then
        has_username=1
        field_list+=( "${PASS_FIELD_USERNAME}" )
    fi
    for line in "${fields[@]}"; do
        if [ "${line}" = "${PASS_FIELD_USERNAME}" ]; then
            if [ ${has_username} -eq 0 ]; then
                has_username=1
                field_list+=( "${line}" )
            fi
        elif [ "${line}" = "otpauth" ]; then
            field_list+=( "OTP" )
        elif [ "${line}" = "autotype_always" ]; then
            FLAG_AUTOTYPE=1
        else
            field_list+=( "${line}" )
        fi
    done

    if [ ${FLAG_TYPE} -eq 1 ] && [ ${has_username} -eq 1 ]; then
        field_list=("autotype" "${field_list[@]}")
    fi

    printf "%s\n" "${field_list[@]}"
}

function pass_field() {
    local -r passname="${1}"
    shift
    _trim "$(_pass show "${passname}" | tail -n+2 | grep "^${*}:.*$" | cut -d: -f1 -s --complement)"
}

function pass_get() {
    local -r passname="${1}"
    local -r field="${2}"
    if [ "${field}" = "password" ]; then
        _pass show "${passname}" | { IFS= read -r pass; printf %s "${pass}"; }
    elif [ "${field}" = "OTP" ]; then
        generate_otp "${passname}"
    elif [ ${FLAG_FILEISUSER} -eq 1 ] && [ "${field}" = "${PASS_FIELD_USERNAME}" ]; then
        printf %s "${passname##*/}"
    else
        pass_field "${@}"
    fi
}

function usage() {
    cat <<EOF
    Usage:
    wofi-pass [options]

    Options:
        -a, --autotype    autotype whatever entry is chosen
        -c, --copy=[cmd]  copy to clipboard. Defaults to wl-copy if no cmd is given.
        -f, --fileisuser  use the name of the password file as username
        -h, --help        show this help message
        -s, --squash      don't show field choice if password file only contains password
        -t, --type=[cmd]  type the selection instead of copying to clipboard.
                          Defaults to wtype if no cmd is given.

    wofi-pass version ${VERSION}
EOF
}

get_config_file () {
    local config_base_dir="${XDG_CONFIG_HOME:-"${HOME}/.config"}"
    readonly config_base_dir

    config_files=("${WOFI_PASS_CONFIG:-""}"
                  "${config_base_dir}/wofi-pass/config"
                  "/etc/wofi-pass.conf")

    # return the first config file with a valid path
    for config in "${config_files[@]}"; do
        if [[ -n "${config}" && -f "${config}" ]]; then
            printf "%s" "${config}"
            return
        fi
    done
}

load_config_file () {
    config_file="$(get_config_file)"
    if [[ -n "${config_file}" ]]; then
        #shellcheck source=wofi-pass.conf
        source "${config_file}"
    fi
}

function parse_arguments() {
    OPTS="$(getopt --options ac::fhst:: --longoptions autotype,copy::,fileisuser,help,squash,type:: -n 'wofi-pass' -- "${@}")"
    eval set -- "${OPTS}"
    while true; do
        case "${1}" in
            -a | --autotype) FLAG_AUTOTYPE=1; shift;;
            -c | --copy)
                FLAG_COPY=1
                [ -n "${2}" ] && CMD_COPY="${2}"
                shift 2;;
            -f | --fileisuser) FLAG_FILEISUSER=1; shift;;
            -h | --help) FLAG_HELP=1; shift;;
            -s | --squash) FLAG_SQUASH=1; shift;;
            -t | --type)
                FLAG_TYPE=1
                [ -n "${2}" ] && CMD_TYPE="${2}"
                shift 2;;
            --) shift; break;;
            *) break;;
      esac
    done
}

function exit_if_help_flag_set() {
    if [ ${FLAG_HELP} -eq 1 ]; then
        usage >&2
        exit 0
    fi
}

function verify_flags() {
    if [ ${FLAG_TYPE} -eq 1 ] && [ ${FLAG_COPY} -eq 1 ]; then
        printf "copy and type cannot be used at same time. Please pass only one.\n" >&2
        exit 1
    elif [ ${FLAG_TYPE} -eq 0 ] && [ ${FLAG_COPY} -eq 0 ]; then
        printf "neither -c/--copy or -t/--type passed. Defaulting to copying with %s.\n" "${CMD_COPY}" >&2
    fi
}

function get_passname_from_menu() {
    local -r pass_dir="${PASSWORD_STORE_DIR:-${HOME}/.password-store}"
    local password_files
    password_files="$(find "${pass_dir}" -name "*.gpg" | sed "s|^${pass_dir}\/\(.*\)\.gpg$|\1|" | sort)"
    readonly password_files

    printf "%s" "$(printf '%s\n' "${password_files}" | _wofi --dmenu)"
}

function get_field_from_menu() {
    local field
    local field_count
    local -r passname="${1}"
    mapfile -t field_list < <(parse_fields "${passname}")
    field_count=${#field_list[@]}
    readonly field_list
    readonly field_count
    if [ ${FLAG_SQUASH} -eq 1 ] && [ "${field_count}" -le 1 ]; then
        printf "password"
        FLAG_ONLYPASSWORD=1
    elif [ ${FLAG_AUTOTYPE} -ne 1 ]; then
        printf "%s" "$(printf '%s\n' "${field_list[@]}" | _wofi --dmenu)"
    fi
}

function get_output_cmd() {
    if [ ${FLAG_TYPE} -eq 1 ]; then
        printf "%s" "${CMD_TYPE}"
    else
        printf "%s" "${CMD_COPY}"
    fi
}

function press_key() {
    local -r key="${1}"
    wtype -P "${key}" -p "${key}"
}

function output_autotype() {
    local -r passname="${1}"
    local -r cmd_output="${2}"
    local password
    password="$(pass_get "${passname}" "password")"
    readonly password
    local autotype

    autotype="$(pass_get "${passname}" "autotype")"
    [ -z "${autotype}" ] && autotype="${WOFI_PASS_AUTOTYPE}"
    readonly autotype

    for word in ${autotype}; do
        case "${word}" in
            ":tab") [ ${FLAG_TYPE} -eq 1 ] && press_key "Tab" || printf "\t";;
            ":space") [ ${FLAG_TYPE} -eq 1 ] && press_key "space" || printf " ";;
            ":enter") [ ${FLAG_TYPE} -eq 1 ] && press_key "Return" || printf "\n";;
            ":delay") [ ${FLAG_TYPE} -eq 1 ] && sleep "${WOFI_PASS_DELAY}";;
            ":otp") printf "%s" "$(generate_otp "${passname}")" | ${cmd_output};;
            ":password") printf "%s" "${password}" | ${cmd_output};;
            ":username") printf "%s" "$(pass_get "${passname}" "${PASS_FIELD_USERNAME}")" | ${cmd_output};;
            ":path") printf "%s" "${passname##*/}" | ${cmd_output};;
            *) printf "%s" "$(pass_get "${passname}" "${word}")" | ${cmd_output}
        esac
    done
    [[ "${WOFI_PASS_AUTO_ENTER}" != "false" ]] && press_key "Return"
}

function output_field() {
    local cmd_output
    local -r passname="${1}"
    local -r field="${2}"
    cmd_output="$(get_output_cmd)"

    if [ ${FLAG_AUTOTYPE} -eq 1 ] || [ "${field}" = "autotype" ]; then
        # check if we are autotyping a password-only file
        if [ ${FLAG_ONLYPASSWORD} -eq 1 ]; then
            local -r password="$(pass_get "${passname}" "password")"
            printf '%s\n' "${password}" | ${cmd_output}
        else
            output_autotype "${passname}" "${cmd_output}"
        fi
    else
        pass_get "${passname}" "${field}" | ${cmd_output}
    fi
}

function main() {
    local passname
    local field

    load_config_file

    parse_arguments "${@}"

    exit_if_help_flag_set

    verify_flags

    passname="$(get_passname_from_menu)"
    [ -n "${passname}" ] || exit

    field="$(get_field_from_menu "${passname}")"

    output_field "${passname}" "${field}"
}

if [[ -z "${WOFI_PASS_TESTING:-""}" ]]; then
    main "${@}"
fi