auth_unix.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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(machname, uid, gid, len, aup_gids)
  85. char *machname;
  86. int uid;
  87. int gid;
  88. register int len;
  89. int *aup_gids;
  90. {
  91. struct authunix_parms aup;
  92. char mymem[MAX_AUTH_BYTES];
  93. struct timeval now;
  94. XDR xdrs;
  95. register AUTH *auth;
  96. register struct audata *au;
  97. /*
  98. * Allocate and set up auth handle
  99. */
  100. auth = (AUTH *) mem_alloc(sizeof(*auth));
  101. #ifndef KERNEL
  102. if (auth == NULL) {
  103. (void) fprintf(stderr, "authunix_create: out of memory\n");
  104. return (NULL);
  105. }
  106. #endif
  107. au = (struct audata *) mem_alloc(sizeof(*au));
  108. #ifndef KERNEL
  109. if (au == NULL) {
  110. (void) fprintf(stderr, "authunix_create: out of memory\n");
  111. return (NULL);
  112. }
  113. #endif
  114. auth->ah_ops = &auth_unix_ops;
  115. auth->ah_private = (caddr_t) au;
  116. auth->ah_verf = au->au_shcred = _null_auth;
  117. au->au_shfaults = 0;
  118. /*
  119. * fill in param struct from the given params
  120. */
  121. (void) gettimeofday(&now, (struct timezone *) 0);
  122. aup.aup_time = now.tv_sec;
  123. aup.aup_machname = machname;
  124. aup.aup_uid = uid;
  125. aup.aup_gid = gid;
  126. aup.aup_len = (u_int) len;
  127. aup.aup_gids = aup_gids;
  128. /*
  129. * Serialize the parameters into origcred
  130. */
  131. xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
  132. if (!xdr_authunix_parms(&xdrs, &aup))
  133. abort();
  134. au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
  135. au->au_origcred.oa_flavor = AUTH_UNIX;
  136. #ifdef KERNEL
  137. au->au_origcred.oa_base = mem_alloc((u_int) len);
  138. #else
  139. if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
  140. (void) fprintf(stderr, "authunix_create: out of memory\n");
  141. return (NULL);
  142. }
  143. #endif
  144. bcopy(mymem, au->au_origcred.oa_base, (u_int) len);
  145. /*
  146. * set auth handle to reflect new cred.
  147. */
  148. auth->ah_cred = au->au_origcred;
  149. marshal_new_auth(auth);
  150. return (auth);
  151. }
  152. /*
  153. * Returns an auth handle with parameters determined by doing lots of
  154. * syscalls.
  155. */
  156. AUTH *authunix_create_default()
  157. {
  158. register int len;
  159. char machname[MAX_MACHINE_NAME + 1];
  160. register int uid;
  161. register int gid;
  162. int gids[NGRPS];
  163. if (gethostname(machname, MAX_MACHINE_NAME) == -1)
  164. abort();
  165. machname[MAX_MACHINE_NAME] = 0;
  166. uid = geteuid();
  167. gid = getegid();
  168. if ((len = getgroups(NGRPS, gids)) < 0)
  169. abort();
  170. return (authunix_create(machname, uid, gid, len, gids));
  171. }
  172. /*
  173. * authunix operations
  174. */
  175. static void authunix_nextverf(auth)
  176. AUTH *auth;
  177. {
  178. /* no action necessary */
  179. }
  180. static bool_t 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 authunix_validate(auth, verf)
  188. register AUTH *auth;
  189. struct opaque_auth verf;
  190. {
  191. register struct audata *au;
  192. XDR xdrs;
  193. if (verf.oa_flavor == AUTH_SHORT) {
  194. au = AUTH_PRIVATE(auth);
  195. xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
  196. if (au->au_shcred.oa_base != NULL) {
  197. mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
  198. au->au_shcred.oa_base = NULL;
  199. }
  200. if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
  201. auth->ah_cred = au->au_shcred;
  202. } else {
  203. xdrs.x_op = XDR_FREE;
  204. (void) xdr_opaque_auth(&xdrs, &au->au_shcred);
  205. au->au_shcred.oa_base = NULL;
  206. auth->ah_cred = au->au_origcred;
  207. }
  208. marshal_new_auth(auth);
  209. }
  210. return (TRUE);
  211. }
  212. static bool_t authunix_refresh(auth)
  213. register AUTH *auth;
  214. {
  215. register struct audata *au = AUTH_PRIVATE(auth);
  216. struct authunix_parms aup;
  217. struct timeval now;
  218. XDR xdrs;
  219. register int stat;
  220. if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
  221. /* there is no hope. Punt */
  222. return (FALSE);
  223. }
  224. au->au_shfaults++;
  225. /* first deserialize the creds back into a struct authunix_parms */
  226. aup.aup_machname = NULL;
  227. aup.aup_gids = (int *) NULL;
  228. xdrmem_create(&xdrs, au->au_origcred.oa_base,
  229. au->au_origcred.oa_length, XDR_DECODE);
  230. stat = xdr_authunix_parms(&xdrs, &aup);
  231. if (!stat)
  232. goto done;
  233. /* update the time and serialize in place */
  234. (void) gettimeofday(&now, (struct timezone *) 0);
  235. aup.aup_time = now.tv_sec;
  236. xdrs.x_op = XDR_ENCODE;
  237. XDR_SETPOS(&xdrs, 0);
  238. stat = xdr_authunix_parms(&xdrs, &aup);
  239. if (!stat)
  240. goto done;
  241. auth->ah_cred = au->au_origcred;
  242. marshal_new_auth(auth);
  243. done:
  244. /* free the struct authunix_parms created by deserializing */
  245. xdrs.x_op = XDR_FREE;
  246. (void) xdr_authunix_parms(&xdrs, &aup);
  247. XDR_DESTROY(&xdrs);
  248. return (stat);
  249. }
  250. static void authunix_destroy(auth)
  251. register AUTH *auth;
  252. {
  253. register struct audata *au = AUTH_PRIVATE(auth);
  254. mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
  255. if (au->au_shcred.oa_base != NULL)
  256. mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
  257. mem_free(auth->ah_private, sizeof(struct audata));
  258. if (auth->ah_verf.oa_base != NULL)
  259. mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
  260. mem_free((caddr_t) auth, sizeof(*auth));
  261. }
  262. /*
  263. * Marshals (pre-serializes) an auth struct.
  264. * sets private data, au_marshed and au_mpos
  265. */
  266. static bool_t marshal_new_auth(auth)
  267. register AUTH *auth;
  268. {
  269. XDR xdr_stream;
  270. register XDR *xdrs = &xdr_stream;
  271. register struct audata *au = AUTH_PRIVATE(auth);
  272. xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
  273. if ((!xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
  274. (!xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
  275. perror("auth_none.c - Fatal marshalling problem");
  276. } else {
  277. au->au_mpos = XDR_GETPOS(xdrs);
  278. }
  279. XDR_DESTROY(xdrs);
  280. }