home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / rpc / key_call.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-15  |  5.6 KB  |  232 lines

  1. #if defined(LIBC_SCCS) && !defined(lint)
  2. static char sccsid[] =     "@(#)key_call.c    2.2 88/08/15 4.0 RPCSRC; from 1.11 88/02/08 SMI";
  3. #endif
  4. /*
  5.  * Copyright (c) 1988 by Sun Microsystems, Inc.
  6.  */
  7. /*
  8.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  9.  * unrestricted use provided that this legend is included on all tape
  10.  * media and as a part of the software program in whole or part.  Users
  11.  * may copy or modify Sun RPC without charge, but are not authorized
  12.  * to license or distribute it to anyone else except as part of a product or
  13.  * program developed by the user.
  14.  * 
  15.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  16.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  17.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  18.  * 
  19.  * Sun RPC is provided with no support and without any obligation on the
  20.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  21.  * modification or enhancement.
  22.  * 
  23.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  24.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  25.  * OR ANY PART THEREOF.
  26.  * 
  27.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  28.  * or profits or other special, indirect and consequential damages, even if
  29.  * Sun has been advised of the possibility of such damages.
  30.  * 
  31.  * Sun Microsystems, Inc.
  32.  * 2550 Garcia Avenue
  33.  * Mountain View, California  94043
  34.  */
  35.  
  36. /*
  37.  * key_call.c, Interface to keyserver
  38.  *
  39.  * setsecretkey(key) - set your secret key
  40.  * encryptsessionkey(agent, deskey) - encrypt a session key to talk to agent
  41.  * decryptsessionkey(agent, deskey) - decrypt ditto
  42.  * gendeskey(deskey) - generate a secure des key
  43.  * netname2user(...) - get unix credential for given name (kernel only)
  44.  */
  45. #include <sys/param.h>
  46. #include <sys/socket.h>
  47. #include <rpc/rpc.h>
  48. #include <rpc/key_prot.h>
  49.  
  50. #define KEY_TIMEOUT    5    /* per-try timeout in seconds */
  51. #define KEY_NRETRY    12    /* number of retries */
  52.  
  53. #define debug(msg)        /* turn off debugging */
  54.  
  55. static struct timeval trytimeout = { KEY_TIMEOUT, 0 };
  56. static struct timeval tottimeout = { KEY_TIMEOUT * KEY_NRETRY, 0 };
  57.  
  58. static int key_call();
  59.  
  60. key_setsecret(secretkey)
  61.     char *secretkey;
  62. {
  63.     keystatus status;
  64.  
  65.     if (!key_call((u_long)KEY_SET, xdr_keybuf, secretkey, xdr_keystatus, 
  66.         (char*)&status)) 
  67.     {
  68.         return (-1);
  69.     }
  70.     if (status != KEY_SUCCESS) {
  71.         debug("set status is nonzero");
  72.         return (-1);
  73.     }
  74.     return (0);
  75. }
  76.  
  77.  
  78. key_encryptsession(remotename, deskey)
  79.     char *remotename;
  80.     des_block *deskey;
  81. {
  82.     cryptkeyarg arg;
  83.     cryptkeyres res;
  84.  
  85.     arg.remotename = remotename;
  86.     arg.deskey = *deskey;
  87.     if (!key_call((u_long)KEY_ENCRYPT, 
  88.         xdr_cryptkeyarg, (char *)&arg, xdr_cryptkeyres, (char *)&res)) 
  89.     {
  90.         return (-1);
  91.     }
  92.     if (res.status != KEY_SUCCESS) {
  93.         debug("encrypt status is nonzero");
  94.         return (-1);
  95.     }
  96.     *deskey = res.cryptkeyres_u.deskey;
  97.     return (0);
  98. }
  99.  
  100.  
  101. key_decryptsession(remotename, deskey)
  102.     char *remotename;
  103.     des_block *deskey;
  104. {
  105.     cryptkeyarg arg;
  106.     cryptkeyres res;
  107.  
  108.     arg.remotename = remotename;
  109.     arg.deskey = *deskey;
  110.     if (!key_call((u_long)KEY_DECRYPT, 
  111.         xdr_cryptkeyarg, (char *)&arg, xdr_cryptkeyres, (char *)&res))  
  112.     {
  113.         return (-1);
  114.     }
  115.     if (res.status != KEY_SUCCESS) {
  116.         debug("decrypt status is nonzero");
  117.         return (-1);
  118.     }
  119.     *deskey = res.cryptkeyres_u.deskey;
  120.     return (0);
  121. }
  122.  
  123. key_gendes(key)
  124.     des_block *key;
  125. {
  126.     struct sockaddr_in sin;
  127.     CLIENT *client;
  128.     int socket;
  129.     enum clnt_stat stat;
  130.  
  131.  
  132.     sin.sin_family = AF_INET;
  133.     sin.sin_port = 0;
  134.     sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  135.     bzero(sin.sin_zero, sizeof(sin.sin_zero));
  136.     socket = RPC_ANYSOCK;
  137.     client = clntudp_bufcreate(&sin, (u_long)KEY_PROG, (u_long)KEY_VERS,
  138.         trytimeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
  139.     if (client == NULL) {
  140.         return (-1);
  141.     }
  142.     stat = clnt_call(client, KEY_GEN, xdr_void, NULL,
  143.         xdr_des_block, key, tottimeout);
  144.     clnt_destroy(client);
  145.     (void) close(socket);
  146.     if (stat != RPC_SUCCESS) {
  147.         return (-1);
  148.     }
  149.     return (0);
  150. }
  151.  
  152.  
  153. #include <stdio.h>
  154. #include <signal.h>
  155. #include <sys/wait.h>
  156.  
  157.  
  158. static
  159. key_call(proc, xdr_arg, arg, xdr_rslt, rslt)
  160.     u_long proc;
  161.     bool_t (*xdr_arg)();
  162.     char *arg;
  163.     bool_t (*xdr_rslt)();
  164.     char *rslt;
  165. {
  166.     XDR xdrargs;
  167.     XDR xdrrslt;
  168.     FILE *fargs;
  169.     FILE *frslt;
  170.     int (*osigchild)();
  171.     union wait status;
  172.     int pid;
  173.     int success;
  174.     int ruid;
  175.     int euid;
  176.     static char MESSENGER[] = "/usr/etc/keyenvoy";
  177.  
  178.     success = 1;
  179.     osigchild = signal(SIGCHLD, SIG_IGN);
  180.  
  181.     /*
  182.      * We are going to exec a set-uid program which makes our effective uid
  183.      * zero, and authenticates us with our real uid. We need to make the 
  184.      * effective uid be the real uid for the setuid program, and 
  185.      * the real uid be the effective uid so that we can change things back.
  186.      */
  187.     euid = geteuid();
  188.     ruid = getuid();
  189.     (void) setreuid(euid, ruid);
  190.     pid = _openchild(MESSENGER, &fargs, &frslt);
  191.     (void) setreuid(ruid, euid);
  192.     if (pid < 0) {
  193.         debug("open_streams");
  194.         return (0);
  195.     }
  196.     xdrstdio_create(&xdrargs, fargs, XDR_ENCODE);
  197.     xdrstdio_create(&xdrrslt, frslt, XDR_DECODE);
  198.  
  199.     if (!xdr_u_long(&xdrargs, &proc) || !(*xdr_arg)(&xdrargs, arg)) {
  200.         debug("xdr args");
  201.         success = 0; 
  202.     }
  203.     (void) fclose(fargs);
  204.  
  205.     if (success && !(*xdr_rslt)(&xdrrslt, rslt)) {
  206.         debug("xdr rslt");
  207.         success = 0;
  208.     }
  209.  
  210. #ifdef NOTDEF
  211.     /*
  212.      * WARNING! XXX
  213.      * The original code appears first.  wait4 returns only after the process
  214.      * with the requested pid terminates.  The effect of using wait() instead
  215.      * has not been determined.
  216.      */
  217.     (void) fclose(frslt);
  218.     if (wait4(pid, &status, 0, NULL) < 0 || status.w_retcode != 0) {
  219.         debug("wait4");
  220.         success = 0;
  221.     }
  222. #endif /* def NOTDEF */
  223.     if (wait(&status) < 0 || status.w_retcode != 0) {
  224.         debug("wait");
  225.         success = 0;
  226.     }
  227.     (void)signal(SIGCHLD, osigchild);
  228.  
  229.     return (success);
  230. }
  231.  
  232.