auth_unix.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. * auth_unix.c, Implements UNIX style authentication parameters.
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. *
  38. * The system is very weak. The client uses no encryption for it's
  39. * credentials and only sends null verifiers. The server sends backs
  40. * null verifiers or optionally a verifier that suggests a new short hand
  41. * for the credentials.
  42. *
  43. */
  44. #include <stdio.h>
  45. #include <rpc/types.h>
  46. #include <rpc/xdr.h>
  47. #include <rpc/auth.h>
  48. #include <rpc/auth_unix.h>
  49. /*
  50. * Unix authenticator operations vector
  51. */
  52. static void authunix_nextverf();
  53. static bool_t authunix_marshal();
  54. static bool_t authunix_validate();
  55. static bool_t authunix_refresh();
  56. static void authunix_destroy();
  57. static struct auth_ops auth_unix_ops = {
  58. authunix_nextverf,
  59. authunix_marshal,
  60. authunix_validate,
  61. authunix_refresh,
  62. authunix_destroy
  63. };
  64. /*
  65. * This struct is pointed to by the ah_private field of an auth_handle.
  66. */
  67. struct audata {
  68. struct opaque_auth au_origcred; /* original credentials */
  69. struct opaque_auth au_shcred; /* short hand cred */
  70. u_long au_shfaults; /* short hand cache faults */
  71. char au_marshed[MAX_AUTH_BYTES];
  72. u_int au_mpos; /* xdr pos at end of marshed */
  73. };
  74. #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
  75. static bool_t marshal_new_auth();
  76. /*
  77. * Create a unix style authenticator.
  78. * Returns an auth handle with the given stuff in it.
  79. */
  80. AUTH *
  81. authunix_create(machname, uid, gid, len, aup_gids)
  82. char *machname;
  83. int uid;
  84. int gid;
  85. register int len;
  86. int *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 *
  154. authunix_create_default()
  155. {
  156. register int len;
  157. char machname[MAX_MACHINE_NAME + 1];
  158. register int uid;
  159. register int gid;
  160. int gids[NGRPS];
  161. if (gethostname(machname, MAX_MACHINE_NAME) == -1)
  162. abort();
  163. machname[MAX_MACHINE_NAME] = 0;
  164. uid = geteuid();
  165. gid = getegid();
  166. if ((len = getgroups(NGRPS, gids)) < 0)
  167. abort();
  168. return (authunix_create(machname, uid, gid, len, gids));
  169. }
  170. /*
  171. * authunix operations
  172. */
  173. static void
  174. authunix_nextverf(auth)
  175. AUTH *auth;
  176. {
  177. /* no action necessary */
  178. }
  179. static bool_t
  180. authunix_marshal(auth, xdrs)
  181. AUTH *auth;
  182. XDR *xdrs;
  183. {
  184. register struct audata *au = AUTH_PRIVATE(auth);
  185. return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
  186. }
  187. static bool_t
  188. authunix_validate(auth, verf)
  189. register AUTH *auth;
  190. struct opaque_auth verf;
  191. {
  192. register struct audata *au;
  193. XDR xdrs;
  194. if (verf.oa_flavor == AUTH_SHORT) {
  195. au = AUTH_PRIVATE(auth);
  196. xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
  197. if (au->au_shcred.oa_base != NULL) {
  198. mem_free(au->au_shcred.oa_base,
  199. au->au_shcred.oa_length);
  200. au->au_shcred.oa_base = NULL;
  201. }
  202. if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
  203. auth->ah_cred = au->au_shcred;
  204. } else {
  205. xdrs.x_op = XDR_FREE;
  206. (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
  207. au->au_shcred.oa_base = NULL;
  208. auth->ah_cred = au->au_origcred;
  209. }
  210. marshal_new_auth(auth);
  211. }
  212. return (TRUE);
  213. }
  214. static bool_t
  215. authunix_refresh(auth)
  216. register AUTH *auth;
  217. {
  218. register struct audata *au = AUTH_PRIVATE(auth);
  219. struct authunix_parms aup;
  220. struct timeval now;
  221. XDR xdrs;
  222. register int stat;
  223. if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
  224. /* there is no hope. Punt */
  225. return (FALSE);
  226. }
  227. au->au_shfaults ++;
  228. /* first deserialize the creds back into a struct authunix_parms */
  229. aup.aup_machname = NULL;
  230. aup.aup_gids = (int *)NULL;
  231. xdrmem_create(&xdrs, au->au_origcred.oa_base,
  232. au->au_origcred.oa_length, XDR_DECODE);
  233. stat = xdr_authunix_parms(&xdrs, &aup);
  234. if (! stat)
  235. goto done;
  236. /* update the time and serialize in place */
  237. (void)gettimeofday(&now, (struct timezone *)0);
  238. aup.aup_time = now.tv_sec;
  239. xdrs.x_op = XDR_ENCODE;
  240. XDR_SETPOS(&xdrs, 0);
  241. stat = xdr_authunix_parms(&xdrs, &aup);
  242. if (! stat)
  243. goto done;
  244. auth->ah_cred = au->au_origcred;
  245. marshal_new_auth(auth);
  246. done:
  247. /* free the struct authunix_parms created by deserializing */
  248. xdrs.x_op = XDR_FREE;
  249. (void)xdr_authunix_parms(&xdrs, &aup);
  250. XDR_DESTROY(&xdrs);
  251. return (stat);
  252. }
  253. static void
  254. authunix_destroy(auth)
  255. register AUTH *auth;
  256. {
  257. register struct audata *au = AUTH_PRIVATE(auth);
  258. mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
  259. if (au->au_shcred.oa_base != NULL)
  260. mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
  261. mem_free(auth->ah_private, sizeof(struct audata));
  262. if (auth->ah_verf.oa_base != NULL)
  263. mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
  264. mem_free((caddr_t)auth, sizeof(*auth));
  265. }
  266. /*
  267. * Marshals (pre-serializes) an auth struct.
  268. * sets private data, au_marshed and au_mpos
  269. */
  270. static bool_t
  271. marshal_new_auth(auth)
  272. register AUTH *auth;
  273. {
  274. XDR xdr_stream;
  275. register XDR *xdrs = &xdr_stream;
  276. register struct audata *au = AUTH_PRIVATE(auth);
  277. xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
  278. if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
  279. (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
  280. perror("auth_none.c - Fatal marshalling problem");
  281. } else {
  282. au->au_mpos = XDR_GETPOS(xdrs);
  283. }
  284. XDR_DESTROY(xdrs);
  285. }