Correct math and limerick.
[dragonfly.git] / usr.bin / chkey / chkey.c
blobf31c719973f0050de11c8f79e0a6acbba4edf189
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.
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.
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.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
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
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>
41 #include <err.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
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
58 #include "externs.h"
60 #ifdef YPPASSWD
61 static struct passwd *ypgetpwuid(uid_t);
62 #endif
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";
72 static void usage(void);
73 static int setpublicmap(char *, char *, char *);
75 int
76 main(int argc, char **argv)
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 #ifdef YPPASSWD
93 char *cryptpw;
94 #endif
96 while ((ch = getopt(argc, argv, "f")) != -1)
97 switch(ch) {
98 case 'f':
99 force = 1;
100 break;
101 default:
102 usage();
104 argc -= optind;
105 argv += optind;
107 if (argc != 0)
108 usage();
110 #ifdef YP
111 yp_get_default_domain(&domain);
112 if (yp_master(domain, PKMAP, &master) != 0)
113 errx(1, "can't find master of publickey database");
114 #endif
115 uid = getuid() /*geteuid()*/;
116 if (uid == 0) {
117 if (host2netname(name, NULL, NULL) == 0)
118 errx(1, "cannot convert hostname to netname");
119 } else {
120 if (user2netname(name, uid, NULL) == 0)
121 errx(1, "cannot convert username to netname");
123 printf("Generating new key for %s.\n", name);
125 if (!force) {
126 if (uid != 0) {
127 #ifdef YPPASSWD
128 pw = ypgetpwuid(uid);
129 #else
130 pw = getpwuid(uid);
131 #endif
132 if (pw == NULL) {
133 #ifdef YPPASSWD
134 errx(1,
135 "no NIS password entry found: can't change key");
136 #else
137 errx(1,
138 "no password entry found: can't change key");
139 #endif
141 } else {
142 pw = getpwuid(0);
143 if (pw == NULL)
144 errx(1, "no password entry found: can't change key");
147 pass = getpass("Password:");
148 #ifdef YPPASSWD
149 if (!force) {
150 cryptpw = crypt(pass, pw->pw_passwd);
151 if (cryptpw == NULL || strcmp(cryptpw, pw->pw_passwd) != 0)
152 errx(1, "invalid password");
154 #else
155 force = 1; /* Make this mandatory */
156 #endif
157 genkeys(public, secret, pass);
159 memcpy(crypt1, secret, HEXKEYBYTES);
160 memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE);
161 crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
162 xencrypt(crypt1, pass);
164 if (force) {
165 memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1);
166 xdecrypt(crypt2, getpass("Retype password:"));
167 if (memcmp(crypt2, crypt2 + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0 ||
168 memcmp(crypt2, secret, HEXKEYBYTES) != 0)
169 errx(1, "password incorrect");
172 #ifdef YP
173 printf("Sending key change request to %s...\n", master);
174 #endif
175 status = setpublicmap(name, public, crypt1);
176 if (status != 0) {
177 #ifdef YP
178 errx(1, "unable to update NIS database (%u): %s",
179 status, yperr_string(status));
180 #else
181 errx(1, "unable to update publickey database");
182 #endif
185 if (uid == 0) {
187 * Root users store their key in /etc/$ROOTKEY so
188 * that they can auto reboot without having to be
189 * around to type a password. Storing this in a file
190 * is rather dubious: it should really be in the EEPROM
191 * so it does not go over the net.
193 int fd;
195 fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
196 if (fd < 0) {
197 warn("%s", ROOTKEY);
198 } else {
199 char newline = '\n';
201 if (write(fd, secret, strlen(secret)) < 0 ||
202 write(fd, &newline, sizeof(newline)) < 0)
203 warn("%s: write", ROOTKEY);
207 if (key_setsecret(secret) < 0)
208 errx(1, "unable to login with new secret key");
209 printf("Done.\n");
210 exit(0);
211 /* NOTREACHED */
214 static void
215 usage(void)
217 fprintf(stderr, "usage: chkey [-f]\n");
218 exit(1);
219 /* NOTREACHED */
224 * Set the entry in the public key file
226 static int
227 setpublicmap(char *name, char *public, char *secret)
229 char pkent[1024];
231 sprintf(pkent,"%s:%s", public, secret);
232 #ifdef YP
233 return (yp_update(domain, PKMAP, YPOP_STORE,
234 name, strlen(name), pkent, strlen(pkent)));
235 #else
236 return (localupdate(name, PKFILE, YPOP_STORE,
237 strlen(name), name, strlen(pkent), pkent));
238 #endif
241 #ifdef YPPASSWD
242 static struct passwd *
243 ypgetpwuid(uid_t uid)
245 char uidstr[10];
246 char *val;
247 int vallen;
248 static struct passwd pw;
249 char *p;
251 sprintf(uidstr, "%d", uid);
252 if (yp_match(domain, "passwd.byuid", uidstr, strlen(uidstr),
253 &val, &vallen) != 0) {
254 return (NULL);
256 p = strchr(val, ':');
257 if (p == NULL) {
258 return (NULL);
260 pw.pw_passwd = p + 1;
261 p = strchr(pw.pw_passwd, ':');
262 if (p == NULL) {
263 return (NULL);
265 *p = 0;
266 return (&pw);
268 #endif /* YPPASSWD */