auth_unix.c 8.9 KB

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