Annotation of src/usr.bin/chpass/chpass.c, Revision 1.24
1.24 ! millert 1: /* $OpenBSD: chpass.c,v 1.23 2002/06/27 19:02:40 deraadt Exp $ */
1.2 deraadt 2: /* $NetBSD: chpass.c,v 1.8 1996/05/15 21:50:43 jtc Exp $ */
1.1 deraadt 3:
4: /*-
5: * Copyright (c) 1988, 1993, 1994
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1988, 1993, 1994\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)chpass.c 8.4 (Berkeley) 4/2/94";
1.23 deraadt 46: #else
1.24 ! millert 47: static char rcsid[] = "$OpenBSD: chpass.c,v 1.23 2002/06/27 19:02:40 deraadt Exp $";
1.1 deraadt 48: #endif
49: #endif /* not lint */
50:
51: #include <sys/param.h>
1.17 millert 52: #include <sys/resource.h>
1.1 deraadt 53: #include <sys/stat.h>
54: #include <sys/time.h>
1.17 millert 55: #include <sys/uio.h>
1.1 deraadt 56:
57: #include <err.h>
58: #include <errno.h>
59: #include <fcntl.h>
60: #include <pwd.h>
1.17 millert 61: #include <signal.h>
1.1 deraadt 62: #include <stdio.h>
63: #include <stdlib.h>
64: #include <string.h>
65: #include <unistd.h>
1.2 deraadt 66: #include <util.h>
1.1 deraadt 67:
68: #include "chpass.h"
69: #include "pathnames.h"
70:
1.17 millert 71: enum { NEWSH, LOADENTRY, EDITENTRY } op;
1.1 deraadt 72: uid_t uid;
73:
1.13 millert 74: extern char *__progname;
75:
1.1 deraadt 76: #ifdef YP
77: int use_yp;
78: int force_yp = 0;
79: extern struct passwd *ypgetpwnam(), *ypgetpwuid();
1.21 millert 80: int _yp_check(char **);
81: int pw_yp(struct passwd *, uid_t);
1.1 deraadt 82: #endif
83:
1.21 millert 84: void baduser(void);
85: void kbintr(int);
86: void usage(void);
1.1 deraadt 87:
88: int
1.23 deraadt 89: main(int argc, char *argv[])
1.1 deraadt 90: {
1.23 deraadt 91: struct passwd *pw = NULL, lpw;
1.20 millert 92: int i, ch, pfd, tfd, dfd;
1.23 deraadt 93: char *arg = NULL;
1.17 millert 94: sigset_t fullset;
1.1 deraadt 95:
96: #ifdef YP
97: use_yp = _yp_check(NULL);
98: #endif
99:
100: op = EDITENTRY;
1.7 millert 101: while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
1.1 deraadt 102: switch(ch) {
103: case 'a':
104: op = LOADENTRY;
105: arg = optarg;
106: break;
107: case 's':
108: op = NEWSH;
109: arg = optarg;
110: break;
111: #ifdef YP
112: case 'l':
113: use_yp = 0;
114: break;
115: case 'y':
116: if (!use_yp) {
117: warnx("YP not in use.");
118: usage();
119: }
120: force_yp = 1;
121: break;
122: #endif
123: case '?':
124: default:
125: usage();
126: }
127: argc -= optind;
128: argv += optind;
129:
130: #ifdef YP
131: if (op == LOADENTRY && use_yp)
1.23 deraadt 132: errx(1, "cannot load using YP, use -l to load local.");
1.1 deraadt 133: #endif
134: uid = getuid();
135:
136: if (op == EDITENTRY || op == NEWSH)
137: switch(argc) {
138: case 0:
139: pw = getpwuid(uid);
140: #ifdef YP
141: if (pw && !force_yp)
142: use_yp = 0;
143: else if (use_yp)
144: pw = ypgetpwuid(uid);
145: #endif /* YP */
146: if (!pw)
1.22 mpech 147: errx(1, "unknown user: uid %u", uid);
1.1 deraadt 148: break;
149: case 1:
150: pw = getpwnam(*argv);
151: #ifdef YP
152: if (pw && !force_yp)
153: use_yp = 0;
154: else if (use_yp)
155: pw = ypgetpwnam(*argv);
156: #endif /* YP */
157: if (!pw)
158: errx(1, "unknown user: %s", *argv);
159: if (uid && uid != pw->pw_uid)
160: baduser();
161: break;
162: default:
163: usage();
164: }
165:
166: if (op == NEWSH) {
167: /* protect p_shell -- it thinks NULL is /bin/sh */
168: if (!arg[0])
169: usage();
1.9 kstailey 170: if (p_shell(arg, pw, NULL))
171: pw_error(NULL, 0, 1);
1.1 deraadt 172: }
173:
174: if (op == LOADENTRY) {
175: if (uid)
176: baduser();
177: pw = &lpw;
1.9 kstailey 178: if (!pw_scan(arg, pw, NULL))
1.1 deraadt 179: exit(1);
180: }
181:
1.2 deraadt 182: /* Edit the user passwd information if requested. */
1.1 deraadt 183: if (op == EDITENTRY) {
1.24 ! millert 184: char tempname[] = __CONCAT(_PATH_VARTMP,"pw.XXXXXXXX");
! 185:
1.2 deraadt 186: dfd = mkstemp(tempname);
1.12 millert 187: if (dfd == -1 || fcntl(dfd, F_SETFD, 1) == -1)
1.2 deraadt 188: pw_error(tempname, 1, 1);
189: display(tempname, dfd, pw);
190: edit(tempname, pw);
1.24 ! millert 191: close(dfd);
! 192: unlink(tempname);
1.1 deraadt 193: }
1.2 deraadt 194:
1.17 millert 195: /* Drop user's real uid and block all signals to avoid a DoS. */
196: setuid(0);
197: sigfillset(&fullset);
198: sigdelset(&fullset, SIGINT);
199: sigprocmask(SIG_BLOCK, &fullset, NULL);
200:
201: /* Get the passwd lock file and open the passwd file for reading. */
202: pw_init();
1.20 millert 203: for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
204: if (i == 4)
205: (void)fputs("Attempting lock password file, "
206: "please wait or press ^C to abort", stderr);
207: (void)signal(SIGINT, kbintr);
208: if (i % 16 == 0)
1.17 millert 209: fputc('.', stderr);
1.20 millert 210: usleep(250000);
1.17 millert 211: (void)signal(SIGINT, SIG_IGN);
212: }
1.20 millert 213: if (i >= 4)
214: fputc('\n', stderr);
1.17 millert 215: pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
216: if (pfd == -1 || fcntl(pfd, F_SETFD, 1) == -1)
217: pw_error(_PATH_MASTERPASSWD, 1, 1);
218:
1.1 deraadt 219: #ifdef YP
220: if (use_yp) {
1.17 millert 221: if (pw_yp(pw, uid))
1.9 kstailey 222: pw_error(NULL, 0, 1);
1.17 millert 223: else {
1.5 deraadt 224: pw_abort();
1.1 deraadt 225: exit(0);
1.5 deraadt 226: }
1.4 deraadt 227: } else
1.1 deraadt 228: #endif /* YP */
1.8 deraadt 229: {
230: /* Copy the passwd file to the lock file, updating pw. */
231: pw_copy(pfd, tfd, pw);
1.2 deraadt 232:
1.8 deraadt 233: /* Now finish the passwd file update. */
1.19 millert 234: if (pw_mkdb(pw->pw_name, 0) == -1)
1.9 kstailey 235: pw_error(NULL, 0, 1);
1.8 deraadt 236: }
1.1 deraadt 237:
238: exit(0);
239: }
240:
241: void
1.23 deraadt 242: baduser(void)
1.1 deraadt 243: {
244:
245: errx(1, "%s", strerror(EACCES));
1.14 millert 246: }
247:
248: void
1.23 deraadt 249: kbintr(int signo)
1.17 millert 250: {
251: struct iovec iv[5];
252:
253: iv[0].iov_base = "\n";
254: iv[0].iov_len = 1;
255: iv[1].iov_base = __progname;
256: iv[1].iov_len = strlen(__progname);
257: iv[2].iov_base = ": ";
258: iv[2].iov_len = 2;
259: iv[3].iov_base = _PATH_MASTERPASSWD;
260: iv[3].iov_len = sizeof(_PATH_MASTERPASSWD) - 1;
261: iv[4].iov_base = " unchanged\n";
262: iv[4].iov_len = 11;
263: writev(STDERR_FILENO, iv, 5);
264:
265: _exit(1);
1.1 deraadt 266: }
267:
268: void
1.23 deraadt 269: usage(void)
1.1 deraadt 270: {
271:
272: #ifdef YP
1.15 aaron 273: (void)fprintf(stderr,
274: "usage: %s [-l%s] [-a list] [-s newshell] [user]\n",
275: __progname, use_yp ? "y" : "");
1.1 deraadt 276: #else
1.15 aaron 277: (void)fprintf(stderr, "usage: %s [-a list] [-s newshell] [user]\n",
1.13 millert 278: __progname);
1.1 deraadt 279: #endif
280: exit(1);
281: }