Difference between revisions of "Execute on USB insert"

From ArchWiki
Jump to: navigation, search
(Security)
(exdongle script)
Line 44: Line 44:
  
 
{{File|name=/usr/bin/exdongle|content=<nowiki>
 
{{File|name=/usr/bin/exdongle|content=<nowiki>
 +
 
#!/bin/sh
 
#!/bin/sh
# exdongle
+
 
 +
# exdongle mkexdongle
  
 
###############################################################################
 
###############################################################################
Line 56: Line 58:
  
 
   echo ""
 
   echo ""
   echo "USAGE: $0 new <DIR> <PROG>"
+
   echo "USAGE: $0 new <DIR> <PROG>"
   echo "      $0 conf [-k KEY] [-p PRIORITY]"
+
  echo "      $0 del <DIR>"
 +
   echo "      $0 conf [-k KEY] [-p PRIORITY] [-s SWITCH]"
 
   echo "      $0 run <DIR>"
 
   echo "      $0 run <DIR>"
 
   echo ""
 
   echo ""
 
   echo "  new:"
 
   echo "  new:"
   echo "    DIR: The directory on the dongle to execute"
+
   echo "    DIR <S>:   The directory on the dongle to execute"
   echo "    PROG: The script to run on dongle"
+
   echo "    SCRIPT <S>: The script to run on dongle"
 +
  echo ""
 +
  echo "  del:"
 +
  echo "    DIR [S]: The directory on the dongle to execute"
 
   echo ""
 
   echo ""
 
   echo "  conf:"
 
   echo "  conf:"
   echo "    -k: specify key"
+
   echo "    -k <S>: specify key"
   echo "    -p: priority to run script at"
+
   echo "    -p <N>: priority to run script at"
 +
  echo "    -s <on|off>: Activate or deactivate exdongle"
 
   echo ""
 
   echo ""
 
   echo "  run:"
 
   echo "  run:"
   echo "    DIR: Directory to find script in"
+
   echo "    DIR <S>: Directory to find script in"
 
   echo ""
 
   echo ""
  
Line 98: Line 105:
 
###############################################################################
 
###############################################################################
  
function conf() {
+
function del() {
  
   while getopts "k:p:" optname; do
+
   DIR="$2"
 +
  PLOC="$DIR/.$(hostname).$key"
  
 +
  if [ -e "$PLOC" ]; then
 +
    rm -f "$PLOC"
 +
  fi
 +
 +
  exit 0
 +
 +
}
 +
 +
###############################################################################
 +
 +
function conf() {
 +
 +
  while getopts "k:p:s:" optname; do
 
     case "$optname" in
 
     case "$optname" in
 
       k)
 
       k)
Line 108: Line 129:
 
       p)
 
       p)
 
         priority="$OPTARG"
 
         priority="$OPTARG"
 +
        ;;
 +
      s)
 +
        if [ "$OPTARG" = "on" ] || [ "$OPTARG" = "off" ]; then
 +
          switch="$OPTARG"
 +
        fi
 
         ;;
 
         ;;
 
       *)
 
       *)
Line 118: Line 144:
 
   echo "key=$key" >> "$CONFFILE"
 
   echo "key=$key" >> "$CONFFILE"
 
   echo "priority=$priority" >> "$CONFFILE"
 
   echo "priority=$priority" >> "$CONFFILE"
 +
  echo "switch=$switch" >> "$CONFFILE"
  
 
   chmod 0600 "$CONFFILE"
 
   chmod 0600 "$CONFFILE"
Line 128: Line 155:
  
 
function run() {
 
function run() {
 +
 +
  if [ ! "$switch" = "on" ]; then
 +
    exit 0
 +
  fi
  
 
   if [ ! "$#" -eq 2 ]; then
 
   if [ ! "$#" -eq 2 ]; then
Line 146: Line 177:
 
   cp "$PLOC" "$ELOC"
 
   cp "$PLOC" "$ELOC"
 
   chmod 0500 "$ELOC"
 
   chmod 0500 "$ELOC"
   nice -n $priority $ELOC
+
   nice -n "$priority" $ELOC
 
+
  rm -f "$ELOC"
 
   exit 0
 
   exit 0
  
Line 166: Line 197:
 
   new)
 
   new)
 
     new $@
 
     new $@
 +
    ;;
 +
  del)
 +
    del $@
 
     ;;
 
     ;;
 
   conf)
 
   conf)
Line 179: Line 213:
  
 
exit 0
 
exit 0
 
 
</nowiki>}}
 
</nowiki>}}

Revision as of 20:51, 14 December 2010

Suppose you want to execute a certain program on the insertion of a usb dongle. Personally, I added this feature because I was getting frustrated with the computer locking up and all keyboard and mouse input was lost (The sysrq keys were not working).

There is an added security feature so that only dongles with the correct key can run the program.

Installation

Put a copy of the [[Execute_on_usb_insert#exdongle_script|Template:Filename script]] (given below) in your path.

Add the following line to your udev USB mount rule (Modify to fit your configuration):

 ACTION=="add", RUN+="/usr/bin/exdongle run /media/%k-%E{dir_name}"

Run the configuration option, for example

 exdongle conf -k "$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM" -p "-10"

This sets the configuration to a random key value (only programs with the correct keys can be run on the computer), and the runtime priority to -10.

Setting up a USB dongle

First, write a script that you want to run when the dongle is inserted.

For example:

Template:File

Now run the new option:

 exdongle new <Dongle mount point> test.sh

Now, whenever you insert the dongle, a root shell will be opened under the X server running on display :0. This may be useful for some administrative purposes.

Security

There may be security issues with this, and it is probably best used for a personal computer.

exdongle script

Template:File