2 Copyright (C) 2012 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * This file contains definitions for the built in ZDR implementation.
19 * This is a very limited ZDR subset that can only marshal to/from a momory buffer,
20 * i.e. zdrmem_create() buffers.
21 * It aims to be compatible with normal rpcgen generated functions.
28 #include "win32_compat.h"
32 #include "aros_compat.h"
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
43 #include "libnfs-zdr.h"
45 struct opaque_auth _null_auth
;
47 bool_t
libnfs_zdr_setpos(ZDR
*zdrs
, uint32_t pos
)
54 uint32_t libnfs_zdr_getpos(ZDR
*zdrs
)
59 void libnfs_zdrmem_create(ZDR
*zdrs
, const caddr_t addr
, uint32_t size
, enum zdr_op xop
)
68 static void *zdr_malloc(ZDR
*zdrs
, uint32_t size
)
72 mem
= malloc(sizeof(struct zdr_mem
));
73 mem
->next
= zdrs
->mem
;
75 mem
->buf
= malloc(mem
->size
);
81 void libnfs_zdr_destroy(ZDR
*zdrs
)
83 while (zdrs
->mem
!= NULL
) {
84 struct zdr_mem
*mem
= zdrs
->mem
->next
;
91 bool_t
libnfs_zdr_u_int(ZDR
*zdrs
, uint32_t *u
)
93 if (zdrs
->pos
+ 4 > zdrs
->size
) {
99 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl(*u
);
104 *u
= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
113 bool_t
libnfs_zdr_int(ZDR
*zdrs
, int32_t *i
)
115 return libnfs_zdr_u_int(zdrs
, (uint32_t *)i
);
118 bool_t
libnfs_zdr_u_quad_t(ZDR
*zdrs
, uint64_t *u
)
120 if (zdrs
->pos
+ 8 > zdrs
->size
) {
124 switch (zdrs
->x_op
) {
126 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl((*u
>> 32));
128 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl((*u
& 0xffffffff));
133 *u
= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
136 *u
|= (uint32_t)ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
145 bool_t
libnfs_zdr_quad_t(ZDR
*zdrs
, int64_t *i
)
147 return libnfs_zdr_u_quad_t(zdrs
, (uint64_t *)i
);
150 bool_t
libnfs_zdr_bytes(ZDR
*zdrs
, char **bufp
, uint32_t *size
, uint32_t maxsize
)
152 if (!libnfs_zdr_u_int(zdrs
, size
)) {
156 if (zdrs
->pos
+ *size
> zdrs
->size
) {
160 switch (zdrs
->x_op
) {
162 memcpy(&zdrs
->buf
[zdrs
->pos
], *bufp
, *size
);
164 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
168 *bufp
= zdr_malloc(zdrs
, *size
);
170 memcpy(*bufp
, &zdrs
->buf
[zdrs
->pos
], *size
);
172 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
180 bool_t
libnfs_zdr_enum(ZDR
*zdrs
, enum_t
*e
)
185 ret
= libnfs_zdr_u_int(zdrs
, (uint32_t *)&i
);
191 bool_t
libnfs_zdr_bool(ZDR
*zdrs
, bool_t
*b
)
193 return libnfs_zdr_u_int(zdrs
, (uint32_t *)b
);
196 bool_t
libnfs_zdr_void(void)
201 bool_t
libnfs_zdr_pointer(ZDR
*zdrs
, char **objp
, uint32_t size
, zdrproc_t proc
)
205 more_data
= (*objp
!= NULL
);
207 if (!libnfs_zdr_bool(zdrs
, &more_data
)) {
210 if (more_data
== 0) {
215 if (zdrs
->x_op
== ZDR_DECODE
) {
216 *objp
= zdr_malloc(zdrs
, size
);
220 memset(*objp
, 0, size
);
222 return proc(zdrs
, *objp
);
225 bool_t
libnfs_zdr_opaque(ZDR
*zdrs
, char *objp
, uint32_t size
)
227 switch (zdrs
->x_op
) {
229 memcpy(&zdrs
->buf
[zdrs
->pos
], objp
, size
);
232 memset(&zdrs
->buf
[zdrs
->pos
], 0x00, 4 - (zdrs
->pos
& 3));
234 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
237 memcpy(objp
, &zdrs
->buf
[zdrs
->pos
], size
);
239 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
246 bool_t
libnfs_zdr_string(ZDR
*zdrs
, char **strp
, uint32_t maxsize
)
250 if (zdrs
->x_op
== ZDR_ENCODE
) {
251 size
= strlen(*strp
);
254 if (!libnfs_zdr_u_int(zdrs
, &size
)) {
258 if (zdrs
->pos
+ size
> zdrs
->size
) {
262 switch (zdrs
->x_op
) {
264 return libnfs_zdr_opaque(zdrs
, *strp
, size
);
266 *strp
= zdr_malloc(zdrs
, size
+ 1);
271 return libnfs_zdr_opaque(zdrs
, *strp
, size
);
277 bool_t
libnfs_zdr_array(ZDR
*zdrs
, char **arrp
, uint32_t *size
, uint32_t maxsize
, uint32_t elsize
, zdrproc_t proc
)
281 if (!libnfs_zdr_u_int(zdrs
, size
)) {
285 if (zdrs
->pos
+ *size
* elsize
> zdrs
->size
) {
289 if (zdrs
->x_op
== ZDR_DECODE
) {
290 *arrp
= zdr_malloc(zdrs
, *size
* elsize
);
294 memset(*arrp
, 0, *size
* elsize
);
297 for (i
= 0; i
< *size
; i
++) {
298 if (!proc(zdrs
, *arrp
+ i
* elsize
)) {
305 void libnfs_zdr_free(zdrproc_t proc
, char *objp
)
309 static bool_t
libnfs_opaque_auth(ZDR
*zdrs
, struct opaque_auth
*auth
)
311 if (!libnfs_zdr_u_int(zdrs
, &auth
->oa_flavor
)) {
315 if (!libnfs_zdr_bytes(zdrs
, &auth
->oa_base
, &auth
->oa_length
, auth
->oa_length
)) {
322 static bool_t
libnfs_rpc_call_body(struct rpc_context
*rpc
, ZDR
*zdrs
, struct call_body
*cmb
)
324 if (!libnfs_zdr_u_int(zdrs
, &cmb
->rpcvers
)) {
325 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
330 if (!libnfs_zdr_u_int(zdrs
, &cmb
->prog
)) {
331 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
336 if (!libnfs_zdr_u_int(zdrs
, &cmb
->vers
)) {
337 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
342 if (!libnfs_zdr_u_int(zdrs
, &cmb
->proc
)) {
343 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
348 if (!libnfs_opaque_auth(zdrs
, &cmb
->cred
)) {
349 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
354 if (!libnfs_opaque_auth(zdrs
, &cmb
->verf
)) {
355 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
363 static bool_t
libnfs_accepted_reply(ZDR
*zdrs
, struct accepted_reply
*ar
)
365 if (!libnfs_opaque_auth(zdrs
, &ar
->verf
)) {
369 if (!libnfs_zdr_u_int(zdrs
, &ar
->stat
)) {
375 if (!ar
->reply_data
.results
.proc(zdrs
, ar
->reply_data
.results
.where
)) {
380 if (!libnfs_zdr_u_int(zdrs
, &ar
->reply_data
.mismatch_info
.low
)) {
383 if (!libnfs_zdr_u_int(zdrs
, &ar
->reply_data
.mismatch_info
.high
)) {
394 static bool_t
libnfs_rejected_reply(ZDR
*zdrs
, struct rejected_reply
*rr
)
396 if (!libnfs_zdr_u_int(zdrs
, &rr
->stat
)) {
402 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.mismatch_info
.low
)) {
405 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.mismatch_info
.high
)) {
410 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.stat
)) {
421 static bool_t
libnfs_rpc_reply_body(struct rpc_context
*rpc
, ZDR
*zdrs
, struct reply_body
*rmb
)
423 if (!libnfs_zdr_u_int(zdrs
, &rmb
->stat
)) {
424 rpc_set_error(rpc
, "libnfs_rpc_reply_body failed to decode "
431 if (!libnfs_accepted_reply(zdrs
, &rmb
->reply
.areply
)) {
432 rpc_set_error(rpc
, "libnfs_rpc_reply_body failed to "
438 if (!libnfs_rejected_reply(zdrs
, &rmb
->reply
.rreply
)) {
439 rpc_set_error(rpc
, "libnfs_rpc_reply_body failed to "
446 rpc_set_error(rpc
, "libnfs_rpc_reply_body failed to "
447 "decode. Neither ACCEPTED nor DENIED");
451 static bool_t
libnfs_rpc_msg(struct rpc_context
*rpc
, ZDR
*zdrs
, struct rpc_msg
*msg
)
455 if (!libnfs_zdr_u_int(zdrs
, &msg
->xid
)) {
456 rpc_set_error(rpc
, "libnfs_rpc_msg failed to decode XID");
460 if (!libnfs_zdr_u_int(zdrs
, &msg
->direction
)) {
461 rpc_set_error(rpc
, "libnfs_rpc_msg failed to decode DIRECTION");
465 switch (msg
->direction
) {
467 ret
= libnfs_rpc_call_body(rpc
, zdrs
, &msg
->body
.cbody
);
469 rpc_set_error(rpc
, "libnfs_rpc_msg failed to encode "
470 "CALL, ret=%d: %s", ret
, rpc_get_error(rpc
));
474 ret
= libnfs_rpc_reply_body(rpc
, zdrs
, &msg
->body
.rbody
);
476 rpc_set_error(rpc
, "libnfs_rpc_msg failed to decode "
477 "REPLY, ret=%d: %s", ret
, rpc_get_error(rpc
));
481 rpc_set_error(rpc
, "libnfs_rpc_msg failed to decode. "
482 "Neither CALL not REPLY");
487 bool_t
libnfs_zdr_callmsg(struct rpc_context
*rpc
, ZDR
*zdrs
, struct rpc_msg
*msg
)
489 return libnfs_rpc_msg(rpc
, zdrs
, msg
);
492 bool_t
libnfs_zdr_replymsg(struct rpc_context
*rpc
, ZDR
*zdrs
, struct rpc_msg
*msg
)
494 return libnfs_rpc_msg(rpc
, zdrs
, msg
);
497 struct AUTH
*authnone_create(void)
501 auth
= malloc(sizeof(struct AUTH
));
503 auth
->ah_cred
.oa_flavor
= AUTH_NONE
;
504 auth
->ah_cred
.oa_length
= 0;
505 auth
->ah_cred
.oa_base
= NULL
;
507 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
508 auth
->ah_verf
.oa_length
= 0;
509 auth
->ah_verf
.oa_base
= NULL
;
511 auth
->ah_private
= NULL
;
516 struct AUTH
*libnfs_authunix_create(char *host
, uint32_t uid
, uint32_t gid
, uint32_t len
, uint32_t *groups
)
523 size
= 4 + 4 + ((strlen(host
) + 3) & ~3) + 4 + 4 + 4 + len
* 4;
524 auth
= malloc(sizeof(struct AUTH
));
525 memset(auth
, 0x00, sizeof(struct AUTH
));
526 auth
->ah_cred
.oa_flavor
= AUTH_UNIX
;
527 auth
->ah_cred
.oa_length
= size
;
528 auth
->ah_cred
.oa_base
= malloc(size
);
530 memset(auth
->ah_cred
.oa_base
, 0x00, size
);
531 buf
= (uint32_t *)auth
->ah_cred
.oa_base
;
533 buf
[idx
++] = htonl(time(NULL
));
534 buf
[idx
++] = htonl(strlen(host
));
535 memcpy(&buf
[2], host
, strlen(host
));
537 idx
+= (strlen(host
) + 3) >> 2;
538 buf
[idx
++] = htonl(uid
);
539 buf
[idx
++] = htonl(gid
);
540 buf
[idx
++] = htonl(len
);
542 buf
[idx
++] = htonl(*groups
++);
545 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
546 auth
->ah_verf
.oa_length
= 0;
547 auth
->ah_verf
.oa_base
= NULL
;
549 auth
->ah_private
= NULL
;
554 struct AUTH
*libnfs_authunix_create_default(void)
557 return libnfs_authunix_create("libnfs", 65534, 65534, 0, NULL
);
559 return libnfs_authunix_create("libnfs", getuid(), getgid(), 0, NULL
);
563 void libnfs_auth_destroy(struct AUTH
*auth
)
565 if (auth
->ah_cred
.oa_base
) {
566 free(auth
->ah_cred
.oa_base
);
568 if (auth
->ah_verf
.oa_base
) {
569 free(auth
->ah_verf
.oa_base
);