Annotation of src/usr.bin/chpass/chpass.c, Revision 1.32
1.32 ! deraadt 1: /* $OpenBSD: chpass.c,v 1.31 2004/05/10 20:05:48 wilfried 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.32 ! deraadt 43: static char rcsid[] = "$OpenBSD: chpass.c,v 1.31 2004/05/10 20:05:48 wilfried 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.30 millert 83: struct passwd *pw = NULL, *opw, 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 == LOADENTRY) {
159: if (uid)
160: baduser();
161: pw = &lpw;
1.9 kstailey 162: if (!pw_scan(arg, pw, NULL))
1.1 deraadt 163: exit(1);
164: }
1.30 millert 165: if ((opw = pw_dup(pw)) == NULL)
166: err(1, NULL);
1.1 deraadt 167:
1.2 deraadt 168: /* Edit the user passwd information if requested. */
1.1 deraadt 169: if (op == EDITENTRY) {
1.29 espie 170: char tempname[] = _PATH_VARTMP "pw.XXXXXXXXXX";
1.26 millert 171: int edit_status;
1.24 millert 172:
1.30 millert 173: if ((pw = pw_dup(pw)) == NULL)
174: pw_error(NULL, 1, 1);
1.2 deraadt 175: dfd = mkstemp(tempname);
1.12 millert 176: if (dfd == -1 || fcntl(dfd, F_SETFD, 1) == -1)
1.2 deraadt 177: pw_error(tempname, 1, 1);
178: display(tempname, dfd, pw);
1.26 millert 179: edit_status = edit(tempname, pw);
1.24 millert 180: close(dfd);
181: unlink(tempname);
1.26 millert 182:
183: switch (edit_status) {
184: case EDIT_OK:
185: break;
186: case EDIT_NOCHANGE:
187: pw_error(NULL, 0, 0);
188: break;
189: case EDIT_ERROR:
190: default:
191: pw_error(tempname, 1, 1);
192: break;
193: }
1.31 wilfried 194: }
195:
196: if (op == NEWSH) {
197: /* protect p_shell -- it thinks NULL is /bin/sh */
198: if (!arg[0])
199: usage();
200: if (p_shell(arg, pw, NULL))
201: pw_error(NULL, 0, 1);
1.1 deraadt 202: }
1.2 deraadt 203:
1.17 millert 204: /* Drop user's real uid and block all signals to avoid a DoS. */
205: setuid(0);
206: sigfillset(&fullset);
207: sigdelset(&fullset, SIGINT);
208: sigprocmask(SIG_BLOCK, &fullset, NULL);
209:
210: /* Get the passwd lock file and open the passwd file for reading. */
211: pw_init();
1.20 millert 212: for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
213: if (i == 4)
214: (void)fputs("Attempting lock password file, "
215: "please wait or press ^C to abort", stderr);
216: (void)signal(SIGINT, kbintr);
217: if (i % 16 == 0)
1.17 millert 218: fputc('.', stderr);
1.20 millert 219: usleep(250000);
1.17 millert 220: (void)signal(SIGINT, SIG_IGN);
221: }
1.20 millert 222: if (i >= 4)
223: fputc('\n', stderr);
1.17 millert 224: pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
225: if (pfd == -1 || fcntl(pfd, F_SETFD, 1) == -1)
226: pw_error(_PATH_MASTERPASSWD, 1, 1);
227:
1.1 deraadt 228: #ifdef YP
229: if (use_yp) {
1.17 millert 230: if (pw_yp(pw, uid))
1.9 kstailey 231: pw_error(NULL, 0, 1);
1.17 millert 232: else {
1.5 deraadt 233: pw_abort();
1.1 deraadt 234: exit(0);
1.5 deraadt 235: }
1.4 deraadt 236: } else
1.1 deraadt 237: #endif /* YP */
1.8 deraadt 238: {
239: /* Copy the passwd file to the lock file, updating pw. */
1.30 millert 240: pw_copy(pfd, tfd, pw, opw);
241:
242: /* If username changed we need to rebuild the entire db. */
243: arg = !strcmp(opw->pw_name, pw->pw_name) ? pw->pw_name : NULL;
1.2 deraadt 244:
1.8 deraadt 245: /* Now finish the passwd file update. */
1.30 millert 246: if (pw_mkdb(arg, 0) == -1)
1.9 kstailey 247: pw_error(NULL, 0, 1);
1.8 deraadt 248: }
1.1 deraadt 249:
250: exit(0);
251: }
252:
253: void
1.23 deraadt 254: baduser(void)
1.1 deraadt 255: {
256:
257: errx(1, "%s", strerror(EACCES));
1.14 millert 258: }
259:
1.32 ! deraadt 260: /* ARGSUSED */
1.14 millert 261: void
1.23 deraadt 262: kbintr(int signo)
1.17 millert 263: {
264: struct iovec iv[5];
265:
266: iv[0].iov_base = "\n";
267: iv[0].iov_len = 1;
268: iv[1].iov_base = __progname;
269: iv[1].iov_len = strlen(__progname);
270: iv[2].iov_base = ": ";
271: iv[2].iov_len = 2;
272: iv[3].iov_base = _PATH_MASTERPASSWD;
273: iv[3].iov_len = sizeof(_PATH_MASTERPASSWD) - 1;
274: iv[4].iov_base = " unchanged\n";
275: iv[4].iov_len = 11;
276: writev(STDERR_FILENO, iv, 5);
277:
278: _exit(1);
1.1 deraadt 279: }
280:
281: void
1.23 deraadt 282: usage(void)
1.1 deraadt 283: {
284:
285: #ifdef YP
1.15 aaron 286: (void)fprintf(stderr,
287: "usage: %s [-l%s] [-a list] [-s newshell] [user]\n",
288: __progname, use_yp ? "y" : "");
1.1 deraadt 289: #else
1.15 aaron 290: (void)fprintf(stderr, "usage: %s [-a list] [-s newshell] [user]\n",
1.13 millert 291: __progname);
1.1 deraadt 292: #endif
293: exit(1);
294: }