xref: /freebsd/usr.bin/chkey/chkey.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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 #include <sys/cdefs.h>
32 /*
33  * Copyright (C) 1986, Sun Microsystems, Inc.
34  */
35 
36 /*
37  * Command to change one's public key in the public key database
38  */
39 #include <rpc/rpc.h>
40 #include <rpc/key_prot.h>
41 #ifdef YP
42 #include <rpcsvc/yp_prot.h>
43 #include <rpcsvc/ypclnt.h>
44 #else
45 #define	YPOP_STORE	4
46 #endif
47 #include <sys/fcntl.h>
48 #include <err.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #include "extern.h"
56 
57 #ifdef YPPASSWD
58 struct passwd *ypgetpwuid(uid_t);
59 #endif
60 
61 #ifdef YP
62 static char *domain;
63 static char PKMAP[] = "publickey.byname";
64 #else
65 static char PKFILE[] = "/etc/publickey";
66 #endif	/* YP */
67 static char ROOTKEY[] = "/etc/.rootkey";
68 
69 static void usage(void) __dead2;
70 extern int yp_update(char *, char *, int, char *, size_t, char *, size_t);
71 
72 int
73 main(int argc, char **argv)
74 {
75 	char name[MAXNETNAMELEN+1];
76 	char public[HEXKEYBYTES + 1];
77 	char secret[HEXKEYBYTES + 1];
78 	char crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
79 	char crypt2[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
80 	int status;
81 	char *pass;
82 	struct passwd *pw;
83 	uid_t uid;
84 	int force = 0;
85 	int ch;
86 #ifdef YP
87 	char *master;
88 #endif
89 #ifdef YPPASSWD
90 	char *cryptpw;
91 #endif
92 
93 	while ((ch = getopt(argc, argv, "f")) != -1)
94 		switch(ch) {
95 		case 'f':
96 			force = 1;
97 			break;
98 		default:
99 			usage();
100 		}
101 	argc -= optind;
102 	argv += optind;
103 
104 	if (argc != 0)
105 		usage();
106 
107 #ifdef YP
108 	(void)yp_get_default_domain(&domain);
109 	if (yp_master(domain, PKMAP, &master) != 0)
110 		errx(1, "can't find master of publickey database");
111 #endif
112 	uid = getuid() /*geteuid()*/;
113 	if (uid == 0) {
114 		if (host2netname(name, NULL, NULL) == 0)
115 			errx(1, "cannot convert hostname to netname");
116 	} else {
117 		if (user2netname(name, uid, NULL) == 0)
118 			errx(1, "cannot convert username to netname");
119 	}
120 	(void)printf("Generating new key for %s.\n", name);
121 
122 	if (!force) {
123 		if (uid != 0) {
124 #ifdef YPPASSWD
125 			pw = ypgetpwuid(uid);
126 #else
127 			pw = getpwuid(uid);
128 #endif
129 			if (pw == NULL) {
130 #ifdef YPPASSWD
131 				errx(1,
132 			"no NIS password entry found: can't change key");
133 #else
134 				errx(1,
135 			"no password entry found: can't change key");
136 #endif
137 			}
138 		} else {
139 			pw = getpwuid(0);
140 			if (pw == NULL)
141 			  errx(1, "no password entry found: can't change key");
142 		}
143 	}
144 	pass = getpass("Password:");
145 #ifdef YPPASSWD
146 	if (!force) {
147 		cryptpw = crypt(pass, pw->pw_passwd);
148 		if (cryptpw == NULL || strcmp(cryptpw, pw->pw_passwd) != 0)
149 			errx(1, "invalid password");
150 	}
151 #else
152 	force = 1;	/* Make this mandatory */
153 #endif
154 	genkeys(public, secret, pass);
155 
156 	memcpy(crypt1, secret, HEXKEYBYTES);
157 	memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE);
158 	crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
159 	xencrypt(crypt1, pass);
160 
161 	if (force) {
162 		memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1);
163 		xdecrypt(crypt2, getpass("Retype password:"));
164 		if (memcmp(crypt2, crypt2 + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0
165 			|| memcmp(crypt2, secret, HEXKEYBYTES) != 0)
166 			errx(1, "password incorrect");
167 	}
168 
169 #ifdef YP
170 	(void)printf("Sending key change request to %s...\n", master);
171 #endif
172 	status = setpublicmap(name, public, crypt1);
173 	if (status != 0) {
174 #ifdef YP
175 		errx(1, "unable to update NIS database (%u): %s",
176 				status, yperr_string(status));
177 #else
178 		errx(1, "unable to update publickey database");
179 #endif
180 	}
181 
182 	if (uid == 0) {
183 		/*
184 		 * Root users store their key in /etc/$ROOTKEY so
185 		 * that they can auto reboot without having to be
186 		 * around to type a password. Storing this in a file
187 		 * is rather dubious: it should really be in the EEPROM
188 		 * so it does not go over the net.
189 		 */
190 		int fd;
191 
192 		fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
193 		if (fd < 0) {
194 			warn("%s", ROOTKEY);
195 		} else {
196 			char newline = '\n';
197 
198 			if (write(fd, secret, strlen(secret)) < 0 ||
199 			    write(fd, &newline, sizeof(newline)) < 0)
200 				warn("%s: write", ROOTKEY);
201 		}
202 		close(fd);
203 	}
204 
205 	if (key_setsecret(secret) < 0)
206 		errx(1, "unable to login with new secret key");
207 	(void)printf("Done.\n");
208 	exit(0);
209 	/* NOTREACHED */
210 }
211 
212 static void
213 usage(void)
214 {
215 	(void)fprintf(stderr, "usage: chkey [-f]\n");
216 	exit(1);
217 	/* NOTREACHED */
218 }
219 
220 
221 /*
222  * Set the entry in the public key file
223  */
224 int
225 setpublicmap(char *name, char *public, char *secret)
226 {
227 	char pkent[1024];
228 
229 	(void)sprintf(pkent,"%s:%s", public, secret);
230 #ifdef YP
231 	return (yp_update(domain, PKMAP, YPOP_STORE,
232 		name, strlen(name), pkent, strlen(pkent)));
233 #else
234 	return (localupdate(name, PKFILE, YPOP_STORE,
235 		strlen(name), name, strlen(pkent), pkent));
236 #endif
237 }
238 
239 #ifdef YPPASSWD
240 struct passwd *
241 ypgetpwuid(uid_t uid)
242 {
243 	char uidstr[10];
244 	char *val;
245 	int vallen;
246 	static struct passwd pw;
247 	char *p;
248 
249 	(void)sprintf(uidstr, "%d", uid);
250 	if (yp_match(domain, "passwd.byuid", uidstr, strlen(uidstr),
251 			&val, &vallen) != 0) {
252 		return (NULL);
253 	}
254 	p = strchr(val, ':');
255 	if (p == NULL) {
256 		return (NULL);
257 	}
258 	pw.pw_passwd = p + 1;
259 	p = strchr(pw.pw_passwd, ':');
260 	if (p == NULL) {
261 		return (NULL);
262 	}
263 	*p = 0;
264 	return (&pw);
265 }
266 #endif	/* YPPASSWD */
267