]> andersk Git - nss_nonlocal.git/blobdiff - nonlocal-passwd.c
Guard one-time initialization with memory barriers
[nss_nonlocal.git] / nonlocal-passwd.c
index 052af95e0a498f1686255ec268ef878b8340dfee..f8e7f96f823ded1760977b01a28025ea8b851f43 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"
 
@@ -143,7 +144,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 = {
+       .lookup = __nss_passwd_nonlocal_lookup, .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 {
@@ -168,9 +187,12 @@ _nss_nonlocal_setpwent(int stayopen)
     if (status != NSS_STATUS_SUCCESS)
        return status;
 
-    if (pwent_fct_start == NULL)
+    if (!pwent_initialized) {
        __nss_passwd_nonlocal_lookup(&pwent_startp, pwent_fct_name,
                                     &pwent_fct_start);
+       __sync_synchronize();
+       pwent_initialized = true;
+    }
     pwent_nip = pwent_startp;
     pwent_fct.ptr = pwent_fct_start;
     return NSS_STATUS_SUCCESS;
@@ -262,7 +284,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;
@@ -299,7 +321,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.345885 seconds and 4 git commands to generate.