pw_nis.c revision 242349
1139825Simp/*- 222338Skato * Copyright (C) 1996 322338Skato * David L. Nugent. All rights reserved. 422338Skato * 522338Skato * Redistribution and use in source and binary forms, with or without 622338Skato * modification, are permitted provided that the following conditions 717350Sasami * are met: 817350Sasami * 1. Redistributions of source code must retain the above copyright 917350Sasami * notice, this list of conditions and the following disclaimer. 1017350Sasami * 2. Redistributions in binary form must reproduce the above copyright 1117350Sasami * notice, this list of conditions and the following disclaimer in the 1217350Sasami * documentation and/or other materials provided with the distribution. 1317350Sasami * 1417350Sasami * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 1517350Sasami * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1617350Sasami * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1717350Sasami * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 1817350Sasami * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1917350Sasami * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2017350Sasami * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2117350Sasami * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2217350Sasami * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2317350Sasami * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2417350Sasami * SUCH DAMAGE. 2517350Sasami */ 2617350Sasami 2717350Sasami#ifndef lint 2817350Sasamistatic const char rcsid[] = 2917350Sasami "$FreeBSD: head/usr.sbin/pw/pw_nis.c 242349 2012-10-30 08:00:53Z bapt $"; 3052467Snyan#endif /* not lint */ 3152467Snyan 3217350Sasami#include <stdio.h> 3317350Sasami#include <stdlib.h> 3433715Skato#include <string.h> 3533715Skato#include <sys/types.h> 3617350Sasami#include <err.h> 3717350Sasami#include <pwd.h> 3817350Sasami#include <libutil.h> 39161129Simp 40161129Simp#include "pw.h" 41161129Simp 42184327Skatostatic int 43184327Skatopw_nisupdate(const char * path, struct passwd * pwd, char const * user) 44184327Skato{ 45184327Skato int pfd, tfd; 46161129Simp struct passwd *pw = NULL; 47161129Simp struct passwd *old_pw = NULL; 4822407Skato 4917350Sasami if (pwd != NULL) 50184327Skato pw = pw_dup(pwd); 51184327Skato 52184327Skato if (user != NULL) 53184327Skato old_pw = GETPWNAM(user); 54184327Skato 55184327Skato if (pw_init(NULL, path)) 5622338Skato err(1,"pw_init()"); 5722338Skato if ((pfd = pw_lock()) == -1) { 5822338Skato pw_fini(); 5922338Skato err(1, "pw_lock()"); 6022338Skato } 6122338Skato if ((tfd = pw_tmp(-1)) == -1) { 6222338Skato pw_fini(); 6322338Skato err(1, "pw_tmp()"); 6422338Skato } 6522338Skato if (pw_copy(pfd, tfd, pw, old_pw) == -1) { 6622338Skato pw_fini(); 6722338Skato err(1, "pw_copy()"); 6822338Skato } 6922338Skato if (rename(pw_tempname(), path) == -1) 7017350Sasami err(1, "rename()"); 7122338Skato 7222338Skato free(pw); 7322338Skato pw_fini(); 7422338Skato 7522338Skato return (0); 7622338Skato} 7722165Skato 7822165Skatoint 7917350Sasamiaddnispwent(const char *path, struct passwd * pwd) 8058145Snyan{ 8158145Snyan return pw_nisupdate(path, pwd, NULL); 82150128Snyan} 83150128Snyan 84150128Snyanint 8558145Snyanchgnispwent(const char *path, char const * login, struct passwd * pwd) 8658145Snyan{ 8758145Snyan return pw_nisupdate(path, pwd, login); 8822165Skato} 8958145Snyan 9058145Snyanint 9158145Snyandelnispwent(const char *path, const char *login) 9258145Snyan{ 9358145Snyan return pw_nisupdate(path, NULL, login); 9458145Snyan} 9558145Snyan