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