1 /*- 2 * Copyright (c) 1988 The Regents of the University of California. 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. 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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 char copyright[] = 36 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ 37 All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)chpass.c 5.17 (Berkeley) 3/3/91"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 #include <sys/signal.h> 47 #include <sys/time.h> 48 #include <sys/resource.h> 49 #include <fcntl.h> 50 #include <pwd.h> 51 #include <errno.h> 52 #include <stdio.h> 53 #include <ctype.h> 54 #include <string.h> 55 #include "chpass.h" 56 #include "pathnames.h" 57 58 char *progname = "chpass"; 59 char *tempname; 60 uid_t uid; 61 62 #ifdef YP 63 int use_yp; 64 int force_yp = 0; 65 extern struct passwd *ypgetpwnam(), *ypgetpwuid(); 66 #endif 67 68 main(argc, argv) 69 int argc; 70 char **argv; 71 { 72 extern int optind; 73 extern char *optarg; 74 register enum { NEWSH, LOADENTRY, EDITENTRY } op; 75 register struct passwd *pw; 76 struct passwd lpw; 77 int ch, pfd, tfd; 78 char *arg; 79 80 #ifdef YP 81 use_yp = _yp_check(NULL); 82 #endif 83 84 op = EDITENTRY; 85 while ((ch = getopt(argc, argv, "a:s:ly")) != EOF) 86 switch(ch) { 87 case 'a': 88 op = LOADENTRY; 89 arg = optarg; 90 break; 91 case 's': 92 op = NEWSH; 93 arg = optarg; 94 break; 95 #ifdef YP 96 case 'l': 97 use_yp = 0; 98 break; 99 case 'y': 100 if (!use_yp) { 101 fprintf(stderr, "chpass: YP not in use.\n"); 102 usage(); 103 exit(1); 104 } 105 force_yp = 1; 106 break; 107 #endif 108 case '?': 109 default: 110 usage(); 111 } 112 argc -= optind; 113 argv += optind; 114 115 #ifdef YP 116 if (op == LOADENTRY && use_yp) { 117 (void)fprintf(stderr, "chpass: cannot load entry using NIS.\n\tUse the -l flag to load local.\n"); 118 exit(1); 119 } 120 #endif 121 uid = getuid(); 122 123 if (op == EDITENTRY || op == NEWSH) 124 switch(argc) { 125 case 0: 126 pw = getpwuid(uid); 127 #ifdef YP 128 if (pw && !force_yp) 129 use_yp = 0; 130 else if (use_yp) 131 pw = ypgetpwuid(uid); 132 #endif /* YP */ 133 if (!pw) { 134 (void)fprintf(stderr, 135 "chpass: unknown user: uid %u\n", uid); 136 exit(1); 137 } 138 break; 139 case 1: 140 pw = getpwnam(*argv); 141 #ifdef YP 142 if (pw && !force_yp) 143 use_yp = 0; 144 else if (use_yp) 145 pw = ypgetpwnam(*argv); 146 #endif /* YP */ 147 if (!pw) { 148 (void)fprintf(stderr, 149 "chpass: unknown user %s.\n", *argv); 150 exit(1); 151 } 152 if (uid && uid != pw->pw_uid) 153 baduser(); 154 break; 155 default: 156 usage(); 157 } 158 159 if (op == NEWSH) { 160 /* protect p_shell -- it thinks NULL is /bin/sh */ 161 if (!arg[0]) 162 usage(); 163 if (p_shell(arg, pw, (ENTRY *)NULL)) 164 pw_error((char *)NULL, 0, 1); 165 } 166 167 if (op == LOADENTRY) { 168 if (uid) 169 baduser(); 170 pw = &lpw; 171 if (!pw_scan(arg, pw)) 172 exit(1); 173 } 174 175 /* 176 * The temporary file/file descriptor usage is a little tricky here. 177 * 1: We start off with two fd's, one for the master password 178 * file (used to lock everything), and one for a temporary file. 179 * 2: Display() gets an fp for the temporary file, and copies the 180 * user's information into it. It then gives the temporary file 181 * to the user and closes the fp, closing the underlying fd. 182 * 3: The user edits the temporary file some number of times. 183 * 4: Verify() gets an fp for the temporary file, and verifies the 184 * contents. It can't use an fp derived from the step #2 fd, 185 * because the user's editor may have created a new instance of 186 * the file. Once the file is verified, its contents are stored 187 * in a password structure. The verify routine closes the fp, 188 * closing the underlying fd. 189 * 5: Delete the temporary file. 190 * 6: Get a new temporary file/fd. Pw_copy() gets an fp for it 191 * file and copies the master password file into it, replacing 192 * the user record with a new one. We can't use the first 193 * temporary file for this because it was owned by the user. 194 * Pw_copy() closes its fp, flushing the data and closing the 195 * underlying file descriptor. We can't close the master 196 * password fp, or we'd lose the lock. 197 * 7: Call pw_mkdb() (which renames the temporary file) and exit. 198 * The exit closes the master passwd fp/fd. 199 */ 200 pw_init(); 201 pfd = pw_lock(); 202 tfd = pw_tmp(); 203 204 if (op == EDITENTRY) { 205 display(tfd, pw); 206 edit(pw); 207 (void)unlink(tempname); 208 tfd = pw_tmp(); 209 } 210 211 #ifdef YP 212 if (use_yp) { 213 (void)unlink(tempname); 214 if (pw_yp(pw, uid)) 215 pw_error((char *)NULL, 0, 1); 216 else 217 exit(0); 218 } 219 else 220 #endif /* YP */ 221 pw_copy(pfd, tfd, pw); 222 223 if (!pw_mkdb()) 224 pw_error((char *)NULL, 0, 1); 225 226 exit(0); 227 } 228 229 baduser() 230 { 231 (void)fprintf(stderr, "chpass: %s\n", strerror(EACCES)); 232 exit(1); 233 } 234 235 usage() 236 { 237 #ifdef YP 238 (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [-l]%s [user]\n", use_yp?" [-y]":""); 239 #else 240 (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n"); 241 #endif 242 exit(1); 243 } 244