Skip to content
Browse files

Changing the way triggers are handled so only one saved state is needed

  • Loading branch information...
1 parent a5886fc commit 8c48b833adf6eff85b02906075976d8411f7e702 @geirha committed Sep 19, 2012
Showing with 45 additions and 59 deletions.
  1. +1 −6 Makefile
  2. +12 −30 evalbot.pl
  3. +11 −4 evalcmd
  4. +10 −15 generate-initramfs
  5. +2 −1 runqemu
  6. +2 −3 savestate
  7. +7 −0 triggers
View
7 Makefile
@@ -15,12 +15,7 @@ build/bzImage:
hda: initramfs.cpio.gz build/bzImage
qemu-img create -f qcow2 hda.tmp 1M
- ./savestate hda.tmp _sh
- ./savestate hda.tmp _4
- ./savestate hda.tmp _3
- ./savestate hda.tmp _2
- ./savestate hda.tmp _1
- ./savestate hda.tmp _k93
+ ./savestate hda.tmp save
mv hda.tmp hda
clean:
View
42 evalbot.pl
@@ -19,7 +19,7 @@
if(!$password) { die "password unspecified"; }
if(!$server) { die "irc server unspecified"; }
if(!$owner) { die "owner unspecified"; }
-if($#channels == 0) { print "No channels specified. Continuing anyways\n"; }
+if(scalar @channels == 0) { print "No channels specified. Continuing anyways\n"; }
$SIG{'INT'} = 'my_sigint_catcher';
$SIG{'TERM'} = 'my_sigint_catcher';
@@ -79,29 +79,17 @@ sub message
my( $conn, $event ) = @_;
my( $msg ) = $event->args;
- if( $msg =~/^([1-4k]|sh)?# botsnack$/ ) {
+ if( $msg =~/^# botsnack$/ ) {
$conn->privmsg($event->to, "Core dumped.");
- } elsif( $msg =~/^([1-4k]|sh)?# botsmack$/ ) {
+ } elsif( $msg =~/^# botsmack$/ ) {
$conn->privmsg($event->to, "Segmentation fault");
- } elsif( $msg =~/^([1-4]|sh)# (.*)/ ) {
- open(FOO, "-|", "./evalcmd", "_$1", "$2");
+ } elsif( $msg =~/^([^#]*)# (.*)/ ) {
+ open(FOO, "-|", "./evalcmd", "$1", "$2");
while(<FOO>) {
$conn->privmsg($event->to, $event->nick . ": $_");
}
close(FOO);
- } elsif( $msg =~/^k# (.*)/ ) {
- open(FOO, "-|", "./evalcmd", "_k93", "$1");
- while(<FOO>) {
- $conn->privmsg($event->to, $event->nick . ": $_");
- }
- close(FOO);
- } elsif( $msg =~ /^# (.*)/ ) {
- open(FOO, "-|", "./evalcmd", "_4", "$1");
- while(<FOO>) {
- $conn->privmsg($event->to, $event->nick . ": $_");
- }
- close(FOO);
- }
+ }
}
sub private
@@ -113,24 +101,18 @@ sub private
$conn->privmsg( "$owner", "< " . $event->nick . "> $msg" );
- if($msg =~ /^!help.*/) {
- $conn->privmsg( $event->nick, "Usage: 4# cmd" );
+ if($msg =~ /^!help$/) {
+ $conn->privmsg( $event->nick, "Usage: # cmd" );
} elsif($msg =~ /^!raw $password (.*)/) {
$conn->sl($1);
- } elsif( $msg =~/^([1-4]|sh)# (.*)/ ) {
- open(FOO, "-|", "./evalcmd", "_$1", "$2");
- while(<FOO>) {
- $conn->privmsg($event->nick, "$_");
- }
- close(FOO);
- } elsif( $msg =~/^k# (.*)/ ) {
- open(FOO, "-|", "./evalcmd", "_k93", "$1");
+ } elsif( $msg =~/^([^#]*)# (.*)/ ) {
+ open(FOO, "-|", "./evalcmd", "$1", "$2");
while(<FOO>) {
$conn->privmsg($event->nick, "$_");
}
close(FOO);
- } elsif( $msg =~/^#? ?(.*)/ ) {
- open(FOO, "-|", "./evalcmd", "_4", "$1");
+ } else {
+ open(FOO, "-|", "./evalcmd", "4", "$msg");
while(<FOO>) {
$conn->privmsg($event->nick, "$_");
}
View
15 evalcmd
@@ -1,14 +1,21 @@
-#!/bin/bash
+#!/bin/bash -x
-output=$(./limitcmd.pl "$1" "$2" | head -c 4242 | tr -d '\r' | awk '{ do { print substr($0, 1, 120); $0 = substr($0, 121); } while(length($0)>0); }'; exit ${PIPESTATUS[0]}; )
+t=${1:-4}
+declare -A triggers='()'
+while read -r trigger shell; do
+ triggers[$trigger]=$shell
+done < ./triggers
+[[ ${triggers[$t]} ]] || exit
+
+output=$(./limitcmd.pl "${triggers[$t]}" "$2" | head -c 4242 | tr -d '\r' | awk '{ do { print substr($0, 1, 120); $0 = substr($0, 121); } while(length($0)>0); }'; exit ${PIPESTATUS[0]}; )
result=$?
lines=$(wc -l <<< "$output")
shopt -s extglob
if [[ $output == *( ) ]]
then
- if (( $result > 0 ))
+ if (( result > 0 ))
then
echo "no output within the time limit"
else
@@ -17,7 +24,7 @@ then
exit 0
fi
-if (( $lines <= 3 )); then
+if (( lines <= 3 )); then
printf '%s\n' "$output";
exit 0
fi
View
25 generate-initramfs
@@ -296,19 +296,14 @@ EOF
# /root/*
cat > initramfs/root/.profile << 'EOF'
-[ -n "$BASH" ] && [ -f ~/.bashrc ] && . ~/.bashrc
-
-[ -f ~/.savestate ] && . ~/.savestate
-rm -f ~/.savestate
-EOF
-cat > initramfs/root/.savestate << 'EOF'
-printf "Ready\n" # Tells savestate it's ok to save now
read RANDOM
read date
date -s "1970-01-01 + $date seconds" > /dev/null 2>&1
printf %d "$RANDOM" > /dev/urandom
unset date
+
+[ -n "$BASH" ] && [ -f ~/.bashrc ] && . ~/.bashrc
EOF
cat > initramfs/root/.bashrc << 'EOF'
# Various useful options for various bash versions.
@@ -343,12 +338,12 @@ export HOME=/root
export PATH=/bin
cd "$HOME"
-case $2 in
- _1) setsid bash1 -login ;;
- _[234]) setsid "bash${2#_}" -l ;;
- _k93) setsid ksh -l ;;
- _sh) setsid sh -l ;;
- *) setsid bash -l ;;
+printf 'Ready\n'
+read -r shell
+
+case $shell in
+ bash1) setsid bash1 -login ;;
+ *) setsid "$shell" -l ;;
esac
printf "(shell exited with %d)\n" "$?"
@@ -373,10 +368,10 @@ done
# awk symlinks
awks=( initramfs/bin/gawk?* )
-ln -s "${awks[-1]##*/}" initramfs/bin/gawk || exit
+ln -sf "${awks[-1]##*/}" initramfs/bin/gawk || exit
for awk in nawk mawk gawk oawk bwk; do
[[ -e initramfs/bin/$awk ]] || continue;
- ln -s "$awk" initramfs/bin/awk
+ ln -sf "$awk" initramfs/bin/awk
break
done
View
3 runqemu
@@ -1,5 +1,6 @@
#!/bin/bash
-exec kvm -hda hda -net none -m 64 -nographic -loadvm "$1" -no-kvm 2>/dev/null << EOF
+exec kvm -hda hda -net none -m 64 -nographic -loadvm 1 -no-kvm 2>/dev/null << EOF
+$1
$RANDOM
$(date -u +%s)
$2
View
5 savestate
@@ -1,16 +1,15 @@
#!/bin/bash
image="$1"
-state=$2
mkfifo fifo # &> /dev/null
[[ -r fifo && -w fifo && -p fifo ]] || { echo "fifo fail"; exit 1; }
-< fifo kvm -kernel build/bzImage -append "-- $state" -initrd initramfs.cpio.gz -hda "$image" -net none -m 64 -nographic -no-kvm |
+< fifo kvm -kernel build/bzImage -initrd initramfs.cpio.gz -hda "$image" -net none -m 64 -nographic -no-kvm |
while read -r f
do
printf '%s\n' "$f" >&2
if [[ $f = $'Ready\r' ]]; then
- printf '\x01c\nsavevm %s\nquit\n' "$state"
+ printf '\x01c\nsavevm\nquit\n'
fi
done > fifo
View
7 triggers
@@ -0,0 +1,7 @@
+1 bash1
+2 bash2
+3 bash3
+4 bash
+k ksh
+sh sh
+d dash

0 comments on commit 8c48b83

Please sign in to comment.
Something went wrong with that request. Please try again.