-
Notifications
You must be signed in to change notification settings - Fork 0
/
pswrd
executable file
·296 lines (247 loc) · 8.71 KB
/
pswrd
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
#!/bin/sh
set -e
## pswrd - simple password generating util.
## Copyright (C) 2019 BrainFucker <retratserif@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
_NAME_="$(basename "$0")"
USAGE="$_NAME_ - Password management/generating util. It doesn't store passwords anywhere.
USAGE
$_NAME_ -simple
$_NAME_ -simple 'string'
Simple mode. Generate password for 'string'. Ommiting 'string' will prompt for input.
$_NAME_
Running with no args will prompt for master password, username, domain and password version. Input also can be specified in args:
-password 'string'
master password
-user 'string'
User name (login).
-type
Service type: web, email, ssh, e.t.c. Default: empty.
-domain 'string'
Service name this password is for (usually domain name).
-version
password version, default: 1
Any of args above will be promted if ommited.
$_NAME_ -file 'file'
Use file as source for password.
$_NAME_ -random [bytes]
Print random string. If 'bytes' specified, will use specified bytes of random data for input. Default: 16.
$_NAME_ -alnum
Remove non alphanumeric chars from output.
$_NAME_ -nc
Don't put result to clipboard.
$_NAME_ -compat
Compatibility with old version.
$_NAME_ -help
Show this help.
Output will be placed to the clipboard if xclip util available and -nc arg not specified.
EXAMPLES
$_NAME_ -simple 'https://github.com/username'
Out: MJf8qbHsiULEMF5VDvG1
$_NAME_ -file 'picture.png'
Out: dA9gZR290cDEkt4DczVfTA
$_NAME_ -random
Out: mbNJ0vgRQvgbrdvOXpCg
$_NAME_ -random 32
Out: GTILy9aUh87lz75VcwY1KJ9rpzTkwESP1X1tGsHsgI
$_NAME_ -password 'my master password' -user username -domain 'facebook.com' -version 1
Out: L9f2jwYN/3ipAs+8NFnl
Interactive mode:
$_NAME_
Enter master password:
Enter user name (default: empty): username
Enter service name (usually domain name): facebook.com
Enter password version (default: 1):
L9f2jwYN/3ipAs+8NFnl
COPYRIGHT
$_NAME_ Copyright (C) 2019 BrainFucker
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions."
## print messages to stderr
errlog () {
echo "$@" 1>&2
}
## Args parser
## Supported arg formats:
## -arg --arg (both are identical)
## -arg value
## -arg=value
parse_args() {
local ARG _PREV_ARG _ARG _VAR _VAL _ARGN SKIP
for ARG in "$@"; do
case "$ARG" in
--) _PREV_ARG=''
SKIP=1 ;;
-*) if [ -z "$SKIP" ]; then
_ARG="$(echo -n "$ARG" | sed 's/^-\+//' | sed 's/-/_/g' | tr -d '\r' | tr '\t\v\n' ' ')"
case "$_ARG" in
*=*) _PREV_ARG=''
_VAR="$(echo -n "$_ARG" | sed 's/=.*//')"
_VAL="$(echo -n "$_ARG" | sed 's/.\+=//')"
if [ -z "$_VAL" ]; then
_VAL=0
fi
eval "_arg_$_VAR=\$_VAL" ;;
*) _PREV_ARG="_arg_$_ARG"
eval "_arg_$_ARG=1" ;;
esac
fi ;;
*) if [ -n "$_PREV_ARG" ]; then
eval "$_PREV_ARG=\$ARG"
_PREV_ARG=''
else
_ARGN=$((_ARGN+1))
eval "_arg$_ARGN=\$ARG"
fi ;;
esac
done
} # parse_args()
fn_merge_keys() {
local key result
for key in "$@"; do
if [ -n "$key" ]; then
if [ -z "$result" ]; then
result="$key"
else
result="$result\t$key"
fi
fi
done
echo "$result"
}
## check for installed requirements
UTILS="md5sum head sed xxd base64 stty"
for _util in $UTILS; do
if [ -z "$(which "$_util")" ]; then
errlog "Error: util '$_util' not found."
exit 1
fi
done
parse_args "$@"
## Check if clipboard available (running under X session).
if [ -n "$DISPLAY" ] && [ -n "$(which xclip)" ] && [ -z "$_arg_nc" ]; then
CLIPBOARD=1
fi
if [ -n "$_arg_help" ] || [ -n "$_arg_h" ]; then
## Show help message and exit
errlog "$USAGE"
exit
elif [ -n "$_arg_random" ]; then
## Generating random string
if [ "$_arg_random" = 1 ]; then
BS=15
elif [ "$_arg_random" -lt 3 ]; then
BS=3
else
BS="$_arg_random"
fi
BS=$(( (BS/3)*3 ))
if [ ! -c /dev/urandom ]; then
errlog '/dev/urandom not found or not a character device.'
exit 1
fi
RESULT="$(head -c"$BS" /dev/urandom | base64)"
elif [ -n "$_arg_file" ]; then
## Use file as source for password
if [ ! -f "$_arg_file" ]; then
errlog "File '$_arg_file' not found."
exit 1
fi
RESULT="$(md5sum "$_arg_file" | head -c30 | xxd -p -r | base64)"
else
if [ -n "$_arg_simple" ]; then
## Simple mode
if [ "$_arg_simple" != 1 ]; then
INPUT="$_arg_simple"
fi
if [ -z "$INPUT" ]; then
read -p 'Enter passphrase: ' INPUT
if [ -z "$INPUT" ]; then
errlog 'Error: input must be non empty.'
exit 1
fi
fi
if [ -n "$_arg_verbose" ]; then
errlog "INPUT: $INPUT"
fi
RESULT="$(echo -n "$INPUT" | md5sum | head -c30 | xxd -p -r | base64)"
else
## Default (advanced) mode
if [ -z "$_arg_password" ] || [ "$_arg_password" = 1 ]; then
stty -echo
read -p 'Enter master password: ' _arg_password
stty echo
errlog
fi
if [ -z "$_arg_user" ] || [ "$_arg_user" = 1 ]; then
read -p 'Enter user name (default: empty): ' _arg_user
fi
if [ -z "$_arg_type" ] || [ "$_arg_type" = 1 ]; then
read -p 'Enter service type (web, email, ssh, e.t.c. Default: empty): ' _arg_type
fi
if [ -z "$_arg_domain" ] || [ "$_arg_domain" = 1 ]; then
read -p 'Enter service name (usually domain name): ' _arg_domain
if [ -z "$_arg_domain" ]; then
errlog 'Error: input must be non empty.'
exit 1
fi
fi
if [ -z "$_arg_version" ]; then
read -p 'Enter password version (default: 1): ' _arg_version
if [ -z "$_arg_version" ]; then
_arg_version=1
fi
fi
if [ -z "$_arg_compat" ]; then
INPUT="$(fn_merge_keys "$_arg_type" "$_arg_password" "$_arg_user" "$_arg_domain" "$_arg_version")"
elif [ "$_arg_compat" = 2 ]; then
## Old versions compatibility modes.
INPUT="$(printf '%s\t' "$_arg_type" "$_arg_password" "$_arg_user" "$_arg_domain")"$_arg_version""
else
INPUT="$_arg_type$_arg_password$_arg_user$_arg_domain$_arg_version"
fi
if [ -n "$_arg_verbose" ]; then
errlog "INPUT: $INPUT"
fi
RESULT="$(echo -n "$INPUT" | md5sum | head -c 30 | xxd -p -r | base64)"
fi
fi
## Removing spaces
if [ -n "$_arg_random" ] && [ "$BS" -gt 59 ]; then
for L in $RESULT; do
_RESULT="$_RESULT$L"
done
RESULT="$_RESULT"
fi
if [ -n "$_arg_alnum" ]; then
## remove non alnum symbols
RESULT="$(echo -n "$RESULT" | sed 's/\/\|\+\|=//g')"
fi
## Replacing slashes
if [ -z "$_arg_keepslash" ]; then
RESULT="$(echo -n "$RESULT" | sed 's#/#_#g')"
fi
## put to clipboard if available
if [ -n "$CLIPBOARD" ]; then
echo -n "$RESULT" | xclip -selection clipboard > /dev/null
errlog "Copied to clipboard"
fi
## Print passvord only if -verbose mode or no clipboard
if [ -n "$_arg_verbose" ] || [ -z "$CLIPBOARD" ]; then
echo $RESULT
else
## Show only first 3 chars and replacing remaining with *
echo -n "$RESULT" | head --bytes=3
echo -n "$RESULT" | head --bytes=-3 | sed 's/./*/g'
echo
fi