Implement checking whether LDAP users exist
This commit is contained in:
parent
4e17e2ddd5
commit
b43122a80f
6 changed files with 69 additions and 1 deletions
|
@ -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
51
check-users.c
Normal 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
5
check-users.h
Normal file
|
@ -0,0 +1,5 @@
|
|||
#ifndef CHECK_USERS_H_INCLUDED
|
||||
#define CHECK_USERS_H_INCLUDED
|
||||
int check_users();
|
||||
void check_users_init();
|
||||
#endif
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#ifndef RUNTIME_CONFIG_INCLUDED
|
||||
#define RUNTIME_CONFIG_INCLUDED
|
||||
char *config_get_url();
|
||||
int should_enable_debug();
|
||||
#endif
|
||||
|
|
8
sanity.c
8
sanity.c
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue