]> andersk Git - nss_nonlocal.git/blobdiff - nonlocal-passwd.c
Switch from __nss_next API to __nss_next2 API
[nss_nonlocal.git] / nonlocal-passwd.c
index 052af95e0a498f1686255ec268ef878b8340dfee..3bc9398b4abc5d2627e83be1e92038ea2ef4d86a 100644 (file)
 
 
 #define _GNU_SOURCE
+
 #include <sys/types.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
 #include <dlfcn.h>
-#include <stdio.h>
-#include <syslog.h>
 #include <errno.h>
-#include <pwd.h>
-#include <grp.h>
 #include <nss.h>
+#include <pwd.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+#include <unistd.h>
+
 #include "nsswitch-internal.h"
 #include "nonlocal.h"
 
@@ -53,8 +54,8 @@ static service_user *__nss_passwd_nonlocal_database;
 
 static int
 internal_function
-__nss_passwd_nonlocal_lookup(service_user **ni, const char *fct_name,
-                            void **fctp)
+__nss_passwd_nonlocal_lookup2(service_user **ni, const char *fct_name,
+                             const char *fct2_name, void **fctp)
 {
     if (__nss_passwd_nonlocal_database == NULL
        && __nss_database_lookup("passwd_nonlocal", NULL, NULL,
@@ -64,6 +65,8 @@ __nss_passwd_nonlocal_lookup(service_user **ni, const char *fct_name,
     *ni = __nss_passwd_nonlocal_database;
 
     *fctp = __nss_lookup_function(*ni, fct_name);
+    if (*fctp == NULL && fct2_name != NULL)
+       *fctp = __nss_lookup_function(*ni, fct2_name);
     return 0;
 }
 
@@ -76,7 +79,7 @@ check_nonlocal_uid(const char *user, uid_t uid, int *errnop)
     char *buf;
     size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
     const struct walk_nss w = {
-       .lookup = &__nss_passwd_lookup, .fct_name = "getpwuid_r",
+       .lookup2 = &__nss_passwd_lookup2, .fct_name = "getpwuid_r",
        .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen
     };
     const __typeof__(&_nss_nonlocal_getpwuid_r) self = &_nss_nonlocal_getpwuid_r;
@@ -125,7 +128,7 @@ check_nonlocal_user(const char *user, int *errnop)
     char *buf;
     size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
     const struct walk_nss w = {
-       .lookup = __nss_passwd_lookup, .fct_name = "getpwnam_r",
+       .lookup2 = __nss_passwd_lookup2, .fct_name = "getpwnam_r",
        .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen
     };
     const __typeof__(&_nss_nonlocal_getpwnam_r) self = &_nss_nonlocal_getpwnam_r;
@@ -143,7 +146,25 @@ check_nonlocal_user(const char *user, int *errnop)
     return status;
 }
 
+enum nss_status
+get_nonlocal_passwd(const char *name, struct passwd *pwd, char **buffer,
+                   int *errnop)
+{
+    enum nss_status status;
+    size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+    const struct walk_nss w = {
+       .lookup2 = __nss_passwd_nonlocal_lookup2, .fct_name = "getpwnam_r",
+       .status = &status, .errnop = errnop, .buf = buffer, .buflen = &buflen
+    };
+    const __typeof__(&_nss_nonlocal_getpwnam_r) self = NULL;
+#define args (name, pwd, *buffer, buflen, errnop)
+#include "walk_nss.h"
+#undef args
+    return status;
+}
+
 
+static bool pwent_initialized = false;
 static service_user *pwent_startp, *pwent_nip;
 static void *pwent_fct_start;
 static union {
@@ -158,7 +179,7 @@ _nss_nonlocal_setpwent(int stayopen)
 {
     enum nss_status status;
     const struct walk_nss w = {
-       .lookup = &__nss_passwd_nonlocal_lookup, .fct_name = "setpwent",
+       .lookup2 = &__nss_passwd_nonlocal_lookup2, .fct_name = "setpwent",
        .status = &status
     };
     const __typeof__(&_nss_nonlocal_setpwent) self = NULL;
@@ -168,9 +189,12 @@ _nss_nonlocal_setpwent(int stayopen)
     if (status != NSS_STATUS_SUCCESS)
        return status;
 
-    if (pwent_fct_start == NULL)
-       __nss_passwd_nonlocal_lookup(&pwent_startp, pwent_fct_name,
-                                    &pwent_fct_start);
+    if (!pwent_initialized) {
+       __nss_passwd_nonlocal_lookup2(&pwent_startp, pwent_fct_name, NULL,
+                                     &pwent_fct_start);
+       __sync_synchronize();
+       pwent_initialized = true;
+    }
     pwent_nip = pwent_startp;
     pwent_fct.ptr = pwent_fct_start;
     return NSS_STATUS_SUCCESS;
@@ -181,8 +205,8 @@ _nss_nonlocal_endpwent(void)
 {
     enum nss_status status;
     const struct walk_nss w = {
-       .lookup = &__nss_passwd_nonlocal_lookup, .fct_name = "endpwent",
-       .status = &status
+       .lookup2 = &__nss_passwd_nonlocal_lookup2, .fct_name = "endpwent",
+       .status = &status, .all_values = 1,
     };
     const __typeof__(&_nss_nonlocal_endpwent) self = NULL;
 
@@ -224,7 +248,8 @@ _nss_nonlocal_getpwent_r(struct passwd *pwd, char *buffer, size_t buflen,
 
        if (status == NSS_STATUS_SUCCESS)
            return NSS_STATUS_SUCCESS;
-    } while (__nss_next(&pwent_nip, pwent_fct_name, &pwent_fct.ptr, status, 0) == 0);
+    } while (__nss_next2(&pwent_nip, pwent_fct_name, 0, &pwent_fct.ptr, status,
+                         0) == 0);
 
     pwent_nip = NULL;
     return NSS_STATUS_NOTFOUND;
@@ -238,7 +263,7 @@ _nss_nonlocal_getpwnam_r(const char *name, struct passwd *pwd,
     enum nss_status status;
     int group_errno;
     const struct walk_nss w = {
-       .lookup = __nss_passwd_nonlocal_lookup, .fct_name = "getpwnam_r",
+       .lookup2 = __nss_passwd_nonlocal_lookup2, .fct_name = "getpwnam_r",
        .status = &status, .errnop = errnop
     };
     const __typeof__(&_nss_nonlocal_getpwnam_r) self = NULL;
@@ -262,7 +287,7 @@ _nss_nonlocal_getpwnam_r(const char *name, struct passwd *pwd,
     if (status != NSS_STATUS_SUCCESS)
        return status;
 
-    if (check_nonlocal_gid(name, pwd->pw_gid, &group_errno) !=
+    if (check_nonlocal_gid(name, NULL, pwd->pw_gid, &group_errno) !=
        NSS_STATUS_SUCCESS)
        pwd->pw_gid = 65534 /* nogroup */;
     return NSS_STATUS_SUCCESS;
@@ -275,7 +300,7 @@ _nss_nonlocal_getpwuid_r(uid_t uid, struct passwd *pwd,
     enum nss_status status;
     int group_errno;
     const struct walk_nss w = {
-       .lookup = &__nss_passwd_nonlocal_lookup, .fct_name = "getpwuid_r",
+       .lookup2 = &__nss_passwd_nonlocal_lookup2, .fct_name = "getpwuid_r",
        .status = &status, .errnop = errnop
     };
     const __typeof__(&_nss_nonlocal_getpwuid_r) self = NULL;
@@ -299,7 +324,7 @@ _nss_nonlocal_getpwuid_r(uid_t uid, struct passwd *pwd,
     if (status != NSS_STATUS_SUCCESS)
        return status;
 
-    if (check_nonlocal_gid(pwd->pw_name, pwd->pw_gid, &group_errno) !=
+    if (check_nonlocal_gid(pwd->pw_name, NULL, pwd->pw_gid, &group_errno) !=
        NSS_STATUS_SUCCESS)
        pwd->pw_gid = 65534 /* nogroup */;
     return NSS_STATUS_SUCCESS;
This page took 0.233577 seconds and 4 git commands to generate.