auth_null.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/net/sunrpc/auth_null.c
  4. *
  5. * AUTH_NULL authentication. Really :-)
  6. *
  7. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/module.h>
  11. #include <linux/sunrpc/clnt.h>
  12. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  13. # define RPCDBG_FACILITY RPCDBG_AUTH
  14. #endif
  15. static struct rpc_auth null_auth;
  16. static struct rpc_cred null_cred;
  17. static struct rpc_auth *
  18. nul_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
  19. {
  20. refcount_inc(&null_auth.au_count);
  21. return &null_auth;
  22. }
  23. static void
  24. nul_destroy(struct rpc_auth *auth)
  25. {
  26. }
  27. /*
  28. * Lookup NULL creds for current process
  29. */
  30. static struct rpc_cred *
  31. nul_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  32. {
  33. return get_rpccred(&null_cred);
  34. }
  35. /*
  36. * Destroy cred handle.
  37. */
  38. static void
  39. nul_destroy_cred(struct rpc_cred *cred)
  40. {
  41. }
  42. /*
  43. * Match cred handle against current process
  44. */
  45. static int
  46. nul_match(struct auth_cred *acred, struct rpc_cred *cred, int taskflags)
  47. {
  48. return 1;
  49. }
  50. /*
  51. * Marshal credential.
  52. */
  53. static int
  54. nul_marshal(struct rpc_task *task, struct xdr_stream *xdr)
  55. {
  56. __be32 *p;
  57. p = xdr_reserve_space(xdr, 4 * sizeof(*p));
  58. if (!p)
  59. return -EMSGSIZE;
  60. /* Credential */
  61. *p++ = rpc_auth_null;
  62. *p++ = xdr_zero;
  63. /* Verifier */
  64. *p++ = rpc_auth_null;
  65. *p = xdr_zero;
  66. return 0;
  67. }
  68. /*
  69. * Refresh credential. This is a no-op for AUTH_NULL
  70. */
  71. static int
  72. nul_refresh(struct rpc_task *task)
  73. {
  74. set_bit(RPCAUTH_CRED_UPTODATE, &task->tk_rqstp->rq_cred->cr_flags);
  75. return 0;
  76. }
  77. static int
  78. nul_validate(struct rpc_task *task, struct xdr_stream *xdr)
  79. {
  80. __be32 *p;
  81. p = xdr_inline_decode(xdr, 2 * sizeof(*p));
  82. if (!p)
  83. return -EIO;
  84. if (*p++ != rpc_auth_null)
  85. return -EIO;
  86. if (*p != xdr_zero)
  87. return -EIO;
  88. return 0;
  89. }
  90. const struct rpc_authops authnull_ops = {
  91. .owner = THIS_MODULE,
  92. .au_flavor = RPC_AUTH_NULL,
  93. .au_name = "NULL",
  94. .create = nul_create,
  95. .destroy = nul_destroy,
  96. .lookup_cred = nul_lookup_cred,
  97. };
  98. static
  99. struct rpc_auth null_auth = {
  100. .au_cslack = NUL_CALLSLACK,
  101. .au_rslack = NUL_REPLYSLACK,
  102. .au_verfsize = NUL_REPLYSLACK,
  103. .au_ralign = NUL_REPLYSLACK,
  104. .au_ops = &authnull_ops,
  105. .au_flavor = RPC_AUTH_NULL,
  106. .au_count = REFCOUNT_INIT(1),
  107. };
  108. static
  109. const struct rpc_credops null_credops = {
  110. .cr_name = "AUTH_NULL",
  111. .crdestroy = nul_destroy_cred,
  112. .crmatch = nul_match,
  113. .crmarshal = nul_marshal,
  114. .crwrap_req = rpcauth_wrap_req_encode,
  115. .crrefresh = nul_refresh,
  116. .crvalidate = nul_validate,
  117. .crunwrap_resp = rpcauth_unwrap_resp_decode,
  118. };
  119. static
  120. struct rpc_cred null_cred = {
  121. .cr_lru = LIST_HEAD_INIT(null_cred.cr_lru),
  122. .cr_auth = &null_auth,
  123. .cr_ops = &null_credops,
  124. .cr_count = REFCOUNT_INIT(2),
  125. .cr_flags = 1UL << RPCAUTH_CRED_UPTODATE,
  126. };