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 close(tfd); 71 if (chmod(pw_tempname(), 0644) == -1) 72 err(1, "chmod()"); 73 if (rename(pw_tempname(), path) == -1) 74 err(1, "rename()"); 75 76 free(pw); 77 pw_fini(); 78 79 return (0); 80 } 81 82 int 83 addnispwent(const char *path, struct passwd * pwd) 84 { 85 return pw_nisupdate(path, pwd, NULL); 86 } 87 88 int 89 chgnispwent(const char *path, char const * login, struct passwd * pwd) 90 { 91 return pw_nisupdate(path, pwd, login); 92 } 93 94 int 95 delnispwent(const char *path, const char *login) 96 { 97 return pw_nisupdate(path, NULL, login); 98 } 99