iPXE
oncrpc.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 #include <stdint.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <byteswap.h>
27 #include <ipxe/socket.h>
28 #include <ipxe/tcpip.h>
29 #include <ipxe/in.h>
30 #include <ipxe/iobuf.h>
31 #include <ipxe/dhcp.h>
32 #include <ipxe/xfer.h>
33 #include <ipxe/open.h>
34 #include <ipxe/uri.h>
35 #include <ipxe/features.h>
36 #include <ipxe/oncrpc.h>
37 #include <ipxe/oncrpc_iob.h>
38 #include <ipxe/init.h>
39 #include <ipxe/settings.h>
40 #include <ipxe/version.h>
41 
42 /** @file
43  *
44  * SUN ONC RPC protocol
45  *
46  */
47 
48 /** Set most significant bit to 1. */
49 #define SET_LAST_FRAME( x ) ( (x) | 1 << 31 )
50 #define GET_FRAME_SIZE( x ) ( (x) & ~( 1 << 31 ) )
51 
52 #define ONCRPC_CALL 0
53 #define ONCRPC_REPLY 1
54 
55 /** AUTH NONE authentication flavor */
58  .length = 0
59 };
60 
61 const struct setting uid_setting __setting ( SETTING_AUTH, uid ) = {
62  .name = "uid",
63  .description = "User ID",
64  .tag = DHCP_EB_UID,
65  .type = &setting_type_uint32
66 };
67 
68 const struct setting gid_setting __setting ( SETTING_AUTH, gid ) = {
69  .name = "gid",
70  .description = "Group ID",
71  .tag = DHCP_EB_GID,
72  .type = &setting_type_uint32
73 };
74 
75 /**
76  * Initialize an ONC RPC AUTH SYS credential structure
77  *
78  * @v auth_sys The structure to initialize
79  *
80  * The hostname field is filled with the value of the hostname setting, if the
81  * hostname setting is empty, PRODUCT_SHORT_NAME (usually "iPXE") is used
82  * instead.
83  */
84 int oncrpc_init_cred_sys ( struct oncrpc_cred_sys *auth_sys ) {
85  if ( ! auth_sys )
86  return -EINVAL;
87 
88  fetch_string_setting_copy ( NULL, &hostname_setting,
89  &auth_sys->hostname );
90  if ( ! auth_sys->hostname )
91  if ( ! ( auth_sys->hostname = strdup ( product_short_name ) ) )
92  return -ENOMEM;
93 
94  auth_sys->uid = fetch_uintz_setting ( NULL, &uid_setting );
95  auth_sys->gid = fetch_uintz_setting ( NULL, &uid_setting );
96  auth_sys->aux_gid_len = 0;
97  auth_sys->stamp = 0;
98 
99  auth_sys->credential.flavor = ONCRPC_AUTH_SYS;
100  auth_sys->credential.length = 16 +
101  oncrpc_strlen ( auth_sys->hostname );
102 
103  return 0;
104 }
105 
106 /**
107  * Prepare an ONC RPC session structure to be used by the ONC RPC layer
108  *
109  * @v session ONC RPC session
110  * @v credential Credential structure pointer
111  * @v verifier Verifier structure pointer
112  * @v prog_name ONC RPC program number
113  * @v prog_vers ONC RPC program version number
114  */
116  struct oncrpc_cred *credential,
117  struct oncrpc_cred *verifier, uint32_t prog_name,
118  uint32_t prog_vers ) {
119  if ( ! session )
120  return;
121 
122  session->rpc_id = rand();
123  session->credential = credential;
124  session->verifier = verifier;
125  session->prog_name = prog_name;
126  session->prog_vers = prog_vers;
127 }
128 
129 int oncrpc_call ( struct interface *intf, struct oncrpc_session *session,
130  uint32_t proc_name, const struct oncrpc_field fields[] ) {
131  size_t frame_size;
132  struct io_buffer *io_buf;
133 
134  if ( ! session )
135  return -EINVAL;
136 
137  struct oncrpc_field header[] = {
138  ONCRPC_FIELD ( int32, 0 ),
139  ONCRPC_FIELD ( int32, ++session->rpc_id ),
142  ONCRPC_FIELD ( int32, session->prog_name ),
143  ONCRPC_FIELD ( int32, session->prog_vers ),
144  ONCRPC_FIELD ( int32, proc_name ),
145  ONCRPC_FIELD ( cred, session->credential ),
146  ONCRPC_FIELD ( cred, session->verifier ),
148  };
149 
150  frame_size = oncrpc_compute_size ( header );
151  frame_size += oncrpc_compute_size ( fields );
152 
153  io_buf = alloc_iob ( frame_size );
154  if ( ! io_buf )
155  return -ENOBUFS;
156 
157  header[0].value.int32 = SET_LAST_FRAME ( frame_size -
158  sizeof ( uint32_t ) );
159 
160  oncrpc_iob_add_fields ( io_buf, header );
161  oncrpc_iob_add_fields ( io_buf, fields );
162 
163  return xfer_deliver_iob ( intf, iob_disown ( io_buf ) );
164 }
165 
166 size_t oncrpc_compute_size ( const struct oncrpc_field fields[] ) {
167 
168  size_t i;
169  size_t size = 0;
170 
171  for ( i = 0; fields[i].type != oncrpc_none; i++ ) {
172  switch ( fields[i].type ) {
173  case oncrpc_int32:
174  size += sizeof ( uint32_t );
175  break;
176 
177  case oncrpc_int64:
178  size += sizeof ( uint64_t );
179  break;
180 
181  case oncrpc_str:
182  size += oncrpc_strlen ( fields[i].value.str );
183  break;
184 
185  case oncrpc_array:
186  size += oncrpc_align ( fields[i].value.array.length );
187  size += sizeof ( uint32_t );
188  break;
189 
190  case oncrpc_intarray:
191  size += sizeof ( uint32_t ) *
192  fields[i].value.intarray.length;
193  size += sizeof ( uint32_t );
194  break;
195 
196  case oncrpc_cred:
197  size += fields[i].value.cred->length;
198  size += 2 * sizeof ( uint32_t );
199  break;
200 
201  default:
202  return size;
203  }
204  }
205 
206  return size;
207 }
208 
209 /**
210  * Parse an I/O buffer to extract a ONC RPC REPLY
211  * @v session ONC RPC session
212  * @v reply Reply structure where data will be saved
213  * @v io_buf I/O buffer
214  */
216  struct oncrpc_reply *reply, struct io_buffer *io_buf ) {
217  if ( ! reply || ! io_buf )
218  return -EINVAL;
219 
220  reply->frame_size = GET_FRAME_SIZE ( oncrpc_iob_get_int ( io_buf ) );
221  reply->rpc_id = oncrpc_iob_get_int ( io_buf );
222 
223  /* iPXE has no support for handling ONC RPC call */
224  if ( oncrpc_iob_get_int ( io_buf ) != ONCRPC_REPLY )
225  return -ENOSYS;
226 
227  reply->reply_state = oncrpc_iob_get_int ( io_buf );
228 
229  if ( reply->reply_state == 0 )
230  {
231  /* verifier.flavor */
232  oncrpc_iob_get_int ( io_buf );
233  /* verifier.length */
234  iob_pull ( io_buf, oncrpc_iob_get_int ( io_buf ));
235 
236  /* We don't use the verifier in iPXE, let it be an empty
237  verifier. */
238  reply->verifier = &oncrpc_auth_none;
239  }
240 
241  reply->accept_state = oncrpc_iob_get_int ( io_buf );
242  reply->data = io_buf;
243 
244  return 0;
245 }
const char product_short_name[]
Product short name string.
Definition: version.c:75
#define ONCRPC_FIELD(type, value)
Definition: oncrpc.h:28
#define iob_pull(iobuf, len)
Definition: iobuf.h:98
size_t oncrpc_iob_add_fields(struct io_buffer *io_buf, const struct oncrpc_field fields[])
Definition: oncrpc_iob.c:44
#define EINVAL
Invalid argument.
Definition: errno.h:428
uint32_t uid
Definition: oncrpc.h:55
#define ONCRPC_AUTH_SYS
ONC RPC System Authentication (also called UNIX Authentication)
Definition: oncrpc.h:23
struct oncrpc_cred * verifier
Definition: oncrpc.h:63
#define oncrpc_strlen(str)
Calculate the length of a string, including padding bytes.
Definition: oncrpc.h:43
Dynamic Host Configuration Protocol.
#define ONCRPC_VERS
ONC RCP Version.
Definition: oncrpc.h:17
uint32_t gid
Definition: oncrpc.h:56
const struct setting uid_setting __setting(SETTING_AUTH, uid)
int xfer_deliver_iob(struct interface *intf, struct io_buffer *iobuf)
Deliver datagram as I/O buffer without metadata.
Definition: xfer.c:254
#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.
uint8_t type
Type.
Definition: ena.h:16
#define SETTING_AUTH
Authentication settings.
Definition: settings.h:77
uint32_t aux_gid_len
Definition: oncrpc.h:57
unsigned long long uint64_t
Definition: stdint.h:13
#define SET_LAST_FRAME(x)
Set most significant bit to 1.
Definition: oncrpc.c:49
uint32_t accept_state
Definition: oncrpc.h:66
void oncrpc_init_session(struct oncrpc_session *session, struct oncrpc_cred *credential, struct oncrpc_cred *verifier, uint32_t prog_name, uint32_t prog_vers)
Prepare an ONC RPC session structure to be used by the ONC RPC layer.
Definition: oncrpc.c:115
Uniform Resource Identifiers.
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:128
int oncrpc_call(struct interface *intf, struct oncrpc_session *session, uint32_t proc_name, const struct oncrpc_field fields[])
Definition: oncrpc.c:129
Data transfer interfaces.
const char * name
Name.
Definition: settings.h:28
#define DHCP_EB_GID
Group ID.
Definition: dhcp.h:502
#define ENOMEM
Not enough space.
Definition: errno.h:534
uint32_t rpc_id
Definition: oncrpc.h:64
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:208
#define ONCRPC_REPLY
Definition: oncrpc.c:53
#define oncrpc_iob_get_int(buf)
Get a 32 bits integer from the beginning of an I/O buffer.
Definition: oncrpc_iob.h:38
uint32_t frame_size
Definition: oncrpc.h:67
int32_t int32
Definition: stdint.h:31
Assertions.
An object interface.
Definition: interface.h:109
static int rand(void)
Definition: stdlib.h:59
union oncrpc_field_value value
Definition: oncrpc.h:109
struct ntlm_data session
Session key.
Definition: ntlm.h:24
int oncrpc_init_cred_sys(struct oncrpc_cred_sys *auth_sys)
Initialize an ONC RPC AUTH SYS credential structure.
Definition: oncrpc.c:84
enum oncrpc_field_type type
Definition: oncrpc.h:108
Transport-network layer interface.
Feature list.
int fetch_string_setting_copy(struct settings *settings, const struct setting *setting, char **data)
Fetch value of string setting.
Definition: settings.c:877
int oncrpc_get_reply(struct oncrpc_session *session __unused, struct oncrpc_reply *reply, struct io_buffer *io_buf)
Parse an I/O buffer to extract a ONC RPC REPLY.
Definition: oncrpc.c:215
uint32_t flavor
Definition: oncrpc.h:47
size_t oncrpc_compute_size(const struct oncrpc_field fields[])
Definition: oncrpc.c:166
SUN ONC RPC protocol.
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
Configuration settings.
struct oncrpc_cred oncrpc_auth_none
AUTH NONE authentication flavor.
Definition: oncrpc.c:56
char * strdup(const char *src)
Duplicate string.
Definition: string.c:350
char * hostname
Definition: oncrpc.h:54
Data transfer interface opening.
unsigned long fetch_uintz_setting(struct settings *settings, const struct setting *setting)
Fetch value of unsigned integer setting, or zero.
Definition: settings.c:1072
Version number.
unsigned int uint32_t
Definition: stdint.h:12
A setting.
Definition: settings.h:23
uint32_t reply_state
Definition: oncrpc.h:65
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
#define GET_FRAME_SIZE(x)
Definition: oncrpc.c:50
#define ENOBUFS
No buffer space available.
Definition: errno.h:498
#define ONCRPC_FIELD_END
Definition: oncrpc.h:32
struct ena_aq_header header
Header.
Definition: ena.h:12
uint32_t stamp
Definition: oncrpc.h:53
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
#define ONCRPC_CALL
Definition: oncrpc.c:52
#define ENOSYS
Function not implemented.
Definition: errno.h:564
uint32_t length
Definition: oncrpc.h:48
Socket addresses.
u8 gid[16]
Definition: CIB_PRM.h:31
SUN ONC RPC protocol.
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct oncrpc_cred credential
Definition: oncrpc.h:52
String functions.
const struct oncrpc_cred * cred
Definition: oncrpc.h:104
#define DHCP_EB_UID
User ID.
Definition: dhcp.h:496
struct io_buffer * data
Definition: oncrpc.h:68
A persistent I/O buffer.
Definition: iobuf.h:32