4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley 4.3 BSD
31 * under license from the Regents of the University of California.
34 #pragma ident "%Z%%M% %I% %E% SMI"
36 #include <sys/types.h>
39 #include <sys/fcntl.h>
48 * Password file editor with locking.
51 #define DEFAULT_EDITOR "/usr/bin/vi"
53 static int copyfile(char *, char *);
54 static int editfile(char *, char *, char *, time_t *);
55 static int sanity_check(char *, time_t *, char *);
56 static int validsh(char *);
58 char *ptemp
= "/etc/ptmp";
59 char *stemp
= "/etc/stmp";
60 char *passwd
= "/etc/passwd";
61 char *shadow
= "/etc/shadow";
71 time_t o_mtime
, n_mtime
;
72 struct stat osbuf
, sbuf
, oshdbuf
, shdbuf
;
75 (void)signal(SIGINT
, SIG_IGN
);
76 (void)signal(SIGQUIT
, SIG_IGN
);
77 (void)signal(SIGHUP
, SIG_IGN
);
78 setbuf(stderr
, (char *)NULL
);
80 editor
= getenv("VISUAL");
82 editor
= getenv("EDITOR");
84 editor
= DEFAULT_EDITOR
;
87 if (stat(passwd
, &osbuf
) < 0) {
88 (void)fprintf(stderr
,"vipw: can't stat passwd file.\n");
92 if (copyfile(passwd
, ptemp
))
95 if (stat(ptemp
, &sbuf
) < 0) {
97 "vipw: can't stat ptemp file, %s unchanged\n",
102 o_mtime
= sbuf
.st_mtime
;
104 if (editfile(editor
, ptemp
, passwd
, &n_mtime
)) {
105 if (sanity_check(ptemp
, &n_mtime
, passwd
))
107 if (o_mtime
>= n_mtime
)
112 if (o_mtime
< n_mtime
) {
113 fprintf(stdout
, "\nYou have modified the password file.\n");
115 "Press 'e' to edit the shadow file for consistency,\n 'q' to quit: ");
116 if ((c
= getchar()) == 'q') {
117 if (chmod(ptemp
, (osbuf
.st_mode
& 0644)) < 0) {
118 (void) fprintf(stderr
, "vipw: %s: ", ptemp
);
122 if (rename(ptemp
, passwd
) < 0) {
123 (void) fprintf(stderr
, "vipw: %s: ", ptemp
);
127 if (((osbuf
.st_gid
!= sbuf
.st_gid
) ||
128 (osbuf
.st_uid
!= sbuf
.st_uid
)) &&
129 (chown(passwd
, osbuf
.st_uid
, osbuf
.st_gid
) < 0)) {
130 (void) fprintf(stderr
, "vipw: %s ", ptemp
);
134 } else if (c
== 'e') {
135 if (stat(shadow
, &oshdbuf
) < 0) {
136 (void) fprintf(stderr
,
137 "vipw: can't stat shadow file.\n");
141 if (copyfile(shadow
, stemp
))
143 if (stat(stemp
, &shdbuf
) < 0) {
144 (void) fprintf(stderr
,
145 "vipw: can't stat stmp file.\n");
149 if (editfile(editor
, stemp
, shadow
, &o_mtime
))
152 if (chmod(ptemp
, (osbuf
.st_mode
& 0644)) < 0) {
153 (void) fprintf(stderr
, "vipw: %s: ", ptemp
);
157 if (chmod(stemp
, (oshdbuf
.st_mode
& 0400)) < 0) {
158 (void) fprintf(stderr
, "vipw: %s: ", stemp
);
162 if (rename(ptemp
, passwd
) < 0) {
163 (void) fprintf(stderr
, "vipw: %s: ", ptemp
);
167 if (((osbuf
.st_gid
!= sbuf
.st_gid
) ||
168 (osbuf
.st_uid
!= sbuf
.st_uid
)) &&
169 (chown(passwd
, osbuf
.st_uid
, osbuf
.st_gid
) < 0)) {
170 (void) fprintf(stderr
, "vipw: %s ", ptemp
);
173 if (rename(stemp
, shadow
) < 0) {
174 (void) fprintf(stderr
, "vipw: %s: ", stemp
);
177 } else if (((oshdbuf
.st_gid
!= shdbuf
.st_gid
) ||
178 (oshdbuf
.st_uid
!= shdbuf
.st_uid
)) &&
179 (chown(shadow
, oshdbuf
.st_uid
, oshdbuf
.st_gid
) < 0)) {
180 (void) fprintf(stderr
, "vipw: %s ", stemp
);
186 (void) unlink(ptemp
);
187 (void) unlink(stemp
);
194 copyfile(char *from
, char *to
)
199 fd
= open(to
, O_WRONLY
|O_CREAT
|O_EXCL
, 0600);
201 if (errno
== EEXIST
) {
202 (void) fprintf(stderr
, "vipw: %s file busy\n", from
);
205 (void) fprintf(stderr
, "vipw: "); perror(to
);
208 ft
= fdopen(fd
, "w");
210 (void) fprintf(stderr
, "vipw: "); perror(to
);
213 fp
= fopen(from
, "r");
215 (void) fprintf(stderr
, "vipw: "); perror(from
);
218 while (fgets(buf
, sizeof (buf
) - 1, fp
) != NULL
)
226 editfile(char *editor
, char *temp
, char *orig
, time_t *mtime
)
228 (void)sprintf(buf
, "%s %s", editor
, temp
);
229 if (system(buf
) == 0) {
230 return (sanity_check(temp
, mtime
, orig
));
237 validsh(char *rootsh
)
240 char *sh
, *getusershell();
244 while((sh
= getusershell()) != NULL
) {
245 if( strcmp( rootsh
, sh
) == 0 ) {
256 * return 0 if ok, 1 otherwise
259 sanity_check(char *temp
, time_t *mtime
, char *orig
)
263 struct stat sbuf
, statbuf
;
267 if (!strcmp(orig
, shadow
))
271 if (stat(temp
, &sbuf
) < 0) {
272 (void)fprintf(stderr
,
273 "vipw: can't stat %s file, %s unchanged\n",
277 *mtime
= sbuf
.st_mtime
;
278 if (sbuf
.st_size
== 0) {
279 (void)fprintf(stderr
, "vipw: bad %s file, %s unchanged\n",
283 ft
= fopen(temp
, "r");
285 (void)fprintf(stderr
,
286 "vipw: can't reopen %s file, %s unchanged\n",
291 while (fgets(buf
, sizeof (buf
) - 1, ft
) != NULL
) {
294 cp
= index(buf
, '\n');
296 continue; /* ??? allow very long lines
297 * and passwd files that do
298 * not end in '\n' ???
302 cp
= index(buf
, ':');
303 if (cp
== 0) /* lines without colon
309 if (strcmp(buf
, "root"))
314 cp
= index(cp
+ 1, ':');
318 /* root uid for password */
320 if (atoi(cp
+ 1) != 0) {
322 (void)fprintf(stderr
, "root UID != 0:\n%s\n",
326 /* root uid for passwd and sp_lstchg for shadow */
327 cp
= index(cp
+ 1, ':');
331 /* root's gid for passwd and sp_min for shadow*/
332 cp
= index(cp
+ 1, ':');
336 /* root's gecos for passwd and sp_max for shadow*/
337 cp
= index(cp
+ 1, ':');
340 if ((cp
= index(cp
+ 1, ':')) == 0)
344 bad_root
: (void)fprintf(stderr
,
345 "Missing fields in root entry:\n%s\n", buf
);
350 /* root's login directory */
356 if (stat(ldir
, &statbuf
) < 0) {
358 (void) fprintf(stderr
,
359 "root login dir doesn't exist:\n%s\n",
362 } else if (!S_ISDIR(statbuf
.st_mode
)) {
364 (void) fprintf(stderr
,
365 "root login dir is not a directory:\n%s\n",
371 /* root's login shell */
373 if (*cp
&& ! validsh(cp
)) {
374 (void)fprintf(stderr
,
375 "Invalid root shell:\n%s\n", buf
);
386 (void)fprintf(stderr
,
387 "vipw: you mangled the %s file, %s unchanged\n",