blob: d070ae0747d9d57ea55d7a353138c6efb316738e (
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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
|
#! /bin/sh
#
# $FreeBSD$
#
# PROVIDE: dhcpd
# REQUIRE: DAEMON
# KEYWORD: FreeBSD
#
# Add the following line to /etc/rc.conf to enable dhcpd:
#
# dhcpd_enable="YES"
#
. %%RC_SUBR%%
name=dhcpd
paranoia=%%PARANOIA%% # compiled in paranoia?
jail=%%JAIL%% # compiled in jail?
# override these variables in /etc/rc.conf
dhcpd_enable=${dhcpd_enable:-"NO"}
dhcpd_flags=${dhcpd_flags:-} # -q -early_chroot # command option(s)
dhcpd_conf=${dhcpd_conf:-%%PREFIX%%/etc/${name}.conf} # configuration file
dhcpd_ifaces=${dhcpd_ifaces:-} # ethernet interface(s)
dhcpd_withumask=${dhcpd_withumask:-022} # file creation mask
dhcpd_chuser_enable=${dhcpd_chuser_enable:-"%%PARANOIA%%"} # runs w/o privileges?
dhcpd_withuser=${dhcpd_withuser:-${name}} # user name to run as
dhcpd_withgroup=${dhcpd_withgroup:-${name}} # group name to run as
dhcpd_chroot_enable=${dhcpd_chroot_enable:-"NO"} # runs chrooted?
dhcpd_rootdir=${dhcpd_rootdir:-/var/db/${name}} # directory to run in
# untested
dhcpd_jail_enable=${dhcpd_jail_enable:-"NO"} # runs imprisoned?
dhcpd_hostname=${dhcpd_hostname:-} # jail hostname
dhcpd_ipaddress=${dhcpd_ipaddress:-} # jail ip address
safe_run () # rc command [args...]
{
local _rc
_rc=$1
shift
if [ "${_rc}" -eq 0 ]; then
debug safe_run: "$@"
"$@" || _rc=1
else
warn safe_run: "$@"
fi
return ${_rc}
}
precious () # entry...
{
local _entry _rc
_rc=0
for _entry; do
# do nothing if /var/run or /var/db
echo ${_entry} | egrep -q '^//*(var//*(run|db)/*)?$' || _rc=1
done
debug precious: "$@" rc=${_rc}
return ${_rc}
}
lsmod () # user group file...
{
local _entry _user _group _rc
_user=$1 _group=$2
shift 2
_rc=0
for _entry; do
ls -ld ${_entry} 2> /dev/null |
awk -v u=${_user} -v g=${_group} '{
exit ((u && $3 != u) || (g && $4 != g))
}' || _rc=1
done
debug lsmod: "$@" rc=${_rc}
return ${_rc}
}
safe_chmog () # entry...
{
local _entry _user _group _usergroup _rc
_user=${dhcpd_withuser}
_group=${dhcpd_withgroup}
_rc=0
if [ -n "${_user}" -o -n "${_group}" ]; then
_usergroup=${_user}${_group:+:${_group}}
for _entry; do
if [ -d ${_entry} ] && mounted ${_entry}; then
continue
fi
if [ -e ${_entry} ] &&
! precious ${_entry} &&
! lsmod ${_user} ${_group} ${_entry} &&
! safe_run ${_rc} chown ${_usergroup} ${_entry}; then
warn "unable to change permissions of ${_entry}"
_rc=1
fi
done
fi
return ${_rc}
}
safe_mkdir () # dir...
{
local _dir _rc
_rc=0
for _dir; do
if [ ! -d ${_dir} ] &&
! precious ${_dir} &&
! safe_run ${_rc} mkdir -p ${_dir}; then
err 1 "unable to create directory ${_dir}"
_rc=1
fi
done
safe_run ${_rc} safe_chmog "$@" || _rc=1
return ${_rc}
}
safe_rmdir () # dir...
{
local _dir _rc
_rc=0
for _dir; do
if [ -d ${_dir} ] &&
! precious ${_dir} &&
! mounted ${_dir}; then
if safe_run ${_rc} rmdir ${_dir}; then
safe_run ${_rc} safe_rmdir ${_dir%/*} || _rc=1
else
warn "unable to remove directory ${_dir}"
rc=1
fi
fi
done
return ${_rc}
}
safe_touch () # file...
{
local _file _rc
_rc=0
for _file; do
if [ ! -e ${_file} ] &&
! safe_run ${_rc} touch ${_file}; then
err 1 "unable to create file ${_file}"
_rc=1
fi
done
safe_run ${_rc} safe_chmog "$@" || _rc=1
return ${_rc}
}
safe_remove () # file...
{
local _file _rc
_rc=0
for _file; do
if [ -f ${_file} ] &&
! safe_run ${_rc} rm -f ${_file}; then
warn "unable to remove file ${_file}"
_rc=1
fi
done
return ${_rc}
}
safe_copy () # src dst
{
local _src _dst _rc
_src=$1 _dst=$2
_rc=0
if ! safe_run ${_rc} safe_remove ${_dst} ||
! safe_run ${_rc} cp -p ${_src} ${_dst}; then
err 1 "unable to copy file ${_src} to ${_dst}"
_rc=1
fi
safe_run ${_rc} safe_chmog ${_dst} || _rc=1
return ${_rc}
}
mounted () # dir...
{
local _rc
_rc=1
mount -t devfs | awk '
BEGIN { n = ARGC; ARGC = 2 }
{ for (i = 2; i != n; i++) if ($3 == ARGV[i]) exit 1 }
' - "$@" || _rc=0
debug mounted: "$@" rc=${_rc}
return ${_rc}
}
safe_mount () # dir
{
local _dir _rc
_dir=$1
_rc=0
if ! mounted ${_dir} &&
! safe_run ${_rc} mount -t devfs devfs ${_dir}; then
err 1 "unable to mount ${_dir}"
_rc=1
fi
return ${_rc}
}
safe_umount () # dir
{
local _dir _rc
_dir=$1
_rc=0
if mounted ${_dir} &&
! safe_run ${_rc} umount ${_dir}; then
warn "unable to unmount ${_dir}"
_rc=1
fi
return ${_rc}
}
safe_useradd ()
{
local _user _group _home _shell _gecos
_user=$1 _group=$2 _gecos=${3:-"& daemon"}
_home=${4:-/nonexistent} _shell=${5:-/sbin/nologin}
if [ -n "${_group}" ]; then
if pw group show ${_group} 2>/dev/null; then
echo "You already have a group \"${_group}\"," \
"so I will use it."
elif pw groupadd ${_group} -h -; then
echo "Added group \"${_group}\"."
else
echo "Adding group \"${_group}\" failed..."
echo "Please create it, and try again."
exit 1
fi
fi
if [ -n "${_user}" ]; then
if pw user show ${_user} 2>/dev/null; then
echo "You already have a user \"${_user}\"," \
"so I will use it."
elif pw useradd ${_user} -g ${_group} -h - \
-d ${_home} -s ${_shell} -c "${_gecos}"; then
echo "Added user \"${_user}\"."
else
echo "Adding user \"${_user}\" failed..."
echo "Please create it, and try again."
exit 1
fi
fi
}
check_chuser ()
{
if checkyesno paranoia; then
if checkyesno dhcpd_chuser_enable &&
[ -z "${dhcpd_withuser}" -a -z "${dhcpd_withgroup}" ]; then
err 1 "one of dhcpd_withuser and dhcpd_withgroup" \
"must be set if dhcpd_chuser_enable is enabled"
fi
else
if checkyesno dhcpd_chuser_enable; then
warn "dhcpd_chuser_enable disabled -- not compiled in"
dhcpd_chuser_enable=NO
fi
fi
}
check_jail ()
{
if checkyesno paranoia && checkyesno jail; then
if checkyesno dhcpd_jail_enable &&
! checkyesno dhcpd_chroot_enable; then
warn "dhcpd_chroot_enable implied by dhcpd_jail_enable"
dhcpd_chroot_enable=YES
fi
if checkyesno dhcpd_jail_enable &&
[ -n "${dhcpd_hostname}" -a -z "${dhcpd_ipaddress}" ] ||
[ -z "${dhcpd_hostname}" -a -n "${dhcpd_ipaddress}" ]; then
err 1 "both dhcpd_hostname and dhcpd_ipaddress" \
"must be set if dhcpd_jail_enable is enabled"
fi
else
if checkyesno dhcpd_jail_enable; then
warn "dhcpd_jail_enable disabled -- not compiled in"
dhcpd_jail_enable=NO
fi
fi
}
check_chroot ()
{
if checkyesno paranoia; then
if checkyesno dhcpd_chroot_enable &&
[ -z "${dhcpd_rootdir}" ]; then
err 1 "dhcpd_rootdir" \
"must be set if dhcpd_chroot_enable is enabled"
fi
else
if checkyesno dhcpd_chroot_enable; then
warn "dhcpd_chroot_enable disabled -- not compiled in"
dhcpd_chroot_enable=NO
fi
fi
}
rcvar_chuser ()
{
if checkyesno paranoia && checkyesno dhcpd_chuser_enable; then
dhcpd_piddir=${__dhcpd_piddir}/${name}
dhcpd_leasesdir=${__dhcpd_leasesdir}/${name}
else
dhcpd_withuser= dhcpd_withgroup=
fi
}
rcvar_jail ()
{
if ! checkyesno paranoia || ! checkyesno jail ||
! checkyesno dhcpd_jail_enable; then
dhcpd_hostname= dhcpd_ipaddress=
fi
}
rcvar_chroot ()
{
if ! checkyesno paranoia || ! checkyesno dhcpd_chroot_enable; then
dhcpd_rootdir=
fi
}
rcvar_pidnleases ()
{
if ! checkyesno dhcpd_chuser_enable; then
dhcpd_piddir=${__dhcpd_piddir}
dhcpd_leasesdir=${__dhcpd_leasesdir}
fi
dhcpd_pidfile=${dhcpd_piddir}/${name}.pid
dhcpd_leasesfile=${dhcpd_leasesdir}/${name}.leases
dhcpd_conffile=${dhcpd_conf} # for convenience only
dhcpd_confdir=$(dirname ${dhcpd_conffile})
}
rcvar_rooted ()
{
_dhcpd_rootdir=${dhcpd_rootdir}
_dhcpd_devdir=${dhcpd_rootdir}/dev
_dhcpd_confdir=${dhcpd_rootdir}${dhcpd_confdir}
_dhcpd_piddir=${dhcpd_rootdir}${dhcpd_piddir}
_dhcpd_leasesdir=${dhcpd_rootdir}${dhcpd_leasesdir}
_dhcpd_conffile=${dhcpd_rootdir}${dhcpd_conffile}
_dhcpd_pidfile=${dhcpd_rootdir}${dhcpd_pidfile}
_dhcpd_leasesfile=${dhcpd_rootdir}${dhcpd_leasesfile}
}
setup_compat ()
{
local dhcpd_rcconf
# suck in old configuration file and variables
#
dhcpd_rcconf=${dhcpd_confdir}/rc.isc-dhcpd.conf
if [ -f ${dhcpd_rcconf} ]; then
warn "${dhcpd_rcconf} is obsolete, use /etc/rc.conf and/or" \
"/etc/rc.conf.d/${name} instead."
. ${dhcpd_rcconf}
if [ -n "${dhcpd_options}" -a -z "${rc_flags}" ]; then
warn "dhcpd_options is obsolete," \
"use dhcpd_flags instead."
rc_flags=${dhcpd_options}
fi
fi
}
setup_umask ()
{
if [ -n "${dhcpd_withumask}" ]; then
umask ${dhcpd_withumask}
fi
}
setup_chroot ()
{
if checkyesno paranoia && checkyesno dhcpd_chroot_enable; then
safe_mkdir ${_dhcpd_rootdir} ${_dhcpd_devdir} ${_dhcpd_confdir}
safe_mount ${_dhcpd_devdir}
safe_copy ${dhcpd_conffile} ${_dhcpd_conffile}
fi
}
setup_chuser ()
{
if checkyesno paranoia && {
checkyesno dhcpd_chuser_enable || checkyesno dhcpd_chroot_enable
}; then
safe_mkdir ${_dhcpd_piddir} ${_dhcpd_leasesdir}
fi
}
setup_leases ()
{
safe_touch ${_dhcpd_leasesfile}
}
setup_flags ()
{
if [ -n "${dhcpd_conf}" ]; then
rc_flags="${rc_flags} -cf ${dhcpd_conf}"
fi
if [ -n "${dhcpd_leasesfile}" ]; then
rc_flags="${rc_flags} -lf ${dhcpd_leasesfile}"
fi
if [ -n "${dhcpd_pidfile}" ]; then
rc_flags="${rc_flags} -pf ${dhcpd_pidfile}"
fi
if [ -n "${dhcpd_withuser}" ]; then
rc_flags="${rc_flags} -user ${dhcpd_withuser}"
fi
if [ -n "${dhcpd_withgroup}" ]; then
rc_flags="${rc_flags} -group ${dhcpd_withgroup}"
fi
if [ -n "${dhcpd_rootdir}" ]; then
rc_flags="${rc_flags} -chroot ${dhcpd_rootdir}"
fi
if [ -n "${dhcpd_hostname}" -a -n "${dhcpd_ipaddress}" ]; then
rc_flags="${rc_flags} -jail ${dhcpd_hostname} ${dhcpd_ipaddress}"
fi
rc_flags="${rc_flags} ${dhcpd_ifaces}"
}
cleanup_chroot ()
{
if checkyesno paranoia && checkyesno dhcpd_chroot_enable; then
safe_umount ${_dhcpd_devdir}
fi
}
dhcpd_stop ()
{
if sh $0 forcestatus; then
sh $0 forcestop
fi
}
remove_pid ()
{
if [ -e ${_dhcpd_pidfile} ]; then
warn "${_dhcpd_pidfile} still exists! -- removing anyway"
fi
safe_remove ${_dhcpd_pidfile}
}
remove_leases ()
{
if [ -s ${_dhcpd_leasesfile} ]; then
warn "${_dhcpd_leasesfile} not empty -- not removed --" \
"futher warning messages expected, don't care."
else
safe_remove ${_dhcpd_leasesfile} ${_dhcpd_leasesfile}~
fi
}
remove_chuser ()
{
if checkyesno paranoia && {
checkyesno dhcpd_chuser_enable || checkyesno dhcpd_chroot_enable
}; then
safe_rmdir ${_dhcpd_piddir} ${_dhcpd_leasesdir}
fi
}
remove_chroot ()
{
if checkyesno paranoia && checkyesno dhcpd_chroot_enable; then
safe_remove ${_dhcpd_conffile}
safe_umount ${_dhcpd_devdir}
safe_rmdir ${_dhcpd_confdir} ${_dhcpd_devdir} ${_dhcpd_rootdir}
fi
}
dhcpd_check ()
{
check_chuser
check_jail
check_chroot
}
dhcpd_rcvar ()
{
rcvar_chuser
rcvar_jail
rcvar_chroot
rcvar_pidnleases
rcvar_rooted
}
dhcpd_precmd ()
{
setup_compat
setup_umask
setup_chroot
setup_chuser
setup_leases
setup_flags
}
dhcpd_postcmd ()
{
cleanup_chroot
}
dhcpd_install ()
{
if checkyesno paranoia; then
safe_useradd "${dhcpd_withuser}" "${dhcpd_withgroup}" \
"DHCP Daemon"
fi
}
_dhcpd_uninstall () # user group root
{
local _user _group _root
_user=$1 _group=$2 _root=$3
if [ -n "${_user}" -o -n "${_group}" ]; then
dhcpd_chuser_enable=YES
dhcpd_withuser=${_user}
dhcpd_withgroup=${_group}
else
dhcpd_chuser_enable=NO
fi
if [ -n "${_root}" ]; then
dhcpd_chroot_enable=YES
dhcpd_rootdir=${_root}
else
dhcpd_chroot_enable=NO
fi
dhcpd_check
dhcpd_rcvar
dhcpd_uninstall
}
dhcpd_uninstall ()
{
if checkyesno __dhcpd_uninstall; then
dhcpd_stop
remove_pid
remove_leases
remove_chuser
remove_chroot
else
local _user _group _root
__dhcpd_uninstall=YES
_user=${dhcpd_withuser}
_group=${dhcpd_withgroup}
_root=${dhcpd_rootdir}
_dhcpd_uninstall "" "" ""
if checkyesno paranoia; then
if [ -n "${_user}" -o -n "${_group}" ]; then
_dhcpd_uninstall "${_user}" "${_group}" ""
fi
if [ -n "${_root}" ]; then
_dhcpd_uninstall "" "" "${_root}"
fi
if [ -n "${_user}" -o -n "${_group}" ] &&
[ -n "${_root}" ]; then
_dhcpd_uninstall "${_user}" "${_group}" "${_root}"
fi
fi
fi
}
rcvar=$(set_rcvar)
load_rc_config ${name}
__dhcpd_uninstall="NO" # internal use only
__dhcpd_piddir=/var/run # pid file directory
__dhcpd_leasesdir=/var/db # leases file directory
#__dhcpd_rootdir=/var/db/${name} # root directory
dhcpd_check
dhcpd_rcvar
command=%%PREFIX%%/sbin/${name}
pidfile=${_dhcpd_pidfile}
required_files=${dhcpd_conf}
start_precmd=${name}_precmd
stop_postcmd=${name}_postcmd
install_cmd=dhcpd_install
uninstall_cmd=dhcpd_uninstall
extra_commands="install uninstall"
run_rc_command "$1"
|