summaryrefslogtreecommitdiffstats
path: root/src/net/oncrpc/oncrpc_iob.c
blob: be51805e7007fbba63029390895728a245739d42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (C) 2013 Marin Hannache <ipxe@mareo.fr>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/socket.h>
#include <ipxe/tcpip.h>
#include <ipxe/in.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/uri.h>
#include <ipxe/features.h>
#include <ipxe/oncrpc.h>
#include <ipxe/oncrpc_iob.h>

/** @file
 *
 * SUN ONC RPC protocol
 *
 */

size_t oncrpc_iob_add_fields ( struct io_buffer *io_buf,
                               const struct oncrpc_field fields[] ) {
	size_t i;
	size_t s = 0;

	struct oncrpc_field f;

	if ( ! io_buf )
		return 0;

	for ( i = 0; fields[i].type != oncrpc_none; i++ ) {
		f = fields[i];
		switch ( f.type ) {
		case oncrpc_int32:
			s += oncrpc_iob_add_int ( io_buf, f.value.int32 );
			break;

		case oncrpc_int64:
			s += oncrpc_iob_add_int64 ( io_buf, f.value.int64 );
			break;

		case oncrpc_str:
			s += oncrpc_iob_add_string ( io_buf, f.value.str );
			break;

		case oncrpc_array:
			s += oncrpc_iob_add_array ( io_buf,
			                            f.value.array.length,
			                            f.value.array.ptr );
			break;

		case oncrpc_intarray:
			s += oncrpc_iob_add_intarray ( io_buf,
			                               f.value.intarray.length,
			                               f.value.intarray.ptr );
			break;

		case oncrpc_cred:
			s += oncrpc_iob_add_cred ( io_buf, f.value.cred);
			break;

		default:
			return s;
		}
	}

	return s;
}

/**
 * Add an array of bytes to the end of an I/O buffer
 *
 * @v io_buf            I/O buffer
 * @v val               String
 * @ret size            Size of the data written
 *
 * In the ONC RPC protocol, every data is four byte paded, we add padding when
 * necessary by using oncrpc_align()
 */
size_t oncrpc_iob_add_array ( struct io_buffer *io_buf, size_t length,
                              const void *data ) {
	size_t padding = oncrpc_align ( length ) - length;

	oncrpc_iob_add_int ( io_buf, length );
	memcpy ( iob_put ( io_buf, length ), data, length );
	memset ( iob_put ( io_buf, padding ), 0, padding );

	return length + padding + sizeof ( uint32_t );
}

/**
 * Add an int array to the end of an I/O buffer
 *
 * @v io_buf            I/O buffer
 * @v length            Length od the array
 * @v val               Int array
 * @ret size            Size of the data written
 */
size_t oncrpc_iob_add_intarray ( struct io_buffer *io_buf, size_t length,
                                 const uint32_t *array ) {
	size_t                  i;

	oncrpc_iob_add_int ( io_buf, length );

	for ( i = 0; i < length; ++i )
		oncrpc_iob_add_int ( io_buf, array[i] );

	return ( ( length + 1 ) * sizeof ( uint32_t ) );
}

/**
 * Add credential information to the end of an I/O buffer
 *
 * @v io_buf            I/O buffer
 * @v cred              Credential information
 * @ret size            Size of the data written
 */
size_t oncrpc_iob_add_cred ( struct io_buffer *io_buf,
                             const struct oncrpc_cred *cred ) {
	struct oncrpc_cred_sys  *syscred;
	size_t                  s;

	struct oncrpc_field credfields[] = {
		ONCRPC_FIELD ( int32, cred->flavor ),
		ONCRPC_FIELD ( int32, cred->length ),
		ONCRPC_FIELD_END,
	};

	if ( ! io_buf || ! cred )
		return 0;

	s  = oncrpc_iob_add_fields ( io_buf, credfields);

	switch ( cred->flavor ) {
	case ONCRPC_AUTH_NONE:
		break;

	case ONCRPC_AUTH_SYS:
		syscred = container_of ( cred, struct oncrpc_cred_sys,
		                         credential );

		struct oncrpc_field syscredfields[] = {
			ONCRPC_FIELD ( int32, syscred->stamp ),
			ONCRPC_FIELD ( str, syscred->hostname ),
			ONCRPC_FIELD ( int32, syscred->uid ),
			ONCRPC_FIELD ( int32, syscred->gid ),
			ONCRPC_SUBFIELD ( intarray, syscred->aux_gid_len,
			                  syscred->aux_gid ),
			ONCRPC_FIELD_END,
		};

		s += oncrpc_iob_add_fields ( io_buf, syscredfields );
		break;
	}

	return s;
}

/**
 * Get credential information from the beginning of an I/O buffer
 *
 * @v io_buf            I/O buffer
 * @v cred              Struct where the information will be saved
 * @ret size            Size of the data read
 */
size_t oncrpc_iob_get_cred ( struct io_buffer *io_buf,
                             struct oncrpc_cred *cred ) {
	if ( cred == NULL )
		return * ( uint32_t * ) io_buf->data;

	cred->flavor = oncrpc_iob_get_int ( io_buf );
	cred->length = oncrpc_iob_get_int ( io_buf );

	iob_pull ( io_buf, cred->length );

	return ( 2 * sizeof ( uint32_t ) + cred->length );
}