Annotation of src/usr.bin/chpass/chpass.c, Revision 1.25
1.25 ! deraadt 1: /* $OpenBSD: chpass.c,v 1.24 2002/06/27 19:16:50 millert 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.25 ! deraadt 47: static char rcsid[] = "$OpenBSD: chpass.c,v 1.24 2002/06/27 19:16:50 millert 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.25 ! deraadt 71: extern char *__progname;
! 72:
1.17 millert 73: enum { NEWSH, LOADENTRY, EDITENTRY } op;
1.1 deraadt 74: uid_t uid;
75: #ifdef YP
1.25 ! deraadt 76: int use_yp;
! 77: int force_yp = 0;
1.1 deraadt 78: #endif
79:
1.21 millert 80: void baduser(void);
81: void kbintr(int);
82: void usage(void);
1.1 deraadt 83:
84: int
1.23 deraadt 85: main(int argc, char *argv[])
1.1 deraadt 86: {
1.23 deraadt 87: struct passwd *pw = NULL, lpw;
1.20 millert 88: int i, ch, pfd, tfd, dfd;
1.23 deraadt 89: char *arg = NULL;
1.17 millert 90: sigset_t fullset;
1.1 deraadt 91:
92: #ifdef YP
93: use_yp = _yp_check(NULL);
94: #endif
95:
96: op = EDITENTRY;
1.7 millert 97: while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
1.1 deraadt 98: switch(ch) {
99: case 'a':
100: op = LOADENTRY;
101: arg = optarg;
102: break;
103: case 's':
104: op = NEWSH;
105: arg = optarg;
106: break;
107: #ifdef YP
108: case 'l':
109: use_yp = 0;
110: break;
111: case 'y':
112: if (!use_yp) {
113: warnx("YP not in use.");
114: usage();
115: }
116: force_yp = 1;
117: break;
118: #endif
119: case '?':
120: default:
121: usage();
122: }
123: argc -= optind;
124: argv += optind;
125:
126: #ifdef YP
127: if (op == LOADENTRY && use_yp)
1.23 deraadt 128: errx(1, "cannot load using YP, use -l to load local.");
1.1 deraadt 129: #endif
130: uid = getuid();
131:
132: if (op == EDITENTRY || op == NEWSH)
133: switch(argc) {
134: case 0:
135: pw = getpwuid(uid);
136: #ifdef YP
137: if (pw && !force_yp)
138: use_yp = 0;
139: else if (use_yp)
140: pw = ypgetpwuid(uid);
141: #endif /* YP */
142: if (!pw)
1.22 mpech 143: errx(1, "unknown user: uid %u", uid);
1.1 deraadt 144: break;
145: case 1:
146: pw = getpwnam(*argv);
147: #ifdef YP
148: if (pw && !force_yp)
149: use_yp = 0;
150: else if (use_yp)
151: pw = ypgetpwnam(*argv);
152: #endif /* YP */
153: if (!pw)
154: errx(1, "unknown user: %s", *argv);
155: if (uid && uid != pw->pw_uid)
156: baduser();
157: break;
158: default:
159: usage();
160: }
161:
162: if (op == NEWSH) {
163: /* protect p_shell -- it thinks NULL is /bin/sh */
164: if (!arg[0])
165: usage();
1.9 kstailey 166: if (p_shell(arg, pw, NULL))
167: pw_error(NULL, 0, 1);
1.1 deraadt 168: }
169:
170: if (op == LOADENTRY) {
171: if (uid)
172: baduser();
173: pw = &lpw;
1.9 kstailey 174: if (!pw_scan(arg, pw, NULL))
1.1 deraadt 175: exit(1);
176: }
177:
1.2 deraadt 178: /* Edit the user passwd information if requested. */
1.1 deraadt 179: if (op == EDITENTRY) {
1.24 millert 180: char tempname[] = __CONCAT(_PATH_VARTMP,"pw.XXXXXXXX");
181:
1.2 deraadt 182: dfd = mkstemp(tempname);
1.12 millert 183: if (dfd == -1 || fcntl(dfd, F_SETFD, 1) == -1)
1.2 deraadt 184: pw_error(tempname, 1, 1);
185: display(tempname, dfd, pw);
186: edit(tempname, pw);
1.24 millert 187: close(dfd);
188: unlink(tempname);
1.1 deraadt 189: }
1.2 deraadt 190:
1.17 millert 191: /* Drop user's real uid and block all signals to avoid a DoS. */
192: setuid(0);
193: sigfillset(&fullset);
194: sigdelset(&fullset, SIGINT);
195: sigprocmask(SIG_BLOCK, &fullset, NULL);
196:
197: /* Get the passwd lock file and open the passwd file for reading. */
198: pw_init();
1.20 millert 199: for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
200: if (i == 4)
201: (void)fputs("Attempting lock password file, "
202: "please wait or press ^C to abort", stderr);
203: (void)signal(SIGINT, kbintr);
204: if (i % 16 == 0)
1.17 millert 205: fputc('.', stderr);
1.20 millert 206: usleep(250000);
1.17 millert 207: (void)signal(SIGINT, SIG_IGN);
208: }
1.20 millert 209: if (i >= 4)
210: fputc('\n', stderr);
1.17 millert 211: pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
212: if (pfd == -1 || fcntl(pfd, F_SETFD, 1) == -1)
213: pw_error(_PATH_MASTERPASSWD, 1, 1);
214:
1.1 deraadt 215: #ifdef YP
216: if (use_yp) {
1.17 millert 217: if (pw_yp(pw, uid))
1.9 kstailey 218: pw_error(NULL, 0, 1);
1.17 millert 219: else {
1.5 deraadt 220: pw_abort();
1.1 deraadt 221: exit(0);
1.5 deraadt 222: }
1.4 deraadt 223: } else
1.1 deraadt 224: #endif /* YP */
1.8 deraadt 225: {
226: /* Copy the passwd file to the lock file, updating pw. */
227: pw_copy(pfd, tfd, pw);
1.2 deraadt 228:
1.8 deraadt 229: /* Now finish the passwd file update. */
1.19 millert 230: if (pw_mkdb(pw->pw_name, 0) == -1)
1.9 kstailey 231: pw_error(NULL, 0, 1);
1.8 deraadt 232: }
1.1 deraadt 233:
234: exit(0);
235: }
236:
237: void
1.23 deraadt 238: baduser(void)
1.1 deraadt 239: {
240:
241: errx(1, "%s", strerror(EACCES));
1.14 millert 242: }
243:
244: void
1.23 deraadt 245: kbintr(int signo)
1.17 millert 246: {
247: struct iovec iv[5];
248:
249: iv[0].iov_base = "\n";
250: iv[0].iov_len = 1;
251: iv[1].iov_base = __progname;
252: iv[1].iov_len = strlen(__progname);
253: iv[2].iov_base = ": ";
254: iv[2].iov_len = 2;
255: iv[3].iov_base = _PATH_MASTERPASSWD;
256: iv[3].iov_len = sizeof(_PATH_MASTERPASSWD) - 1;
257: iv[4].iov_base = " unchanged\n";
258: iv[4].iov_len = 11;
259: writev(STDERR_FILENO, iv, 5);
260:
261: _exit(1);
1.1 deraadt 262: }
263:
264: void
1.23 deraadt 265: usage(void)
1.1 deraadt 266: {
267:
268: #ifdef YP
1.15 aaron 269: (void)fprintf(stderr,
270: "usage: %s [-l%s] [-a list] [-s newshell] [user]\n",
271: __progname, use_yp ? "y" : "");
1.1 deraadt 272: #else
1.15 aaron 273: (void)fprintf(stderr, "usage: %s [-a list] [-s newshell] [user]\n",
1.13 millert 274: __progname);
1.1 deraadt 275: #endif
276: exit(1);
277: }