Initial import from FreeBSD RELENG_4:
[games.git] / usr.bin / chkey / chkey.c
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, MERCHANTIBILITY 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 #ifndef lint
31 static char sccsid[] = "@(#)chkey.c 1.7 91/03/11 Copyr 1986 Sun Micro";
32 #endif
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 <stdio.h>
41 #include <rpc/rpc.h>
42 #include <rpc/key_prot.h>
43 #ifdef YP
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
46 #else
47 #define YPOP_STORE      4
48 #endif
49 #include <pwd.h>
50 #include <string.h>
51 #include <sys/fcntl.h>
52
53 extern char *getpass();
54 #define index strchr
55 extern char *crypt();
56 #ifdef YPPASSWD
57 struct passwd *ypgetpwuid();
58 #endif
59
60 #ifdef YP
61 static char *domain;
62 static char PKMAP[] = "publickey.byname";
63 #else
64 static char PKFILE[] = "/etc/publickey";
65 #endif  /* YP */
66 static char ROOTKEY[] = "/etc/.rootkey";
67
68 main(argc, argv)
69         int argc;
70         char **argv;
71 {
72         char name[MAXNETNAMELEN+1];
73         char public[HEXKEYBYTES + 1];
74         char secret[HEXKEYBYTES + 1];
75         char crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
76         char crypt2[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
77         int status;     
78         char *pass;
79         struct passwd *pw;
80         uid_t uid;
81         int force = 0;
82         char *self;
83 #ifdef YP
84         char *master;
85 #endif
86
87         self = argv[0];
88         for (argc--, argv++; argc > 0 && **argv == '-'; argc--, argv++) {
89                 if (argv[0][2] != 0) {
90                         usage(self);
91                 }
92                 switch (argv[0][1]) {
93                 case 'f':
94                         force = 1;
95                         break;
96                 default:
97                         usage(self);
98                 }
99         }
100         if (argc != 0) {
101                 usage(self);
102         }
103
104 #ifdef YP
105         (void)yp_get_default_domain(&domain);
106         if (yp_master(domain, PKMAP, &master) != 0) {
107                 (void)fprintf(stderr, 
108                         "can't find master of publickey database\n");
109                 exit(1);
110         }
111 #endif
112         uid = getuid() /*geteuid()*/;
113         if (uid == 0) {
114                 if (host2netname(name, NULL, NULL) == 0) {
115                         (void)fprintf(stderr,
116                         "chkey: cannot convert hostname to netname\n");
117                         exit(1);
118                 }
119         } else {
120                 if (user2netname(name, uid, NULL) == 0) {
121                         (void)fprintf(stderr,
122                         "chkey: cannot convert username to netname\n");
123                         exit(1);
124                 }
125         }
126         (void)printf("Generating new key for %s.\n", name);
127
128         if (!force) {
129                 if (uid != 0) {
130 #ifdef YPPASSWD
131                         pw = ypgetpwuid(uid);
132 #else
133                         pw = getpwuid(uid);
134 #endif
135                         if (pw == NULL) {
136 #ifdef YPPASSWD
137                                 (void)fprintf(stderr, 
138                 "No NIS password entry found: can't change key.\n");
139 #else
140                                 (void)fprintf(stderr,
141                 "No password entry found: can't change key.\n");
142 #endif
143                                 exit(1);
144                         }
145                 } else {
146                         pw = getpwuid(0);
147                         if (pw == NULL) {
148                                 (void)fprintf(stderr, 
149                                 "No password entry found: can't change key.\n");
150                                 exit(1);
151                         }
152                 }
153         }
154         pass = getpass("Password:");
155 #ifdef YPPASSWD
156         if (!force) {
157                 if (strcmp(crypt(pass, pw->pw_passwd), pw->pw_passwd) != 0) {
158                         (void)fprintf(stderr, "Invalid password.\n");
159                         exit(1);
160                 }
161         }
162 #else
163         force = 1;      /* Make this mandatory */
164 #endif
165         genkeys(public, secret, pass);  
166
167         memcpy(crypt1, secret, HEXKEYBYTES);
168         memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE);
169         crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
170         xencrypt(crypt1, pass);
171
172         if (force) {
173                 memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1);      
174                 xdecrypt(crypt2, getpass("Retype password:"));
175                 if (memcmp(crypt2, crypt2 + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0 ||
176                     memcmp(crypt2, secret, HEXKEYBYTES) != 0) { 
177                         (void)fprintf(stderr, "Password incorrect.\n");
178                         exit(1);
179                 }
180         }
181
182 #ifdef YP
183         (void)printf("Sending key change request to %s...\n", master);
184 #endif
185         status = setpublicmap(name, public, crypt1);
186         if (status != 0) {
187 #ifdef YP
188                 (void)fprintf(stderr,
189                 "%s: unable to update NIS database (%u): %s\n",
190                                 self, status, yperr_string(status));
191 #else
192                 (void)fprintf(stderr,
193                 "%s: unable to update publickey database\n", self);
194 #endif
195                 exit(1);
196         }
197
198         if (uid == 0) {
199                 /*
200                  * Root users store their key in /etc/$ROOTKEY so
201                  * that they can auto reboot without having to be
202                  * around to type a password. Storing this in a file
203                  * is rather dubious: it should really be in the EEPROM
204                  * so it does not go over the net.
205                  */
206                 int fd;
207
208                 fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
209                 if (fd < 0) {
210                         perror(ROOTKEY);
211                 } else {
212                         char newline = '\n';
213
214                         if (write(fd, secret, strlen(secret)) < 0 ||
215                             write(fd, &newline, sizeof(newline)) < 0) {
216                                 (void)fprintf(stderr, "%s: ", ROOTKEY);
217                                 perror("write");
218                         }
219                 }
220         }
221
222         if (key_setsecret(secret) < 0) {
223                 (void)printf("Unable to login with new secret key.\n");
224                 exit(1);
225         }
226         (void)printf("Done.\n");
227         exit(0);
228         /* NOTREACHED */
229 }
230
231 usage(name)
232         char *name;
233 {
234         (void)fprintf(stderr, "usage: %s [-f]\n", name);
235         exit(1);
236         /* NOTREACHED */
237 }
238
239
240 /*
241  * Set the entry in the public key file
242  */
243 setpublicmap(name, public, secret)
244         char *name;
245         char *public;
246         char *secret;
247 {
248         char pkent[1024];
249         
250         (void)sprintf(pkent,"%s:%s", public, secret);
251 #ifdef YP
252         return (yp_update(domain, PKMAP, YPOP_STORE,
253                 name, strlen(name), pkent, strlen(pkent)));
254 #else
255         return (localupdate(name, PKFILE, YPOP_STORE,
256                 strlen(name), name, strlen(pkent), pkent));
257 #endif
258 }
259
260 #ifdef YPPASSWD
261 struct passwd *
262 ypgetpwuid(uid)
263         uid_t uid;
264 {
265         char uidstr[10];
266         char *val;
267         int vallen;
268         static struct passwd pw;
269         char *p;
270
271         (void)sprintf(uidstr, "%d", uid);
272         if (yp_match(domain, "passwd.byuid", uidstr, strlen(uidstr), 
273                         &val, &vallen) != 0) {
274                 return (NULL);
275         }
276         p = index(val, ':');
277         if (p == NULL) {        
278                 return (NULL);
279         }
280         pw.pw_passwd = p + 1;
281         p = index(pw.pw_passwd, ':');
282         if (p == NULL) {
283                 return (NULL);
284         }
285         *p = 0;
286         return (&pw);
287 }
288 #endif  /* YPPASSWD */