Implement checking whether LDAP users exist

This commit is contained in:
9pfs 2024-04-24 02:01:26 +00:00
parent 4e17e2ddd5
commit b43122a80f
Signed by: 9pfs
SSH key fingerprint: SHA256:TOcGxMQCfy4VvRM8AzgXErKXdkAtaTcpGXgYMpyoJoY
6 changed files with 69 additions and 1 deletions

View file

@ -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

51
check-users.c Normal file
View file

@ -0,0 +1,51 @@
#include <sys/types.h>
#include <pwd.h>
#include "notify.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#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;
}
}
}

5
check-users.h Normal file
View file

@ -0,0 +1,5 @@
#ifndef CHECK_USERS_H_INCLUDED
#define CHECK_USERS_H_INCLUDED
int check_users();
void check_users_init();
#endif

View file

@ -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;
}

View file

@ -1,4 +1,5 @@
#ifndef RUNTIME_CONFIG_INCLUDED
#define RUNTIME_CONFIG_INCLUDED
char *config_get_url();
int should_enable_debug();
#endif

View file

@ -1,7 +1,15 @@
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#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);
}
}