898a95acae22114e83aa85cc92520a99894781a1
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"
30 #include "libnfs-zdr.h"
32 struct opaque_auth _null_auth
;
34 bool_t
libnfs_zdr_setpos(ZDR
*zdrs
, uint32_t pos
)
41 uint32_t libnfs_zdr_getpos(ZDR
*zdrs
)
46 void libnfs_zdrmem_create(ZDR
*zdrs
, const caddr_t addr
, uint32_t size
, enum zdr_op xop
)
55 static void *zdr_malloc(ZDR
*zdrs
, uint32_t size
)
59 mem
= malloc(sizeof(struct zdr_mem
));
60 mem
->next
= zdrs
->mem
;
62 mem
->buf
= malloc(mem
->size
);
68 void libnfs_zdr_destroy(ZDR
*zdrs
)
70 while (zdrs
->mem
!= NULL
) {
71 struct zdr_mem
*mem
= zdrs
->mem
->next
;
78 bool_t
libnfs_zdr_u_int(ZDR
*zdrs
, uint32_t *u
)
80 if (zdrs
->pos
+ 4 > zdrs
->size
) {
86 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl(*u
);
91 *u
= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
100 bool_t
libnfs_zdr_int(ZDR
*zdrs
, int32_t *i
)
102 return libnfs_zdr_u_int(zdrs
, (uint32_t *)i
);
105 bool_t
libnfs_zdr_u_quad_t(ZDR
*zdrs
, uint64_t *u
)
107 if (zdrs
->pos
+ 8 > zdrs
->size
) {
111 switch (zdrs
->x_op
) {
113 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl((*u
>> 32));
115 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl((*u
& 0xffffffff));
120 *u
= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
123 *u
|= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
132 bool_t
libnfs_zdr_quad_t(ZDR
*zdrs
, int64_t *i
)
134 return libnfs_zdr_u_quad_t(zdrs
, (uint64_t *)i
);
137 bool_t
libnfs_zdr_bytes(ZDR
*zdrs
, char **bufp
, uint32_t *size
, uint32_t maxsize
)
139 if (!libnfs_zdr_u_int(zdrs
, size
)) {
143 if (zdrs
->pos
+ *size
> zdrs
->size
) {
147 switch (zdrs
->x_op
) {
149 memcpy(&zdrs
->buf
[zdrs
->pos
], *bufp
, *size
);
151 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
155 *bufp
= zdr_malloc(zdrs
, *size
);
157 memcpy(*bufp
, &zdrs
->buf
[zdrs
->pos
], *size
);
159 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
167 bool_t
libnfs_zdr_enum(ZDR
*zdrs
, enum_t
*e
)
172 ret
= libnfs_zdr_u_int(zdrs
, (uint32_t *)&i
);
178 bool_t
libnfs_zdr_bool(ZDR
*zdrs
, bool_t
*b
)
180 return libnfs_zdr_u_int(zdrs
, (uint32_t *)b
);
183 bool_t
libnfs_zdr_void(void)
188 bool_t
libnfs_zdr_pointer(ZDR
*zdrs
, char **objp
, uint32_t size
, zdrproc_t proc
)
192 more_data
= (*objp
!= NULL
);
194 if (!libnfs_zdr_bool(zdrs
, &more_data
)) {
197 if (more_data
== 0) {
202 if (zdrs
->x_op
== ZDR_DECODE
) {
203 *objp
= zdr_malloc(zdrs
, size
);
207 memset(*objp
, 0, size
);
209 return proc(zdrs
, *objp
);
212 bool_t
libnfs_zdr_opaque(ZDR
*zdrs
, char *objp
, uint32_t size
)
214 switch (zdrs
->x_op
) {
216 memcpy(&zdrs
->buf
[zdrs
->pos
], objp
, size
);
218 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
221 memcpy(objp
, &zdrs
->buf
[zdrs
->pos
], size
);
223 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
230 bool_t
libnfs_zdr_string(ZDR
*zdrs
, char **strp
, uint32_t maxsize
)
234 if (zdrs
->x_op
== ZDR_ENCODE
) {
235 size
= strlen(*strp
);
238 if (!libnfs_zdr_u_int(zdrs
, &size
)) {
242 if (zdrs
->pos
+ size
> zdrs
->size
) {
246 switch (zdrs
->x_op
) {
248 return libnfs_zdr_opaque(zdrs
, *strp
, size
);
250 *strp
= zdr_malloc(zdrs
, size
+ 1);
255 return libnfs_zdr_opaque(zdrs
, *strp
, size
);
261 bool_t
libnfs_zdr_array(ZDR
*zdrs
, char **arrp
, uint32_t *size
, uint32_t maxsize
, uint32_t elsize
, zdrproc_t proc
)
265 if (!libnfs_zdr_u_int(zdrs
, size
)) {
269 if (zdrs
->pos
+ *size
* elsize
> zdrs
->size
) {
273 if (zdrs
->x_op
== ZDR_DECODE
) {
274 *arrp
= zdr_malloc(zdrs
, *size
* elsize
);
278 memset(*arrp
, 0, *size
* elsize
);
281 for (i
= 0; i
< *size
; i
++) {
282 if (!proc(zdrs
, *arrp
+ i
* elsize
)) {
289 void libnfs_zdr_free(zdrproc_t proc
, char *objp
)
293 static bool_t
libnfs_opaque_auth(ZDR
*zdrs
, struct opaque_auth
*auth
)
295 if (!libnfs_zdr_u_int(zdrs
, &auth
->oa_flavor
)) {
299 if (!libnfs_zdr_bytes(zdrs
, &auth
->oa_base
, &auth
->oa_length
, auth
->oa_length
)) {
306 static bool_t
libnfs_rpc_call_body(ZDR
*zdrs
, struct call_body
*cmb
)
308 if (!libnfs_zdr_u_int(zdrs
, &cmb
->rpcvers
)) {
312 if (!libnfs_zdr_u_int(zdrs
, &cmb
->prog
)) {
316 if (!libnfs_zdr_u_int(zdrs
, &cmb
->vers
)) {
320 if (!libnfs_zdr_u_int(zdrs
, &cmb
->proc
)) {
324 if (!libnfs_opaque_auth(zdrs
, &cmb
->cred
)) {
328 if (!libnfs_opaque_auth(zdrs
, &cmb
->verf
)) {
335 static bool_t
libnfs_accepted_reply(ZDR
*zdrs
, struct accepted_reply
*ar
)
337 if (!libnfs_opaque_auth(zdrs
, &ar
->verf
)) {
341 if (!libnfs_zdr_u_int(zdrs
, &ar
->stat
)) {
347 if (!ar
->reply_data
.results
.proc(zdrs
, ar
->reply_data
.results
.where
)) {
352 if (!libnfs_zdr_u_int(zdrs
, &ar
->reply_data
.mismatch_info
.low
)) {
355 if (!libnfs_zdr_u_int(zdrs
, &ar
->reply_data
.mismatch_info
.high
)) {
366 static bool_t
libnfs_rejected_reply(ZDR
*zdrs
, struct rejected_reply
*rr
)
368 if (!libnfs_zdr_u_int(zdrs
, &rr
->stat
)) {
374 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.mismatch_info
.low
)) {
377 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.mismatch_info
.high
)) {
382 if (!libnfs_zdr_u_int(zdrs
, &rr
->reject_data
.stat
)) {
393 static bool_t
libnfs_rpc_reply_body(ZDR
*zdrs
, struct reply_body
*rmb
)
395 if (!libnfs_zdr_u_int(zdrs
, &rmb
->stat
)) {
401 if (!libnfs_accepted_reply(zdrs
, &rmb
->reply
.areply
)) {
406 if (!libnfs_rejected_reply(zdrs
, &rmb
->reply
.rreply
)) {
415 static bool_t
libnfs_rpc_msg(ZDR
*zdrs
, struct rpc_msg
*msg
)
417 if (!libnfs_zdr_u_int(zdrs
, &msg
->xid
)) {
421 if (!libnfs_zdr_u_int(zdrs
, &msg
->direction
)) {
425 switch (msg
->direction
) {
427 return libnfs_rpc_call_body(zdrs
, &msg
->body
.cbody
);
430 return libnfs_rpc_reply_body(zdrs
, &msg
->body
.rbody
);
437 bool_t
libnfs_zdr_callmsg(ZDR
*zdrs
, struct rpc_msg
*msg
)
439 return libnfs_rpc_msg(zdrs
, msg
);
442 bool_t
libnfs_zdr_replymsg(ZDR
*zdrs
, struct rpc_msg
*msg
)
444 return libnfs_rpc_msg(zdrs
, msg
);
447 struct AUTH
*authnone_create(void)
451 auth
= malloc(sizeof(struct AUTH
));
453 auth
->ah_cred
.oa_flavor
= AUTH_NONE
;
454 auth
->ah_cred
.oa_length
= 0;
455 auth
->ah_cred
.oa_base
= NULL
;
457 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
458 auth
->ah_verf
.oa_length
= 0;
459 auth
->ah_verf
.oa_base
= NULL
;
461 auth
->ah_private
= NULL
;
466 struct AUTH
*libnfs_authunix_create(char *host
, uint32_t uid
, uint32_t gid
, uint32_t len
, uint32_t *groups
)
473 size
= 4 + 4 + ((strlen(host
) + 3) & ~3) + 4 + 4 + 4 + len
* 4;
474 auth
= malloc(sizeof(struct AUTH
));
475 auth
->ah_cred
.oa_flavor
= AUTH_UNIX
;
476 auth
->ah_cred
.oa_length
= size
;
477 auth
->ah_cred
.oa_base
= malloc(size
);
479 buf
= (uint32_t *)auth
->ah_cred
.oa_base
;
481 buf
[idx
++] = htonl(time(NULL
));
482 buf
[idx
++] = htonl(strlen(host
));
483 memcpy(&buf
[2], host
, strlen(host
));
485 idx
+= (strlen(host
) + 3) >> 2;
486 buf
[idx
++] = htonl(uid
);
487 buf
[idx
++] = htonl(gid
);
488 buf
[idx
++] = htonl(len
);
490 buf
[idx
++] = htonl(*groups
++);
493 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
494 auth
->ah_verf
.oa_length
= 0;
495 auth
->ah_verf
.oa_base
= NULL
;
497 auth
->ah_private
= NULL
;
502 struct AUTH
*libnfs_authunix_create_default(void)
505 return libnfs_authunix_create("libnfs", 65535, 65535, 0, NULL
);
507 return libnfs_authunix_create("libnfs", getuid(), getgid(), 0, NULL
);
511 void libnfs_auth_destroy(struct AUTH
*auth
)
513 if (auth
->ah_cred
.oa_base
) {
514 free(auth
->ah_cred
.oa_base
);
516 if (auth
->ah_verf
.oa_base
) {
517 free(auth
->ah_verf
.oa_base
);