auth_none.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/err.h>
  4. #include <linux/module.h>
  5. #include <linux/random.h>
  6. #include <linux/slab.h>
  7. #include <linux/ceph/decode.h>
  8. #include <linux/ceph/auth.h>
  9. #include "auth_none.h"
  10. static void reset(struct ceph_auth_client *ac)
  11. {
  12. struct ceph_auth_none_info *xi = ac->private;
  13. xi->starting = true;
  14. }
  15. static void destroy(struct ceph_auth_client *ac)
  16. {
  17. kfree(ac->private);
  18. ac->private = NULL;
  19. }
  20. static int is_authenticated(struct ceph_auth_client *ac)
  21. {
  22. struct ceph_auth_none_info *xi = ac->private;
  23. return !xi->starting;
  24. }
  25. static int should_authenticate(struct ceph_auth_client *ac)
  26. {
  27. struct ceph_auth_none_info *xi = ac->private;
  28. return xi->starting;
  29. }
  30. static int ceph_auth_none_build_authorizer(struct ceph_auth_client *ac,
  31. struct ceph_none_authorizer *au)
  32. {
  33. void *p = au->buf;
  34. void *const end = p + sizeof(au->buf);
  35. int ret;
  36. ceph_encode_8_safe(&p, end, 1, e_range);
  37. ret = ceph_auth_entity_name_encode(ac->name, &p, end);
  38. if (ret < 0)
  39. return ret;
  40. ceph_encode_64_safe(&p, end, ac->global_id, e_range);
  41. au->buf_len = p - (void *)au->buf;
  42. dout("%s built authorizer len %d\n", __func__, au->buf_len);
  43. return 0;
  44. e_range:
  45. return -ERANGE;
  46. }
  47. static int build_request(struct ceph_auth_client *ac, void *buf, void *end)
  48. {
  49. return 0;
  50. }
  51. /*
  52. * the generic auth code decode the global_id, and we carry no actual
  53. * authenticate state, so nothing happens here.
  54. */
  55. static int handle_reply(struct ceph_auth_client *ac, int result,
  56. void *buf, void *end)
  57. {
  58. struct ceph_auth_none_info *xi = ac->private;
  59. xi->starting = false;
  60. return result;
  61. }
  62. static void ceph_auth_none_destroy_authorizer(struct ceph_authorizer *a)
  63. {
  64. kfree(a);
  65. }
  66. /*
  67. * build an 'authorizer' with our entity_name and global_id. it is
  68. * identical for all services we connect to.
  69. */
  70. static int ceph_auth_none_create_authorizer(
  71. struct ceph_auth_client *ac, int peer_type,
  72. struct ceph_auth_handshake *auth)
  73. {
  74. struct ceph_none_authorizer *au;
  75. int ret;
  76. au = kmalloc(sizeof(*au), GFP_NOFS);
  77. if (!au)
  78. return -ENOMEM;
  79. au->base.destroy = ceph_auth_none_destroy_authorizer;
  80. ret = ceph_auth_none_build_authorizer(ac, au);
  81. if (ret) {
  82. kfree(au);
  83. return ret;
  84. }
  85. auth->authorizer = (struct ceph_authorizer *) au;
  86. auth->authorizer_buf = au->buf;
  87. auth->authorizer_buf_len = au->buf_len;
  88. auth->authorizer_reply_buf = au->reply_buf;
  89. auth->authorizer_reply_buf_len = sizeof (au->reply_buf);
  90. return 0;
  91. }
  92. static const struct ceph_auth_client_ops ceph_auth_none_ops = {
  93. .name = "none",
  94. .reset = reset,
  95. .destroy = destroy,
  96. .is_authenticated = is_authenticated,
  97. .should_authenticate = should_authenticate,
  98. .build_request = build_request,
  99. .handle_reply = handle_reply,
  100. .create_authorizer = ceph_auth_none_create_authorizer,
  101. };
  102. int ceph_auth_none_init(struct ceph_auth_client *ac)
  103. {
  104. struct ceph_auth_none_info *xi;
  105. dout("ceph_auth_none_init %p\n", ac);
  106. xi = kzalloc(sizeof(*xi), GFP_NOFS);
  107. if (!xi)
  108. return -ENOMEM;
  109. xi->starting = true;
  110. ac->protocol = CEPH_AUTH_NONE;
  111. ac->private = xi;
  112. ac->ops = &ceph_auth_none_ops;
  113. return 0;
  114. }