You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

oncrpc_iob.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2013 Marin Hannache <ipxe@mareo.fr>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include <byteswap.h>
  26. #include <ipxe/socket.h>
  27. #include <ipxe/tcpip.h>
  28. #include <ipxe/in.h>
  29. #include <ipxe/iobuf.h>
  30. #include <ipxe/xfer.h>
  31. #include <ipxe/open.h>
  32. #include <ipxe/uri.h>
  33. #include <ipxe/features.h>
  34. #include <ipxe/oncrpc.h>
  35. #include <ipxe/oncrpc_iob.h>
  36. /** @file
  37. *
  38. * SUN ONC RPC protocol
  39. *
  40. */
  41. size_t oncrpc_iob_add_fields ( struct io_buffer *io_buf,
  42. const struct oncrpc_field fields[] ) {
  43. size_t i;
  44. size_t s = 0;
  45. struct oncrpc_field f;
  46. if ( ! io_buf )
  47. return 0;
  48. for ( i = 0; fields[i].type != oncrpc_none; i++ ) {
  49. f = fields[i];
  50. switch ( f.type ) {
  51. case oncrpc_int32:
  52. s += oncrpc_iob_add_int ( io_buf, f.value.int32 );
  53. break;
  54. case oncrpc_int64:
  55. s += oncrpc_iob_add_int64 ( io_buf, f.value.int64 );
  56. break;
  57. case oncrpc_str:
  58. s += oncrpc_iob_add_string ( io_buf, f.value.str );
  59. break;
  60. case oncrpc_array:
  61. s += oncrpc_iob_add_array ( io_buf,
  62. f.value.array.length,
  63. f.value.array.ptr );
  64. break;
  65. case oncrpc_intarray:
  66. s += oncrpc_iob_add_intarray ( io_buf,
  67. f.value.intarray.length,
  68. f.value.intarray.ptr );
  69. break;
  70. case oncrpc_cred:
  71. s += oncrpc_iob_add_cred ( io_buf, f.value.cred);
  72. break;
  73. default:
  74. return s;
  75. }
  76. }
  77. return s;
  78. }
  79. /**
  80. * Add an array of bytes to the end of an I/O buffer
  81. *
  82. * @v io_buf I/O buffer
  83. * @v val String
  84. * @ret size Size of the data written
  85. *
  86. * In the ONC RPC protocol, every data is four byte paded, we add padding when
  87. * necessary by using oncrpc_align()
  88. */
  89. size_t oncrpc_iob_add_array ( struct io_buffer *io_buf, size_t length,
  90. const void *data ) {
  91. size_t padding = oncrpc_align ( length ) - length;
  92. oncrpc_iob_add_int ( io_buf, length );
  93. memcpy ( iob_put ( io_buf, length ), data, length );
  94. memset ( iob_put ( io_buf, padding ), 0, padding );
  95. return length + padding + sizeof ( uint32_t );
  96. }
  97. /**
  98. * Add an int array to the end of an I/O buffer
  99. *
  100. * @v io_buf I/O buffer
  101. * @v length Length od the array
  102. * @v val Int array
  103. * @ret size Size of the data written
  104. */
  105. size_t oncrpc_iob_add_intarray ( struct io_buffer *io_buf, size_t length,
  106. const uint32_t *array ) {
  107. size_t i;
  108. oncrpc_iob_add_int ( io_buf, length );
  109. for ( i = 0; i < length; ++i )
  110. oncrpc_iob_add_int ( io_buf, array[i] );
  111. return ( ( length + 1 ) * sizeof ( uint32_t ) );
  112. }
  113. /**
  114. * Add credential information to the end of an I/O buffer
  115. *
  116. * @v io_buf I/O buffer
  117. * @v cred Credential information
  118. * @ret size Size of the data written
  119. */
  120. size_t oncrpc_iob_add_cred ( struct io_buffer *io_buf,
  121. const struct oncrpc_cred *cred ) {
  122. struct oncrpc_cred_sys *syscred;
  123. size_t s;
  124. struct oncrpc_field credfields[] = {
  125. ONCRPC_FIELD ( int32, cred->flavor ),
  126. ONCRPC_FIELD ( int32, cred->length ),
  127. ONCRPC_FIELD_END,
  128. };
  129. if ( ! io_buf || ! cred )
  130. return 0;
  131. s = oncrpc_iob_add_fields ( io_buf, credfields);
  132. switch ( cred->flavor ) {
  133. case ONCRPC_AUTH_NONE:
  134. break;
  135. case ONCRPC_AUTH_SYS:
  136. syscred = container_of ( cred, struct oncrpc_cred_sys,
  137. credential );
  138. struct oncrpc_field syscredfields[] = {
  139. ONCRPC_FIELD ( int32, syscred->stamp ),
  140. ONCRPC_FIELD ( str, syscred->hostname ),
  141. ONCRPC_FIELD ( int32, syscred->uid ),
  142. ONCRPC_FIELD ( int32, syscred->gid ),
  143. ONCRPC_SUBFIELD ( intarray, syscred->aux_gid_len,
  144. syscred->aux_gid ),
  145. ONCRPC_FIELD_END,
  146. };
  147. s += oncrpc_iob_add_fields ( io_buf, syscredfields );
  148. break;
  149. }
  150. return s;
  151. }
  152. /**
  153. * Get credential information from the beginning of an I/O buffer
  154. *
  155. * @v io_buf I/O buffer
  156. * @v cred Struct where the information will be saved
  157. * @ret size Size of the data read
  158. */
  159. size_t oncrpc_iob_get_cred ( struct io_buffer *io_buf,
  160. struct oncrpc_cred *cred ) {
  161. if ( cred == NULL )
  162. return * ( uint32_t * ) io_buf->data;
  163. cred->flavor = oncrpc_iob_get_int ( io_buf );
  164. cred->length = oncrpc_iob_get_int ( io_buf );
  165. iob_pull ( io_buf, cred->length );
  166. return ( 2 * sizeof ( uint32_t ) + cred->length );
  167. }