-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsupd
More file actions
executable file
·571 lines (489 loc) · 10.3 KB
/
nsupd
File metadata and controls
executable file
·571 lines (489 loc) · 10.3 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#!/bin/sh
############################################################ IDENT(1)
#
# $Title: Script to make dynamic changes to nsadmin $
# $Copyright: 2019 Devin Teske. All rights reserved. $
# $FrauBSD: nsadmin/nsupd 2019-11-13 21:43:03 -0800 freebsdfrau $
#
############################################################ CONFIGURATION
# Supported commands with defined evaluators
COMMANDS="add answer del delete send server update zone"
# Default timeout for ssh command used by send evaluator
DEFAULT_TIMEOUT=300
# Default port for ssh command used by send evaluator
DEFAULT_SSHPORT=22
# Default keyfile for ssh command used by send evaluator
DEFAULT_SSHKEY=
############################################################ GLOBALS
VERSION='$Version: 0.1.9 $'
pgm="${0##*/}" # Program basename
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
#
# Global exit status
#
SUCCESS=0
FAILURE=1
#
# OS Glue
#
: ${UNAME_s:=$( uname -s )}
#
# Command-line options
#
DEBUG=0 # -d | -D
INTERACTIVE=1 # -i
LOCAL= # -l
SSHPORT=$DEFAULT_SSHPORT # -p port
SSHKEY="$DEFAULT_SSHKEY" # -k keyfile
TIMEOUT=$DEFAULT_TIMEOUT # -t timeout
#
# Command-line arguments
#
FILE=
#
# Literals
#
NL="
" # END-QUOTE
#
# Miscellaneous
#
ARGS=
CMD=
ERROR=
INPUT=
PROMPT="> "
SERVER=
ZONE=
############################################################ FUNCTIONS
debug(){ [ $DEBUG -gt 0 ] || return; printf "%s\n" "$*" >&2; }
xdebug(){ [ $DEBUG -eq 2 ] || return; printf "%s\n" "$*" >&2; }
# die [$fmt [$args ...]]
#
# Optionally write a message to stderr before exiting with error status.
#
die()
{
local fmt="$1"
if [ $# -gt 0 ]; then
shift 1 # fmt
printf "$fmt\n" "$@" >&2
fi
exit $FAILURE
}
# usage [$fmt [$args ...]]
#
# Print usage statement and exit. Optional error message supported.
#
usage()
{
local optfmt="\t%-16s %s\n"
exec >&2
if [ $# -gt 0 ]; then
local fmt="$1"
shift 1 # fmt
printf "%s: $fmt\n" "$pgm" "$@"
fi
printf "Usage: %s [-dDhilV] %s [ignored] [--] [filename]\n" \
"[-t timeout] [-p port]" "$pgm"
printf "Options:\n"
printf "$optfmt" "-d" "Debug mode."
printf "$optfmt" "-D" "Extra debug mode."
printf "$optfmt" "-h" "Print usage statement and exit."
printf "$optfmt" "-i" "Force interactive mode."
printf "$optfmt" "-k keyfile" "SSH key. No default."
printf "$optfmt" "-l" "Local-host only mode."
printf "$optfmt" "-p port" \
"Port to connect on. Default $DEFAULT_SSHPORT."
printf "$optfmt" "-t timeout" \
"Update timeout in seconds. Default $DEFAULT_TIMEOUT."
printf "$optfmt" "-V" "Print version and exit."
printf "Ignored Options (for compatibility):\n"
printf "$optfmt" "-g" "Not implemented."
printf "$optfmt" "-L level" "Not implemented."
printf "$optfmt" "-o" "Not implemented."
printf "$optfmt" "-P" "Not implemented. Silently exits."
printf "$optfmt" "-r udpretries" "Not implemented."
printf "$optfmt" "-R randomdev" "Not implemented."
printf "$optfmt" "-T" "Not implemented. Silently exits."
printf "$optfmt" "-u udptimeout" "Not implemented."
printf "$optfmt" "-v" "Not implemented."
printf "$optfmt" "-y [hmac:]keyname:secret" ""
printf "$optfmt" "" "Not implemented."
die
}
# get_random [$var_to_set]
#
# Obtain a random number. If $var_to_set is NULL or missing, printed on stdout.
#
get_random()
{
local var_to_set="$1"
local dd="dd if=/dev/urandom of=/dev/stdout"
local rand
rand=$( awk -v dd="$dd 2> /dev/null" -v sample=256 '
BEGIN {
while (sample && dd | getline > 0)
seed += int($0 sample--)
close(dd)
srand(seed)
printf "%.0f\n", rand() * 65535
}
' )
if [ "$var_to_set" ]; then
eval $var_to_set=\"\$rand\"
else
echo "$rand"
fi
}
# setup_system "$@"
#
# Setup parsing system based on script arguments.
#
setup_system()
{
xdebug "setup_system()"
if [ $# -gt 0 ]; then
FILE="$1"
INTERACTIVE=
exec < "$FILE"
elif [ ! "$INTERACTIVE" ]; then
FILE=/dev/stdin
exec < "$FILE"
fi
}
# reset_system
#
# Reset globals in-between successful commands.
#
reset_system()
{
xdebug "reset_system()"
CMD=
ARGS=
}
# user_interaction
#
# Produce user prompt when running interactive.
#
user_interaction()
{
[ "$ERROR" ] || xdebug "user_interaction()"
ERROR=
[ "$INTERACTIVE" ] && printf "%s" "$PROMPT"
read CMD ARGS || return
: "${CMD:=send}" # Default action
}
# start_update
#
# Called before send.
#
start_update()
{
xdebug "start_update()"
}
# done_update
#
# Called after a successful send.
#
done_update()
{
xdebug "done_update()"
reset_system
}
# cleanup
#
# Called before program shutdown.
#
cleanup()
{
xdebug "cleanup()"
}
# shutdown_program
#
# Called right before exit.
#
shutdown_program()
{
xdebug "shutdown_program()"
}
# call_evaluator $cmd [$args ...]
#
# Call the command-evaluator for $cmd.
#
call_evaluator()
{
local cmd="$1"
[ $# -gt 0 -a "$cmd" ] || return $SUCCESS
shift 1 # cmd
# Translate `-' to `_' if necessary
case "$cmd" in
*-*) cmd=$( echo "$cmd" | awk 'gsub(/-/,"_")||1' ) ;;
esac
evaluate_$cmd "$@"
}
# do_next_command $cmd [$args ...]
#
# Evaluate a single command.
#
do_next_command()
{
local cmd="$1" args
local found=
xdebug "do_next_command()"
[ $# -gt 0 ] && shift 1 # cmd
# Check for invalid commands
case "$cmd" in
*[!a-zA-Z-]*|-*|*-)
printf "incorrect section name: %s\n" "$cmd" >&2
[ "$INTERACTIVE" ] || die "syntax error"
return $FAILURE
;;
esac
# Convert to lower-case if necessary
case "$cmd" in
*[A-Z]*) cmd=$( echo "$cmd" | awk '{print tolower($0)}' ) ;;
esac
# Find command handler
for c in "" $COMMANDS; do
[ "$cmd" = "$c" ] || continue
found=1
break
done
if [ "$found" ]; then
call_evaluator "$cmd" "$@" || [ "$INTERACTIVE" ] || die
elif [ "$cmd" ]; then
printf "incorrect section name: %s\n" "$cmd" >&2
[ "$INTERACTIVE" ] || die "syntax error"
return $FAILURE
fi
return $SUCCESS
}
# send_local
#
# Perform local actions.
#
send_local()
{
local n=0
local cmd
local func
while read cmd args; do
set -- $cmd $args
handler_$cmd "$@"
done <<-EOF
$INPUT
EOF
return $SUCCESS # Non-fatal
}
# send_server
#
# Perform actions on $SERVER.
#
send_server()
{
local xtra=
local output
[ "$INPUT" ] || return $SUCCESS
if [ $DEBUG -eq 1 ]; then
xtra="$xtra -d"
elif [ $DEBUG -eq 2 ]; then
xtra="$xtra -D"
fi
echo "$INPUT" | awk '
$1 == "server" { next }
{ print }
' | ssh -oStrictHostKeyChecking=no \
-oBatchMode=yes \
-oPasswordAuthentication=no \
${SSHKEY:+-i "$SSHKEY"} \
-p $SSHPORT \
"root@$SERVER" \
/usr/local/bin/nsupd $xtra -l -
return $SUCCESS
}
############################################################ CMD PROCESSORS
# Called when command defined in COMMANDS is encountered
# input_add [$cmd ...]
#
# Cache input leading up to `send'.
#
input_add()
{
[ $# -gt 0 ] || return
[ "$1" ] || return
INPUT="$INPUT${INPUT:+$NL}$*"
}
# evaluate_add [$args ...]
#
# Shortcut to `evaluate_update add [$args ...]'
#
evaluate_add()
{
evaluate_update add "$@"
}
# evaluate_answer
#
# Evaluator for the answer command.
#
evaluate_answer()
{
handle_answer
}
# evaluate_del [$args ...]
#
# Shortcut to `evaluate_update del [$args ...]'
#
evaluate_del()
{
evaluate_update delete "$@"
}
# evaluate_delete [$args ...]
#
# Shortcut to `evaluate_update delete [$args ...]'
#
evaluate_delete()
{
evaluate_update delete "$@"
}
# evaluate_send [$args ...]
#
# Evaluator for the send command.
#
evaluate_send()
{
start_update
if [ "$LOCAL" ]; then
send_local
else
send_server
fi
done_update
}
# evaluate_server $server [$port]
#
# Evaluator for the server command.
#
evaluate_server()
{
if [ "$LOCAL" ]; then
echo "cannot reset server in localhost-only mode" >&2
return
fi
SERVER="$1"
SSHPORT="${2:-$SSHPORT}"
# Remaining arguments silently ignored
}
# evaluate_update [$args ...]
#
# Evaluator for the update command.
#
evaluate_update()
{
xdebug "evaluate_update()"
case "$1" in
[Aa][Dd][Dd])
xdebug "update_addordelete()"
shift 1 # add
input_add update add "$@"
;;
[Dd][Ee][Ll]|[Dd][Ee][Ll][Ee][Tt][Ee])
xdebug "update_addordelete()"
shift 1 # del | delete
input_add update delete "$@"
;;
esac
return $SUCCESS # Non-fatal
}
# evaluate_zone $zone
#
# Evaluator for the zone command.
#
evaluate_zone()
{
local zone="$1"
if [ ! "$zone" ]; then
echo "could not read zone name" >&2
return
fi
ZONE="$1"
}
############################################################ CMD HANDLERS
# Called when handling commands stored by evaluators
# NB: Not all evaluators store commands.
# handle_update [$args ...]
#
# Handler for the update command.
#
handle_update()
{
printf "%s: handle_update: Implementation pending\n" "$pgm" >&2
return $SUCCESS # Non-fatal
}
# handle_answer
#
# Handler for the answer command.
#
handle_answer()
{
local id
local zone
[ "$STATUS" ] || return
# STATUS is NOERROR/REFUSED/NOTZONE/etc. (checked by foreman)
get_random id
printf "Answer:\n"
printf ";; ->>HEADER<<- opcode: UPDATE, status: %s, id: %6u\n" \
"${STATUS:-NOERROR}" "$id"
printf ";; flags: qr; ZONE: 1, PREREQ: 0, UPDATE: 0, ADDITIONAL: 1\n"
printf ";; ZONE SECTION:\n"
printf ";%s.\t\t\tIN\tSOA\n" "$ZONE"
printf "\n"
# NB: Foreman does not need the TSIG PSEUDOSECTION
return $SUCCESS # Non-fatal
}
############################################################ MAIN
[ -t 0 ] || INTERACTIVE= # stdin is not a TTY
#
# Process command-line options
#
optign=gL:or:R:u:vy:
optignx=PT
while getopts dDhik:lp:t:V$optign$optignx flag; do
case "$flag" in
g|L|o|r|R|u|v|y) : ignored ;;
d) [ $DEBUG -lt 1 ] && DEBUG=1 ;;
D) [ $DEBUG -lt 2 ] && DEBUG=2 ;;
p) SSHPORT="$OPTARG" ;;
P|T) exit $SUCCESS ;; # Not implemented
i) INTERACTIVE=1 ;;
k) SSHKEY="$OPTARG" ;;
l) LOCAL=1 ;;
t) TIMEOUT="$OPTARG" ;;
V) VERSION="${VERSION#*: }"
echo "${VERSION% $}"
exit $SUCCESS ;;
*) usage # NOTREACHED
esac
done
shift $(( $OPTIND - 1 ))
#
# Validate command-line options
#
[ "$LOCAL" ] || [ -e "$SSHKEY" ] || [ ! "$SSHKEY" ] ||
die "can't read key from %s: file not found\n" "$SSHKEY"
case "$TIMEOUT" in ""|*[!0-9]*) die "bad timeout '%s'" "$TIMEOUT" ;; esac
case "$SSHPORT" in ""|*[!0-9]*) die "bad port number '%s'" "$SSHPORT" ;; esac
#
# Process input
#
setup_system "$@"
reset_system
while user_interaction; do
do_next_command $CMD $ARGS || ERROR=1
done
cleanup
shutdown_program
exit $SUCCESS
################################################################################
# END
################################################################################