121330Sdavidn/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
421330Sdavidn * Copyright (C) 1996
521330Sdavidn *	David L. Nugent.  All rights reserved.
621330Sdavidn *
721330Sdavidn * Redistribution and use in source and binary forms, with or without
821330Sdavidn * modification, are permitted provided that the following conditions
921330Sdavidn * are met:
1021330Sdavidn * 1. Redistributions of source code must retain the above copyright
1121330Sdavidn *    notice, this list of conditions and the following disclaimer.
1221330Sdavidn * 2. Redistributions in binary form must reproduce the above copyright
1321330Sdavidn *    notice, this list of conditions and the following disclaimer in the
1421330Sdavidn *    documentation and/or other materials provided with the distribution.
1521330Sdavidn *
1621330Sdavidn * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1721330Sdavidn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1821330Sdavidn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1921330Sdavidn * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
2021330Sdavidn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2121330Sdavidn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2221330Sdavidn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2321330Sdavidn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2421330Sdavidn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2521330Sdavidn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2621330Sdavidn * SUCH DAMAGE.
2721330Sdavidn */
2821330Sdavidn
2930259Scharnier#ifndef lint
3030259Scharnierstatic const char rcsid[] =
3150479Speter  "$FreeBSD: stable/11/usr.sbin/pw/pw_nis.c 330449 2018-03-05 07:26:05Z eadler $";
3230259Scharnier#endif /* not lint */
3330259Scharnier
3421330Sdavidn#include <sys/types.h>
35286201Sbapt
36242349Sbapt#include <err.h>
37242349Sbapt#include <pwd.h>
38242349Sbapt#include <libutil.h>
39308815Sasomers#include <unistd.h>
4021330Sdavidn
4121330Sdavidn#include "pw.h"
4221330Sdavidn
4321330Sdavidnstatic int
44242349Sbaptpw_nisupdate(const char * path, struct passwd * pwd, char const * user)
4521330Sdavidn{
46242349Sbapt	int pfd, tfd;
47242349Sbapt	struct passwd *pw = NULL;
48242349Sbapt	struct passwd *old_pw = NULL;
4921330Sdavidn
50286196Sbapt	printf("===> %s\n", path);
51242349Sbapt	if (pwd != NULL)
52242349Sbapt		pw = pw_dup(pwd);
53242349Sbapt
54242349Sbapt	if (user != NULL)
55242349Sbapt		old_pw = GETPWNAM(user);
56242349Sbapt
57242349Sbapt	if (pw_init(NULL, path))
58242349Sbapt		err(1,"pw_init()");
59242349Sbapt	if ((pfd = pw_lock()) == -1) {
60242349Sbapt		pw_fini();
61242349Sbapt		err(1, "pw_lock()");
62242349Sbapt	}
63242349Sbapt	if ((tfd = pw_tmp(-1)) == -1) {
64242349Sbapt		pw_fini();
65242349Sbapt		err(1, "pw_tmp()");
66242349Sbapt	}
67242349Sbapt	if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
68242349Sbapt		pw_fini();
69308815Sasomers		close(tfd);
70242349Sbapt		err(1, "pw_copy()");
71242349Sbapt	}
72310176Sasomers	fsync(tfd);
73308815Sasomers	close(tfd);
74243335Sbapt	if (chmod(pw_tempname(), 0644) == -1)
75243335Sbapt		err(1, "chmod()");
76242349Sbapt	if (rename(pw_tempname(), path) == -1)
77242349Sbapt		err(1, "rename()");
78242349Sbapt
79242349Sbapt	free(pw);
80242349Sbapt	pw_fini();
81242349Sbapt
82242349Sbapt	return (0);
8321330Sdavidn}
8421330Sdavidn
8521330Sdavidnint
8621330Sdavidnaddnispwent(const char *path, struct passwd * pwd)
8721330Sdavidn{
88242349Sbapt	return pw_nisupdate(path, pwd, NULL);
8921330Sdavidn}
9021330Sdavidn
9121330Sdavidnint
9221330Sdavidnchgnispwent(const char *path, char const * login, struct passwd * pwd)
9321330Sdavidn{
94242349Sbapt	return pw_nisupdate(path, pwd, login);
9521330Sdavidn}
9621330Sdavidn
9721330Sdavidnint
9821330Sdavidndelnispwent(const char *path, const char *login)
9921330Sdavidn{
100242349Sbapt	return pw_nisupdate(path, NULL, login);
10121330Sdavidn}
102