auth_unix.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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[] =
  32. "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
  33. #endif
  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. #include <stdio.h>
  46. #include <unistd.h>
  47. #include <sys/types.h>
  48. #include <string.h>
  49. #include <rpc/types.h>
  50. #include <rpc/xdr.h>
  51. #include <rpc/auth.h>
  52. #include <rpc/auth_unix.h>
  53. /*
  54. * Unix authenticator operations vector
  55. */
  56. static void authunix_nextverf();
  57. static bool_t authunix_marshal();
  58. static bool_t authunix_validate();
  59. static bool_t authunix_refresh();
  60. static void authunix_destroy();
  61. static struct auth_ops auth_unix_ops = {
  62. authunix_nextverf,
  63. authunix_marshal,
  64. authunix_validate,
  65. authunix_refresh,
  66. authunix_destroy
  67. };
  68. /*
  69. * This struct is pointed to by the ah_private field of an auth_handle.
  70. */
  71. struct audata {
  72. struct opaque_auth au_origcred; /* original credentials */
  73. struct opaque_auth au_shcred; /* short hand cred */
  74. u_long au_shfaults; /* short hand cache faults */
  75. char au_marshed[MAX_AUTH_BYTES];
  76. u_int au_mpos; /* xdr pos at end of marshed */
  77. };
  78. #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
  79. static bool_t marshal_new_auth();
  80. /*
  81. * Create a unix style authenticator.
  82. * Returns an auth handle with the given stuff in it.
  83. */
  84. AUTH *authunix_create __P ((char *machname, uid_t uid,
  85. gid_t gid, int len,
  86. gid_t *aup_gids))
  87. {
  88. struct authunix_parms aup;
  89. char mymem[MAX_AUTH_BYTES];
  90. struct timeval now;
  91. XDR xdrs;
  92. register AUTH *auth;
  93. register struct audata *au;
  94. /*
  95. * Allocate and set up auth handle
  96. */
  97. auth = (AUTH *) mem_alloc(sizeof(*auth));
  98. #ifndef KERNEL
  99. if (auth == NULL) {
  100. (void) fprintf(stderr, "authunix_create: out of memory\n");
  101. return (NULL);
  102. }
  103. #endif
  104. au = (struct audata *) mem_alloc(sizeof(*au));
  105. #ifndef KERNEL
  106. if (au == NULL) {
  107. (void) fprintf(stderr, "authunix_create: out of memory\n");
  108. return (NULL);
  109. }
  110. #endif
  111. auth->ah_ops = &auth_unix_ops;
  112. auth->ah_private = (caddr_t) au;
  113. auth->ah_verf = au->au_shcred = _null_auth;
  114. au->au_shfaults = 0;
  115. /*
  116. * fill in param struct from the given params
  117. */
  118. (void) gettimeofday(&now, (struct timezone *) 0);
  119. aup.aup_time = now.tv_sec;
  120. aup.aup_machname = machname;
  121. aup.aup_uid = uid;
  122. aup.aup_gid = gid;
  123. aup.aup_len = (u_int) len;
  124. aup.aup_gids = aup_gids;
  125. /*
  126. * Serialize the parameters into origcred
  127. */
  128. xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
  129. if (!xdr_authunix_parms(&xdrs, &aup))
  130. abort();
  131. au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
  132. au->au_origcred.oa_flavor = AUTH_UNIX;
  133. #ifdef KERNEL
  134. au->au_origcred.oa_base = mem_alloc((u_int) len);
  135. #else
  136. if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
  137. (void) fprintf(stderr, "authunix_create: out of memory\n");
  138. return (NULL);
  139. }
  140. #endif
  141. bcopy(mymem, au->au_origcred.oa_base, (u_int) len);
  142. /*
  143. * set auth handle to reflect new cred.
  144. */
  145. auth->ah_cred = au->au_origcred;
  146. marshal_new_auth(auth);
  147. return (auth);
  148. }
  149. /*
  150. * Returns an auth handle with parameters determined by doing lots of
  151. * syscalls.
  152. */
  153. AUTH *authunix_create_default()
  154. {
  155. register int len;
  156. char machname[MAX_MACHINE_NAME + 1];
  157. register int uid;
  158. register int gid;
  159. int gids[NGRPS];
  160. if (gethostname(machname, MAX_MACHINE_NAME) == -1)
  161. abort();
  162. machname[MAX_MACHINE_NAME] = 0;
  163. uid = geteuid();
  164. gid = getegid();
  165. if ((len = getgroups(NGRPS, gids)) < 0)
  166. abort();
  167. return (authunix_create(machname, uid, gid, len, gids));
  168. }
  169. /*
  170. * authunix operations
  171. */
  172. static void authunix_nextverf(auth)
  173. AUTH *auth;
  174. {
  175. /* no action necessary */
  176. }
  177. static bool_t authunix_marshal(auth, xdrs)
  178. AUTH *auth;
  179. XDR *xdrs;
  180. {
  181. register struct audata *au = AUTH_PRIVATE(auth);
  182. return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
  183. }
  184. static bool_t authunix_validate(auth, verf)
  185. register AUTH *auth;
  186. struct opaque_auth verf;
  187. {
  188. register struct audata *au;
  189. XDR xdrs;
  190. if (verf.oa_flavor == AUTH_SHORT) {
  191. au = AUTH_PRIVATE(auth);
  192. xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
  193. if (au->au_shcred.oa_base != NULL) {
  194. mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
  195. au->au_shcred.oa_base = NULL;
  196. }
  197. if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
  198. auth->ah_cred = au->au_shcred;
  199. } else {
  200. xdrs.x_op = XDR_FREE;
  201. (void) xdr_opaque_auth(&xdrs, &au->au_shcred);
  202. au->au_shcred.oa_base = NULL;
  203. auth->ah_cred = au->au_origcred;
  204. }
  205. marshal_new_auth(auth);
  206. }
  207. return (TRUE);
  208. }
  209. static bool_t authunix_refresh(auth)
  210. register AUTH *auth;
  211. {
  212. register struct audata *au = AUTH_PRIVATE(auth);
  213. struct authunix_parms aup;
  214. struct timeval now;
  215. XDR xdrs;
  216. register int stat;
  217. if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
  218. /* there is no hope. Punt */
  219. return (FALSE);
  220. }
  221. au->au_shfaults++;
  222. /* first deserialize the creds back into a struct authunix_parms */
  223. aup.aup_machname = NULL;
  224. aup.aup_gids = (int *) NULL;
  225. xdrmem_create(&xdrs, au->au_origcred.oa_base,
  226. au->au_origcred.oa_length, XDR_DECODE);
  227. stat = xdr_authunix_parms(&xdrs, &aup);
  228. if (!stat)
  229. goto done;
  230. /* update the time and serialize in place */
  231. (void) gettimeofday(&now, (struct timezone *) 0);
  232. aup.aup_time = now.tv_sec;
  233. xdrs.x_op = XDR_ENCODE;
  234. XDR_SETPOS(&xdrs, 0);
  235. stat = xdr_authunix_parms(&xdrs, &aup);
  236. if (!stat)
  237. goto done;
  238. auth->ah_cred = au->au_origcred;
  239. marshal_new_auth(auth);
  240. done:
  241. /* free the struct authunix_parms created by deserializing */
  242. xdrs.x_op = XDR_FREE;
  243. (void) xdr_authunix_parms(&xdrs, &aup);
  244. XDR_DESTROY(&xdrs);
  245. return (stat);
  246. }
  247. static void authunix_destroy(auth)
  248. register AUTH *auth;
  249. {
  250. register struct audata *au = AUTH_PRIVATE(auth);
  251. mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
  252. if (au->au_shcred.oa_base != NULL)
  253. mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
  254. mem_free(auth->ah_private, sizeof(struct audata));
  255. if (auth->ah_verf.oa_base != NULL)
  256. mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
  257. mem_free((caddr_t) auth, sizeof(*auth));
  258. }
  259. /*
  260. * Marshals (pre-serializes) an auth struct.
  261. * sets private data, au_marshed and au_mpos
  262. */
  263. static bool_t marshal_new_auth(auth)
  264. register AUTH *auth;
  265. {
  266. XDR xdr_stream;
  267. register XDR *xdrs = &xdr_stream;
  268. register struct audata *au = AUTH_PRIVATE(auth);
  269. xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
  270. if ((!xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
  271. (!xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
  272. perror("auth_none.c - Fatal marshalling problem");
  273. } else {
  274. au->au_mpos = XDR_GETPOS(xdrs);
  275. }
  276. XDR_DESTROY(xdrs);
  277. }