pw_nis.c revision 243335
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: head/usr.sbin/pw/pw_nis.c 243335 2012-11-20 14:05:46Z bapt $"; 3030259Scharnier#endif /* not lint */ 3130259Scharnier 3221330Sdavidn#include <stdio.h> 3321330Sdavidn#include <stdlib.h> 3421330Sdavidn#include <string.h> 3521330Sdavidn#include <sys/types.h> 36242349Sbapt#include <err.h> 37242349Sbapt#include <pwd.h> 38242349Sbapt#include <libutil.h> 3921330Sdavidn 4021330Sdavidn#include "pw.h" 4121330Sdavidn 4221330Sdavidnstatic int 43242349Sbaptpw_nisupdate(const char * path, struct passwd * pwd, char const * user) 4421330Sdavidn{ 45242349Sbapt int pfd, tfd; 46242349Sbapt struct passwd *pw = NULL; 47242349Sbapt struct passwd *old_pw = NULL; 4821330Sdavidn 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(); 67242349Sbapt err(1, "pw_copy()"); 68242349Sbapt } 69243335Sbapt if (chmod(pw_tempname(), 0644) == -1) 70243335Sbapt err(1, "chmod()"); 71242349Sbapt if (rename(pw_tempname(), path) == -1) 72242349Sbapt err(1, "rename()"); 73242349Sbapt 74242349Sbapt free(pw); 75242349Sbapt pw_fini(); 76242349Sbapt 77242349Sbapt return (0); 7821330Sdavidn} 7921330Sdavidn 8021330Sdavidnint 8121330Sdavidnaddnispwent(const char *path, struct passwd * pwd) 8221330Sdavidn{ 83242349Sbapt return pw_nisupdate(path, pwd, NULL); 8421330Sdavidn} 8521330Sdavidn 8621330Sdavidnint 8721330Sdavidnchgnispwent(const char *path, char const * login, struct passwd * pwd) 8821330Sdavidn{ 89242349Sbapt return pw_nisupdate(path, pwd, login); 9021330Sdavidn} 9121330Sdavidn 9221330Sdavidnint 9321330Sdavidndelnispwent(const char *path, const char *login) 9421330Sdavidn{ 95242349Sbapt return pw_nisupdate(path, NULL, login); 9621330Sdavidn} 97