xref: /freebsd/lib/libutil/pw_util.3 (revision bc89b58def9c028d29a5faf3a03eee2b8b5337ae)
142818163SBaptiste Daroussin.\" Copyright (c) 2012 Baptiste Daroussin <bapt@FreeBSD.org>
242818163SBaptiste Daroussin.\" All rights reserved.
342818163SBaptiste Daroussin.\"
442818163SBaptiste Daroussin.\" Redistribution and use in source and binary forms, with or without
542818163SBaptiste Daroussin.\" modification, are permitted provided that the following conditions
642818163SBaptiste Daroussin.\" are met:
742818163SBaptiste Daroussin.\" 1. Redistributions of source code must retain the above copyright
842818163SBaptiste Daroussin.\"    notice, this list of conditions and the following disclaimer.
942818163SBaptiste Daroussin.\" 2. Redistributions in binary form must reproduce the above copyright
1042818163SBaptiste Daroussin.\"    notice, this list of conditions and the following disclaimer in the
1142818163SBaptiste Daroussin.\"    documentation and/or other materials provided with the distribution.
1242818163SBaptiste Daroussin.\"
1342818163SBaptiste Daroussin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1442818163SBaptiste Daroussin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1542818163SBaptiste Daroussin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1642818163SBaptiste Daroussin.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
1742818163SBaptiste Daroussin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1842818163SBaptiste Daroussin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1942818163SBaptiste Daroussin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2042818163SBaptiste Daroussin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2142818163SBaptiste Daroussin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2242818163SBaptiste Daroussin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2342818163SBaptiste Daroussin.\" SUCH DAMAGE.
2442818163SBaptiste Daroussin.\"
2542818163SBaptiste Daroussin.\" $FreeBSD$
2642818163SBaptiste Daroussin.\"
27*bc89b58dSGordon Bergling.Dd May 10, 2020
2842818163SBaptiste Daroussin.Dt PW_UTIL 3
2942818163SBaptiste Daroussin.Os
3042818163SBaptiste Daroussin.Sh NAME
3142818163SBaptiste Daroussin.Nm pw_copy ,
3242818163SBaptiste Daroussin.Nm pw_dup ,
3342818163SBaptiste Daroussin.Nm pw_edit ,
3442818163SBaptiste Daroussin.Nm pw_equal ,
3542818163SBaptiste Daroussin.Nm pw_fini ,
3642818163SBaptiste Daroussin.Nm pw_init ,
3742818163SBaptiste Daroussin.Nm pw_make ,
3842818163SBaptiste Daroussin.Nm pw_make_v7 ,
3942818163SBaptiste Daroussin.Nm pw_mkdb ,
4042818163SBaptiste Daroussin.Nm pw_lock ,
4142818163SBaptiste Daroussin.Nm pw_scan ,
4242818163SBaptiste Daroussin.Nm pw_tempname ,
4342818163SBaptiste Daroussin.Nm pw_tmp
4442818163SBaptiste Daroussin.Nd "functions for passwd file handling"
4542818163SBaptiste Daroussin.Sh LIBRARY
4642818163SBaptiste Daroussin.Lb libutil
4742818163SBaptiste Daroussin.Sh SYNOPSIS
4842818163SBaptiste Daroussin.In pwd.h
4942818163SBaptiste Daroussin.In libutil.h
5042818163SBaptiste Daroussin.Ft int
5117483344SEitan Adler.Fn pw_copy "int ffd" "int tfd" "const struct passwd *pw" "struct passwd *oldpw"
5242818163SBaptiste Daroussin.Ft "struct passwd *"
5342818163SBaptiste Daroussin.Fn pw_dup "const struct passwd *pw"
5442818163SBaptiste Daroussin.Ft int
5542818163SBaptiste Daroussin.Fn pw_edit "int nosetuid"
5642818163SBaptiste Daroussin.Ft int
5717483344SEitan Adler.Fn pw_equal "const struct passwd *pw1" "const struct passwd *pw2"
5842818163SBaptiste Daroussin.Ft void
5942818163SBaptiste Daroussin.Fn pw_fini "void"
6042818163SBaptiste Daroussin.Ft int
618e7299c0SMark Johnston.Fn pw_init "const char *dir" "const char *master"
6234e9190dSIan Lepore.Ft void
6334e9190dSIan Lepore.Fn pw_initpwd "struct passwd *pw"
6442818163SBaptiste Daroussin.Ft "char *"
6542818163SBaptiste Daroussin.Fn pw_make "const struct passwd *pw"
6642818163SBaptiste Daroussin.Ft "char *"
6742818163SBaptiste Daroussin.Fn pw_make_v7 "const struct passwd *pw"
6842818163SBaptiste Daroussin.Ft int
6942818163SBaptiste Daroussin.Fn pw_mkdb "const char *user"
7042818163SBaptiste Daroussin.Ft int
7142818163SBaptiste Daroussin.Fn pw_lock "void"
7242818163SBaptiste Daroussin.Ft "struct passwd *"
7342818163SBaptiste Daroussin.Fn pw_scan "const char *line" "int flags"
7442818163SBaptiste Daroussin.Ft "const char *"
7542818163SBaptiste Daroussin.Fn pw_tempname "void"
7642818163SBaptiste Daroussin.Ft int
7742818163SBaptiste Daroussin.Fn pw_tmp "int mfd"
7842818163SBaptiste Daroussin.Sh DESCRIPTION
7942818163SBaptiste DaroussinThe
8042818163SBaptiste Daroussin.Fn pw_copy
8142818163SBaptiste Daroussinfunction reads a password file from
8242818163SBaptiste Daroussin.Vt ffd
8342818163SBaptiste Daroussinand writes it back out to
8442818163SBaptiste Daroussin.Vt tfd
8542818163SBaptiste Daroussinpossibly with modifications:
8642818163SBaptiste Daroussin.Bl -dash
8742818163SBaptiste Daroussin.It
8842818163SBaptiste DaroussinIf
8942818163SBaptiste Daroussin.Fa pw
9042818163SBaptiste Daroussinis
9142818163SBaptiste Daroussin.Dv NULL
9242818163SBaptiste Daroussinand
9342818163SBaptiste Daroussin.Fa oldpw
9442818163SBaptiste Daroussinis not
9542818163SBaptiste Daroussin.Dv NULL ,
9642818163SBaptiste Daroussinthen the record represented by
9742818163SBaptiste Daroussin.Fa oldpw
9842818163SBaptiste Daroussinwill not be copied (corresponding to user deletion).
9942818163SBaptiste Daroussin.It
10042818163SBaptiste DaroussinIf
10142818163SBaptiste Daroussin.Fa pw
10242818163SBaptiste Daroussinand
10342818163SBaptiste Daroussin.Fa oldpw
10442818163SBaptiste Daroussinare not
10542818163SBaptiste Daroussin.Dv NULL
10642818163SBaptiste Daroussinthen the record corresponding to
10742818163SBaptiste Daroussin.Fa pw
1082acb4741SJoel Dahlwill be replaced by the record corresponding to
10942818163SBaptiste Daroussin.Fa oldpw .
11042818163SBaptiste Daroussin.It
11142818163SBaptiste DaroussinIf
11242818163SBaptiste Daroussin.Vt pw
11342818163SBaptiste Daroussinis set and
11442818163SBaptiste Daroussin.Vt oldpw
11542818163SBaptiste Daroussinis
11642818163SBaptiste Daroussin.Dv NULL
11742818163SBaptiste Daroussinthen the record corresponding to
11842818163SBaptiste Daroussin.Vt pw
11942818163SBaptiste Daroussinwill be appended (corresponding to user addition).
12042818163SBaptiste Daroussin.El
12142818163SBaptiste Daroussin.Pp
12242818163SBaptiste DaroussinThe
12342818163SBaptiste Daroussin.Fn pw_copy
12442818163SBaptiste Daroussinfunction returns -1 in case of failure otherwise 0.
12542818163SBaptiste Daroussin.Pp
12642818163SBaptiste DaroussinThe
12742818163SBaptiste Daroussin.Fn pw_dup
12842818163SBaptiste Daroussinfunction duplicates the
12942818163SBaptiste Daroussin.Vt struct passwd
13042818163SBaptiste Daroussinpointed to by
13142818163SBaptiste Daroussin.Fa pw
13242818163SBaptiste Daroussinand returns a pointer to the copy, or
13342818163SBaptiste Daroussin.Dv NULL
13442818163SBaptiste Daroussinin case of failure.
13542818163SBaptiste DaroussinThe new
13642818163SBaptiste Daroussin.Vt struct passwd
13742818163SBaptiste Daroussinis allocated with
13842818163SBaptiste Daroussin.Xr malloc 3 ,
13942818163SBaptiste Daroussinand it is the caller's responsibility to free it with
14042818163SBaptiste Daroussin.Xr free 3 .
14142818163SBaptiste Daroussin.Pp
14242818163SBaptiste DaroussinThe
14342818163SBaptiste Daroussin.Fn pw_edit
14442818163SBaptiste Daroussinfunction invokes the command specified by the
14542818163SBaptiste Daroussin.Ev EDITOR
14642818163SBaptiste Daroussinenvironment variable (or
14742818163SBaptiste Daroussin.Pa /usr/bin/vi
14842818163SBaptiste Daroussinif
14942818163SBaptiste Daroussin.Ev EDITOR
15042818163SBaptiste Daroussinis not defined)
15142818163SBaptiste Daroussinon a temporary copy of the master password file created by
15242818163SBaptiste Daroussin.Fn pw_tmp .
15342818163SBaptiste DaroussinIf the file was modified,
15442818163SBaptiste Daroussin.Fn pw_edit
15542818163SBaptiste Daroussininstalls it and regenerates the password database.
15642818163SBaptiste DaroussinThe
15742818163SBaptiste Daroussin.Fn pw_edit
15842818163SBaptiste Daroussinfunction returns -1 in case of failure, 0 if the file was not modified,
15942818163SBaptiste Daroussinand a non-zero positive number if the file was modified and successfully
16042818163SBaptiste Daroussininstalled.
16142818163SBaptiste Daroussin.Pp
16242818163SBaptiste DaroussinThe
16342818163SBaptiste Daroussin.Fn pw_equal
16442818163SBaptiste Daroussinfunction compares two
16542818163SBaptiste Daroussin.Vt struct passwd
16642818163SBaptiste Daroussinand returns 0 if they are equal.
16742818163SBaptiste Daroussin.Pp
16842818163SBaptiste DaroussinThe
16942818163SBaptiste Daroussin.Fn pw_fini
17042818163SBaptiste Daroussinfunction destroy the temporary file created by
17142818163SBaptiste Daroussin.Fn pw_tmp
17242818163SBaptiste Daroussinif any,
17342818163SBaptiste Daroussinkills any running instance of
17442818163SBaptiste Daroussin.Ev EDITOR
1752acb4741SJoel Dahlexecuted by
17642818163SBaptiste Daroussin.Fn pw_edit
17742818163SBaptiste Daroussinif any,
17842818163SBaptiste Daroussinand closes the lock created by
17942818163SBaptiste Daroussin.Fn pw_lock
18042818163SBaptiste Daroussinif any.
18142818163SBaptiste Daroussin.Pp
18242818163SBaptiste DaroussinThe
18342818163SBaptiste Daroussin.Fn pw_init
18434e9190dSIan Leporeinitializes the static variable representing the path to a password file.
18542818163SBaptiste Daroussin.Fa dir
18642818163SBaptiste Daroussinis the directory where the password file is located.
18742818163SBaptiste DaroussinIf set to
18842818163SBaptiste Daroussin.Dv NULL ,
18942818163SBaptiste Daroussinit will default to
19042818163SBaptiste Daroussin.Pa /etc .
19142818163SBaptiste Daroussin.Fa master
19242818163SBaptiste Daroussinis the name of the password file.
19342818163SBaptiste DaroussinIf set to
19442818163SBaptiste Daroussin.Dv NULL?
19542818163SBaptiste Daroussinit will default to
19642818163SBaptiste Daroussin.Pa master.passwd
19742818163SBaptiste Daroussin.Pp
19842818163SBaptiste DaroussinThe
19934e9190dSIan Lepore.Fn pw_initpwd
20034e9190dSIan Leporefunction initializes the
20134e9190dSIan Lepore.Vt passwd
20234e9190dSIan Leporestruct to canonical values.
20334e9190dSIan LeporeThe entire structure is zeroed, then
20434e9190dSIan Lepore.Va pw_uid
20534e9190dSIan Leporeand
20634e9190dSIan Lepore.Va pw_gid
20734e9190dSIan Leporeare set to -1, and all string pointers are set to point at
20834e9190dSIan Leporean internally-defined zero-length string.
20934e9190dSIan Lepore.Pp
21034e9190dSIan LeporeThe
21142818163SBaptiste Daroussin.Fn pw_make
21242818163SBaptiste Daroussinfunction creates a properly formatted
21342818163SBaptiste Daroussin.Bx
21442818163SBaptiste Daroussin.Xr passwd 5
21542818163SBaptiste Daroussinline from a
21642818163SBaptiste Daroussin.Vt struct passwd ,
21742818163SBaptiste Daroussinand returns a pointer to the resulting string.
21842818163SBaptiste DaroussinThe string is allocated with
21942818163SBaptiste Daroussin.Xr malloc 3 ,
22042818163SBaptiste Daroussinand it is the caller's responsibility to free it with
22142818163SBaptiste Daroussin.Xr free 3 .
22242818163SBaptiste Daroussin.Pp
22342818163SBaptiste DaroussinThe
22442818163SBaptiste Daroussin.Fn pw_make_v7
22542818163SBaptiste Daroussinfunction creates a properly formatted
22642818163SBaptiste Daroussin.Ux V7
22742818163SBaptiste Daroussin.Xr passwd 5
22842818163SBaptiste Daroussinline from a
22942818163SBaptiste Daroussin.Vt struct passwd ,
23042818163SBaptiste Daroussinand returns a pointer to the resulting string.
23142818163SBaptiste DaroussinThe string is allocated with
23242818163SBaptiste Daroussin.Xr malloc 3 ,
23342818163SBaptiste Daroussinand it is the caller's responsibility to free it with
23442818163SBaptiste Daroussin.Xr free 3 .
23542818163SBaptiste Daroussin.Pp
23642818163SBaptiste DaroussinThe
23742818163SBaptiste Daroussin.Fn pw_mkdb
23842818163SBaptiste Daroussinfunction regenerates the password database by running
239e2835957SBaptiste Daroussin.Xr pwd_mkdb 8 .
24042818163SBaptiste DaroussinIf
24142818163SBaptiste Daroussin.Fa user
242c24c3080SBaptiste Daroussinonly the record corresponding to that user will be updated.
24342818163SBaptiste DaroussinThe
24442818163SBaptiste Daroussin.Fn pw_mkdb
24542818163SBaptiste Daroussinfunction returns 0 in case of success and -1 in case of failure.
24642818163SBaptiste Daroussin.Pp
24742818163SBaptiste DaroussinThe
24842818163SBaptiste Daroussin.Fn pw_lock
24942818163SBaptiste Daroussinfunction locks the master password file.
2506c5f7b23SRenato BotelhoIt returns a file descriptor to the master password file on success
2516c5f7b23SRenato Botelhoand -1 on failure.
25242818163SBaptiste Daroussin.Pp
25342818163SBaptiste DaroussinThe
25442818163SBaptiste Daroussin.Fn pw_scan
25542818163SBaptiste Daroussinfunction is a wrapper around the internal libc function
25642818163SBaptiste Daroussin.Fn __pw_scan .
25742818163SBaptiste DaroussinIt scans the master password file for a line corresponding to the
25842818163SBaptiste Daroussin.Fa line
25942818163SBaptiste Daroussinprovided and return a
26042818163SBaptiste Daroussin.Vt struct passwd
26142818163SBaptiste Daroussinif it matched an existing record.
26242818163SBaptiste DaroussinIn case of failure, it returns
26342818163SBaptiste Daroussin.Dv NULL .
26442818163SBaptiste DaroussinOtherwise, it returns a pointer to a
26542818163SBaptiste Daroussin.Vt struct passwd
26642818163SBaptiste Daroussincontaining the matching record.
26742818163SBaptiste DaroussinThe
26842818163SBaptiste Daroussin.Vt struct passwd
26942818163SBaptiste Daroussinis allocated with
27042818163SBaptiste Daroussin.Xr malloc 3 ,
27142818163SBaptiste Daroussinand it is the caller's responsibility to free it with
27242818163SBaptiste Daroussin.Xr free 3 .
27342818163SBaptiste Daroussin.Pp
27442818163SBaptiste DaroussinThe
27542818163SBaptiste Daroussin.Fn pw_tempname
27642818163SBaptiste Daroussinfunction returns the temporary name of the masterfile created via
27742818163SBaptiste Daroussin.Fn pw_tmp .
27842818163SBaptiste Daroussin.Pp
27942818163SBaptiste DaroussinThe
28042818163SBaptiste Daroussin.Fn pw_tmp
28142818163SBaptiste Daroussincreates and opens a presumably safe temporary password file.
28242818163SBaptiste DaroussinIf
28342818163SBaptiste Daroussin.Fa mfd
28442818163SBaptiste Daroussinis a file descriptor to an open password file, it will be read and
28542818163SBaptiste Daroussinwritten back to the temporary password file.
28642818163SBaptiste DaroussinOtherwise if should be set -1.
28742818163SBaptiste DaroussinThe
28842818163SBaptiste Daroussin.Fn pw_tmp
28942818163SBaptiste Daroussinreturns an open file descriptor to the temporary password file or -1 in case of
29042818163SBaptiste Daroussinfailure.
291*bc89b58dSGordon Bergling.Sh HISTORY
292*bc89b58dSGordon BerglingThe functions for passwd file handling first appeared in
293*bc89b58dSGordon Bergling.Bx 4.4 .
29442818163SBaptiste Daroussin.Sh AUTHORS
295192bc3edSDag-Erling SmørgravPortions of this software were developed for the
296192bc3edSDag-Erling Smørgrav.Fx
297192bc3edSDag-Erling SmørgravProject by ThinkSec AS and Network Associates Laboratories, the
298192bc3edSDag-Erling SmørgravSecurity Research Division of Network Associates, Inc.\& under
299192bc3edSDag-Erling SmørgravDARPA/SPAWAR contract N66001-01-C-8035
300192bc3edSDag-Erling Smørgrav.Pq Dq CBOSS ,
301192bc3edSDag-Erling Smørgravas part of the DARPA CHATS research program.
302192bc3edSDag-Erling Smørgrav.Pp
30342818163SBaptiste DaroussinThis manual page was written by
3042b7af31cSBaptiste Daroussin.An Baptiste Daroussin Aq Mt bapt@FreeBSD.org .
305