125839Speter/*-
2175267Sobrien * Copyright (C) 1996
325839Speter *	David L. Nugent.  All rights reserved.
4175267Sobrien *
5175267Sobrien * Redistribution and use in source and binary forms, with or without
6175267Sobrien * modification, are permitted provided that the following conditions
7175267Sobrien * are met:
8175267Sobrien * 1. Redistributions of source code must retain the above copyright
9175267Sobrien *    notice, this list of conditions and the following disclaimer.
1025839Speter * 2. Redistributions in binary form must reproduce the above copyright
1125839Speter *    notice, this list of conditions and the following disclaimer in the
1225839Speter *    documentation and/or other materials provided with the distribution.
1317721Speter *
1417721Speter * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1517721Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1617721Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1754431Speter * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1817721Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1917721Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2017721Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21107487Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22107487Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23107487Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2417721Speter * SUCH DAMAGE.
2517721Speter */
2617721Speter
2717721Speter#ifndef lint
2817721Speterstatic const char rcsid[] =
2917721Speter  "$FreeBSD$";
3017721Speter#endif /* not lint */
3117721Speter
3217721Speter#include <stdio.h>
3317721Speter#include <stdlib.h>
3417721Speter#include <string.h>
3517721Speter#include <sys/types.h>
3617721Speter#include <err.h>
3717721Speter#include <pwd.h>
3817721Speter#include <libutil.h>
3917721Speter
4017721Speter#include "pw.h"
4117721Speter
4217721Speterstatic int
4317721Speterpw_nisupdate(const char * path, struct passwd * pwd, char const * user)
4417721Speter{
4517721Speter	int pfd, tfd;
4617721Speter	struct passwd *pw = NULL;
4717721Speter	struct passwd *old_pw = NULL;
4817721Speter
49130307Speter	if (pwd != NULL)
50130307Speter		pw = pw_dup(pwd);
51130307Speter
52130307Speter	if (user != NULL)
5317721Speter		old_pw = GETPWNAM(user);
5417721Speter
5517721Speter	if (pw_init(NULL, path))
5617721Speter		err(1,"pw_init()");
5717721Speter	if ((pfd = pw_lock()) == -1) {
5817721Speter		pw_fini();
5917721Speter		err(1, "pw_lock()");
6017721Speter	}
6117721Speter	if ((tfd = pw_tmp(-1)) == -1) {
6217721Speter		pw_fini();
6317721Speter		err(1, "pw_tmp()");
6417721Speter	}
6517721Speter	if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
6617721Speter		pw_fini();
6717721Speter		err(1, "pw_copy()");
6817721Speter	}
6917721Speter	if (chmod(pw_tempname(), 0644) == -1)
7017721Speter		err(1, "chmod()");
7117721Speter	if (rename(pw_tempname(), path) == -1)
7217721Speter		err(1, "rename()");
7317721Speter
7417721Speter	free(pw);
7517721Speter	pw_fini();
76107487Speter
77107487Speter	return (0);
78107487Speter}
79107487Speter
80107487Speterint
8117721Speteraddnispwent(const char *path, struct passwd * pwd)
8217721Speter{
8317721Speter	return pw_nisupdate(path, pwd, NULL);
8417721Speter}
8517721Speter
8617721Speterint
8717721Speterchgnispwent(const char *path, char const * login, struct passwd * pwd)
8817721Speter{
8917721Speter	return pw_nisupdate(path, pwd, login);
9017721Speter}
9117721Speter
9217721Speterint
9317721Speterdelnispwent(const char *path, const char *login)
9417721Speter{
9517721Speter	return pw_nisupdate(path, NULL, login);
9617721Speter}
97128269Speter