xref: /freebsd/lib/libutil/pw_util.3 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
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.\"
25*bc89b58dSGordon Bergling.Dd May 10, 2020
2642818163SBaptiste Daroussin.Dt PW_UTIL 3
2742818163SBaptiste Daroussin.Os
2842818163SBaptiste Daroussin.Sh NAME
2942818163SBaptiste Daroussin.Nm pw_copy ,
3042818163SBaptiste Daroussin.Nm pw_dup ,
3142818163SBaptiste Daroussin.Nm pw_edit ,
3242818163SBaptiste Daroussin.Nm pw_equal ,
3342818163SBaptiste Daroussin.Nm pw_fini ,
3442818163SBaptiste Daroussin.Nm pw_init ,
3542818163SBaptiste Daroussin.Nm pw_make ,
3642818163SBaptiste Daroussin.Nm pw_make_v7 ,
3742818163SBaptiste Daroussin.Nm pw_mkdb ,
3842818163SBaptiste Daroussin.Nm pw_lock ,
3942818163SBaptiste Daroussin.Nm pw_scan ,
4042818163SBaptiste Daroussin.Nm pw_tempname ,
4142818163SBaptiste Daroussin.Nm pw_tmp
4242818163SBaptiste Daroussin.Nd "functions for passwd file handling"
4342818163SBaptiste Daroussin.Sh LIBRARY
4442818163SBaptiste Daroussin.Lb libutil
4542818163SBaptiste Daroussin.Sh SYNOPSIS
4642818163SBaptiste Daroussin.In pwd.h
4742818163SBaptiste Daroussin.In libutil.h
4842818163SBaptiste Daroussin.Ft int
4917483344SEitan Adler.Fn pw_copy "int ffd" "int tfd" "const struct passwd *pw" "struct passwd *oldpw"
5042818163SBaptiste Daroussin.Ft "struct passwd *"
5142818163SBaptiste Daroussin.Fn pw_dup "const struct passwd *pw"
5242818163SBaptiste Daroussin.Ft int
5342818163SBaptiste Daroussin.Fn pw_edit "int nosetuid"
5442818163SBaptiste Daroussin.Ft int
5517483344SEitan Adler.Fn pw_equal "const struct passwd *pw1" "const struct passwd *pw2"
5642818163SBaptiste Daroussin.Ft void
5742818163SBaptiste Daroussin.Fn pw_fini "void"
5842818163SBaptiste Daroussin.Ft int
598e7299c0SMark Johnston.Fn pw_init "const char *dir" "const char *master"
6034e9190dSIan Lepore.Ft void
6134e9190dSIan Lepore.Fn pw_initpwd "struct passwd *pw"
6242818163SBaptiste Daroussin.Ft "char *"
6342818163SBaptiste Daroussin.Fn pw_make "const struct passwd *pw"
6442818163SBaptiste Daroussin.Ft "char *"
6542818163SBaptiste Daroussin.Fn pw_make_v7 "const struct passwd *pw"
6642818163SBaptiste Daroussin.Ft int
6742818163SBaptiste Daroussin.Fn pw_mkdb "const char *user"
6842818163SBaptiste Daroussin.Ft int
6942818163SBaptiste Daroussin.Fn pw_lock "void"
7042818163SBaptiste Daroussin.Ft "struct passwd *"
7142818163SBaptiste Daroussin.Fn pw_scan "const char *line" "int flags"
7242818163SBaptiste Daroussin.Ft "const char *"
7342818163SBaptiste Daroussin.Fn pw_tempname "void"
7442818163SBaptiste Daroussin.Ft int
7542818163SBaptiste Daroussin.Fn pw_tmp "int mfd"
7642818163SBaptiste Daroussin.Sh DESCRIPTION
7742818163SBaptiste DaroussinThe
7842818163SBaptiste Daroussin.Fn pw_copy
7942818163SBaptiste Daroussinfunction reads a password file from
8042818163SBaptiste Daroussin.Vt ffd
8142818163SBaptiste Daroussinand writes it back out to
8242818163SBaptiste Daroussin.Vt tfd
8342818163SBaptiste Daroussinpossibly with modifications:
8442818163SBaptiste Daroussin.Bl -dash
8542818163SBaptiste Daroussin.It
8642818163SBaptiste DaroussinIf
8742818163SBaptiste Daroussin.Fa pw
8842818163SBaptiste Daroussinis
8942818163SBaptiste Daroussin.Dv NULL
9042818163SBaptiste Daroussinand
9142818163SBaptiste Daroussin.Fa oldpw
9242818163SBaptiste Daroussinis not
9342818163SBaptiste Daroussin.Dv NULL ,
9442818163SBaptiste Daroussinthen the record represented by
9542818163SBaptiste Daroussin.Fa oldpw
9642818163SBaptiste Daroussinwill not be copied (corresponding to user deletion).
9742818163SBaptiste Daroussin.It
9842818163SBaptiste DaroussinIf
9942818163SBaptiste Daroussin.Fa pw
10042818163SBaptiste Daroussinand
10142818163SBaptiste Daroussin.Fa oldpw
10242818163SBaptiste Daroussinare not
10342818163SBaptiste Daroussin.Dv NULL
10442818163SBaptiste Daroussinthen the record corresponding to
10542818163SBaptiste Daroussin.Fa pw
1062acb4741SJoel Dahlwill be replaced by the record corresponding to
10742818163SBaptiste Daroussin.Fa oldpw .
10842818163SBaptiste Daroussin.It
10942818163SBaptiste DaroussinIf
11042818163SBaptiste Daroussin.Vt pw
11142818163SBaptiste Daroussinis set and
11242818163SBaptiste Daroussin.Vt oldpw
11342818163SBaptiste Daroussinis
11442818163SBaptiste Daroussin.Dv NULL
11542818163SBaptiste Daroussinthen the record corresponding to
11642818163SBaptiste Daroussin.Vt pw
11742818163SBaptiste Daroussinwill be appended (corresponding to user addition).
11842818163SBaptiste Daroussin.El
11942818163SBaptiste Daroussin.Pp
12042818163SBaptiste DaroussinThe
12142818163SBaptiste Daroussin.Fn pw_copy
12242818163SBaptiste Daroussinfunction returns -1 in case of failure otherwise 0.
12342818163SBaptiste Daroussin.Pp
12442818163SBaptiste DaroussinThe
12542818163SBaptiste Daroussin.Fn pw_dup
12642818163SBaptiste Daroussinfunction duplicates the
12742818163SBaptiste Daroussin.Vt struct passwd
12842818163SBaptiste Daroussinpointed to by
12942818163SBaptiste Daroussin.Fa pw
13042818163SBaptiste Daroussinand returns a pointer to the copy, or
13142818163SBaptiste Daroussin.Dv NULL
13242818163SBaptiste Daroussinin case of failure.
13342818163SBaptiste DaroussinThe new
13442818163SBaptiste Daroussin.Vt struct passwd
13542818163SBaptiste Daroussinis allocated with
13642818163SBaptiste Daroussin.Xr malloc 3 ,
13742818163SBaptiste Daroussinand it is the caller's responsibility to free it with
13842818163SBaptiste Daroussin.Xr free 3 .
13942818163SBaptiste Daroussin.Pp
14042818163SBaptiste DaroussinThe
14142818163SBaptiste Daroussin.Fn pw_edit
14242818163SBaptiste Daroussinfunction invokes the command specified by the
14342818163SBaptiste Daroussin.Ev EDITOR
14442818163SBaptiste Daroussinenvironment variable (or
14542818163SBaptiste Daroussin.Pa /usr/bin/vi
14642818163SBaptiste Daroussinif
14742818163SBaptiste Daroussin.Ev EDITOR
14842818163SBaptiste Daroussinis not defined)
14942818163SBaptiste Daroussinon a temporary copy of the master password file created by
15042818163SBaptiste Daroussin.Fn pw_tmp .
15142818163SBaptiste DaroussinIf the file was modified,
15242818163SBaptiste Daroussin.Fn pw_edit
15342818163SBaptiste Daroussininstalls it and regenerates the password database.
15442818163SBaptiste DaroussinThe
15542818163SBaptiste Daroussin.Fn pw_edit
15642818163SBaptiste Daroussinfunction returns -1 in case of failure, 0 if the file was not modified,
15742818163SBaptiste Daroussinand a non-zero positive number if the file was modified and successfully
15842818163SBaptiste Daroussininstalled.
15942818163SBaptiste Daroussin.Pp
16042818163SBaptiste DaroussinThe
16142818163SBaptiste Daroussin.Fn pw_equal
16242818163SBaptiste Daroussinfunction compares two
16342818163SBaptiste Daroussin.Vt struct passwd
16442818163SBaptiste Daroussinand returns 0 if they are equal.
16542818163SBaptiste Daroussin.Pp
16642818163SBaptiste DaroussinThe
16742818163SBaptiste Daroussin.Fn pw_fini
16842818163SBaptiste Daroussinfunction destroy the temporary file created by
16942818163SBaptiste Daroussin.Fn pw_tmp
17042818163SBaptiste Daroussinif any,
17142818163SBaptiste Daroussinkills any running instance of
17242818163SBaptiste Daroussin.Ev EDITOR
1732acb4741SJoel Dahlexecuted by
17442818163SBaptiste Daroussin.Fn pw_edit
17542818163SBaptiste Daroussinif any,
17642818163SBaptiste Daroussinand closes the lock created by
17742818163SBaptiste Daroussin.Fn pw_lock
17842818163SBaptiste Daroussinif any.
17942818163SBaptiste Daroussin.Pp
18042818163SBaptiste DaroussinThe
18142818163SBaptiste Daroussin.Fn pw_init
18234e9190dSIan Leporeinitializes the static variable representing the path to a password file.
18342818163SBaptiste Daroussin.Fa dir
18442818163SBaptiste Daroussinis the directory where the password file is located.
18542818163SBaptiste DaroussinIf set to
18642818163SBaptiste Daroussin.Dv NULL ,
18742818163SBaptiste Daroussinit will default to
18842818163SBaptiste Daroussin.Pa /etc .
18942818163SBaptiste Daroussin.Fa master
19042818163SBaptiste Daroussinis the name of the password file.
19142818163SBaptiste DaroussinIf set to
19242818163SBaptiste Daroussin.Dv NULL ?
19342818163SBaptiste Daroussinit will default to
19442818163SBaptiste Daroussin.Pa master.passwd
19542818163SBaptiste Daroussin.Pp
19642818163SBaptiste DaroussinThe
19734e9190dSIan Lepore.Fn pw_initpwd
19834e9190dSIan Leporefunction initializes the
19934e9190dSIan Lepore.Vt passwd
20034e9190dSIan Leporestruct to canonical values.
20134e9190dSIan LeporeThe entire structure is zeroed, then
20234e9190dSIan Lepore.Va pw_uid
20334e9190dSIan Leporeand
20434e9190dSIan Lepore.Va pw_gid
20534e9190dSIan Leporeare set to -1, and all string pointers are set to point at
20634e9190dSIan Leporean internally-defined zero-length string.
20734e9190dSIan Lepore.Pp
20834e9190dSIan LeporeThe
20942818163SBaptiste Daroussin.Fn pw_make
21042818163SBaptiste Daroussinfunction creates a properly formatted
21142818163SBaptiste Daroussin.Bx
21242818163SBaptiste Daroussin.Xr passwd 5
21342818163SBaptiste Daroussinline from a
21442818163SBaptiste Daroussin.Vt struct passwd ,
21542818163SBaptiste Daroussinand returns a pointer to the resulting string.
21642818163SBaptiste DaroussinThe string is allocated with
21742818163SBaptiste Daroussin.Xr malloc 3 ,
21842818163SBaptiste Daroussinand it is the caller's responsibility to free it with
21942818163SBaptiste Daroussin.Xr free 3 .
22042818163SBaptiste Daroussin.Pp
22142818163SBaptiste DaroussinThe
22242818163SBaptiste Daroussin.Fn pw_make_v7
22342818163SBaptiste Daroussinfunction creates a properly formatted
22442818163SBaptiste Daroussin.Ux V7
22542818163SBaptiste Daroussin.Xr passwd 5
22642818163SBaptiste Daroussinline from a
22742818163SBaptiste Daroussin.Vt struct passwd ,
22842818163SBaptiste Daroussinand returns a pointer to the resulting string.
22942818163SBaptiste DaroussinThe string is allocated with
23042818163SBaptiste Daroussin.Xr malloc 3 ,
23142818163SBaptiste Daroussinand it is the caller's responsibility to free it with
23242818163SBaptiste Daroussin.Xr free 3 .
23342818163SBaptiste Daroussin.Pp
23442818163SBaptiste DaroussinThe
23542818163SBaptiste Daroussin.Fn pw_mkdb
23642818163SBaptiste Daroussinfunction regenerates the password database by running
237e2835957SBaptiste Daroussin.Xr pwd_mkdb 8 .
23842818163SBaptiste DaroussinIf
23942818163SBaptiste Daroussin.Fa user
240c24c3080SBaptiste Daroussinonly the record corresponding to that user will be updated.
24142818163SBaptiste DaroussinThe
24242818163SBaptiste Daroussin.Fn pw_mkdb
24342818163SBaptiste Daroussinfunction returns 0 in case of success and -1 in case of failure.
24442818163SBaptiste Daroussin.Pp
24542818163SBaptiste DaroussinThe
24642818163SBaptiste Daroussin.Fn pw_lock
24742818163SBaptiste Daroussinfunction locks the master password file.
2486c5f7b23SRenato BotelhoIt returns a file descriptor to the master password file on success
2496c5f7b23SRenato Botelhoand -1 on failure.
25042818163SBaptiste Daroussin.Pp
25142818163SBaptiste DaroussinThe
25242818163SBaptiste Daroussin.Fn pw_scan
25342818163SBaptiste Daroussinfunction is a wrapper around the internal libc function
25442818163SBaptiste Daroussin.Fn __pw_scan .
25542818163SBaptiste DaroussinIt scans the master password file for a line corresponding to the
25642818163SBaptiste Daroussin.Fa line
25742818163SBaptiste Daroussinprovided and return a
25842818163SBaptiste Daroussin.Vt struct passwd
25942818163SBaptiste Daroussinif it matched an existing record.
26042818163SBaptiste DaroussinIn case of failure, it returns
26142818163SBaptiste Daroussin.Dv NULL .
26242818163SBaptiste DaroussinOtherwise, it returns a pointer to a
26342818163SBaptiste Daroussin.Vt struct passwd
26442818163SBaptiste Daroussincontaining the matching record.
26542818163SBaptiste DaroussinThe
26642818163SBaptiste Daroussin.Vt struct passwd
26742818163SBaptiste Daroussinis allocated with
26842818163SBaptiste Daroussin.Xr malloc 3 ,
26942818163SBaptiste Daroussinand it is the caller's responsibility to free it with
27042818163SBaptiste Daroussin.Xr free 3 .
27142818163SBaptiste Daroussin.Pp
27242818163SBaptiste DaroussinThe
27342818163SBaptiste Daroussin.Fn pw_tempname
27442818163SBaptiste Daroussinfunction returns the temporary name of the masterfile created via
27542818163SBaptiste Daroussin.Fn pw_tmp .
27642818163SBaptiste Daroussin.Pp
27742818163SBaptiste DaroussinThe
27842818163SBaptiste Daroussin.Fn pw_tmp
27942818163SBaptiste Daroussincreates and opens a presumably safe temporary password file.
28042818163SBaptiste DaroussinIf
28142818163SBaptiste Daroussin.Fa mfd
28242818163SBaptiste Daroussinis a file descriptor to an open password file, it will be read and
28342818163SBaptiste Daroussinwritten back to the temporary password file.
28442818163SBaptiste DaroussinOtherwise if should be set -1.
28542818163SBaptiste DaroussinThe
28642818163SBaptiste Daroussin.Fn pw_tmp
28742818163SBaptiste Daroussinreturns an open file descriptor to the temporary password file or -1 in case of
28842818163SBaptiste Daroussinfailure.
289*bc89b58dSGordon Bergling.Sh HISTORY
290*bc89b58dSGordon BerglingThe functions for passwd file handling first appeared in
291*bc89b58dSGordon Bergling.Bx 4.4 .
29242818163SBaptiste Daroussin.Sh AUTHORS
293192bc3edSDag-Erling SmørgravPortions of this software were developed for the
294192bc3edSDag-Erling Smørgrav.Fx
295192bc3edSDag-Erling SmørgravProject by ThinkSec AS and Network Associates Laboratories, the
296192bc3edSDag-Erling SmørgravSecurity Research Division of Network Associates, Inc.\& under
297192bc3edSDag-Erling SmørgravDARPA/SPAWAR contract N66001-01-C-8035
298192bc3edSDag-Erling Smørgrav.Pq Dq CBOSS ,
299192bc3edSDag-Erling Smørgravas part of the DARPA CHATS research program.
300192bc3edSDag-Erling Smørgrav.Pp
30142818163SBaptiste DaroussinThis manual page was written by
3022b7af31cSBaptiste Daroussin.An Baptiste Daroussin Aq Mt bapt@FreeBSD.org .
303