authunix_prot.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* $NetBSD: authunix_prot.c,v 1.12 2000/01/22 22:19:17 mycroft Exp $ */
  2. /*-
  3. * SPDX-License-Identifier: BSD-3-Clause
  4. *
  5. * Copyright (c) 2009, Sun Microsystems, Inc.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * - Neither the name of Sun Microsystems, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #if defined(LIBC_SCCS) && !defined(lint)
  32. static char *sccsid2 = "@(#)authunix_prot.c 1.15 87/08/11 Copyr 1984 Sun Micro";
  33. static char *sccsid = "@(#)authunix_prot.c 2.1 88/07/29 4.0 RPCSRC";
  34. #endif
  35. #include <sys/cdefs.h>
  36. __FBSDID("$FreeBSD$");
  37. /*
  38. * authunix_prot.c
  39. * XDR for UNIX style authentication parameters for RPC
  40. *
  41. * Copyright (C) 1984, Sun Microsystems, Inc.
  42. */
  43. #include <sys/param.h>
  44. #include <sys/jail.h>
  45. #include <sys/kernel.h>
  46. #include <sys/systm.h>
  47. #include <sys/ucred.h>
  48. #include <rpc/types.h>
  49. #include <rpc/xdr.h>
  50. #include <rpc/auth.h>
  51. #include <rpc/rpc_com.h>
  52. /* gids compose part of a credential; there may not be more than 16 of them */
  53. #define NGRPS 16
  54. /*
  55. * XDR for unix authentication parameters.
  56. */
  57. bool_t
  58. xdr_authunix_parms(XDR *xdrs, uint32_t *time, struct xucred *cred)
  59. {
  60. uint32_t namelen;
  61. uint32_t ngroups, i;
  62. uint32_t junk;
  63. char hostbuf[MAXHOSTNAMELEN];
  64. if (xdrs->x_op == XDR_ENCODE) {
  65. /*
  66. * Restrict name length to 255 according to RFC 1057.
  67. */
  68. getcredhostname(NULL, hostbuf, sizeof(hostbuf));
  69. namelen = strlen(hostbuf);
  70. if (namelen > 255)
  71. namelen = 255;
  72. } else {
  73. namelen = 0;
  74. }
  75. junk = 0;
  76. if (!xdr_uint32_t(xdrs, time)
  77. || !xdr_uint32_t(xdrs, &namelen))
  78. return (FALSE);
  79. /*
  80. * Ignore the hostname on decode.
  81. */
  82. if (xdrs->x_op == XDR_ENCODE) {
  83. if (!xdr_opaque(xdrs, hostbuf, namelen))
  84. return (FALSE);
  85. } else {
  86. xdr_setpos(xdrs, xdr_getpos(xdrs) + RNDUP(namelen));
  87. }
  88. if (!xdr_uint32_t(xdrs, &cred->cr_uid))
  89. return (FALSE);
  90. if (!xdr_uint32_t(xdrs, &cred->cr_groups[0]))
  91. return (FALSE);
  92. if (xdrs->x_op == XDR_ENCODE) {
  93. ngroups = cred->cr_ngroups - 1;
  94. if (ngroups > NGRPS)
  95. ngroups = NGRPS;
  96. }
  97. if (!xdr_uint32_t(xdrs, &ngroups))
  98. return (FALSE);
  99. for (i = 0; i < ngroups; i++) {
  100. if (i + 1 < ngroups_max + 1) {
  101. if (!xdr_uint32_t(xdrs, &cred->cr_groups[i + 1]))
  102. return (FALSE);
  103. } else {
  104. if (!xdr_uint32_t(xdrs, &junk))
  105. return (FALSE);
  106. }
  107. }
  108. if (xdrs->x_op == XDR_DECODE) {
  109. if (ngroups + 1 > ngroups_max + 1)
  110. cred->cr_ngroups = ngroups_max + 1;
  111. else
  112. cred->cr_ngroups = ngroups + 1;
  113. }
  114. return (TRUE);
  115. }