auth_unix.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #define __FORCE_GLIBC
  41. #include <features.h>
  42. #include <limits.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <unistd.h>
  46. #include <sys/param.h>
  47. #include <rpc/types.h>
  48. #include <rpc/xdr.h>
  49. #include <rpc/auth.h>
  50. #include <rpc/auth_unix.h>
  51. #ifdef USE_IN_LIBIO
  52. # include <wchar.h>
  53. #endif
  54. /*
  55. * Unix authenticator operations vector
  56. */
  57. static void authunix_nextverf (AUTH *);
  58. static bool_t authunix_marshal (AUTH *, XDR *);
  59. static bool_t authunix_validate (AUTH *, struct opaque_auth *);
  60. static bool_t authunix_refresh (AUTH *);
  61. static void authunix_destroy (AUTH *);
  62. static struct auth_ops auth_unix_ops = {
  63. authunix_nextverf,
  64. authunix_marshal,
  65. authunix_validate,
  66. authunix_refresh,
  67. authunix_destroy
  68. };
  69. /*
  70. * This struct is pointed to by the ah_private field of an auth_handle.
  71. */
  72. struct audata {
  73. struct opaque_auth au_origcred; /* original credentials */
  74. struct opaque_auth au_shcred; /* short hand cred */
  75. u_long au_shfaults; /* short hand cache faults */
  76. char au_marshed[MAX_AUTH_BYTES];
  77. u_int au_mpos; /* xdr pos at end of marshed */
  78. };
  79. #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
  80. static bool_t marshal_new_auth (AUTH *) internal_function;
  81. /*
  82. * Create a unix style authenticator.
  83. * Returns an auth handle with the given stuff in it.
  84. */
  85. AUTH *
  86. authunix_create (char *machname, uid_t uid, gid_t gid, int len,
  87. gid_t *aup_gids)
  88. {
  89. struct authunix_parms aup;
  90. char mymem[MAX_AUTH_BYTES];
  91. struct timeval now;
  92. XDR xdrs;
  93. AUTH *auth;
  94. struct audata *au;
  95. /*
  96. * Allocate and set up auth handle
  97. */
  98. auth = (AUTH *) mem_alloc (sizeof (*auth));
  99. au = (struct audata *) mem_alloc (sizeof (*au));
  100. if (auth == NULL || au == NULL)
  101. {
  102. no_memory:
  103. #ifdef USE_IN_LIBIO
  104. if (_IO_fwide (stderr, 0) > 0)
  105. (void) fwprintf (stderr, L"%s",
  106. _("authunix_create: out of memory\n"));
  107. else
  108. #endif
  109. (void) fputs (_("authunix_create: out of memory\n"), stderr);
  110. mem_free (auth, sizeof (*auth));
  111. mem_free (au, sizeof (*au));
  112. return NULL;
  113. }
  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. au->au_origcred.oa_base = mem_alloc ((u_int) len);
  137. if (au->au_origcred.oa_base == NULL)
  138. goto no_memory;
  139. memcpy(au->au_origcred.oa_base, mymem, (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. libc_hidden_def(authunix_create)
  148. /*
  149. * Returns an auth handle with parameters determined by doing lots of
  150. * syscalls.
  151. */
  152. AUTH *
  153. authunix_create_default (void)
  154. {
  155. int len;
  156. char machname[MAX_MACHINE_NAME + 1];
  157. uid_t uid;
  158. gid_t gid;
  159. int max_nr_groups = sysconf (_SC_NGROUPS_MAX);
  160. gid_t *gids = NULL;
  161. AUTH *ret_auth;
  162. if (max_nr_groups) {
  163. gids = (gid_t*)malloc(sizeof(*gids) * max_nr_groups);
  164. if (gids == NULL)
  165. abort ();
  166. }
  167. if (gethostname (machname, MAX_MACHINE_NAME) == -1)
  168. abort ();
  169. machname[MAX_MACHINE_NAME] = 0;
  170. uid = geteuid ();
  171. gid = getegid ();
  172. if ((len = getgroups (max_nr_groups, gids)) < 0)
  173. abort ();
  174. /* This braindamaged Sun code forces us here to truncate the
  175. list of groups to NGRPS members since the code in
  176. authuxprot.c transforms a fixed array. Grrr. */
  177. ret_auth = authunix_create (machname, uid, gid, MIN (NGRPS, len), gids);
  178. free (gids);
  179. return ret_auth;
  180. }
  181. libc_hidden_def(authunix_create_default)
  182. /*
  183. * authunix operations
  184. */
  185. static void
  186. authunix_nextverf (AUTH *auth attribute_unused)
  187. {
  188. /* no action necessary */
  189. }
  190. static bool_t
  191. authunix_marshal (AUTH *auth, XDR *xdrs)
  192. {
  193. struct audata *au = AUTH_PRIVATE (auth);
  194. return XDR_PUTBYTES (xdrs, au->au_marshed, au->au_mpos);
  195. }
  196. static bool_t
  197. authunix_validate (AUTH *auth, struct opaque_auth *verf)
  198. {
  199. struct audata *au;
  200. XDR xdrs;
  201. if (verf->oa_flavor == AUTH_SHORT)
  202. {
  203. au = AUTH_PRIVATE (auth);
  204. xdrmem_create (&xdrs, verf->oa_base, verf->oa_length,
  205. XDR_DECODE);
  206. if (au->au_shcred.oa_base != NULL)
  207. {
  208. mem_free (au->au_shcred.oa_base,
  209. au->au_shcred.oa_length);
  210. au->au_shcred.oa_base = NULL;
  211. }
  212. if (xdr_opaque_auth (&xdrs, &au->au_shcred))
  213. {
  214. auth->ah_cred = au->au_shcred;
  215. }
  216. else
  217. {
  218. xdrs.x_op = XDR_FREE;
  219. (void) xdr_opaque_auth (&xdrs, &au->au_shcred);
  220. au->au_shcred.oa_base = NULL;
  221. auth->ah_cred = au->au_origcred;
  222. }
  223. marshal_new_auth (auth);
  224. }
  225. return TRUE;
  226. }
  227. static bool_t
  228. authunix_refresh (AUTH *auth)
  229. {
  230. struct audata *au = AUTH_PRIVATE (auth);
  231. struct authunix_parms aup;
  232. struct timeval now;
  233. XDR xdrs;
  234. int stat;
  235. if (auth->ah_cred.oa_base == au->au_origcred.oa_base)
  236. {
  237. /* there is no hope. Punt */
  238. return FALSE;
  239. }
  240. au->au_shfaults++;
  241. /* first deserialize the creds back into a struct authunix_parms */
  242. aup.aup_machname = NULL;
  243. aup.aup_gids = (gid_t *) NULL;
  244. xdrmem_create (&xdrs, au->au_origcred.oa_base,
  245. au->au_origcred.oa_length, XDR_DECODE);
  246. stat = xdr_authunix_parms (&xdrs, &aup);
  247. if (!stat)
  248. goto done;
  249. /* update the time and serialize in place */
  250. (void) gettimeofday (&now, (struct timezone *) 0);
  251. aup.aup_time = now.tv_sec;
  252. xdrs.x_op = XDR_ENCODE;
  253. XDR_SETPOS (&xdrs, 0);
  254. stat = xdr_authunix_parms (&xdrs, &aup);
  255. if (!stat)
  256. goto done;
  257. auth->ah_cred = au->au_origcred;
  258. marshal_new_auth (auth);
  259. done:
  260. /* free the struct authunix_parms created by deserializing */
  261. xdrs.x_op = XDR_FREE;
  262. (void) xdr_authunix_parms (&xdrs, &aup);
  263. XDR_DESTROY (&xdrs);
  264. return stat;
  265. }
  266. static void
  267. authunix_destroy (AUTH *auth)
  268. {
  269. struct audata *au = AUTH_PRIVATE (auth);
  270. mem_free (au->au_origcred.oa_base, au->au_origcred.oa_length);
  271. if (au->au_shcred.oa_base != NULL)
  272. mem_free (au->au_shcred.oa_base, au->au_shcred.oa_length);
  273. mem_free (auth->ah_private, sizeof (struct audata));
  274. if (auth->ah_verf.oa_base != NULL)
  275. mem_free (auth->ah_verf.oa_base, auth->ah_verf.oa_length);
  276. mem_free ((caddr_t) auth, sizeof (*auth));
  277. }
  278. /*
  279. * Marshals (pre-serializes) an auth struct.
  280. * sets private data, au_marshed and au_mpos
  281. */
  282. static bool_t
  283. internal_function
  284. marshal_new_auth (AUTH *auth)
  285. {
  286. XDR xdr_stream;
  287. XDR *xdrs = &xdr_stream;
  288. struct audata *au = AUTH_PRIVATE (auth);
  289. xdrmem_create (xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
  290. if ((!xdr_opaque_auth (xdrs, &(auth->ah_cred))) ||
  291. (!xdr_opaque_auth (xdrs, &(auth->ah_verf))))
  292. perror (_("auth_none.c - Fatal marshalling problem"));
  293. else
  294. au->au_mpos = XDR_GETPOS (xdrs);
  295. XDR_DESTROY (xdrs);
  296. return TRUE;
  297. }