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.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)keyserv.c 1.15 94/04/25 SMI
30 * $FreeBSD: src/usr.sbin/keyserv/keyserv.c,v 1.3.2.2 2001/07/19 10:58:22 roam Exp $
31 * $DragonFly: src/usr.sbin/keyserv/keyserv.c,v 1.2 2003/06/17 04:29:55 dillon Exp $
35 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
40 * Store secret keys per uid. Do public key encryption and decryption
41 * operations. Generate "random" keys.
42 * Do not talk to anything but a local root
43 * process on the local transport only
53 #include <sys/types.h>
55 #include <rpc/pmap_clnt.h>
56 #include <sys/param.h>
58 #include <rpc/des_crypt.h>
60 #include <rpc/key_prot.h>
61 #include <rpcsvc/crypt.h>
69 #define KEYSERVSOCK "/var/run/keyservsock"
72 static void randomize __P(( des_block * ));
73 static void usage __P(( void ));
74 static int getrootkey __P(( des_block *, int ));
75 static int root_auth __P(( SVCXPRT *, struct svc_req * ));
78 static int debugging = 1;
80 static int debugging = 0;
83 static void keyprogram();
84 static des_block masterkey;
86 static char ROOTKEY[] = "/etc/.rootkey";
89 * Hack to allow the keyserver to use AUTH_DES (for authenticated
90 * NIS+ calls, for example). The only functions that get called
91 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
93 * The approach is to have the keyserver fill in pointers to local
94 * implementations of these functions, and to call those in key_call().
97 extern cryptkeyres *(*__key_encryptsession_pk_LOCAL)();
98 extern cryptkeyres *(*__key_decryptsession_pk_LOCAL)();
99 extern des_block *(*__key_gendes_LOCAL)();
100 extern int (*__des_crypt_LOCAL)();
102 cryptkeyres *key_encrypt_pk_2_svc_prog __P(( uid_t, cryptkeyarg2 * ));
103 cryptkeyres *key_decrypt_pk_2_svc_prog __P(( uid_t, cryptkeyarg2 * ));
104 des_block *key_gen_1_svc_prog __P(( void *, struct svc_req * ));
113 register SVCXPRT *transp;
114 int sock = RPC_ANYSOCK;
118 __key_encryptsession_pk_LOCAL = &key_encrypt_pk_2_svc_prog;
119 __key_decryptsession_pk_LOCAL = &key_decrypt_pk_2_svc_prog;
120 __key_gendes_LOCAL = &key_gen_1_svc_prog;
122 while ((c = getopt(argc, argv, "ndDvp:")) != -1)
143 load_des(warn, path);
144 __des_crypt_LOCAL = _my_crypt;
145 if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1)
146 errx(1, "failed to register AUTH_DES authenticator");
148 if (optind != argc) {
155 (void) umask(066); /* paranoia */
157 errx(1, "keyserv must be run as root");
158 setmodulus(HEXMODULUS);
159 getrootkey(&masterkey, nflag);
162 /* Create services. */
164 (void) pmap_unset(KEY_PROG, KEY_VERS);
165 (void) pmap_unset(KEY_PROG, KEY_VERS2);
168 transp = svcudp_create(RPC_ANYSOCK);
170 errx(1, "cannot create udp service");
171 if (!svc_register(transp, KEY_PROG, KEY_VERS, keyprogram, IPPROTO_UDP))
172 errx(1, "unable to register (KEY_PROG, KEY_VERS, udp)");
173 if (!svc_register(transp, KEY_PROG, KEY_VERS2, keyprogram, IPPROTO_UDP))
174 errx(1, "unable to register (KEY_PROG, KEY_VERS2, udp)");
176 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
178 errx(1, "cannot create tcp service");
179 if (!svc_register(transp, KEY_PROG, KEY_VERS, keyprogram, IPPROTO_TCP))
180 errx(1, "unable to register (KEY_PROG, KEY_VERS, tcp)");
181 if (!svc_register(transp, KEY_PROG, KEY_VERS2, keyprogram, IPPROTO_TCP))
182 errx(1, "unable to register (KEY_PROG, KEY_VERS2, tcp)");
184 transp = svcunix_create(sock, 0, 0, KEYSERVSOCK);
185 chmod(KEYSERVSOCK, 0666);
187 errx(1, "cannot create AF_UNIX service");
188 if (!svc_register(transp, KEY_PROG, KEY_VERS, keyprogram, 0))
189 errx(1, "unable to register (KEY_PROG, KEY_VERS, unix)");
190 if (!svc_register(transp, KEY_PROG, KEY_VERS2, keyprogram, 0))
191 errx(1, "unable to register (KEY_PROG, KEY_VERS2, unix)");
192 if (!svc_register(transp, CRYPT_PROG, CRYPT_VERS, crypt_prog_1, 0))
193 errx(1, "unable to register (CRYPT_PROG, CRYPT_VERS, unix)");
199 signal(SIGPIPE, SIG_IGN);
207 * In the event that we don't get a root password, we try to
208 * randomize the master key the best we can
220 for (i = 0; i < 1024; i++) {
221 (void) gettimeofday(&tv, (struct timezone *) NULL);
222 shift = i % 8 * sizeof (int);
223 seed ^= (tv.tv_usec << shift) | (tv.tv_usec >> (32 - shift));
225 #ifdef KEYSERV_RANDOM
227 master->key.low = random();
228 master->key.high = random();
231 /* use stupid dangerous bad rand() */
233 master->key.low = rand();
234 master->key.high = rand();
240 * Try to get root's secret key, by prompting if terminal is a tty, else trying
241 * from standard input.
242 * Returns 1 on success.
245 getrootkey(master, prompt)
250 char name[MAXNETNAMELEN + 1];
251 char secret[HEXKEYBYTES];
252 key_netstarg netstore;
257 * Read secret key out of ROOTKEY
259 fd = open(ROOTKEY, O_RDONLY, 0);
264 if (read(fd, secret, HEXKEYBYTES) < HEXKEYBYTES) {
265 warnx("the key read from %s was too short", ROOTKEY);
270 if (!getnetname(name)) {
272 "failed to generate host's netname when establishing root's key");
275 memcpy(netstore.st_priv_key, secret, HEXKEYBYTES);
276 memset(netstore.st_pub_key, 0, HEXKEYBYTES);
277 netstore.st_netname = name;
278 if (pk_netput(0, &netstore) != KEY_SUCCESS) {
279 warnx("could not set root's key and netname");
285 * Decrypt yellow pages publickey entry to get secret key
287 passwd = getpass("root password:");
288 passwd2des(passwd, (char *)master);
290 if (!getsecretkey(name, secret, passwd)) {
291 warnx("can't find %s's secret key", name);
294 if (secret[0] == 0) {
295 warnx("password does not decrypt secret key for %s", name);
298 (void) pk_setkey(0, secret);
300 * Store it for future use in $ROOTKEY, if possible
302 fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
306 write(fd, secret, strlen(secret));
307 write(fd, &newline, sizeof (newline));
314 * Procedures to implement RPC service
322 return ("KEY_SUCCESS");
324 return ("KEY_NOSECRET");
326 return ("KEY_UNKNOWN");
328 return ("KEY_SYSTEMERR");
330 return ("(bad result code)");
335 key_set_1_svc_prog(uid, key)
339 static keystatus status;
342 (void) fprintf(stderr, "set(%ld, %.*s) = ", uid,
343 (int) sizeof (keybuf), key);
345 status = pk_setkey(uid, key);
347 (void) fprintf(stderr, "%s\n", strstatus(status));
348 (void) fflush(stderr);
354 key_encrypt_pk_2_svc_prog(uid, arg)
358 static cryptkeyres res;
361 (void) fprintf(stderr, "encrypt(%ld, %s, %08x%08x) = ", uid,
362 arg->remotename, arg->deskey.key.high,
363 arg->deskey.key.low);
365 res.cryptkeyres_u.deskey = arg->deskey;
366 res.status = pk_encrypt(uid, arg->remotename, &(arg->remotekey),
367 &res.cryptkeyres_u.deskey);
369 if (res.status == KEY_SUCCESS) {
370 (void) fprintf(stderr, "%08x%08x\n",
371 res.cryptkeyres_u.deskey.key.high,
372 res.cryptkeyres_u.deskey.key.low);
374 (void) fprintf(stderr, "%s\n", strstatus(res.status));
376 (void) fflush(stderr);
382 key_decrypt_pk_2_svc_prog(uid, arg)
386 static cryptkeyres res;
389 (void) fprintf(stderr, "decrypt(%ld, %s, %08x%08x) = ", uid,
390 arg->remotename, arg->deskey.key.high,
391 arg->deskey.key.low);
393 res.cryptkeyres_u.deskey = arg->deskey;
394 res.status = pk_decrypt(uid, arg->remotename, &(arg->remotekey),
395 &res.cryptkeyres_u.deskey);
397 if (res.status == KEY_SUCCESS) {
398 (void) fprintf(stderr, "%08x%08x\n",
399 res.cryptkeyres_u.deskey.key.high,
400 res.cryptkeyres_u.deskey.key.low);
402 (void) fprintf(stderr, "%s\n", strstatus(res.status));
404 (void) fflush(stderr);
410 key_net_put_2_svc_prog(uid, arg)
414 static keystatus status;
417 (void) fprintf(stderr, "net_put(%s, %.*s, %.*s) = ",
418 arg->st_netname, (int)sizeof (arg->st_pub_key),
419 arg->st_pub_key, (int)sizeof (arg->st_priv_key),
423 status = pk_netput(uid, arg);
426 (void) fprintf(stderr, "%s\n", strstatus(status));
427 (void) fflush(stderr);
434 key_net_get_2_svc_prog(uid, arg)
438 static key_netstres keynetname;
441 (void) fprintf(stderr, "net_get(%ld) = ", uid);
443 keynetname.status = pk_netget(uid, &keynetname.key_netstres_u.knet);
445 if (keynetname.status == KEY_SUCCESS) {
446 fprintf(stderr, "<%s, %.*s, %.*s>\n",
447 keynetname.key_netstres_u.knet.st_netname,
448 (int)sizeof (keynetname.key_netstres_u.knet.st_pub_key),
449 keynetname.key_netstres_u.knet.st_pub_key,
450 (int)sizeof (keynetname.key_netstres_u.knet.st_priv_key),
451 keynetname.key_netstres_u.knet.st_priv_key);
453 (void) fprintf(stderr, "NOT FOUND\n");
455 (void) fflush(stderr);
458 return (&keynetname);
463 key_get_conv_2_svc_prog(uid, arg)
467 static cryptkeyres res;
470 (void) fprintf(stderr, "get_conv(%ld, %.*s) = ", uid,
471 (int)sizeof (arg), arg);
474 res.status = pk_get_conv_key(uid, arg, &res);
477 if (res.status == KEY_SUCCESS) {
478 (void) fprintf(stderr, "%08x%08x\n",
479 res.cryptkeyres_u.deskey.key.high,
480 res.cryptkeyres_u.deskey.key.low);
482 (void) fprintf(stderr, "%s\n", strstatus(res.status));
484 (void) fflush(stderr);
491 key_encrypt_1_svc_prog(uid, arg)
495 static cryptkeyres res;
498 (void) fprintf(stderr, "encrypt(%ld, %s, %08x%08x) = ", uid,
499 arg->remotename, arg->deskey.key.high,
500 arg->deskey.key.low);
502 res.cryptkeyres_u.deskey = arg->deskey;
503 res.status = pk_encrypt(uid, arg->remotename, NULL,
504 &res.cryptkeyres_u.deskey);
506 if (res.status == KEY_SUCCESS) {
507 (void) fprintf(stderr, "%08x%08x\n",
508 res.cryptkeyres_u.deskey.key.high,
509 res.cryptkeyres_u.deskey.key.low);
511 (void) fprintf(stderr, "%s\n", strstatus(res.status));
513 (void) fflush(stderr);
519 key_decrypt_1_svc_prog(uid, arg)
523 static cryptkeyres res;
526 (void) fprintf(stderr, "decrypt(%ld, %s, %08x%08x) = ", uid,
527 arg->remotename, arg->deskey.key.high,
528 arg->deskey.key.low);
530 res.cryptkeyres_u.deskey = arg->deskey;
531 res.status = pk_decrypt(uid, arg->remotename, NULL,
532 &res.cryptkeyres_u.deskey);
534 if (res.status == KEY_SUCCESS) {
535 (void) fprintf(stderr, "%08x%08x\n",
536 res.cryptkeyres_u.deskey.key.high,
537 res.cryptkeyres_u.deskey.key.low);
539 (void) fprintf(stderr, "%s\n", strstatus(res.status));
541 (void) fflush(stderr);
548 key_gen_1_svc_prog(v, s)
553 static des_block keygen;
554 static des_block key;
556 (void) gettimeofday(&time, (struct timezone *) NULL);
557 keygen.key.high += (time.tv_sec ^ time.tv_usec);
558 keygen.key.low += (time.tv_sec ^ time.tv_usec);
559 ecb_crypt((char *)&masterkey, (char *)&keygen, sizeof (keygen),
560 DES_ENCRYPT | DES_HW);
562 des_setparity((char *)&key);
564 (void) fprintf(stderr, "gen() = %08x%08x\n", key.key.high,
566 (void) fflush(stderr);
572 key_getcred_1_svc_prog(uid, name)
576 static getcredres res;
577 static u_int gids[NGROUPS];
578 struct unixcred *cred;
580 cred = &res.getcredres_u.cred;
581 cred->gids.gids_val = gids;
582 if (!netname2user(*name, (uid_t *) &cred->uid, (gid_t *) &cred->gid,
583 (int *)&cred->gids.gids_len, (gid_t *)gids)) {
584 res.status = KEY_UNKNOWN;
586 res.status = KEY_SUCCESS;
589 (void) fprintf(stderr, "getcred(%s) = ", *name);
590 if (res.status == KEY_SUCCESS) {
591 (void) fprintf(stderr, "uid=%d, gid=%d, grouplen=%d\n",
592 cred->uid, cred->gid, cred->gids.gids_len);
594 (void) fprintf(stderr, "%s\n", strstatus(res.status));
596 (void) fflush(stderr);
605 keyprogram(rqstp, transp)
606 struct svc_req *rqstp;
610 keybuf key_set_1_arg;
611 cryptkeyarg key_encrypt_1_arg;
612 cryptkeyarg key_decrypt_1_arg;
613 netnamestr key_getcred_1_arg;
614 cryptkeyarg key_encrypt_2_arg;
615 cryptkeyarg key_decrypt_2_arg;
616 netnamestr key_getcred_2_arg;
617 cryptkeyarg2 key_encrypt_pk_2_arg;
618 cryptkeyarg2 key_decrypt_pk_2_arg;
619 key_netstarg key_net_put_2_arg;
620 netobj key_get_conv_2_arg;
623 bool_t(*xdr_argument)(), (*xdr_result)();
628 switch (rqstp->rq_proc) {
630 svc_sendreply(transp, xdr_void, (char *)NULL);
634 xdr_argument = xdr_keybuf;
635 xdr_result = xdr_int;
636 local = (char *(*)()) key_set_1_svc_prog;
641 xdr_argument = xdr_cryptkeyarg;
642 xdr_result = xdr_cryptkeyres;
643 local = (char *(*)()) key_encrypt_1_svc_prog;
648 xdr_argument = xdr_cryptkeyarg;
649 xdr_result = xdr_cryptkeyres;
650 local = (char *(*)()) key_decrypt_1_svc_prog;
655 xdr_argument = xdr_void;
656 xdr_result = xdr_des_block;
657 local = (char *(*)()) key_gen_1_svc_prog;
662 xdr_argument = xdr_netnamestr;
663 xdr_result = xdr_getcredres;
664 local = (char *(*)()) key_getcred_1_svc_prog;
669 xdr_argument = xdr_cryptkeyarg2;
670 xdr_result = xdr_cryptkeyres;
671 local = (char *(*)()) key_encrypt_pk_2_svc_prog;
676 xdr_argument = xdr_cryptkeyarg2;
677 xdr_result = xdr_cryptkeyres;
678 local = (char *(*)()) key_decrypt_pk_2_svc_prog;
684 xdr_argument = xdr_key_netstarg;
685 xdr_result = xdr_keystatus;
686 local = (char *(*)()) key_net_put_2_svc_prog;
691 xdr_argument = (xdrproc_t) xdr_void;
692 xdr_result = xdr_key_netstres;
693 local = (char *(*)()) key_net_get_2_svc_prog;
698 xdr_argument = (xdrproc_t) xdr_keybuf;
699 xdr_result = xdr_cryptkeyres;
700 local = (char *(*)()) key_get_conv_2_svc_prog;
705 svcerr_noproc(transp);
709 if (root_auth(transp, rqstp) == 0) {
711 (void) fprintf(stderr,
712 "not local privileged process\n");
714 svcerr_weakauth(transp);
717 if (rqstp->rq_cred.oa_flavor != AUTH_SYS) {
719 (void) fprintf(stderr,
720 "not unix authentication\n");
722 svcerr_weakauth(transp);
725 uid = ((struct authsys_parms *)rqstp->rq_clntcred)->aup_uid;
728 memset((char *) &argument, 0, sizeof (argument));
729 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
730 svcerr_decode(transp);
733 result = (*local) (uid, &argument);
734 if (!svc_sendreply(transp, xdr_result, (char *) result)) {
736 (void) fprintf(stderr, "unable to reply\n");
737 svcerr_systemerr(transp);
739 if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
741 (void) fprintf(stderr,
742 "unable to free arguments\n");
749 root_auth(trans, rqstp)
751 struct svc_req *rqstp;
754 struct sockaddr_in *remote;
756 remote = svc_getcaller(trans);
757 if (remote->sin_family == AF_INET) {
759 fprintf(stderr, "client didn't use AF_UNIX\n");
763 if (__rpc_get_local_uid(&uid, trans) < 0) {
765 fprintf(stderr, "__rpc_get_local_uid failed\n");
770 fprintf(stderr, "local_uid %ld\n", uid);
773 if (rqstp->rq_cred.oa_flavor == AUTH_SYS) {
774 if (((uid_t) ((struct authunix_parms *)
775 rqstp->rq_clntcred)->aup_uid)
781 "local_uid %ld mismatches auth %ld\n", uid,
782 ((uid_t) ((struct authunix_parms *)rqstp->rq_clntcred)->aup_uid));
787 fprintf(stderr, "Not auth sys\n");
795 (void) fprintf(stderr,
796 "usage: keyserv [-n] [-D] [-d] [-v] [-p path]\n");
797 (void) fprintf(stderr, "-d disables the use of default keys\n");