121330Sdavidn/*- 221330Sdavidn * Copyright (C) 1996 321330Sdavidn * David L. Nugent. All rights reserved. 421330Sdavidn * 521330Sdavidn * Redistribution and use in source and binary forms, with or without 621330Sdavidn * modification, are permitted provided that the following conditions 721330Sdavidn * are met: 821330Sdavidn * 1. Redistributions of source code must retain the above copyright 921330Sdavidn * notice, this list of conditions and the following disclaimer. 1021330Sdavidn * 2. Redistributions in binary form must reproduce the above copyright 1121330Sdavidn * notice, this list of conditions and the following disclaimer in the 1221330Sdavidn * documentation and/or other materials provided with the distribution. 1321330Sdavidn * 1421330Sdavidn * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 1521330Sdavidn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1621330Sdavidn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1721330Sdavidn * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 1821330Sdavidn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1921330Sdavidn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2021330Sdavidn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2121330Sdavidn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2221330Sdavidn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2321330Sdavidn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2421330Sdavidn * SUCH DAMAGE. 2521330Sdavidn */ 2621330Sdavidn 2730259Scharnier#ifndef lint 2830259Scharnierstatic const char rcsid[] = 2950479Speter "$FreeBSD: releng/10.3/usr.sbin/pw/pw_nis.c 287084 2015-08-23 21:42:27Z bapt $"; 3030259Scharnier#endif /* not lint */ 3130259Scharnier 3221330Sdavidn#include <sys/types.h> 33287084Sbapt 34242349Sbapt#include <err.h> 35242349Sbapt#include <pwd.h> 36242349Sbapt#include <libutil.h> 3721330Sdavidn 3821330Sdavidn#include "pw.h" 3921330Sdavidn 4021330Sdavidnstatic int 41242349Sbaptpw_nisupdate(const char * path, struct passwd * pwd, char const * user) 4221330Sdavidn{ 43242349Sbapt int pfd, tfd; 44242349Sbapt struct passwd *pw = NULL; 45242349Sbapt struct passwd *old_pw = NULL; 4621330Sdavidn 47287084Sbapt printf("===> %s\n", path); 48242349Sbapt if (pwd != NULL) 49242349Sbapt pw = pw_dup(pwd); 50242349Sbapt 51242349Sbapt if (user != NULL) 52242349Sbapt old_pw = GETPWNAM(user); 53242349Sbapt 54242349Sbapt if (pw_init(NULL, path)) 55242349Sbapt err(1,"pw_init()"); 56242349Sbapt if ((pfd = pw_lock()) == -1) { 57242349Sbapt pw_fini(); 58242349Sbapt err(1, "pw_lock()"); 59242349Sbapt } 60242349Sbapt if ((tfd = pw_tmp(-1)) == -1) { 61242349Sbapt pw_fini(); 62242349Sbapt err(1, "pw_tmp()"); 63242349Sbapt } 64242349Sbapt if (pw_copy(pfd, tfd, pw, old_pw) == -1) { 65242349Sbapt pw_fini(); 66242349Sbapt err(1, "pw_copy()"); 67242349Sbapt } 68243335Sbapt if (chmod(pw_tempname(), 0644) == -1) 69243335Sbapt err(1, "chmod()"); 70242349Sbapt if (rename(pw_tempname(), path) == -1) 71242349Sbapt err(1, "rename()"); 72242349Sbapt 73242349Sbapt free(pw); 74242349Sbapt pw_fini(); 75242349Sbapt 76242349Sbapt return (0); 7721330Sdavidn} 7821330Sdavidn 7921330Sdavidnint 8021330Sdavidnaddnispwent(const char *path, struct passwd * pwd) 8121330Sdavidn{ 82242349Sbapt return pw_nisupdate(path, pwd, NULL); 8321330Sdavidn} 8421330Sdavidn 8521330Sdavidnint 8621330Sdavidnchgnispwent(const char *path, char const * login, struct passwd * pwd) 8721330Sdavidn{ 88242349Sbapt return pw_nisupdate(path, pwd, login); 8921330Sdavidn} 9021330Sdavidn 9121330Sdavidnint 9221330Sdavidndelnispwent(const char *path, const char *login) 9321330Sdavidn{ 94242349Sbapt return pw_nisupdate(path, NULL, login); 9521330Sdavidn} 96