]> git.saurik.com Git - apple/system_cmds.git/blame - chpass.tproj/chpass.c
system_cmds-230.7.tar.gz
[apple/system_cmds.git] / chpass.tproj / chpass.c
CommitLineData
1815bff5
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6d658acd
A
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
1815bff5
A
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
6d658acd
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1815bff5
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*-
26 * Copyright (c) 1990, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58#ifndef lint
59static char copyright[] =
60"@(#) Copyright (c) 1988, 1993, 1994\n\
61 The Regents of the University of California. All rights reserved.\n";
62#endif /* not lint */
63
64#include <sys/param.h>
65#include <sys/stat.h>
66#include <sys/signal.h>
67#include <sys/time.h>
68#include <sys/resource.h>
69
70#include <ctype.h>
71#include <err.h>
72#include <errno.h>
73#include <fcntl.h>
74#include <pwd.h>
75#include <stdio.h>
76#include <stdlib.h>
77#include <string.h>
78#include <unistd.h>
79
80#include <pw_scan.h>
81#include <pw_util.h>
82#include "pw_copy.h"
83
84#include "chpass.h"
85#include "pathnames.h"
86
87char *progname = "chpass";
88char *tempname;
89uid_t uid;
90
91void baduser __P((void));
92void usage __P((void));
93
94int
95main(argc, argv)
96 int argc;
97 char **argv;
98{
99 enum { NEWSH, LOADENTRY, EDITENTRY } op;
100 struct passwd *pw, lpw;
101 int ch, pfd, tfd;
102 char *arg;
103
104 op = EDITENTRY;
105 while ((ch = getopt(argc, argv, "a:s:")) != EOF)
106 switch(ch) {
107 case 'a':
108 op = LOADENTRY;
109 arg = optarg;
110 break;
111 case 's':
112 op = NEWSH;
113 arg = optarg;
114 break;
115 case '?':
116 default:
117 usage();
118 }
119 argc -= optind;
120 argv += optind;
121
122 uid = getuid();
123
124 if (op == EDITENTRY || op == NEWSH)
125 switch(argc) {
126 case 0:
127 if (!(pw = getpwuid(uid)))
128 errx(1, "unknown user: uid %u", uid);
129 break;
130 case 1:
131 if (!(pw = getpwnam(*argv)))
132 errx(1, "unknown user: %s", *argv);
133 if (uid && uid != pw->pw_uid)
134 baduser();
135 break;
136 default:
137 usage();
138 }
139
140 if (op == NEWSH) {
141 /* protect p_shell -- it thinks NULL is /bin/sh */
142 if (!arg[0])
143 usage();
144 if (p_shell(arg, pw, (ENTRY *)NULL))
145 pw_error((char *)NULL, 0, 1);
146 }
147
148 if (op == LOADENTRY) {
149 if (uid)
150 baduser();
151 pw = &lpw;
152 if (!pw_scan(arg, pw))
153 exit(1);
154 }
155
156 /*
157 * The temporary file/file descriptor usage is a little tricky here.
158 * 1: We start off with two fd's, one for the master password
159 * file (used to lock everything), and one for a temporary file.
160 * 2: Display() gets an fp for the temporary file, and copies the
161 * user's information into it. It then gives the temporary file
162 * to the user and closes the fp, closing the underlying fd.
163 * 3: The user edits the temporary file some number of times.
164 * 4: Verify() gets an fp for the temporary file, and verifies the
165 * contents. It can't use an fp derived from the step #2 fd,
166 * because the user's editor may have created a new instance of
167 * the file. Once the file is verified, its contents are stored
168 * in a password structure. The verify routine closes the fp,
169 * closing the underlying fd.
170 * 5: Delete the temporary file.
171 * 6: Get a new temporary file/fd. Pw_copy() gets an fp for it
172 * file and copies the master password file into it, replacing
173 * the user record with a new one. We can't use the first
174 * temporary file for this because it was owned by the user.
175 * Pw_copy() closes its fp, flushing the data and closing the
176 * underlying file descriptor. We can't close the master
177 * password fp, or we'd lose the lock.
178 * 7: Call pw_mkdb() (which renames the temporary file) and exit.
179 * The exit closes the master passwd fp/fd.
180 */
181 pw_init();
182 pfd = pw_lock();
183 tfd = pw_tmp();
184
185 if (op == EDITENTRY) {
186 display(tfd, pw);
187 edit(pw);
188 (void)unlink(tempname);
189 tfd = pw_tmp();
190 }
191
192 pw_copy(pfd, tfd, pw);
193
194 if (!pw_mkdb())
195 pw_error((char *)NULL, 0, 1);
196 exit(0);
197}
198
199void
200baduser()
201{
202
203 errx(1, "%s", strerror(EACCES));
204}
205
206void
207usage()
208{
209
210 (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n");
211 exit(1);
212}