pwupd.c revision 242349
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: head/usr.sbin/pw/pwupd.c 242349 2012-10-30 08:00:53Z bapt $"; 30#endif /* not lint */ 31 32#include <stdio.h> 33#include <stdlib.h> 34#include <string.h> 35#include <unistd.h> 36#include <stdarg.h> 37#include <pwd.h> 38#include <libutil.h> 39#include <errno.h> 40#include <err.h> 41#include <sys/types.h> 42#include <sys/stat.h> 43#include <sys/param.h> 44#include <sys/wait.h> 45 46#include "pwupd.h" 47 48#define HAVE_PWDB_C 1 49#define HAVE_PWDB_U 1 50 51static char pathpwd[] = _PATH_PWD; 52static char * pwpath = pathpwd; 53 54int 55setpwdir(const char * dir) 56{ 57 if (dir == NULL) 58 return -1; 59 else { 60 char * d = malloc(strlen(dir)+1); 61 if (d == NULL) 62 return -1; 63 pwpath = strcpy(d, dir); 64 } 65 return 0; 66} 67 68char * 69getpwpath(char const * file) 70{ 71 static char pathbuf[MAXPATHLEN]; 72 73 snprintf(pathbuf, sizeof pathbuf, "%s/%s", pwpath, file); 74 return pathbuf; 75} 76 77static int 78pwdb(char *arg,...) 79{ 80 int i = 0; 81 pid_t pid; 82 va_list ap; 83 char *args[10]; 84 85 args[i++] = _PATH_PWD_MKDB; 86 va_start(ap, arg); 87 while (i < 6 && arg != NULL) { 88 args[i++] = arg; 89 arg = va_arg(ap, char *); 90 } 91 if (pwpath != pathpwd) { 92 args[i++] = "-d"; 93 args[i++] = pwpath; 94 } 95 args[i++] = getpwpath(_MASTERPASSWD); 96 args[i] = NULL; 97 98 if ((pid = fork()) == -1) /* Error (errno set) */ 99 i = errno; 100 else if (pid == 0) { /* Child */ 101 execv(args[0], args); 102 _exit(1); 103 } else { /* Parent */ 104 waitpid(pid, &i, 0); 105 if (WEXITSTATUS(i)) 106 i = EIO; 107 } 108 va_end(ap); 109 return i; 110} 111 112static int 113pw_update(struct passwd * pwd, char const * user) 114{ 115 int rc = 0; 116 117 /* 118 * First, let's check the see if the database is alright 119 * Note: -C is only available in FreeBSD 2.2 and above 120 */ 121#ifdef HAVE_PWDB_C 122 rc = pwdb("-C", (char *)NULL); /* Check only */ 123 if (rc == 0) { 124#else 125 { /* No -C */ 126#endif 127 int pfd, tfd; 128 struct passwd *pw = NULL; 129 struct passwd *old_pw = NULL; 130 131 if (pwd != NULL) 132 pw = pw_dup(pwd); 133 134 if (user != NULL) 135 old_pw = GETPWNAM(user); 136 137 if (pw_init(pwpath, NULL)) 138 err(1, "pw_init()"); 139 if ((pfd = pw_lock()) == -1) { 140 pw_fini(); 141 err(1, "pw_lock()"); 142 } 143 if ((tfd = pw_tmp(-1)) == -1) { 144 pw_fini(); 145 err(1, "pw_tmp()"); 146 } 147 if (pw_copy(pfd, tfd, pw, old_pw) == -1) { 148 pw_fini(); 149 err(1, "pw_copy()"); 150 } 151 if (pw_mkdb(user) == -1) { 152 pw_fini(); 153 err(1, "pw_mkdb()"); 154 } 155 free(pw); 156 pw_fini(); 157 } 158 return 0; 159} 160 161int 162addpwent(struct passwd * pwd) 163{ 164 return pw_update(pwd, NULL); 165} 166 167int 168chgpwent(char const * login, struct passwd * pwd) 169{ 170 return pw_update(pwd, login); 171} 172 173int 174delpwent(struct passwd * pwd) 175{ 176 char login[MAXLOGNAME]; 177 178 strlcpy(login, pwd->pw_name, MAXLOGNAME); 179 return pw_update(NULL, login); 180} 181