2016-05-15 19:19:39 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 2011 , Julian Pidancet
|
|
|
|
* SPDX-FileCopyrightText: 2011 , Nicolas François
|
2016-05-15 19:19:39 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2016-05-15 19:19:39 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ident "$Id$"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
2023-02-05 03:11:18 +05:30
|
|
|
|
2016-05-15 19:19:39 +05:30
|
|
|
#include "defines.h"
|
2023-02-05 03:11:18 +05:30
|
|
|
#include "alloc.h"
|
2016-05-15 19:19:39 +05:30
|
|
|
#include "prototypes.h"
|
|
|
|
/*@-exitarg@*/
|
|
|
|
#include "exitcodes.h"
|
|
|
|
#include "groupio.h"
|
|
|
|
#include "pwio.h"
|
|
|
|
#ifdef SHADOWGRP
|
|
|
|
#include "sgroupio.h"
|
|
|
|
#endif
|
|
|
|
#include "shadowio.h"
|
|
|
|
#ifdef ENABLE_SUBIDS
|
|
|
|
#include "subordinateio.h"
|
|
|
|
#endif /* ENABLE_SUBIDS */
|
|
|
|
#include "getdef.h"
|
2021-11-29 05:07:53 +05:30
|
|
|
#include "shadowlog.h"
|
2016-05-15 19:19:39 +05:30
|
|
|
|
|
|
|
static char *passwd_db_file = NULL;
|
|
|
|
static char *spw_db_file = NULL;
|
|
|
|
static char *group_db_file = NULL;
|
|
|
|
static char *sgroup_db_file = NULL;
|
|
|
|
static char *suid_db_file = NULL;
|
|
|
|
static char *sgid_db_file = NULL;
|
|
|
|
static char *def_conf_file = NULL;
|
|
|
|
static FILE* fp_pwent = NULL;
|
|
|
|
static FILE* fp_grent = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process_prefix_flag - prefix all paths if given the --prefix option
|
|
|
|
*
|
|
|
|
* This shall be called before accessing the passwd, group, shadow,
|
|
|
|
* gshadow, useradd's default, login.defs files (non exhaustive list)
|
|
|
|
* or authenticating the caller.
|
|
|
|
*
|
|
|
|
* The audit, syslog, or locale files shall be open before
|
|
|
|
*/
|
|
|
|
extern const char* process_prefix_flag (const char* short_opt, int argc, char **argv)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Parse the command line options.
|
|
|
|
*/
|
|
|
|
int i;
|
2020-02-08 03:27:12 +05:30
|
|
|
const char *prefix = NULL, *val;
|
2016-05-15 19:19:39 +05:30
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
2020-02-08 03:27:12 +05:30
|
|
|
val = NULL;
|
2016-05-15 19:19:39 +05:30
|
|
|
if ( (strcmp (argv[i], "--prefix") == 0)
|
2020-02-08 03:27:12 +05:30
|
|
|
|| ((strncmp (argv[i], "--prefix=", 9) == 0)
|
|
|
|
&& (val = argv[i] + 9))
|
2016-05-15 19:19:39 +05:30
|
|
|
|| (strcmp (argv[i], short_opt) == 0)) {
|
|
|
|
if (NULL != prefix) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(),
|
2016-05-15 19:19:39 +05:30
|
|
|
_("%s: multiple --prefix options\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname());
|
2016-05-15 19:19:39 +05:30
|
|
|
exit (E_BAD_ARG);
|
|
|
|
}
|
|
|
|
|
2020-02-08 03:27:12 +05:30
|
|
|
if (val) {
|
|
|
|
prefix = val;
|
|
|
|
} else if (i + 1 == argc) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(),
|
2016-05-15 19:19:39 +05:30
|
|
|
_("%s: option '%s' requires an argument\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname(), argv[i]);
|
2016-05-15 19:19:39 +05:30
|
|
|
exit (E_BAD_ARG);
|
2020-02-08 03:27:12 +05:30
|
|
|
} else {
|
|
|
|
prefix = argv[++ i];
|
2016-05-15 19:19:39 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 13:57:31 +05:30
|
|
|
|
2016-05-15 19:19:39 +05:30
|
|
|
|
|
|
|
if (prefix != NULL) {
|
|
|
|
if ( prefix[0] == '\0' || !strcmp(prefix, "/"))
|
|
|
|
return ""; /* if prefix is "/" then we ignore the flag option */
|
|
|
|
/* should we prevent symbolic link from being used as a prefix? */
|
|
|
|
|
2021-06-02 08:41:37 +05:30
|
|
|
if ( prefix[0] != '/') {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(),
|
2021-06-02 08:41:37 +05:30
|
|
|
_("%s: prefix must be an absolute path\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname());
|
2021-06-02 08:41:37 +05:30
|
|
|
exit (E_BAD_ARG);
|
|
|
|
}
|
2016-05-15 19:19:39 +05:30
|
|
|
size_t len;
|
|
|
|
len = strlen(prefix) + strlen(PASSWD_FILE) + 2;
|
2023-02-05 03:11:18 +05:30
|
|
|
passwd_db_file = XMALLOCARRAY(len, char);
|
2016-05-15 19:19:39 +05:30
|
|
|
snprintf(passwd_db_file, len, "%s/%s", prefix, PASSWD_FILE);
|
|
|
|
pw_setdbname(passwd_db_file);
|
|
|
|
|
|
|
|
len = strlen(prefix) + strlen(GROUP_FILE) + 2;
|
2023-02-05 03:11:18 +05:30
|
|
|
group_db_file = XMALLOCARRAY(len, char);
|
2016-05-15 19:19:39 +05:30
|
|
|
snprintf(group_db_file, len, "%s/%s", prefix, GROUP_FILE);
|
|
|
|
gr_setdbname(group_db_file);
|
2019-09-20 13:57:31 +05:30
|
|
|
|
2016-05-15 19:19:39 +05:30
|
|
|
#ifdef SHADOWGRP
|
|
|
|
len = strlen(prefix) + strlen(SGROUP_FILE) + 2;
|
2023-02-05 03:11:18 +05:30
|
|
|
sgroup_db_file = XMALLOCARRAY(len, char);
|
2016-05-15 19:19:39 +05:30
|
|
|
snprintf(sgroup_db_file, len, "%s/%s", prefix, SGROUP_FILE);
|
|
|
|
sgr_setdbname(sgroup_db_file);
|
|
|
|
#endif
|
|
|
|
#ifdef USE_NIS
|
|
|
|
__setspNIS(0); /* disable NIS for now, at least until it is properly supporting a "prefix" */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
len = strlen(prefix) + strlen(SHADOW_FILE) + 2;
|
2023-02-05 03:11:18 +05:30
|
|
|
spw_db_file = XMALLOCARRAY(len, char);
|
2016-05-15 19:19:39 +05:30
|
|
|
snprintf(spw_db_file, len, "%s/%s", prefix, SHADOW_FILE);
|
|
|
|
spw_setdbname(spw_db_file);
|
2019-09-20 13:57:31 +05:30
|
|
|
|
2018-06-18 19:21:27 +05:30
|
|
|
#ifdef ENABLE_SUBIDS
|
2016-05-15 19:19:39 +05:30
|
|
|
len = strlen(prefix) + strlen("/etc/subuid") + 2;
|
2023-02-05 03:11:18 +05:30
|
|
|
suid_db_file = XMALLOCARRAY(len, char);
|
2016-05-15 19:19:39 +05:30
|
|
|
snprintf(suid_db_file, len, "%s/%s", prefix, "/etc/subuid");
|
|
|
|
sub_uid_setdbname(suid_db_file);
|
|
|
|
|
|
|
|
len = strlen(prefix) + strlen("/etc/subgid") + 2;
|
2023-02-05 03:11:18 +05:30
|
|
|
sgid_db_file = XMALLOCARRAY(len, char);
|
2016-05-15 19:19:39 +05:30
|
|
|
snprintf(sgid_db_file, len, "%s/%s", prefix, "/etc/subgid");
|
|
|
|
sub_gid_setdbname(sgid_db_file);
|
2018-06-18 19:21:27 +05:30
|
|
|
#endif
|
2016-05-15 19:19:39 +05:30
|
|
|
|
2019-09-20 13:57:31 +05:30
|
|
|
#ifdef USE_ECONF
|
|
|
|
setdef_config_file(prefix);
|
|
|
|
#else
|
2016-05-15 19:19:39 +05:30
|
|
|
len = strlen(prefix) + strlen("/etc/login.defs") + 2;
|
2023-02-05 03:11:18 +05:30
|
|
|
def_conf_file = XMALLOCARRAY(len, char);
|
2016-05-15 19:19:39 +05:30
|
|
|
snprintf(def_conf_file, len, "%s/%s", prefix, "/etc/login.defs");
|
|
|
|
setdef_config_file(def_conf_file);
|
2019-09-20 13:57:31 +05:30
|
|
|
#endif
|
|
|
|
}
|
2016-05-15 19:19:39 +05:30
|
|
|
|
|
|
|
if (prefix == NULL)
|
|
|
|
return "";
|
|
|
|
return prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern struct group *prefix_getgrnam(const char *name)
|
|
|
|
{
|
|
|
|
if (group_db_file) {
|
|
|
|
FILE* fg;
|
|
|
|
struct group * grp = NULL;
|
|
|
|
|
|
|
|
fg = fopen(group_db_file, "rt");
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!fg)
|
2016-05-15 19:19:39 +05:30
|
|
|
return NULL;
|
2021-08-18 23:36:02 +05:30
|
|
|
while ((grp = fgetgrent(fg)) != NULL) {
|
|
|
|
if (!strcmp(name, grp->gr_name))
|
2016-05-15 19:19:39 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fg);
|
|
|
|
return grp;
|
|
|
|
}
|
2019-09-20 13:57:31 +05:30
|
|
|
|
2018-03-29 00:44:12 +05:30
|
|
|
return getgrnam(name);
|
2016-05-15 19:19:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
extern struct group *prefix_getgrgid(gid_t gid)
|
|
|
|
{
|
|
|
|
if (group_db_file) {
|
|
|
|
FILE* fg;
|
|
|
|
struct group * grp = NULL;
|
|
|
|
|
|
|
|
fg = fopen(group_db_file, "rt");
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!fg)
|
2016-05-15 19:19:39 +05:30
|
|
|
return NULL;
|
2021-08-18 23:36:02 +05:30
|
|
|
while ((grp = fgetgrent(fg)) != NULL) {
|
|
|
|
if (gid == grp->gr_gid)
|
2016-05-15 19:19:39 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fg);
|
|
|
|
return grp;
|
|
|
|
}
|
2018-03-29 00:44:12 +05:30
|
|
|
|
|
|
|
return getgrgid(gid);
|
2016-05-15 19:19:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
extern struct passwd *prefix_getpwuid(uid_t uid)
|
|
|
|
{
|
|
|
|
if (passwd_db_file) {
|
|
|
|
FILE* fg;
|
|
|
|
struct passwd *pwd = NULL;
|
|
|
|
|
|
|
|
fg = fopen(passwd_db_file, "rt");
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!fg)
|
2016-05-15 19:19:39 +05:30
|
|
|
return NULL;
|
2021-08-18 23:36:02 +05:30
|
|
|
while ((pwd = fgetpwent(fg)) != NULL) {
|
|
|
|
if (uid == pwd->pw_uid)
|
2016-05-15 19:19:39 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fg);
|
|
|
|
return pwd;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return getpwuid(uid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern struct passwd *prefix_getpwnam(const char* name)
|
|
|
|
{
|
|
|
|
if (passwd_db_file) {
|
|
|
|
FILE* fg;
|
|
|
|
struct passwd *pwd = NULL;
|
|
|
|
|
|
|
|
fg = fopen(passwd_db_file, "rt");
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!fg)
|
2016-05-15 19:19:39 +05:30
|
|
|
return NULL;
|
2021-08-18 23:36:02 +05:30
|
|
|
while ((pwd = fgetpwent(fg)) != NULL) {
|
|
|
|
if (!strcmp(name, pwd->pw_name))
|
2016-05-15 19:19:39 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fg);
|
|
|
|
return pwd;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return getpwnam(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern struct spwd *prefix_getspnam(const char* name)
|
|
|
|
{
|
|
|
|
if (spw_db_file) {
|
|
|
|
FILE* fg;
|
|
|
|
struct spwd *sp = NULL;
|
|
|
|
|
|
|
|
fg = fopen(spw_db_file, "rt");
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!fg)
|
2016-05-15 19:19:39 +05:30
|
|
|
return NULL;
|
2021-08-18 23:36:02 +05:30
|
|
|
while ((sp = fgetspent(fg)) != NULL) {
|
|
|
|
if (!strcmp(name, sp->sp_namp))
|
2016-05-15 19:19:39 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fg);
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return getspnam(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:13:01 +05:30
|
|
|
extern void prefix_setpwent(void)
|
2016-05-15 19:19:39 +05:30
|
|
|
{
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!passwd_db_file) {
|
2016-05-15 19:19:39 +05:30
|
|
|
setpwent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fp_pwent)
|
|
|
|
fclose (fp_pwent);
|
2019-09-20 13:57:31 +05:30
|
|
|
|
2016-05-15 19:19:39 +05:30
|
|
|
fp_pwent = fopen(passwd_db_file, "rt");
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!fp_pwent)
|
2016-05-15 19:19:39 +05:30
|
|
|
return;
|
|
|
|
}
|
2022-01-03 17:13:01 +05:30
|
|
|
extern struct passwd* prefix_getpwent(void)
|
2016-05-15 19:19:39 +05:30
|
|
|
{
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!passwd_db_file) {
|
2016-05-15 19:19:39 +05:30
|
|
|
return getpwent();
|
|
|
|
}
|
2021-11-01 18:24:25 +05:30
|
|
|
if (!fp_pwent) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-05-15 19:19:39 +05:30
|
|
|
return fgetpwent(fp_pwent);
|
|
|
|
}
|
2022-01-03 17:13:01 +05:30
|
|
|
extern void prefix_endpwent(void)
|
2016-05-15 19:19:39 +05:30
|
|
|
{
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!passwd_db_file) {
|
2016-05-15 19:19:39 +05:30
|
|
|
endpwent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fp_pwent)
|
|
|
|
fclose(fp_pwent);
|
|
|
|
fp_pwent = NULL;
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:13:01 +05:30
|
|
|
extern void prefix_setgrent(void)
|
2016-05-15 19:19:39 +05:30
|
|
|
{
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!group_db_file) {
|
2016-05-15 19:19:39 +05:30
|
|
|
setgrent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fp_grent)
|
|
|
|
fclose (fp_grent);
|
2019-09-20 13:57:31 +05:30
|
|
|
|
2016-05-15 19:19:39 +05:30
|
|
|
fp_grent = fopen(group_db_file, "rt");
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!fp_grent)
|
2016-05-15 19:19:39 +05:30
|
|
|
return;
|
|
|
|
}
|
2022-01-03 17:13:01 +05:30
|
|
|
extern struct group* prefix_getgrent(void)
|
2016-05-15 19:19:39 +05:30
|
|
|
{
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!group_db_file) {
|
2016-05-15 19:19:39 +05:30
|
|
|
return getgrent();
|
|
|
|
}
|
|
|
|
return fgetgrent(fp_grent);
|
|
|
|
}
|
2022-01-03 17:13:01 +05:30
|
|
|
extern void prefix_endgrent(void)
|
2016-05-15 19:19:39 +05:30
|
|
|
{
|
2021-08-18 23:36:02 +05:30
|
|
|
if (!group_db_file) {
|
2016-05-15 19:19:39 +05:30
|
|
|
endgrent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fp_grent)
|
|
|
|
fclose(fp_grent);
|
|
|
|
fp_grent = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern struct group *prefix_getgr_nam_gid(const char *grname)
|
|
|
|
{
|
|
|
|
long long int gid;
|
|
|
|
char *endptr;
|
2018-05-09 08:07:55 +05:30
|
|
|
struct group *g;
|
2016-05-15 19:19:39 +05:30
|
|
|
|
|
|
|
if (NULL == grname) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group_db_file) {
|
|
|
|
errno = 0;
|
|
|
|
gid = strtoll (grname, &endptr, 10);
|
|
|
|
if ( ('\0' != *grname)
|
|
|
|
&& ('\0' == *endptr)
|
|
|
|
&& (ERANGE != errno)
|
|
|
|
&& (gid == (gid_t)gid)) {
|
2023-02-01 18:20:48 +05:30
|
|
|
return prefix_getgrgid (gid);
|
2016-05-15 19:19:39 +05:30
|
|
|
}
|
2018-05-09 08:07:55 +05:30
|
|
|
g = prefix_getgrnam (grname);
|
|
|
|
return g ? __gr_dup(g) : NULL;
|
2016-05-15 19:19:39 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
return getgr_nam_gid(grname);
|
|
|
|
}
|