Compare commits
43 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ad511590d5 | ||
|
35633d4567 | ||
|
2d2a21a90a | ||
|
325581b935 | ||
|
0ff0d9f7a7 | ||
|
7a604ec1fa | ||
|
fa11589584 | ||
|
d5da84cf5a | ||
|
ae96836f90 | ||
|
7d31ff65a0 | ||
|
597469541c | ||
|
c96e725df0 | ||
|
6a1bd89635 | ||
|
b099d2fd18 | ||
|
0a43b78d00 | ||
|
bd069b08c5 | ||
|
2d85c5baed | ||
|
a98fba8971 | ||
|
dc2e8e839e | ||
|
9a617db716 | ||
|
b00f444a4e | ||
|
cd3c546c37 | ||
|
22eba05f36 | ||
|
04143fd68d | ||
|
9698224090 | ||
|
a55594fdd6 | ||
|
03a8717991 | ||
|
39fb855aa1 | ||
|
e378f735d8 | ||
|
1f66885fbf | ||
|
137f0076c2 | ||
|
a9eddbd94f | ||
|
d8bec0f6fd | ||
|
b87bfa2343 | ||
|
c2f975773d | ||
|
3bb868e408 | ||
|
a7afade170 | ||
|
65b8d52788 | ||
|
ec46680fe1 | ||
|
b02c4d452a | ||
|
a6dc051e37 | ||
|
bdcbeab0d8 | ||
|
3abbffa493 |
8
Makefile
8
Makefile
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
include config.mk
|
include config.mk
|
||||||
|
|
||||||
SRC = slock.c
|
SRC = slock.c ${COMPATSRC}
|
||||||
OBJ = ${SRC:.c=.o}
|
OBJ = ${SRC:.c=.o}
|
||||||
|
|
||||||
all: options slock
|
all: options slock
|
||||||
@@ -18,7 +18,7 @@ options:
|
|||||||
@echo CC $<
|
@echo CC $<
|
||||||
@${CC} -c ${CFLAGS} $<
|
@${CC} -c ${CFLAGS} $<
|
||||||
|
|
||||||
${OBJ}: config.h config.mk
|
${OBJ}: config.h config.mk arg.h util.h
|
||||||
|
|
||||||
config.h:
|
config.h:
|
||||||
@echo creating $@ from config.def.h
|
@echo creating $@ from config.def.h
|
||||||
@@ -35,8 +35,8 @@ clean:
|
|||||||
dist: clean
|
dist: clean
|
||||||
@echo creating dist tarball
|
@echo creating dist tarball
|
||||||
@mkdir -p slock-${VERSION}
|
@mkdir -p slock-${VERSION}
|
||||||
@cp -R LICENSE Makefile README config.def.h config.mk ${SRC} slock.1 \
|
@cp -R LICENSE Makefile README slock.1 config.mk \
|
||||||
slock-${VERSION}
|
${SRC} explicit_bzero.c config.def.h arg.h util.h slock-${VERSION}
|
||||||
@tar -cf slock-${VERSION}.tar slock-${VERSION}
|
@tar -cf slock-${VERSION}.tar slock-${VERSION}
|
||||||
@gzip slock-${VERSION}.tar
|
@gzip slock-${VERSION}.tar
|
||||||
@rm -rf slock-${VERSION}
|
@rm -rf slock-${VERSION}
|
||||||
|
2
README
2
README
@@ -1,6 +1,6 @@
|
|||||||
slock - simple screen locker
|
slock - simple screen locker
|
||||||
============================
|
============================
|
||||||
simple screen locker utility for X.
|
simple screen locker utility for X.
|
||||||
|
|
||||||
|
|
||||||
Requirements
|
Requirements
|
||||||
|
65
arg.h
Normal file
65
arg.h
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Copy me if you can.
|
||||||
|
* by 20h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ARG_H__
|
||||||
|
#define ARG_H__
|
||||||
|
|
||||||
|
extern char *argv0;
|
||||||
|
|
||||||
|
/* use main(int argc, char *argv[]) */
|
||||||
|
#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
|
||||||
|
argv[0] && argv[0][0] == '-'\
|
||||||
|
&& argv[0][1];\
|
||||||
|
argc--, argv++) {\
|
||||||
|
char argc_;\
|
||||||
|
char **argv_;\
|
||||||
|
int brk_;\
|
||||||
|
if (argv[0][1] == '-' && argv[0][2] == '\0') {\
|
||||||
|
argv++;\
|
||||||
|
argc--;\
|
||||||
|
break;\
|
||||||
|
}\
|
||||||
|
for (brk_ = 0, argv[0]++, argv_ = argv;\
|
||||||
|
argv[0][0] && !brk_;\
|
||||||
|
argv[0]++) {\
|
||||||
|
if (argv_ != argv)\
|
||||||
|
break;\
|
||||||
|
argc_ = argv[0][0];\
|
||||||
|
switch (argc_)
|
||||||
|
|
||||||
|
/* Handles obsolete -NUM syntax */
|
||||||
|
#define ARGNUM case '0':\
|
||||||
|
case '1':\
|
||||||
|
case '2':\
|
||||||
|
case '3':\
|
||||||
|
case '4':\
|
||||||
|
case '5':\
|
||||||
|
case '6':\
|
||||||
|
case '7':\
|
||||||
|
case '8':\
|
||||||
|
case '9'
|
||||||
|
|
||||||
|
#define ARGEND }\
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ARGC() argc_
|
||||||
|
|
||||||
|
#define ARGNUMF() (brk_ = 1, estrtonum(argv[0], 0, INT_MAX))
|
||||||
|
|
||||||
|
#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\
|
||||||
|
((x), abort(), (char *)0) :\
|
||||||
|
(brk_ = 1, (argv[0][1] != '\0')?\
|
||||||
|
(&argv[0][1]) :\
|
||||||
|
(argc--, argv++, argv[0])))
|
||||||
|
|
||||||
|
#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\
|
||||||
|
(char *)0 :\
|
||||||
|
(brk_ = 1, (argv[0][1] != '\0')?\
|
||||||
|
(&argv[0][1]) :\
|
||||||
|
(argc--, argv++, argv[0])))
|
||||||
|
|
||||||
|
#define LNGARG() &argv[0][0]
|
||||||
|
|
||||||
|
#endif
|
14
config.def.h
14
config.def.h
@@ -1,6 +1,12 @@
|
|||||||
|
/* user and group to drop privileges to */
|
||||||
|
static const char *user = "krrish";
|
||||||
|
static const char *group = "krrish";
|
||||||
|
|
||||||
static const char *colorname[NUMCOLS] = {
|
static const char *colorname[NUMCOLS] = {
|
||||||
"black", /* after initialization */
|
[INIT] = "black", /* after initialization */
|
||||||
"#005577", /* during input */
|
[INPUT] = "#005577", /* during input */
|
||||||
"#CC3333", /* failed/cleared the input */
|
[FAILED] = "#CC3333", /* wrong password */
|
||||||
};
|
};
|
||||||
static const Bool failonclear = True;
|
|
||||||
|
/* treat a cleared input like a wrong password (color) */
|
||||||
|
static const int failonclear = 1;
|
||||||
|
12
config.h
Normal file
12
config.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
/* user and group to drop privileges to */
|
||||||
|
static const char *user = "krrish";
|
||||||
|
static const char *group = "krrish";
|
||||||
|
|
||||||
|
static const char *colorname[NUMCOLS] = {
|
||||||
|
[INIT] = "black", /* after initialization */
|
||||||
|
[INPUT] = "#005577", /* during input */
|
||||||
|
[FAILED] = "#CC3333", /* wrong password */
|
||||||
|
};
|
||||||
|
|
||||||
|
/* treat a cleared input like a wrong password (color) */
|
||||||
|
static const int failonclear = 1;
|
17
config.mk
17
config.mk
@@ -1,5 +1,5 @@
|
|||||||
# slock version
|
# slock version
|
||||||
VERSION = 1.3
|
VERSION = 1.4
|
||||||
|
|
||||||
# Customize below to fit your system
|
# Customize below to fit your system
|
||||||
|
|
||||||
@@ -15,17 +15,18 @@ INCS = -I. -I/usr/include -I${X11INC}
|
|||||||
LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr
|
LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr
|
||||||
|
|
||||||
# flags
|
# flags
|
||||||
CPPFLAGS = -DVERSION=\"${VERSION}\" -DHAVE_SHADOW_H
|
CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H
|
||||||
CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
|
CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
|
||||||
LDFLAGS = -s ${LIBS}
|
LDFLAGS = -s ${LIBS}
|
||||||
|
COMPATSRC = explicit_bzero.c
|
||||||
|
|
||||||
# On *BSD remove -DHAVE_SHADOW_H from CPPFLAGS and add -DHAVE_BSD_AUTH
|
|
||||||
# On OpenBSD and Darwin remove -lcrypt from LIBS
|
# On OpenBSD and Darwin remove -lcrypt from LIBS
|
||||||
|
#LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lXext -lXrandr
|
||||||
|
# On *BSD remove -DHAVE_SHADOW_H from CPPFLAGS
|
||||||
|
# On NetBSD add -D_NETBSD_SOURCE to CPPFLAGS
|
||||||
|
#CPPFLAGS = -DVERSION=\"${VERSION}\" -D_BSD_SOURCE -D_NETBSD_SOURCE
|
||||||
|
# On OpenBSD set COMPATSRC to empty
|
||||||
|
#COMPATSRC =
|
||||||
|
|
||||||
# compiler and linker
|
# compiler and linker
|
||||||
CC = cc
|
CC = cc
|
||||||
|
|
||||||
# Install mode. On BSD systems MODE=2755 and GROUP=auth
|
|
||||||
# On others MODE=4755 and GROUP=root
|
|
||||||
#MODE=2755
|
|
||||||
#GROUP=auth
|
|
||||||
|
19
explicit_bzero.c
Normal file
19
explicit_bzero.c
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
/* $OpenBSD: explicit_bzero.c,v 1.3 2014/06/21 02:34:26 matthew Exp $ */
|
||||||
|
/*
|
||||||
|
* Public domain.
|
||||||
|
* Written by Matthew Dempsky.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
__attribute__((weak)) void
|
||||||
|
__explicit_bzero_hook(void *buf, size_t len)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
explicit_bzero(void *buf, size_t len)
|
||||||
|
{
|
||||||
|
memset(buf, 0, len);
|
||||||
|
__explicit_bzero_hook(buf, len);
|
||||||
|
}
|
68
slock.1
68
slock.1
@@ -1,29 +1,39 @@
|
|||||||
.TH SLOCK 1 slock\-VERSION
|
.Dd 2016-08-23
|
||||||
.SH NAME
|
.Dt SLOCK 1
|
||||||
slock \- simple X display locker
|
.Sh NAME
|
||||||
.SH SYNOPSIS
|
.Nm slock
|
||||||
.B slock
|
.Nd simple X screen locker
|
||||||
.RB [ \-v
|
.Sh SYNOPSIS
|
||||||
|
|
.Nm
|
||||||
.IR post_lock_command ]
|
.Op Fl v
|
||||||
.SH DESCRIPTION
|
.Op Ar cmd Op Ar arg ...
|
||||||
.B slock
|
.Sh DESCRIPTION
|
||||||
is a screen locker for X. If provided, the
|
.Nm
|
||||||
.IR post_lock_command
|
is a simple X screen locker. If provided,
|
||||||
will be executed when the screen is locked.
|
.Ar cmd Op Ar arg ...
|
||||||
.SH OPTIONS
|
is executed after the screen has been locked.
|
||||||
.TP
|
.Sh OPTIONS
|
||||||
.B \-v
|
.Bl -tag -width Ds
|
||||||
prints version information to stdout, then exits.
|
.It Fl v
|
||||||
.SH EXAMPLES
|
Print version information to stdout and exit.
|
||||||
$ slock /usr/sbin/s2ram
|
.El
|
||||||
.SH CUSTOMIZATION
|
.Sh SECURITY CONSIDERATIONS
|
||||||
.B slock
|
To make sure a locked screen can not be bypassed by switching VTs
|
||||||
can be customized by creating a custom config.h and (re)compiling the source
|
or killing the X server with Ctrl+Alt+Backspace, it is recommended
|
||||||
code. This keeps it fast, secure and simple.
|
to disable both in
|
||||||
.SH AUTHORS
|
.Xr xorg.conf 5
|
||||||
See the LICENSE file for the authors.
|
for maximum security:
|
||||||
.SH LICENSE
|
.Bd -literal -offset left
|
||||||
See the LICENSE file for the terms of redistribution.
|
Section "ServerFlags"
|
||||||
.SH BUGS
|
Option "DontVTSwitch" "True"
|
||||||
Please report them.
|
Option "DontZap" "True"
|
||||||
|
EndSection
|
||||||
|
.Ed
|
||||||
|
.Sh EXAMPLES
|
||||||
|
$
|
||||||
|
.Nm
|
||||||
|
/usr/sbin/s2ram
|
||||||
|
.Sh CUSTOMIZATION
|
||||||
|
.Nm
|
||||||
|
can be customized by creating a custom config.h from config.def.h and
|
||||||
|
(re)compiling the source code. This keeps it fast, secure and simple.
|
||||||
|
387
slock.c
387
slock.c
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <grp.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -18,10 +19,10 @@
|
|||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
#include <X11/Xutil.h>
|
#include <X11/Xutil.h>
|
||||||
|
|
||||||
#if HAVE_BSD_AUTH
|
#include "arg.h"
|
||||||
#include <login_cap.h>
|
#include "util.h"
|
||||||
#include <bsd_auth.h>
|
|
||||||
#endif
|
char *argv0;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
INIT,
|
INIT,
|
||||||
@@ -30,22 +31,20 @@ enum {
|
|||||||
NUMCOLS
|
NUMCOLS
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "config.h"
|
struct lock {
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int screen;
|
int screen;
|
||||||
Window root, win;
|
Window root, win;
|
||||||
Pixmap pmap;
|
Pixmap pmap;
|
||||||
unsigned long colors[NUMCOLS];
|
unsigned long colors[NUMCOLS];
|
||||||
} Lock;
|
};
|
||||||
|
|
||||||
static Lock **locks;
|
struct xrandr {
|
||||||
static int nscreens;
|
int active;
|
||||||
static Bool running = True;
|
int evbase;
|
||||||
static Bool failure = False;
|
int errbase;
|
||||||
static Bool rr;
|
};
|
||||||
static int rrevbase;
|
|
||||||
static int rrerrbase;
|
#include "config.h"
|
||||||
|
|
||||||
static void
|
static void
|
||||||
die(const char *errstr, ...)
|
die(const char *errstr, ...)
|
||||||
@@ -65,34 +64,32 @@ die(const char *errstr, ...)
|
|||||||
static void
|
static void
|
||||||
dontkillme(void)
|
dontkillme(void)
|
||||||
{
|
{
|
||||||
int fd;
|
FILE *f;
|
||||||
int length;
|
const char oomfile[] = "/proc/self/oom_score_adj";
|
||||||
char value[64];
|
|
||||||
|
|
||||||
fd = open("/proc/self/oom_score_adj", O_WRONLY);
|
if (!(f = fopen(oomfile, "w"))) {
|
||||||
if (fd < 0 && errno == ENOENT)
|
if (errno == ENOENT)
|
||||||
return;
|
return;
|
||||||
|
die("slock: fopen %s: %s\n", oomfile, strerror(errno));
|
||||||
/* convert OOM_SCORE_ADJ_MIN to string for writing */
|
}
|
||||||
length = snprintf(value, sizeof(value), "%d\n", OOM_SCORE_ADJ_MIN);
|
fprintf(f, "%d", OOM_SCORE_ADJ_MIN);
|
||||||
|
if (fclose(f)) {
|
||||||
/* bail on truncation */
|
if (errno == EACCES)
|
||||||
if (length >= sizeof(value))
|
die("slock: unable to disable OOM killer. "
|
||||||
die("buffer too small\n");
|
"Make sure to suid or sgid slock.\n");
|
||||||
|
else
|
||||||
if (fd < 0 || write(fd, value, length) != length || close(fd) != 0)
|
die("slock: fclose %s: %s\n", oomfile, strerror(errno));
|
||||||
die("cannot disable the out-of-memory killer for this process (make sure to suid or sgid slock)\n");
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef HAVE_BSD_AUTH
|
|
||||||
/* only run as root */
|
|
||||||
static const char *
|
static const char *
|
||||||
getpw(void)
|
gethash(void)
|
||||||
{
|
{
|
||||||
const char *rval;
|
const char *hash;
|
||||||
struct passwd *pw;
|
struct passwd *pw;
|
||||||
|
|
||||||
|
/* Check if the current user has a password entry */
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if (!(pw = getpwuid(getuid()))) {
|
if (!(pw = getpwuid(getuid()))) {
|
||||||
if (errno)
|
if (errno)
|
||||||
@@ -100,49 +97,52 @@ getpw(void)
|
|||||||
else
|
else
|
||||||
die("slock: cannot retrieve password entry\n");
|
die("slock: cannot retrieve password entry\n");
|
||||||
}
|
}
|
||||||
rval = pw->pw_passwd;
|
hash = pw->pw_passwd;
|
||||||
|
|
||||||
#if HAVE_SHADOW_H
|
#if HAVE_SHADOW_H
|
||||||
if (rval[0] == 'x' && rval[1] == '\0') {
|
if (!strcmp(hash, "x")) {
|
||||||
struct spwd *sp;
|
struct spwd *sp;
|
||||||
if (!(sp = getspnam(getenv("USER"))))
|
if (!(sp = getspnam(pw->pw_name)))
|
||||||
die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
|
die("slock: getspnam: cannot retrieve shadow entry. "
|
||||||
rval = sp->sp_pwdp;
|
"Make sure to suid or sgid slock.\n");
|
||||||
|
hash = sp->sp_pwdp;
|
||||||
}
|
}
|
||||||
#endif
|
#else
|
||||||
|
if (!strcmp(hash, "*")) {
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
if (!(pw = getpwuid_shadow(getuid())))
|
||||||
|
die("slock: getpwnam_shadow: cannot retrieve shadow entry. "
|
||||||
|
"Make sure to suid or sgid slock.\n");
|
||||||
|
hash = pw->pw_passwd;
|
||||||
|
#else
|
||||||
|
die("slock: getpwuid: cannot retrieve shadow entry. "
|
||||||
|
"Make sure to suid or sgid slock.\n");
|
||||||
|
#endif /* __OpenBSD__ */
|
||||||
|
}
|
||||||
|
#endif /* HAVE_SHADOW_H */
|
||||||
|
|
||||||
/* drop privileges */
|
return hash;
|
||||||
if (geteuid() == 0 &&
|
|
||||||
((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0))
|
|
||||||
die("slock: cannot drop privileges\n");
|
|
||||||
return rval;
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
#ifdef HAVE_BSD_AUTH
|
readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
|
||||||
readpw(Display *dpy)
|
const char *hash)
|
||||||
#else
|
|
||||||
readpw(Display *dpy, const char *pws)
|
|
||||||
#endif
|
|
||||||
{
|
{
|
||||||
char buf[32], passwd[256];
|
XRRScreenChangeNotifyEvent *rre;
|
||||||
int num, screen;
|
char buf[32], passwd[256], *inputhash;
|
||||||
|
int num, screen, running, failure, oldc;
|
||||||
unsigned int len, color;
|
unsigned int len, color;
|
||||||
KeySym ksym;
|
KeySym ksym;
|
||||||
XEvent ev;
|
XEvent ev;
|
||||||
static int oldc = INIT;
|
|
||||||
|
|
||||||
len = 0;
|
len = 0;
|
||||||
running = True;
|
running = 1;
|
||||||
|
failure = 0;
|
||||||
|
oldc = INIT;
|
||||||
|
|
||||||
/* As "slock" stands for "Simple X display locker", the DPMS settings
|
|
||||||
* had been removed and you can set it with "xset" or some other
|
|
||||||
* utility. This way the user can easily set a customized DPMS
|
|
||||||
* timeout. */
|
|
||||||
while (running && !XNextEvent(dpy, &ev)) {
|
while (running && !XNextEvent(dpy, &ev)) {
|
||||||
if (ev.type == KeyPress) {
|
if (ev.type == KeyPress) {
|
||||||
buf[0] = 0;
|
explicit_bzero(&buf, sizeof(buf));
|
||||||
num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
|
num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
|
||||||
if (IsKeypadKey(ksym)) {
|
if (IsKeypadKey(ksym)) {
|
||||||
if (ksym == XK_KP_Enter)
|
if (ksym == XK_KP_Enter)
|
||||||
@@ -158,201 +158,238 @@ readpw(Display *dpy, const char *pws)
|
|||||||
continue;
|
continue;
|
||||||
switch (ksym) {
|
switch (ksym) {
|
||||||
case XK_Return:
|
case XK_Return:
|
||||||
passwd[len] = 0;
|
passwd[len] = '\0';
|
||||||
#ifdef HAVE_BSD_AUTH
|
errno = 0;
|
||||||
running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
|
if (!(inputhash = crypt(passwd, hash)))
|
||||||
#else
|
fprintf(stderr, "slock: crypt: %s\n", strerror(errno));
|
||||||
running = !!strcmp(crypt(passwd, pws), pws);
|
else
|
||||||
#endif
|
running = !!strcmp(inputhash, hash);
|
||||||
if (running) {
|
if (running) {
|
||||||
XBell(dpy, 100);
|
XBell(dpy, 100);
|
||||||
failure = True;
|
failure = 1;
|
||||||
}
|
}
|
||||||
|
explicit_bzero(&passwd, sizeof(passwd));
|
||||||
len = 0;
|
len = 0;
|
||||||
break;
|
break;
|
||||||
case XK_Escape:
|
case XK_Escape:
|
||||||
|
explicit_bzero(&passwd, sizeof(passwd));
|
||||||
len = 0;
|
len = 0;
|
||||||
break;
|
break;
|
||||||
case XK_BackSpace:
|
case XK_BackSpace:
|
||||||
if (len)
|
if (len)
|
||||||
--len;
|
passwd[--len] = '\0';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (num && !iscntrl((int)buf[0]) && (len + num < sizeof(passwd))) {
|
if (num && !iscntrl((int)buf[0]) &&
|
||||||
|
(len + num < sizeof(passwd))) {
|
||||||
memcpy(passwd + len, buf, num);
|
memcpy(passwd + len, buf, num);
|
||||||
len += num;
|
len += num;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
color = len ? INPUT : (failure || failonclear ? FAILED : INIT);
|
color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
|
||||||
if (running && oldc != color) {
|
if (running && oldc != color) {
|
||||||
for (screen = 0; screen < nscreens; screen++) {
|
for (screen = 0; screen < nscreens; screen++) {
|
||||||
XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[color]);
|
XSetWindowBackground(dpy,
|
||||||
|
locks[screen]->win,
|
||||||
|
locks[screen]->colors[color]);
|
||||||
XClearWindow(dpy, locks[screen]->win);
|
XClearWindow(dpy, locks[screen]->win);
|
||||||
}
|
}
|
||||||
oldc = color;
|
oldc = color;
|
||||||
}
|
}
|
||||||
} else if (rr && ev.type == rrevbase + RRScreenChangeNotify) {
|
} else if (rr->active && ev.type == rr->evbase + RRScreenChangeNotify) {
|
||||||
XRRScreenChangeNotifyEvent *rre = (XRRScreenChangeNotifyEvent*)&ev;
|
rre = (XRRScreenChangeNotifyEvent*)&ev;
|
||||||
for (screen = 0; screen < nscreens; screen++) {
|
for (screen = 0; screen < nscreens; screen++) {
|
||||||
if (locks[screen]->win == rre->window) {
|
if (locks[screen]->win == rre->window) {
|
||||||
XResizeWindow(dpy, locks[screen]->win, rre->width, rre->height);
|
if (rre->rotation == RR_Rotate_90 ||
|
||||||
|
rre->rotation == RR_Rotate_270)
|
||||||
|
XResizeWindow(dpy, locks[screen]->win,
|
||||||
|
rre->height, rre->width);
|
||||||
|
else
|
||||||
|
XResizeWindow(dpy, locks[screen]->win,
|
||||||
|
rre->width, rre->height);
|
||||||
XClearWindow(dpy, locks[screen]->win);
|
XClearWindow(dpy, locks[screen]->win);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else for (screen = 0; screen < nscreens; screen++)
|
} else {
|
||||||
XRaiseWindow(dpy, locks[screen]->win);
|
for (screen = 0; screen < nscreens; screen++)
|
||||||
|
XRaiseWindow(dpy, locks[screen]->win);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static struct lock *
|
||||||
unlockscreen(Display *dpy, Lock *lock)
|
lockscreen(Display *dpy, struct xrandr *rr, int screen)
|
||||||
{
|
|
||||||
if(dpy == NULL || lock == NULL)
|
|
||||||
return;
|
|
||||||
|
|
||||||
XUngrabPointer(dpy, CurrentTime);
|
|
||||||
XFreeColors(dpy, DefaultColormap(dpy, lock->screen), lock->colors, NUMCOLS, 0);
|
|
||||||
XFreePixmap(dpy, lock->pmap);
|
|
||||||
XDestroyWindow(dpy, lock->win);
|
|
||||||
|
|
||||||
free(lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
static Lock *
|
|
||||||
lockscreen(Display *dpy, int screen)
|
|
||||||
{
|
{
|
||||||
char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||||
unsigned int len;
|
int i, ptgrab, kbgrab;
|
||||||
int i;
|
struct lock *lock;
|
||||||
Lock *lock;
|
|
||||||
XColor color, dummy;
|
XColor color, dummy;
|
||||||
XSetWindowAttributes wa;
|
XSetWindowAttributes wa;
|
||||||
Cursor invisible;
|
Cursor invisible;
|
||||||
|
|
||||||
if (!running || dpy == NULL || screen < 0 || !(lock = malloc(sizeof(Lock))))
|
if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct lock))))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
lock->screen = screen;
|
lock->screen = screen;
|
||||||
lock->root = RootWindow(dpy, lock->screen);
|
lock->root = RootWindow(dpy, lock->screen);
|
||||||
|
|
||||||
for (i = 0; i < NUMCOLS; i++) {
|
for (i = 0; i < NUMCOLS; i++) {
|
||||||
XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), colorname[i], &color, &dummy);
|
XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen),
|
||||||
|
colorname[i], &color, &dummy);
|
||||||
lock->colors[i] = color.pixel;
|
lock->colors[i] = color.pixel;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* init */
|
/* init */
|
||||||
wa.override_redirect = 1;
|
wa.override_redirect = 1;
|
||||||
wa.background_pixel = lock->colors[INIT];
|
wa.background_pixel = lock->colors[INIT];
|
||||||
lock->win = XCreateWindow(dpy, lock->root, 0, 0, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen),
|
lock->win = XCreateWindow(dpy, lock->root, 0, 0,
|
||||||
0, DefaultDepth(dpy, lock->screen), CopyFromParent,
|
DisplayWidth(dpy, lock->screen),
|
||||||
DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa);
|
DisplayHeight(dpy, lock->screen),
|
||||||
|
0, DefaultDepth(dpy, lock->screen),
|
||||||
|
CopyFromParent,
|
||||||
|
DefaultVisual(dpy, lock->screen),
|
||||||
|
CWOverrideRedirect | CWBackPixel, &wa);
|
||||||
lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
|
lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
|
||||||
invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
|
invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap,
|
||||||
|
&color, &color, 0, 0);
|
||||||
XDefineCursor(dpy, lock->win, invisible);
|
XDefineCursor(dpy, lock->win, invisible);
|
||||||
XMapRaised(dpy, lock->win);
|
|
||||||
if (rr)
|
|
||||||
XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
|
|
||||||
|
|
||||||
/* Try to grab mouse pointer *and* keyboard, else fail the lock */
|
/* Try to grab mouse pointer *and* keyboard for 600ms, else fail the lock */
|
||||||
for (len = 1000; len; len--) {
|
for (i = 0, ptgrab = kbgrab = -1; i < 6; i++) {
|
||||||
if (XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
|
if (ptgrab != GrabSuccess) {
|
||||||
GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
|
ptgrab = XGrabPointer(dpy, lock->root, False,
|
||||||
break;
|
ButtonPressMask | ButtonReleaseMask |
|
||||||
usleep(1000);
|
PointerMotionMask, GrabModeAsync,
|
||||||
}
|
GrabModeAsync, None, invisible, CurrentTime);
|
||||||
if (!len) {
|
|
||||||
fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n", screen);
|
|
||||||
} else {
|
|
||||||
for (len = 1000; len; len--) {
|
|
||||||
if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) {
|
|
||||||
/* everything fine, we grabbed both inputs */
|
|
||||||
XSelectInput(dpy, lock->root, SubstructureNotifyMask);
|
|
||||||
return lock;
|
|
||||||
}
|
|
||||||
usleep(1000);
|
|
||||||
}
|
}
|
||||||
fprintf(stderr, "slock: unable to grab keyboard for screen %d\n", screen);
|
if (kbgrab != GrabSuccess) {
|
||||||
|
kbgrab = XGrabKeyboard(dpy, lock->root, True,
|
||||||
|
GrabModeAsync, GrabModeAsync, CurrentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* input is grabbed: we can lock the screen */
|
||||||
|
if (ptgrab == GrabSuccess && kbgrab == GrabSuccess) {
|
||||||
|
XMapRaised(dpy, lock->win);
|
||||||
|
if (rr->active)
|
||||||
|
XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
|
||||||
|
|
||||||
|
XSelectInput(dpy, lock->root, SubstructureNotifyMask);
|
||||||
|
return lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* retry on AlreadyGrabbed but fail on other errors */
|
||||||
|
if ((ptgrab != AlreadyGrabbed && ptgrab != GrabSuccess) ||
|
||||||
|
(kbgrab != AlreadyGrabbed && kbgrab != GrabSuccess))
|
||||||
|
break;
|
||||||
|
|
||||||
|
usleep(100000);
|
||||||
}
|
}
|
||||||
/* grabbing one of the inputs failed */
|
|
||||||
running = 0;
|
/* we couldn't grab all input: fail out */
|
||||||
unlockscreen(dpy, lock);
|
if (ptgrab != GrabSuccess)
|
||||||
|
fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n",
|
||||||
|
screen);
|
||||||
|
if (kbgrab != GrabSuccess)
|
||||||
|
fprintf(stderr, "slock: unable to grab keyboard for screen %d\n",
|
||||||
|
screen);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "usage: slock [-v|POST_LOCK_CMD]\n");
|
die("usage: slock [-v] [cmd [arg ...]]\n");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv) {
|
main(int argc, char **argv) {
|
||||||
#ifndef HAVE_BSD_AUTH
|
struct xrandr rr;
|
||||||
const char *pws;
|
struct lock **locks;
|
||||||
#endif
|
struct passwd *pwd;
|
||||||
|
struct group *grp;
|
||||||
|
uid_t duid;
|
||||||
|
gid_t dgid;
|
||||||
|
const char *hash;
|
||||||
Display *dpy;
|
Display *dpy;
|
||||||
int screen;
|
int s, nlocks, nscreens;
|
||||||
|
|
||||||
if ((argc == 2) && !strcmp("-v", argv[1]))
|
ARGBEGIN {
|
||||||
die("slock-%s, © 2006-2016 slock engineers\n", VERSION);
|
case 'v':
|
||||||
|
fprintf(stderr, "slock-"VERSION"\n");
|
||||||
if ((argc == 2) && !strcmp("-h", argv[1]))
|
return 0;
|
||||||
|
default:
|
||||||
usage();
|
usage();
|
||||||
|
} ARGEND
|
||||||
|
|
||||||
|
/* validate drop-user and -group */
|
||||||
|
errno = 0;
|
||||||
|
if (!(pwd = getpwnam(user)))
|
||||||
|
die("slock: getpwnam %s: %s\n", user,
|
||||||
|
errno ? strerror(errno) : "user entry not found");
|
||||||
|
duid = pwd->pw_uid;
|
||||||
|
errno = 0;
|
||||||
|
if (!(grp = getgrnam(group)))
|
||||||
|
die("slock: getgrnam %s: %s\n", group,
|
||||||
|
errno ? strerror(errno) : "group entry not found");
|
||||||
|
dgid = grp->gr_gid;
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
dontkillme();
|
dontkillme();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!getpwuid(getuid()))
|
hash = gethash();
|
||||||
die("slock: no passwd entry for you\n");
|
errno = 0;
|
||||||
|
if (!crypt("", hash))
|
||||||
|
die("slock: crypt: %s\n", strerror(errno));
|
||||||
|
|
||||||
#ifndef HAVE_BSD_AUTH
|
if (!(dpy = XOpenDisplay(NULL)))
|
||||||
pws = getpw();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (!(dpy = XOpenDisplay(0)))
|
|
||||||
die("slock: cannot open display\n");
|
die("slock: cannot open display\n");
|
||||||
rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
|
|
||||||
/* Get the number of screens in display "dpy" and blank them all. */
|
/* drop privileges */
|
||||||
|
if (setgroups(0, NULL) < 0)
|
||||||
|
die("slock: setgroups: %s\n", strerror(errno));
|
||||||
|
if (setgid(dgid) < 0)
|
||||||
|
die("slock: setgid: %s\n", strerror(errno));
|
||||||
|
if (setuid(duid) < 0)
|
||||||
|
die("slock: setuid: %s\n", strerror(errno));
|
||||||
|
|
||||||
|
/* check for Xrandr support */
|
||||||
|
rr.active = XRRQueryExtension(dpy, &rr.evbase, &rr.errbase);
|
||||||
|
|
||||||
|
/* get number of screens in display "dpy" and blank them */
|
||||||
nscreens = ScreenCount(dpy);
|
nscreens = ScreenCount(dpy);
|
||||||
if (!(locks = malloc(sizeof(Lock*) * nscreens)))
|
if (!(locks = calloc(nscreens, sizeof(struct lock *))))
|
||||||
die("slock: malloc: %s\n", strerror(errno));
|
die("slock: out of memory\n");
|
||||||
int nlocks = 0;
|
for (nlocks = 0, s = 0; s < nscreens; s++) {
|
||||||
for (screen = 0; screen < nscreens; screen++) {
|
if ((locks[s] = lockscreen(dpy, &rr, s)) != NULL)
|
||||||
if ((locks[screen] = lockscreen(dpy, screen)) != NULL)
|
|
||||||
nlocks++;
|
nlocks++;
|
||||||
|
else
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
XSync(dpy, False);
|
XSync(dpy, 0);
|
||||||
|
|
||||||
/* Did we actually manage to lock something? */
|
/* did we manage to lock everything? */
|
||||||
if (nlocks == 0) { /* nothing to protect */
|
if (nlocks != nscreens)
|
||||||
free(locks);
|
|
||||||
XCloseDisplay(dpy);
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
/* run post-lock command */
|
||||||
|
if (argc > 0) {
|
||||||
|
switch (fork()) {
|
||||||
|
case -1:
|
||||||
|
die("slock: fork failed: %s\n", strerror(errno));
|
||||||
|
case 0:
|
||||||
|
if (close(ConnectionNumber(dpy)) < 0)
|
||||||
|
die("slock: close: %s\n", strerror(errno));
|
||||||
|
execvp(argv[0], argv);
|
||||||
|
fprintf(stderr, "slock: execvp %s: %s\n", argv[0], strerror(errno));
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argc >= 2 && fork() == 0) {
|
/* everything is now blank. Wait for the correct password */
|
||||||
if (dpy)
|
readpw(dpy, &rr, locks, nscreens, hash);
|
||||||
close(ConnectionNumber(dpy));
|
|
||||||
execvp(argv[1], argv+1);
|
|
||||||
die("slock: execvp %s failed: %s\n", argv[1], strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Everything is now blank. Now wait for the correct password. */
|
|
||||||
#ifdef HAVE_BSD_AUTH
|
|
||||||
readpw(dpy);
|
|
||||||
#else
|
|
||||||
readpw(dpy, pws);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Password ok, unlock everything and quit. */
|
|
||||||
for (screen = 0; screen < nscreens; screen++)
|
|
||||||
unlockscreen(dpy, locks[screen]);
|
|
||||||
|
|
||||||
free(locks);
|
|
||||||
XCloseDisplay(dpy);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user