xref: /freebsd/usr.sbin/pw/pw_nis.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
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
28 static const char rcsid[] =
29   "$FreeBSD$";
30 #endif /* not lint */
31 
32 #include <sys/types.h>
33 
34 #include <err.h>
35 #include <pwd.h>
36 #include <libutil.h>
37 #include <unistd.h>
38 
39 #include "pw.h"
40 
41 static int
42 pw_nisupdate(const char * path, struct passwd * pwd, char const * user)
43 {
44 	int pfd, tfd;
45 	struct passwd *pw = NULL;
46 	struct passwd *old_pw = NULL;
47 
48 	printf("===> %s\n", path);
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 		close(tfd);
68 		err(1, "pw_copy()");
69 	}
70 	fsync(tfd);
71 	close(tfd);
72 	if (chmod(pw_tempname(), 0644) == -1)
73 		err(1, "chmod()");
74 	if (rename(pw_tempname(), path) == -1)
75 		err(1, "rename()");
76 
77 	free(pw);
78 	pw_fini();
79 
80 	return (0);
81 }
82 
83 int
84 addnispwent(const char *path, struct passwd * pwd)
85 {
86 	return pw_nisupdate(path, pwd, NULL);
87 }
88 
89 int
90 chgnispwent(const char *path, char const * login, struct passwd * pwd)
91 {
92 	return pw_nisupdate(path, pwd, login);
93 }
94 
95 int
96 delnispwent(const char *path, const char *login)
97 {
98 	return pw_nisupdate(path, NULL, login);
99 }
100