forked from JimDunphy/kidtimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kidtimer
executable file
·612 lines (531 loc) · 16.1 KB
/
kidtimer
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
#!/bin/bash
# Restrict kids computer access to specific hours and total time.
# By: Michael Groves - grover66_at_gmail_dot_com
#variables
#LANG="en_US.UTF-8" #unremark to change preferred language.
basedir="/usr/local/kidtimer"
configdir="/etc/kidtimer"
Cdate=`/bin/date +%Y-%m-%d`
TUI=0
HOUR=`/bin/date +%H`
DOW=`/bin/date +%u`
WEEKEND="no"
[ "$DOW" == "6" ] && WEEKEND="yes"
[ "$DOW" == "7" ] && WEEKEND="yes"
[ ! -e $configdir/kid.list ] && touch $configdir/kid.list
[ ! -e $basedir/locale/$LANG ] && LANG="en_US.UTF-8"
#arguments
[ $# -eq 0 ] && TUI=1
[ $# -eq 1 ] && COMMAND=$1
[ $# -eq 2 ] && COMMAND=$1 && KID=$2
[ $# -eq 3 ] && COMMAND=$1 && KID=$2 && Time=$3
################# Subroutines ##################
################################################
#
# %%% not used yet
#
ttl_decr () {
min=$1
if [ $C -le 0 ]; then
min=0
else
min=$((min - 1))
fi
}
#
# Decriment ttl (time to live) in mins and return current time left
#
set_ttl () {
kid=$1
min=$2
#more time
mtime=`date +%s`
mfile=$(expr $mtime / 10000)
if [ -e /tmp/$mfile ]; then
/bin/rm -f /tmp/$mfile
reset_time $kid
return
fi
# Normal mode is to decriment a minute
if [ -e $basedir/time/$kid.ttl ]; then
min=`/bin/cat $basedir/time/$kid.ttl`
if [ $min -le 0 ]; then
min=0
else
min=$((min - 1))
fi
echo $min > $basedir/time/$kid.ttl
#first time for a user, create file, and set default ttl
else
get_ttl_time $kid > $basedir/time/$kid.ttl
min=`/bin/cat $basedir/time/$kid.ttl`
fi
return 0
}
get_info () {
echo "kidtimer info"
echo "---------------------------------------------"
/bin/date
echo "---"
echo "find /usr/local/kidtimer/ -print | sort"
/usr/bin/find /usr/local/kidtimer/ -print | /usr/bin/sort
echo "---"
echo "cat $configdir/kid.list"
/bin/cat $configdir/kid.list
echo "---"
echo "passwd -S -a"
/usr/bin/passwd -S -a
echo "---"
echo "cat /usr/local/kidtimer/schedule/*"
/bin/cat /usr/local/kidtimer/schedule/*
echo "---"
echo "cat /usr/local/kidtimer/time/*"
/bin/cat /usr/local/kidtimer/time/*
echo "---"
echo "cat /etc/cron.d/kidtimer"
/bin/cat /etc/cron.d/kidtimer
echo "---"
echo "apt-cache showpkg kidtimer"
/usr/bin/apt-cache showpkg kidtimer
echo "---"
echo "cat /etc/lsb-release"
/bin/cat /etc/lsb-release
echo "---"
echo "uname -a"
/bin/uname -a
echo "---"
echo "env"
/usr/bin/env
echo
}
go_check () {
for kid in `/bin/cat $configdir/kid.list | sort -u`; do
# New day means we reset the ttl
/usr/bin/stat -c %y $basedir/time/$kid.ttl | /bin/grep -q $Cdate
if [ ! $? -eq 0 ]; then
reset_time $kid
return
fi
# This method ( /usr/bin/users ) to determine who is logged in is a kludge.
# It ignores locked screens (screensaver active) and switched user states.
# change ttl for logged in user and return remaining time
/usr/bin/users | /bin/grep -q $kid
if [ $? -eq 0 ]; then
set_ttl $kid $min
# Are they allowed to use computer during this hour
hourlyCheck $kid $allowed
# check time, lock account, and start stutdown procedure, queue up jobs
if [ $min -le 5 ] || [ $allowed -eq 0 ]; then
go_logout $kid
fi
fi
done
}
get_ttl_time () {
[ "$WEEKEND" == "no" ] && /bin/cat $basedir/schedule/$1 | /bin/grep ^MAX | /usr/bin/awk '{ print $2 }'
[ "$WEEKEND" == "yes" ] && /bin/cat $basedir/schedule/$1 | /bin/grep ^MAX | /usr/bin/awk '{ print $3 }'
}
go_clean_jobs () {
K=$1
for I in `/usr/bin/atq | /usr/bin/awk '{ print $1 }' | /usr/bin/sort`; do
/usr/bin/at -d $I
# /usr/bin/at -c $I | /bin/grep -q "bash.$K"
# [ $? -eq 0 ] && /usr/bin/at -d $I
done
[ -e /tmp/kidtimer.shutdown.$K ] && /bin/rm -rf /tmp/kidtimer.shutdown.$K
[ -e /tmp/kidtimer.send1.bash.$K ] && /bin/rm -rf /tmp/kidtimer.send1.bash.$K
[ -e /tmp/kidtimer.send2.bash.$K ] && /bin/rm -rf /tmp/kidtimer.send2.bash.$K
[ -e /tmp/kidtimer.send3.bash.$K ] && /bin/rm -rf /tmp/kidtimer.send3.bash.$K
[ -e /tmp/kidtimer.send4.bash.$K ] && /bin/rm -rf /tmp/kidtimer.send4.bash.$K
[ -e /tmp/kidtimer.send5.bash.$K ] && /bin/rm -rf /tmp/kidtimer.send5.bash.$K
[ -e /tmp/killall.bash.$K ] && /bin/rm -rf /tmp/killall.bash.$K
}
# From command line only.... check now handles this autmoatically... don't need
# to run from cron
#
# runs once per day and resets ttl back for kid
# %%% what happens if machine is suspended and it missed its chance to run?
#
go_daily () {
for I in `/bin/cat $configdir/kid.list | sort -u`; do
/usr/bin/stat -c %y $basedir/time/$I.ttl | /bin/grep -q $Cdate
#reset ttl for next day
if [ ! $? -eq 0 ]; then
reset_time $I
fi
done
# %%% needs to be rethought
#go_hourly
}
#
# Determine if allowed to use computer during this schedule
#
hourlyCheck () {
kid=$1
allowed=$2
min=1 #non zero value is all we need
if [ -e $basedir/schedule/$kid ]; then
#grab mins left
[ -e $basedir/time/$kid.ttl ] && min=`/bin/cat $basedir/time/$kid.ttl`
#lock account if out of minutes
#%%% bug should return and not exit on 0
if [ $min -le 0 ] ; then
#/usr/bin/passwd $kid -l
allowed=0
return 0
fi
# Disable/Enable depending on if allowed during time of day
[ "$WEEKEND" == "no" ] && R=`/bin/grep ^$HOUR $basedir/schedule/$kid | /usr/bin/awk '{ print $2 }'`
[ "$WEEKEND" == "yes" ] && R=`/bin/grep ^$HOUR $basedir/schedule/$kid | /usr/bin/awk '{ print $3 }'`
if [ "$R" == "y" ]; then
allowed=1 # allowed to use computer
else
allowed=0 # not allowed to use computer
fi
fi
return 0
}
go_hourly () {
if [ -s $configdir/kid.list ]; then
for I in `/bin/cat $configdir/kid.list | sort -u`; do
if [ -e $basedir/schedule/$I ]; then
[ -e $basedir/time/$I.ttl ] && C=`/bin/cat $basedir/time/$I.ttl`
# %%% if ttl has gone negative, we lock password file and exit????
#[ $C -le 0 ] && /usr/bin/passwd $I -l && exit 0
[ $C -le 0 ] && exit 0
[ "$WEEKEND" == "no" ] && R=`/bin/grep ^$HOUR $basedir/schedule/$I | /usr/bin/awk '{ print $2 }'`
[ "$WEEKEND" == "yes" ] && R=`/bin/grep ^$HOUR $basedir/schedule/$I | /usr/bin/awk '{ print $3 }'`
if [ "$R" == "y" ]; then
/usr/bin/passwd $I -u
else
/usr/bin/passwd $I -l
# should gnome-screensaver-command -l
# This method ( /usr/bin/users ) to determine who is logged in is a kludge. It ignores locked screens (screensaver active) and switched user states.
/usr/bin/users | /bin/grep -q $I
if [ $? -eq 0 ]; then
# %%% if this file exists, do no issue logout sequence
if [ ! -e /tmp/kidtimer.shutdown.$I ]; then
go_logout $I
fi
fi
fi
fi
done
fi
}
go_logout () {
kid=$1
# only start this if they are logged in
/usr/bin/users | /bin/grep -q $kid
if [ $? -ne 0 ]; then
return
fi
# schedule the shutdown if we haven't already
if [ ! -e /tmp/kidtimer.shutdown.$kid ]; then
go_send $kid 5
go_send $kid 4
go_send $kid 3
go_send $kid 2
go_send $kid 1
go_killall $kid
/usr/bin/touch /tmp/kidtimer.shutdown.$kid
fi
}
go_send () {
K=$1
T=$2
LINE=$((7-$T))
TEXT=$((6-$T))
TMPFILE="/tmp/kidtimer.send$T.bash.$K"
/bin/rm -f $TMPFILE
touch $TMPFILE
chmod +x $TMPFILE
echo "#!/bin/bash" > $TMPFILE
# This method to determine the correct display doesn't work, at least on zorin.
# It only seems to detect a user who has an active shell running
#D=`/usr/bin/who | grep $K | grep -m1 "(:" | cut -d\( -f2 | sed s/\)// | sed s/://`
# So instead I'll just send the message to all the first 10 displays, and the ones owned by the correct user will show the message, the rest won't
if [ -e $basedir/locale/$LANG ]; then
MSG="$K: `/bin/sed -n "$LINE"p /usr/local/kidtimer/locale/$LANG`"
else
MSG="$K's computer time will end in $TEXT minutes."
fi
for (( D = 0 ; D < 10 ; D = D + 1 )); do
echo "/bin/su $K -c 'DISPLAY=:$D /usr/bin/notify-send -i /usr/local/kidtimer/icons/kidtimer-$TEXT.png \
\"ALERT\" \"$MSG\"'" >> $TMPFILE
done
echo "/bin/rm -f $TMPFILE" >> $TMPFILE
echo "/bin/bash $TMPFILE" | /usr/bin/at now + $T minutes
# Note you need the file /var/spool/cron/atjobs/.SEQ owned by daemon:daemon for at to work properly!!!
}
go_killall () {
K=$1
/bin/rm -f /tmp/killall.bash.$K
touch /tmp/killall.bash.$K
chmod +x /tmp/killall.bash.$K
#/usr/bin/passwd $K -l
echo "#!/bin/bash" > /tmp/killall.bash.$K
# Not sure if it is better or worse to do a normal kill first...
# Programs like Firefox will reopen all their previous tabs if it is killed with -KILL
# But other programs would probably do better if given a chance to save state/data and clean up after themselves eg open office???
#echo "/usr/bin/pkill -TERM -u $K" >> /tmp/killall.bash.$K
#echo "/bin/sleep 5" >> /tmp/killall.bash.$K
#
# should do gnome-screensaver-command -l and passwd -l here
# if user hibernates first and resumes, logic will continue up resumption
echo "/bin/rm -rf /tmp/kidtimer.shutdown.$K" >> /tmp/killall.bash.$K
echo "/bin/rm -f /tmp/killall.bash.$K" >> /tmp/killall.bash.$K
#echo "/usr/bin/pkill -KILL -u $K" >> /tmp/killall.bash.$K
echo "/usr/sbin/pm-hibernate" >> /tmp/killall.bash.$K
echo "/bin/bash /tmp/killall.bash.$K" | /usr/bin/at now + 6 minutes
}
go_echo_error () {
echo "`/bin/sed -n '12p' /usr/local/kidtimer/locale/$LANG`"
}
go_echo_done () {
echo "`/bin/sed -n '13p' /usr/local/kidtimer/locale/$LANG`"
}
reset_time () {
KID=$1
# make sure this kid is in the list to begin with
grep -q $KID $configdir/kid.list
if [ $? -eq 0 ]; then
get_ttl_time $KID > $basedir/time/$KID.ttl
/usr/bin/passwd $KID -u
go_clean_jobs $KID
go_echo_done
fi
}
go_addtime () {
U=$KID
A=$Time
grep -q $U $configdir/kid.list
if [ $? -eq 0 ]; then
if [ "$A" == "reset" ]; then
get_ttl_time $U > $basedir/time/$U.ttl
go_echo_done
exit 0
elif [ "$A" == "" ]; then
go_echo_error
#english: Syntax: addtime <user> <minutes|reset>
echo "`/bin/sed -n '22p' /usr/local/kidtimer/locale/$LANG`"
exit 1
else
C=`/bin/cat $basedir/time/$KID.ttl`
C=$((C + Time))
echo $C > $basedir/time/$KID.ttl
get_time
fi
/usr/bin/passwd $KID -u
go_clean_jobs $KID
else
go_echo_error
#english: User not setup.
echo "`/bin/sed -n '19p' /usr/local/kidtimer/locale/$LANG`"
exit 1
fi
}
get_time () {
if [ -e $basedir/time/$KID.ttl ]; then
cat $basedir/time/$KID.ttl
else
echo
#english: User not setup.
echo "`/bin/sed -n '19p' /usr/local/kidtimer/locale/$LANG`"
echo
fi
}
go_tui () {
go_command_list
echo -n "Choose: "; read X
case "$X" in
1) go_setup_user
;;
2) go_modify_user
;;
3) go_remove_user
;;
4) go_list_users
;;
5) exit 0
;;
esac
go_tui
}
go_command_list () {
echo
echo "1) "`/bin/sed -n '7p' /usr/local/kidtimer/locale/$LANG`
echo "2) "`/bin/sed -n '8p' /usr/local/kidtimer/locale/$LANG`
echo "3) "`/bin/sed -n '9p' /usr/local/kidtimer/locale/$LANG`
echo "4) "`/bin/sed -n '10p' /usr/local/kidtimer/locale/$LANG`
echo "5) "`/bin/sed -n '11p' /usr/local/kidtimer/locale/$LANG`
echo
}
go_list_users () {
echo
#english: Users configured for kidtimer:
echo "`/bin/sed -n '14p' /usr/local/kidtimer/locale/$LANG`"
if [ -s $configdir/kid.list ]; then
/bin/cat $configdir/kid.list
else
#english: No configured users.
echo "`/bin/sed -n '15p' /usr/local/kidtimer/locale/$LANG`"
echo
fi
}
go_setup_user () {
echo
#english: Username:
echo -n "`/bin/sed -n '16p' /usr/local/kidtimer/locale/$LANG` "; read U
/usr/bin/id $U > /dev/null 2>&1
if [ $? -eq 0 ]; then
/bin/cp $basedir/schedule/blank $basedir/schedule/$U
get_ttl_time $U > $basedir/time/$U.ttl
echo $U >> $configdir/kid.list
go_echo_done
echo
#english: Modify limits now ?(y/n):
echo -n "`/bin/sed -n '17p' /usr/local/kidtimer/locale/$LANG`"; read M
if [ "$M" == "y" ]; then
if [ -e /usr/bin/nano ]; then
/usr/bin/nano $basedir/schedule/$U
get_ttl_time $U > $basedir/time/$U.ttl
go_echo_done
else
/usr/bin/vi $basedir/schedule/$U
get_ttl_time $U > $basedir/time/$U.ttl
go_echo_done
fi
fi
else
go_echo_error
#english: User does not exist. Please create user using the useradd command first.
echo "`/bin/sed -n '18p' /usr/local/kidtimer/locale/$LANG`"
fi
}
go_modify_user () {
echo
#english: Username:
echo -n "`/bin/sed -n '16p' /usr/local/kidtimer/locale/$LANG` "; read U
grep -q ^$U $configdir/kid.list
if [ $? -eq 0 ]; then
if [ -e /usr/bin/nano ]; then
/usr/bin/nano $basedir/schedule/$U
go_echo_done
else
/usr/bin/vi $basedir/schedule/$U
go_echo_done
fi
else
go_echo_error
#english: User not setup.
echo "`/bin/sed -n '19p' /usr/local/kidtimer/locale/$LANG`"
echo
fi
}
go_remove_user () {
echo
#english: Username:
echo -n "`/bin/sed -n '16p' /usr/local/kidtimer/locale/$LANG` "; read U
grep -q ^$U $configdir/kid.list
if [ $? -eq 0 ]; then
/bin/grep -v ^$U $configdir/kid.list > /tmp/kidtimer.tmp
/bin/cat /tmp/kidtimer.tmp > $configdir/kid.list
go_echo_done
else
go_echo_error
#english: User not setup.
echo "`/bin/sed -n '19p' /usr/local/kidtimer/locale/$LANG`"
echo
fi
}
go_help () {
echo
echo "Commands:"
echo "--------------------------------------------------------------------------------"
echo "addtime <user> <minutes> ... Increases allowed time for the day."
echo "gettime <user> ... Prints remaining time for the day."
echo "reset <user> ... Reset time for the day."
echo "logout <user> ... Starts logout sequence for user."
echo "hourly ... Enables/disables user access based on the schedule."
echo "daily ... Resets time for the new day."
echo "update ... Updates kidtimer to the version."
echo "info ... Gather local configurations to troubleshoot issues."
echo "help ... This list."
echo "--------------------------------------------------------------------------------"
}
check_dependencies () {
#at
P1="";P2="";P3="";pstatus="0"
/usr/bin/dpkg -s at >/dev/null 2>/dev/null
if [ ! $? -eq 0 ]; then
P1="at"
pstatus=1
fi
#libnotify-bin
/usr/bin/dpkg -s libnotify-bin >/dev/null 2>/dev/null
if [ ! $? -eq 0 ]; then
P2="libnotify-bin"
pstatus=1
fi
#bsdutils
/usr/bin/dpkg -s bsdutils >/dev/null 2>/dev/null
if [ ! $? -eq 0 ]; then
P3="bsdutils"
pstatus=1
fi
if [ "$pstatus" == "1" ]; then
echo
#english: Error. Missing package dependencies.
echo "`/bin/sed -n '20p' /usr/local/kidtimer/locale/$LANG`"
#english: Please install using the following line;
echo "`/bin/sed -n '21p' /usr/local/kidtimer/locale/$LANG`"
echo "sudo apt-get install "$P1" "$P2" "$P3
exit 1
fi
}
get_update () {
DEB_NAME='kidtimer.deb'
URL="https://github.com/SirYaro/kidtimer/raw/master/DEBS/${DEB_NAME}"
#'https://github.com/grover66/kidtimer/raw/master/DEBS/kidtimer_latest.deb'
if [ -e /usr/bin/wget ]; then
/usr/bin/wget "$URL" -qO /tmp/${DEB_NAME}
/usr/bin/dpkg -i /tmp/${DEB_NAME}
/bin/rm -f /tmp/${DEB_NAME}
else
echo
echo "Error. Requires wget"
echo "To install wget; sudo apt-get install wget"
fi
}
###################### Code ####################
################################################
if [ $TUI -eq 1 ]; then
check_dependencies
go_tui
fi
case "$COMMAND" in
addtime) go_addtime
;;
reset) reset_time $KID
;;
gettime) get_time
;;
logout) go_logout $KID
;;
hourly) go_hourly
;;
daily) go_daily
;;
update) get_update
;;
check) go_check
;;
info) get_info
;;
-h) go_help
;;
help) go_help
;;
esac
exit 0