svc_authux.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * svc_auth_unix.c
  3. * Handles UNIX flavor authentication parameters on the service side of rpc.
  4. * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
  5. * _svcauth_unix does full blown unix style uid,gid+gids auth,
  6. * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
  7. * Note: the shorthand has been gutted for efficiency.
  8. *
  9. * Copyright (c) 2010, Oracle America, Inc.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are
  13. * met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials
  20. * provided with the distribution.
  21. * * Neither the name of the "Oracle America, Inc." nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  30. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  32. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  34. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <rpc/rpc.h>
  41. #include <rpc/svc.h>
  42. /*
  43. * Unix longhand authenticator
  44. */
  45. enum auth_stat
  46. _svcauth_unix (struct svc_req *rqst, struct rpc_msg *msg)
  47. {
  48. enum auth_stat stat;
  49. XDR xdrs;
  50. struct authunix_parms *aup;
  51. int32_t *buf;
  52. struct area
  53. {
  54. struct authunix_parms area_aup;
  55. char area_machname[MAX_MACHINE_NAME + 1];
  56. gid_t area_gids[NGRPS];
  57. }
  58. *area;
  59. u_int auth_len;
  60. u_int str_len, gid_len;
  61. u_int i;
  62. area = (struct area *) rqst->rq_clntcred;
  63. aup = &area->area_aup;
  64. aup->aup_machname = area->area_machname;
  65. aup->aup_gids = area->area_gids;
  66. auth_len = (u_int) msg->rm_call.cb_cred.oa_length;
  67. xdrmem_create (&xdrs, msg->rm_call.cb_cred.oa_base, auth_len, XDR_DECODE);
  68. buf = XDR_INLINE (&xdrs, auth_len);
  69. if (buf != NULL)
  70. {
  71. aup->aup_time = IXDR_GET_LONG (buf);
  72. str_len = IXDR_GET_U_INT32 (buf);
  73. if (str_len > MAX_MACHINE_NAME)
  74. {
  75. stat = AUTH_BADCRED;
  76. goto done;
  77. }
  78. memcpy (aup->aup_machname, (caddr_t) buf, (u_int) str_len);
  79. aup->aup_machname[str_len] = 0;
  80. str_len = RNDUP (str_len);
  81. buf = (int32_t *) ((char *) buf + str_len);
  82. aup->aup_uid = IXDR_GET_LONG (buf);
  83. aup->aup_gid = IXDR_GET_LONG (buf);
  84. gid_len = IXDR_GET_U_INT32 (buf);
  85. if (gid_len > NGRPS)
  86. {
  87. stat = AUTH_BADCRED;
  88. goto done;
  89. }
  90. aup->aup_len = gid_len;
  91. for (i = 0; i < gid_len; i++)
  92. {
  93. aup->aup_gids[i] = IXDR_GET_LONG (buf);
  94. }
  95. /*
  96. * five is the smallest unix credentials structure -
  97. * timestamp, hostname len (0), uid, gid, and gids len (0).
  98. */
  99. if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len)
  100. {
  101. stat = AUTH_BADCRED;
  102. goto done;
  103. }
  104. }
  105. else if (!xdr_authunix_parms (&xdrs, aup))
  106. {
  107. xdrs.x_op = XDR_FREE;
  108. (void) xdr_authunix_parms (&xdrs, aup);
  109. stat = AUTH_BADCRED;
  110. goto done;
  111. }
  112. /* get the verifier */
  113. if ((u_int)msg->rm_call.cb_verf.oa_length)
  114. {
  115. rqst->rq_xprt->xp_verf.oa_flavor =
  116. msg->rm_call.cb_verf.oa_flavor;
  117. rqst->rq_xprt->xp_verf.oa_base =
  118. msg->rm_call.cb_verf.oa_base;
  119. rqst->rq_xprt->xp_verf.oa_length =
  120. msg->rm_call.cb_verf.oa_length;
  121. }
  122. else
  123. {
  124. rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
  125. rqst->rq_xprt->xp_verf.oa_length = 0;
  126. }
  127. stat = AUTH_OK;
  128. done:
  129. XDR_DESTROY (&xdrs);
  130. return stat;
  131. }
  132. /*
  133. * Shorthand unix authenticator
  134. * Looks up longhand in a cache.
  135. */
  136. /*ARGSUSED */
  137. enum auth_stat
  138. _svcauth_short (struct svc_req *rqst, struct rpc_msg *msg)
  139. {
  140. return AUTH_REJECTEDCRED;
  141. }