xref: /dragonfly/usr.bin/chkey/chkey.c (revision 9f3fc534)
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  * $DragonFly: src/usr.bin/chkey/chkey.c,v 1.6 2008/11/11 18:06:12 pavalos Exp $
31  * @(#)chkey.c 1.7 91/03/11 Copyr 1986 Sun Micro
32  */
33 /*
34  * Copyright (C) 1986, Sun Microsystems, Inc.
35  */
36 
37 /*
38  * Command to change one's public key in the public key database
39  */
40 #include <sys/fcntl.h>
41 #include <err.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include <rpc/rpc.h>
49 #include <rpc/key_prot.h>
50 #ifdef YP
51 #include <rpcsvc/yp_prot.h>
52 #include <rpcsvc/ypclnt.h>
53 #include <rpcsvc/yppasswd.h>
54 #else
55 #define	YPOP_STORE	4
56 #endif
57 
58 #include "externs.h"
59 
60 #ifdef YPPASSWD
61 static struct passwd	*ypgetpwuid(uid_t);
62 #endif
63 
64 #ifdef YP
65 static char *domain;
66 static char PKMAP[] = "publickey.byname";
67 #else
68 static char PKFILE[] = "/etc/publickey";
69 #endif	/* YP */
70 static char ROOTKEY[] = "/etc/.rootkey";
71 
72 static void	usage(void);
73 static int	setpublicmap(char *, char *, char *);
74 
75 int
76 main(int argc, char **argv)
77 {
78 	char name[MAXNETNAMELEN+1];
79 	char public[HEXKEYBYTES + 1];
80 	char secret[HEXKEYBYTES + 1];
81 	char crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
82 	char crypt2[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
83 	int status;
84 	char *pass;
85 	struct passwd *pw;
86 	uid_t uid;
87 	int force = 0;
88 	int ch;
89 #ifdef YP
90 	char *master;
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 	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 	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 		if (strcmp(crypt(pass, pw->pw_passwd), pw->pw_passwd) != 0)
148 			errx(1, "invalid password");
149 	}
150 #else
151 	force = 1;	/* Make this mandatory */
152 #endif
153 	genkeys(public, secret, pass);
154 
155 	memcpy(crypt1, secret, HEXKEYBYTES);
156 	memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE);
157 	crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
158 	xencrypt(crypt1, pass);
159 
160 	if (force) {
161 		memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1);
162 		xdecrypt(crypt2, getpass("Retype password:"));
163 		if (memcmp(crypt2, crypt2 + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0 ||
164 		    memcmp(crypt2, secret, HEXKEYBYTES) != 0)
165 			errx(1, "password incorrect");
166 	}
167 
168 #ifdef YP
169 	printf("Sending key change request to %s...\n", master);
170 #endif
171 	status = setpublicmap(name, public, crypt1);
172 	if (status != 0) {
173 #ifdef YP
174 		errx(1, "unable to update NIS database (%u): %s",
175 				status, yperr_string(status));
176 #else
177 		errx(1, "unable to update publickey database");
178 #endif
179 	}
180 
181 	if (uid == 0) {
182 		/*
183 		 * Root users store their key in /etc/$ROOTKEY so
184 		 * that they can auto reboot without having to be
185 		 * around to type a password. Storing this in a file
186 		 * is rather dubious: it should really be in the EEPROM
187 		 * so it does not go over the net.
188 		 */
189 		int fd;
190 
191 		fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
192 		if (fd < 0) {
193 			warn("%s", ROOTKEY);
194 		} else {
195 			char newline = '\n';
196 
197 			if (write(fd, secret, strlen(secret)) < 0 ||
198 			    write(fd, &newline, sizeof(newline)) < 0)
199 				warn("%s: write", ROOTKEY);
200 		}
201 	}
202 
203 	if (key_setsecret(secret) < 0)
204 		errx(1, "unable to login with new secret key");
205 	printf("Done.\n");
206 	exit(0);
207 	/* NOTREACHED */
208 }
209 
210 static void
211 usage(void)
212 {
213 	fprintf(stderr, "usage: chkey [-f]\n");
214 	exit(1);
215 	/* NOTREACHED */
216 }
217 
218 
219 /*
220  * Set the entry in the public key file
221  */
222 static int
223 setpublicmap(char *name, char *public, char *secret)
224 {
225 	char pkent[1024];
226 
227 	sprintf(pkent,"%s:%s", public, secret);
228 #ifdef YP
229 	return (yp_update(domain, PKMAP, YPOP_STORE,
230 		name, strlen(name), pkent, strlen(pkent)));
231 #else
232 	return (localupdate(name, PKFILE, YPOP_STORE,
233 		strlen(name), name, strlen(pkent), pkent));
234 #endif
235 }
236 
237 #ifdef YPPASSWD
238 static struct passwd *
239 ypgetpwuid(uid_t uid)
240 {
241 	char uidstr[10];
242 	char *val;
243 	int vallen;
244 	static struct passwd pw;
245 	char *p;
246 
247 	sprintf(uidstr, "%d", uid);
248 	if (yp_match(domain, "passwd.byuid", uidstr, strlen(uidstr),
249 			&val, &vallen) != 0) {
250 		return (NULL);
251 	}
252 	p = strchr(val, ':');
253 	if (p == NULL) {
254 		return (NULL);
255 	}
256 	pw.pw_passwd = p + 1;
257 	p = strchr(pw.pw_passwd, ':');
258 	if (p == NULL) {
259 		return (NULL);
260 	}
261 	*p = 0;
262 	return (&pw);
263 }
264 #endif	/* YPPASSWD */
265