4.4BSD snapshot (revision 8.1)
[unix-history] / usr / src / usr.bin / chpass / chpass.c
CommitLineData
59f1a2c1 1/*-
8b35ab41
KB
2 * Copyright (c) 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
59f1a2c1 5 * %sccs.include.redist.c%
8b35ab41
KB
6 */
7
8#ifndef lint
9char copyright[] =
10"@(#) Copyright (c) 1988 The Regents of the University of California.\n\
11 All rights reserved.\n";
12#endif /* not lint */
13
14#ifndef lint
456319a0 15static char sccsid[] = "@(#)chpass.c 5.17 (Berkeley) %G%";
8b35ab41
KB
16#endif /* not lint */
17
18#include <sys/param.h>
8b35ab41
KB
19#include <sys/stat.h>
20#include <sys/signal.h>
21#include <sys/time.h>
22#include <sys/resource.h>
bc3d7b01 23#include <fcntl.h>
b7eb8299 24#include <pwd.h>
8b35ab41
KB
25#include <errno.h>
26#include <stdio.h>
27#include <ctype.h>
59f1a2c1 28#include <string.h>
435e8dff
KB
29#include "chpass.h"
30#include "pathnames.h"
8b35ab41 31
bc3d7b01
KB
32char *progname = "chpass";
33char *tempname;
b7eb8299 34uid_t uid;
8b35ab41
KB
35
36main(argc, argv)
37 int argc;
38 char **argv;
39{
bc3d7b01 40 extern int optind;
b7eb8299 41 extern char *optarg;
bc3d7b01
KB
42 register enum { NEWSH, LOADENTRY, EDITENTRY } op;
43 register struct passwd *pw;
44 struct passwd lpw;
45 int ch, pfd, tfd;
46 char *arg;
8b35ab41 47
bc3d7b01 48 op = EDITENTRY;
34dda72c 49 while ((ch = getopt(argc, argv, "a:s:")) != EOF)
b7eb8299
KB
50 switch(ch) {
51 case 'a':
bc3d7b01
KB
52 op = LOADENTRY;
53 arg = optarg;
b7eb8299 54 break;
34dda72c 55 case 's':
bc3d7b01
KB
56 op = NEWSH;
57 arg = optarg;
34dda72c 58 break;
b7eb8299
KB
59 case '?':
60 default:
61 usage();
8b35ab41 62 }
b7eb8299
KB
63 argc -= optind;
64 argv += optind;
65
bc3d7b01
KB
66 uid = getuid();
67
68 if (op == EDITENTRY || op == NEWSH)
b7eb8299
KB
69 switch(argc) {
70 case 0:
71 if (!(pw = getpwuid(uid))) {
72 (void)fprintf(stderr,
73 "chpass: unknown user: uid %u\n", uid);
74 exit(1);
75 }
76 break;
77 case 1:
78 if (!(pw = getpwnam(*argv))) {
79 (void)fprintf(stderr,
80 "chpass: unknown user %s.\n", *argv);
81 exit(1);
82 }
34dda72c
KB
83 if (uid && uid != pw->pw_uid)
84 baduser();
b7eb8299
KB
85 break;
86 default:
87 usage();
8b35ab41 88 }
8b35ab41 89
bc3d7b01
KB
90 if (op == NEWSH) {
91 /* protect p_shell -- it thinks NULL is /bin/sh */
92 if (!arg[0])
93 usage();
94 if (p_shell(arg, pw, (ENTRY *)NULL))
95 pw_error((char *)NULL, 0, 1);
34dda72c 96 }
8b35ab41 97
bc3d7b01
KB
98 if (op == LOADENTRY) {
99 if (uid)
100 baduser();
101 pw = &lpw;
102 if (!pw_scan(arg, pw))
8b35ab41 103 exit(1);
8b35ab41 104 }
7df40547 105
b7eb8299 106 /*
456319a0 107 * The temporary file/file descriptor usage is a little tricky here.
bc3d7b01 108 * 1: We start off with two fd's, one for the master password
456319a0
KB
109 * file (used to lock everything), and one for a temporary file.
110 * 2: Display() gets an fp for the temporary file, and copies the
111 * user's information into it. It then gives the temporary file
112 * to the user and closes the fp, closing the underlying fd.
bc3d7b01 113 * 3: The user edits the temporary file some number of times.
456319a0
KB
114 * 4: Verify() gets an fp for the temporary file, and verifies the
115 * contents. It can't use an fp derived from the step #2 fd,
116 * because the user's editor may have created a new instance of
117 * the file. Once the file is verified, its contents are stored
118 * in a password structure. The verify routine closes the fp,
119 * closing the underlying fd.
120 * 5: Delete the temporary file.
121 * 6: Get a new temporary file/fd. Pw_copy() gets an fp for it
122 * file and copies the master password file into it, replacing
123 * the user record with a new one. We can't use the first
124 * temporary file for this because it was owned by the user.
125 * Pw_copy() closes its fp, flushing the data and closing the
126 * underlying file descriptor. We can't close the master
127 * password fp, or we'd lose the lock.
128 * 7: Call pw_mkdb() (which renames the temporary file) and exit.
129 * The exit closes the master passwd fp/fd.
b7eb8299 130 */
bc3d7b01
KB
131 pw_init();
132 pfd = pw_lock();
133 tfd = pw_tmp();
8b35ab41 134
456319a0
KB
135 if (op == EDITENTRY) {
136 display(tfd, pw);
137 edit(pw);
138 (void)unlink(tempname);
139 tfd = pw_tmp();
140 }
141
142 pw_copy(pfd, tfd, pw);
b7eb8299 143
bc3d7b01
KB
144 if (!pw_mkdb())
145 pw_error((char *)NULL, 0, 1);
146 exit(0);
7df40547
KB
147}
148
34dda72c
KB
149baduser()
150{
151 (void)fprintf(stderr, "chpass: %s\n", strerror(EACCES));
152 exit(1);
153}
154
b7eb8299
KB
155usage()
156{
34dda72c 157 (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n");
b7eb8299
KB
158 exit(1);
159}