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/10/usr.sbin/pw/pw_nis.c 310173 2016-12-16 20:10:55Z asomers $";
3030259Scharnier#endif /* not lint */
3130259Scharnier
3221330Sdavidn#include <sys/types.h>
33287084Sbapt
34242349Sbapt#include <err.h>
35242349Sbapt#include <pwd.h>
36242349Sbapt#include <libutil.h>
37308814Sasomers#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
48287084Sbapt	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();
67308814Sasomers		close(tfd);
68242349Sbapt		err(1, "pw_copy()");
69242349Sbapt	}
70310173Sasomers	fsync(tfd);
71308814Sasomers	close(tfd);
72243335Sbapt	if (chmod(pw_tempname(), 0644) == -1)
73243335Sbapt		err(1, "chmod()");
74242349Sbapt	if (rename(pw_tempname(), path) == -1)
75242349Sbapt		err(1, "rename()");
76242349Sbapt
77242349Sbapt	free(pw);
78242349Sbapt	pw_fini();
79242349Sbapt
80242349Sbapt	return (0);
8121330Sdavidn}
8221330Sdavidn
8321330Sdavidnint
8421330Sdavidnaddnispwent(const char *path, struct passwd * pwd)
8521330Sdavidn{
86242349Sbapt	return pw_nisupdate(path, pwd, NULL);
8721330Sdavidn}
8821330Sdavidn
8921330Sdavidnint
9021330Sdavidnchgnispwent(const char *path, char const * login, struct passwd * pwd)
9121330Sdavidn{
92242349Sbapt	return pw_nisupdate(path, pwd, login);
9321330Sdavidn}
9421330Sdavidn
9521330Sdavidnint
9621330Sdavidndelnispwent(const char *path, const char *login)
9721330Sdavidn{
98242349Sbapt	return pw_nisupdate(path, NULL, login);
9921330Sdavidn}
100