Skip to content

Commit 142b430

Browse files
committed
pw: Skip root check with alternate root
pw may be run by an unprivileged user for creating an image or jail. EPERM will still be reported from the file open if the user does not have appropriate permission. Reviewed by: markj Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D50710
1 parent 1344979 commit 142b430

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

usr.sbin/pw/pw.8

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2323
.\" SUCH DAMAGE.
2424
.\"
25-
.Dd July 29, 2024
25+
.Dd August 19, 2025
2626
.Dt PW 8
2727
.Os
2828
.Sh NAME
@@ -191,7 +191,12 @@ utility handles updating the
191191
.Xr master.passwd 5 ,
192192
.Xr group 5
193193
and the secure and insecure
194-
password database files, and must be run as root.
194+
password database files, and must be run as root
195+
.Po except when using
196+
.Fl R
197+
or
198+
.Fl V
199+
.Pc .
195200
.Pp
196201
The first one or two keywords provided to
197202
.Nm

usr.sbin/pw/pw.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ main(int argc, char *argv[])
162162
snprintf(conf.etcpath, sizeof(conf.etcpath),
163163
"%s%s", optarg, arg == 'R' ?
164164
_PATH_PWD : "");
165+
conf.altroot = true;
165166
} else
166167
break;
167168
}

usr.sbin/pw/pw_user.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ perform_chgpwent(const char *name, struct passwd *pwd, char *nispasswd)
238238
}
239239
}
240240

241+
static void
242+
pw_check_root(void)
243+
{
244+
if (!conf.altroot && geteuid() != 0)
245+
errx(EX_NOPERM, "you must be root");
246+
}
247+
241248
/*
242249
* The M_LOCK and M_UNLOCK functions simply add or remove
243250
* a "*LOCKED*" prefix from in front of the password to
@@ -256,8 +263,7 @@ pw_userlock(char *arg1, int mode)
256263
bool locked = false;
257264
uid_t id = (uid_t)-1;
258265

259-
if (geteuid() != 0)
260-
errx(EX_NOPERM, "you must be root");
266+
pw_check_root();
261267

262268
if (arg1 == NULL)
263269
errx(EX_DATAERR, "username or id required");
@@ -1324,8 +1330,8 @@ pw_user_add(int argc, char **argv, char *arg1)
13241330
if (argc > 0)
13251331
usage();
13261332

1327-
if (geteuid() != 0 && ! dryrun)
1328-
errx(EX_NOPERM, "you must be root");
1333+
if (!dryrun)
1334+
pw_check_root();
13291335

13301336
if (quiet)
13311337
freopen(_PATH_DEVNULL, "w", stderr);
@@ -1641,8 +1647,8 @@ pw_user_mod(int argc, char **argv, char *arg1)
16411647
if (argc > 0)
16421648
usage();
16431649

1644-
if (geteuid() != 0 && ! dryrun)
1645-
errx(EX_NOPERM, "you must be root");
1650+
if (!dryrun)
1651+
pw_check_root();
16461652

16471653
if (quiet)
16481654
freopen(_PATH_DEVNULL, "w", stderr);

usr.sbin/pw/pwupd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ struct pwconf {
7878
char etcpath[MAXPATHLEN];
7979
int fd;
8080
int rootfd;
81+
bool altroot;
8182
bool checkduplicate;
8283
};
8384

0 commit comments

Comments
 (0)