passwd: Respect --prefix/-P options

Add prefix_getpwnam_r() and xprefix_getpwnam() and make passwd
use prefix-aware functions when handling the database.
This commit is contained in:
Jaroslav Jindrak
2023-06-09 16:22:24 +02:00
committed by Iker Pedrosa
parent ded9cab35d
commit 43e60eb681
5 changed files with 74 additions and 7 deletions
+1
View File
@@ -75,6 +75,7 @@ libmisc_la_SOURCES = \
utmp.c \
valid.c \
xgetpwnam.c \
xprefix_getpwnam.c \
xgetpwuid.c \
xgetgrnam.c \
xgetgrgid.c \
+21
View File
@@ -238,6 +238,27 @@ extern struct passwd *prefix_getpwnam(const char* name)
return getpwnam(name);
}
}
extern int prefix_getpwnam_r(const char* name, struct passwd* pwd,
char* buf, size_t buflen, struct passwd** result)
{
if (passwd_db_file) {
FILE* fg;
int ret = 0;
fg = fopen(passwd_db_file, "rt");
if (!fg)
return errno;
while ((ret = fgetpwent_r(fg, pwd, buf, buflen, result)) == 0) {
if (!strcmp(name, pwd->pw_name))
break;
}
fclose(fg);
return ret;
}
else {
return getpwnam_r(name, pwd, buf, buflen, result);
}
}
extern struct spwd *prefix_getspnam(const char* name)
{
if (spw_db_file) {
+41
View File
@@ -0,0 +1,41 @@
/*
* SPDX-FileCopyrightText: 2007 - 2009, Nicolas François
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/*
* According to the Linux-PAM documentation:
*
* 4.1. Care about standard library calls
*
* In general, writers of authorization-granting applications should
* assume that each module is likely to call any or all 'libc' functions.
* For 'libc' functions that return pointers to static/dynamically
* allocated structures (ie. the library allocates the memory and the
* user is not expected to 'free()' it) any module call to this function
* is likely to corrupt a pointer previously obtained by the application.
* The application programmer should either re-call such a 'libc'
* function after a call to the Linux-PAM library, or copy the structure
* contents to some safe area of memory before passing control to the
* Linux-PAM library.
*
* Two important function classes that fall into this category are
* getpwnam(3) and syslog(3).
*
* This file provides wrapper to the prefix_getpwnam or prefix_getpwnam_r functions.
*/
#include <config.h>
#include "pwio.h"
#define LOOKUP_TYPE struct passwd
#define FUNCTION_NAME prefix_getpwnam
#define ARG_TYPE const char *
#define ARG_NAME name
#define DUP_FUNCTION __pw_dup
#define HAVE_FUNCTION_R 1
#include "xgetXXbyYY.c"