nfs_auth.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <rpc/types.h>
  10. #include <rpc/xdr.h>
  11. #include <rpc/auth.h>
  12. #define MAX_MARSHEL_SIZE 64
  13. struct nfs_credentia
  14. {
  15. rt_uint32_t stamp;
  16. char *name;
  17. rt_uint32_t uid;
  18. rt_uint32_t gid;
  19. rt_uint32_t *auxi;
  20. rt_uint32_t auxi_count;
  21. };
  22. static void authnone_verf(AUTH *);
  23. static bool_t authnone_validate(AUTH *, struct opaque_auth *);
  24. static bool_t authnone_refresh(AUTH *);
  25. static void authnone_destroy(AUTH *);
  26. static bool_t authnone_marshal(AUTH *client, XDR *xdrs);
  27. static struct nfs_credentia _credentia = {
  28. .stamp = 0,
  29. .name = "rt-thread",
  30. .uid = 0,
  31. .gid = 0,
  32. .auxi = NULL,
  33. .auxi_count = 0,
  34. };
  35. struct opaque_auth _null_auth;
  36. static struct auth_ops ops =
  37. {
  38. authnone_verf,
  39. authnone_marshal,
  40. authnone_validate,
  41. authnone_refresh,
  42. authnone_destroy
  43. };
  44. static struct authnone_private
  45. {
  46. AUTH no_client;
  47. char marshalled_client[MAX_MARSHEL_SIZE];
  48. unsigned int mcnt;
  49. } *authnone_private;
  50. AUTH *authnone_create(void)
  51. {
  52. register struct authnone_private *ap = authnone_private;
  53. XDR xdr_stream;
  54. register XDR *xdrs;
  55. extern bool_t xdr_opaque_auth(XDR * xdrs, struct opaque_auth * ap);
  56. struct opaque_auth auth;
  57. rt_uint32_t *auth_buf, *auth_base;
  58. int buf_len = 0, str_len = 0;
  59. if (_credentia.name)
  60. {
  61. str_len = strlen(_credentia.name);
  62. }
  63. if (str_len == 0)
  64. {
  65. _credentia.name = "unknown";
  66. str_len = strlen(_credentia.name);
  67. }
  68. buf_len = ((str_len) + (sizeof(rt_uint32_t)) - 1) & ~((sizeof(rt_uint32_t)) - 1);
  69. buf_len += sizeof(struct nfs_credentia);
  70. if (_credentia.auxi && _credentia.auxi_count)
  71. {
  72. buf_len += sizeof(rt_uint32_t) * _credentia.auxi_count;
  73. }
  74. auth_buf = auth_base = rt_malloc(buf_len);
  75. if (auth_buf == NULL)
  76. {
  77. return NULL;
  78. }
  79. memset(auth_buf, 0, buf_len);
  80. *auth_buf++ = htonl(rt_tick_get());
  81. *auth_buf++ = htonl(str_len);
  82. memcpy(auth_buf, _credentia.name, str_len);
  83. auth_buf += (str_len + sizeof(rt_uint32_t) - 1) >> 2;
  84. *auth_buf++ = htonl(_credentia.uid);
  85. *auth_buf++ = htonl(_credentia.gid);
  86. if (_credentia.auxi && _credentia.auxi_count)
  87. {
  88. rt_uint32_t tmp_cnt = 0;
  89. *auth_buf++ = htonl(_credentia.auxi_count);
  90. while (tmp_cnt < _credentia.auxi_count)
  91. {
  92. *auth_buf++ = htonl(_credentia.auxi[tmp_cnt]);
  93. }
  94. }
  95. else
  96. {
  97. *auth_buf++ = htonl(0);
  98. }
  99. if (ap == 0)
  100. {
  101. ap = (struct authnone_private *) rt_malloc(sizeof(*ap));
  102. if (ap == 0)
  103. {
  104. rt_free(auth_base);
  105. return NULL;
  106. }
  107. memset(ap, 0, sizeof(*ap));
  108. authnone_private = ap;
  109. }
  110. if (!ap->mcnt)
  111. {
  112. memset(&auth, 0, sizeof(auth));
  113. auth.oa_flavor = 1;
  114. auth.oa_base = (char *)auth_base;
  115. auth.oa_length = (auth_buf - auth_base) * sizeof(rt_uint32_t);
  116. ap->no_client.ah_cred = auth;
  117. ap->no_client.ah_verf = _null_auth;
  118. ap->no_client.ah_ops = &ops;
  119. xdrs = &xdr_stream;
  120. xdrmem_create(xdrs, ap->marshalled_client,
  121. (unsigned int) MAX_MARSHEL_SIZE, XDR_ENCODE);
  122. (void) xdr_opaque_auth(xdrs, &ap->no_client.ah_cred);
  123. (void) xdr_opaque_auth(xdrs, &ap->no_client.ah_verf);
  124. ap->mcnt = XDR_GETPOS(xdrs);
  125. XDR_DESTROY(xdrs);
  126. }
  127. rt_free(auth_base);
  128. return (&ap->no_client);
  129. }
  130. /*ARGSUSED*/
  131. static bool_t authnone_marshal(AUTH *client, XDR *xdrs)
  132. {
  133. register struct authnone_private *ap = authnone_private;
  134. if (ap == 0)
  135. return (0);
  136. return ((*xdrs->x_ops->x_putbytes)(xdrs,
  137. ap->marshalled_client, ap->mcnt));
  138. }
  139. static void authnone_verf(AUTH *x)
  140. {
  141. }
  142. static bool_t authnone_validate(AUTH *x, struct opaque_auth *x1)
  143. {
  144. return (TRUE);
  145. }
  146. static bool_t authnone_refresh(AUTH *x)
  147. {
  148. return (FALSE);
  149. }
  150. static void authnone_destroy(AUTH *x)
  151. {
  152. }