pw_nis.c revision 308815
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: stable/11/usr.sbin/pw/pw_nis.c 308815 2016-11-18 22:28:57Z asomers $"; 3030259Scharnier#endif /* not lint */ 3130259Scharnier 3221330Sdavidn#include <sys/types.h> 33286201Sbapt 34242349Sbapt#include <err.h> 35242349Sbapt#include <pwd.h> 36242349Sbapt#include <libutil.h> 37308815Sasomers#include <unistd.h> 3821330Sdavidn 3921330Sdavidn#include "pw.h" 4021330Sdavidn 4121330Sdavidnstatic int 42242349Sbaptpw_nisupdate(const char * path, struct passwd * pwd, char const * user) 4321330Sdavidn{ 44242349Sbapt int pfd, tfd; 45242349Sbapt struct passwd *pw = NULL; 46242349Sbapt struct passwd *old_pw = NULL; 4721330Sdavidn 48286196Sbapt printf("===> %s\n", path); 49242349Sbapt if (pwd != NULL) 50242349Sbapt pw = pw_dup(pwd); 51242349Sbapt 52242349Sbapt if (user != NULL) 53242349Sbapt old_pw = GETPWNAM(user); 54242349Sbapt 55242349Sbapt if (pw_init(NULL, path)) 56242349Sbapt err(1,"pw_init()"); 57242349Sbapt if ((pfd = pw_lock()) == -1) { 58242349Sbapt pw_fini(); 59242349Sbapt err(1, "pw_lock()"); 60242349Sbapt } 61242349Sbapt if ((tfd = pw_tmp(-1)) == -1) { 62242349Sbapt pw_fini(); 63242349Sbapt err(1, "pw_tmp()"); 64242349Sbapt } 65242349Sbapt if (pw_copy(pfd, tfd, pw, old_pw) == -1) { 66242349Sbapt pw_fini(); 67308815Sasomers close(tfd); 68242349Sbapt err(1, "pw_copy()"); 69242349Sbapt } 70308815Sasomers close(tfd); 71243335Sbapt if (chmod(pw_tempname(), 0644) == -1) 72243335Sbapt err(1, "chmod()"); 73242349Sbapt if (rename(pw_tempname(), path) == -1) 74242349Sbapt err(1, "rename()"); 75242349Sbapt 76242349Sbapt free(pw); 77242349Sbapt pw_fini(); 78242349Sbapt 79242349Sbapt return (0); 8021330Sdavidn} 8121330Sdavidn 8221330Sdavidnint 8321330Sdavidnaddnispwent(const char *path, struct passwd * pwd) 8421330Sdavidn{ 85242349Sbapt return pw_nisupdate(path, pwd, NULL); 8621330Sdavidn} 8721330Sdavidn 8821330Sdavidnint 8921330Sdavidnchgnispwent(const char *path, char const * login, struct passwd * pwd) 9021330Sdavidn{ 91242349Sbapt return pw_nisupdate(path, pwd, login); 9221330Sdavidn} 9321330Sdavidn 9421330Sdavidnint 9521330Sdavidndelnispwent(const char *path, const char *login) 9621330Sdavidn{ 97242349Sbapt return pw_nisupdate(path, NULL, login); 9821330Sdavidn} 99