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(ZDR
*zdrs
, struct call_body
*cmb
)
315 if (!libnfs_zdr_u_int(zdrs
, &cmb
->rpcvers
)) {
319 if (!libnfs_zdr_u_int(zdrs
, &cmb
->prog
)) {
323 if (!libnfs_zdr_u_int(zdrs
, &cmb
->vers
)) {
327 if (!libnfs_zdr_u_int(zdrs
, &cmb
->proc
)) {
331 if (!libnfs_opaque_auth(zdrs
, &cmb
->cred
)) {
335 if (!libnfs_opaque_auth(zdrs
, &cmb
->verf
)) {
342 static bool_t
libnfs_accepted_reply(ZDR
*zdrs
, struct accepted_reply
*ar
)
344 if (!libnfs_opaque_auth(zdrs
, &ar
->verf
)) {
348 if (!libnfs_zdr_u_int(zdrs
, &ar
->stat
)) {
354 if (!ar
->reply_data
.results
.proc(zdrs
, ar
->reply_data
.results
.where
)) {
359 if (!libnfs_zdr_u_int(zdrs
, &ar
->reply_data
.mismatch_info
.low
)) {
362 if (!libnfs_zdr_u_int(zdrs
, &ar
->reply_data
.mismatch_info
.high
)) {
373 static bool_t
libnfs_rejected_reply(ZDR
*zdrs
, struct rejected_reply
*rr
)
375 if (!libnfs_zdr_u_int(zdrs
, &rr
->stat
)) {
381 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.mismatch_info
.low
)) {
384 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.mismatch_info
.high
)) {
389 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.stat
)) {
400 static bool_t
libnfs_rpc_reply_body(ZDR
*zdrs
, struct reply_body
*rmb
)
402 if (!libnfs_zdr_u_int(zdrs
, &rmb
->stat
)) {
408 if (!libnfs_accepted_reply(zdrs
, &rmb
->reply
.areply
)) {
413 if (!libnfs_rejected_reply(zdrs
, &rmb
->reply
.rreply
)) {
422 static bool_t
libnfs_rpc_msg(ZDR
*zdrs
, struct rpc_msg
*msg
)
424 if (!libnfs_zdr_u_int(zdrs
, &msg
->xid
)) {
428 if (!libnfs_zdr_u_int(zdrs
, &msg
->direction
)) {
432 switch (msg
->direction
) {
434 return libnfs_rpc_call_body(zdrs
, &msg
->body
.cbody
);
437 return libnfs_rpc_reply_body(zdrs
, &msg
->body
.rbody
);
444 bool_t
libnfs_zdr_callmsg(ZDR
*zdrs
, struct rpc_msg
*msg
)
446 return libnfs_rpc_msg(zdrs
, msg
);
449 bool_t
libnfs_zdr_replymsg(ZDR
*zdrs
, struct rpc_msg
*msg
)
451 return libnfs_rpc_msg(zdrs
, msg
);
454 struct AUTH
*authnone_create(void)
458 auth
= malloc(sizeof(struct AUTH
));
460 auth
->ah_cred
.oa_flavor
= AUTH_NONE
;
461 auth
->ah_cred
.oa_length
= 0;
462 auth
->ah_cred
.oa_base
= NULL
;
464 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
465 auth
->ah_verf
.oa_length
= 0;
466 auth
->ah_verf
.oa_base
= NULL
;
468 auth
->ah_private
= NULL
;
473 struct AUTH
*libnfs_authunix_create(char *host
, uint32_t uid
, uint32_t gid
, uint32_t len
, uint32_t *groups
)
480 size
= 4 + 4 + ((strlen(host
) + 3) & ~3) + 4 + 4 + 4 + len
* 4;
481 auth
= malloc(sizeof(struct AUTH
));
482 memset(auth
, 0x00, sizeof(struct AUTH
));
483 auth
->ah_cred
.oa_flavor
= AUTH_UNIX
;
484 auth
->ah_cred
.oa_length
= size
;
485 auth
->ah_cred
.oa_base
= malloc(size
);
487 memset(auth
->ah_cred
.oa_base
, 0x00, size
);
488 buf
= (uint32_t *)auth
->ah_cred
.oa_base
;
490 buf
[idx
++] = htonl(time(NULL
));
491 buf
[idx
++] = htonl(strlen(host
));
492 memcpy(&buf
[2], host
, strlen(host
));
494 idx
+= (strlen(host
) + 3) >> 2;
495 buf
[idx
++] = htonl(uid
);
496 buf
[idx
++] = htonl(gid
);
497 buf
[idx
++] = htonl(len
);
499 buf
[idx
++] = htonl(*groups
++);
502 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
503 auth
->ah_verf
.oa_length
= 0;
504 auth
->ah_verf
.oa_base
= NULL
;
506 auth
->ah_private
= NULL
;
511 struct AUTH
*libnfs_authunix_create_default(void)
514 return libnfs_authunix_create("libnfs", 65534, 65534, 0, NULL
);
516 return libnfs_authunix_create("libnfs", getuid(), getgid(), 0, NULL
);
520 void libnfs_auth_destroy(struct AUTH
*auth
)
522 if (auth
->ah_cred
.oa_base
) {
523 free(auth
->ah_cred
.oa_base
);
525 if (auth
->ah_verf
.oa_base
) {
526 free(auth
->ah_verf
.oa_base
);