1f1d684faSDavid Nugent /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 31de7b4b8SPedro F. Giffuni * 4f1d684faSDavid Nugent * Copyright (C) 1996 5f1d684faSDavid Nugent * David L. Nugent. All rights reserved. 6f1d684faSDavid Nugent * 7f1d684faSDavid Nugent * Redistribution and use in source and binary forms, with or without 8f1d684faSDavid Nugent * modification, are permitted provided that the following conditions 9f1d684faSDavid Nugent * are met: 10f1d684faSDavid Nugent * 1. Redistributions of source code must retain the above copyright 11f1d684faSDavid Nugent * notice, this list of conditions and the following disclaimer. 12f1d684faSDavid Nugent * 2. Redistributions in binary form must reproduce the above copyright 13f1d684faSDavid Nugent * notice, this list of conditions and the following disclaimer in the 14f1d684faSDavid Nugent * documentation and/or other materials provided with the distribution. 15f1d684faSDavid Nugent * 16f1d684faSDavid Nugent * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 17f1d684faSDavid Nugent * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18f1d684faSDavid Nugent * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19f1d684faSDavid Nugent * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 20f1d684faSDavid Nugent * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21f1d684faSDavid Nugent * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22f1d684faSDavid Nugent * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23f1d684faSDavid Nugent * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24f1d684faSDavid Nugent * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25f1d684faSDavid Nugent * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26f1d684faSDavid Nugent * SUCH DAMAGE. 27f1d684faSDavid Nugent */ 28f1d684faSDavid Nugent 291dcc6ec7SPhilippe Charnier #ifndef lint 301dcc6ec7SPhilippe Charnier static const char rcsid[] = 3197d92980SPeter Wemm "$FreeBSD$"; 321dcc6ec7SPhilippe Charnier #endif /* not lint */ 331dcc6ec7SPhilippe Charnier 34f1d684faSDavid Nugent #include <sys/types.h> 35bcbdb01eSBaptiste Daroussin 36fb2db031SBaptiste Daroussin #include <err.h> 37fb2db031SBaptiste Daroussin #include <pwd.h> 38fb2db031SBaptiste Daroussin #include <libutil.h> 3907c4acccSAlan Somers #include <unistd.h> 40f1d684faSDavid Nugent 41f1d684faSDavid Nugent #include "pw.h" 42f1d684faSDavid Nugent 43f1d684faSDavid Nugent static int 44fb2db031SBaptiste Daroussin pw_nisupdate(const char * path, struct passwd * pwd, char const * user) 45f1d684faSDavid Nugent { 46fb2db031SBaptiste Daroussin int pfd, tfd; 47fb2db031SBaptiste Daroussin struct passwd *pw = NULL; 48fb2db031SBaptiste Daroussin struct passwd *old_pw = NULL; 49f1d684faSDavid Nugent 50d2d022b9SBaptiste Daroussin printf("===> %s\n", path); 51fb2db031SBaptiste Daroussin if (pwd != NULL) 52fb2db031SBaptiste Daroussin pw = pw_dup(pwd); 53fb2db031SBaptiste Daroussin 54fb2db031SBaptiste Daroussin if (user != NULL) 55fb2db031SBaptiste Daroussin old_pw = GETPWNAM(user); 56fb2db031SBaptiste Daroussin 57fb2db031SBaptiste Daroussin if (pw_init(NULL, path)) 58fb2db031SBaptiste Daroussin err(1,"pw_init()"); 59fb2db031SBaptiste Daroussin if ((pfd = pw_lock()) == -1) { 60fb2db031SBaptiste Daroussin pw_fini(); 61fb2db031SBaptiste Daroussin err(1, "pw_lock()"); 62fb2db031SBaptiste Daroussin } 63fb2db031SBaptiste Daroussin if ((tfd = pw_tmp(-1)) == -1) { 64fb2db031SBaptiste Daroussin pw_fini(); 65fb2db031SBaptiste Daroussin err(1, "pw_tmp()"); 66fb2db031SBaptiste Daroussin } 67fb2db031SBaptiste Daroussin if (pw_copy(pfd, tfd, pw, old_pw) == -1) { 68fb2db031SBaptiste Daroussin pw_fini(); 6907c4acccSAlan Somers close(tfd); 70fb2db031SBaptiste Daroussin err(1, "pw_copy()"); 71fb2db031SBaptiste Daroussin } 72cbaba16bSAlan Somers fsync(tfd); 7307c4acccSAlan Somers close(tfd); 744efe6c74SBaptiste Daroussin if (chmod(pw_tempname(), 0644) == -1) 754efe6c74SBaptiste Daroussin err(1, "chmod()"); 76fb2db031SBaptiste Daroussin if (rename(pw_tempname(), path) == -1) 77fb2db031SBaptiste Daroussin err(1, "rename()"); 78fb2db031SBaptiste Daroussin 79fb2db031SBaptiste Daroussin free(pw); 80fb2db031SBaptiste Daroussin pw_fini(); 81fb2db031SBaptiste Daroussin 82fb2db031SBaptiste Daroussin return (0); 83f1d684faSDavid Nugent } 84f1d684faSDavid Nugent 85f1d684faSDavid Nugent int 86f1d684faSDavid Nugent addnispwent(const char *path, struct passwd * pwd) 87f1d684faSDavid Nugent { 88fb2db031SBaptiste Daroussin return pw_nisupdate(path, pwd, NULL); 89f1d684faSDavid Nugent } 90f1d684faSDavid Nugent 91f1d684faSDavid Nugent int 92f1d684faSDavid Nugent chgnispwent(const char *path, char const * login, struct passwd * pwd) 93f1d684faSDavid Nugent { 94fb2db031SBaptiste Daroussin return pw_nisupdate(path, pwd, login); 95f1d684faSDavid Nugent } 96f1d684faSDavid Nugent 97f1d684faSDavid Nugent int 98f1d684faSDavid Nugent delnispwent(const char *path, const char *login) 99f1d684faSDavid Nugent { 100fb2db031SBaptiste Daroussin return pw_nisupdate(path, NULL, login); 101f1d684faSDavid Nugent } 102