diff options
| -rw-r--r-- | include/swaylock.h | 2 | ||||
| -rw-r--r-- | main.c | 26 | ||||
| -rw-r--r-- | meson.build | 2 | ||||
| -rw-r--r-- | pam.c | 8 | ||||
| -rw-r--r-- | pango.c | 133 | ||||
| -rw-r--r-- | shadow.c | 54 |
6 files changed, 48 insertions, 177 deletions
diff --git a/include/swaylock.h b/include/swaylock.h index 32ded18..56cee3b 100644 --- a/include/swaylock.h +++ b/include/swaylock.h @@ -112,7 +112,7 @@ void damage_state(struct swaylock_state *state); void clear_password_buffer(struct swaylock_password *pw); void schedule_indicator_clear(struct swaylock_state *state); -void initialize_pw_backend(void); +void initialize_pw_backend(int argc, char **argv); void run_pw_backend_child(void); void clear_buffer(char *buf, size_t size); @@ -882,18 +882,9 @@ static char *get_config_path(void) { SYSCONFDIR "/swaylock/config", }; - if (!getenv("XDG_CONFIG_HOME")) { - char *home = getenv("HOME"); - char *config_home = malloc(strlen(home) + strlen("/.config") + 1); - if (!config_home) { - swaylock_log(LOG_ERROR, "Unable to allocate $HOME/.config"); - } else { - strcpy(config_home, home); - strcat(config_home, "/.config"); - setenv("XDG_CONFIG_HOME", config_home, 1); - swaylock_log(LOG_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); - free(config_home); - } + char *config_home = getenv("XDG_CONFIG_HOME"); + if (!config_home || config_home[0] == '\0') { + config_paths[1] = "$HOME/.config/swaylock/config"; } wordexp_t p; @@ -936,10 +927,17 @@ static int load_config(char *path, struct swaylock_state *state, } swaylock_log(LOG_DEBUG, "Config Line #%d: %s", line_number, line); - char flag[nread + 3]; + char *flag = malloc(nread + 3); + if (flag == NULL) { + free(line); + free(config); + swaylock_log(LOG_ERROR, "Failed to allocate memory"); + return 0; + } sprintf(flag, "--%s", line); char *argv[] = {"swaylock", flag}; result = parse_options(2, argv, state, line_mode, NULL); + free(flag); if (result != 0) { break; } @@ -970,7 +968,7 @@ static void comm_in(int fd, short mask, void *data) { int main(int argc, char **argv) { swaylock_log_init(LOG_ERROR); - initialize_pw_backend(); + initialize_pw_backend(argc, argv); enum line_mode line_mode = LM_LINE; state.args = (struct swaylock_args){ diff --git a/meson.build b/meson.build index ad2681f..c937e19 100644 --- a/meson.build +++ b/meson.build @@ -15,6 +15,7 @@ add_project_arguments( '-Wno-unused-parameter', '-Wno-unused-result', '-Wundef', + '-Wvla', ], language: 'c', ) @@ -132,7 +133,6 @@ sources = [ 'log.c', 'loop.c', 'main.c', - 'pango.c', 'password.c', 'pool-buffer.c', 'render.c', @@ -11,7 +11,13 @@ static char *pw_buf = NULL; -void initialize_pw_backend(void) { +void initialize_pw_backend(int argc, char **argv) { + if (getuid() != geteuid() || getgid() != getegid()) { + swaylock_log(LOG_ERROR, + "swaylock is setuid, but was compiled with the PAM" + " backend. Run 'chmod a-s %s' to fix. Aborting.", argv[0]); + exit(EXIT_FAILURE); + } if (!spawn_comm_child()) { exit(EXIT_FAILURE); } diff --git a/pango.c b/pango.c deleted file mode 100644 index 097816c..0000000 --- a/pango.c +++ /dev/null @@ -1,133 +0,0 @@ -#include <cairo/cairo.h> -#include <pango/pangocairo.h> -#include <stdarg.h> -#include <stdbool.h> -#include <stdint.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include "cairo.h" -#include "log.h" - -static const char overflow[] = "[buffer overflow]"; -static const int max_chars = 16384; - -static char *lenient_strcat(char *dest, const char *src) { - if (dest && src) { - return strcat(dest, src); - } - return dest; -} - -size_t escape_markup_text(const char *src, char *dest) { - size_t length = 0; - if (dest) { - dest[0] = '\0'; - } - - while (src[0]) { - switch (src[0]) { - case '&': - length += 5; - lenient_strcat(dest, "&"); - break; - case '<': - length += 4; - lenient_strcat(dest, "<"); - break; - case '>': - length += 4; - lenient_strcat(dest, ">"); - break; - case '\'': - length += 6; - lenient_strcat(dest, "'"); - break; - case '"': - length += 6; - lenient_strcat(dest, """); - break; - default: - if (dest) { - dest[length] = *src; - dest[length + 1] = '\0'; - } - length += 1; - } - src++; - } - return length; -} - -PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, - const char *text, double scale, bool markup) { - PangoLayout *layout = pango_cairo_create_layout(cairo); - PangoAttrList *attrs; - if (markup) { - char *buf; - GError *error = NULL; - if (pango_parse_markup(text, -1, 0, &attrs, &buf, NULL, &error)) { - pango_layout_set_text(layout, buf, -1); - free(buf); - } else { - swaylock_log(LOG_ERROR, "pango_parse_markup '%s' -> error %s", - text, error->message); - g_error_free(error); - markup = false; // fallback to plain text - } - } - if (!markup) { - attrs = pango_attr_list_new(); - pango_layout_set_text(layout, text, -1); - } - - pango_attr_list_insert(attrs, pango_attr_scale_new(scale)); - PangoFontDescription *desc = pango_font_description_from_string(font); - pango_layout_set_font_description(layout, desc); - pango_layout_set_single_paragraph_mode(layout, 1); - pango_layout_set_attributes(layout, attrs); - pango_attr_list_unref(attrs); - pango_font_description_free(desc); - return layout; -} - -void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, - int *baseline, double scale, bool markup, const char *fmt, ...) { - char buf[max_chars]; - - va_list args; - va_start(args, fmt); - if (vsnprintf(buf, sizeof(buf), fmt, args) >= max_chars) { - strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow); - } - va_end(args); - - PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup); - pango_cairo_update_layout(cairo, layout); - pango_layout_get_pixel_size(layout, width, height); - if (baseline) { - *baseline = pango_layout_get_baseline(layout) / PANGO_SCALE; - } - g_object_unref(layout); -} - -void pango_printf(cairo_t *cairo, const char *font, - double scale, bool markup, const char *fmt, ...) { - char buf[max_chars]; - - va_list args; - va_start(args, fmt); - if (vsnprintf(buf, sizeof(buf), fmt, args) >= max_chars) { - strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow); - } - va_end(args); - - PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup); - cairo_font_options_t *fo = cairo_font_options_create(); - cairo_get_font_options(cairo, fo); - pango_cairo_context_set_font_options(pango_layout_get_context(layout), fo); - cairo_font_options_destroy(fo); - pango_cairo_update_layout(cairo, layout); - pango_cairo_show_layout(cairo, layout); - g_object_unref(layout); -} @@ -12,6 +12,32 @@ #include "log.h" #include "swaylock.h" +void initialize_pw_backend(int argc, char **argv) { + if (geteuid() != 0) { + swaylock_log(LOG_ERROR, + "swaylock needs to be setuid to read /etc/shadow"); + exit(EXIT_FAILURE); + } + + if (!spawn_comm_child()) { + exit(EXIT_FAILURE); + } + + if (setgid(getgid()) != 0) { + swaylock_log_errno(LOG_ERROR, "Unable to drop root"); + exit(EXIT_FAILURE); + } + if (setuid(getuid()) != 0) { + swaylock_log_errno(LOG_ERROR, "Unable to drop root"); + exit(EXIT_FAILURE); + } + if (setuid(0) != -1) { + swaylock_log_errno(LOG_ERROR, "Unable to drop root (we shouldn't be " + "able to restore it after setuid)"); + exit(EXIT_FAILURE); + } +} + void run_pw_backend_child(void) { /* This code runs as root */ struct passwd *pwent = getpwuid(getuid()); @@ -72,30 +98,4 @@ void run_pw_backend_child(void) { clear_buffer(encpw, strlen(encpw)); exit(EXIT_SUCCESS); -} - -void initialize_pw_backend(void) { - if (geteuid() != 0) { - swaylock_log(LOG_ERROR, - "swaylock needs to be setuid to read /etc/shadow"); - exit(EXIT_FAILURE); - } - - if (!spawn_comm_child()) { - exit(EXIT_FAILURE); - } - - if (setgid(getgid()) != 0) { - swaylock_log_errno(LOG_ERROR, "Unable to drop root"); - exit(EXIT_FAILURE); - } - if (setuid(getuid()) != 0) { - swaylock_log_errno(LOG_ERROR, "Unable to drop root"); - exit(EXIT_FAILURE); - } - if (setuid(0) != -1) { - swaylock_log_errno(LOG_ERROR, "Unable to drop root (we shouldn't be " - "able to restore it after setuid)"); - exit(EXIT_FAILURE); - } -} +}
\ No newline at end of file |
