1 /* $NetBSD: chpass.c,v 1.9 1996/08/09 09:22:11 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1988, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1988, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)chpass.c 8.4 (Berkeley) 4/2/94"; 45 #else 46 static char rcsid[] = "$NetBSD: chpass.c,v 1.9 1996/08/09 09:22:11 thorpej Exp $"; 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/stat.h> 52 #include <sys/time.h> 53 #include <sys/resource.h> 54 55 #include <ctype.h> 56 #include <err.h> 57 #include <errno.h> 58 #include <fcntl.h> 59 #include <pwd.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 #include <util.h> 65 66 #include "chpass.h" 67 #include "pathnames.h" 68 69 extern char *__progname; /* from crt0.o */ 70 71 char *tempname; 72 uid_t uid; 73 int use_yp; 74 75 void (*Pw_error) __P((const char *, int, int)); 76 77 #ifdef YP 78 extern struct passwd *ypgetpwnam(), *ypgetpwuid(); 79 extern int _yp_check __P((char **)); /* buried deep inside libc */ 80 #endif 81 82 void baduser __P((void)); 83 void usage __P((void)); 84 85 int 86 main(argc, argv) 87 int argc; 88 char **argv; 89 { 90 enum { NEWSH, LOADENTRY, EDITENTRY } op; 91 struct passwd *pw, lpw; 92 int ch, pfd, tfd, dfd; 93 char *arg, tempname[] = "/etc/pw.XXXXXX"; 94 95 #ifdef YP 96 use_yp = _yp_check(NULL); 97 #endif 98 99 op = EDITENTRY; 100 while ((ch = getopt(argc, argv, "a:s:ly")) != EOF) 101 switch(ch) { 102 case 'a': 103 op = LOADENTRY; 104 arg = optarg; 105 break; 106 case 's': 107 op = NEWSH; 108 arg = optarg; 109 break; 110 case 'l': 111 use_yp = 0; 112 break; 113 case 'y': 114 #ifdef YP 115 if (!use_yp) 116 errx(1, "YP not in use."); 117 #else 118 errx(1, "YP support not compiled in."); 119 #endif 120 break; 121 default: 122 usage(); 123 } 124 argc -= optind; 125 argv += optind; 126 127 #ifdef YP 128 if (use_yp) 129 Pw_error = yppw_error; 130 else 131 #endif 132 Pw_error = pw_error; 133 134 #ifdef YP 135 if (op == LOADENTRY && use_yp) 136 errx(1, "cannot load entry using YP.\n\tUse the -l flag to load local."); 137 #endif 138 uid = getuid(); 139 140 if (op == EDITENTRY || op == NEWSH) 141 switch(argc) { 142 case 0: 143 #ifdef YP 144 if (use_yp) 145 pw = ypgetpwuid(uid); 146 else 147 #endif /* YP */ 148 pw = getpwuid(uid); 149 if (!pw) 150 errx(1, "unknown user: uid %u\n", uid); 151 break; 152 case 1: 153 #ifdef YP 154 if (use_yp) 155 pw = ypgetpwnam(*argv); 156 else 157 #endif /* YP */ 158 pw = getpwnam(*argv); 159 if (!pw) 160 errx(1, "unknown user: %s", *argv); 161 if (uid && uid != pw->pw_uid) 162 baduser(); 163 break; 164 default: 165 usage(); 166 } 167 168 if (op == NEWSH) { 169 /* protect p_shell -- it thinks NULL is /bin/sh */ 170 if (!arg[0]) 171 usage(); 172 if (p_shell(arg, pw, (ENTRY *)NULL)) 173 (*Pw_error)((char *)NULL, 0, 1); 174 } 175 176 if (op == LOADENTRY) { 177 if (uid) 178 baduser(); 179 pw = &lpw; 180 if (!pw_scan(arg, pw, (int *)NULL)) 181 exit(1); 182 } 183 184 if (!use_yp) { 185 /* 186 * Get the passwd lock file and open the passwd file for 187 * reading. 188 */ 189 pw_init(); 190 tfd = pw_lock(0); 191 if (tfd < 0) 192 errx(1, "the passwd file is busy."); 193 pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0); 194 if (pfd < 0) 195 pw_error(_PATH_MASTERPASSWD, 1, 1); 196 } 197 198 /* Edit the user passwd information if requested. */ 199 if (op == EDITENTRY) { 200 dfd = mkstemp(tempname); 201 if (dfd < 0) { 202 (*Pw_error)(tempname, 1, 1); 203 } 204 display(tempname, dfd, pw); 205 edit(tempname, pw); 206 (void)unlink(tempname); 207 } 208 209 #ifdef YP 210 if (use_yp) { 211 if (pw_yp(pw, uid)) 212 yppw_error((char *)NULL, 0, 1); 213 else 214 exit(0); 215 } 216 else 217 #endif /* YP */ 218 219 /* Copy the passwd file to the lock file, updating pw. */ 220 pw_copy(pfd, tfd, pw); 221 222 /* Now finish the passwd file update. */ 223 if (pw_mkdb() < 0) 224 pw_error((char *)NULL, 0, 1); 225 226 exit(0); 227 } 228 229 void 230 baduser() 231 { 232 233 errx(1, "%s", strerror(EACCES)); 234 } 235 236 void 237 usage() 238 { 239 240 #ifdef YP 241 (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [-l]%s [user]\n", use_yp?" [-y]":""); 242 #else 243 (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n"); 244 #endif 245 exit(1); 246 } 247