aboutsummaryrefslogtreecommitdiff
path: root/nss
diff options
context:
space:
mode:
authorArjun Shankar <arjun@redhat.com>2023-10-02 14:55:17 +0200
committerArjun Shankar <arjun@redhat.com>2023-10-24 12:30:59 +0200
commit0ac35d181edd38563f95c3b38e74476bfbff76d3 (patch)
tree7f0cdb954a8d458a895d3aa138882b79397381d6 /nss
parent98b107e30848198e7128f80b38b406585f0317d6 (diff)
downloadglibc-0ac35d181edd38563f95c3b38e74476bfbff76d3.tar.xz
glibc-0ac35d181edd38563f95c3b38e74476bfbff76d3.zip
Remove 'pwd' and merge into 'nss'
The majority of pwd routines are entry points for nss functionality. This commit removes the 'pwd' subdirectory and moves all functionality and tests to 'nss'. References to pwd/ are accordingly changed. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'nss')
-rw-r--r--nss/Makefile24
-rw-r--r--nss/Versions8
-rw-r--r--nss/fgetpwent.c87
-rw-r--r--nss/fgetpwent_r.c79
-rw-r--r--nss/getpw.c62
-rw-r--r--nss/getpwent.c28
-rw-r--r--nss/getpwent_r.c28
-rw-r--r--nss/getpwnam.c28
-rw-r--r--nss/getpwnam_r.c28
-rw-r--r--nss/getpwuid.c28
-rw-r--r--nss/getpwuid_r.c28
-rw-r--r--nss/putpwent.c65
-rw-r--r--nss/pwd.h193
-rw-r--r--nss/tst-getpw.c114
-rw-r--r--nss/tst-putpwent.c168
15 files changed, 968 insertions, 0 deletions
diff --git a/nss/Makefile b/nss/Makefile
index 5256b90f8b..28648ea884 100644
--- a/nss/Makefile
+++ b/nss/Makefile
@@ -26,6 +26,7 @@ headers := \
grp.h \
gshadow.h \
nss.h \
+ pwd.h \
# headers
# This is the trivial part which goes into libc itself.
@@ -103,6 +104,27 @@ CFLAGS-getsgnam.c += -fexceptions
CFLAGS-getsgnam_r.c += -fexceptions
endif
+# pwd routines:
+routines += \
+ fgetpwent \
+ fgetpwent_r \
+ getpw \
+ getpwent \
+ getpwent_r \
+ getpwnam \
+ getpwnam_r \
+ getpwuid \
+ getpwuid_r \
+ putpwent \
+ # routines
+
+ifeq ($(have-thread-library),yes)
+CFLAGS-fgetpwent_r.c += $(libio-mtsafe)
+CFLAGS-getpw.c += -fexceptions
+CFLAGS-getpwent.c += -fexceptions
+CFLAGS-getpwent_r.c += -fexceptions
+endif
+
# These are the databases that go through nss dispatch.
# Caution: if you add a database here, you must add its real name
# in databases.def, too.
@@ -143,6 +165,7 @@ tests := \
test-netdb \
testgrp \
tst-fgetsgent_r \
+ tst-getpw \
tst-gshadow \
tst-nss-getpwent \
tst-nss-hash \
@@ -152,6 +175,7 @@ tests := \
tst-nss-test5 \
tst-nss-test_errno \
tst-putgrent \
+ tst-putpwent \
tst-putsgent \
tst-sgetsgent \
# tests
diff --git a/nss/Versions b/nss/Versions
index 6204ac0af1..58ca73c9df 100644
--- a/nss/Versions
+++ b/nss/Versions
@@ -8,19 +8,26 @@ libc {
# e*
endgrent;
+ endpwent;
# f*
fgetgrent; fgetgrent_r;
+ fgetpwent; fgetpwent_r;
# g*
getgrent; getgrent_r; getgrgid; getgrgid_r; getgrnam; getgrnam_r;
getgroups;
+ getpw; getpwent; getpwent_r; getpwnam; getpwnam_r; getpwuid; getpwuid_r;
# i*
initgroups;
+ # p*
+ putpwent;
+
# s*
setgrent;
+ setpwent;
}
GLIBC_2.1 {
# p*
@@ -29,6 +36,7 @@ libc {
GLIBC_2.1.2 {
# g*
getgrent_r; getgrgid_r; getgrnam_r;
+ getpwent_r; getpwuid_r; getpwnam_r;
}
GLIBC_2.2.2 {
__nss_hostname_digits_dots;
diff --git a/nss/fgetpwent.c b/nss/fgetpwent.c
new file mode 100644
index 0000000000..b7864d263a
--- /dev/null
+++ b/nss/fgetpwent.c
@@ -0,0 +1,87 @@
+/* Copyright (C) 1991-2023 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <errno.h>
+#include <libc-lock.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <set-freeres.h>
+
+
+/* We need to protect the dynamic buffer handling. */
+__libc_lock_define_initialized (static, lock);
+
+static char *buffer;
+
+/* Read one entry from the given stream. */
+struct passwd *
+fgetpwent (FILE *stream)
+{
+ static size_t buffer_size;
+ static struct passwd resbuf;
+ fpos_t pos;
+ struct passwd *result;
+ int save;
+
+ if (fgetpos (stream, &pos) != 0)
+ return NULL;
+
+ /* Get lock. */
+ __libc_lock_lock (lock);
+
+ /* Allocate buffer if not yet available. */
+ if (buffer == NULL)
+ {
+ buffer_size = NSS_BUFLEN_PASSWD;
+ buffer = malloc (buffer_size);
+ }
+
+ while (buffer != NULL
+ && (__fgetpwent_r (stream, &resbuf, buffer, buffer_size, &result)
+ == ERANGE))
+ {
+ char *new_buf;
+ buffer_size += NSS_BUFLEN_PASSWD;
+ new_buf = realloc (buffer, buffer_size);
+ if (new_buf == NULL)
+ {
+ /* We are out of memory. Free the current buffer so that the
+ process gets a chance for a normal termination. */
+ save = errno;
+ free (buffer);
+ __set_errno (save);
+ }
+ buffer = new_buf;
+
+ /* Reset the stream. */
+ if (fsetpos (stream, &pos) != 0)
+ buffer = NULL;
+ }
+
+ if (buffer == NULL)
+ result = NULL;
+
+ /* Release lock. Preserve error value. */
+ save = errno;
+ __libc_lock_unlock (lock);
+ __set_errno (save);
+
+ return result;
+}
+
+weak_alias (buffer, __libc_fgetpwent_freemem_ptr)
diff --git a/nss/fgetpwent_r.c b/nss/fgetpwent_r.c
new file mode 100644
index 0000000000..5427447a36
--- /dev/null
+++ b/nss/fgetpwent_r.c
@@ -0,0 +1,79 @@
+/* Copyright (C) 1991-2023 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <pwd.h>
+
+/* Define a line parsing function using the common code
+ used in the nss_files module. */
+
+#define STRUCTURE passwd
+#define ENTNAME pwent
+struct pwent_data {};
+
+#include <nss/nss_files/files-parse.c>
+LINE_PARSER
+(,
+ STRING_FIELD (result->pw_name, ISCOLON, 0);
+ if (line[0] == '\0'
+ && (result->pw_name[0] == '+' || result->pw_name[0] == '-'))
+ {
+ /* This a special case. We allow lines containing only a `+' sign
+ since this is used for nss_compat. All other services will
+ reject this entry later. Initialize all other fields now. */
+ result->pw_passwd = NULL;
+ result->pw_uid = 0;
+ result->pw_gid = 0;
+ result->pw_gecos = NULL;
+ result->pw_dir = NULL;
+ result->pw_shell = NULL;
+ }
+ else
+ {
+ STRING_FIELD (result->pw_passwd, ISCOLON, 0);
+ if (result->pw_name[0] == '+' || result->pw_name[0] == '-')
+ {
+ INT_FIELD_MAYBE_NULL (result->pw_uid, ISCOLON, 0, 10, , 0)
+ INT_FIELD_MAYBE_NULL (result->pw_gid, ISCOLON, 0, 10, , 0)
+ }
+ else
+ {
+ INT_FIELD (result->pw_uid, ISCOLON, 0, 10,)
+ INT_FIELD (result->pw_gid, ISCOLON, 0, 10,)
+ }
+ STRING_FIELD (result->pw_gecos, ISCOLON, 0);
+ STRING_FIELD (result->pw_dir, ISCOLON, 0);
+ result->pw_shell = line;
+ }
+ )
+
+
+/* Read one entry from the given stream. */
+int
+__fgetpwent_r (FILE *stream, struct passwd *resbuf, char *buffer,
+ size_t buflen, struct passwd **result)
+{
+ int ret = __nss_fgetent_r (stream, resbuf, buffer, buflen, parse_line);
+ if (ret == 0)
+ *result = resbuf;
+ else
+ *result = NULL;
+ return ret;
+}
+weak_alias (__fgetpwent_r, fgetpwent_r)
diff --git a/nss/getpw.c b/nss/getpw.c
new file mode 100644
index 0000000000..cf747374b8
--- /dev/null
+++ b/nss/getpw.c
@@ -0,0 +1,62 @@
+/* Copyright (C) 1991-2023 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <alloca.h>
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <pwd.h>
+
+
+/* Re-construct the password-file line for the given uid
+ in the given buffer. This knows the format that the caller
+ will expect, but this need not be the format of the password file. */
+
+int __getpw (__uid_t uid, char *buf);
+
+int
+__getpw (__uid_t uid, char *buf)
+{
+ size_t buflen;
+ char *tmpbuf;
+ struct passwd resbuf, *p;
+
+ if (buf == NULL)
+ {
+ __set_errno (EINVAL);
+ return -1;
+ }
+
+ buflen = __sysconf (_SC_GETPW_R_SIZE_MAX);
+ tmpbuf = alloca (buflen);
+
+ if (__getpwuid_r (uid, &resbuf, tmpbuf, buflen, &p) != 0)
+ return -1;
+
+ if (p == NULL)
+ return -1;
+
+ if (sprintf (buf, "%s:%s:%lu:%lu:%s:%s:%s", p->pw_name, p->pw_passwd,
+ (unsigned long int) p->pw_uid, (unsigned long int) p->pw_gid,
+ p->pw_gecos, p->pw_dir, p->pw_shell) < 0)
+ return -1;
+
+ return 0;
+}
+weak_alias (__getpw, getpw)
+
+link_warning (getpw, "the `getpw' function is dangerous and should not be used.")
diff --git a/nss/getpwent.c b/nss/getpwent.c
new file mode 100644
index 0000000000..eb65bdc73b
--- /dev/null
+++ b/nss/getpwent.c
@@ -0,0 +1,28 @@
+/* Copyright (C) 1996-2023 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <pwd.h>
+
+
+#define LOOKUP_TYPE struct passwd
+#define SETFUNC_NAME setpwent
+#define GETFUNC_NAME getpwent
+#define ENDFUNC_NAME endpwent
+#define DATABASE_NAME passwd
+#define BUFLEN NSS_BUFLEN_PASSWD
+
+#include "../nss/getXXent.c"
diff --git a/nss/getpwent_r.c b/nss/getpwent_r.c
new file mode 100644
index 0000000000..f26bf89ddc
--- /dev/null
+++ b/nss/getpwent_r.c
@@ -0,0 +1,28 @@
+/* Copyright (C) 1996-2023 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <pwd.h>
+
+
+#define LOOKUP_TYPE struct passwd
+#define SETFUNC_NAME setpwent
+#define GETFUNC_NAME getpwent
+#define ENDFUNC_NAME endpwent
+#define DATABASE_NAME passwd
+#define BUFLEN NSS_BUFLEN_PASSWD
+
+#include "../nss/getXXent_r.c"
diff --git a/nss/getpwnam.c b/nss/getpwnam.c
new file mode 100644
index 0000000000..9a28d53670
--- /dev/null
+++ b/nss/getpwnam.c
@@ -0,0 +1,28 @@
+/* Copyright (C) 1996-2023 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <pwd.h>
+
+
+#define LOOKUP_TYPE struct passwd
+#define FUNCTION_NAME getpwnam
+#define DATABASE_NAME passwd
+#define ADD_PARAMS const char *name
+#define ADD_VARIABLES name
+#define BUFLEN NSS_BUFLEN_PASSWD
+
+#include "../nss/getXXbyYY.c"
diff --git a/nss/getpwnam_r.c b/nss/getpwnam_r.c
new file mode 100644
index 0000000000..e91067080c
--- /dev/null
+++ b/nss/getpwnam_r.c
@@ -0,0 +1,28 @@
+/* Copyright (C) 1996-2023 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <pwd.h>
+
+
+#define LOOKUP_TYPE struct passwd
+#define FUNCTION_NAME getpwnam
+#define DATABASE_NAME passwd
+#define ADD_PARAMS const char *name
+#define ADD_VARIABLES name
+#define BUFLEN NSS_BUFLEN_PASSWD
+
+#include <nss/getXXbyYY_r.c>
diff --git a/nss/getpwuid.c b/nss/getpwuid.c
new file mode 100644
index 0000000000..d92fb22a0e
--- /dev/null
+++ b/nss/getpwuid.c
@@ -0,0 +1,28 @@
+/* Copyright (C) 1996-2023 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <pwd.h>
+
+
+#define LOOKUP_TYPE struct passwd
+#define FUNCTION_NAME getpwuid
+#define DATABASE_NAME passwd
+#define ADD_PARAMS uid_t uid
+#define ADD_VARIABLES uid
+#define BUFLEN NSS_BUFLEN_PASSWD
+
+#include "../nss/getXXbyYY.c"
diff --git a/nss/getpwuid_r.c b/nss/getpwuid_r.c
new file mode 100644
index 0000000000..763fad5191
--- /dev/null
+++ b/nss/getpwuid_r.c
@@ -0,0 +1,28 @@
+/* Copyright (C) 1996-2023 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <pwd.h>
+
+
+#define LOOKUP_TYPE struct passwd
+#define FUNCTION_NAME getpwuid
+#define DATABASE_NAME passwd
+#define ADD_PARAMS uid_t uid
+#define ADD_VARIABLES uid
+#define BUFLEN NSS_BUFLEN_PASSWD
+
+#include <nss/getXXbyYY_r.c>
diff --git a/nss/putpwent.c b/nss/putpwent.c
new file mode 100644
index 0000000000..335e20ad86
--- /dev/null
+++ b/nss/putpwent.c
@@ -0,0 +1,65 @@
+/* Copyright (C) 1991-2023 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <errno.h>
+#include <stdio.h>
+#include <pwd.h>
+#include <stdlib.h>
+#include <nss.h>
+
+#define _S(x) x ?: ""
+
+/* Write an entry to the given stream. This must know the format of
+ the password file. If the input contains invalid characters,
+ return EINVAL, or replace them with spaces (if they are contained
+ in the GECOS field). */
+int
+putpwent (const struct passwd *p, FILE *stream)
+{
+ if (p == NULL || stream == NULL
+ || p->pw_name == NULL || !__nss_valid_field (p->pw_name)
+ || !__nss_valid_field (p->pw_passwd)
+ || !__nss_valid_field (p->pw_dir)
+ || !__nss_valid_field (p->pw_shell))
+ {
+ __set_errno (EINVAL);
+ return -1;
+ }
+
+ int ret;
+ char *gecos_alloc;
+ const char *gecos = __nss_rewrite_field (p->pw_gecos, &gecos_alloc);
+
+ if (gecos == NULL)
+ return -1;
+
+ if (p->pw_name[0] == '+' || p->pw_name[0] == '-')
+ ret = fprintf (stream, "%s:%s:::%s:%s:%s\n",
+ p->pw_name, _S (p->pw_passwd),
+ gecos, _S (p->pw_dir), _S (p->pw_shell));
+ else
+ ret = fprintf (stream, "%s:%s:%lu:%lu:%s:%s:%s\n",
+ p->pw_name, _S (p->pw_passwd),
+ (unsigned long int) p->pw_uid,
+ (unsigned long int) p->pw_gid,
+ gecos, _S (p->pw_dir), _S (p->pw_shell));
+
+ free (gecos_alloc);
+ if (ret >= 0)
+ ret = 0;
+ return ret;
+}
diff --git a/nss/pwd.h b/nss/pwd.h
new file mode 100644
index 0000000000..d96f153da8
--- /dev/null
+++ b/nss/pwd.h
@@ -0,0 +1,193 @@
+/* Copyright (C) 1991-2023 Free Software Foundation, Inc.