update suckless

This commit is contained in:
2026-10-25 04:40:51 +01:00
parent 05c1337f0b
commit e9e8ad1b67
19 changed files with 1339 additions and 52 deletions
+164 -11
View File
@@ -6,7 +6,9 @@
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <limits.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdlib.h>
@@ -16,20 +18,29 @@
#include <spawn.h>
#include <sys/types.h>
#include <X11/extensions/Xrandr.h>
#include <X11/extensions/dpms.h>
#include <X11/keysym.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XF86keysym.h>
#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <Imlib2.h>
#include "arg.h"
#include "util.h"
char *argv0;
static int pam_conv(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr);
struct pam_conv pamc = {pam_conv, NULL};
char passwd[256];
enum {
INIT,
INPUT,
FAILED,
PAM,
NUMCOLS
};
@@ -37,6 +48,7 @@ struct lock {
int screen;
Window root, win;
Pixmap pmap;
Pixmap bgmap;
unsigned long colors[NUMCOLS];
};
@@ -48,6 +60,8 @@ struct xrandr {
#include "config.h"
Imlib_Image image;
static void
die(const char *errstr, ...)
{
@@ -59,8 +73,32 @@ die(const char *errstr, ...)
exit(1);
}
static int
pam_conv(int num_msg, const struct pam_message **msg,
struct pam_response **resp, void *appdata_ptr)
{
int retval = PAM_CONV_ERR;
for(int i=0; i<num_msg; i++) {
if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF &&
strncmp(msg[i]->msg, "Password: ", 10) == 0) {
struct pam_response *resp_msg = malloc(sizeof(struct pam_response));
if (!resp_msg)
die("malloc failed\n");
char *password = malloc(strlen(passwd) + 1);
if (!password)
die("malloc failed\n");
memset(password, 0, strlen(passwd) + 1);
strcpy(password, passwd);
resp_msg->resp_retcode = 0;
resp_msg->resp = password;
resp[i] = resp_msg;
retval = PAM_SUCCESS;
}
}
return retval;
}
#ifdef __linux__
#include <fcntl.h>
#include <linux/oom.h>
static void
@@ -123,6 +161,8 @@ gethash(void)
}
#endif /* HAVE_SHADOW_H */
/* pam, store user name */
hash = pw->pw_name;
return hash;
}
@@ -131,11 +171,12 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
const char *hash)
{
XRRScreenChangeNotifyEvent *rre;
char buf[32], passwd[256], *inputhash;
int num, screen, running, failure, oldc;
char buf[32];
int num, screen, running, failure, oldc, retval;
unsigned int len, color;
KeySym ksym;
XEvent ev;
pam_handle_t *pamh;
len = 0;
running = 1;
@@ -170,10 +211,26 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
case XK_Return:
passwd[len] = '\0';
errno = 0;
if (!(inputhash = crypt(passwd, hash)))
fprintf(stderr, "slock: crypt: %s\n", strerror(errno));
retval = pam_start(pam_service, hash, &pamc, &pamh);
// color = PAM;
// for (screen = 0; screen < nscreens; screen++) {
// XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[color]);
// XClearWindow(dpy, locks[screen]->win);
// XRaiseWindow(dpy, locks[screen]->win);
// }
// XSync(dpy, False);
if (retval == PAM_SUCCESS)
retval = pam_authenticate(pamh, 0);
if (retval == PAM_SUCCESS)
retval = pam_acct_mgmt(pamh, 0);
running = 1;
if (retval == PAM_SUCCESS)
running = 0;
else
running = !!strcmp(inputhash, hash);
fprintf(stderr, "slock: %s\n", pam_strerror(pamh, retval));
pam_end(pamh, retval);
if (running) {
XBell(dpy, 100);
failure = 1;
@@ -203,9 +260,10 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
if (running && oldc != color) {
for (screen = 0; screen < nscreens; screen++) {
XSetWindowBackground(dpy,
locks[screen]->win,
locks[screen]->colors[color]);
if (locks[screen]->bgmap)
XSetWindowBackgroundPixmap(dpy, locks[screen]->win, locks[screen]->bgmap);
else
XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[0]);
XClearWindow(dpy, locks[screen]->win);
}
oldc = color;
@@ -248,6 +306,17 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
lock->screen = screen;
lock->root = RootWindow(dpy, lock->screen);
if(image)
{
lock->bgmap = XCreatePixmap(dpy, lock->root, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen), DefaultDepth(dpy, lock->screen));
imlib_context_set_display(dpy);
imlib_context_set_visual(DefaultVisual(dpy, lock->screen));
imlib_context_set_colormap(DefaultColormap(dpy, lock->screen));
imlib_context_set_drawable(lock->bgmap);
imlib_render_image_on_drawable(0, 0);
imlib_free_image();
}
for (i = 0; i < NUMCOLS; i++) {
XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen),
colorname[i], &color, &dummy);
@@ -264,6 +333,8 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
CopyFromParent,
DefaultVisual(dpy, lock->screen),
CWOverrideRedirect | CWBackPixel, &wa);
if(lock->bgmap)
XSetWindowBackgroundPixmap(dpy, lock->win, lock->bgmap);
lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap,
&color, &color, 0, 0);
@@ -310,6 +381,28 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
return NULL;
}
static int
get_xss_sleep_lock_fd(void)
{
char *env_str, *end;
long env_val;
env_str = getenv("XSS_SLEEP_LOCK_FD");
if (!env_str || !*env_str)
return -1;
errno = 0;
env_val = strtol(env_str, &end, 10);
if (errno ||
*end ||
(env_val < 0 || env_val > INT_MAX)) {
fprintf(stderr, "slock: strtol $XSS_SLEEP_LOCK_FD: %s\n",
errno ? strerror(errno) : "invalid value");
return -1;
}
return (int)env_val;
}
static void
usage(void)
{
@@ -327,6 +420,8 @@ main(int argc, char **argv) {
const char *hash;
Display *dpy;
int s, nlocks, nscreens;
CARD16 standby, suspend, off;
int lockfd;
ARGBEGIN {
case 'v':
@@ -336,6 +431,18 @@ main(int argc, char **argv) {
usage();
} ARGEND
lockfd = get_xss_sleep_lock_fd();
if (lockfd >= 0) {
int flags = fcntl(lockfd, F_GETFD);
if (flags < 0) {
fprintf(stderr, "slock: fcntl $XSS_SLEEP_LOCK_FD F_GETFD: %s\n",
strerror(errno));
} else if (fcntl(lockfd, F_SETFD, flags | FD_CLOEXEC) == -1) {
fprintf(stderr, "slock: fcntl $XSS_SLEEP_LOCK_FD F_SETFD: %s\n",
strerror(errno));
}
}
/* validate drop-user and -group */
errno = 0;
if (!(pwd = getpwnam(user)))
@@ -352,10 +459,9 @@ main(int argc, char **argv) {
dontkillme();
#endif
/* the contents of hash are used to transport the current user name */
hash = gethash();
errno = 0;
if (!crypt("", hash))
die("slock: crypt: %s\n", strerror(errno));
if (!(dpy = XOpenDisplay(NULL)))
die("slock: cannot open display\n");
@@ -368,6 +474,32 @@ main(int argc, char **argv) {
if (setuid(duid) < 0)
die("slock: setuid: %s\n", strerror(errno));
/* Load picture */
Imlib_Image buffer = imlib_load_image(background_image);
imlib_context_set_image(buffer);
int background_image_width = imlib_image_get_width();
int background_image_height = imlib_image_get_height();
/* Create an image to be rendered */
Screen *scr = ScreenOfDisplay(dpy, DefaultScreen(dpy));
image = imlib_create_image(scr->width, scr->height);
imlib_context_set_image(image);
/* Fill the image for every X monitor */
XRRMonitorInfo *monitors;
int number_of_monitors;
monitors = XRRGetMonitors(dpy, RootWindow(dpy, XScreenNumberOfScreen(scr)), True, &number_of_monitors);
int i;
for (i = 0; i < number_of_monitors; i++) {
imlib_blend_image_onto_image(buffer, 0, 0, 0, background_image_width, background_image_height, monitors[i].x, monitors[i].y, monitors[i].width, monitors[i].height);
}
/* Clean up */
imlib_context_set_image(buffer);
imlib_free_image();
imlib_context_set_image(image);
/* check for Xrandr support */
rr.active = XRRQueryExtension(dpy, &rr.evbase, &rr.errbase);
@@ -387,6 +519,23 @@ main(int argc, char **argv) {
if (nlocks != nscreens)
return 1;
if (lockfd >= 0 && close(lockfd) < 0)
fprintf(stderr, "slock: close $XSS_SLEEP_LOCK_FD: %s\n", strerror(errno));
/* DPMS magic to disable the monitor */
if (!DPMSCapable(dpy))
die("slock: DPMSCapable failed\n");
if (!DPMSEnable(dpy))
die("slock: DPMSEnable failed\n");
if (!DPMSGetTimeouts(dpy, &standby, &suspend, &off))
die("slock: DPMSGetTimeouts failed\n");
if (!standby || !suspend || !off)
die("slock: at least one DPMS variable is zero\n");
if (!DPMSSetTimeouts(dpy, monitortime, monitortime, monitortime))
die("slock: DPMSSetTimeouts failed\n");
XSync(dpy, 0);
/* run post-lock command */
if (argc > 0) {
pid_t pid;
@@ -401,5 +550,9 @@ main(int argc, char **argv) {
/* everything is now blank. Wait for the correct password */
readpw(dpy, &rr, locks, nscreens, hash);
/* reset DPMS values to inital ones */
DPMSSetTimeouts(dpy, standby, suspend, off);
XSync(dpy, 0);
return 0;
}