home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp-sdk / src / rpclib / auth_unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  7.1 KB  |  303 lines

  1. /*
  2.  *      $Id: auth_unix.c,v 4.2 1994/09/29 23:48:50 jraja Exp $
  3.  *
  4.  *      Implements UNIX style authentication parameters. 
  5.  *       
  6.  *      Copyright © 1994 AmiTCP/IP Group,
  7.  *                       Network Solutions Development Inc.
  8.  *                       All rights reserved. 
  9.  */
  10.  
  11. /* @(#)auth_unix.c    2.2 88/08/01 4.0 RPCSRC */
  12. #if !defined(lint) && defined(SCCSIDS)
  13. static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
  14. #endif
  15.  
  16. /*
  17.  * Copyright (C) 1984, Sun Microsystems, Inc.
  18.  *
  19.  * The system is very weak.  The client uses no encryption for it's
  20.  * credentials and only sends null verifiers.  The server sends backs
  21.  * null verifiers or optionally a verifier that suggests a new short hand
  22.  * for the credentials.
  23.  *
  24.  */
  25.  
  26. #include <sys/param.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <rpc/types.h>
  30. #include <rpc/xdr.h>
  31. #include <rpc/auth.h>
  32. #include <rpc/auth_unix.h>
  33.  
  34. #ifdef AMITCP
  35. #include <bsdsocket.h>
  36. #endif
  37.  
  38. /*
  39.  * Unix authenticator operations vector
  40.  */
  41. static void    authunix_nextverf(AUTH * auth);
  42. static bool_t    authunix_marshal(AUTH * auth, XDR * xdrs);
  43. static bool_t    authunix_validate(AUTH * auth, struct opaque_auth * verfp);
  44. static bool_t    authunix_refresh(AUTH * auth);
  45. static void    authunix_destroy(AUTH * auth);
  46.  
  47. static struct auth_ops auth_unix_ops = {
  48.     authunix_nextverf,
  49.     authunix_marshal,
  50.     authunix_validate,
  51.     authunix_refresh,
  52.     authunix_destroy
  53. };
  54.  
  55. /*
  56.  * This struct is pointed to by the ah_private field of an auth_handle.
  57.  */
  58. struct audata {
  59.     struct opaque_auth    au_origcred;    /* original credentials */
  60.     struct opaque_auth    au_shcred;    /* short hand cred */
  61.     u_long            au_shfaults;    /* short hand cache faults */
  62.     char            au_marshed[MAX_AUTH_BYTES];
  63.     u_int            au_mpos;    /* xdr pos at end of marshed */
  64. };
  65. #define    AUTH_PRIVATE(auth)    ((struct audata *)auth->ah_private)
  66.  
  67. static void marshal_new_auth(AUTH *);
  68.  
  69.  
  70. /*
  71.  * Create a unix style authenticator.
  72.  * Returns an auth handle with the given stuff in it.
  73.  */
  74. AUTH *
  75. authunix_create(machname, uid, gid, len, aup_gids)
  76.     char *machname;
  77.     uid_t uid;
  78.     gid_t gid;
  79.     register int len;
  80.     gid_t *aup_gids;
  81. {
  82.     struct authunix_parms aup;
  83.     char mymem[MAX_AUTH_BYTES];
  84.     struct timeval now;
  85.     XDR xdrs;
  86.     register AUTH *auth;
  87.     register struct audata *au;
  88.  
  89.     /*
  90.      * Allocate and set up auth handle
  91.      */
  92.     auth = (AUTH *)mem_alloc(sizeof(*auth));
  93. #ifndef KERNEL
  94.     if (auth == NULL) {
  95.         (void)fprintf(stderr, "authunix_create: out of memory\n");
  96.         return (NULL);
  97.     }
  98. #endif
  99.     au = (struct audata *)mem_alloc(sizeof(*au));
  100. #ifndef KERNEL
  101.     if (au == NULL) {
  102.         (void)fprintf(stderr, "authunix_create: out of memory\n");
  103.         return (NULL);
  104.     }
  105. #endif
  106.     auth->ah_ops = &auth_unix_ops;
  107.     auth->ah_private = (caddr_t)au;
  108.     auth->ah_verf = au->au_shcred = _null_auth;
  109.     au->au_shfaults = 0;
  110.  
  111.     /*
  112.      * fill in param struct from the given params
  113.      */
  114.     (void)gettimeofday(&now,  (struct timezone *)0);
  115.     aup.aup_time = now.tv_sec;
  116.     aup.aup_machname = machname;
  117.     aup.aup_uid = uid;
  118.     aup.aup_gid = gid;
  119.     aup.aup_len = (u_int)len;
  120.     aup.aup_gids = aup_gids;
  121.  
  122.     /*
  123.      * Serialize the parameters into origcred
  124.      */
  125.     xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
  126.     if (! xdr_authunix_parms(&xdrs, &aup)) 
  127.         return (NULL); /* was exit(3) */
  128.     au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
  129.     au->au_origcred.oa_flavor = AUTH_UNIX;
  130. #ifdef KERNEL
  131.     au->au_origcred.oa_base = mem_alloc((u_int) len);
  132. #else
  133.     if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
  134.         (void)fprintf(stderr, "authunix_create: out of memory\n");
  135.         return (NULL);
  136.     }
  137. #endif
  138.     bcopy(mymem, au->au_origcred.oa_base, (u_int)len);
  139.  
  140.     /*
  141.      * set auth handle to reflect new cred.
  142.      */
  143.     auth->ah_cred = au->au_origcred;
  144.     marshal_new_auth(auth);
  145.     return (auth);
  146. }
  147.  
  148. /*
  149.  * Returns an auth handle with parameters determined by doing lots of
  150.  * syscalls.
  151.  */
  152. AUTH *
  153. authunix_create_default(void)
  154. {
  155.     register int len;
  156.     char machname[MAX_MACHINE_NAME + 1];
  157.     register uid_t uid;
  158.     register gid_t gid;
  159.     gid_t gids[NGRPS];
  160.  
  161.     if (gethostname(machname, MAX_MACHINE_NAME) == -1)
  162.         return (NULL); /* was exit(3) */
  163.     machname[MAX_MACHINE_NAME] = 0;
  164.     uid = geteuid();
  165.     gid = getegid();
  166.     if ((len = getgroups(NGRPS, gids)) < 0)
  167.         return (NULL); /* was abort() */
  168.     return (authunix_create(machname, uid, gid, len, gids));
  169. }
  170.  
  171. /*
  172.  * authunix operations
  173.  */
  174.  
  175. static void
  176. authunix_nextverf(auth)
  177.     AUTH *auth;
  178. {
  179.     /* no action necessary */
  180. }
  181.  
  182. static bool_t
  183. authunix_marshal(auth, xdrs)
  184.     AUTH *auth;
  185.     XDR *xdrs;
  186. {
  187.     register struct audata *au = AUTH_PRIVATE(auth);
  188.  
  189.     return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
  190. }
  191.  
  192. static bool_t
  193. authunix_validate(AUTH *auth, struct opaque_auth *verfp)
  194. {
  195.     register struct audata *au;
  196.     XDR xdrs;
  197.  
  198.     if (verfp->oa_flavor == AUTH_SHORT) {
  199.         au = AUTH_PRIVATE(auth);
  200.         xdrmem_create(&xdrs, verfp->oa_base, verfp->oa_length, XDR_DECODE);
  201.  
  202.         if (au->au_shcred.oa_base != NULL) {
  203.             mem_free(au->au_shcred.oa_base,
  204.                 au->au_shcred.oa_length);
  205.             au->au_shcred.oa_base = NULL;
  206.         }
  207.         if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
  208.             auth->ah_cred = au->au_shcred;
  209.         } else {
  210.             xdrs.x_op = XDR_FREE;
  211.             (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
  212.             au->au_shcred.oa_base = NULL;
  213.             auth->ah_cred = au->au_origcred;
  214.         }
  215.         marshal_new_auth(auth);
  216.     }
  217.     return (TRUE);
  218. }
  219.  
  220. static bool_t
  221. authunix_refresh(auth)
  222.     register AUTH *auth;
  223. {
  224.     register struct audata *au = AUTH_PRIVATE(auth);
  225.     struct authunix_parms aup;
  226.     struct timeval now;
  227.     XDR xdrs;
  228.     register int stat;
  229.  
  230.     if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
  231.         /* there is no hope.  Punt */
  232.         return (FALSE);
  233.     }
  234.     au->au_shfaults ++;
  235.  
  236.     /* first deserialize the creds back into a struct authunix_parms */
  237.     aup.aup_machname = NULL;
  238.     aup.aup_gids = (gid_t *)NULL;
  239.     xdrmem_create(&xdrs, au->au_origcred.oa_base,
  240.         au->au_origcred.oa_length, XDR_DECODE);
  241.     stat = xdr_authunix_parms(&xdrs, &aup);
  242.     if (! stat) 
  243.         goto done;
  244.  
  245.     /* update the time and serialize in place */
  246.     (void)gettimeofday(&now, (struct timezone *)0);
  247.     aup.aup_time = now.tv_sec;
  248.     xdrs.x_op = XDR_ENCODE;
  249.     XDR_SETPOS(&xdrs, 0);
  250.     stat = xdr_authunix_parms(&xdrs, &aup);
  251.     if (! stat)
  252.         goto done;
  253.     auth->ah_cred = au->au_origcred;
  254.     marshal_new_auth(auth);
  255. done:
  256.     /* free the struct authunix_parms created by deserializing */
  257.     xdrs.x_op = XDR_FREE;
  258.     (void)xdr_authunix_parms(&xdrs, &aup);
  259.     XDR_DESTROY(&xdrs);
  260.     return (stat);
  261. }
  262.  
  263. static void
  264. authunix_destroy(auth)
  265.     register AUTH *auth;
  266. {
  267.     register struct audata *au = AUTH_PRIVATE(auth);
  268.  
  269.     mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
  270.  
  271.     if (au->au_shcred.oa_base != NULL)
  272.         mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
  273.  
  274.     mem_free(auth->ah_private, sizeof(struct audata));
  275.  
  276.     if (auth->ah_verf.oa_base != NULL)
  277.         mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
  278.  
  279.     mem_free((caddr_t)auth, sizeof(*auth));
  280. }
  281.  
  282. /*
  283.  * Marshals (pre-serializes) an auth struct.
  284.  * sets private data, au_marshed and au_mpos
  285.  */
  286. static void
  287. marshal_new_auth(auth)
  288.     register AUTH *auth;
  289. {
  290.     XDR        xdr_stream;
  291.     register XDR    *xdrs = &xdr_stream;
  292.     register struct audata *au = AUTH_PRIVATE(auth);
  293.  
  294.     xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
  295.     if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
  296.         (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
  297.         perror("auth_none.c - Fatal marshalling problem");
  298.     } else {
  299.         au->au_mpos = XDR_GETPOS(xdrs);
  300.     }
  301.     XDR_DESTROY(xdrs);
  302. }
  303.