| 1 | /* |
| 2 | Copyright (C) 2012 by Ronnie Sahlberg <ronniesahlberg@gmail.com> |
| 3 | |
| 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. |
| 8 | |
| 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. |
| 13 | |
| 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/>. |
| 16 | */ |
| 17 | /* |
| 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. |
| 22 | */ |
| 23 | |
| 24 | /************************************************************ |
| 25 | * Definitions copied from RFC 5531 |
| 26 | * and slightly modified. |
| 27 | ************************************************************/ |
| 28 | |
| 29 | #include "config.h" |
| 30 | |
| 31 | #ifndef _LIBNFS_ZDR_H_ |
| 32 | #define _LIBNFS_ZDR_H_ |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <assert.h> |
| 36 | #include <stdint.h> |
| 37 | #include <sys/types.h> |
| 38 | |
| 39 | #define _RPC_RPC_H 1 |
| 40 | #define _RPC_ZDR_H 1 |
| 41 | #define _RPC_AUTH_H 1 |
| 42 | |
| 43 | /* we dont need these */ |
| 44 | typedef void CLIENT; |
| 45 | struct svc_req { |
| 46 | }; |
| 47 | typedef void SVCXPRT; |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | #define ZDR_INLINE(...) NULL |
| 54 | #define IZDR_PUT_U_LONG(...) assert(0) |
| 55 | #define IZDR_GET_U_LONG(...) (assert(0), 0) |
| 56 | #define IZDR_PUT_LONG(...) assert(0) |
| 57 | #define IZDR_GET_LONG(...) (assert(0), 0) |
| 58 | #define IZDR_PUT_BOOL(...) assert(0) |
| 59 | #define IZDR_GET_BOOL(...) (assert(0), 0) |
| 60 | |
| 61 | #define TRUE 1 |
| 62 | #define FALSE 0 |
| 63 | |
| 64 | enum zdr_op { |
| 65 | ZDR_ENCODE = 0, |
| 66 | ZDR_DECODE = 1 |
| 67 | }; |
| 68 | |
| 69 | struct zdr_mem { |
| 70 | struct zdr_mem *next; |
| 71 | caddr_t buf; |
| 72 | uint32_t size; |
| 73 | }; |
| 74 | |
| 75 | struct ZDR { |
| 76 | enum zdr_op x_op; |
| 77 | caddr_t buf; |
| 78 | int size; |
| 79 | int pos; |
| 80 | struct zdr_mem *mem; |
| 81 | }; |
| 82 | typedef struct ZDR ZDR; |
| 83 | |
| 84 | |
| 85 | typedef uint32_t u_int; |
| 86 | typedef uint32_t enum_t; |
| 87 | typedef uint32_t bool_t; |
| 88 | |
| 89 | typedef int (*zdrproc_t) (ZDR *, void *,...); |
| 90 | |
| 91 | /* XXX find out what we can get rid of */ |
| 92 | |
| 93 | #define AUTH_NONE 0 |
| 94 | #define AUTH_NULL 0 |
| 95 | #define AUTH_UNIX 1 |
| 96 | |
| 97 | struct opaque_auth { |
| 98 | uint32_t oa_flavor; |
| 99 | caddr_t oa_base; |
| 100 | uint32_t oa_length; |
| 101 | }; |
| 102 | extern struct opaque_auth _null_auth; |
| 103 | |
| 104 | struct AUTH { |
| 105 | struct opaque_auth ah_cred; |
| 106 | struct opaque_auth ah_verf; |
| 107 | caddr_t ah_private; |
| 108 | }; |
| 109 | |
| 110 | #define RPC_MSG_VERSION 2 |
| 111 | |
| 112 | enum msg_type { |
| 113 | CALL = 0, |
| 114 | REPLY = 1 |
| 115 | }; |
| 116 | |
| 117 | enum reply_stat { |
| 118 | MSG_ACCEPTED=0, |
| 119 | MSG_DENIED=1 |
| 120 | }; |
| 121 | |
| 122 | enum accept_stat { |
| 123 | SUCCESS = 0, |
| 124 | PROG_UNAVAIL = 1, |
| 125 | PROG_MISMATCH = 2, |
| 126 | PROC_UNAVAIL = 3, |
| 127 | GARBAGE_ARGS = 4, |
| 128 | SYSTEM_ERR = 5 |
| 129 | }; |
| 130 | |
| 131 | enum reject_stat { |
| 132 | RPC_MISMATCH = 0, |
| 133 | AUTH_ERROR = 1 |
| 134 | }; |
| 135 | |
| 136 | enum auth_stat { |
| 137 | AUTH_OK=0, |
| 138 | /* |
| 139 | * failed at remote end |
| 140 | */ |
| 141 | AUTH_BADCRED = 1, /* bogus credentials (seal broken) */ |
| 142 | AUTH_REJECTEDCRED = 2, /* client should begin new session */ |
| 143 | AUTH_BADVERF = 3, /* bogus verifier (seal broken) */ |
| 144 | AUTH_REJECTEDVERF = 4, /* verifier expired or was replayed */ |
| 145 | AUTH_TOOWEAK = 5, /* rejected due to security reasons */ |
| 146 | /* |
| 147 | * failed locally |
| 148 | */ |
| 149 | AUTH_INVALIDRESP = 6, /* bogus response verifier */ |
| 150 | AUTH_FAILED = 7 /* some unknown reason */ |
| 151 | }; |
| 152 | |
| 153 | struct call_body { |
| 154 | uint32_t rpcvers; |
| 155 | uint32_t prog; |
| 156 | uint32_t vers; |
| 157 | uint32_t proc; |
| 158 | struct opaque_auth cred; |
| 159 | struct opaque_auth verf; |
| 160 | }; |
| 161 | |
| 162 | struct accepted_reply { |
| 163 | struct opaque_auth verf; |
| 164 | uint32_t stat; |
| 165 | union { |
| 166 | struct { |
| 167 | caddr_t where; |
| 168 | zdrproc_t proc; |
| 169 | } results; |
| 170 | struct { |
| 171 | uint32_t low; |
| 172 | uint32_t high; |
| 173 | } mismatch_info; |
| 174 | } reply_data; |
| 175 | }; |
| 176 | |
| 177 | struct rejected_reply { |
| 178 | enum reject_stat stat; |
| 179 | union { |
| 180 | struct { |
| 181 | u_long low; |
| 182 | u_long high; |
| 183 | } mismatch_info; |
| 184 | enum auth_stat stat; |
| 185 | } reject_data; |
| 186 | }; |
| 187 | |
| 188 | struct reply_body { |
| 189 | uint32_t stat; |
| 190 | union { |
| 191 | struct accepted_reply areply; |
| 192 | struct rejected_reply rreply; |
| 193 | } reply; |
| 194 | }; |
| 195 | |
| 196 | struct rpc_msg { |
| 197 | uint32_t xid; |
| 198 | |
| 199 | uint32_t direction; |
| 200 | union { |
| 201 | struct call_body cbody; |
| 202 | struct reply_body rbody; |
| 203 | } body; |
| 204 | }; |
| 205 | |
| 206 | #define zdrmem_create libnfs_zdrmem_create |
| 207 | void libnfs_zdrmem_create(ZDR *zdrs, const caddr_t addr, uint32_t size, enum zdr_op xop); |
| 208 | |
| 209 | #define zdr_destroy libnfs_zdr_destroy |
| 210 | void libnfs_zdr_destroy(ZDR *zdrs); |
| 211 | |
| 212 | #define zdr_bytes libnfs_zdr_bytes |
| 213 | bool_t libnfs_zdr_bytes(ZDR *zdrs, char **bufp, uint32_t *size, uint32_t maxsize); |
| 214 | |
| 215 | #define zdr_u_int libnfs_zdr_u_int |
| 216 | bool_t libnfs_zdr_u_int(ZDR *zdrs, uint32_t *u); |
| 217 | |
| 218 | #define zdr_int libnfs_zdr_int |
| 219 | bool_t libnfs_zdr_int(ZDR *zdrs, int32_t *i); |
| 220 | |
| 221 | #define zdr_u_quad_t libnfs_zdr_u_quad_t |
| 222 | bool_t libnfs_zdr_u_quad_t(ZDR *zdrs, uint64_t *u); |
| 223 | |
| 224 | #define zdr_quad_t libnfs_zdr_quad_t |
| 225 | bool_t libnfs_zdr_quad_t(ZDR *zdrs, int64_t *i); |
| 226 | |
| 227 | #define zdr_enum libnfs_zdr_enum |
| 228 | bool_t libnfs_zdr_enum(ZDR *zdrs, enum_t *e); |
| 229 | |
| 230 | #define zdr_bool libnfs_zdr_bool |
| 231 | bool_t libnfs_zdr_bool(ZDR *zdrs, bool_t *b); |
| 232 | |
| 233 | #define zdr_void libnfs_zdr_void |
| 234 | bool_t libnfs_zdr_void(void); |
| 235 | |
| 236 | #define zdr_pointer libnfs_zdr_pointer |
| 237 | bool_t libnfs_zdr_pointer(ZDR *zdrs, char **objp, uint32_t size, zdrproc_t proc); |
| 238 | |
| 239 | #define zdr_opaque libnfs_zdr_opaque |
| 240 | bool_t libnfs_zdr_opaque(ZDR *zdrs, char *objp, uint32_t size); |
| 241 | |
| 242 | #define zdr_string libnfs_zdr_string |
| 243 | bool_t libnfs_zdr_string(ZDR *zdrs, char **strp, uint32_t maxsize); |
| 244 | |
| 245 | #define zdr_array libnfs_zdr_array |
| 246 | bool_t libnfs_zdr_array(ZDR *zdrs, char **arrp, uint32_t *size, uint32_t maxsize, uint32_t elsize, zdrproc_t proc); |
| 247 | |
| 248 | #define zdr_setpos libnfs_zdr_setpos |
| 249 | bool_t libnfs_zdr_setpos(ZDR *zdrs, uint32_t pos); |
| 250 | |
| 251 | #define zdr_getpos libnfs_zdr_getpos |
| 252 | uint32_t libnfs_zdr_getpos(ZDR *zdrs); |
| 253 | |
| 254 | #define zdr_free libnfs_zdr_free |
| 255 | void libnfs_zdr_free(zdrproc_t proc, char *objp); |
| 256 | |
| 257 | #define zdr_callmsg libnfs_zdr_callmsg |
| 258 | bool_t libnfs_zdr_callmsg(ZDR *zdrs, struct rpc_msg *msg); |
| 259 | |
| 260 | #define zdr_replymsg libnfs_zdr_replymsg |
| 261 | bool_t libnfs_zdr_replymsg(ZDR *zdrs, struct rpc_msg *msg); |
| 262 | |
| 263 | #define authnone_create libnfs_authnone_create |
| 264 | struct AUTH *libnfs_authnone_create(void); |
| 265 | |
| 266 | #define authunix_create libnfs_authunix_create |
| 267 | struct AUTH *libnfs_authunix_create(char *host, uint32_t uid, uint32_t gid, uint32_t len, uint32_t *groups); |
| 268 | |
| 269 | #define authunix_create_default libnfs_authunix_create_default |
| 270 | struct AUTH *libnfs_authunix_create_default(void); |
| 271 | |
| 272 | #define auth_destroy libnfs_auth_destroy |
| 273 | void libnfs_auth_destroy(struct AUTH *auth); |
| 274 | |
| 275 | #endif |