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.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
30 * $DragonFly: src/usr.bin/chkey/chkey.c,v 1.5 2005/01/11 00:51:11 joerg Exp $
31 * @(#)chkey.c 1.7 91/03/11 Copyr 1986 Sun Micro
34 * Copyright (C) 1986, Sun Microsystems, Inc.
38 * Command to change one's public key in the public key database
40 #include <sys/fcntl.h>
48 #include <rpc/key_prot.h>
50 #include <rpcsvc/yp_prot.h>
51 #include <rpcsvc/ypclnt.h>
52 #include <rpcsvc/yppasswd.h>
60 static struct passwd
*ypgetpwuid(uid_t
);
65 static char PKMAP
[] = "publickey.byname";
67 static char PKFILE
[] = "/etc/publickey";
69 static char ROOTKEY
[] = "/etc/.rootkey";
71 static void usage(char *);
72 static int setpublicmap(char *, char *, char *);
75 main(int argc
, char **argv
)
77 char name
[MAXNETNAMELEN
+1];
78 char public[HEXKEYBYTES
+ 1];
79 char secret
[HEXKEYBYTES
+ 1];
80 char crypt1
[HEXKEYBYTES
+ KEYCHECKSUMSIZE
+ 1];
81 char crypt2
[HEXKEYBYTES
+ KEYCHECKSUMSIZE
+ 1];
93 for (argc
--, argv
++; argc
> 0 && **argv
== '-'; argc
--, argv
++) {
94 if (argv
[0][2] != 0) {
110 (void)yp_get_default_domain(&domain
);
111 if (yp_master(domain
, PKMAP
, &master
) != 0) {
112 (void)fprintf(stderr
,
113 "can't find master of publickey database\n");
117 uid
= getuid() /*geteuid()*/;
119 if (host2netname(name
, NULL
, NULL
) == 0) {
120 (void)fprintf(stderr
,
121 "chkey: cannot convert hostname to netname\n");
125 if (user2netname(name
, uid
, NULL
) == 0) {
126 (void)fprintf(stderr
,
127 "chkey: cannot convert username to netname\n");
131 (void)printf("Generating new key for %s.\n", name
);
136 pw
= ypgetpwuid(uid
);
142 (void)fprintf(stderr
,
143 "No NIS password entry found: can't change key.\n");
145 (void)fprintf(stderr
,
146 "No password entry found: can't change key.\n");
153 (void)fprintf(stderr
,
154 "No password entry found: can't change key.\n");
159 pass
= getpass("Password:");
162 if (strcmp(crypt(pass
, pw
->pw_passwd
), pw
->pw_passwd
) != 0) {
163 (void)fprintf(stderr
, "Invalid password.\n");
168 force
= 1; /* Make this mandatory */
170 genkeys(public, secret
, pass
);
172 memcpy(crypt1
, secret
, HEXKEYBYTES
);
173 memcpy(crypt1
+ HEXKEYBYTES
, secret
, KEYCHECKSUMSIZE
);
174 crypt1
[HEXKEYBYTES
+ KEYCHECKSUMSIZE
] = 0;
175 xencrypt(crypt1
, pass
);
178 memcpy(crypt2
, crypt1
, HEXKEYBYTES
+ KEYCHECKSUMSIZE
+ 1);
179 xdecrypt(crypt2
, getpass("Retype password:"));
180 if (memcmp(crypt2
, crypt2
+ HEXKEYBYTES
, KEYCHECKSUMSIZE
) != 0 ||
181 memcmp(crypt2
, secret
, HEXKEYBYTES
) != 0) {
182 (void)fprintf(stderr
, "Password incorrect.\n");
188 (void)printf("Sending key change request to %s...\n", master
);
190 status
= setpublicmap(name
, public, crypt1
);
193 (void)fprintf(stderr
,
194 "%s: unable to update NIS database (%u): %s\n",
195 self
, status
, yperr_string(status
));
197 (void)fprintf(stderr
,
198 "%s: unable to update publickey database\n", self
);
205 * Root users store their key in /etc/$ROOTKEY so
206 * that they can auto reboot without having to be
207 * around to type a password. Storing this in a file
208 * is rather dubious: it should really be in the EEPROM
209 * so it does not go over the net.
213 fd
= open(ROOTKEY
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0);
219 if (write(fd
, secret
, strlen(secret
)) < 0 ||
220 write(fd
, &newline
, sizeof(newline
)) < 0) {
221 (void)fprintf(stderr
, "%s: ", ROOTKEY
);
227 if (key_setsecret(secret
) < 0) {
228 (void)printf("Unable to login with new secret key.\n");
231 (void)printf("Done.\n");
239 (void)fprintf(stderr
, "usage: %s [-f]\n", name
);
246 * Set the entry in the public key file
249 setpublicmap(char *name
, char *public, char *secret
)
253 (void)sprintf(pkent
,"%s:%s", public, secret
);
255 return (yp_update(domain
, PKMAP
, YPOP_STORE
,
256 name
, strlen(name
), pkent
, strlen(pkent
)));
258 return (localupdate(name
, PKFILE
, YPOP_STORE
,
259 strlen(name
), name
, strlen(pkent
), pkent
));
264 static struct passwd
*
265 ypgetpwuid(uid_t uid
)
270 static struct passwd pw
;
273 (void)sprintf(uidstr
, "%d", uid
);
274 if (yp_match(domain
, "passwd.byuid", uidstr
, strlen(uidstr
),
275 &val
, &vallen
) != 0) {
278 p
= strchr(val
, ':');
282 pw
.pw_passwd
= p
+ 1;
283 p
= strchr(pw
.pw_passwd
, ':');
290 #endif /* YPPASSWD */