From 3a1c71669f3051b52af3fbc4fdcf9cea2962764d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 19 Aug 2014 20:10:34 -0700 Subject: [PATCH] refactor. --- README | 8 ++++---- config.mk | 2 +- slock.c | 42 +++++++++++++++++++++++++----------------- twilio_example.h | 3 +-- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/README b/README index 7341ab2..d39e2f5 100644 --- a/README +++ b/README @@ -21,11 +21,11 @@ Changes by chjj - Automatic shutdown requires a sudoers option to be set in /etc/sudoers: - [user] [host] =NOPASSWD: /usr/bin/systemctl poweroff,/usr/bin/systemctl halt,/usr/bin/systemctl reboot + - systemd: `[username] [hostname] =NOPASSWD: /usr/bin/systemctl poweroff` + - sysvinit: `[username] [hostname] =NOPASSWD: /usr/bin/shutdown -h now` - You must change [user] and [host] to your username and the hostname of the - machine. Note that this only works with systemd, however, it could be - modified for a regular init by using the shutdown command. + You must change [username] and [hostname] to your username and the hostname + of the machine. - Twilio Support: You will receive an SMS to your phone when someone inputs a wrong password or pressed ALT/CTRL/F1-13/SYSRQ. See twilio_example.h to create a diff --git a/config.mk b/config.mk index 8cc3f68..e566abe 100644 --- a/config.mk +++ b/config.mk @@ -1,5 +1,5 @@ # slock version -VERSION = 1.2 +VERSION = 2.0 # Customize below to fit your system diff --git a/slock.c b/slock.c index 6855e1d..d02a749 100644 --- a/slock.c +++ b/slock.c @@ -25,12 +25,14 @@ #include #endif -#include "twilio.h" - #define SLOCK_SHUTDOWN 1 +#define TWILIO_SEND 1 + +#if TWILIO_SEND +#include "twilio.h" +#endif char *g_pw = NULL; -int alt_kill = 1; int lock_tries = 0; typedef struct { @@ -109,6 +111,20 @@ getpw(void) { /* only run as root */ } #endif +#if SLOCK_SHUTDOWN +static void +poweroff() { + // Needs sudo privileges - alter your /etc/sudoers file: + // systemd: [username] [hostname] =NOPASSWD: /usr/bin/systemctl poweroff + // sysvinit: [username] [hostname] =NOPASSWD: /usr/bin/shutdown -h now + char *args[] = { "sudo", "systemctl", "poweroff", NULL }; + execvp("sudo", args); + char *args_legacy[] = { "sudo", "shutdown", "-h", "now", NULL }; + execvp("sudo", args_legacy); + fprintf(stderr, "Error: cannot shutdown. Check your /etc/sudoers file.\n"); +} +#endif + static void #ifdef HAVE_BSD_AUTH readpw(Display *dpy) @@ -158,14 +174,12 @@ readpw(Display *dpy, const char *pws) if(running) { XBell(dpy, 100); lock_tries++; -#ifdef TWILIO_SEND +#if TWILIO_SEND twilio_send("Bad screenlock password.", 1); #endif #if SLOCK_SHUTDOWN if(lock_tries > 5) { - // Needs sudo privileges for systemctl - char *args[] = { "sudo", "systemctl", "poweroff", NULL }; - execvp("sudo", args); + poweroff(); // if we failed, simply resume len = 0; break; @@ -214,15 +228,11 @@ readpw(Display *dpy, const char *pws) case XK_F11: case XK_F12: case XK_F13: -#ifdef TWILIO_SEND +#if TWILIO_SEND twilio_send("Bad screenlock key.", 0); #endif - // Needs sudo privileges for systemctl - if (alt_kill) { - char *args[] = { "sudo", "systemctl", "poweroff", NULL }; - execvp("sudo", args); - // fall-through if we fail - } + poweroff(); + ; // fall-through if we fail #endif default: if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) { @@ -365,9 +375,7 @@ main(int argc, char **argv) { snprintf(buf, sizeof(buf), "%s/.slock_passwd", getenv("HOME")); g_pw = read_pfile(buf); - if((argc > 1) && !strcmp("-n", argv[1])) { - alt_kill = 0; - } else if((argc >= 2) && !strcmp("-v", argv[1])) { + if((argc >= 2) && !strcmp("-v", argv[1])) { die("slock-%s, © 2006-2012 Anselm R Garbe\n", VERSION); } else if(argc != 1) { usage(); diff --git a/twilio_example.h b/twilio_example.h index 84d48fd..298f8ca 100644 --- a/twilio_example.h +++ b/twilio_example.h @@ -1,5 +1,4 @@ -#define TWILIO_SEND -#define TWILIO_API_SIZE (450 * sizeof(char)) +#define TWILIO_API_SIZE (500 * sizeof(char)) static int twilio_send(const char *msg, int async) {