1
0
Fork 0
usblocker/usblocker.go

214 lines
5.9 KiB
Go
Raw Normal View History

2013-11-20 20:53:27 +01:00
package main
import (
2014-01-14 01:18:52 +01:00
//"code.google.com/p/gcfg"
"encoding/json"
2013-11-20 20:53:27 +01:00
"fmt"
2014-01-14 01:18:52 +01:00
"io/ioutil"
2013-11-20 20:53:27 +01:00
"log"
"os"
2014-01-14 01:18:52 +01:00
"os/exec"
2013-11-20 20:53:27 +01:00
"path/filepath"
2014-01-14 01:18:52 +01:00
"regexp"
2013-11-20 20:53:27 +01:00
"strings"
"time"
)
2014-01-14 01:18:52 +01:00
type Configuration struct {
Lockfile string
Keyfile string
Xlock_path string
Blacklist_devices bool
}
2013-11-21 23:23:28 +01:00
var VERSION = "v0.1.0"
2014-01-14 01:18:52 +01:00
var CONFIGFILE = "config.json"
// Default configuration
var cfg = Configuration{
Lockfile: "/dev/sd??",
Keyfile: "usblocker.key",
Xlock_path: "/bin/xlock",
Blacklist_devices: true,
}
var FIRSTRUN = true
var cmd = exec.Command("")
2013-11-21 23:23:28 +01:00
2013-11-20 20:53:27 +01:00
/* Shows the help message
*/
func ShowHelp() {
fmt.Println("Watches a device and locks the screen if the device is missing.")
fmt.Println()
fmt.Println("Usage:")
2014-01-14 01:18:52 +01:00
fmt.Println(" usblocker start: Start USBLocker.")
2013-11-20 20:53:27 +01:00
//fmt.Println(" usblocker daemon: Runs in background.")
//fmt.Println(" usblocker create: Prepares a device for usage with USBLocker.")
2013-11-21 23:23:28 +01:00
}
2014-01-14 01:18:52 +01:00
/* Shows the message of the day
2013-11-21 23:23:28 +01:00
*/
func ShowMOTD() {
fmt.Println("Welcome to USBLocker ", VERSION)
fmt.Println()
fmt.Println("Press Ctrl-C to exit.")
2013-11-20 20:53:27 +01:00
}
/* The watcher himself!
*/
func WatcherUnlocked(device string) {
// Watch!
for {
_, errs := os.Stat(device)
if errs != nil {
break
}
time.Sleep(time.Duration(1) * time.Second)
}
log.Println("Locked")
2014-01-14 01:18:52 +01:00
cmd = exec.Command("/usr/bin/xlock", "-mode", "blank")
err := cmd.Start()
2013-11-20 20:53:27 +01:00
if err != nil {
log.Fatal("Error: ", err)
}
2014-01-14 01:18:52 +01:00
WatcherLocked()
2013-11-20 20:53:27 +01:00
}
/* Watcher for locked state / no device
*/
2014-01-14 01:18:52 +01:00
func WatcherLocked() {
var passphrase string // the key
var passtest = make([]byte, 1024) // slice for testing the passphrase
var devices []string // list of devices
var devblacklist []string // blacklist of devices
var lfile *os.File // device file
ldev := "" // device file name
lfileversion := "v1" // version string
2013-11-20 20:53:27 +01:00
2013-11-21 23:23:28 +01:00
// Set the passphrase
passphrase = "wictQQT86qi7YhZP3wVIZek+x9X0ELJA5LN2amEdh54xXs0yGlGx2kRrfIXC+C3iIX9iYRMKrv0bFSyR29SVTVhl2Q0LFOvvAmawiRrJ9MLwyiBrTVfhsVLcZcKNI5Up6xjebezqyhTcZBzEuRYk4VueCXbjI0ISK0LQgMdKxiybQOt1+FLAmgtYj+izB4xvEd9A+wQOnb+wKqZ8TpWh8h/HVfQLgobXDND8I7SXYC1Qm5Kv4wUBWUDg3YOWwMLWfYvyCU4ZyKi5FG3GWvLvKKMxk7vHA/YDyz/eXo3x07zmqDqZHP19k9nGex6ubzmZOWLZz1zEwsdweXCqYps9waXwhJ/9zgrx735IRE3gPL/KYP/X5WTMBXMYFkCAzT6LHNO2gWzdHqdIjyBPBoOHDtVFhKp7prN6GCn/h4Bwr5VzxC+NrUQPG3AqdQdnNGf9zmUV7VE4ZtRdC5dA5I0rJkX9xIbps7IiF9TACIHwXWVEQ+VpQFXRT063DcMxzlmP0b55k4Y2YEZsFEIYO0/hNqMl5D/ETNriiKIpr296WfRqPC6RsGgodZ6TIA8/XqRWsmkCjBHHPSv7BfVDNUbH/hiINH+8U/YfUGfFOly301HXZlEpqLjCXdEw8kzNN06anFTd+6YsTOScGLeddi1urqUCQ9v5nM1iAoXj0djOUdbZks8RgXdf583hhwHWG/Ib5HLSr4ISMQxFhjWKcHvo4ffeYLiOP85ulNsL5ZbtOUL2MX/DuLE1mKopoj50Jjb0mV+QiXUkifjZgGuXyGuZYfNYtMSmkRGERkJfrLeQyd69o+CLG7sfywOgelFWHTwkeVQNbpjRPmTLdSsq0MuJeIlcIAEKECQgBkNMOE2WuHOC2jr8Ba018wESW/hXAairG7ft7Mmf+AMCdjamFhb56YFbE9LumQ8ILgK9q6FQ6OU39qP+wCivdtSEnDnip4EVw7yItj1/0GVs1QPr20gDHO0G2t90JPyApmqtfKq4CY/tKgDhAkBwNXyGEVCrsA=="
2013-11-20 20:53:27 +01:00
for {
var err error
2014-01-14 01:18:52 +01:00
devices, err = filepath.Glob(cfg.Lockfile)
2013-11-20 20:53:27 +01:00
if err != nil {
2014-01-14 01:18:52 +01:00
log.Fatal("Error while getting list of aviable devices ", cfg.Lockfile, ":", err)
}
if len(devices) == 0 {
log.Fatal("Not found: ", cfg.Lockfile)
2013-11-20 20:53:27 +01:00
}
// Search lock device
2014-01-14 01:18:52 +01:00
for _, device := range devices {
log.Println("Checking", device, "for usblocker data...")
lfile, err = os.Open(device)
2013-11-20 20:53:27 +01:00
if err != nil {
log.Fatal("Error while opening the device file:", err)
}
// Check for magic string
2014-01-14 01:18:52 +01:00
magictest := make([]byte, 9)
2013-11-20 20:53:27 +01:00
length, err := lfile.Read(magictest)
if err != nil {
log.Fatal("Error while reading the device file:", err)
_ = lfile.Close()
}
if length == 9 && string(magictest) == "usblocker" {
2014-01-14 01:18:52 +01:00
ldev = device
2013-11-20 20:53:27 +01:00
break
2014-01-14 01:18:52 +01:00
} else {
if cfg.Blacklist_devices && FIRSTRUN {
if len(devblacklist) == 0 {
devblacklist = append(devblacklist, device)
} else {
for _, v := range devblacklist {
if v != device {
devblacklist = append(devblacklist, device)
}
}
}
FIRSTRUN = false
}
2013-11-20 20:53:27 +01:00
}
}
// Check lockdev version
if ldev != "" {
log.Println("Checking", ldev, "for right version...")
2014-01-14 01:18:52 +01:00
versiontest := make([]byte, 7)
2013-11-20 20:53:27 +01:00
length, err := lfile.Read(versiontest)
if err != nil {
log.Fatal("Error while reading the device file:", err)
}
2014-01-14 01:18:52 +01:00
if length == 7 && strings.HasPrefix(string(versiontest), lfileversion) {
2013-11-21 23:23:28 +01:00
log.Println("Checking passphrase...")
length, err := lfile.Read(passtest)
_ = lfile.Close()
if err != nil {
log.Fatal("Error while reading the device file:", err)
}
if length == 1024 && string(passtest) == passphrase {
// And finally: UNLOCK!
log.Println("Unlocked")
2014-01-14 01:18:52 +01:00
if cmd.Process != nil {
cmd.Process.Kill()
_ = cmd.Wait()
2013-11-21 23:23:28 +01:00
}
WatcherUnlocked(ldev)
2013-11-20 20:53:27 +01:00
}
}
}
time.Sleep(time.Duration(1) * time.Second)
}
}
/* The main function
*/
func main() {
// Determining what to do
2014-01-14 01:18:52 +01:00
if len(os.Args) < 2 {
2013-11-20 20:53:27 +01:00
ShowHelp()
return
}
switch os.Args[1] {
2014-01-14 01:18:52 +01:00
case "start":
2013-11-24 22:13:43 +01:00
//case "daemon":
// log.Fatal("Not yet implemented. Probably won't be implemented anyway, go doesn't seem to support forking...")
2013-11-20 20:53:27 +01:00
case "create":
log.Fatal("Not yet implemented.")
default:
ShowHelp()
2014-01-14 01:18:52 +01:00
return
2013-11-20 20:53:27 +01:00
}
// Check for root
if os.Getuid() != 0 {
2014-01-14 01:18:52 +01:00
log.Fatal("Y U NO GOT ROOT??")
}
// Get configuration from file config.json
b, err := ioutil.ReadFile(CONFIGFILE)
if err != nil {
log.Println("Cannot read configuration file:", err)
log.Println("Using default configuration")
} else {
re := regexp.MustCompile("(?m)^\\s*//.*$")
jdata := re.ReplaceAllString(string(b), "")
err = json.Unmarshal([]byte(jdata), &cfg)
if err != nil {
log.Fatal("Failed to get configuration from json:", err)
}
}
// Check for xlock
_, err = os.Stat(cfg.Xlock_path)
if err != nil {
os.Stdout.Write([]byte("Is xlock installed and the right path set in config.json?\n"))
log.Fatal("Stat for ", cfg.Xlock_path, " failed:", err)
2013-11-20 20:53:27 +01:00
}
2013-11-21 23:23:28 +01:00
ShowMOTD()
2014-01-14 01:18:52 +01:00
WatcherLocked()
2013-11-20 20:53:27 +01:00
}