auth_unix.c 9.1 KB

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