58726188b177726e2b41aa1ef79cf477b86a9e3d
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.
26 #include "libnfs-zdr.h"
28 struct opaque_auth _null_auth
;
30 bool_t
libnfs_zdr_setpos(ZDR
*zdrs
, uint32_t pos
)
37 uint32_t libnfs_zdr_getpos(ZDR
*zdrs
)
42 void libnfs_zdrmem_create(ZDR
*zdrs
, const caddr_t addr
, uint32_t size
, enum zdr_op xop
)
51 static void *zdr_malloc(ZDR
*zdrs
, uint32_t size
)
55 mem
= malloc(sizeof(struct zdr_mem
));
56 mem
->next
= zdrs
->mem
;
58 mem
->buf
= malloc(mem
->size
);
64 void libnfs_zdr_destroy(ZDR
*zdrs
)
66 while (zdrs
->mem
!= NULL
) {
67 struct zdr_mem
*mem
= zdrs
->mem
->next
;
74 bool_t
libnfs_zdr_u_int(ZDR
*zdrs
, uint32_t *u
)
76 if (zdrs
->pos
+ 4 > zdrs
->size
) {
82 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl(*u
);
87 *u
= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
96 bool_t
libnfs_zdr_int(ZDR
*zdrs
, int32_t *i
)
98 return libnfs_zdr_u_int(zdrs
, (uint32_t *)i
);
101 bool_t
libnfs_zdr_u_quad_t(ZDR
*zdrs
, uint64_t *u
)
103 if (zdrs
->pos
+ 8 > zdrs
->size
) {
107 switch (zdrs
->x_op
) {
109 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl((*u
>> 32));
111 *(uint32_t *)&zdrs
->buf
[zdrs
->pos
] = htonl((*u
& 0xffffffff));
116 *u
= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
119 *u
|= ntohl(*(uint32_t *)&zdrs
->buf
[zdrs
->pos
]);
128 bool_t
libnfs_zdr_quad_t(ZDR
*zdrs
, int64_t *i
)
130 return libnfs_zdr_u_quad_t(zdrs
, (uint64_t *)i
);
133 bool_t
libnfs_zdr_bytes(ZDR
*zdrs
, char **bufp
, uint32_t *size
, uint32_t maxsize
)
135 if (!libnfs_zdr_u_int(zdrs
, size
)) {
139 if (zdrs
->pos
+ *size
> zdrs
->size
) {
143 switch (zdrs
->x_op
) {
145 memcpy(&zdrs
->buf
[zdrs
->pos
], *bufp
, *size
);
147 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
151 *bufp
= zdr_malloc(zdrs
, *size
);
153 memcpy(*bufp
, &zdrs
->buf
[zdrs
->pos
], *size
);
155 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
163 bool_t
libnfs_zdr_enum(ZDR
*zdrs
, int32_t *e
)
165 return libnfs_zdr_u_int(zdrs
, (uint32_t *)e
);
168 bool_t
libnfs_zdr_bool(ZDR
*zdrs
, bool_t
*b
)
170 return libnfs_zdr_u_int(zdrs
, (uint32_t *)b
);
173 bool_t
libnfs_zdr_void(void)
178 bool_t
libnfs_zdr_pointer(ZDR
*zdrs
, char **objp
, uint32_t size
, zdrproc_t proc
)
182 more_data
= (*objp
!= NULL
);
184 if (!libnfs_zdr_bool(zdrs
, &more_data
)) {
187 if (more_data
== 0) {
192 if (zdrs
->x_op
== ZDR_DECODE
) {
193 *objp
= zdr_malloc(zdrs
, size
);
197 memset(*objp
, 0, size
);
199 return proc(zdrs
, *objp
);
202 bool_t
libnfs_zdr_opaque(ZDR
*zdrs
, char *objp
, uint32_t size
)
204 switch (zdrs
->x_op
) {
206 memcpy(&zdrs
->buf
[zdrs
->pos
], objp
, size
);
208 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
211 memcpy(objp
, &zdrs
->buf
[zdrs
->pos
], size
);
213 zdrs
->pos
= (zdrs
->pos
+ 3) & ~3;
220 bool_t
libnfs_zdr_string(ZDR
*zdrs
, char **strp
, uint32_t maxsize
)
224 if (zdrs
->x_op
== ZDR_ENCODE
) {
225 size
= strlen(*strp
);
228 if (!libnfs_zdr_u_int(zdrs
, &size
)) {
232 if (zdrs
->pos
+ size
> zdrs
->size
) {
236 switch (zdrs
->x_op
) {
238 return libnfs_zdr_opaque(zdrs
, *strp
, size
);
240 *strp
= zdr_malloc(zdrs
, size
+ 1);
245 return libnfs_zdr_opaque(zdrs
, *strp
, size
);
251 bool_t
libnfs_zdr_array(ZDR
*zdrs
, char **arrp
, uint32_t *size
, uint32_t maxsize
, uint32_t elsize
, zdrproc_t proc
)
255 if (!libnfs_zdr_u_int(zdrs
, size
)) {
259 if (zdrs
->pos
+ *size
* elsize
> zdrs
->size
) {
263 if (zdrs
->x_op
== ZDR_DECODE
) {
264 *arrp
= zdr_malloc(zdrs
, *size
* elsize
);
268 memset(*arrp
, 0, *size
* elsize
);
271 for (i
= 0; i
< *size
; i
++) {
272 if (proc(zdrs
, *arrp
+ i
* elsize
)) {
279 void libnfs_zdr_free(zdrproc_t proc
, char *objp
)
283 static bool_t
libnfs_opaque_auth(ZDR
*zdrs
, struct opaque_auth
*auth
)
285 if (!libnfs_zdr_u_int(zdrs
, &auth
->oa_flavor
)) {
289 if (!libnfs_zdr_bytes(zdrs
, &auth
->oa_base
, &auth
->oa_length
, &auth
->oa_length
)) {
296 static bool_t
libnfs_rpc_call_body(ZDR
*zdrs
, struct call_body
*cmb
)
298 if (!libnfs_zdr_u_int(zdrs
, &cmb
->cb_rpcvers
)) {
302 if (!libnfs_zdr_u_int(zdrs
, &cmb
->cb_prog
)) {
306 if (!libnfs_zdr_u_int(zdrs
, &cmb
->cb_vers
)) {
310 if (!libnfs_zdr_u_int(zdrs
, &cmb
->cb_proc
)) {
314 if (!libnfs_opaque_auth(zdrs
, &cmb
->cb_cred
)) {
318 if (!libnfs_opaque_auth(zdrs
, &cmb
->cb_verf
)) {
323 static bool_t
libnfs_accepted_reply(ZDR
*zdrs
, struct accepted_reply
*ar
)
325 if (!libnfs_opaque_auth(zdrs
, &ar
->ar_verf
)) {
329 if (!libnfs_zdr_u_int(zdrs
, &ar
->ar_stat
)) {
333 switch (ar
->ar_stat
) {
335 if (!ar
->ar_results
.proc(zdrs
, ar
->ar_results
.where
)) {
340 if (!libnfs_zdr_u_int(zdrs
, &ar
->ar_vers
.low
)) {
343 if (!libnfs_zdr_u_int(zdrs
, &ar
->ar_vers
.high
)) {
354 static bool_t
libnfs_rejected_reply(ZDR
*zdrs
, struct rejected_reply
*RP_dr
)
356 printf("rejected reply\n");
360 static bool_t
libnfs_rpc_reply_body(ZDR
*zdrs
, struct reply_body
*rmb
)
362 if (!libnfs_zdr_u_int(zdrs
, &rmb
->rp_stat
)) {
366 switch (rmb
->rp_stat
) {
368 if (!libnfs_accepted_reply(zdrs
, &rmb
->rp_acpt
)) {
373 if (!libnfs_rejected_reply(zdrs
, &rmb
->rp_rjct
)) {
382 static bool_t
libnfs_rpc_msg(ZDR
*zdrs
, struct rpc_msg
*msg
)
384 if (!libnfs_zdr_u_int(zdrs
, &msg
->rm_xid
)) {
388 if (!libnfs_zdr_u_int(zdrs
, &msg
->rm_direction
)) {
392 switch (msg
->rm_direction
) {
394 return libnfs_rpc_call_body(zdrs
, &msg
->ru
.RM_cmb
);
397 return libnfs_rpc_reply_body(zdrs
, &msg
->ru
.RM_rmb
);
404 bool_t
libnfs_zdr_callmsg(ZDR
*zdrs
, struct rpc_msg
*msg
)
406 return libnfs_rpc_msg(zdrs
, msg
);
409 bool_t
libnfs_zdr_replymsg(ZDR
*zdrs
, struct rpc_msg
*msg
)
411 return libnfs_rpc_msg(zdrs
, msg
);
414 AUTH
*authnone_create(void)
418 auth
= malloc(sizeof(AUTH
));
420 auth
->ah_cred
.oa_flavor
= AUTH_NONE
;
421 auth
->ah_cred
.oa_length
= 0;
422 auth
->ah_cred
.oa_base
= NULL
;
424 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
425 auth
->ah_verf
.oa_length
= 0;
426 auth
->ah_verf
.oa_base
= NULL
;
428 auth
->ah_private
= NULL
;
433 AUTH
*libnfs_authunix_create(char *host
, uint32_t uid
, uint32_t gid
, uint32_t len
, uint32_t *groups
)
440 size
= 4 + 4 + ((strlen(host
) + 3) & ~3) + 4 + 4 + 4 + len
* 4;
441 auth
= malloc(sizeof(AUTH
));
442 auth
->ah_cred
.oa_flavor
= AUTH_UNIX
;
443 auth
->ah_cred
.oa_length
= size
;
444 auth
->ah_cred
.oa_base
= malloc(size
);
446 buf
= auth
->ah_cred
.oa_base
;
448 buf
[idx
++] = htonl(time(NULL
));
449 buf
[idx
++] = htonl(strlen(host
));
450 memcpy(&buf
[2], host
, strlen(host
));
452 idx
+= (strlen(host
) + 3) >> 2;
453 buf
[idx
++] = htonl(uid
);
454 buf
[idx
++] = htonl(gid
);
455 buf
[idx
++] = htonl(len
);
457 buf
[idx
++] = htonl(*groups
++);
460 auth
->ah_verf
.oa_flavor
= AUTH_NONE
;
461 auth
->ah_verf
.oa_length
= 0;
462 auth
->ah_verf
.oa_base
= NULL
;
464 auth
->ah_private
= NULL
;
469 AUTH
*libnfs_authunix_create_default(void)
471 return libnfs_authunix_create("libnfs", getuid(), -1, 0, NULL
);
474 void libnfs_auth_destroy(AUTH
*auth
)
476 if (auth
->ah_cred
.oa_base
) {
477 free(auth
->ah_cred
.oa_base
);
479 if (auth
->ah_verf
.oa_base
) {
480 free(auth
->ah_verf
.oa_base
);