xref: /freebsd/usr.bin/chkey/chkey.c (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user or with the express written consent of
8  * Sun Microsystems, Inc.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 
31 #if 0
32 #ifndef lint
33 static char sccsid[] = "@(#)chkey.c 1.7 91/03/11 Copyr 1986 Sun Micro";
34 #endif
35 #endif
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 /*
41  * Copyright (C) 1986, Sun Microsystems, Inc.
42  */
43 
44 /*
45  * Command to change one's public key in the public key database
46  */
47 #include <rpc/rpc.h>
48 #include <rpc/key_prot.h>
49 #ifdef YP
50 #include <rpcsvc/yp_prot.h>
51 #include <rpcsvc/ypclnt.h>
52 #else
53 #define	YPOP_STORE	4
54 #endif
55 #include <sys/fcntl.h>
56 #include <err.h>
57 #include <pwd.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 
63 #include "extern.h"
64 
65 #ifdef YPPASSWD
66 struct passwd *ypgetpwuid(uid_t);
67 #endif
68 
69 #ifdef YP
70 static char *domain;
71 static char PKMAP[] = "publickey.byname";
72 #else
73 static char PKFILE[] = "/etc/publickey";
74 #endif	/* YP */
75 static char ROOTKEY[] = "/etc/.rootkey";
76 
77 static void usage(void);
78 extern int yp_update(char *, char *, int, char *, size_t, char *, size_t);
79 
80 int
81 main(int argc, char **argv)
82 {
83 	char name[MAXNETNAMELEN+1];
84 	char public[HEXKEYBYTES + 1];
85 	char secret[HEXKEYBYTES + 1];
86 	char crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
87 	char crypt2[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
88 	int status;
89 	char *pass;
90 	struct passwd *pw;
91 	uid_t uid;
92 	int force = 0;
93 	int ch;
94 #ifdef YP
95 	char *master;
96 #endif
97 #ifdef YPPASSWD
98 	char *cryptpw;
99 #endif
100 
101 	while ((ch = getopt(argc, argv, "f")) != -1)
102 		switch(ch) {
103 		case 'f':
104 			force = 1;
105 			break;
106 		default:
107 			usage();
108 		}
109 	argc -= optind;
110 	argv += optind;
111 
112 	if (argc != 0)
113 		usage();
114 
115 #ifdef YP
116 	(void)yp_get_default_domain(&domain);
117 	if (yp_master(domain, PKMAP, &master) != 0)
118 		errx(1, "can't find master of publickey database");
119 #endif
120 	uid = getuid() /*geteuid()*/;
121 	if (uid == 0) {
122 		if (host2netname(name, NULL, NULL) == 0)
123 			errx(1, "cannot convert hostname to netname");
124 	} else {
125 		if (user2netname(name, uid, NULL) == 0)
126 			errx(1, "cannot convert username to netname");
127 	}
128 	(void)printf("Generating new key for %s.\n", name);
129 
130 	if (!force) {
131 		if (uid != 0) {
132 #ifdef YPPASSWD
133 			pw = ypgetpwuid(uid);
134 #else
135 			pw = getpwuid(uid);
136 #endif
137 			if (pw == NULL) {
138 #ifdef YPPASSWD
139 				errx(1,
140 			"no NIS password entry found: can't change key");
141 #else
142 				errx(1,
143 			"no password entry found: can't change key");
144 #endif
145 			}
146 		} else {
147 			pw = getpwuid(0);
148 			if (pw == NULL)
149 			  errx(1, "no password entry found: can't change key");
150 		}
151 	}
152 	pass = getpass("Password:");
153 #ifdef YPPASSWD
154 	if (!force) {
155 		cryptpw = crypt(pass, pw->pw_passwd);
156 		if (cryptpw == NULL || strcmp(cryptpw, pw->pw_passwd) != 0)
157 			errx(1, "invalid password");
158 	}
159 #else
160 	force = 1;	/* Make this mandatory */
161 #endif
162 	genkeys(public, secret, pass);
163 
164 	memcpy(crypt1, secret, HEXKEYBYTES);
165 	memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE);
166 	crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
167 	xencrypt(crypt1, pass);
168 
169 	if (force) {
170 		memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1);
171 		xdecrypt(crypt2, getpass("Retype password:"));
172 		if (memcmp(crypt2, crypt2 + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0
173 			|| memcmp(crypt2, secret, HEXKEYBYTES) != 0)
174 			errx(1, "password incorrect");
175 	}
176 
177 #ifdef YP
178 	(void)printf("Sending key change request to %s...\n", master);
179 #endif
180 	status = setpublicmap(name, public, crypt1);
181 	if (status != 0) {
182 #ifdef YP
183 		errx(1, "unable to update NIS database (%u): %s",
184 				status, yperr_string(status));
185 #else
186 		errx(1, "unable to update publickey database");
187 #endif
188 	}
189 
190 	if (uid == 0) {
191 		/*
192 		 * Root users store their key in /etc/$ROOTKEY so
193 		 * that they can auto reboot without having to be
194 		 * around to type a password. Storing this in a file
195 		 * is rather dubious: it should really be in the EEPROM
196 		 * so it does not go over the net.
197 		 */
198 		int fd;
199 
200 		fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
201 		if (fd < 0) {
202 			warn("%s", ROOTKEY);
203 		} else {
204 			char newline = '\n';
205 
206 			if (write(fd, secret, strlen(secret)) < 0 ||
207 			    write(fd, &newline, sizeof(newline)) < 0)
208 				warn("%s: write", ROOTKEY);
209 		}
210 		close(fd);
211 	}
212 
213 	if (key_setsecret(secret) < 0)
214 		errx(1, "unable to login with new secret key");
215 	(void)printf("Done.\n");
216 	exit(0);
217 	/* NOTREACHED */
218 }
219 
220 static void
221 usage(void)
222 {
223 	(void)fprintf(stderr, "usage: chkey [-f]\n");
224 	exit(1);
225 	/* NOTREACHED */
226 }
227 
228 
229 /*
230  * Set the entry in the public key file
231  */
232 int
233 setpublicmap(char *name, char *public, char *secret)
234 {
235 	char pkent[1024];
236 
237 	(void)sprintf(pkent,"%s:%s", public, secret);
238 #ifdef YP
239 	return (yp_update(domain, PKMAP, YPOP_STORE,
240 		name, strlen(name), pkent, strlen(pkent)));
241 #else
242 	return (localupdate(name, PKFILE, YPOP_STORE,
243 		strlen(name), name, strlen(pkent), pkent));
244 #endif
245 }
246 
247 #ifdef YPPASSWD
248 struct passwd *
249 ypgetpwuid(uid_t uid)
250 {
251 	char uidstr[10];
252 	char *val;
253 	int vallen;
254 	static struct passwd pw;
255 	char *p;
256 
257 	(void)sprintf(uidstr, "%d", uid);
258 	if (yp_match(domain, "passwd.byuid", uidstr, strlen(uidstr),
259 			&val, &vallen) != 0) {
260 		return (NULL);
261 	}
262 	p = strchr(val, ':');
263 	if (p == NULL) {
264 		return (NULL);
265 	}
266 	pw.pw_passwd = p + 1;
267 	p = strchr(pw.pw_passwd, ':');
268 	if (p == NULL) {
269 		return (NULL);
270 	}
271 	*p = 0;
272 	return (&pw);
273 }
274 #endif	/* YPPASSWD */
275