Commit 526e8cff authored by Anders Blomdell's avatar Anders Blomdell
Browse files

commiting conditional_ldap.c

parents
#include <nss.h>
#include <pwd.h>
#include <shadow.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dlfcn.h>
static struct {
time_t mtime;
void *handle;
enum nss_status (*setpwent)(int i);
enum nss_status (*endpwent)(void);
enum nss_status (*getpwnam_r)(const char *name,
struct passwd *pwd,
char *buffer,
size_t buflen,
int *errnop);
enum nss_status (*getpwuid_r)(uid_t uid,
struct passwd *pwd,
char *buffer,
size_t buflen,
int *errnop);
enum nss_status (*getpwent_r)(struct passwd *result,
char *buffer,
size_t buflen,
int *errnop);
enum nss_status (*setspent)(int i);
enum nss_status (*endspent)(void);
enum nss_status (*getspent_r)(struct spwd *pwd,
char *buffer,
size_t buflen,
int *errnop);
enum nss_status (*getspnam_r)(const char *name,
struct spwd *pwd,
char *buffer,
size_t buflen,
int *errnop);
} ldap;
__attribute__((constructor)) static void a()
{
// printf("%s:%d %s\n", __FUNCTION__, __LINE__, __TIME__);
}
__attribute__((destructor)) void b()
{
// printf("%s:%d %s\n", __FUNCTION__, __LINE__, __TIME__);
if (ldap.handle) {
dlclose(ldap.handle);
}
}
static int active()
{
int result = 0;
struct stat st;
if (stat("/var/run/conditional_ldap", &st) == 0) {
if (st.st_mtime != ldap.mtime) {
// printf("rebind %ld\n", st.st_mtime);
if (ldap.handle) {
dlclose(ldap.handle);
}
}
ldap.handle = dlopen("libnss_ldap.so", RTLD_LAZY);
if (!ldap.handle) {
fprintf(stderr, "Failed to load ldap %s\n", dlerror());
} else {
#define LOADSYM(SYM) ldap.SYM = dlsym(ldap.handle, "_nss_ldap_" # SYM)
LOADSYM(setpwent);
LOADSYM(endpwent);
LOADSYM(getpwnam_r);
LOADSYM(getpwuid_r);
LOADSYM(getpwent_r);
LOADSYM(setspent);
LOADSYM(endspent);
LOADSYM(getspent_r);
LOADSYM(getspnam_r);
#undef LOADSYM
result = 1;
}
}
return result;
}
enum nss_status _nss_conditional_ldap_setpwent(int i)
{
enum nss_status result = NSS_STATUS_UNAVAIL;
// printf("%s:%d %s\n", __FUNCTION__, __LINE__, __TIME__);
if (active() && ldap.setpwent) {
result = ldap.setpwent(i);
}
return result;
}
enum nss_status _nss_conditional_ldap_endpwent(void)
{
enum nss_status result = NSS_STATUS_UNAVAIL;
// printf("%s:%d %s\n", __FUNCTION__, __LINE__, __TIME__);
if (active() && ldap.endpwent) {
result = ldap.endpwent();
}
return result;
}
enum nss_status _nss_conditional_ldap_getpwnam_r(const char *name,
struct passwd *pwd,
char *buffer,
size_t buflen,
int *errnop)
{
enum nss_status result = NSS_STATUS_UNAVAIL;
// printf("%s:%d %s\n", __FUNCTION__, __LINE__, __TIME__);
if (active() && ldap.getpwnam_r) {
result = ldap.getpwnam_r(name, pwd, buffer, buflen, errnop);
}
return result;
}
enum nss_status _nss_conditional_ldap_getpwuid_r(uid_t uid,
struct passwd *pwd,
char *buffer,
size_t buflen,
int *errnop)
{
enum nss_status result = NSS_STATUS_UNAVAIL;
// printf("%s:%d %s\n", __FUNCTION__, __LINE__, __TIME__);
if (active() && ldap.getpwuid_r) {
result = ldap.getpwuid_r(uid, pwd, buffer, buflen, errnop);
}
return result;
}
enum nss_status _nss_conditional_ldap_getpwent_r(struct passwd *pwd,
char *buffer,
size_t buflen,
int *errnop)
{
enum nss_status result = NSS_STATUS_UNAVAIL;
// printf("%s:%d %s\n", __FUNCTION__, __LINE__, __TIME__);
if (active() && ldap.getpwent_r) {
result = ldap.getpwent_r(pwd, buffer, buflen, errnop);
}
return result;
}
enum nss_status _nss_conditional_ldap_setspent(int i)
{
enum nss_status result = NSS_STATUS_UNAVAIL;
// printf("%s:%d %s\n", __FUNCTION__, __LINE__, __TIME__);
if (active() && ldap.setspent) {
result = ldap.setspent(i);
}
return result;
}
enum nss_status _nss_conditional_ldap_endspent(void)
{
enum nss_status result = NSS_STATUS_UNAVAIL;
// printf("%s:%d %s\n", __FUNCTION__, __LINE__, __TIME__);
if (active() && ldap.endspent) {
result = ldap.endspent();
}
return result;
}
enum nss_status _nss_conditional_ldap_getspent_r(struct spwd *pwd,
char *buffer,
size_t buflen,
int *errnop)
{
enum nss_status result = NSS_STATUS_UNAVAIL;
// printf("%s:%d %s\n", __FUNCTION__, __LINE__, __TIME__);
if (active() && ldap.getspent_r) {
result = ldap.getspent_r(pwd, buffer, buflen, errnop);
}
return result;
}
enum nss_status _nss_conditional_ldap_getspnam_r(const char *name,
struct spwd *pwd,
char *buffer,
size_t buflen,
int *errnop)
{
enum nss_status result = NSS_STATUS_UNAVAIL;
// printf("%s:%d %s\n", __FUNCTION__, __LINE__, __TIME__);
if (active() && ldap.getspnam_r) {
result = ldap.getspnam_r(name, pwd, buffer, buflen, errnop);
}
return result;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment