From b43122a80fdb4781628eb0b49659d79f3743f1bd Mon Sep 17 00:00:00 2001 From: 9pfs <9pfs@amcforum.wiki> Date: Wed, 24 Apr 2024 02:01:26 +0000 Subject: [PATCH] Implement checking whether LDAP users exist --- Makefile.am | 2 +- check-users.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ check-users.h | 5 +++++ runtime-config.c | 3 +++ runtime-config.h | 1 + sanity.c | 8 ++++++++ 6 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 check-users.c create mode 100644 check-users.h diff --git a/Makefile.am b/Makefile.am index 5672975..3dfda90 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,7 @@ bin_PROGRAMS = nest-sanity noinst_PROGRAMS = notify-test nest_sanity_CFLAGS = $(LIBCURL_CFLAGS) nest_sanity_LDADD = $(LIBCURL_LIBS) -nest_sanity_SOURCES = sanity.c notify.c runtime-config.c +nest_sanity_SOURCES = sanity.c notify.c runtime-config.c check-users.c notify_test_CFLAGS = $(LIBCURL_CFLAGS) notify_test_LDADD = $(LIBCURL_LIBS) notify_test_SOURCES = notify.c runtime-config.c notify-test.c diff --git a/check-users.c b/check-users.c new file mode 100644 index 0000000..2dfeed7 --- /dev/null +++ b/check-users.c @@ -0,0 +1,51 @@ +#include +#include +#include "notify.h" +#include +#include +#include +#include +#include "runtime-config.h" +/* If samuel truly doesn't exist, + * there's probably a bigger issue than just an LDAP failure. + */ +#define CHECK_USERS_CANARY_USER "samuel" +int check_users_state; +void check_users_init() { + check_users_state = 0; +} +void check_users() { + if(should_enable_debug()) { + printf("[D] Starting check_users\n"); + } + struct passwd *res; + errno = 0; + res = getpwnam(CHECK_USERS_CANARY_USER); + if(res == NULL) { + /* If we've already reported it, let's not report it again */ + if(check_users_state == 1) return; + int res_errno; + res_errno = errno; + char *res_err_txt; + res_err_txt = strerror(res_errno); + char *notification_payload; + int notification_payload_len; + const char *notification_payload_fmt = "samuel doesn't exist, LDAP is probably broken somehow. (getpwnam returned %s)"; + /* TODO: set a realistic payload length */ + notification_payload_len = strlen(notification_payload_fmt) + strlen(res_err_txt) + 1; + notification_payload = malloc(notification_payload_len+1); + snprintf(notification_payload, notification_payload_len, notification_payload_fmt, res_err_txt); + int send_alarm_result; + send_alarm_result = send_alarm(notification_payload); + /* We only set this to 1 if we successfully reported the issue. */ + if(send_alarm_result == 0) check_users_state = 1; + } + else { + if(check_users_state > 0) { + int send_alarm_result; + send_alarm_result = send_alarm("samuel exists again, everything is probably okay now"); + /* Keep notifying until it sends successfully. */ + if(send_alarm_result == 0) check_users_state = 0; + } + } +} diff --git a/check-users.h b/check-users.h new file mode 100644 index 0000000..8163dea --- /dev/null +++ b/check-users.h @@ -0,0 +1,5 @@ +#ifndef CHECK_USERS_H_INCLUDED +#define CHECK_USERS_H_INCLUDED +int check_users(); +void check_users_init(); +#endif diff --git a/runtime-config.c b/runtime-config.c index 6bdd60d..ba4d5a6 100644 --- a/runtime-config.c +++ b/runtime-config.c @@ -5,3 +5,6 @@ char *config_get_url() { /* TODO: find a better way to do this */ return getenv("NOTIFY_URL"); } +int should_enable_debug() { + return getenv("NEST_SANITY_DEBUG")!=NULL?1:0; +} diff --git a/runtime-config.h b/runtime-config.h index a0a8504..cc9dd7d 100644 --- a/runtime-config.h +++ b/runtime-config.h @@ -1,4 +1,5 @@ #ifndef RUNTIME_CONFIG_INCLUDED #define RUNTIME_CONFIG_INCLUDED char *config_get_url(); +int should_enable_debug(); #endif diff --git a/sanity.c b/sanity.c index df3080d..4cac868 100644 --- a/sanity.c +++ b/sanity.c @@ -1,7 +1,15 @@ #include +#include +#include #include "runtime-config.h" #include "notify.h" +#include "check-users.h" int main() { notify_init_early(); notify_init(); + check_users_init(); + while(true) { + check_users(); + sleep(30); + } }