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 / auth_unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-01  |  9.0 KB  |  376 lines

  1. /* @(#)auth_unix.c    2.2 88/08/01 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  *
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /*
  35.  * auth_unix.c, Implements UNIX style authentication parameters. 
  36.  *  
  37.  * Copyright (C) 1984, Sun Microsystems, Inc.
  38.  *
  39.  * The system is very weak.  The client uses no encryption for it's
  40.  * credentials and only sends null verifiers.  The server sends backs
  41.  * null verifiers or optionally a verifier that suggests a new short hand
  42.  * for the credentials.
  43.  *
  44.  */
  45.  
  46. #include <stdio.h>
  47.  
  48. #include <rpc/types.h>
  49. #include <rpc/xdr.h>
  50. #include <rpc/auth.h>
  51. #include <rpc/auth_unix.h>
  52.  
  53. #ifdef __STDC__
  54. #include <stdlib.h>
  55. #endif
  56.  
  57. #include <unistd.h>
  58. #include <netdb.h>
  59. #include <strings.h>
  60.  
  61. #ifdef _POSIX_SOURCE
  62. #include <limits.h>
  63. #endif
  64.  
  65. #if NLS
  66. #include "nl_types.h"
  67. #endif
  68.  
  69. /*
  70.  * Unix authenticator operations vector
  71.  */
  72. static void    authunix_nextverf();
  73. static bool_t    authunix_marshal();
  74. static bool_t    authunix_validate();
  75. static bool_t    authunix_refresh();
  76. static void    authunix_destroy();
  77.  
  78. static struct auth_ops auth_unix_ops = {
  79.     authunix_nextverf,
  80.     authunix_marshal,
  81.     authunix_validate,
  82.     authunix_refresh,
  83.     authunix_destroy
  84. };
  85.  
  86. /*
  87.  * This struct is pointed to by the ah_private field of an auth_handle.
  88.  */
  89. struct audata {
  90.     struct opaque_auth    au_origcred;    /* original credentials */
  91.     struct opaque_auth    au_shcred;    /* short hand cred */
  92.     u_long            au_shfaults;    /* short hand cache faults */
  93.     char            au_marshed[MAX_AUTH_BYTES];
  94.     u_int            au_mpos;    /* xdr pos at end of marshed */
  95. };
  96. #define    AUTH_PRIVATE(auth)    ((struct audata *)auth->ah_private)
  97.  
  98. static void marshal_new_auth();
  99.  
  100.  
  101. /*
  102.  * Create a unix style authenticator.
  103.  * Returns an auth handle with the given stuff in it.
  104.  */
  105. AUTH *
  106. authunix_create(machname, uid, gid, len, aup_gids)
  107.     char *machname;
  108.     uid_t uid;
  109.     gid_t gid;
  110.     register int len;
  111.     gid_t *aup_gids;
  112. {
  113.     struct authunix_parms aup;
  114.     char mymem[MAX_AUTH_BYTES];
  115.     struct timeval now;
  116.     XDR xdrs;
  117.     register AUTH *auth;
  118.     register struct audata *au;
  119.  
  120. #if NLS
  121.     libc_nls_init();
  122. #endif
  123.     /*
  124.      * Allocate and set up auth handle
  125.      */
  126.     auth = (AUTH *)mem_alloc(sizeof(*auth));
  127. #ifndef KERNEL
  128.     if (auth == NULL) {
  129. #if NLS
  130.         (void)fprintf(stderr, "authunix_create: %s\n",
  131.                   catgets(_libc_cat, RpcMiscSet,
  132.                       RpcMiscOutOfMemory, "out of memory"));
  133. #else
  134.         (void)fprintf(stderr, "authunix_create: out of memory\n");
  135. #endif
  136.         return (NULL);
  137.     }
  138. #endif
  139.     au = (struct audata *)mem_alloc(sizeof(*au));
  140. #ifndef KERNEL
  141.     if (au == NULL) {
  142. #if NLS
  143.         (void)fprintf(stderr, "authunix_create: %s\n",
  144.                   catgets(_libc_cat, RpcMiscSet,
  145.                       RpcMiscOutOfMemory, "out of memory"));
  146. #else
  147.         (void)fprintf(stderr, "authunix_create: out of memory\n");
  148. #endif
  149.         return (NULL);
  150.     }
  151. #endif
  152.     auth->ah_ops = &auth_unix_ops;
  153.     auth->ah_private = (caddr_t)au;
  154.     auth->ah_verf = au->au_shcred = _null_auth;
  155.     au->au_shfaults = 0;
  156.  
  157.     /*
  158.      * fill in param struct from the given params
  159.      */
  160.     (void)gettimeofday(&now,  (struct timezone *)0);
  161.     aup.aup_time = now.tv_sec;
  162.     aup.aup_machname = machname;
  163.     aup.aup_uid = uid;
  164.     aup.aup_gid = gid;
  165.     aup.aup_len = (u_int)len;
  166.     aup.aup_gids = aup_gids;
  167.  
  168.     /*
  169.      * Serialize the parameters into origcred
  170.      */
  171.     xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
  172.     if (! xdr_authunix_parms(&xdrs, &aup)) 
  173.         abort();
  174.     au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
  175.     au->au_origcred.oa_flavor = AUTH_UNIX;
  176. #ifdef KERNEL
  177.     au->au_origcred.oa_base = mem_alloc((u_int) len);
  178. #else
  179.     if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
  180.         (void)fprintf(stderr, "authunix_create: out of memory\n");
  181.         return (NULL);
  182.     }
  183. #endif
  184.     bcopy(mymem, au->au_origcred.oa_base, (u_int)len);
  185.  
  186.     /*
  187.      * set auth handle to reflect new cred.
  188.      */
  189.     auth->ah_cred = au->au_origcred;
  190.     marshal_new_auth(auth);
  191.     return (auth);
  192. }
  193.  
  194. /*
  195.  * Returns an auth handle with parameters determined by doing lots of
  196.  * syscalls.
  197.  */
  198. AUTH *
  199. authunix_create_default()
  200. {
  201.     register int len;
  202.     char machname[MAX_MACHINE_NAME + 1];
  203.     register int uid;
  204.     register int gid;
  205. #ifdef _POSIX_SOURCE
  206.     gid_t *gids;        /* NGROUPS_MAX may not be a constant */
  207. #else
  208.     int gids[NGROUPS];
  209. #endif
  210.     AUTH *result;
  211.  
  212.     if (gethostname(machname, MAX_MACHINE_NAME) == -1)
  213.         abort();
  214.     machname[MAX_MACHINE_NAME] = 0;
  215.     uid = geteuid();
  216.     gid = getegid();
  217. #ifdef _POSIX_SOURCE
  218.     gids = (gid_t *) mem_alloc(sizeof(gid_t)*NGROUPS_MAX);
  219.     if ((len = getgroups(NGROUPS_MAX, gids)) < 0)
  220.         abort();
  221. #else
  222.     if ((len = getgroups(NGROUPS, gids)) < 0)
  223.         abort();
  224. #endif
  225.     if (len > NGRPS)
  226.         len = NGRPS;    /* quietly truncate to RPC max size */
  227.     result = authunix_create(machname, uid, gid, len, gids);
  228. #ifdef _POSIX_SOURCE
  229.     mem_free(gids, sizeof(gid_t)*NGROUPS_MAX);
  230. #endif
  231.     return result;
  232. }
  233.  
  234. /*
  235.  * authunix operations
  236.  */
  237.  
  238. static void
  239. authunix_nextverf(auth)
  240.     AUTH *auth;
  241. {
  242.     /* no action necessary */
  243. }
  244.  
  245. static bool_t
  246. authunix_marshal(auth, xdrs)
  247.     AUTH *auth;
  248.     XDR *xdrs;
  249. {
  250.     register struct audata *au = AUTH_PRIVATE(auth);
  251.  
  252.     return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
  253. }
  254.  
  255. static bool_t
  256. authunix_validate(auth, verf)
  257.     register AUTH *auth;
  258.     struct opaque_auth verf;
  259. {
  260.     register struct audata *au;
  261.     XDR xdrs;
  262.  
  263.     if (verf.oa_flavor == AUTH_SHORT) {
  264.         au = AUTH_PRIVATE(auth);
  265.         xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
  266.  
  267.         if (au->au_shcred.oa_base != NULL) {
  268.             mem_free(au->au_shcred.oa_base,
  269.                 au->au_shcred.oa_length);
  270.             au->au_shcred.oa_base = NULL;
  271.         }
  272.         if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
  273.             auth->ah_cred = au->au_shcred;
  274.         } else {
  275.             xdrs.x_op = XDR_FREE;
  276.             (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
  277.             au->au_shcred.oa_base = NULL;
  278.             auth->ah_cred = au->au_origcred;
  279.         }
  280.         marshal_new_auth(auth);
  281.     }
  282.     return (TRUE);
  283. }
  284.  
  285. static bool_t
  286. authunix_refresh(auth)
  287.     register AUTH *auth;
  288. {
  289.     register struct audata *au = AUTH_PRIVATE(auth);
  290.     struct authunix_parms aup;
  291.     struct timeval now;
  292.     XDR xdrs;
  293.     register int stat;
  294.  
  295.     if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
  296.         /* there is no hope.  Punt */
  297.         return (FALSE);
  298.     }
  299.     au->au_shfaults ++;
  300.  
  301.     /* first deserialize the creds back into a struct authunix_parms */
  302.     aup.aup_machname = NULL;
  303.     aup.aup_gids = NULL;
  304.     xdrmem_create(&xdrs, au->au_origcred.oa_base,
  305.         au->au_origcred.oa_length, XDR_DECODE);
  306.     stat = xdr_authunix_parms(&xdrs, &aup);
  307.     if (! stat) 
  308.         goto done;
  309.  
  310.     /* update the time and serialize in place */
  311.     (void)gettimeofday(&now, (struct timezone *)0);
  312.     aup.aup_time = now.tv_sec;
  313.     xdrs.x_op = XDR_ENCODE;
  314.     XDR_SETPOS(&xdrs, 0);
  315.     stat = xdr_authunix_parms(&xdrs, &aup);
  316.     if (! stat)
  317.         goto done;
  318.     auth->ah_cred = au->au_origcred;
  319.     marshal_new_auth(auth);
  320. done:
  321.     /* free the struct authunix_parms created by deserializing */
  322.     xdrs.x_op = XDR_FREE;
  323.     (void)xdr_authunix_parms(&xdrs, &aup);
  324.     XDR_DESTROY(&xdrs);
  325.     return (stat);
  326. }
  327.  
  328. static void
  329. authunix_destroy(auth)
  330.     register AUTH *auth;
  331. {
  332.     register struct audata *au = AUTH_PRIVATE(auth);
  333.  
  334.     mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
  335.  
  336.     if (au->au_shcred.oa_base != NULL)
  337.         mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
  338.  
  339.     mem_free(auth->ah_private, sizeof(struct audata));
  340.  
  341.     if (auth->ah_verf.oa_base != NULL)
  342.         mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
  343.  
  344.     mem_free((caddr_t)auth, sizeof(*auth));
  345. }
  346.  
  347. /*
  348.  * Marshals (pre-serializes) an auth struct.
  349.  * sets private data, au_marshed and au_mpos
  350.  */
  351. static void
  352. marshal_new_auth(auth)
  353.     register AUTH *auth;
  354. {
  355.     XDR        xdr_stream;
  356.     register XDR    *xdrs = &xdr_stream;
  357.     register struct audata *au = AUTH_PRIVATE(auth);
  358.  
  359. #if NLS
  360.     libc_nls_init();
  361. #endif
  362.     xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
  363.     if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
  364.         (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
  365. #if NLS
  366.         perror(catgets(_libc_cat, RpcMiscSet, RpcMiscFatalMarshall,
  367.                 "auth_none.c - Fatal marshalling problem"));
  368. #else
  369.         perror("auth_none.c - Fatal marshalling problem");
  370. #endif
  371.     } else {
  372.         au->au_mpos = XDR_GETPOS(xdrs);
  373.     }
  374.     XDR_DESTROY(xdrs);
  375. }
  376.