1 /*- 2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>. 3 * 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 as 10 * the first lines of this file unmodified. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by David L. Nugent. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $Id$ 33 */ 34 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <unistd.h> 39 #include <stdarg.h> 40 #include <errno.h> 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <sys/wait.h> 44 #include <stdarg.h> 45 46 #include "pwupd.h" 47 48 static int 49 pwdb(char *arg,...) 50 { 51 int i = 0; 52 pid_t pid; 53 va_list ap; 54 char *args[8]; 55 56 args[i++] = _PATH_PWD_MKDB; 57 va_start(ap, arg); 58 while (i < 6 && arg != NULL) { 59 args[i++] = arg; 60 arg = va_arg(ap, char *); 61 } 62 args[i++] = _PATH_MASTERPASSWD; 63 args[i] = NULL; 64 65 if ((pid = fork()) == -1) /* Error (errno set) */ 66 i = -1; 67 else if (pid == 0) { /* Child */ 68 execv(args[0], args); 69 _exit(1); 70 } else { /* Parent */ 71 waitpid(pid, &i, 0); 72 if ((i = WEXITSTATUS(i)) != 0) 73 errno = EIO; /* set SOMETHING */ 74 } 75 return i; 76 } 77 78 int 79 fmtpwentry(char *buf, struct passwd * pwd, int type) 80 { 81 int l; 82 char *pw; 83 84 pw = (pwd->pw_passwd == NULL || !*pwd->pw_passwd) ? "" : (type == PWF_MASTER) ? pwd->pw_passwd : "*"; 85 86 if (type == PWF_PASSWD) 87 l = sprintf(buf, "%s:*:%ld:%ld:%s:%s:%s\n", 88 pwd->pw_name, (long) pwd->pw_uid, (long) pwd->pw_gid, 89 pwd->pw_gecos ? pwd->pw_gecos : "User &", 90 pwd->pw_dir, pwd->pw_shell); 91 else 92 l = sprintf(buf, "%s:%s:%ld:%ld:%s:%lu:%lu:%s:%s:%s\n", 93 pwd->pw_name, pw, (long) pwd->pw_uid, (long) pwd->pw_gid, 94 pwd->pw_class ? pwd->pw_class : "", 95 (unsigned long) pwd->pw_change, 96 (unsigned long) pwd->pw_expire, 97 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 98 return l; 99 } 100 101 102 int 103 fmtpwent(char *buf, struct passwd * pwd) 104 { 105 return fmtpwentry(buf, pwd, PWF_STANDARD); 106 } 107 108 static int 109 pw_update(struct passwd * pwd, char const * user, int mode) 110 { 111 int rc = 0; 112 113 endpwent(); 114 115 /* 116 * First, let's check the see if the database is alright 117 */ 118 if (pwdb("-c", NULL) == 0) { /* Check only */ 119 char pfx[32]; 120 char pwbuf[MAXPWLINE]; 121 int l = sprintf(pfx, "%s:", user); 122 123 /* 124 * Update the passwd file first 125 */ 126 if (pwd == NULL) 127 *pwbuf = '\0'; 128 else 129 fmtpwentry(pwbuf, pwd, PWF_PASSWD); 130 if ((rc = fileupdate(_PATH_PASSWD, 0644, pwbuf, pfx, l, mode)) != 0) { 131 132 /* 133 * Then the master.passwd file 134 */ 135 if (pwd != NULL) 136 fmtpwentry(pwbuf, pwd, PWF_MASTER); 137 if ((rc = fileupdate(_PATH_MASTERPASSWD, 0644, pwbuf, pfx, l, mode)) != 0) 138 rc = pwdb(NULL) == 0; 139 } 140 } 141 return rc; 142 } 143 144 int 145 addpwent(struct passwd * pwd) 146 { 147 return pw_update(pwd, pwd->pw_name, UPD_CREATE); 148 } 149 150 int 151 chgpwent(char const * login, struct passwd * pwd) 152 { 153 return pw_update(pwd, login, UPD_REPLACE); 154 } 155 156 int 157 delpwent(struct passwd * pwd) 158 { 159 return pw_update(NULL, pwd->pw_name, UPD_DELETE); 160 } 161