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"
26 #include <arpa/inet.h>
32 #include "libnfs-zdr.h"
34 struct opaque_auth _null_auth
;
36 bool_t
libnfs_zdr_setpos(ZDR
*zdrs
, uint32_t pos
)
43 uint32_t libnfs_zdr_getpos(ZDR
*zdrs
)
48 void libnfs_zdrmem_create(ZDR
*zdrs
, const caddr_t addr
, uint32_t size
, enum zdr_op xop
)
57 static void *zdr_malloc(ZDR
*zdrs
, uint32_t size
)
61 mem
= malloc(sizeof(struct zdr_mem
));
62 mem
->next
= zdrs
->mem
;
64 mem
->buf
= malloc(mem
->size
);
70 void libnfs_zdr_destroy(ZDR
*zdrs
)
72 while (zdrs
->mem
!= NULL
) {
73 struct zdr_mem
*mem
= zdrs
->mem
->next
;
80 bool_t
libnfs_zdr_u_int(ZDR
*zdrs
, uint32_t *u
)
82 if (zdrs
->pos
+ 4 > zdrs
->size
) {
88 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl(*u
);
93 *u
= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
102 bool_t
libnfs_zdr_int(ZDR
*zdrs
, int32_t *i
)
104 return libnfs_zdr_u_int(zdrs
, (uint32_t *)i
);
107 bool_t
libnfs_zdr_u_quad_t(ZDR
*zdrs
, uint64_t *u
)
109 if (zdrs
->pos
+ 8 > zdrs
->size
) {
113 switch (zdrs
->x_op
) {
115 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl((*u
>> 32));
117 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl((*u
& 0xffffffff));
122 *u
= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
125 *u
|= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
134 bool_t
libnfs_zdr_quad_t(ZDR
*zdrs
, int64_t *i
)
136 return libnfs_zdr_u_quad_t(zdrs
, (uint64_t *)i
);
139 bool_t
libnfs_zdr_bytes(ZDR
*zdrs
, char **bufp
, uint32_t *size
, uint32_t maxsize
)
141 if (!libnfs_zdr_u_int(zdrs
, size
)) {
145 if (zdrs
->pos
+ *size
> zdrs
->size
) {
149 switch (zdrs
->x_op
) {
151 memcpy(&zdrs
->buf
[zdrs
->pos
], *bufp
, *size
);
153 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
157 *bufp
= zdr_malloc(zdrs
, *size
);
159 memcpy(*bufp
, &zdrs
->buf
[zdrs
->pos
], *size
);
161 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
169 bool_t
libnfs_zdr_enum(ZDR
*zdrs
, enum_t
*e
)
174 ret
= libnfs_zdr_u_int(zdrs
, (uint32_t *)&i
);
180 bool_t
libnfs_zdr_bool(ZDR
*zdrs
, bool_t
*b
)
182 return libnfs_zdr_u_int(zdrs
, (uint32_t *)b
);
185 bool_t
libnfs_zdr_void(void)
190 bool_t
libnfs_zdr_pointer(ZDR
*zdrs
, char **objp
, uint32_t size
, zdrproc_t proc
)
194 more_data
= (*objp
!= NULL
);
196 if (!libnfs_zdr_bool(zdrs
, &more_data
)) {
199 if (more_data
== 0) {
204 if (zdrs
->x_op
== ZDR_DECODE
) {
205 *objp
= zdr_malloc(zdrs
, size
);
209 memset(*objp
, 0, size
);
211 return proc(zdrs
, *objp
);
214 bool_t
libnfs_zdr_opaque(ZDR
*zdrs
, char *objp
, uint32_t size
)
216 switch (zdrs
->x_op
) {
218 memcpy(&zdrs
->buf
[zdrs
->pos
], objp
, size
);
220 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
223 memcpy(objp
, &zdrs
->buf
[zdrs
->pos
], size
);
225 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
232 bool_t
libnfs_zdr_string(ZDR
*zdrs
, char **strp
, uint32_t maxsize
)
236 if (zdrs
->x_op
== ZDR_ENCODE
) {
237 size
= strlen(*strp
);
240 if (!libnfs_zdr_u_int(zdrs
, &size
)) {
244 if (zdrs
->pos
+ size
> zdrs
->size
) {
248 switch (zdrs
->x_op
) {
250 return libnfs_zdr_opaque(zdrs
, *strp
, size
);
252 *strp
= zdr_malloc(zdrs
, size
+ 1);
257 return libnfs_zdr_opaque(zdrs
, *strp
, size
);
263 bool_t
libnfs_zdr_array(ZDR
*zdrs
, char **arrp
, uint32_t *size
, uint32_t maxsize
, uint32_t elsize
, zdrproc_t proc
)
267 if (!libnfs_zdr_u_int(zdrs
, size
)) {
271 if (zdrs
->pos
+ *size
* elsize
> zdrs
->size
) {
275 if (zdrs
->x_op
== ZDR_DECODE
) {
276 *arrp
= zdr_malloc(zdrs
, *size
* elsize
);
280 memset(*arrp
, 0, *size
* elsize
);
283 for (i
= 0; i
< *size
; i
++) {
284 if (!proc(zdrs
, *arrp
+ i
* elsize
)) {
291 void libnfs_zdr_free(zdrproc_t proc
, char *objp
)
295 static bool_t
libnfs_opaque_auth(ZDR
*zdrs
, struct opaque_auth
*auth
)
297 if (!libnfs_zdr_u_int(zdrs
, &auth
->oa_flavor
)) {
301 if (!libnfs_zdr_bytes(zdrs
, &auth
->oa_base
, &auth
->oa_length
, auth
->oa_length
)) {
308 static bool_t
libnfs_rpc_call_body(ZDR
*zdrs
, struct call_body
*cmb
)
310 if (!libnfs_zdr_u_int(zdrs
, &cmb
->rpcvers
)) {
314 if (!libnfs_zdr_u_int(zdrs
, &cmb
->prog
)) {
318 if (!libnfs_zdr_u_int(zdrs
, &cmb
->vers
)) {
322 if (!libnfs_zdr_u_int(zdrs
, &cmb
->proc
)) {
326 if (!libnfs_opaque_auth(zdrs
, &cmb
->cred
)) {
330 if (!libnfs_opaque_auth(zdrs
, &cmb
->verf
)) {
337 static bool_t
libnfs_accepted_reply(ZDR
*zdrs
, struct accepted_reply
*ar
)
339 if (!libnfs_opaque_auth(zdrs
, &ar
->verf
)) {
343 if (!libnfs_zdr_u_int(zdrs
, &ar
->stat
)) {
349 if (!ar
->reply_data
.results
.proc(zdrs
, ar
->reply_data
.results
.where
)) {
354 if (!libnfs_zdr_u_int(zdrs
, &ar
->reply_data
.mismatch_info
.low
)) {
357 if (!libnfs_zdr_u_int(zdrs
, &ar
->reply_data
.mismatch_info
.high
)) {
368 static bool_t
libnfs_rejected_reply(ZDR
*zdrs
, struct rejected_reply
*rr
)
370 if (!libnfs_zdr_u_int(zdrs
, &rr
->stat
)) {
376 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.mismatch_info
.low
)) {
379 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.mismatch_info
.high
)) {
384 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.stat
)) {
395 static bool_t
libnfs_rpc_reply_body(ZDR
*zdrs
, struct reply_body
*rmb
)
397 if (!libnfs_zdr_u_int(zdrs
, &rmb
->stat
)) {
403 if (!libnfs_accepted_reply(zdrs
, &rmb
->reply
.areply
)) {
408 if (!libnfs_rejected_reply(zdrs
, &rmb
->reply
.rreply
)) {
417 static bool_t
libnfs_rpc_msg(ZDR
*zdrs
, struct rpc_msg
*msg
)
419 if (!libnfs_zdr_u_int(zdrs
, &msg
->xid
)) {
423 if (!libnfs_zdr_u_int(zdrs
, &msg
->direction
)) {
427 switch (msg
->direction
) {
429 return libnfs_rpc_call_body(zdrs
, &msg
->body
.cbody
);
432 return libnfs_rpc_reply_body(zdrs
, &msg
->body
.rbody
);
439 bool_t
libnfs_zdr_callmsg(ZDR
*zdrs
, struct rpc_msg
*msg
)
441 return libnfs_rpc_msg(zdrs
, msg
);
444 bool_t
libnfs_zdr_replymsg(ZDR
*zdrs
, struct rpc_msg
*msg
)
446 return libnfs_rpc_msg(zdrs
, msg
);
449 struct AUTH
*authnone_create(void)
453 auth
= malloc(sizeof(struct AUTH
));
455 auth
->ah_cred
.oa_flavor
= AUTH_NONE
;
456 auth
->ah_cred
.oa_length
= 0;
457 auth
->ah_cred
.oa_base
= NULL
;
459 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
460 auth
->ah_verf
.oa_length
= 0;
461 auth
->ah_verf
.oa_base
= NULL
;
463 auth
->ah_private
= NULL
;
468 struct AUTH
*libnfs_authunix_create(char *host
, uint32_t uid
, uint32_t gid
, uint32_t len
, uint32_t *groups
)
475 size
= 4 + 4 + ((strlen(host
) + 3) & ~3) + 4 + 4 + 4 + len
* 4;
476 auth
= malloc(sizeof(struct AUTH
));
477 auth
->ah_cred
.oa_flavor
= AUTH_UNIX
;
478 auth
->ah_cred
.oa_length
= size
;
479 auth
->ah_cred
.oa_base
= malloc(size
);
481 buf
= (uint32_t *)auth
->ah_cred
.oa_base
;
483 buf
[idx
++] = htonl(time(NULL
));
484 buf
[idx
++] = htonl(strlen(host
));
485 memcpy(&buf
[2], host
, strlen(host
));
487 idx
+= (strlen(host
) + 3) >> 2;
488 buf
[idx
++] = htonl(uid
);
489 buf
[idx
++] = htonl(gid
);
490 buf
[idx
++] = htonl(len
);
492 buf
[idx
++] = htonl(*groups
++);
495 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
496 auth
->ah_verf
.oa_length
= 0;
497 auth
->ah_verf
.oa_base
= NULL
;
499 auth
->ah_private
= NULL
;
504 struct AUTH
*libnfs_authunix_create_default(void)
506 return libnfs_authunix_create("libnfs", getuid(), -1, 0, NULL
);
509 void libnfs_auth_destroy(struct AUTH
*auth
)
511 if (auth
->ah_cred
.oa_base
) {
512 free(auth
->ah_cred
.oa_base
);
514 if (auth
->ah_verf
.oa_base
) {
515 free(auth
->ah_verf
.oa_base
);