Annotation of src/usr.bin/chpass/chpass.c, Revision 1.29
1.29 ! espie 1: /* $OpenBSD: chpass.c,v 1.28 2003/07/01 01:01:28 avsm 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.
1.27 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 THE REGENTS OR CONTRIBUTORS 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:
33: #ifndef lint
34: static char copyright[] =
35: "@(#) Copyright (c) 1988, 1993, 1994\n\
36: The Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)chpass.c 8.4 (Berkeley) 4/2/94";
1.23 deraadt 42: #else
1.29 ! espie 43: static char rcsid[] = "$OpenBSD: chpass.c,v 1.28 2003/07/01 01:01:28 avsm Exp $";
1.1 deraadt 44: #endif
45: #endif /* not lint */
46:
47: #include <sys/param.h>
1.17 millert 48: #include <sys/resource.h>
1.1 deraadt 49: #include <sys/stat.h>
50: #include <sys/time.h>
1.17 millert 51: #include <sys/uio.h>
1.1 deraadt 52:
53: #include <err.h>
54: #include <errno.h>
55: #include <fcntl.h>
1.28 avsm 56: #include <paths.h>
1.1 deraadt 57: #include <pwd.h>
1.17 millert 58: #include <signal.h>
1.1 deraadt 59: #include <stdio.h>
60: #include <stdlib.h>
61: #include <string.h>
62: #include <unistd.h>
1.2 deraadt 63: #include <util.h>
1.1 deraadt 64:
65: #include "chpass.h"
66:
1.25 deraadt 67: extern char *__progname;
68:
1.17 millert 69: enum { NEWSH, LOADENTRY, EDITENTRY } op;
1.1 deraadt 70: uid_t uid;
71: #ifdef YP
1.25 deraadt 72: int use_yp;
73: int force_yp = 0;
1.1 deraadt 74: #endif
75:
1.21 millert 76: void baduser(void);
77: void kbintr(int);
78: void usage(void);
1.1 deraadt 79:
80: int
1.23 deraadt 81: main(int argc, char *argv[])
1.1 deraadt 82: {
1.23 deraadt 83: struct passwd *pw = NULL, lpw;
1.20 millert 84: int i, ch, pfd, tfd, dfd;
1.23 deraadt 85: char *arg = NULL;
1.17 millert 86: sigset_t fullset;
1.1 deraadt 87:
88: #ifdef YP
89: use_yp = _yp_check(NULL);
90: #endif
91:
92: op = EDITENTRY;
1.7 millert 93: while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
1.1 deraadt 94: switch(ch) {
95: case 'a':
96: op = LOADENTRY;
97: arg = optarg;
98: break;
99: case 's':
100: op = NEWSH;
101: arg = optarg;
102: break;
103: #ifdef YP
104: case 'l':
105: use_yp = 0;
106: break;
107: case 'y':
108: if (!use_yp) {
109: warnx("YP not in use.");
110: usage();
111: }
112: force_yp = 1;
113: break;
114: #endif
115: case '?':
116: default:
117: usage();
118: }
119: argc -= optind;
120: argv += optind;
121:
122: #ifdef YP
123: if (op == LOADENTRY && use_yp)
1.23 deraadt 124: errx(1, "cannot load using YP, use -l to load local.");
1.1 deraadt 125: #endif
126: uid = getuid();
127:
128: if (op == EDITENTRY || op == NEWSH)
129: switch(argc) {
130: case 0:
131: pw = getpwuid(uid);
132: #ifdef YP
133: if (pw && !force_yp)
134: use_yp = 0;
135: else if (use_yp)
136: pw = ypgetpwuid(uid);
137: #endif /* YP */
138: if (!pw)
1.22 mpech 139: errx(1, "unknown user: uid %u", uid);
1.1 deraadt 140: break;
141: case 1:
142: pw = getpwnam(*argv);
143: #ifdef YP
144: if (pw && !force_yp)
145: use_yp = 0;
146: else if (use_yp)
147: pw = ypgetpwnam(*argv);
148: #endif /* YP */
149: if (!pw)
150: errx(1, "unknown user: %s", *argv);
151: if (uid && uid != pw->pw_uid)
152: baduser();
153: break;
154: default:
155: usage();
156: }
157:
158: if (op == NEWSH) {
159: /* protect p_shell -- it thinks NULL is /bin/sh */
160: if (!arg[0])
161: usage();
1.9 kstailey 162: if (p_shell(arg, pw, NULL))
163: pw_error(NULL, 0, 1);
1.1 deraadt 164: }
165:
166: if (op == LOADENTRY) {
167: if (uid)
168: baduser();
169: pw = &lpw;
1.9 kstailey 170: if (!pw_scan(arg, pw, NULL))
1.1 deraadt 171: exit(1);
172: }
173:
1.2 deraadt 174: /* Edit the user passwd information if requested. */
1.1 deraadt 175: if (op == EDITENTRY) {
1.29 ! espie 176: char tempname[] = _PATH_VARTMP "pw.XXXXXXXXXX";
1.26 millert 177: int edit_status;
1.24 millert 178:
1.2 deraadt 179: dfd = mkstemp(tempname);
1.12 millert 180: if (dfd == -1 || fcntl(dfd, F_SETFD, 1) == -1)
1.2 deraadt 181: pw_error(tempname, 1, 1);
182: display(tempname, dfd, pw);
1.26 millert 183: edit_status = edit(tempname, pw);
1.24 millert 184: close(dfd);
185: unlink(tempname);
1.26 millert 186:
187: switch (edit_status) {
188: case EDIT_OK:
189: break;
190: case EDIT_NOCHANGE:
191: pw_error(NULL, 0, 0);
192: break;
193: case EDIT_ERROR:
194: default:
195: pw_error(tempname, 1, 1);
196: break;
197: }
1.1 deraadt 198: }
1.2 deraadt 199:
1.17 millert 200: /* Drop user's real uid and block all signals to avoid a DoS. */
201: setuid(0);
202: sigfillset(&fullset);
203: sigdelset(&fullset, SIGINT);
204: sigprocmask(SIG_BLOCK, &fullset, NULL);
205:
206: /* Get the passwd lock file and open the passwd file for reading. */
207: pw_init();
1.20 millert 208: for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
209: if (i == 4)
210: (void)fputs("Attempting lock password file, "
211: "please wait or press ^C to abort", stderr);
212: (void)signal(SIGINT, kbintr);
213: if (i % 16 == 0)
1.17 millert 214: fputc('.', stderr);
1.20 millert 215: usleep(250000);
1.17 millert 216: (void)signal(SIGINT, SIG_IGN);
217: }
1.20 millert 218: if (i >= 4)
219: fputc('\n', stderr);
1.17 millert 220: pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
221: if (pfd == -1 || fcntl(pfd, F_SETFD, 1) == -1)
222: pw_error(_PATH_MASTERPASSWD, 1, 1);
223:
1.1 deraadt 224: #ifdef YP
225: if (use_yp) {
1.17 millert 226: if (pw_yp(pw, uid))
1.9 kstailey 227: pw_error(NULL, 0, 1);
1.17 millert 228: else {
1.5 deraadt 229: pw_abort();
1.1 deraadt 230: exit(0);
1.5 deraadt 231: }
1.4 deraadt 232: } else
1.1 deraadt 233: #endif /* YP */
1.8 deraadt 234: {
235: /* Copy the passwd file to the lock file, updating pw. */
236: pw_copy(pfd, tfd, pw);
1.2 deraadt 237:
1.8 deraadt 238: /* Now finish the passwd file update. */
1.19 millert 239: if (pw_mkdb(pw->pw_name, 0) == -1)
1.9 kstailey 240: pw_error(NULL, 0, 1);
1.8 deraadt 241: }
1.1 deraadt 242:
243: exit(0);
244: }
245:
246: void
1.23 deraadt 247: baduser(void)
1.1 deraadt 248: {
249:
250: errx(1, "%s", strerror(EACCES));
1.14 millert 251: }
252:
253: void
1.23 deraadt 254: kbintr(int signo)
1.17 millert 255: {
256: struct iovec iv[5];
257:
258: iv[0].iov_base = "\n";
259: iv[0].iov_len = 1;
260: iv[1].iov_base = __progname;
261: iv[1].iov_len = strlen(__progname);
262: iv[2].iov_base = ": ";
263: iv[2].iov_len = 2;
264: iv[3].iov_base = _PATH_MASTERPASSWD;
265: iv[3].iov_len = sizeof(_PATH_MASTERPASSWD) - 1;
266: iv[4].iov_base = " unchanged\n";
267: iv[4].iov_len = 11;
268: writev(STDERR_FILENO, iv, 5);
269:
270: _exit(1);
1.1 deraadt 271: }
272:
273: void
1.23 deraadt 274: usage(void)
1.1 deraadt 275: {
276:
277: #ifdef YP
1.15 aaron 278: (void)fprintf(stderr,
279: "usage: %s [-l%s] [-a list] [-s newshell] [user]\n",
280: __progname, use_yp ? "y" : "");
1.1 deraadt 281: #else
1.15 aaron 282: (void)fprintf(stderr, "usage: %s [-a list] [-s newshell] [user]\n",
1.13 millert 283: __progname);
1.1 deraadt 284: #endif
285: exit(1);
286: }