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(struct svc_req *rqstp, SVCXPRT *transp);
87 static des_block masterkey;
88 static char ROOTKEY[] = "/etc/.rootkey";
91 * Hack to allow the keyserver to use AUTH_DES (for authenticated
92 * NIS+ calls, for example). The only functions that get called
93 * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes.
95 * The approach is to have the keyserver fill in pointers to local
96 * implementations of these functions, and to call those in key_call().
99 extern cryptkeyres *(*__key_encryptsession_pk_LOCAL)();
100 extern cryptkeyres *(*__key_decryptsession_pk_LOCAL)();
101 extern des_block *(*__key_gendes_LOCAL)();
102 extern int (*__des_crypt_LOCAL)();
104 cryptkeyres *key_encrypt_pk_2_svc_prog( uid_t, cryptkeyarg2 * );
105 cryptkeyres *key_decrypt_pk_2_svc_prog( uid_t, cryptkeyarg2 * );
106 des_block *key_gen_1_svc_prog( void *, struct svc_req * );
109 main(int argc, char *argv[])
116 register SVCXPRT *transp;
117 struct netconfig *nconf = NULL;
119 __key_encryptsession_pk_LOCAL = &key_encrypt_pk_2_svc_prog;
120 __key_decryptsession_pk_LOCAL = &key_decrypt_pk_2_svc_prog;
121 __key_gendes_LOCAL = &key_gen_1_svc_prog;
123 while ((c = getopt(argc, argv, "ndDvp:")) != -1)
144 load_des(warn, path);
145 __des_crypt_LOCAL = _my_crypt;
146 if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1)
147 errx(1, "failed to register AUTH_DES authenticator");
149 if (optind != argc) {
156 (void) umask(S_IXUSR|S_IXGRP|S_IXOTH);
158 errx(1, "keyserv must be run as root");
159 setmodulus(HEXMODULUS);
160 getrootkey(&masterkey, nflag);
162 rpcb_unset(KEY_PROG, KEY_VERS, NULL);
163 rpcb_unset(KEY_PROG, KEY_VERS2, NULL);
165 if (svc_create(keyprogram, KEY_PROG, KEY_VERS,
167 (void) fprintf(stderr,
168 "%s: unable to create service\n", argv[0]);
172 if (svc_create(keyprogram, KEY_PROG, KEY_VERS2,
174 (void) fprintf(stderr,
175 "%s: unable to create service\n", argv[0]);
179 localhandle = setnetconfig();
180 while ((nconf = getnetconfig(localhandle)) != NULL) {
181 if (nconf->nc_protofmly != NULL &&
182 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
187 errx(1, "getnetconfig: %s", nc_sperror());
190 rpcb_unset(CRYPT_PROG, CRYPT_VERS, nconf);
191 transp = svcunix_create(RPC_ANYSOCK, 0, 0, KEYSERVSOCK);
193 errx(1, "cannot create AF_LOCAL service");
194 if (!svc_reg(transp, KEY_PROG, KEY_VERS, keyprogram, nconf))
195 errx(1, "unable to register (KEY_PROG, KEY_VERS, unix)");
196 if (!svc_reg(transp, KEY_PROG, KEY_VERS2, keyprogram, nconf))
197 errx(1, "unable to register (KEY_PROG, KEY_VERS2, unix)");
198 if (!svc_reg(transp, CRYPT_PROG, CRYPT_VERS, crypt_prog_1, nconf))
199 errx(1, "unable to register (CRYPT_PROG, CRYPT_VERS, unix)");
201 endnetconfig(localhandle);
203 (void) umask(066); /* paranoia */
209 signal(SIGPIPE, SIG_IGN);
217 * In the event that we don't get a root password, we try to
218 * randomize the master key the best we can
221 randomize(des_block *master)
223 master->key.low = arc4random();
224 master->key.high = arc4random();
228 * Try to get root's secret key, by prompting if terminal is a tty, else trying
229 * from standard input.
230 * Returns 1 on success.
233 getrootkey(des_block *master, int prompt)
236 char name[MAXNETNAMELEN + 1];
237 char secret[HEXKEYBYTES];
238 key_netstarg netstore;
243 * Read secret key out of ROOTKEY
245 fd = open(ROOTKEY, O_RDONLY, 0);
250 if (read(fd, secret, HEXKEYBYTES) < HEXKEYBYTES) {
251 warnx("the key read from %s was too short", ROOTKEY);
256 if (!getnetname(name)) {
258 "failed to generate host's netname when establishing root's key");
261 memcpy(netstore.st_priv_key, secret, HEXKEYBYTES);
262 memset(netstore.st_pub_key, 0, HEXKEYBYTES);
263 netstore.st_netname = name;
264 if (pk_netput(0, &netstore) != KEY_SUCCESS) {
265 warnx("could not set root's key and netname");
271 * Decrypt yellow pages publickey entry to get secret key
273 passwd = getpass("root password:");
274 passwd2des(passwd, (char *)master);
276 if (!getsecretkey(name, secret, passwd)) {
277 warnx("can't find %s's secret key", name);
280 if (secret[0] == 0) {
281 warnx("password does not decrypt secret key for %s", name);
284 (void) pk_setkey(0, secret);
286 * Store it for future use in $ROOTKEY, if possible
288 fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
292 write(fd, secret, strlen(secret));
293 write(fd, &newline, sizeof (newline));
300 * Procedures to implement RPC service
303 strstatus(keystatus status)
307 return ("KEY_SUCCESS");
309 return ("KEY_NOSECRET");
311 return ("KEY_UNKNOWN");
313 return ("KEY_SYSTEMERR");
315 return ("(bad result code)");
320 key_set_1_svc_prog(uid_t uid, keybuf key)
322 static keystatus status;
325 (void) fprintf(stderr, "set(%u, %.*s) = ", uid,
326 (int) sizeof (keybuf), key);
328 status = pk_setkey(uid, key);
330 (void) fprintf(stderr, "%s\n", strstatus(status));
331 (void) fflush(stderr);
337 key_encrypt_pk_2_svc_prog(uid_t uid, cryptkeyarg2 *arg)
339 static cryptkeyres res;
342 (void) fprintf(stderr, "encrypt(%u, %s, %08x%08x) = ", uid,
343 arg->remotename, arg->deskey.key.high,
344 arg->deskey.key.low);
346 res.cryptkeyres_u.deskey = arg->deskey;
347 res.status = pk_encrypt(uid, arg->remotename, &(arg->remotekey),
348 &res.cryptkeyres_u.deskey);
350 if (res.status == KEY_SUCCESS) {
351 (void) fprintf(stderr, "%08x%08x\n",
352 res.cryptkeyres_u.deskey.key.high,
353 res.cryptkeyres_u.deskey.key.low);
355 (void) fprintf(stderr, "%s\n", strstatus(res.status));
357 (void) fflush(stderr);
363 key_decrypt_pk_2_svc_prog(uid_t uid, cryptkeyarg2 *arg)
365 static cryptkeyres res;
368 (void) fprintf(stderr, "decrypt(%u, %s, %08x%08x) = ", uid,
369 arg->remotename, arg->deskey.key.high,
370 arg->deskey.key.low);
372 res.cryptkeyres_u.deskey = arg->deskey;
373 res.status = pk_decrypt(uid, arg->remotename, &(arg->remotekey),
374 &res.cryptkeyres_u.deskey);
376 if (res.status == KEY_SUCCESS) {
377 (void) fprintf(stderr, "%08x%08x\n",
378 res.cryptkeyres_u.deskey.key.high,
379 res.cryptkeyres_u.deskey.key.low);
381 (void) fprintf(stderr, "%s\n", strstatus(res.status));
383 (void) fflush(stderr);
389 key_net_put_2_svc_prog(uid_t uid, key_netstarg *arg)
391 static keystatus status;
394 (void) fprintf(stderr, "net_put(%s, %.*s, %.*s) = ",
395 arg->st_netname, (int)sizeof (arg->st_pub_key),
396 arg->st_pub_key, (int)sizeof (arg->st_priv_key),
400 status = pk_netput(uid, arg);
403 (void) fprintf(stderr, "%s\n", strstatus(status));
404 (void) fflush(stderr);
411 key_net_get_2_svc_prog(uid_t uid, void *arg)
413 static key_netstres keynetname;
416 (void) fprintf(stderr, "net_get(%u) = ", uid);
418 keynetname.status = pk_netget(uid, &keynetname.key_netstres_u.knet);
420 if (keynetname.status == KEY_SUCCESS) {
421 fprintf(stderr, "<%s, %.*s, %.*s>\n",
422 keynetname.key_netstres_u.knet.st_netname,
423 (int)sizeof (keynetname.key_netstres_u.knet.st_pub_key),
424 keynetname.key_netstres_u.knet.st_pub_key,
425 (int)sizeof (keynetname.key_netstres_u.knet.st_priv_key),
426 keynetname.key_netstres_u.knet.st_priv_key);
428 (void) fprintf(stderr, "NOT FOUND\n");
430 (void) fflush(stderr);
433 return (&keynetname);
438 key_get_conv_2_svc_prog(uid_t uid, keybuf arg)
440 static cryptkeyres res;
443 (void) fprintf(stderr, "get_conv(%u, %.*s) = ", uid,
444 (int)sizeof (keybuf), arg);
447 res.status = pk_get_conv_key(uid, arg, &res);
450 if (res.status == KEY_SUCCESS) {
451 (void) fprintf(stderr, "%08x%08x\n",
452 res.cryptkeyres_u.deskey.key.high,
453 res.cryptkeyres_u.deskey.key.low);
455 (void) fprintf(stderr, "%s\n", strstatus(res.status));
457 (void) fflush(stderr);
464 key_encrypt_1_svc_prog(uid_t uid, cryptkeyarg *arg)
466 static cryptkeyres res;
469 (void) fprintf(stderr, "encrypt(%u, %s, %08x%08x) = ", uid,
470 arg->remotename, arg->deskey.key.high,
471 arg->deskey.key.low);
473 res.cryptkeyres_u.deskey = arg->deskey;
474 res.status = pk_encrypt(uid, arg->remotename, NULL,
475 &res.cryptkeyres_u.deskey);
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);
490 key_decrypt_1_svc_prog(uid_t uid, cryptkeyarg *arg)
492 static cryptkeyres res;
495 (void) fprintf(stderr, "decrypt(%u, %s, %08x%08x) = ", uid,
496 arg->remotename, arg->deskey.key.high,
497 arg->deskey.key.low);
499 res.cryptkeyres_u.deskey = arg->deskey;
500 res.status = pk_decrypt(uid, arg->remotename, NULL,
501 &res.cryptkeyres_u.deskey);
503 if (res.status == KEY_SUCCESS) {
504 (void) fprintf(stderr, "%08x%08x\n",
505 res.cryptkeyres_u.deskey.key.high,
506 res.cryptkeyres_u.deskey.key.low);
508 (void) fprintf(stderr, "%s\n", strstatus(res.status));
510 (void) fflush(stderr);
517 key_gen_1_svc_prog(void *v, struct svc_req *s)
520 static des_block keygen;
521 static des_block key;
523 (void)gettimeofday(&time, NULL);
524 keygen.key.high += (time.tv_sec ^ time.tv_usec);
525 keygen.key.low += (time.tv_sec ^ time.tv_usec);
526 ecb_crypt((char *)&masterkey, (char *)&keygen, sizeof (keygen),
527 DES_ENCRYPT | DES_HW);
529 des_setparity((char *)&key);
531 (void) fprintf(stderr, "gen() = %08x%08x\n", key.key.high,
533 (void) fflush(stderr);
539 key_getcred_1_svc_prog(uid_t uid, netnamestr *name)
541 static getcredres res;
542 static u_int gids[NGROUPS];
543 struct unixcred *cred;
545 cred = &res.getcredres_u.cred;
546 cred->gids.gids_val = gids;
547 if (!netname2user(*name, (uid_t *) &cred->uid, (gid_t *) &cred->gid,
548 (int *)&cred->gids.gids_len, (gid_t *)gids)) {
549 res.status = KEY_UNKNOWN;
551 res.status = KEY_SUCCESS;
554 (void) fprintf(stderr, "getcred(%s) = ", *name);
555 if (res.status == KEY_SUCCESS) {
556 (void) fprintf(stderr, "uid=%d, gid=%d, grouplen=%d\n",
557 cred->uid, cred->gid, cred->gids.gids_len);
559 (void) fprintf(stderr, "%s\n", strstatus(res.status));
561 (void) fflush(stderr);
570 keyprogram(struct svc_req *rqstp, SVCXPRT *transp)
573 keybuf key_set_1_arg;
574 cryptkeyarg key_encrypt_1_arg;
575 cryptkeyarg key_decrypt_1_arg;
576 netnamestr key_getcred_1_arg;
577 cryptkeyarg key_encrypt_2_arg;
578 cryptkeyarg key_decrypt_2_arg;
579 netnamestr key_getcred_2_arg;
580 cryptkeyarg2 key_encrypt_pk_2_arg;
581 cryptkeyarg2 key_decrypt_pk_2_arg;
582 key_netstarg key_net_put_2_arg;
583 netobj key_get_conv_2_arg;
586 xdrproc_t xdr_argument, xdr_result;
587 typedef void *(svc_cb)(uid_t uid, void *arg);
592 switch (rqstp->rq_proc) {
594 svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
598 xdr_argument = (xdrproc_t)xdr_keybuf;
599 xdr_result = (xdrproc_t)xdr_int;
600 local = (svc_cb *)key_set_1_svc_prog;
605 xdr_argument = (xdrproc_t)xdr_cryptkeyarg;
606 xdr_result = (xdrproc_t)xdr_cryptkeyres;
607 local = (svc_cb *)key_encrypt_1_svc_prog;
612 xdr_argument = (xdrproc_t)xdr_cryptkeyarg;
613 xdr_result = (xdrproc_t)xdr_cryptkeyres;
614 local = (svc_cb *)key_decrypt_1_svc_prog;
619 xdr_argument = (xdrproc_t)xdr_void;
620 xdr_result = (xdrproc_t)xdr_des_block;
621 local = (svc_cb *)key_gen_1_svc_prog;
626 xdr_argument = (xdrproc_t)xdr_netnamestr;
627 xdr_result = (xdrproc_t)xdr_getcredres;
628 local = (svc_cb *)key_getcred_1_svc_prog;
633 xdr_argument = (xdrproc_t)xdr_cryptkeyarg2;
634 xdr_result = (xdrproc_t)xdr_cryptkeyres;
635 local = (svc_cb *)key_encrypt_pk_2_svc_prog;
640 xdr_argument = (xdrproc_t)xdr_cryptkeyarg2;
641 xdr_result = (xdrproc_t)xdr_cryptkeyres;
642 local = (svc_cb *)key_decrypt_pk_2_svc_prog;
648 xdr_argument = (xdrproc_t)xdr_key_netstarg;
649 xdr_result = (xdrproc_t)xdr_keystatus;
650 local = (svc_cb *)key_net_put_2_svc_prog;
655 xdr_argument = (xdrproc_t) xdr_void;
656 xdr_result = (xdrproc_t)xdr_key_netstres;
657 local = (svc_cb *)key_net_get_2_svc_prog;
662 xdr_argument = (xdrproc_t) xdr_keybuf;
663 xdr_result = (xdrproc_t)xdr_cryptkeyres;
664 local = (svc_cb *)key_get_conv_2_svc_prog;
669 svcerr_noproc(transp);
673 if (root_auth(transp, rqstp) == 0) {
675 (void) fprintf(stderr,
676 "not local privileged process\n");
678 svcerr_weakauth(transp);
681 if (rqstp->rq_cred.oa_flavor != AUTH_SYS) {
683 (void) fprintf(stderr,
684 "not unix authentication\n");
686 svcerr_weakauth(transp);
689 uid = ((struct authsys_parms *)rqstp->rq_clntcred)->aup_uid;
692 memset(&argument, 0, sizeof (argument));
693 if (!svc_getargs(transp, xdr_argument, &argument)) {
694 svcerr_decode(transp);
697 result = (*local) (uid, &argument);
698 if (!svc_sendreply(transp, xdr_result, result)) {
700 (void) fprintf(stderr, "unable to reply\n");
701 svcerr_systemerr(transp);
703 if (!svc_freeargs(transp, xdr_argument, &argument)) {
705 (void) fprintf(stderr,
706 "unable to free arguments\n");
713 root_auth(SVCXPRT *trans, struct svc_req *rqstp)
716 struct sockaddr *remote;
718 remote = svc_getrpccaller(trans)->buf;
719 if (remote->sa_family != AF_UNIX) {
721 fprintf(stderr, "client didn't use AF_UNIX\n");
725 if (__rpc_get_local_uid(trans, &uid) < 0) {
727 fprintf(stderr, "__rpc_get_local_uid failed\n");
732 fprintf(stderr, "local_uid %u\n", uid);
735 if (rqstp->rq_cred.oa_flavor == AUTH_SYS) {
736 if (((uid_t) ((struct authunix_parms *)
737 rqstp->rq_clntcred)->aup_uid)
743 "local_uid %u mismatches auth %u\n", uid,
744 ((uid_t) ((struct authunix_parms *)rqstp->rq_clntcred)->aup_uid));
749 fprintf(stderr, "Not auth sys\n");
757 (void) fprintf(stderr,
758 "usage: keyserv [-n] [-D] [-d] [-v] [-p path]\n");
759 (void) fprintf(stderr, "-d disables the use of default keys\n");