svc_auth.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* $NetBSD: svc_auth.c,v 1.12 2000/07/06 03:10:35 christos 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. /*
  32. * Copyright (c) 1986-1991 by Sun Microsystems Inc.
  33. */
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. #ident "@(#)svc_auth.c 1.16 94/04/24 SMI"
  36. static char sccsid[] = "@(#)svc_auth.c 1.26 89/02/07 Copyr 1984 Sun Micro";
  37. #endif
  38. #include <sys/cdefs.h>
  39. __FBSDID("$FreeBSD$");
  40. /*
  41. * svc_auth.c, Server-side rpc authenticator interface.
  42. *
  43. */
  44. #include <sys/param.h>
  45. #include <sys/lock.h>
  46. #include <sys/mutex.h>
  47. #include <sys/systm.h>
  48. #include <sys/jail.h>
  49. #include <sys/ucred.h>
  50. #include <rpc/rpc.h>
  51. #include <rpc/rpcsec_tls.h>
  52. static enum auth_stat (*_svcauth_rpcsec_gss)(struct svc_req *,
  53. struct rpc_msg *) = NULL;
  54. static int (*_svcauth_rpcsec_gss_getcred)(struct svc_req *,
  55. struct ucred **, int *);
  56. static struct svc_auth_ops svc_auth_null_ops;
  57. /*
  58. * The call rpc message, msg has been obtained from the wire. The msg contains
  59. * the raw form of credentials and verifiers. authenticate returns AUTH_OK
  60. * if the msg is successfully authenticated. If AUTH_OK then the routine also
  61. * does the following things:
  62. * set rqst->rq_xprt->verf to the appropriate response verifier;
  63. * sets rqst->rq_client_cred to the "cooked" form of the credentials.
  64. *
  65. * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
  66. * its length is set appropriately.
  67. *
  68. * The caller still owns and is responsible for msg->u.cmb.cred and
  69. * msg->u.cmb.verf. The authentication system retains ownership of
  70. * rqst->rq_client_cred, the cooked credentials.
  71. *
  72. * There is an assumption that any flavour less than AUTH_NULL is
  73. * invalid.
  74. */
  75. enum auth_stat
  76. _authenticate(struct svc_req *rqst, struct rpc_msg *msg)
  77. {
  78. int cred_flavor;
  79. enum auth_stat dummy;
  80. rqst->rq_cred = msg->rm_call.cb_cred;
  81. rqst->rq_auth.svc_ah_ops = &svc_auth_null_ops;
  82. rqst->rq_auth.svc_ah_private = NULL;
  83. cred_flavor = rqst->rq_cred.oa_flavor;
  84. switch (cred_flavor) {
  85. case AUTH_NULL:
  86. dummy = _svcauth_null(rqst, msg);
  87. return (dummy);
  88. case AUTH_SYS:
  89. if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)
  90. return (AUTH_REJECTEDCRED);
  91. dummy = _svcauth_unix(rqst, msg);
  92. return (dummy);
  93. case AUTH_SHORT:
  94. if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)
  95. return (AUTH_REJECTEDCRED);
  96. dummy = _svcauth_short(rqst, msg);
  97. return (dummy);
  98. case RPCSEC_GSS:
  99. if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)
  100. return (AUTH_REJECTEDCRED);
  101. if (!_svcauth_rpcsec_gss)
  102. return (AUTH_REJECTEDCRED);
  103. dummy = _svcauth_rpcsec_gss(rqst, msg);
  104. return (dummy);
  105. case AUTH_TLS:
  106. dummy = _svcauth_rpcsec_tls(rqst, msg);
  107. return (dummy);
  108. default:
  109. break;
  110. }
  111. return (AUTH_REJECTEDCRED);
  112. }
  113. /*
  114. * A set of null auth methods used by any authentication protocols
  115. * that don't need to inspect or modify the message body.
  116. */
  117. static bool_t
  118. svcauth_null_wrap(SVCAUTH *auth, struct mbuf **mp)
  119. {
  120. return (TRUE);
  121. }
  122. static bool_t
  123. svcauth_null_unwrap(SVCAUTH *auth, struct mbuf **mp)
  124. {
  125. return (TRUE);
  126. }
  127. static void
  128. svcauth_null_release(SVCAUTH *auth)
  129. {
  130. }
  131. static struct svc_auth_ops svc_auth_null_ops = {
  132. svcauth_null_wrap,
  133. svcauth_null_unwrap,
  134. svcauth_null_release,
  135. };
  136. /*ARGSUSED*/
  137. enum auth_stat
  138. _svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
  139. {
  140. rqst->rq_verf = _null_auth;
  141. return (AUTH_OK);
  142. }
  143. int
  144. svc_auth_reg(int flavor,
  145. enum auth_stat (*svcauth)(struct svc_req *, struct rpc_msg *),
  146. int (*getcred)(struct svc_req *, struct ucred **, int *))
  147. {
  148. if (flavor == RPCSEC_GSS) {
  149. _svcauth_rpcsec_gss = svcauth;
  150. _svcauth_rpcsec_gss_getcred = getcred;
  151. }
  152. return (TRUE);
  153. }
  154. int
  155. svc_getcred(struct svc_req *rqst, struct ucred **crp, int *flavorp)
  156. {
  157. struct ucred *cr = NULL;
  158. int flavor;
  159. struct xucred *xcr;
  160. SVCXPRT *xprt = rqst->rq_xprt;
  161. flavor = rqst->rq_cred.oa_flavor;
  162. if (flavorp)
  163. *flavorp = flavor;
  164. /*
  165. * If there are credentials acquired via a TLS
  166. * certificate for this TCP connection, use those
  167. * instead of what is in the RPC header.
  168. */
  169. if ((xprt->xp_tls & (RPCTLS_FLAGS_CERTUSER |
  170. RPCTLS_FLAGS_DISABLED)) == RPCTLS_FLAGS_CERTUSER &&
  171. flavor == AUTH_UNIX) {
  172. cr = crget();
  173. cr->cr_uid = cr->cr_ruid = cr->cr_svuid = xprt->xp_uid;
  174. crsetgroups(cr, xprt->xp_ngrps, xprt->xp_gidp);
  175. cr->cr_rgid = cr->cr_svgid = xprt->xp_gidp[0];
  176. cr->cr_prison = &prison0;
  177. prison_hold(cr->cr_prison);
  178. *crp = cr;
  179. return (TRUE);
  180. }
  181. switch (flavor) {
  182. case AUTH_UNIX:
  183. xcr = (struct xucred *) rqst->rq_clntcred;
  184. cr = crget();
  185. cr->cr_uid = cr->cr_ruid = cr->cr_svuid = xcr->cr_uid;
  186. crsetgroups(cr, xcr->cr_ngroups, xcr->cr_groups);
  187. cr->cr_rgid = cr->cr_svgid = cr->cr_groups[0];
  188. cr->cr_prison = &prison0;
  189. prison_hold(cr->cr_prison);
  190. *crp = cr;
  191. return (TRUE);
  192. case RPCSEC_GSS:
  193. if (!_svcauth_rpcsec_gss_getcred)
  194. return (FALSE);
  195. return (_svcauth_rpcsec_gss_getcred(rqst, crp, flavorp));
  196. default:
  197. return (FALSE);
  198. }
  199. }