nest-sanity-checks/check-users.c

52 lines
1.8 KiB
C

#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 || getenv("NEST_SANITY_CHECK_USERS_DEBUG")!=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 errno %s/%d)";
/* 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, res_errno);
int send_alarm_result;
send_alarm_result = send_alarm(notification_payload);
free(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;
}
}
}