refactor.

This commit is contained in:
Christopher Jeffrey 2014-08-19 20:10:34 -07:00
parent a176f92c8e
commit 3a1c71669f
4 changed files with 31 additions and 24 deletions

8
README
View File

@ -21,11 +21,11 @@ Changes by chjj
- Automatic shutdown requires a sudoers option to be set in /etc/sudoers: - 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 You must change [username] and [hostname] to your username and the hostname
machine. Note that this only works with systemd, however, it could be of the machine.
modified for a regular init by using the shutdown command.
- Twilio Support: You will receive an SMS to your phone when someone inputs a - 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 wrong password or pressed ALT/CTRL/F1-13/SYSRQ. See twilio_example.h to create a

View File

@ -1,5 +1,5 @@
# slock version # slock version
VERSION = 1.2 VERSION = 2.0
# Customize below to fit your system # Customize below to fit your system

42
slock.c
View File

@ -25,12 +25,14 @@
#include <bsd_auth.h> #include <bsd_auth.h>
#endif #endif
#include "twilio.h"
#define SLOCK_SHUTDOWN 1 #define SLOCK_SHUTDOWN 1
#define TWILIO_SEND 1
#if TWILIO_SEND
#include "twilio.h"
#endif
char *g_pw = NULL; char *g_pw = NULL;
int alt_kill = 1;
int lock_tries = 0; int lock_tries = 0;
typedef struct { typedef struct {
@ -109,6 +111,20 @@ getpw(void) { /* only run as root */
} }
#endif #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 static void
#ifdef HAVE_BSD_AUTH #ifdef HAVE_BSD_AUTH
readpw(Display *dpy) readpw(Display *dpy)
@ -158,14 +174,12 @@ readpw(Display *dpy, const char *pws)
if(running) { if(running) {
XBell(dpy, 100); XBell(dpy, 100);
lock_tries++; lock_tries++;
#ifdef TWILIO_SEND #if TWILIO_SEND
twilio_send("Bad screenlock password.", 1); twilio_send("Bad screenlock password.", 1);
#endif #endif
#if SLOCK_SHUTDOWN #if SLOCK_SHUTDOWN
if(lock_tries > 5) { if(lock_tries > 5) {
// Needs sudo privileges for systemctl poweroff();
char *args[] = { "sudo", "systemctl", "poweroff", NULL };
execvp("sudo", args);
// if we failed, simply resume // if we failed, simply resume
len = 0; len = 0;
break; break;
@ -214,15 +228,11 @@ readpw(Display *dpy, const char *pws)
case XK_F11: case XK_F11:
case XK_F12: case XK_F12:
case XK_F13: case XK_F13:
#ifdef TWILIO_SEND #if TWILIO_SEND
twilio_send("Bad screenlock key.", 0); twilio_send("Bad screenlock key.", 0);
#endif #endif
// Needs sudo privileges for systemctl poweroff();
if (alt_kill) { ; // fall-through if we fail
char *args[] = { "sudo", "systemctl", "poweroff", NULL };
execvp("sudo", args);
// fall-through if we fail
}
#endif #endif
default: default:
if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) { 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")); snprintf(buf, sizeof(buf), "%s/.slock_passwd", getenv("HOME"));
g_pw = read_pfile(buf); g_pw = read_pfile(buf);
if((argc > 1) && !strcmp("-n", argv[1])) { if((argc >= 2) && !strcmp("-v", argv[1])) {
alt_kill = 0;
} else if((argc >= 2) && !strcmp("-v", argv[1])) {
die("slock-%s, © 2006-2012 Anselm R Garbe\n", VERSION); die("slock-%s, © 2006-2012 Anselm R Garbe\n", VERSION);
} else if(argc != 1) { } else if(argc != 1) {
usage(); usage();

View File

@ -1,5 +1,4 @@
#define TWILIO_SEND #define TWILIO_API_SIZE (500 * sizeof(char))
#define TWILIO_API_SIZE (450 * sizeof(char))
static int static int
twilio_send(const char *msg, int async) { twilio_send(const char *msg, int async) {