Annotation of src/usr.bin/chpass/chpass.c, Revision 1.9
1.9 ! kstailey 1: /* $OpenBSD: chpass.c,v 1.8 1997/02/13 17:28:39 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";
46: #else
1.9 ! kstailey 47: static char rcsid[] = "$OpenBSD: chpass.c,v 1.8 1997/02/13 17:28:39 deraadt Exp $";
1.1 deraadt 48: #endif
49: #endif /* not lint */
50:
51: #include <sys/param.h>
52: #include <sys/stat.h>
53: #include <sys/time.h>
54: #include <sys/resource.h>
55:
56: #include <ctype.h>
57: #include <err.h>
58: #include <errno.h>
59: #include <fcntl.h>
60: #include <pwd.h>
61: #include <stdio.h>
62: #include <stdlib.h>
63: #include <string.h>
64: #include <unistd.h>
1.2 deraadt 65: #include <util.h>
1.1 deraadt 66:
67: #include "chpass.h"
68: #include "pathnames.h"
69:
70: char *progname = "chpass";
71: char *tempname;
72: uid_t uid;
73:
74: #ifdef YP
75: int use_yp;
76: int force_yp = 0;
77: extern struct passwd *ypgetpwnam(), *ypgetpwuid();
78: #endif
79:
80: void baduser __P((void));
81: void usage __P((void));
82:
83: int
84: main(argc, argv)
85: int argc;
86: char **argv;
87: {
88: enum { NEWSH, LOADENTRY, EDITENTRY } op;
89: struct passwd *pw, lpw;
1.2 deraadt 90: int ch, pfd, tfd, dfd;
91: char *arg, tempname[] = "/etc/pw.XXXXXX";
1.1 deraadt 92:
93: #ifdef YP
94: use_yp = _yp_check(NULL);
95: #endif
96:
97: op = EDITENTRY;
1.7 millert 98: while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
1.1 deraadt 99: switch(ch) {
100: case 'a':
101: op = LOADENTRY;
102: arg = optarg;
103: break;
104: case 's':
105: op = NEWSH;
106: arg = optarg;
107: break;
108: #ifdef YP
109: case 'l':
110: use_yp = 0;
111: break;
112: case 'y':
113: if (!use_yp) {
114: warnx("YP not in use.");
115: usage();
116: }
117: force_yp = 1;
118: break;
119: #endif
120: case '?':
121: default:
122: usage();
123: }
124: argc -= optind;
125: argv += optind;
126:
127: #ifdef YP
128: if (op == LOADENTRY && use_yp)
129: errx(1, "cannot load entry using NIS.\n\tUse the -l flag to load local.");
130: #endif
131: uid = getuid();
132:
133: if (op == EDITENTRY || op == NEWSH)
134: switch(argc) {
135: case 0:
136: pw = getpwuid(uid);
137: #ifdef YP
138: if (pw && !force_yp)
139: use_yp = 0;
140: else if (use_yp)
141: pw = ypgetpwuid(uid);
142: #endif /* YP */
143: if (!pw)
144: errx(1, "unknown user: uid %u\n", uid);
145: break;
146: case 1:
147: pw = getpwnam(*argv);
148: #ifdef YP
149: if (pw && !force_yp)
150: use_yp = 0;
151: else if (use_yp)
152: pw = ypgetpwnam(*argv);
153: #endif /* YP */
154: if (!pw)
155: errx(1, "unknown user: %s", *argv);
156: if (uid && uid != pw->pw_uid)
157: baduser();
158: break;
159: default:
160: usage();
161: }
162:
163: if (op == NEWSH) {
164: /* protect p_shell -- it thinks NULL is /bin/sh */
165: if (!arg[0])
166: usage();
1.9 ! kstailey 167: if (p_shell(arg, pw, NULL))
! 168: pw_error(NULL, 0, 1);
1.1 deraadt 169: }
170:
171: if (op == LOADENTRY) {
172: if (uid)
173: baduser();
174: pw = &lpw;
1.9 ! kstailey 175: if (!pw_scan(arg, pw, NULL))
1.1 deraadt 176: exit(1);
177: }
178:
1.2 deraadt 179: /* Get the passwd lock file and open the passwd file for reading. */
1.1 deraadt 180: pw_init();
1.2 deraadt 181: tfd = pw_lock(0);
1.6 millert 182: if (tfd < 0) {
183: if (errno == EEXIST)
184: errx(1, "the passwd file is busy.");
185: else
186: err(1, "can't open passwd temp file");
187: }
1.2 deraadt 188: pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
189: if (pfd < 0)
190: pw_error(_PATH_MASTERPASSWD, 1, 1);
1.1 deraadt 191:
1.2 deraadt 192: /* Edit the user passwd information if requested. */
1.1 deraadt 193: if (op == EDITENTRY) {
1.2 deraadt 194: dfd = mkstemp(tempname);
195: if (dfd < 0)
196: pw_error(tempname, 1, 1);
197: display(tempname, dfd, pw);
198: edit(tempname, pw);
1.1 deraadt 199: (void)unlink(tempname);
200: }
1.2 deraadt 201:
1.1 deraadt 202: #ifdef YP
203: if (use_yp) {
1.4 deraadt 204: if (pw_yp(pw, uid)) {
1.9 ! kstailey 205: pw_error(NULL, 0, 1);
1.4 deraadt 206: exit(1);
1.5 deraadt 207: } else {
208: pw_abort();
1.1 deraadt 209: exit(0);
1.5 deraadt 210: }
1.4 deraadt 211: } else
1.1 deraadt 212: #endif /* YP */
1.8 deraadt 213: {
214: /* Copy the passwd file to the lock file, updating pw. */
215: pw_copy(pfd, tfd, pw);
1.2 deraadt 216:
1.8 deraadt 217: /* Now finish the passwd file update. */
218: if (pw_mkdb() < 0)
1.9 ! kstailey 219: pw_error(NULL, 0, 1);
1.8 deraadt 220: }
1.1 deraadt 221:
222: exit(0);
223: }
224:
225: void
226: baduser()
227: {
228:
229: errx(1, "%s", strerror(EACCES));
230: }
231:
232: void
233: usage()
234: {
235:
236: #ifdef YP
237: (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [-l]%s [user]\n", use_yp?" [-y]":"");
238: #else
239: (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n");
240: #endif
241: exit(1);
242: }