xref: /f-stack/tools/libutil/pw_util.3 (revision 22ce4aff)
11eaf0ac3Slogwang.\" Copyright (c) 2012 Baptiste Daroussin <[email protected]>
21eaf0ac3Slogwang.\" All rights reserved.
31eaf0ac3Slogwang.\"
41eaf0ac3Slogwang.\" Redistribution and use in source and binary forms, with or without
51eaf0ac3Slogwang.\" modification, are permitted provided that the following conditions
61eaf0ac3Slogwang.\" are met:
71eaf0ac3Slogwang.\" 1. Redistributions of source code must retain the above copyright
81eaf0ac3Slogwang.\"    notice, this list of conditions and the following disclaimer.
91eaf0ac3Slogwang.\" 2. Redistributions in binary form must reproduce the above copyright
101eaf0ac3Slogwang.\"    notice, this list of conditions and the following disclaimer in the
111eaf0ac3Slogwang.\"    documentation and/or other materials provided with the distribution.
121eaf0ac3Slogwang.\"
131eaf0ac3Slogwang.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
141eaf0ac3Slogwang.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
151eaf0ac3Slogwang.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
161eaf0ac3Slogwang.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
171eaf0ac3Slogwang.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
181eaf0ac3Slogwang.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
191eaf0ac3Slogwang.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
201eaf0ac3Slogwang.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
211eaf0ac3Slogwang.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
221eaf0ac3Slogwang.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
231eaf0ac3Slogwang.\" SUCH DAMAGE.
241eaf0ac3Slogwang.\"
251eaf0ac3Slogwang.\" $FreeBSD$
261eaf0ac3Slogwang.\"
27*22ce4affSfengbojiang.Dd May 10, 2020
281eaf0ac3Slogwang.Dt PW_UTIL 3
291eaf0ac3Slogwang.Os
301eaf0ac3Slogwang.Sh NAME
311eaf0ac3Slogwang.Nm pw_copy ,
321eaf0ac3Slogwang.Nm pw_dup ,
331eaf0ac3Slogwang.Nm pw_edit ,
341eaf0ac3Slogwang.Nm pw_equal ,
351eaf0ac3Slogwang.Nm pw_fini ,
361eaf0ac3Slogwang.Nm pw_init ,
371eaf0ac3Slogwang.Nm pw_make ,
381eaf0ac3Slogwang.Nm pw_make_v7 ,
391eaf0ac3Slogwang.Nm pw_mkdb ,
401eaf0ac3Slogwang.Nm pw_lock ,
411eaf0ac3Slogwang.Nm pw_scan ,
421eaf0ac3Slogwang.Nm pw_tempname ,
431eaf0ac3Slogwang.Nm pw_tmp
441eaf0ac3Slogwang.Nd "functions for passwd file handling"
451eaf0ac3Slogwang.Sh LIBRARY
461eaf0ac3Slogwang.Lb libutil
471eaf0ac3Slogwang.Sh SYNOPSIS
481eaf0ac3Slogwang.In pwd.h
491eaf0ac3Slogwang.In libutil.h
501eaf0ac3Slogwang.Ft int
511eaf0ac3Slogwang.Fn pw_copy "int ffd" "int tfd" "const struct passwd *pw" "struct passwd *oldpw"
521eaf0ac3Slogwang.Ft "struct passwd *"
531eaf0ac3Slogwang.Fn pw_dup "const struct passwd *pw"
541eaf0ac3Slogwang.Ft int
551eaf0ac3Slogwang.Fn pw_edit "int nosetuid"
561eaf0ac3Slogwang.Ft int
571eaf0ac3Slogwang.Fn pw_equal "const struct passwd *pw1" "const struct passwd *pw2"
581eaf0ac3Slogwang.Ft void
591eaf0ac3Slogwang.Fn pw_fini "void"
601eaf0ac3Slogwang.Ft int
61*22ce4affSfengbojiang.Fn pw_init "const char *dir" "const char *master"
62*22ce4affSfengbojiang.Ft void
63*22ce4affSfengbojiang.Fn pw_initpwd "struct passwd *pw"
641eaf0ac3Slogwang.Ft "char *"
651eaf0ac3Slogwang.Fn pw_make "const struct passwd *pw"
661eaf0ac3Slogwang.Ft "char *"
671eaf0ac3Slogwang.Fn pw_make_v7 "const struct passwd *pw"
681eaf0ac3Slogwang.Ft int
691eaf0ac3Slogwang.Fn pw_mkdb "const char *user"
701eaf0ac3Slogwang.Ft int
711eaf0ac3Slogwang.Fn pw_lock "void"
721eaf0ac3Slogwang.Ft "struct passwd *"
731eaf0ac3Slogwang.Fn pw_scan "const char *line" "int flags"
741eaf0ac3Slogwang.Ft "const char *"
751eaf0ac3Slogwang.Fn pw_tempname "void"
761eaf0ac3Slogwang.Ft int
771eaf0ac3Slogwang.Fn pw_tmp "int mfd"
781eaf0ac3Slogwang.Sh DESCRIPTION
791eaf0ac3SlogwangThe
801eaf0ac3Slogwang.Fn pw_copy
811eaf0ac3Slogwangfunction reads a password file from
821eaf0ac3Slogwang.Vt ffd
831eaf0ac3Slogwangand writes it back out to
841eaf0ac3Slogwang.Vt tfd
851eaf0ac3Slogwangpossibly with modifications:
861eaf0ac3Slogwang.Bl -dash
871eaf0ac3Slogwang.It
881eaf0ac3SlogwangIf
891eaf0ac3Slogwang.Fa pw
901eaf0ac3Slogwangis
911eaf0ac3Slogwang.Dv NULL
921eaf0ac3Slogwangand
931eaf0ac3Slogwang.Fa oldpw
941eaf0ac3Slogwangis not
951eaf0ac3Slogwang.Dv NULL ,
961eaf0ac3Slogwangthen the record represented by
971eaf0ac3Slogwang.Fa oldpw
981eaf0ac3Slogwangwill not be copied (corresponding to user deletion).
991eaf0ac3Slogwang.It
1001eaf0ac3SlogwangIf
1011eaf0ac3Slogwang.Fa pw
1021eaf0ac3Slogwangand
1031eaf0ac3Slogwang.Fa oldpw
1041eaf0ac3Slogwangare not
1051eaf0ac3Slogwang.Dv NULL
1061eaf0ac3Slogwangthen the record corresponding to
1071eaf0ac3Slogwang.Fa pw
1081eaf0ac3Slogwangwill be replaced by the record corresponding to
1091eaf0ac3Slogwang.Fa oldpw .
1101eaf0ac3Slogwang.It
1111eaf0ac3SlogwangIf
1121eaf0ac3Slogwang.Vt pw
1131eaf0ac3Slogwangis set and
1141eaf0ac3Slogwang.Vt oldpw
1151eaf0ac3Slogwangis
1161eaf0ac3Slogwang.Dv NULL
1171eaf0ac3Slogwangthen the record corresponding to
1181eaf0ac3Slogwang.Vt pw
1191eaf0ac3Slogwangwill be appended (corresponding to user addition).
1201eaf0ac3Slogwang.El
1211eaf0ac3Slogwang.Pp
1221eaf0ac3SlogwangThe
1231eaf0ac3Slogwang.Fn pw_copy
1241eaf0ac3Slogwangfunction returns -1 in case of failure otherwise 0.
1251eaf0ac3Slogwang.Pp
1261eaf0ac3SlogwangThe
1271eaf0ac3Slogwang.Fn pw_dup
1281eaf0ac3Slogwangfunction duplicates the
1291eaf0ac3Slogwang.Vt struct passwd
1301eaf0ac3Slogwangpointed to by
1311eaf0ac3Slogwang.Fa pw
1321eaf0ac3Slogwangand returns a pointer to the copy, or
1331eaf0ac3Slogwang.Dv NULL
1341eaf0ac3Slogwangin case of failure.
1351eaf0ac3SlogwangThe new
1361eaf0ac3Slogwang.Vt struct passwd
1371eaf0ac3Slogwangis allocated with
1381eaf0ac3Slogwang.Xr malloc 3 ,
1391eaf0ac3Slogwangand it is the caller's responsibility to free it with
1401eaf0ac3Slogwang.Xr free 3 .
1411eaf0ac3Slogwang.Pp
1421eaf0ac3SlogwangThe
1431eaf0ac3Slogwang.Fn pw_edit
1441eaf0ac3Slogwangfunction invokes the command specified by the
1451eaf0ac3Slogwang.Ev EDITOR
1461eaf0ac3Slogwangenvironment variable (or
1471eaf0ac3Slogwang.Pa /usr/bin/vi
1481eaf0ac3Slogwangif
1491eaf0ac3Slogwang.Ev EDITOR
1501eaf0ac3Slogwangis not defined)
1511eaf0ac3Slogwangon a temporary copy of the master password file created by
1521eaf0ac3Slogwang.Fn pw_tmp .
1531eaf0ac3SlogwangIf the file was modified,
1541eaf0ac3Slogwang.Fn pw_edit
1551eaf0ac3Slogwanginstalls it and regenerates the password database.
1561eaf0ac3SlogwangThe
1571eaf0ac3Slogwang.Fn pw_edit
1581eaf0ac3Slogwangfunction returns -1 in case of failure, 0 if the file was not modified,
1591eaf0ac3Slogwangand a non-zero positive number if the file was modified and successfully
1601eaf0ac3Slogwanginstalled.
1611eaf0ac3Slogwang.Pp
1621eaf0ac3SlogwangThe
1631eaf0ac3Slogwang.Fn pw_equal
1641eaf0ac3Slogwangfunction compares two
1651eaf0ac3Slogwang.Vt struct passwd
1661eaf0ac3Slogwangand returns 0 if they are equal.
1671eaf0ac3Slogwang.Pp
1681eaf0ac3SlogwangThe
1691eaf0ac3Slogwang.Fn pw_fini
1701eaf0ac3Slogwangfunction destroy the temporary file created by
1711eaf0ac3Slogwang.Fn pw_tmp
1721eaf0ac3Slogwangif any,
1731eaf0ac3Slogwangkills any running instance of
1741eaf0ac3Slogwang.Ev EDITOR
1751eaf0ac3Slogwangexecuted by
1761eaf0ac3Slogwang.Fn pw_edit
1771eaf0ac3Slogwangif any,
1781eaf0ac3Slogwangand closes the lock created by
1791eaf0ac3Slogwang.Fn pw_lock
1801eaf0ac3Slogwangif any.
1811eaf0ac3Slogwang.Pp
1821eaf0ac3SlogwangThe
1831eaf0ac3Slogwang.Fn pw_init
184*22ce4affSfengbojianginitializes the static variable representing the path to a password file.
1851eaf0ac3Slogwang.Fa dir
1861eaf0ac3Slogwangis the directory where the password file is located.
1871eaf0ac3SlogwangIf set to
1881eaf0ac3Slogwang.Dv NULL ,
1891eaf0ac3Slogwangit will default to
1901eaf0ac3Slogwang.Pa /etc .
1911eaf0ac3Slogwang.Fa master
1921eaf0ac3Slogwangis the name of the password file.
1931eaf0ac3SlogwangIf set to
1941eaf0ac3Slogwang.Dv NULL?
1951eaf0ac3Slogwangit will default to
1961eaf0ac3Slogwang.Pa master.passwd
1971eaf0ac3Slogwang.Pp
1981eaf0ac3SlogwangThe
199*22ce4affSfengbojiang.Fn pw_initpwd
200*22ce4affSfengbojiangfunction initializes the
201*22ce4affSfengbojiang.Vt passwd
202*22ce4affSfengbojiangstruct to canonical values.
203*22ce4affSfengbojiangThe entire structure is zeroed, then
204*22ce4affSfengbojiang.Va pw_uid
205*22ce4affSfengbojiangand
206*22ce4affSfengbojiang.Va pw_gid
207*22ce4affSfengbojiangare set to -1, and all string pointers are set to point at
208*22ce4affSfengbojiangan internally-defined zero-length string.
209*22ce4affSfengbojiang.Pp
210*22ce4affSfengbojiangThe
2111eaf0ac3Slogwang.Fn pw_make
2121eaf0ac3Slogwangfunction creates a properly formatted
2131eaf0ac3Slogwang.Bx
2141eaf0ac3Slogwang.Xr passwd 5
2151eaf0ac3Slogwangline from a
2161eaf0ac3Slogwang.Vt struct passwd ,
2171eaf0ac3Slogwangand returns a pointer to the resulting string.
2181eaf0ac3SlogwangThe string is allocated with
2191eaf0ac3Slogwang.Xr malloc 3 ,
2201eaf0ac3Slogwangand it is the caller's responsibility to free it with
2211eaf0ac3Slogwang.Xr free 3 .
2221eaf0ac3Slogwang.Pp
2231eaf0ac3SlogwangThe
2241eaf0ac3Slogwang.Fn pw_make_v7
2251eaf0ac3Slogwangfunction creates a properly formatted
2261eaf0ac3Slogwang.Ux V7
2271eaf0ac3Slogwang.Xr passwd 5
2281eaf0ac3Slogwangline from a
2291eaf0ac3Slogwang.Vt struct passwd ,
2301eaf0ac3Slogwangand returns a pointer to the resulting string.
2311eaf0ac3SlogwangThe string is allocated with
2321eaf0ac3Slogwang.Xr malloc 3 ,
2331eaf0ac3Slogwangand it is the caller's responsibility to free it with
2341eaf0ac3Slogwang.Xr free 3 .
2351eaf0ac3Slogwang.Pp
2361eaf0ac3SlogwangThe
2371eaf0ac3Slogwang.Fn pw_mkdb
2381eaf0ac3Slogwangfunction regenerates the password database by running
2391eaf0ac3Slogwang.Xr pwd_mkdb 8 .
2401eaf0ac3SlogwangIf
2411eaf0ac3Slogwang.Fa user
2421eaf0ac3Slogwangonly the record corresponding to that user will be updated.
2431eaf0ac3SlogwangThe
2441eaf0ac3Slogwang.Fn pw_mkdb
2451eaf0ac3Slogwangfunction returns 0 in case of success and -1 in case of failure.
2461eaf0ac3Slogwang.Pp
2471eaf0ac3SlogwangThe
2481eaf0ac3Slogwang.Fn pw_lock
2491eaf0ac3Slogwangfunction locks the master password file.
2501eaf0ac3SlogwangIt returns a file descriptor to the master password file on success
2511eaf0ac3Slogwangand -1 on failure.
2521eaf0ac3Slogwang.Pp
2531eaf0ac3SlogwangThe
2541eaf0ac3Slogwang.Fn pw_scan
2551eaf0ac3Slogwangfunction is a wrapper around the internal libc function
2561eaf0ac3Slogwang.Fn __pw_scan .
2571eaf0ac3SlogwangIt scans the master password file for a line corresponding to the
2581eaf0ac3Slogwang.Fa line
2591eaf0ac3Slogwangprovided and return a
2601eaf0ac3Slogwang.Vt struct passwd
2611eaf0ac3Slogwangif it matched an existing record.
2621eaf0ac3SlogwangIn case of failure, it returns
2631eaf0ac3Slogwang.Dv NULL .
2641eaf0ac3SlogwangOtherwise, it returns a pointer to a
2651eaf0ac3Slogwang.Vt struct passwd
2661eaf0ac3Slogwangcontaining the matching record.
2671eaf0ac3SlogwangThe
2681eaf0ac3Slogwang.Vt struct passwd
2691eaf0ac3Slogwangis allocated with
2701eaf0ac3Slogwang.Xr malloc 3 ,
2711eaf0ac3Slogwangand it is the caller's responsibility to free it with
2721eaf0ac3Slogwang.Xr free 3 .
2731eaf0ac3Slogwang.Pp
2741eaf0ac3SlogwangThe
2751eaf0ac3Slogwang.Fn pw_tempname
2761eaf0ac3Slogwangfunction returns the temporary name of the masterfile created via
2771eaf0ac3Slogwang.Fn pw_tmp .
2781eaf0ac3Slogwang.Pp
2791eaf0ac3SlogwangThe
2801eaf0ac3Slogwang.Fn pw_tmp
2811eaf0ac3Slogwangcreates and opens a presumably safe temporary password file.
2821eaf0ac3SlogwangIf
2831eaf0ac3Slogwang.Fa mfd
2841eaf0ac3Slogwangis a file descriptor to an open password file, it will be read and
2851eaf0ac3Slogwangwritten back to the temporary password file.
2861eaf0ac3SlogwangOtherwise if should be set -1.
2871eaf0ac3SlogwangThe
2881eaf0ac3Slogwang.Fn pw_tmp
2891eaf0ac3Slogwangreturns an open file descriptor to the temporary password file or -1 in case of
2901eaf0ac3Slogwangfailure.
291*22ce4affSfengbojiang.Sh HISTORY
292*22ce4affSfengbojiangThe functions for passwd file handling first appeared in
293*22ce4affSfengbojiang.Bx 4.4 .
2941eaf0ac3Slogwang.Sh AUTHORS
2951eaf0ac3SlogwangPortions of this software were developed for the
2961eaf0ac3Slogwang.Fx
2971eaf0ac3SlogwangProject by ThinkSec AS and Network Associates Laboratories, the
2981eaf0ac3SlogwangSecurity Research Division of Network Associates, Inc.\& under
2991eaf0ac3SlogwangDARPA/SPAWAR contract N66001-01-C-8035
3001eaf0ac3Slogwang.Pq Dq CBOSS ,
3011eaf0ac3Slogwangas part of the DARPA CHATS research program.
3021eaf0ac3Slogwang.Pp
3031eaf0ac3SlogwangThis manual page was written by
3041eaf0ac3Slogwang.An Baptiste Daroussin Aq Mt [email protected] .
305