iPXE
oncrpc_iob.c
Go to the documentation of this file.
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 
20 FILE_SECBOOT ( FORBIDDEN );
21 
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <byteswap.h>
29 #include <ipxe/socket.h>
30 #include <ipxe/tcpip.h>
31 #include <ipxe/in.h>
32 #include <ipxe/iobuf.h>
33 #include <ipxe/xfer.h>
34 #include <ipxe/open.h>
35 #include <ipxe/uri.h>
36 #include <ipxe/features.h>
37 #include <ipxe/oncrpc.h>
38 #include <ipxe/oncrpc_iob.h>
39 
40 /** @file
41  *
42  * SUN ONC RPC protocol
43  *
44  */
45 
46 size_t oncrpc_iob_add_fields ( struct io_buffer *io_buf,
47  const struct oncrpc_field fields[] ) {
48  size_t i;
49  size_t s = 0;
50 
51  struct oncrpc_field f;
52 
53  if ( ! io_buf )
54  return 0;
55 
56  for ( i = 0; fields[i].type != oncrpc_none; i++ ) {
57  f = fields[i];
58  switch ( f.type ) {
59  case oncrpc_int32:
60  s += oncrpc_iob_add_int ( io_buf, f.value.int32 );
61  break;
62 
63  case oncrpc_int64:
64  s += oncrpc_iob_add_int64 ( io_buf, f.value.int64 );
65  break;
66 
67  case oncrpc_str:
68  s += oncrpc_iob_add_string ( io_buf, f.value.str );
69  break;
70 
71  case oncrpc_array:
72  s += oncrpc_iob_add_array ( io_buf,
73  f.value.array.length,
74  f.value.array.ptr );
75  break;
76 
77  case oncrpc_intarray:
78  s += oncrpc_iob_add_intarray ( io_buf,
80  f.value.intarray.ptr );
81  break;
82 
83  case oncrpc_cred:
84  s += oncrpc_iob_add_cred ( io_buf, f.value.cred);
85  break;
86 
87  default:
88  return s;
89  }
90  }
91 
92  return s;
93 }
94 
95 /**
96  * Add an array of bytes to the end of an I/O buffer
97  *
98  * @v io_buf I/O buffer
99  * @v val String
100  * @ret size Size of the data written
101  *
102  * In the ONC RPC protocol, every data is four byte paded, we add padding when
103  * necessary by using oncrpc_align()
104  */
105 size_t oncrpc_iob_add_array ( struct io_buffer *io_buf, size_t length,
106  const void *data ) {
107  size_t padding = oncrpc_align ( length ) - length;
108 
109  oncrpc_iob_add_int ( io_buf, length );
110  memcpy ( iob_put ( io_buf, length ), data, length );
111  memset ( iob_put ( io_buf, padding ), 0, padding );
112 
113  return length + padding + sizeof ( uint32_t );
114 }
115 
116 /**
117  * Add an int array to the end of an I/O buffer
118  *
119  * @v io_buf I/O buffer
120  * @v length Length od the array
121  * @v val Int array
122  * @ret size Size of the data written
123  */
124 size_t oncrpc_iob_add_intarray ( struct io_buffer *io_buf, size_t length,
125  const uint32_t *array ) {
126  size_t i;
127 
128  oncrpc_iob_add_int ( io_buf, length );
129 
130  for ( i = 0; i < length; ++i )
131  oncrpc_iob_add_int ( io_buf, array[i] );
132 
133  return ( ( length + 1 ) * sizeof ( uint32_t ) );
134 }
135 
136 /**
137  * Add credential information to the end of an I/O buffer
138  *
139  * @v io_buf I/O buffer
140  * @v cred Credential information
141  * @ret size Size of the data written
142  */
143 size_t oncrpc_iob_add_cred ( struct io_buffer *io_buf,
144  const struct oncrpc_cred *cred ) {
145  struct oncrpc_cred_sys *syscred;
146  size_t s;
147 
148  struct oncrpc_field credfields[] = {
149  ONCRPC_FIELD ( int32, cred->flavor ),
150  ONCRPC_FIELD ( int32, cred->length ),
152  };
153 
154  if ( ! io_buf || ! cred )
155  return 0;
156 
157  s = oncrpc_iob_add_fields ( io_buf, credfields);
158 
159  switch ( cred->flavor ) {
160  case ONCRPC_AUTH_NONE:
161  break;
162 
163  case ONCRPC_AUTH_SYS:
164  syscred = container_of ( cred, struct oncrpc_cred_sys,
165  credential );
166 
167  struct oncrpc_field syscredfields[] = {
168  ONCRPC_FIELD ( int32, syscred->stamp ),
169  ONCRPC_FIELD ( str, syscred->hostname ),
170  ONCRPC_FIELD ( int32, syscred->uid ),
171  ONCRPC_FIELD ( int32, syscred->gid ),
172  ONCRPC_SUBFIELD ( intarray, syscred->aux_gid_len,
173  syscred->aux_gid ),
175  };
176 
177  s += oncrpc_iob_add_fields ( io_buf, syscredfields );
178  break;
179  }
180 
181  return s;
182 }
183 
184 /**
185  * Get credential information from the beginning of an I/O buffer
186  *
187  * @v io_buf I/O buffer
188  * @v cred Struct where the information will be saved
189  * @ret size Size of the data read
190  */
191 size_t oncrpc_iob_get_cred ( struct io_buffer *io_buf,
192  struct oncrpc_cred *cred ) {
193  if ( cred == NULL )
194  return * ( uint32_t * ) io_buf->data;
195 
196  cred->flavor = oncrpc_iob_get_int ( io_buf );
197  cred->length = oncrpc_iob_get_int ( io_buf );
198 
199  iob_pull ( io_buf, cred->length );
200 
201  return ( 2 * sizeof ( uint32_t ) + cred->length );
202 }
#define ONCRPC_FIELD(type, value)
Definition: oncrpc.h:28
#define iob_pull(iobuf, len)
Definition: iobuf.h:107
size_t oncrpc_iob_add_fields(struct io_buffer *io_buf, const struct oncrpc_field fields[])
Definition: oncrpc_iob.c:46
u16 length
Definition: sky2.h:9
uint32_t uid
Definition: oncrpc.h:55
#define ONCRPC_AUTH_SYS
ONC RPC System Authentication (also called UNIX Authentication)
Definition: oncrpc.h:23
#define iob_put(iobuf, len)
Definition: iobuf.h:125
uint32_t gid
Definition: oncrpc.h:56
size_t oncrpc_iob_add_intarray(struct io_buffer *io_buf, size_t length, const uint32_t *array)
Add an int array to the end of an I/O buffer.
Definition: oncrpc_iob.c:124
#define oncrpc_align(size)
Enusure that size is a multiple of four.
Definition: oncrpc.h:35
Error codes.
#define ONCRPC_AUTH_NONE
ONC RPC Null Authentication.
Definition: oncrpc.h:20
I/O buffers.
size_t length
Definition: oncrpc.h:92
uint32_t aux_gid_len
Definition: oncrpc.h:57
Uniform Resource Identifiers.
Data transfer interfaces.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define oncrpc_iob_add_string(buf, str)
Add a string to the end of an I/O buffer.
Definition: oncrpc_iob.h:25
#define oncrpc_iob_get_int(buf)
Get a 32 bits integer from the beginning of an I/O buffer.
Definition: oncrpc_iob.h:38
static size_t oncrpc_iob_add_int64(struct io_buffer *io_buf, uint64_t val)
Add a 64 bits integer to the end of an I/O buffer.
Definition: oncrpc_iob.h:96
uint32_t array
Array number.
Definition: edd.h:31
int32_t int32
Definition: stdint.h:32
Assertions.
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:36
union oncrpc_field_value value
Definition: oncrpc.h:109
enum oncrpc_field_type type
Definition: oncrpc.h:108
FILE_SECBOOT(FORBIDDEN)
Transport-network layer interface.
Feature list.
struct oncrpc_field_value::@645 intarray
static size_t oncrpc_iob_add_int(struct io_buffer *io_buf, uint32_t val)
Add a 32 bits integer to the end of an I/O buffer.
Definition: oncrpc_iob.h:83
size_t oncrpc_iob_add_array(struct io_buffer *io_buf, size_t length, const void *data)
Add an array of bytes to the end of an I/O buffer.
Definition: oncrpc_iob.c:105
uint32_t flavor
Definition: oncrpc.h:47
SUN ONC RPC protocol.
#define ONCRPC_SUBFIELD(type, args...)
Definition: oncrpc.h:29
char * hostname
Definition: oncrpc.h:54
struct oncrpc_field_value::@644 array
Data transfer interface opening.
unsigned int uint32_t
Definition: stdint.h:12
int32_t int32
Definition: oncrpc.h:102
const void * ptr
Definition: oncrpc.h:93
#define ONCRPC_FIELD_END
Definition: oncrpc.h:32
void * data
Start of data.
Definition: iobuf.h:53
uint32_t stamp
Definition: oncrpc.h:53
size_t oncrpc_iob_get_cred(struct io_buffer *io_buf, struct oncrpc_cred *cred)
Get credential information from the beginning of an I/O buffer.
Definition: oncrpc_iob.c:191
size_t oncrpc_iob_add_cred(struct io_buffer *io_buf, const struct oncrpc_cred *cred)
Add credential information to the end of an I/O buffer.
Definition: oncrpc_iob.c:143
const char * str
Definition: oncrpc.h:103
uint8_t data[48]
Additional event data.
Definition: ena.h:22
int64_t int64
Definition: oncrpc.h:101
uint32_t length
Definition: oncrpc.h:48
Socket addresses.
uint32_t aux_gid[16]
Definition: oncrpc.h:58
SUN ONC RPC protocol.
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
const struct oncrpc_cred * cred
Definition: oncrpc.h:104
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:38