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
227 master->key.low = arc4random();
228 master->key.high = arc4random();
232 * Try to get root's secret key, by prompting if terminal is a tty, else trying
233 * from standard input.
234 * Returns 1 on success.
237 getrootkey(master, prompt)
242 char name[MAXNETNAMELEN + 1];
243 char secret[HEXKEYBYTES];
244 key_netstarg netstore;
249 * Read secret key out of ROOTKEY
251 fd = open(ROOTKEY, O_RDONLY, 0);
256 if (read(fd, secret, HEXKEYBYTES) < HEXKEYBYTES) {
257 warnx("the key read from %s was too short", ROOTKEY);
262 if (!getnetname(name)) {
264 "failed to generate host's netname when establishing root's key");
267 memcpy(netstore.st_priv_key, secret, HEXKEYBYTES);
268 memset(netstore.st_pub_key, 0, HEXKEYBYTES);
269 netstore.st_netname = name;
270 if (pk_netput(0, &netstore) != KEY_SUCCESS) {
271 warnx("could not set root's key and netname");
277 * Decrypt yellow pages publickey entry to get secret key
279 passwd = getpass("root password:");
280 passwd2des(passwd, (char *)master);
282 if (!getsecretkey(name, secret, passwd)) {
283 warnx("can't find %s's secret key", name);
286 if (secret[0] == 0) {
287 warnx("password does not decrypt secret key for %s", name);
290 (void) pk_setkey(0, secret);
292 * Store it for future use in $ROOTKEY, if possible
294 fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
298 write(fd, secret, strlen(secret));
299 write(fd, &newline, sizeof (newline));
306 * Procedures to implement RPC service
314 return ("KEY_SUCCESS");
316 return ("KEY_NOSECRET");
318 return ("KEY_UNKNOWN");
320 return ("KEY_SYSTEMERR");
322 return ("(bad result code)");
327 key_set_1_svc_prog(uid, key)
331 static keystatus status;
334 (void) fprintf(stderr, "set(%u, %.*s) = ", uid,
335 (int) sizeof (keybuf), key);
337 status = pk_setkey(uid, key);
339 (void) fprintf(stderr, "%s\n", strstatus(status));
340 (void) fflush(stderr);
346 key_encrypt_pk_2_svc_prog(uid, arg)
350 static cryptkeyres res;
353 (void) fprintf(stderr, "encrypt(%u, %s, %08x%08x) = ", uid,
354 arg->remotename, arg->deskey.key.high,
355 arg->deskey.key.low);
357 res.cryptkeyres_u.deskey = arg->deskey;
358 res.status = pk_encrypt(uid, arg->remotename, &(arg->remotekey),
359 &res.cryptkeyres_u.deskey);
361 if (res.status == KEY_SUCCESS) {
362 (void) fprintf(stderr, "%08x%08x\n",
363 res.cryptkeyres_u.deskey.key.high,
364 res.cryptkeyres_u.deskey.key.low);
366 (void) fprintf(stderr, "%s\n", strstatus(res.status));
368 (void) fflush(stderr);
374 key_decrypt_pk_2_svc_prog(uid, arg)
378 static cryptkeyres res;
381 (void) fprintf(stderr, "decrypt(%u, %s, %08x%08x) = ", uid,
382 arg->remotename, arg->deskey.key.high,
383 arg->deskey.key.low);
385 res.cryptkeyres_u.deskey = arg->deskey;
386 res.status = pk_decrypt(uid, arg->remotename, &(arg->remotekey),
387 &res.cryptkeyres_u.deskey);
389 if (res.status == KEY_SUCCESS) {
390 (void) fprintf(stderr, "%08x%08x\n",
391 res.cryptkeyres_u.deskey.key.high,
392 res.cryptkeyres_u.deskey.key.low);
394 (void) fprintf(stderr, "%s\n", strstatus(res.status));
396 (void) fflush(stderr);
402 key_net_put_2_svc_prog(uid, arg)
406 static keystatus status;
409 (void) fprintf(stderr, "net_put(%s, %.*s, %.*s) = ",
410 arg->st_netname, (int)sizeof (arg->st_pub_key),
411 arg->st_pub_key, (int)sizeof (arg->st_priv_key),
415 status = pk_netput(uid, arg);
418 (void) fprintf(stderr, "%s\n", strstatus(status));
419 (void) fflush(stderr);
426 key_net_get_2_svc_prog(uid, arg)
430 static key_netstres keynetname;
433 (void) fprintf(stderr, "net_get(%u) = ", uid);
435 keynetname.status = pk_netget(uid, &keynetname.key_netstres_u.knet);
437 if (keynetname.status == KEY_SUCCESS) {
438 fprintf(stderr, "<%s, %.*s, %.*s>\n",
439 keynetname.key_netstres_u.knet.st_netname,
440 (int)sizeof (keynetname.key_netstres_u.knet.st_pub_key),
441 keynetname.key_netstres_u.knet.st_pub_key,
442 (int)sizeof (keynetname.key_netstres_u.knet.st_priv_key),
443 keynetname.key_netstres_u.knet.st_priv_key);
445 (void) fprintf(stderr, "NOT FOUND\n");
447 (void) fflush(stderr);
450 return (&keynetname);
455 key_get_conv_2_svc_prog(uid, arg)
459 static cryptkeyres res;
462 (void) fprintf(stderr, "get_conv(%u, %.*s) = ", uid,
463 (int)sizeof (keybuf), arg);
466 res.status = pk_get_conv_key(uid, arg, &res);
469 if (res.status == KEY_SUCCESS) {
470 (void) fprintf(stderr, "%08x%08x\n",
471 res.cryptkeyres_u.deskey.key.high,
472 res.cryptkeyres_u.deskey.key.low);
474 (void) fprintf(stderr, "%s\n", strstatus(res.status));
476 (void) fflush(stderr);
483 key_encrypt_1_svc_prog(uid, arg)
487 static cryptkeyres res;
490 (void) fprintf(stderr, "encrypt(%u, %s, %08x%08x) = ", uid,
491 arg->remotename, arg->deskey.key.high,
492 arg->deskey.key.low);
494 res.cryptkeyres_u.deskey = arg->deskey;
495 res.status = pk_encrypt(uid, arg->remotename, NULL,
496 &res.cryptkeyres_u.deskey);
498 if (res.status == KEY_SUCCESS) {
499 (void) fprintf(stderr, "%08x%08x\n",
500 res.cryptkeyres_u.deskey.key.high,
501 res.cryptkeyres_u.deskey.key.low);
503 (void) fprintf(stderr, "%s\n", strstatus(res.status));
505 (void) fflush(stderr);
511 key_decrypt_1_svc_prog(uid, arg)
515 static cryptkeyres res;
518 (void) fprintf(stderr, "decrypt(%u, %s, %08x%08x) = ", uid,
519 arg->remotename, arg->deskey.key.high,
520 arg->deskey.key.low);
522 res.cryptkeyres_u.deskey = arg->deskey;
523 res.status = pk_decrypt(uid, arg->remotename, NULL,
524 &res.cryptkeyres_u.deskey);
526 if (res.status == KEY_SUCCESS) {
527 (void) fprintf(stderr, "%08x%08x\n",
528 res.cryptkeyres_u.deskey.key.high,
529 res.cryptkeyres_u.deskey.key.low);
531 (void) fprintf(stderr, "%s\n", strstatus(res.status));
533 (void) fflush(stderr);
540 key_gen_1_svc_prog(v, s)
545 static des_block keygen;
546 static des_block key;
548 (void)gettimeofday(&time, NULL);
549 keygen.key.high += (time.tv_sec ^ time.tv_usec);
550 keygen.key.low += (time.tv_sec ^ time.tv_usec);
551 ecb_crypt((char *)&masterkey, (char *)&keygen, sizeof (keygen),
552 DES_ENCRYPT | DES_HW);
554 des_setparity((char *)&key);
556 (void) fprintf(stderr, "gen() = %08x%08x\n", key.key.high,
558 (void) fflush(stderr);
564 key_getcred_1_svc_prog(uid, name)
568 static getcredres res;
569 static u_int gids[NGROUPS];
570 struct unixcred *cred;
572 cred = &res.getcredres_u.cred;
573 cred->gids.gids_val = gids;
574 if (!netname2user(*name, (uid_t *) &cred->uid, (gid_t *) &cred->gid,
575 (int *)&cred->gids.gids_len, (gid_t *)gids)) {
576 res.status = KEY_UNKNOWN;
578 res.status = KEY_SUCCESS;
581 (void) fprintf(stderr, "getcred(%s) = ", *name);
582 if (res.status == KEY_SUCCESS) {
583 (void) fprintf(stderr, "uid=%d, gid=%d, grouplen=%d\n",
584 cred->uid, cred->gid, cred->gids.gids_len);
586 (void) fprintf(stderr, "%s\n", strstatus(res.status));
588 (void) fflush(stderr);
597 keyprogram(rqstp, transp)
598 struct svc_req *rqstp;
602 keybuf key_set_1_arg;
603 cryptkeyarg key_encrypt_1_arg;
604 cryptkeyarg key_decrypt_1_arg;
605 netnamestr key_getcred_1_arg;
606 cryptkeyarg key_encrypt_2_arg;
607 cryptkeyarg key_decrypt_2_arg;
608 netnamestr key_getcred_2_arg;
609 cryptkeyarg2 key_encrypt_pk_2_arg;
610 cryptkeyarg2 key_decrypt_pk_2_arg;
611 key_netstarg key_net_put_2_arg;
612 netobj key_get_conv_2_arg;
615 xdrproc_t xdr_argument, xdr_result;
620 switch (rqstp->rq_proc) {
622 svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
626 xdr_argument = (xdrproc_t)xdr_keybuf;
627 xdr_result = (xdrproc_t)xdr_int;
628 local = (char *(*)()) key_set_1_svc_prog;
633 xdr_argument = (xdrproc_t)xdr_cryptkeyarg;
634 xdr_result = (xdrproc_t)xdr_cryptkeyres;
635 local = (char *(*)()) key_encrypt_1_svc_prog;
640 xdr_argument = (xdrproc_t)xdr_cryptkeyarg;
641 xdr_result = (xdrproc_t)xdr_cryptkeyres;
642 local = (char *(*)()) key_decrypt_1_svc_prog;
647 xdr_argument = (xdrproc_t)xdr_void;
648 xdr_result = (xdrproc_t)xdr_des_block;
649 local = (char *(*)()) key_gen_1_svc_prog;
654 xdr_argument = (xdrproc_t)xdr_netnamestr;
655 xdr_result = (xdrproc_t)xdr_getcredres;
656 local = (char *(*)()) key_getcred_1_svc_prog;
661 xdr_argument = (xdrproc_t)xdr_cryptkeyarg2;
662 xdr_result = (xdrproc_t)xdr_cryptkeyres;
663 local = (char *(*)()) key_encrypt_pk_2_svc_prog;
668 xdr_argument = (xdrproc_t)xdr_cryptkeyarg2;
669 xdr_result = (xdrproc_t)xdr_cryptkeyres;
670 local = (char *(*)()) key_decrypt_pk_2_svc_prog;
676 xdr_argument = (xdrproc_t)xdr_key_netstarg;
677 xdr_result = (xdrproc_t)xdr_keystatus;
678 local = (char *(*)()) key_net_put_2_svc_prog;
683 xdr_argument = (xdrproc_t) xdr_void;
684 xdr_result = (xdrproc_t)xdr_key_netstres;
685 local = (char *(*)()) key_net_get_2_svc_prog;
690 xdr_argument = (xdrproc_t) xdr_keybuf;
691 xdr_result = (xdrproc_t)xdr_cryptkeyres;
692 local = (char *(*)()) key_get_conv_2_svc_prog;
697 svcerr_noproc(transp);
701 if (root_auth(transp, rqstp) == 0) {
703 (void) fprintf(stderr,
704 "not local privileged process\n");
706 svcerr_weakauth(transp);
709 if (rqstp->rq_cred.oa_flavor != AUTH_SYS) {
711 (void) fprintf(stderr,
712 "not unix authentication\n");
714 svcerr_weakauth(transp);
717 uid = ((struct authsys_parms *)rqstp->rq_clntcred)->aup_uid;
720 memset(&argument, 0, sizeof (argument));
721 if (!svc_getargs(transp, xdr_argument, &argument)) {
722 svcerr_decode(transp);
725 result = (*local) (uid, &argument);
726 if (!svc_sendreply(transp, xdr_result, result)) {
728 (void) fprintf(stderr, "unable to reply\n");
729 svcerr_systemerr(transp);
731 if (!svc_freeargs(transp, xdr_argument, &argument)) {
733 (void) fprintf(stderr,
734 "unable to free arguments\n");
741 root_auth(trans, rqstp)
743 struct svc_req *rqstp;
746 struct sockaddr *remote;
748 remote = svc_getrpccaller(trans)->buf;
749 if (remote->sa_family != AF_UNIX) {
751 fprintf(stderr, "client didn't use AF_UNIX\n");
755 if (__rpc_get_local_uid(trans, &uid) < 0) {
757 fprintf(stderr, "__rpc_get_local_uid failed\n");
762 fprintf(stderr, "local_uid %u\n", uid);
765 if (rqstp->rq_cred.oa_flavor == AUTH_SYS) {
766 if (((uid_t) ((struct authunix_parms *)
767 rqstp->rq_clntcred)->aup_uid)
773 "local_uid %u mismatches auth %u\n", uid,
774 ((uid_t) ((struct authunix_parms *)rqstp->rq_clntcred)->aup_uid));
779 fprintf(stderr, "Not auth sys\n");
787 (void) fprintf(stderr,
788 "usage: keyserv [-n] [-D] [-d] [-v] [-p path]\n");
789 (void) fprintf(stderr, "-d disables the use of default keys\n");