-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsh-lib
More file actions
442 lines (371 loc) · 9.79 KB
/
sh-lib
File metadata and controls
442 lines (371 loc) · 9.79 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/bin/bash
# Filename: sh-lib
# Purpose: Shellscript library
# Authors: grml-team (grml.org), (c) Michael Gebetsroither <gebi@grml.org>
# Bug-Reports: see http://grml.org/bugs/
# License: This file is licensed under the GPL v2.
################################################################################
# VARIABLES {{{
VERBOSE__=0
# FIXME maybe PROG_PATH__ for better error reporting?
PROG_NAME__="" # initialised within init section
# >= level and the function will print the message
EPRINT__=1 # eprint (error print)
DPRINT__=3 # dprint (debug print)
LANG__="$LANG"
LC_ALL__="$LC_ALL"
LANGUAGE__="$LANGUAGE"
# }}}
# CONFIG FUNCTIONS {{{
setProgName() { PROG_NAME__="$1"; }
saveLang() { LANG__="$LANG"; LC_ALL__="$LC_ALL"; LANGUAGE__="$LANGUAGE"; }
restoreLang() { LANG="$LANG__"; LC_ALL="$LC_ALL__"; LANGUAGE="$LANGUAGE__"; }
setCLang() { saveLang; LANG="C"; LC_ALL="C"; LANGUAGE="C"; }
# }}}
# DEBUG FRAMEWORK {{{
setVerbose() { VERBOSE__=${1:-1}; }
# }}}
# ERROR REPORTING FUNCTIONS {{{
# default print backend (there may be other functions)
vprint()
{
local level_="$1"
local type_="$2"
local message_="$3"
if [ "$VERBOSE__" -ge "$level_" -a -n "$message_" ]; then
echo -n "$type_" >&2
echo "$message_" >&2
fi
}
# print error output
eprint()
{
# FIXME vprint should be a var, because we want to call user-defined functions
# global var (there should be a syslog, and vprint + syslog function)
vprint $EPRINT__ "Error - " "$1"
}
# print debug output (function intern errors)
dprint()
{
vprint $DPRINT__ "Debug - " "$1"
}
# for program notice messages
notice()
{
vprint $EPRINT__ "Notice - " "$1"
}
die()
{
local error_message_="$1" # print this error message
local exit_code_="$2" # command exited with this exit code
echo -n "PANIC: $error_message_" >&2
if [ -n "$2" ]; then
echo "; ret($exit_code_)" >&2
else
echo >&2
fi
kill $$
}
warn()
{
local error_message_="$1" # print this error message
local exit_code_="$2" # command exits with this exit code
echo -n "WARN: $error_message_" >&2
if [ -n "$exit_code_" ]; then
echo "; ret($exit_code_)" >&2
else
echo >&2
fi
}
syslog()
{
local message_="$1" # error message
local exit_code_="$2"
if [ -n "$exit_code_" ]; then
logger -p user.alert -t "$PROG_NAME__" -i "$message_ ret($exit_code_)" >&2
else
logger -p user.alert -t "$PROG_NAME__" -i "$message_" >&2
fi
}
# }}}
# CORE FUNCTIONS {{{
##
# ATTENTION... THIS FUNCTION IS A BIG SECURITY HOLE
# this function will be changed in future release
##
# I don't want to write exit status control stuff every time
execute()
{
local to_exec_="$1" # command to execute
local error_function_=${2:-"eprint"} # function to call on error
local message_="$3" # user supplied error message
local ret_=''
# NOT A GOOD IDEA
eval "$to_exec_"
ret_=$?
if [ $ret_ -eq 127 ]; then
syslog "problems executing ( $to_exec_ )" $ret_
fi
if [ $ret_ -ne 0 ]; then
if [ -z "$message_" ]; then
$error_function_ "problems executing ( $to_exec_ )" "$ret_"
else
$error_function_ "$message_" "$ret_"
fi
fi
dprint "exec-$error_function_: ( $to_exec_ ) ret($ret_)"
return $ret_
}
###
#
# TEST FUNCTIONS
#
###
# if the file DOES exist, everything is fine
isExistent()
{
local file_to_test_="$1" # file to test
local error_function_=${2:-"eprint"} # function to call on error
local message_="$3" # user supplied error message
if [ ! -e "$file_to_test_" ]; then
if [ -z "$message_" ]; then
$error_function_ "file does not exist \"$file_to_test_\"" 66
else
$error_function_ "$message_"
fi
return 1
fi
dprint "isExistent(): file \"$1\" does exist => ready to go"
return 0
}
checkUser()
{
local to_check_="$1" # username to check against running process
local error_function_=${2:-"eprint"} # function to call on error
local message_="$3" # user supplied error message
local user_=''
user_=`id -un`
if [ $user_ != "$to_check_" ]; then
if [ -z "$message_" ]; then
$error_function_ "username \"$user_\" is not \"$to_check_\"" 77 $exit_function_
else
$error_function_ "$message_"
fi
return 1
else
dprint "checkUser(): accepted, username matches \"$to_check_\""
return 0
fi
}
checkId()
{
local to_check_="$1" # user-id to check against running process
local error_function_=${2:-"eprint"} # function to call on error
local message_="$3" # user supplied error message
local user_id_=''
user_id_=$(id -u)
if [ "$user_id_" != "$to_check_" ]; then
if [ -z "$message_" ]; then
$error_function_ "UID \"$user_id_\" is not \"$to_check_\"" 77
else
$error_function_ "$message_"
fi
return 1
else
dprint "checkId(): accepted, UID matches \"$to_check_\""
return 0
fi
}
checkRoot()
{
checkId 0 "$1" "$2"
}
# secure input from console
secureInput()
{
local to_secure_="$1"
local secured_=''
secured_=`echo -n "$to_secure_" |tr -c '[:alnum:]/.\-,\(\)' '_'`
dprint "secureInput(): \"$to_secure_\" => \"$secured_\""
echo "$secured_"
}
# }}}
# NETWORK {{{
# validates an IP FIXME
netValidIp()
{
local ip_="$1" # ip address to validate
local error_function_=${2:-"eprint"} # function to call on error
local message_="$3" # user supplied error message
local ret_=''
echo "$ip_" | grep -E -q -e '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}' \
1>/dev/null 2>&1
ret_=$?
if [ $ret_ -ne 0 ]; then
if [ -z "$message_" ]; then
"$error_function_" "ip-address \"$ip_\" is NOT valid" $ret_
else
"$error_function_" "$message_" $ret_
fi
return 1
fi
dprint "ip-address \"$ip_\" is valid" $ret_
return $ret_
}
netGetIfaces()
{
local error_function_=${1:-"eprint"} # function to call on error
local message_="$2" # user supplied error message
local if_=''
local ret_=''
#ip a|grep 'inet ' |awk '$NF !~ /lo/{print $NF}'
if_="`ip a|grep 'inet ' |awk '{print $NF}'`"
ret_=$?
if [ -z "$if_" ]; then
if [ -z "$message_" ]; then
"$error_function_" "no interfaces found" $ret_
else
"$error_function_" "$message_" $ret_
fi
return 1
fi
dprint "interfaces found" $ret_
echo "$if_"
}
# FIXME
netGetDefaultGateway()
{
local error_function_=${1:-"eprint"} # function to call on error
local message_="$2" # user supplied error message
local ip_=''
local ret_=''
setCLang
ip_=`route -n | awk '/^0\.0\.0\.0/{print $2; exit}'`
ret_=$?
restoreLang
if [ -z "$ip_" ]; then
if [ -z "$message_" ]; then
"$error_function_" "no default gateway found" $ret_
else
"$error_function_" "$message_" $ret_
fi
return 1
fi
dprint "default gateway is \"$ip_\"" $ret_
echo "$ip_"
return 0
}
# FIXME
netGetNetmask()
{
local iface_="$1"
local error_function_=${2:-"eprint"} # function to call on error
local message_="$3" # user supplied error message
local nm_=''
local ret_=''
setCLang
if ifconfig "$iface_" | grep -qi 'Mask:' ; then # old ifconfig output:
nm_=$(ifconfig "$iface_" | awk '/[Mm]ask/{FS="[: ]*"; $0=$0; print $8; exit}')
else # new ifconfig output (net-tools >1.60-27):
nm_=$(ifconfig "$iface_" | awk '/netmask/{print $4}')
fi
ret_=$?
restoreLang
if [ -z "$nm_" ]; then
if [ -z "$message_" ]; then
"$error_function_" "could not find a netmask for \"$iface_\"" $ret_
else
"$error_function_" "$message_" $ret_
fi
return 1
fi
dprint "netmask on \"$iface_\" is \"$nm_\"" $ret_
echo "$nm_"
return 0
}
# FIXME
netGetIp()
{
local iface_="$1"
local error_function_=${2:-"eprint"} # function to call on error
local message_="$3" # user supplied error message
local ip_=""
local ret_=""
setCLang
ip_=$(ip addr show dev "$iface_" | awk '/inet /{split($2,a,"/"); print a[1]}')
ret_=$?
restoreLang
if [ -z "$ip_" ]; then
if [ -z "$message_" ]; then
"$error_function_" "no ip for \"$iface_\" found" $ret_
else
"$error_function_" "$message_" $ret_
fi
return 1
fi
dprint "address for \"$iface_\" is \"$ip_\"" $ret_
echo "$ip_"
return 0
}
netGetNameservers()
{
local error_function_=${1:-"eprint"} # function to call on error
local message_="$2" # user supplied error message
local file_="/etc/resolv.conf"
local ns_=""
if [ ! -e $file_ ]; then
warn "file \"$file_\" does not exist, could not get nameservers"
return 1
fi
setCLang
ns_=`awk '/^nameserver/{printf "%s ",$2}' $file_ |xargs echo`
restoreLang
if [ -z "$ns_" ]; then
if [ -z "$message_" ]; then
"$error_function_" "no nameservers found" $ret_
else
"$error_function_" "$message_" $ret_
fi
return 1
fi
dprint "nameservers: \"$ns_\"" $ret_
echo "$ns_"
return 0
}
# }}}
# INIT {{{
_initProgName()
{
local name_="$1" # program name
local tmp_name_=`basename "$name_"` || \
logger -p user.alert -i "Init-initProgName: problems executing ( basename \"$name_\" ) ret($?)" >/dev/null
secureInput "$tmp_name_"
}
PROG_NAME__=`_initProgName "$0"`
_checkExecutables()
{
local tmp_=""
for i in tr dirname basename id logger kill cat grep route awk ifconfig; do
which $i >/dev/null 2>&1 || tmp_="${tmp_}$i "
done
if [ -n "$tmp_" ]; then
eprint "Init-checkExecutables: following executables not found or not executable:\n$tmp_"
#syslog "Init-checkExecutables: following executables not found or not executable: $tmp_"
fi
}
_checkExecutables
_setDebugLevel()
{
# accept only integer as arguments
if echo "$DEBUG" | grep -E -q '^[0-9]+$' ; then
local debug_="${DEBUG:-0}"
else
local debug_="0"
fi
VERBOSE__="$debug_"
}
_setDebugLevel
# }}}
# END OF FILE
################################################################################
# vim:foldmethod=marker expandtab shiftwidth=2 tabstop=2