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.
24 #include "win32_compat.h"
28 #include "aros_compat.h"
34 #include "libnfs-zdr.h"
36 struct opaque_auth _null_auth
;
38 bool_t
libnfs_zdr_setpos(ZDR
*zdrs
, uint32_t pos
)
45 uint32_t libnfs_zdr_getpos(ZDR
*zdrs
)
50 void libnfs_zdrmem_create(ZDR
*zdrs
, const caddr_t addr
, uint32_t size
, enum zdr_op xop
)
59 static void *zdr_malloc(ZDR
*zdrs
, uint32_t size
)
63 mem
= malloc(sizeof(struct zdr_mem
));
64 mem
->next
= zdrs
->mem
;
66 mem
->buf
= malloc(mem
->size
);
72 void libnfs_zdr_destroy(ZDR
*zdrs
)
74 while (zdrs
->mem
!= NULL
) {
75 struct zdr_mem
*mem
= zdrs
->mem
->next
;
82 bool_t
libnfs_zdr_u_int(ZDR
*zdrs
, uint32_t *u
)
84 if (zdrs
->pos
+ 4 > zdrs
->size
) {
90 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl(*u
);
95 *u
= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
104 bool_t
libnfs_zdr_int(ZDR
*zdrs
, int32_t *i
)
106 return libnfs_zdr_u_int(zdrs
, (uint32_t *)i
);
109 bool_t
libnfs_zdr_u_quad_t(ZDR
*zdrs
, uint64_t *u
)
111 if (zdrs
->pos
+ 8 > zdrs
->size
) {
115 switch (zdrs
->x_op
) {
117 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl((*u
>> 32));
119 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl((*u
& 0xffffffff));
124 *u
= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
127 *u
|= (uint32_t)ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
136 bool_t
libnfs_zdr_quad_t(ZDR
*zdrs
, int64_t *i
)
138 return libnfs_zdr_u_quad_t(zdrs
, (uint64_t *)i
);
141 bool_t
libnfs_zdr_bytes(ZDR
*zdrs
, char **bufp
, uint32_t *size
, uint32_t maxsize
)
143 if (!libnfs_zdr_u_int(zdrs
, size
)) {
147 if (zdrs
->pos
+ *size
> zdrs
->size
) {
151 switch (zdrs
->x_op
) {
153 memcpy(&zdrs
->buf
[zdrs
->pos
], *bufp
, *size
);
155 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
159 *bufp
= zdr_malloc(zdrs
, *size
);
161 memcpy(*bufp
, &zdrs
->buf
[zdrs
->pos
], *size
);
163 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
171 bool_t
libnfs_zdr_enum(ZDR
*zdrs
, enum_t
*e
)
176 ret
= libnfs_zdr_u_int(zdrs
, (uint32_t *)&i
);
182 bool_t
libnfs_zdr_bool(ZDR
*zdrs
, bool_t
*b
)
184 return libnfs_zdr_u_int(zdrs
, (uint32_t *)b
);
187 bool_t
libnfs_zdr_void(void)
192 bool_t
libnfs_zdr_pointer(ZDR
*zdrs
, char **objp
, uint32_t size
, zdrproc_t proc
)
196 more_data
= (*objp
!= NULL
);
198 if (!libnfs_zdr_bool(zdrs
, &more_data
)) {
201 if (more_data
== 0) {
206 if (zdrs
->x_op
== ZDR_DECODE
) {
207 *objp
= zdr_malloc(zdrs
, size
);
211 memset(*objp
, 0, size
);
213 return proc(zdrs
, *objp
);
216 bool_t
libnfs_zdr_opaque(ZDR
*zdrs
, char *objp
, uint32_t size
)
218 switch (zdrs
->x_op
) {
220 memcpy(&zdrs
->buf
[zdrs
->pos
], objp
, size
);
223 memset(&zdrs
->buf
[zdrs
->pos
], 0x00, 4 - (zdrs
->pos
& 3));
225 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
228 memcpy(objp
, &zdrs
->buf
[zdrs
->pos
], size
);
230 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
237 bool_t
libnfs_zdr_string(ZDR
*zdrs
, char **strp
, uint32_t maxsize
)
241 if (zdrs
->x_op
== ZDR_ENCODE
) {
242 size
= strlen(*strp
);
245 if (!libnfs_zdr_u_int(zdrs
, &size
)) {
249 if (zdrs
->pos
+ size
> zdrs
->size
) {
253 switch (zdrs
->x_op
) {
255 return libnfs_zdr_opaque(zdrs
, *strp
, size
);
257 *strp
= zdr_malloc(zdrs
, size
+ 1);
262 return libnfs_zdr_opaque(zdrs
, *strp
, size
);
268 bool_t
libnfs_zdr_array(ZDR
*zdrs
, char **arrp
, uint32_t *size
, uint32_t maxsize
, uint32_t elsize
, zdrproc_t proc
)
272 if (!libnfs_zdr_u_int(zdrs
, size
)) {
276 if (zdrs
->pos
+ *size
* elsize
> zdrs
->size
) {
280 if (zdrs
->x_op
== ZDR_DECODE
) {
281 *arrp
= zdr_malloc(zdrs
, *size
* elsize
);
285 memset(*arrp
, 0, *size
* elsize
);
288 for (i
= 0; i
< *size
; i
++) {
289 if (!proc(zdrs
, *arrp
+ i
* elsize
)) {
296 void libnfs_zdr_free(zdrproc_t proc
, char *objp
)
300 static bool_t
libnfs_opaque_auth(ZDR
*zdrs
, struct opaque_auth
*auth
)
302 if (!libnfs_zdr_u_int(zdrs
, &auth
->oa_flavor
)) {
306 if (!libnfs_zdr_bytes(zdrs
, &auth
->oa_base
, &auth
->oa_length
, auth
->oa_length
)) {
313 static bool_t
libnfs_rpc_call_body(struct rpc_context
*rpc
, ZDR
*zdrs
, struct call_body
*cmb
)
315 if (!libnfs_zdr_u_int(zdrs
, &cmb
->rpcvers
)) {
316 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
321 if (!libnfs_zdr_u_int(zdrs
, &cmb
->prog
)) {
322 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
327 if (!libnfs_zdr_u_int(zdrs
, &cmb
->vers
)) {
328 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
333 if (!libnfs_zdr_u_int(zdrs
, &cmb
->proc
)) {
334 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
339 if (!libnfs_opaque_auth(zdrs
, &cmb
->cred
)) {
340 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
345 if (!libnfs_opaque_auth(zdrs
, &cmb
->verf
)) {
346 rpc_set_error(rpc
, "libnfs_rpc_call_body failed to encode "
354 static bool_t
libnfs_accepted_reply(ZDR
*zdrs
, struct accepted_reply
*ar
)
356 if (!libnfs_opaque_auth(zdrs
, &ar
->verf
)) {
360 if (!libnfs_zdr_u_int(zdrs
, &ar
->stat
)) {
366 if (!ar
->reply_data
.results
.proc(zdrs
, ar
->reply_data
.results
.where
)) {
371 if (!libnfs_zdr_u_int(zdrs
, &ar
->reply_data
.mismatch_info
.low
)) {
374 if (!libnfs_zdr_u_int(zdrs
, &ar
->reply_data
.mismatch_info
.high
)) {
385 static bool_t
libnfs_rejected_reply(ZDR
*zdrs
, struct rejected_reply
*rr
)
387 if (!libnfs_zdr_u_int(zdrs
, &rr
->stat
)) {
393 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.mismatch_info
.low
)) {
396 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.mismatch_info
.high
)) {
401 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.stat
)) {
412 static bool_t
libnfs_rpc_reply_body(struct rpc_context
*rpc
, ZDR
*zdrs
, struct reply_body
*rmb
)
414 if (!libnfs_zdr_u_int(zdrs
, &rmb
->stat
)) {
415 rpc_set_error(rpc
, "libnfs_rpc_reply_body failed to decode "
422 if (!libnfs_accepted_reply(zdrs
, &rmb
->reply
.areply
)) {
423 rpc_set_error(rpc
, "libnfs_rpc_reply_body failed to "
429 if (!libnfs_rejected_reply(zdrs
, &rmb
->reply
.rreply
)) {
430 rpc_set_error(rpc
, "libnfs_rpc_reply_body failed to "
437 rpc_set_error(rpc
, "libnfs_rpc_reply_body failed to "
438 "decode. Neither ACCEPTED nor DENIED");
442 static bool_t
libnfs_rpc_msg(struct rpc_context
*rpc
, ZDR
*zdrs
, struct rpc_msg
*msg
)
446 if (!libnfs_zdr_u_int(zdrs
, &msg
->xid
)) {
447 rpc_set_error(rpc
, "libnfs_rpc_msg failed to decode XID");
451 if (!libnfs_zdr_u_int(zdrs
, &msg
->direction
)) {
452 rpc_set_error(rpc
, "libnfs_rpc_msg failed to decode DIRECTION");
456 switch (msg
->direction
) {
458 ret
= libnfs_rpc_call_body(rpc
, zdrs
, &msg
->body
.cbody
);
460 rpc_set_error(rpc
, "libnfs_rpc_msg failed to encode "
461 "CALL, ret=%d: %s", ret
, rpc_get_error(rpc
));
465 ret
= libnfs_rpc_reply_body(rpc
, zdrs
, &msg
->body
.rbody
);
467 rpc_set_error(rpc
, "libnfs_rpc_msg failed to decode "
468 "REPLY, ret=%d: %s", ret
, rpc_get_error(rpc
));
472 rpc_set_error(rpc
, "libnfs_rpc_msg failed to decode. "
473 "Neither CALL not REPLY");
478 bool_t
libnfs_zdr_callmsg(struct rpc_context
*rpc
, ZDR
*zdrs
, struct rpc_msg
*msg
)
480 return libnfs_rpc_msg(rpc
, zdrs
, msg
);
483 bool_t
libnfs_zdr_replymsg(struct rpc_context
*rpc
, ZDR
*zdrs
, struct rpc_msg
*msg
)
485 return libnfs_rpc_msg(rpc
, zdrs
, msg
);
488 struct AUTH
*authnone_create(void)
492 auth
= malloc(sizeof(struct AUTH
));
494 auth
->ah_cred
.oa_flavor
= AUTH_NONE
;
495 auth
->ah_cred
.oa_length
= 0;
496 auth
->ah_cred
.oa_base
= NULL
;
498 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
499 auth
->ah_verf
.oa_length
= 0;
500 auth
->ah_verf
.oa_base
= NULL
;
502 auth
->ah_private
= NULL
;
507 struct AUTH
*libnfs_authunix_create(char *host
, uint32_t uid
, uint32_t gid
, uint32_t len
, uint32_t *groups
)
514 size
= 4 + 4 + ((strlen(host
) + 3) & ~3) + 4 + 4 + 4 + len
* 4;
515 auth
= malloc(sizeof(struct AUTH
));
516 memset(auth
, 0x00, sizeof(struct AUTH
));
517 auth
->ah_cred
.oa_flavor
= AUTH_UNIX
;
518 auth
->ah_cred
.oa_length
= size
;
519 auth
->ah_cred
.oa_base
= malloc(size
);
521 memset(auth
->ah_cred
.oa_base
, 0x00, size
);
522 buf
= (uint32_t *)auth
->ah_cred
.oa_base
;
524 buf
[idx
++] = htonl(time(NULL
));
525 buf
[idx
++] = htonl(strlen(host
));
526 memcpy(&buf
[2], host
, strlen(host
));
528 idx
+= (strlen(host
) + 3) >> 2;
529 buf
[idx
++] = htonl(uid
);
530 buf
[idx
++] = htonl(gid
);
531 buf
[idx
++] = htonl(len
);
533 buf
[idx
++] = htonl(*groups
++);
536 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
537 auth
->ah_verf
.oa_length
= 0;
538 auth
->ah_verf
.oa_base
= NULL
;
540 auth
->ah_private
= NULL
;
545 struct AUTH
*libnfs_authunix_create_default(void)
548 return libnfs_authunix_create("libnfs", 65534, 65534, 0, NULL
);
550 return libnfs_authunix_create("libnfs", getuid(), getgid(), 0, NULL
);
554 void libnfs_auth_destroy(struct AUTH
*auth
)
556 if (auth
->ah_cred
.oa_base
) {
557 free(auth
->ah_cred
.oa_base
);
559 if (auth
->ah_verf
.oa_base
) {
560 free(auth
->ah_verf
.oa_base
);