auth_unix.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  3. * unrestricted use provided that this legend is included on all tape
  4. * media and as a part of the software program in whole or part. Users
  5. * may copy or modify Sun RPC without charge, but are not authorized
  6. * to license or distribute it to anyone else except as part of a product or
  7. * program developed by the user.
  8. *
  9. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  10. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  11. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  12. *
  13. * Sun RPC is provided with no support and without any obligation on the
  14. * part of Sun Microsystems, Inc. to assist in its use, correction,
  15. * modification or enhancement.
  16. *
  17. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  18. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  19. * OR ANY PART THEREOF.
  20. *
  21. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  22. * or profits or other special, indirect and consequential damages, even if
  23. * Sun has been advised of the possibility of such damages.
  24. *
  25. * Sun Microsystems, Inc.
  26. * 2550 Garcia Avenue
  27. * Mountain View, California 94043
  28. */
  29. /*
  30. * Copyright (C) 1984, Sun Microsystems, Inc.
  31. */
  32. /*
  33. * auth_unix.c, Implements UNIX style authentication parameters.
  34. *
  35. * The system is very weak. The client uses no encryption for it's
  36. * credentials and only sends null verifiers. The server sends backs
  37. * null verifiers or optionally a verifier that suggests a new short hand
  38. * for the credentials.
  39. */
  40. #include <limits.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. #include <libintl.h>
  45. #include <sys/param.h>
  46. #include <rpc/types.h>
  47. #include <rpc/xdr.h>
  48. #include <rpc/auth.h>
  49. #include <rpc/auth_unix.h>
  50. /*
  51. * Unix authenticator operations vector
  52. */
  53. static void authunix_nextverf (AUTH *);
  54. static bool_t authunix_marshal (AUTH *, XDR *);
  55. static bool_t authunix_validate (AUTH *, struct opaque_auth *);
  56. static bool_t authunix_refresh (AUTH *);
  57. static void authunix_destroy (AUTH *);
  58. static const struct auth_ops auth_unix_ops = {
  59. authunix_nextverf,
  60. authunix_marshal,
  61. authunix_validate,
  62. authunix_refresh,
  63. authunix_destroy
  64. };
  65. /*
  66. * This struct is pointed to by the ah_private field of an auth_handle.
  67. */
  68. struct audata {
  69. struct opaque_auth au_origcred; /* original credentials */
  70. struct opaque_auth au_shcred; /* short hand cred */
  71. u_long au_shfaults; /* short hand cache faults */
  72. char au_marshed[MAX_AUTH_BYTES];
  73. u_int au_mpos; /* xdr pos at end of marshed */
  74. };
  75. #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
  76. static bool_t marshal_new_auth (AUTH *) internal_function;
  77. /*
  78. * Create a unix style authenticator.
  79. * Returns an auth handle with the given stuff in it.
  80. */
  81. AUTH *
  82. authunix_create (char *machname, uid_t uid, gid_t gid, int len,
  83. gid_t *aup_gids)
  84. {
  85. struct authunix_parms aup;
  86. char mymem[MAX_AUTH_BYTES];
  87. struct timeval now;
  88. XDR xdrs;
  89. AUTH *auth;
  90. struct audata *au;
  91. /*
  92. * Allocate and set up auth handle
  93. */
  94. auth = (AUTH *) mem_alloc (sizeof (*auth));
  95. au = (struct audata *) mem_alloc (sizeof (*au));
  96. if (auth == NULL || au == NULL)
  97. {
  98. no_memory:
  99. (void) fputs (_("authunix_create: out of memory\n"), stderr);
  100. mem_free (auth, sizeof (*auth));
  101. mem_free (au, sizeof (*au));
  102. return NULL;
  103. }
  104. auth->ah_ops = &auth_unix_ops;
  105. auth->ah_private = (caddr_t) au;
  106. auth->ah_verf = au->au_shcred = _null_auth;
  107. au->au_shfaults = 0;
  108. /*
  109. * fill in param struct from the given params
  110. */
  111. (void) gettimeofday (&now, (struct timezone *) 0);
  112. aup.aup_time = now.tv_sec;
  113. aup.aup_machname = machname;
  114. aup.aup_uid = uid;
  115. aup.aup_gid = gid;
  116. aup.aup_len = (u_int) len;
  117. aup.aup_gids = aup_gids;
  118. /*
  119. * Serialize the parameters into origcred
  120. */
  121. xdrmem_create (&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
  122. if (!xdr_authunix_parms (&xdrs, &aup))
  123. abort ();
  124. au->au_origcred.oa_length = len = XDR_GETPOS (&xdrs);
  125. au->au_origcred.oa_flavor = AUTH_UNIX;
  126. au->au_origcred.oa_base = mem_alloc ((u_int) len);
  127. if (au->au_origcred.oa_base == NULL)
  128. goto no_memory;
  129. memcpy(au->au_origcred.oa_base, mymem, (u_int) len);
  130. /*
  131. * set auth handle to reflect new cred.
  132. */
  133. auth->ah_cred = au->au_origcred;
  134. marshal_new_auth (auth);
  135. return auth;
  136. }
  137. libc_hidden_def(authunix_create)
  138. /*
  139. * Returns an auth handle with parameters determined by doing lots of
  140. * syscalls.
  141. */
  142. AUTH *
  143. authunix_create_default (void)
  144. {
  145. int len;
  146. char machname[MAX_MACHINE_NAME + 1];
  147. uid_t uid;
  148. gid_t gid;
  149. int max_nr_groups = sysconf (_SC_NGROUPS_MAX);
  150. gid_t *gids = NULL;
  151. AUTH *ret_auth;
  152. if (max_nr_groups) {
  153. gids = (gid_t*)malloc(sizeof(*gids) * max_nr_groups);
  154. if (gids == NULL)
  155. abort ();
  156. }
  157. if (gethostname (machname, MAX_MACHINE_NAME) == -1)
  158. abort ();
  159. machname[MAX_MACHINE_NAME] = 0;
  160. uid = geteuid ();
  161. gid = getegid ();
  162. if ((len = getgroups (max_nr_groups, gids)) < 0)
  163. abort ();
  164. /* This braindamaged Sun code forces us here to truncate the
  165. list of groups to NGRPS members since the code in
  166. authuxprot.c transforms a fixed array. Grrr. */
  167. ret_auth = authunix_create (machname, uid, gid, MIN (NGRPS, len), gids);
  168. free (gids);
  169. return ret_auth;
  170. }
  171. libc_hidden_def(authunix_create_default)
  172. /*
  173. * authunix operations
  174. */
  175. static void
  176. authunix_nextverf (AUTH *auth attribute_unused)
  177. {
  178. /* no action necessary */
  179. }
  180. static bool_t
  181. authunix_marshal (AUTH *auth, XDR *xdrs)
  182. {
  183. struct audata *au = AUTH_PRIVATE (auth);
  184. return XDR_PUTBYTES (xdrs, au->au_marshed, au->au_mpos);
  185. }
  186. static bool_t
  187. authunix_validate (AUTH *auth, struct opaque_auth *verf)
  188. {
  189. struct audata *au;
  190. XDR xdrs;
  191. if (verf->oa_flavor == AUTH_SHORT)
  192. {
  193. au = AUTH_PRIVATE (auth);
  194. xdrmem_create (&xdrs, verf->oa_base, verf->oa_length,
  195. XDR_DECODE);
  196. if (au->au_shcred.oa_base != NULL)
  197. {
  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. {
  204. auth->ah_cred = au->au_shcred;
  205. }
  206. else
  207. {
  208. xdrs.x_op = XDR_FREE;
  209. (void) xdr_opaque_auth (&xdrs, &au->au_shcred);
  210. au->au_shcred.oa_base = NULL;
  211. auth->ah_cred = au->au_origcred;
  212. }
  213. marshal_new_auth (auth);
  214. }
  215. return TRUE;
  216. }
  217. static bool_t
  218. authunix_refresh (AUTH *auth)
  219. {
  220. struct audata *au = AUTH_PRIVATE (auth);
  221. struct authunix_parms aup;
  222. struct timeval now;
  223. XDR xdrs;
  224. int stat;
  225. if (auth->ah_cred.oa_base == au->au_origcred.oa_base)
  226. {
  227. /* there is no hope. Punt */
  228. return FALSE;
  229. }
  230. au->au_shfaults++;
  231. /* first deserialize the creds back into a struct authunix_parms */
  232. aup.aup_machname = NULL;
  233. aup.aup_gids = (gid_t *) NULL;
  234. xdrmem_create (&xdrs, au->au_origcred.oa_base,
  235. au->au_origcred.oa_length, XDR_DECODE);
  236. stat = xdr_authunix_parms (&xdrs, &aup);
  237. if (!stat)
  238. goto done;
  239. /* update the time and serialize in place */
  240. (void) gettimeofday (&now, (struct timezone *) 0);
  241. aup.aup_time = now.tv_sec;
  242. xdrs.x_op = XDR_ENCODE;
  243. XDR_SETPOS (&xdrs, 0);
  244. stat = xdr_authunix_parms (&xdrs, &aup);
  245. if (!stat)
  246. goto done;
  247. auth->ah_cred = au->au_origcred;
  248. marshal_new_auth (auth);
  249. done:
  250. /* free the struct authunix_parms created by deserializing */
  251. xdrs.x_op = XDR_FREE;
  252. (void) xdr_authunix_parms (&xdrs, &aup);
  253. XDR_DESTROY (&xdrs);
  254. return stat;
  255. }
  256. static void
  257. authunix_destroy (AUTH *auth)
  258. {
  259. struct audata *au = AUTH_PRIVATE (auth);
  260. mem_free (au->au_origcred.oa_base, au->au_origcred.oa_length);
  261. if (au->au_shcred.oa_base != NULL)
  262. mem_free (au->au_shcred.oa_base, au->au_shcred.oa_length);
  263. mem_free (auth->ah_private, sizeof (struct audata));
  264. if (auth->ah_verf.oa_base != NULL)
  265. mem_free (auth->ah_verf.oa_base, auth->ah_verf.oa_length);
  266. mem_free ((caddr_t) auth, sizeof (*auth));
  267. }
  268. /*
  269. * Marshals (pre-serializes) an auth struct.
  270. * sets private data, au_marshed and au_mpos
  271. */
  272. static bool_t
  273. internal_function
  274. marshal_new_auth (AUTH *auth)
  275. {
  276. XDR xdr_stream;
  277. XDR *xdrs = &xdr_stream;
  278. struct audata *au = AUTH_PRIVATE (auth);
  279. xdrmem_create (xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
  280. if ((!xdr_opaque_auth (xdrs, &(auth->ah_cred))) ||
  281. (!xdr_opaque_auth (xdrs, &(auth->ah_verf))))
  282. perror (_("auth_unix.c - Fatal marshalling problem"));
  283. else
  284. au->au_mpos = XDR_GETPOS (xdrs);
  285. XDR_DESTROY (xdrs);
  286. return TRUE;
  287. }