auth_none.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, u64 global_id,
  56. void *buf, void *end, u8 *session_key,
  57. int *session_key_len, u8 *con_secret,
  58. int *con_secret_len)
  59. {
  60. struct ceph_auth_none_info *xi = ac->private;
  61. xi->starting = false;
  62. ceph_auth_set_global_id(ac, global_id);
  63. return 0;
  64. }
  65. static void ceph_auth_none_destroy_authorizer(struct ceph_authorizer *a)
  66. {
  67. kfree(a);
  68. }
  69. /*
  70. * build an 'authorizer' with our entity_name and global_id. it is
  71. * identical for all services we connect to.
  72. */
  73. static int ceph_auth_none_create_authorizer(
  74. struct ceph_auth_client *ac, int peer_type,
  75. struct ceph_auth_handshake *auth)
  76. {
  77. struct ceph_none_authorizer *au;
  78. int ret;
  79. au = kmalloc(sizeof(*au), GFP_NOFS);
  80. if (!au)
  81. return -ENOMEM;
  82. au->base.destroy = ceph_auth_none_destroy_authorizer;
  83. ret = ceph_auth_none_build_authorizer(ac, au);
  84. if (ret) {
  85. kfree(au);
  86. return ret;
  87. }
  88. auth->authorizer = (struct ceph_authorizer *) au;
  89. auth->authorizer_buf = au->buf;
  90. auth->authorizer_buf_len = au->buf_len;
  91. auth->authorizer_reply_buf = NULL;
  92. auth->authorizer_reply_buf_len = 0;
  93. return 0;
  94. }
  95. static const struct ceph_auth_client_ops ceph_auth_none_ops = {
  96. .reset = reset,
  97. .destroy = destroy,
  98. .is_authenticated = is_authenticated,
  99. .should_authenticate = should_authenticate,
  100. .build_request = build_request,
  101. .handle_reply = handle_reply,
  102. .create_authorizer = ceph_auth_none_create_authorizer,
  103. };
  104. int ceph_auth_none_init(struct ceph_auth_client *ac)
  105. {
  106. struct ceph_auth_none_info *xi;
  107. dout("ceph_auth_none_init %p\n", ac);
  108. xi = kzalloc(sizeof(*xi), GFP_NOFS);
  109. if (!xi)
  110. return -ENOMEM;
  111. xi->starting = true;
  112. ac->protocol = CEPH_AUTH_NONE;
  113. ac->private = xi;
  114. ac->ops = &ceph_auth_none_ops;
  115. return 0;
  116. }