package main import ( "fmt" "log" "os" "path/filepath" "strings" "time" ) var VERSION = "v0.1.0" /* 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:") fmt.Println(" usblocker run: Start USBLocker.") //fmt.Println(" usblocker daemon: Runs in background.") //fmt.Println(" usblocker create: Prepares a device for usage with USBLocker.") } /* */ func ShowMOTD() { fmt.Println("Welcome to USBLocker ", VERSION) fmt.Println() fmt.Println("Press Ctrl-C to exit.") } /* 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") var args []string args = append(args, "-nolock") // Fix for... something. First passed argument keeps beeing ignored, for whatever reason. //args = append(args, "-info") //args = append(args, "") args = append(args, "-mode") args = append(args, "blank") //log.Println(args) var attr os.ProcAttr lpid, err := os.StartProcess("/usr/bin/xlock", args, &attr) if err != nil { log.Fatal("Error: ", err) } WatcherLocked(lpid) } /* Watcher for locked state / no device */ func WatcherLocked(lpid *os.Process) { var passphrase string // TEH KEY!!1! var devices []string // list of devices var lfile *os.File // device file ldev := "" // device file name version := "v0" // version string var passtest []byte for i := 0; i < 1024; i++ { passtest = append(passtest, 0) } // 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==" for { var err error devices, err = filepath.Glob("/dev/?d?1") if err != nil { log.Fatal("Error while getting list of aviable devices (/dev/?d?1):", err) } // Search lock device for i := range devices { log.Println("Checking", devices[i], "for usblocker data...") lfile, err = os.Open(devices[i]) if err != nil { log.Fatal("Error while opening the device file:", err) } // Check for magic string magictest := []byte(" ") 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" { ldev = devices[i] break } } // Check lockdev version if ldev != "" { log.Println("Checking", ldev, "for right version...") versiontest := []byte(" ") length, err := lfile.Read(versiontest) if err != nil { log.Fatal("Error while reading the device file:", err) } if length == 7 && strings.HasPrefix(string(versiontest), version) { log.Println("Checking passphrase...") length, err := lfile.Read(passtest) _ = lfile.Close() if err != nil { log.Fatal("Error while reading the device file:", err) } fmt.Println(passphrase) fmt.Println() fmt.Println(string(passtest)) if length == 1024 && string(passtest) == passphrase { // And finally: UNLOCK! log.Println("Unlocked") if lpid != nil { lpid.Kill() _, _ = lpid.Wait() } WatcherUnlocked(ldev) } } } time.Sleep(time.Duration(1) * time.Second) } } /* The main function */ func main() { // Determining what to do if len(os.Args) == 1 { ShowHelp() return } switch os.Args[1] { case "run": case "daemon": log.Fatal("Not yet implemented. Probably won't be implemented anyway, go doesn't seem to support forking...") case "create": log.Fatal("Not yet implemented.") default: ShowHelp() } // Check for root if os.Getuid() != 0 { log.Fatal("YU NO GOT ROOT??") } ShowMOTD() WatcherLocked(nil) }