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
32 static char sccsid[] = "@(#)keyserv.c 1.15 94/04/25 SMI";
34 static const char rcsid[] =
39 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
44 * Store secret keys per uid. Do public key encryption and decryption
45 * operations. Generate "random" keys.
46 * Do not talk to anything but a local root
47 * process on the local transport only
57 #include <sys/types.h>
59 #include <sys/param.h>
61 #include <rpc/des_crypt.h>
63 #include <rpc/key_prot.h>
64 #include <rpcsvc/crypt.h>
72 #define KEYSERVSOCK "/var/run/keyservsock"
75 static void randomize( des_block * );
76 static void usage( void );
77 static int getrootkey( des_block *, int );
78 static int root_auth( SVCXPRT *, struct svc_req * );
81 static int debugging = 1;
83 static int debugging = 0;
86 static void keyprogram();
87 static des_block masterkey;
89 static char ROOTKEY[] = "/etc/.rootkey";
92 * Hack to allow the keyserver to use AUTH_DES (for authenticated
93 * NIS+ calls, for example). The only functions that get called
94 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
96 * The approach is to have the keyserver fill in pointers to local
97 * implementations of these functions, and to call those in key_call().
100 extern cryptkeyres *(*__key_encryptsession_pk_LOCAL)();
101 extern cryptkeyres *(*__key_decryptsession_pk_LOCAL)();
102 extern des_block *(*__key_gendes_LOCAL)();
103 extern int (*__des_crypt_LOCAL)();
105 cryptkeyres *key_encrypt_pk_2_svc_prog( uid_t, cryptkeyarg2 * );
106 cryptkeyres *key_decrypt_pk_2_svc_prog( uid_t, cryptkeyarg2 * );
107 des_block *key_gen_1_svc_prog( void *, struct svc_req * );
119 register SVCXPRT *transp;
120 struct netconfig *nconf = NULL;
122 __key_encryptsession_pk_LOCAL = &key_encrypt_pk_2_svc_prog;
123 __key_decryptsession_pk_LOCAL = &key_decrypt_pk_2_svc_prog;
124 __key_gendes_LOCAL = &key_gen_1_svc_prog;
126 while ((c = getopt(argc, argv, "ndDvp:")) != -1)
147 load_des(warn, path);
148 __des_crypt_LOCAL = _my_crypt;
149 if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1)
150 errx(1, "failed to register AUTH_DES authenticator");
152 if (optind != argc) {
159 (void) umask(S_IXUSR|S_IXGRP|S_IXOTH);
161 errx(1, "keyserv must be run as root");
162 setmodulus(HEXMODULUS);
163 getrootkey(&masterkey, nflag);
165 rpcb_unset(KEY_PROG, KEY_VERS, NULL);
166 rpcb_unset(KEY_PROG, KEY_VERS2, NULL);
168 if (svc_create(keyprogram, KEY_PROG, KEY_VERS,
170 (void) fprintf(stderr,
171 "%s: unable to create service\n", argv[0]);
175 if (svc_create(keyprogram, KEY_PROG, KEY_VERS2,
177 (void) fprintf(stderr,
178 "%s: unable to create service\n", argv[0]);
182 localhandle = setnetconfig();
183 while ((nconf = getnetconfig(localhandle)) != NULL) {
184 if (nconf->nc_protofmly != NULL &&
185 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
190 errx(1, "getnetconfig: %s", nc_sperror());
193 rpcb_unset(CRYPT_PROG, CRYPT_VERS, nconf);
194 transp = svcunix_create(RPC_ANYSOCK, 0, 0, KEYSERVSOCK);
196 errx(1, "cannot create AF_LOCAL service");
197 if (!svc_reg(transp, KEY_PROG, KEY_VERS, keyprogram, nconf))
198 errx(1, "unable to register (KEY_PROG, KEY_VERS, unix)");
199 if (!svc_reg(transp, KEY_PROG, KEY_VERS2, keyprogram, nconf))
200 errx(1, "unable to register (KEY_PROG, KEY_VERS2, unix)");
201 if (!svc_reg(transp, CRYPT_PROG, CRYPT_VERS, crypt_prog_1, nconf))
202 errx(1, "unable to register (CRYPT_PROG, CRYPT_VERS, unix)");
204 endnetconfig(localhandle);
206 (void) umask(066); /* paranoia */
212 signal(SIGPIPE, SIG_IGN);
220 * In the event that we don't get a root password, we try to
221 * randomize the master key the best we can
234 for (i = 0; i < 1024; i++) {
235 (void)gettimeofday(&tv, NULL);
236 shift = i % 8 * sizeof (int);
237 seed ^= (tv.tv_usec << shift) | (tv.tv_usec >> (32 - shift));
240 #ifdef KEYSERV_RANDOM
242 master->key.low = arc4random();
243 master->key.high = arc4random();
246 master->key.low = random();
247 master->key.high = random();
250 /* use stupid dangerous bad rand() */
256 master->key.low = rand();
257 master->key.high = rand();
262 * Try to get root's secret key, by prompting if terminal is a tty, else trying
263 * from standard input.
264 * Returns 1 on success.
267 getrootkey(master, prompt)
272 char name[MAXNETNAMELEN + 1];
273 char secret[HEXKEYBYTES];
274 key_netstarg netstore;
279 * Read secret key out of ROOTKEY
281 fd = open(ROOTKEY, O_RDONLY, 0);
286 if (read(fd, secret, HEXKEYBYTES) < HEXKEYBYTES) {
287 warnx("the key read from %s was too short", ROOTKEY);
292 if (!getnetname(name)) {
294 "failed to generate host's netname when establishing root's key");
297 memcpy(netstore.st_priv_key, secret, HEXKEYBYTES);
298 memset(netstore.st_pub_key, 0, HEXKEYBYTES);
299 netstore.st_netname = name;
300 if (pk_netput(0, &netstore) != KEY_SUCCESS) {
301 warnx("could not set root's key and netname");
307 * Decrypt yellow pages publickey entry to get secret key
309 passwd = getpass("root password:");
310 passwd2des(passwd, (char *)master);
312 if (!getsecretkey(name, secret, passwd)) {
313 warnx("can't find %s's secret key", name);
316 if (secret[0] == 0) {
317 warnx("password does not decrypt secret key for %s", name);
320 (void) pk_setkey(0, secret);
322 * Store it for future use in $ROOTKEY, if possible
324 fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
328 write(fd, secret, strlen(secret));
329 write(fd, &newline, sizeof (newline));
336 * Procedures to implement RPC service
344 return ("KEY_SUCCESS");
346 return ("KEY_NOSECRET");
348 return ("KEY_UNKNOWN");
350 return ("KEY_SYSTEMERR");
352 return ("(bad result code)");
357 key_set_1_svc_prog(uid, key)
361 static keystatus status;
364 (void) fprintf(stderr, "set(%u, %.*s) = ", uid,
365 (int) sizeof (keybuf), key);
367 status = pk_setkey(uid, key);
369 (void) fprintf(stderr, "%s\n", strstatus(status));
370 (void) fflush(stderr);
376 key_encrypt_pk_2_svc_prog(uid, arg)
380 static cryptkeyres res;
383 (void) fprintf(stderr, "encrypt(%u, %s, %08x%08x) = ", uid,
384 arg->remotename, arg->deskey.key.high,
385 arg->deskey.key.low);
387 res.cryptkeyres_u.deskey = arg->deskey;
388 res.status = pk_encrypt(uid, arg->remotename, &(arg->remotekey),
389 &res.cryptkeyres_u.deskey);
391 if (res.status == KEY_SUCCESS) {
392 (void) fprintf(stderr, "%08x%08x\n",
393 res.cryptkeyres_u.deskey.key.high,
394 res.cryptkeyres_u.deskey.key.low);
396 (void) fprintf(stderr, "%s\n", strstatus(res.status));
398 (void) fflush(stderr);
404 key_decrypt_pk_2_svc_prog(uid, arg)
408 static cryptkeyres res;
411 (void) fprintf(stderr, "decrypt(%u, %s, %08x%08x) = ", uid,
412 arg->remotename, arg->deskey.key.high,
413 arg->deskey.key.low);
415 res.cryptkeyres_u.deskey = arg->deskey;
416 res.status = pk_decrypt(uid, arg->remotename, &(arg->remotekey),
417 &res.cryptkeyres_u.deskey);
419 if (res.status == KEY_SUCCESS) {
420 (void) fprintf(stderr, "%08x%08x\n",
421 res.cryptkeyres_u.deskey.key.high,
422 res.cryptkeyres_u.deskey.key.low);
424 (void) fprintf(stderr, "%s\n", strstatus(res.status));
426 (void) fflush(stderr);
432 key_net_put_2_svc_prog(uid, arg)
436 static keystatus status;
439 (void) fprintf(stderr, "net_put(%s, %.*s, %.*s) = ",
440 arg->st_netname, (int)sizeof (arg->st_pub_key),
441 arg->st_pub_key, (int)sizeof (arg->st_priv_key),
445 status = pk_netput(uid, arg);
448 (void) fprintf(stderr, "%s\n", strstatus(status));
449 (void) fflush(stderr);
456 key_net_get_2_svc_prog(uid, arg)
460 static key_netstres keynetname;
463 (void) fprintf(stderr, "net_get(%u) = ", uid);
465 keynetname.status = pk_netget(uid, &keynetname.key_netstres_u.knet);
467 if (keynetname.status == KEY_SUCCESS) {
468 fprintf(stderr, "<%s, %.*s, %.*s>\n",
469 keynetname.key_netstres_u.knet.st_netname,
470 (int)sizeof (keynetname.key_netstres_u.knet.st_pub_key),
471 keynetname.key_netstres_u.knet.st_pub_key,
472 (int)sizeof (keynetname.key_netstres_u.knet.st_priv_key),
473 keynetname.key_netstres_u.knet.st_priv_key);
475 (void) fprintf(stderr, "NOT FOUND\n");
477 (void) fflush(stderr);
480 return (&keynetname);
485 key_get_conv_2_svc_prog(uid, arg)
489 static cryptkeyres res;
492 (void) fprintf(stderr, "get_conv(%u, %.*s) = ", uid,
493 (int)sizeof (keybuf), arg);
496 res.status = pk_get_conv_key(uid, arg, &res);
499 if (res.status == KEY_SUCCESS) {
500 (void) fprintf(stderr, "%08x%08x\n",
501 res.cryptkeyres_u.deskey.key.high,
502 res.cryptkeyres_u.deskey.key.low);
504 (void) fprintf(stderr, "%s\n", strstatus(res.status));
506 (void) fflush(stderr);
513 key_encrypt_1_svc_prog(uid, arg)
517 static cryptkeyres res;
520 (void) fprintf(stderr, "encrypt(%u, %s, %08x%08x) = ", uid,
521 arg->remotename, arg->deskey.key.high,
522 arg->deskey.key.low);
524 res.cryptkeyres_u.deskey = arg->deskey;
525 res.status = pk_encrypt(uid, arg->remotename, NULL,
526 &res.cryptkeyres_u.deskey);
528 if (res.status == KEY_SUCCESS) {
529 (void) fprintf(stderr, "%08x%08x\n",
530 res.cryptkeyres_u.deskey.key.high,
531 res.cryptkeyres_u.deskey.key.low);
533 (void) fprintf(stderr, "%s\n", strstatus(res.status));
535 (void) fflush(stderr);
541 key_decrypt_1_svc_prog(uid, arg)
545 static cryptkeyres res;
548 (void) fprintf(stderr, "decrypt(%u, %s, %08x%08x) = ", uid,
549 arg->remotename, arg->deskey.key.high,
550 arg->deskey.key.low);
552 res.cryptkeyres_u.deskey = arg->deskey;
553 res.status = pk_decrypt(uid, arg->remotename, NULL,
554 &res.cryptkeyres_u.deskey);
556 if (res.status == KEY_SUCCESS) {
557 (void) fprintf(stderr, "%08x%08x\n",
558 res.cryptkeyres_u.deskey.key.high,
559 res.cryptkeyres_u.deskey.key.low);
561 (void) fprintf(stderr, "%s\n", strstatus(res.status));
563 (void) fflush(stderr);
570 key_gen_1_svc_prog(v, s)
575 static des_block keygen;
576 static des_block key;
578 (void)gettimeofday(&time, NULL);
579 keygen.key.high += (time.tv_sec ^ time.tv_usec);
580 keygen.key.low += (time.tv_sec ^ time.tv_usec);
581 ecb_crypt((char *)&masterkey, (char *)&keygen, sizeof (keygen),
582 DES_ENCRYPT | DES_HW);
584 des_setparity((char *)&key);
586 (void) fprintf(stderr, "gen() = %08x%08x\n", key.key.high,
588 (void) fflush(stderr);
594 key_getcred_1_svc_prog(uid, name)
598 static getcredres res;
599 static u_int gids[NGROUPS];
600 struct unixcred *cred;
602 cred = &res.getcredres_u.cred;
603 cred->gids.gids_val = gids;
604 if (!netname2user(*name, (uid_t *) &cred->uid, (gid_t *) &cred->gid,
605 (int *)&cred->gids.gids_len, (gid_t *)gids)) {
606 res.status = KEY_UNKNOWN;
608 res.status = KEY_SUCCESS;
611 (void) fprintf(stderr, "getcred(%s) = ", *name);
612 if (res.status == KEY_SUCCESS) {
613 (void) fprintf(stderr, "uid=%d, gid=%d, grouplen=%d\n",
614 cred->uid, cred->gid, cred->gids.gids_len);
616 (void) fprintf(stderr, "%s\n", strstatus(res.status));
618 (void) fflush(stderr);
627 keyprogram(rqstp, transp)
628 struct svc_req *rqstp;
632 keybuf key_set_1_arg;
633 cryptkeyarg key_encrypt_1_arg;
634 cryptkeyarg key_decrypt_1_arg;
635 netnamestr key_getcred_1_arg;
636 cryptkeyarg key_encrypt_2_arg;
637 cryptkeyarg key_decrypt_2_arg;
638 netnamestr key_getcred_2_arg;
639 cryptkeyarg2 key_encrypt_pk_2_arg;
640 cryptkeyarg2 key_decrypt_pk_2_arg;
641 key_netstarg key_net_put_2_arg;
642 netobj key_get_conv_2_arg;
645 xdrproc_t xdr_argument, xdr_result;
650 switch (rqstp->rq_proc) {
652 svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
656 xdr_argument = (xdrproc_t)xdr_keybuf;
657 xdr_result = (xdrproc_t)xdr_int;
658 local = (char *(*)()) key_set_1_svc_prog;
663 xdr_argument = (xdrproc_t)xdr_cryptkeyarg;
664 xdr_result = (xdrproc_t)xdr_cryptkeyres;
665 local = (char *(*)()) key_encrypt_1_svc_prog;
670 xdr_argument = (xdrproc_t)xdr_cryptkeyarg;
671 xdr_result = (xdrproc_t)xdr_cryptkeyres;
672 local = (char *(*)()) key_decrypt_1_svc_prog;
677 xdr_argument = (xdrproc_t)xdr_void;
678 xdr_result = (xdrproc_t)xdr_des_block;
679 local = (char *(*)()) key_gen_1_svc_prog;
684 xdr_argument = (xdrproc_t)xdr_netnamestr;
685 xdr_result = (xdrproc_t)xdr_getcredres;
686 local = (char *(*)()) key_getcred_1_svc_prog;
691 xdr_argument = (xdrproc_t)xdr_cryptkeyarg2;
692 xdr_result = (xdrproc_t)xdr_cryptkeyres;
693 local = (char *(*)()) key_encrypt_pk_2_svc_prog;
698 xdr_argument = (xdrproc_t)xdr_cryptkeyarg2;
699 xdr_result = (xdrproc_t)xdr_cryptkeyres;
700 local = (char *(*)()) key_decrypt_pk_2_svc_prog;
706 xdr_argument = (xdrproc_t)xdr_key_netstarg;
707 xdr_result = (xdrproc_t)xdr_keystatus;
708 local = (char *(*)()) key_net_put_2_svc_prog;
713 xdr_argument = (xdrproc_t) xdr_void;
714 xdr_result = (xdrproc_t)xdr_key_netstres;
715 local = (char *(*)()) key_net_get_2_svc_prog;
720 xdr_argument = (xdrproc_t) xdr_keybuf;
721 xdr_result = (xdrproc_t)xdr_cryptkeyres;
722 local = (char *(*)()) key_get_conv_2_svc_prog;
727 svcerr_noproc(transp);
731 if (root_auth(transp, rqstp) == 0) {
733 (void) fprintf(stderr,
734 "not local privileged process\n");
736 svcerr_weakauth(transp);
739 if (rqstp->rq_cred.oa_flavor != AUTH_SYS) {
741 (void) fprintf(stderr,
742 "not unix authentication\n");
744 svcerr_weakauth(transp);
747 uid = ((struct authsys_parms *)rqstp->rq_clntcred)->aup_uid;
750 memset(&argument, 0, sizeof (argument));
751 if (!svc_getargs(transp, xdr_argument, &argument)) {
752 svcerr_decode(transp);
755 result = (*local) (uid, &argument);
756 if (!svc_sendreply(transp, xdr_result, result)) {
758 (void) fprintf(stderr, "unable to reply\n");
759 svcerr_systemerr(transp);
761 if (!svc_freeargs(transp, xdr_argument, &argument)) {
763 (void) fprintf(stderr,
764 "unable to free arguments\n");
771 root_auth(trans, rqstp)
773 struct svc_req *rqstp;
776 struct sockaddr *remote;
778 remote = svc_getrpccaller(trans)->buf;
779 if (remote->sa_family != AF_UNIX) {
781 fprintf(stderr, "client didn't use AF_UNIX\n");
785 if (__rpc_get_local_uid(trans, &uid) < 0) {
787 fprintf(stderr, "__rpc_get_local_uid failed\n");
792 fprintf(stderr, "local_uid %u\n", uid);
795 if (rqstp->rq_cred.oa_flavor == AUTH_SYS) {
796 if (((uid_t) ((struct authunix_parms *)
797 rqstp->rq_clntcred)->aup_uid)
803 "local_uid %u mismatches auth %u\n", uid,
804 ((uid_t) ((struct authunix_parms *)rqstp->rq_clntcred)->aup_uid));
809 fprintf(stderr, "Not auth sys\n");
817 (void) fprintf(stderr,
818 "usage: keyserv [-n] [-D] [-d] [-v] [-p path]\n");
819 (void) fprintf(stderr, "-d disables the use of default keys\n");