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