xref: /openbsd/lib/libutil/pw_init.3 (revision 09467b48)
1.\"	$OpenBSD: pw_init.3,v 1.14 2016/08/14 14:57:16 tb Exp $
2.\"
3.\" Copyright (c) 1995
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software developed by the Computer Systems
7.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
8.\" BG 91-66 and contributed to Berkeley.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.Dd $Mdocdate: August 14 2016 $
35.Dt PW_INIT 3
36.Os
37.Sh NAME
38.Nm pw_init ,
39.Nm pw_setdir ,
40.Nm pw_file ,
41.Nm pw_edit ,
42.Nm pw_prompt ,
43.Nm pw_copy ,
44.Nm pw_scan ,
45.Nm pw_error
46.Nd utility functions for interactive passwd file updates
47.Sh SYNOPSIS
48.In pwd.h
49.In util.h
50.Ft void
51.Fn pw_init void
52.Ft void
53.Fn pw_setdir "const char *directory"
54.Ft char *
55.Fn pw_file "const char *filename"
56.Ft void
57.Fn pw_edit "int notsetuid" "const char *filename"
58.Ft void
59.Fn pw_prompt void
60.Ft void
61.Fn pw_copy "int ffd" "int tfd" "const struct passwd *pw" "const struct passwd *opw"
62.Ft int
63.Fn pw_scan "char *bp" "struct passwd *pw" "int *flags"
64.Ft void
65.Fn pw_error "const char *name" "int err" "int eval"
66.Sh DESCRIPTION
67These functions are designed as conveniences for interactive programs
68which update the passwd file and do nothing else.
69They generally
70handle errors by printing out a message to the standard error stream
71and possibly aborting the process.
72.Pp
73The
74.Fn pw_init
75function prepares for a passwd update by unlimiting all resource
76constraints, disabling core dumps (thus preventing dumping the
77contents of the passwd database into a world-readable file), and
78disabling most signals.
79.Pp
80The
81.Fn pw_setdir
82function sets an alternative directory where the rest of the functions look
83for password-related files.
84Use this if you are writing utilities that should
85be able to handle password files outside of
86.Pa /etc .
87.Pp
88The
89.Fn pw_file
90function transforms filenames so that they end up in the directory specified
91to the latest
92.Fn pw_setdir
93call.
94The rule is that all directories are stripped of the given name and
95only the filename is appended to the directory.
96.Pp
97The
98.Fn pw_edit
99function runs an editor (named by the environment variable EDITOR, or
100.Pa /usr/bin/vi
101if EDITOR is not set) on the file
102.Fa filename
103(or
104.Pa /etc/ptmp
105if
106.Fa filename
107is NULL).
108If
109.Fa notsetuid
110is nonzero,
111.Fn pw_edit
112will set the effective user and group ID to the real user and group ID
113before running the editor.
114.Pp
115The
116.Fn pw_prompt
117function asks the user whether he or she wants to re-edit the password
118file; if the answer is no,
119.Fn pw_prompt
120deletes the lock file and exits the process.
121.Pp
122The
123.Fn pw_copy
124function reads a passwd file from
125.Fa ffd
126and writes it to
127.Fa tfd ,
128updating the entry corresponding to pw-\*(Gtpw_name
129with the information in
130.Fa pw .
131If
132.Fa opw
133is not NULL, opw-\*(Gtpw_name will be used for matching instead.
134Additionally, if the existing entry does not match
135.Fa opw ,
136the operation is aborted.
137The use of
138.Fa opw
139allows the caller to change the user name in an entry as well as
140guarantee that the entry being replaced has not changed in the
141meantime.
142.Pp
143The
144.Fn pw_scan
145function accepts in
146.Fa bp
147a passwd entry as it would be represented in
148.Pa /etc/master.passwd
149and fills in
150.Fa pw
151with corresponding values; string fields in
152.Fa pw
153will be pointers into
154.Fa bp .
155Some characters in
156.Fa bp
157will be overwritten with 0s in order to terminate the strings pointed
158to by
159.Fa pw .
160If
161.Fa flags
162is non-null, it is filled in with the following flags:
163.Bl -tag -width _PASSWORD_NOGIDxxx
164.It Dv _PASSWORD_NOUID
165The uid field of
166.Fa bp
167is empty.
168.It Dv _PASSWORD_NOGID
169The gid field of
170.Fa bp
171is empty.
172.It Dv _PASSWD_NOCHG
173The change field of
174.Fa bp
175is empty.
176.It Dv _PASSWD_NOEXP
177The expire field of
178.Fa bp
179is empty.
180.El
181.Pp
182The
183.Fn pw_error
184function displays an error message, aborts the current passwd update,
185and exits the current process.
186If
187.Fa err
188is non-zero, a warning message beginning with
189.Fa name
190is printed for the current value of
191.Va errno .
192The process exits with status
193.Fa eval .
194.Sh RETURN VALUES
195The
196.Fn pw_scan
197function prints a warning message and returns 0 if the string in the
198.Fa bp
199argument is not a valid passwd string.
200Otherwise,
201.Fn pw_scan
202returns 1.
203.Sh FILES
204.Bl -tag -width "/etc/master.passwdXXX" -compact
205.It Pa /etc/master.passwd
206Current password file.
207.It Pa /etc/passwd
208Legacy password file.
209.It Pa /etc/ptmp
210Password lock file.
211.It Pa /etc/pwd.db
212Insecure password database file.
213.It Pa /etc/spwd.db
214Secure password database file.
215.El
216.Sh SEE ALSO
217.Xr pw_lock 3 ,
218.Xr passwd 5
219