1.\" $NetBSD: pw_init.3,v 1.17 2022/12/04 11:16:39 uwe 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 August 1, 2004 35.Dt PW_INIT 3 36.Os 37.Sh NAME 38.Nm pw_init , 39.Nm pw_edit , 40.Nm pw_prompt , 41.Nm pw_copy , 42.Nm pw_copyx , 43.Nm pw_scan , 44.Nm pw_error 45.Nd utility functions for interactive passwd file updates 46.Sh LIBRARY 47.Lb libutil 48.Sh SYNOPSIS 49.In pwd.h 50.In util.h 51.Ft void 52.Fn pw_init "void" 53.Ft void 54.Fn pw_edit "int notsetuid" "const char *filename" 55.Ft void 56.Fn pw_prompt "void" 57.Ft void 58.Fn pw_copy "int ffd" "int tfd" "struct passwd *pw" "struct passwd *old_pw" 59.Ft int 60.Fn pw_copyx "int ffd" "int tfd" "struct passwd *pw" "struct passwd *old_pw" \ 61 "char *errbuf" "size_t errbufsz" 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 handle errors by printing out a message to the standard error 70stream and possibly aborting the process. 71.Pp 72The 73.Fn pw_init 74function prepares for a passwd update by unlimiting all resource 75constraints, disabling core dumps (thus preventing dumping the 76contents of the passwd database into a world-readable file), and 77disabling most signals. 78.Pp 79The 80.Fn pw_edit 81function runs an editor (named by the environment variable 82.Ev EDITOR , 83or 84.Pa /usr/bin/vi 85if 86.Ev EDITOR 87is not set) on the file 88.Fa filename 89(or 90.Pa /etc/ptmp 91if 92.Fa filename 93is 94.Dv NULL ) . 95If 96.Fa notsetuid 97is nonzero, 98.Fn pw_edit 99will set the effective user and group ID to the real user and group ID 100before running the editor. 101.Pp 102The 103.Fn pw_prompt 104function asks the user whether he or she wants to re-edit the password 105file; if the answer is no, 106.Fn pw_prompt 107deletes the lock file and exits the process. 108.Pp 109The 110.Fn pw_copy 111function reads a passwd file from 112.Fa ffd 113and writes it to 114.Fa tfd , 115updating the entry corresponding to pw->pw_name with the information 116in 117.Fa pw . 118If 119.Fa old_pw 120is not 121.Dv NULL , 122it checks to make sure the old entry is the same as 123the one described in 124.Fa old_pw 125or the process is aborted. 126If an entry is not found to match 127.Fa pw , 128a new entry is appended to the passwd file only if the real user 129ID is 0. 130If an error occurs, 131.Fn pw_copy 132will display a message on 133.Dv stderr 134and call 135.Fn pw_error . 136.Pp 137The 138.Fn pw_copyx 139function performs the same operation as 140.Fn pw_copy 141with the exception of error handling. 142Upon an error, 143.Fn pw_copyx 144will write an error message into the buffer pointed to by 145.Fa errbuf 146which has the size 147.Fa errbufsz . 148.Pp 149The 150.Fn pw_scan 151function accepts in 152.Fa bp 153a passwd entry as it would be represented in 154.Pa /etc/master.passwd 155and fills in 156.Fa pw 157with corresponding values; string fields in 158.Fa pw 159will be pointers into 160.Fa bp . 161Some characters in 162.Fa bp 163will be overwritten with 0s in order to terminate the strings pointed 164to by 165.Fa pw . 166If 167.Fa flags 168is non-null, it should be cleared and the following options 169enabled if required: 170.Bl -tag -offset indent -width _PASSWORD_OLDFMT 171.It Dv _PASSWORD_NOWARN 172Don't print warnings. 173.It Dv _PASSWORD_OLDFMT 174Parse 175.Fa bp 176as an old format entry as found in 177.Pa /etc/passwd . 178.El 179.Pp 180Upon return it is cleared, and filled in with the following flags: 181.Bl -tag -offset indent -width _PASSWORD_NOGID 182.It Dv _PASSWORD_NOUID 183The uid field of 184.Fa bp 185is empty. 186.It Dv _PASSWORD_NOGID 187The gid field of 188.Fa bp 189is empty. 190.It Dv _PASSWORD_NOCHG 191The change field of 192.Fa bp 193is empty. 194.It Dv _PASSWORD_NOEXP 195The expire field of 196.Fa bp 197is empty. 198.El 199.Pp 200The 201.Fn pw_error 202function displays an error message, aborts the current passwd update, 203and exits the current process. 204If 205.Fa err 206is non-zero, a warning message beginning with 207.Fa name 208is printed for the current value of 209.Va errno . 210The process exits with status 211.Fa eval . 212.Sh RETURN VALUES 213The 214.Fn pw_copyx 215function returns 1 if the new password entry was successfully written 216to the destination file, and 0 otherwise. 217.Pp 218The 219.Fn pw_scan 220function prints a warning message and returns 0 if the string in the 221.Fa bp 222argument is not a valid passwd string. 223Otherwise, 224.Fn pw_scan 225returns 1. 226.Sh FILES 227.Bl -tag -width /etc/master.passwd -compact 228.It Pa /etc/master.passwd 229.It Pa /etc/ptmp 230.El 231.Sh SEE ALSO 232.Xr pw_lock 3 , 233.Xr passwd 5 234