1/*-
2 * Copyright (C) 1996
3 *	David L. Nugent.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#ifndef lint
28static const char rcsid[] =
29  "$FreeBSD$";
30#endif /* not lint */
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <sys/types.h>
36#include <err.h>
37#include <pwd.h>
38#include <libutil.h>
39
40#include "pw.h"
41
42static int
43pw_nisupdate(const char * path, struct passwd * pwd, char const * user)
44{
45	int pfd, tfd;
46	struct passwd *pw = NULL;
47	struct passwd *old_pw = NULL;
48
49	if (pwd != NULL)
50		pw = pw_dup(pwd);
51
52	if (user != NULL)
53		old_pw = GETPWNAM(user);
54
55	if (pw_init(NULL, path))
56		err(1,"pw_init()");
57	if ((pfd = pw_lock()) == -1) {
58		pw_fini();
59		err(1, "pw_lock()");
60	}
61	if ((tfd = pw_tmp(-1)) == -1) {
62		pw_fini();
63		err(1, "pw_tmp()");
64	}
65	if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
66		pw_fini();
67		err(1, "pw_copy()");
68	}
69	if (chmod(pw_tempname(), 0644) == -1)
70		err(1, "chmod()");
71	if (rename(pw_tempname(), path) == -1)
72		err(1, "rename()");
73
74	free(pw);
75	pw_fini();
76
77	return (0);
78}
79
80int
81addnispwent(const char *path, struct passwd * pwd)
82{
83	return pw_nisupdate(path, pwd, NULL);
84}
85
86int
87chgnispwent(const char *path, char const * login, struct passwd * pwd)
88{
89	return pw_nisupdate(path, pwd, login);
90}
91
92int
93delnispwent(const char *path, const char *login)
94{
95	return pw_nisupdate(path, NULL, login);
96}
97