auth_unix.c 7.9 KB

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