According to RFC2711 routers shouldn't treat all packets with a Router
[dragonfly/netmp.git] / usr.bin / chkey / chkey.c
blob5137ad9360b85c739be91d040706f74cc2af4002
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, 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.
27 * 2550 Garcia Avenue
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>
41 #include <pwd.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
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 #include <rpcsvc/yppasswd.h>
53 #else
54 #define YPOP_STORE 4
55 #endif
57 #include "externs.h"
59 #ifdef YPPASSWD
60 static struct passwd *ypgetpwuid(uid_t);
61 #endif
63 #ifdef YP
64 static char *domain;
65 static char PKMAP[] = "publickey.byname";
66 #else
67 static char PKFILE[] = "/etc/publickey";
68 #endif /* YP */
69 static char ROOTKEY[] = "/etc/.rootkey";
71 static void usage(char *);
72 static int setpublicmap(char *, char *, char *);
74 int
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];
82 int status;
83 char *pass;
84 struct passwd *pw;
85 uid_t uid;
86 int force = 0;
87 char *self;
88 #ifdef YP
89 char *master;
90 #endif
92 self = argv[0];
93 for (argc--, argv++; argc > 0 && **argv == '-'; argc--, argv++) {
94 if (argv[0][2] != 0) {
95 usage(self);
97 switch (argv[0][1]) {
98 case 'f':
99 force = 1;
100 break;
101 default:
102 usage(self);
105 if (argc != 0) {
106 usage(self);
109 #ifdef YP
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");
114 exit(1);
116 #endif
117 uid = getuid() /*geteuid()*/;
118 if (uid == 0) {
119 if (host2netname(name, NULL, NULL) == 0) {
120 (void)fprintf(stderr,
121 "chkey: cannot convert hostname to netname\n");
122 exit(1);
124 } else {
125 if (user2netname(name, uid, NULL) == 0) {
126 (void)fprintf(stderr,
127 "chkey: cannot convert username to netname\n");
128 exit(1);
131 (void)printf("Generating new key for %s.\n", name);
133 if (!force) {
134 if (uid != 0) {
135 #ifdef YPPASSWD
136 pw = ypgetpwuid(uid);
137 #else
138 pw = getpwuid(uid);
139 #endif
140 if (pw == NULL) {
141 #ifdef YPPASSWD
142 (void)fprintf(stderr,
143 "No NIS password entry found: can't change key.\n");
144 #else
145 (void)fprintf(stderr,
146 "No password entry found: can't change key.\n");
147 #endif
148 exit(1);
150 } else {
151 pw = getpwuid(0);
152 if (pw == NULL) {
153 (void)fprintf(stderr,
154 "No password entry found: can't change key.\n");
155 exit(1);
159 pass = getpass("Password:");
160 #ifdef YPPASSWD
161 if (!force) {
162 if (strcmp(crypt(pass, pw->pw_passwd), pw->pw_passwd) != 0) {
163 (void)fprintf(stderr, "Invalid password.\n");
164 exit(1);
167 #else
168 force = 1; /* Make this mandatory */
169 #endif
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);
177 if (force) {
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");
183 exit(1);
187 #ifdef YP
188 (void)printf("Sending key change request to %s...\n", master);
189 #endif
190 status = setpublicmap(name, public, crypt1);
191 if (status != 0) {
192 #ifdef YP
193 (void)fprintf(stderr,
194 "%s: unable to update NIS database (%u): %s\n",
195 self, status, yperr_string(status));
196 #else
197 (void)fprintf(stderr,
198 "%s: unable to update publickey database\n", self);
199 #endif
200 exit(1);
203 if (uid == 0) {
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.
211 int fd;
213 fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
214 if (fd < 0) {
215 perror(ROOTKEY);
216 } else {
217 char newline = '\n';
219 if (write(fd, secret, strlen(secret)) < 0 ||
220 write(fd, &newline, sizeof(newline)) < 0) {
221 (void)fprintf(stderr, "%s: ", ROOTKEY);
222 perror("write");
227 if (key_setsecret(secret) < 0) {
228 (void)printf("Unable to login with new secret key.\n");
229 exit(1);
231 (void)printf("Done.\n");
232 exit(0);
233 /* NOTREACHED */
236 static void
237 usage(char *name)
239 (void)fprintf(stderr, "usage: %s [-f]\n", name);
240 exit(1);
241 /* NOTREACHED */
246 * Set the entry in the public key file
248 static int
249 setpublicmap(char *name, char *public, char *secret)
251 char pkent[1024];
253 (void)sprintf(pkent,"%s:%s", public, secret);
254 #ifdef YP
255 return (yp_update(domain, PKMAP, YPOP_STORE,
256 name, strlen(name), pkent, strlen(pkent)));
257 #else
258 return (localupdate(name, PKFILE, YPOP_STORE,
259 strlen(name), name, strlen(pkent), pkent));
260 #endif
263 #ifdef YPPASSWD
264 static struct passwd *
265 ypgetpwuid(uid_t uid)
267 char uidstr[10];
268 char *val;
269 int vallen;
270 static struct passwd pw;
271 char *p;
273 (void)sprintf(uidstr, "%d", uid);
274 if (yp_match(domain, "passwd.byuid", uidstr, strlen(uidstr),
275 &val, &vallen) != 0) {
276 return (NULL);
278 p = strchr(val, ':');
279 if (p == NULL) {
280 return (NULL);
282 pw.pw_passwd = p + 1;
283 p = strchr(pw.pw_passwd, ':');
284 if (p == NULL) {
285 return (NULL);
287 *p = 0;
288 return (&pw);
290 #endif /* YPPASSWD */