xref: /freebsd/usr.bin/chkey/chkey.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
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 
98 	while ((ch = getopt(argc, argv, "f")) != -1)
99 		switch(ch) {
100 		case 'f':
101 			force = 1;
102 			break;
103 		default:
104 			usage();
105 		}
106 	argc -= optind;
107 	argv += optind;
108 
109 	if (argc != 0)
110 		usage();
111 
112 #ifdef YP
113 	(void)yp_get_default_domain(&domain);
114 	if (yp_master(domain, PKMAP, &master) != 0)
115 		errx(1, "can't find master of publickey database");
116 #endif
117 	uid = getuid() /*geteuid()*/;
118 	if (uid == 0) {
119 		if (host2netname(name, NULL, NULL) == 0)
120 			errx(1, "cannot convert hostname to netname");
121 	} else {
122 		if (user2netname(name, uid, NULL) == 0)
123 			errx(1, "cannot convert username to netname");
124 	}
125 	(void)printf("Generating new key for %s.\n", name);
126 
127 	if (!force) {
128 		if (uid != 0) {
129 #ifdef YPPASSWD
130 			pw = ypgetpwuid(uid);
131 #else
132 			pw = getpwuid(uid);
133 #endif
134 			if (pw == NULL) {
135 #ifdef YPPASSWD
136 				errx(1,
137 			"no NIS password entry found: can't change key");
138 #else
139 				errx(1,
140 			"no password entry found: can't change key");
141 #endif
142 			}
143 		} else {
144 			pw = getpwuid(0);
145 			if (pw == NULL)
146 			  errx(1, "no password entry found: can't change key");
147 		}
148 	}
149 	pass = getpass("Password:");
150 #ifdef YPPASSWD
151 	if (!force) {
152 		if (strcmp(crypt(pass, pw->pw_passwd), pw->pw_passwd) != 0)
153 			errx(1, "invalid password");
154 	}
155 #else
156 	force = 1;	/* Make this mandatory */
157 #endif
158 	genkeys(public, secret, pass);
159 
160 	memcpy(crypt1, secret, HEXKEYBYTES);
161 	memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE);
162 	crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
163 	xencrypt(crypt1, pass);
164 
165 	if (force) {
166 		memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1);
167 		xdecrypt(crypt2, getpass("Retype password:"));
168 		if (memcmp(crypt2, crypt2 + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0
169 			|| memcmp(crypt2, secret, HEXKEYBYTES) != 0)
170 			errx(1, "password incorrect");
171 	}
172 
173 #ifdef YP
174 	(void)printf("Sending key change request to %s...\n", master);
175 #endif
176 	status = setpublicmap(name, public, crypt1);
177 	if (status != 0) {
178 #ifdef YP
179 		errx(1, "unable to update NIS database (%u): %s",
180 				status, yperr_string(status));
181 #else
182 		errx(1, "unable to update publickey database");
183 #endif
184 	}
185 
186 	if (uid == 0) {
187 		/*
188 		 * Root users store their key in /etc/$ROOTKEY so
189 		 * that they can auto reboot without having to be
190 		 * around to type a password. Storing this in a file
191 		 * is rather dubious: it should really be in the EEPROM
192 		 * so it does not go over the net.
193 		 */
194 		int fd;
195 
196 		fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
197 		if (fd < 0) {
198 			warn("%s", ROOTKEY);
199 		} else {
200 			char newline = '\n';
201 
202 			if (write(fd, secret, strlen(secret)) < 0 ||
203 			    write(fd, &newline, sizeof(newline)) < 0)
204 				warn("%s: write", ROOTKEY);
205 		}
206 		close(fd);
207 	}
208 
209 	if (key_setsecret(secret) < 0)
210 		errx(1, "unable to login with new secret key");
211 	(void)printf("Done.\n");
212 	exit(0);
213 	/* NOTREACHED */
214 }
215 
216 static void
217 usage(void)
218 {
219 	(void)fprintf(stderr, "usage: chkey [-f]\n");
220 	exit(1);
221 	/* NOTREACHED */
222 }
223 
224 
225 /*
226  * Set the entry in the public key file
227  */
228 int
229 setpublicmap(char *name, char *public, char *secret)
230 {
231 	char pkent[1024];
232 
233 	(void)sprintf(pkent,"%s:%s", public, secret);
234 #ifdef YP
235 	return (yp_update(domain, PKMAP, YPOP_STORE,
236 		name, strlen(name), pkent, strlen(pkent)));
237 #else
238 	return (localupdate(name, PKFILE, YPOP_STORE,
239 		strlen(name), name, strlen(pkent), pkent));
240 #endif
241 }
242 
243 #ifdef YPPASSWD
244 struct passwd *
245 ypgetpwuid(uid_t uid)
246 {
247 	char uidstr[10];
248 	char *val;
249 	int vallen;
250 	static struct passwd pw;
251 	char *p;
252 
253 	(void)sprintf(uidstr, "%d", uid);
254 	if (yp_match(domain, "passwd.byuid", uidstr, strlen(uidstr),
255 			&val, &vallen) != 0) {
256 		return (NULL);
257 	}
258 	p = strchr(val, ':');
259 	if (p == NULL) {
260 		return (NULL);
261 	}
262 	pw.pw_passwd = p + 1;
263 	p = strchr(pw.pw_passwd, ':');
264 	if (p == NULL) {
265 		return (NULL);
266 	}
267 	*p = 0;
268 	return (&pw);
269 }
270 #endif	/* YPPASSWD */
271