-
Notifications
You must be signed in to change notification settings - Fork 1
/
parpass
executable file
·304 lines (244 loc) · 7.52 KB
/
parpass
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
#!/usr/bin/env bash
#
# parpass
#
# Version: 1.5.3
# History: 2020-06-14, 2020-07-11, 2023-11-15, 2023-11-19, 2023-11-20
#
# Project Description:
#
# Reads the password file /etc/passwd, searches for user names beginning with $1,
# and parses the record to print it in a human-readable form on the screen.
#
# Author: Bernd Storck
# Contact: https://www.facebook.com/BStLinux/
#
# Copyright: 2023, Bernd Storck
# License: GNU General Public License 3.0
# Daten zur Selbstbeschreibung des Scripts: ...............
readonly ORIGINAL_SCRIPT_NAME="parpass"
readonly CURRENT_SCRIPT_NAME="${0##*/}" ## Name of the script file
readonly TITLE="$ORIGINAL_SCRIPT_NAME (parsePasswd)"
readonly VERSION="1.5.3"
readonly VERSIONS_TIME_STAMP="2023-11-22 21:00:27" # date +"%Y-%m-%d %T"
# Farbnamensvariablen: ....................................
readonly WHITE='\e[0;37m'
readonly WHITE_BOLD='\e[1;37m'
readonly YELLOW='\e[0;33m'
readonly YELLOW_BOLD='\e[1;33m'
readonly YELLOW_BOLD_INTENS='\e[1;93m'
readonly GREEN_BOLD='\e[1;32m'
readonly COLOROFF='\e[0m'
OLDIFS="$IFS"
IFS=":"
# Defines the name of the language file according to system language:
readonly language_file_name="parpass.lang.${LANG:0:2}"
# Searching for an external language file:
for config_path in "$HOME/parpass/" "$HOME/" "/etc/parpass/" "/etc/" "./"; do
language_file="${config_path}$language_file_name"
if [ -f "$language_file" ]; then
source "${language_file}"
file_found=true
break
fi
done
# If no language file is found the following
# will define English text as default output:
if ! "$file_found" ; then
UILANG="English"
label_ID="IDs (uid/gid)"
label_homeDir="Directory"
label_loginShell="Login Shell"
label_realName="Real Name"
label_room="Room"
label_workPhone="Work Fon"
label_phone="Home Phone"
label_comment="Comment"
fi
# Functions: -----------------------------------------
usage () {
# Displays program purpose and usage help.
echo -e "${YELLOW_BOLD}$TITLE $VERSION ${COLOROFF}\n"
if [ "$UILANG" = "Deutsch" ]; then
echo -e "Das Programm liest die Passwortdatei \"/etc/passwd\" und zeigt ihren Inhalt gut lesbar an.
AUFRUFPARAMETER:
--help|-h|--hilf # Diese Hilfe anzeigen
--version|-V # Die Version dieses Installationsskripts anzeigen.
--full_version # Eine ausführliche Versionsinformation anzeigen.
BEISPIELAUFRUFE:
${GREEN_BOLD}Aufruf Beschreibung${COLOROFF}
$CURRENT_SCRIPT_NAME root # Zeigt die Daten aus der passwd zum Account root an.
$CURRENT_SCRIPT_NAME bs # Zeigt die Daten zu allen Accounts an, die mit \"bs\" beginnen.
$CURRENT_SCRIPT_NAME # Zeigt die Daten aus der Datei passwd zu allen Accounts an.
"
else # English help:
echo -e "Reads the password file \"/etc/passwd\" and displays its content human-readable on the screen.
PARAMETERS:
--help|-h|--hilf # Display this help.
--version|-V # Display the script version.
--full_version # Display detailed version information.
EXAMPLES:
${GREEN_BOLD}Call Description${COLOROFF}
$CURRENT_SCRIPT_NAME root # Shows information from passwd according to the account. \"root\".
$CURRENT_SCRIPT_NAME bs # Shows information about all accounts, which do begin with \"bs\".
$CURRENT_SCRIPT_NAME # Shows information from file \"passwd\" for all accounts.\n"
fi
}
print_program_identification () {
# Gibt die vollstaendige Programmselbstbezeichnung aus.
printf "\n${WHITE}%s (%s, %s, %s)${COLOROFF}\n\n" \
"$PROG_NAME" "$ORIGINAL_SCRIPT_NAME" "$VERSION" "$VERSIONS_TIME_STAMP"
}
normalize_option () {
# Wenn eine Option mehr als einen Buchstaben hat, korrigiert diese Funktion
# beispielsweise '-examples' zu '--examples'.
#
# Zudem wandelt sie mehrbuchstabige Optionen in Kleinbuchstaben um.
#
local input="$1"
local output="$input"
if [[ ! "$1" =~ ^[a-zA-Z0-9] ]]; then
input="$input"
# Prueft mit bash-internen Kommandos, ob die Option mit nur genau einem '-'
# beginnt und darauf mindestens zwei Buchtaben folgen und ersetzt
# gegebenenfalls das '-' am Anfang durch '--':
if [[ $input =~ ^-([a-zA-Z]{2,}) ]]; then
output="--${BASH_REMATCH[1]}"
output="${output,,}" # Converts to lower case.
fi
fi
echo "$output"
}
internal_filter () {
# Replaces functionallity of the programs 'look', 'grep' etc.,
# if none of those is available.
local lines
while read -r line; do
if [[ "$line" =~ ^$1 ]]; then
lines+="$line\n"
fi
done < /etc/passwd
printf "%b" "$lines"
}
getLines () {
# Filters those lines which are beginning with "$1".
#
local line_start="$1"
local data_file
local prog
local program_found
local answer
[ ! "$2" ] && data_file="/etc/passwd" || data_file="$2"
for prog in look rg grep agrep tre-agrep; do
if command -v "$prog" > /dev/null 2>&1; then
case "$prog" in
look)
answer="$(look "$line_start" "$data_file")"
;;
rg)
answer="$(rg -N "^$line_start" "$data_file")"
;;
grep|agrep|tre-agrep)
answer="$($prog "^$line_start" "$data_file")"
;;
esac
program_found=true
break
fi
done
if ! "$program_found"; then
answer="$(internal_filter "$1")"
fi
echo "$answer"
}
plotHeadLine () {
local len
local line=
len="$(( 62 - ${#1} ))"
echo -en "\n${YELLOW_BOLD_INTENS}${user} ${YELLOW}"
for ((i=0; i<len; i++ )); do
line+="-"
done
echo -e "$line${COLOROFF}";
}
printField() {
# Pairs of field names and their content are written to a variable,
# allowing the result to be reformatted at the end once all fields
# of a data set are collected in the variable.
dataSet+="\t${WHITE_BOLD}${2}:${COLOROFF}# ${1}\n"
}
printNotEmpty() {
if [ "$1" != "" ]; then
printField "$1" "$2"
fi
}
internal_column () {
# Replaces the external program 'column', if it is not available.
local line
local maxLen=0
while read -r line; do
field1="${line%%#*}" # Like: field1="$(cut -d'#' -f1 <<< "$line")"
if [ "${#field1}" -gt "$maxLen" ]; then
maxLen="${#field1}"
fi
done <<< "$1"
while read -r line; do
IFS="#" read -r field1 field2 <<< "${line}"
printf "%-${maxLen}b %b\n" "$field1" "$field2"
done <<< "$1"
}
external_output () {
echo -e "$1" | column -s# -t
}
internal_output () {
internal_column "$(echo -e "$1")"
}
# Main: ----------------------------------------------
option=$(normalize_option "$1")
case "$option" in
--help|-h|--hilf|-help)
usage
exit 0
;;
--version|-V)
printf "%s (Version %s)\n" "$ORIGINAL_SCRIPT_NAME" "$VERSION"
exit 0
;;
--fullversion|--id|--identifi[ck]ation)
print_program_identification
exit 0
;;
-\#) # Versionsnummer anzeigen
echo "$VERSION"
exit 0
;;
*)
unset option
;;
esac
if command -v column > /dev/null 2>&1; then
printData="external_output"
else
printData="internal_output"
fi
lines="$(getLines "$1")"
if [ -n "$lines" ]; then
while read -r user trash uid gid data homeDir loginShell; do
plotHeadLine "$user"
printField "$uid / $gid" "$label_ID"
printField "$homeDir" "$label_homeDir"
printField "$loginShell" "$label_loginShell"
IFS=','
read -r realName room workPhone phone comment <<< "$data"
printNotEmpty "$realName" "$label_realName"
printNotEmpty "$room" "$label_room"
printNotEmpty "$workPhone" "$label_workPhone"
printNotEmpty "$phone" "$label_phone"
printNotEmpty "$comment" "$label_comment"
"$printData" "$dataSet" # Prints data set to screen.
unset dataSet
IFS=':'
done <<< "$lines"
fi
IFS="$OLDIFS"
exit 0