| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <stdarg.h> |
| 40 |
#include <string.h> |
| 41 |
#include <rpc/types.h> |
| 42 |
#include <rpc/xdr.h> |
| 43 |
#include <rpc/auth.h> |
| 44 |
#include <rpc/auth_gss.h> |
| 45 |
#include <rpc/rpc.h> |
| 46 |
#include <ctype.h> |
| 47 |
#include <gssapi/gssapi.h> |
| 48 |
|
| 49 |
#include "debug.h" |
| 50 |
|
| 51 |
|
| 52 |
#define RPC_SLACK_SPACE 1024 |
| 53 |
|
| 54 |
bool_t |
| 55 |
xdr_rpc_gss_buf(XDR *xdrs, gss_buffer_t buf, u_int maxsize) |
| 56 |
{ |
| 57 |
bool_t xdr_stat; |
| 58 |
u_int tmplen; |
| 59 |
|
| 60 |
if (xdrs->x_op != XDR_DECODE) { |
| 61 |
if (buf->length > UINT_MAX) |
| 62 |
return FALSE; |
| 63 |
else |
| 64 |
tmplen = buf->length; |
| 65 |
} |
| 66 |
xdr_stat = xdr_bytes(xdrs, (char **)&buf->value, &tmplen, maxsize); |
| 67 |
|
| 68 |
if (xdr_stat && xdrs->x_op == XDR_DECODE) |
| 69 |
buf->length = tmplen; |
| 70 |
|
| 71 |
gss_log_debug("xdr_rpc_gss_buf: %s %s (%p:%lu)", |
| 72 |
(xdrs->x_op == XDR_ENCODE) ? "encode" : "decode", |
| 73 |
(xdr_stat == TRUE) ? "success" : "failure", |
| 74 |
buf->value, buf->length); |
| 75 |
|
| 76 |
return xdr_stat; |
| 77 |
} |
| 78 |
|
| 79 |
bool_t |
| 80 |
xdr_rpc_gss_cred(XDR *xdrs, struct rpc_gss_cred *p) |
| 81 |
{ |
| 82 |
bool_t xdr_stat; |
| 83 |
|
| 84 |
xdr_stat = (xdr_u_int(xdrs, &p->gc_v) && |
| 85 |
xdr_enum(xdrs, (enum_t *)&p->gc_proc) && |
| 86 |
xdr_u_int(xdrs, &p->gc_seq) && |
| 87 |
xdr_enum(xdrs, (enum_t *)&p->gc_svc) && |
| 88 |
xdr_rpc_gss_buf(xdrs, &p->gc_ctx, MAX_AUTH_BYTES)); |
| 89 |
|
| 90 |
gss_log_debug("xdr_rpc_gss_cred: %s %s " |
| 91 |
"(v %d, proc %d, seq %d, svc %d, ctx %p:%lu)", |
| 92 |
(xdrs->x_op == XDR_ENCODE) ? "encode" : "decode", |
| 93 |
(xdr_stat == TRUE) ? "success" : "failure", |
| 94 |
p->gc_v, p->gc_proc, p->gc_seq, p->gc_svc, |
| 95 |
p->gc_ctx.value, p->gc_ctx.length); |
| 96 |
|
| 97 |
return (xdr_stat); |
| 98 |
} |
| 99 |
|
| 100 |
bool_t |
| 101 |
xdr_rpc_gss_init_args(XDR *xdrs, gss_buffer_desc *p) |
| 102 |
{ |
| 103 |
bool_t xdr_stat; |
| 104 |
u_int maxlen = (u_int)(p->length + RPC_SLACK_SPACE); |
| 105 |
|
| 106 |
xdr_stat = xdr_rpc_gss_buf(xdrs, p, maxlen); |
| 107 |
|
| 108 |
gss_log_debug("xdr_rpc_gss_init_args: %s %s (token %p:%lu)", |
| 109 |
(xdrs->x_op == XDR_ENCODE) ? "encode" : "decode", |
| 110 |
(xdr_stat == TRUE) ? "success" : "failure", |
| 111 |
p->value, p->length); |
| 112 |
|
| 113 |
return (xdr_stat); |
| 114 |
} |
| 115 |
|
| 116 |
bool_t |
| 117 |
xdr_rpc_gss_init_res(XDR *xdrs, struct rpc_gss_init_res *p) |
| 118 |
{ |
| 119 |
bool_t xdr_stat; |
| 120 |
|
| 121 |
u_int ctx_maxlen = (u_int)(p->gr_ctx.length + RPC_SLACK_SPACE); |
| 122 |
u_int tok_maxlen = (u_int)(p->gr_token.length + RPC_SLACK_SPACE); |
| 123 |
|
| 124 |
xdr_stat = (xdr_rpc_gss_buf(xdrs, &p->gr_ctx, ctx_maxlen) && |
| 125 |
xdr_u_int(xdrs, &p->gr_major) && |
| 126 |
xdr_u_int(xdrs, &p->gr_minor) && |
| 127 |
xdr_u_int(xdrs, &p->gr_win) && |
| 128 |
xdr_rpc_gss_buf(xdrs, &p->gr_token, tok_maxlen)); |
| 129 |
|
| 130 |
gss_log_debug("xdr_rpc_gss_init_res %s %s " |
| 131 |
"(ctx %p:%lu, maj %d, min %d, win %d, token %p:%lu)", |
| 132 |
(xdrs->x_op == XDR_ENCODE) ? "encode" : "decode", |
| 133 |
(xdr_stat == TRUE) ? "success" : "failure", |
| 134 |
p->gr_ctx.value, p->gr_ctx.length, |
| 135 |
p->gr_major, p->gr_minor, p->gr_win, |
| 136 |
p->gr_token.value, p->gr_token.length); |
| 137 |
|
| 138 |
return (xdr_stat); |
| 139 |
} |
| 140 |
|
| 141 |
bool_t |
| 142 |
xdr_rpc_gss_wrap_data(XDR *xdrs, xdrproc_t xdr_func, caddr_t xdr_ptr, |
| 143 |
gss_ctx_id_t ctx, gss_qop_t qop, |
| 144 |
rpc_gss_svc_t svc, u_int seq) |
| 145 |
{ |
| 146 |
gss_buffer_desc databuf, wrapbuf; |
| 147 |
OM_uint32 maj_stat, min_stat; |
| 148 |
int start, end, conf_state; |
| 149 |
bool_t xdr_stat; |
| 150 |
u_int databuflen, maxwrapsz; |
| 151 |
|
| 152 |
|
| 153 |
start = XDR_GETPOS(xdrs); |
| 154 |
XDR_SETPOS(xdrs, start + 4); |
| 155 |
|
| 156 |
memset(&databuf, 0, sizeof(databuf)); |
| 157 |
memset(&wrapbuf, 0, sizeof(wrapbuf)); |
| 158 |
|
| 159 |
|
| 160 |
if (!xdr_u_int(xdrs, &seq) || !(*xdr_func)(xdrs, xdr_ptr)) |
| 161 |
return (FALSE); |
| 162 |
end = XDR_GETPOS(xdrs); |
| 163 |
|
| 164 |
|
| 165 |
databuflen = end - start - 4; |
| 166 |
XDR_SETPOS(xdrs, start + 4); |
| 167 |
databuf.value = XDR_INLINE(xdrs, databuflen); |
| 168 |
databuf.length = databuflen; |
| 169 |
|
| 170 |
xdr_stat = FALSE; |
| 171 |
|
| 172 |
if (svc == RPCSEC_GSS_SVC_INTEGRITY) { |
| 173 |
|
| 174 |
XDR_SETPOS(xdrs, start); |
| 175 |
if (!xdr_u_int(xdrs, (u_int *)&databuflen)) |
| 176 |
return (FALSE); |
| 177 |
|
| 178 |
|
| 179 |
maj_stat = gss_get_mic(&min_stat, ctx, qop, |
| 180 |
&databuf, &wrapbuf); |
| 181 |
if (maj_stat != GSS_S_COMPLETE) { |
| 182 |
gss_log_status("xdr_rpc_gss_wrap_data: gss_get_mic", |
| 183 |
maj_stat, min_stat); |
| 184 |
return (FALSE); |
| 185 |
} |
| 186 |
|
| 187 |
XDR_SETPOS(xdrs, end); |
| 188 |
maxwrapsz = (u_int)(wrapbuf.length + RPC_SLACK_SPACE); |
| 189 |
xdr_stat = xdr_rpc_gss_buf(xdrs, &wrapbuf, maxwrapsz); |
| 190 |
gss_release_buffer(&min_stat, &wrapbuf); |
| 191 |
} |
| 192 |
else if (svc == RPCSEC_GSS_SVC_PRIVACY) { |
| 193 |
|
| 194 |
maj_stat = gss_wrap(&min_stat, ctx, TRUE, qop, &databuf, |
| 195 |
&conf_state, &wrapbuf); |
| 196 |
if (maj_stat != GSS_S_COMPLETE) { |
| 197 |
gss_log_status("xdr_rpc_gss_wrap_data: gss_wrap", |
| 198 |
maj_stat, min_stat); |
| 199 |
return (FALSE); |
| 200 |
} |
| 201 |
|
| 202 |
XDR_SETPOS(xdrs, start); |
| 203 |
maxwrapsz = (u_int)(wrapbuf.length + RPC_SLACK_SPACE); |
| 204 |
xdr_stat = xdr_rpc_gss_buf(xdrs, &wrapbuf, maxwrapsz); |
| 205 |
gss_release_buffer(&min_stat, &wrapbuf); |
| 206 |
} |
| 207 |
return (xdr_stat); |
| 208 |
} |
| 209 |
|
| 210 |
bool_t |
| 211 |
xdr_rpc_gss_unwrap_data(XDR *xdrs, xdrproc_t xdr_func, caddr_t xdr_ptr, |
| 212 |
gss_ctx_id_t ctx, gss_qop_t qop, |
| 213 |
rpc_gss_svc_t svc, u_int seq) |
| 214 |
{ |
| 215 |
XDR tmpxdrs; |
| 216 |
gss_buffer_desc databuf, wrapbuf; |
| 217 |
OM_uint32 maj_stat, min_stat; |
| 218 |
u_int seq_num, qop_state; |
| 219 |
int conf_state; |
| 220 |
bool_t xdr_stat; |
| 221 |
|
| 222 |
if (xdr_func == (xdrproc_t)xdr_void || xdr_ptr == NULL) |
| 223 |
return (TRUE); |
| 224 |
|
| 225 |
memset(&databuf, 0, sizeof(databuf)); |
| 226 |
memset(&wrapbuf, 0, sizeof(wrapbuf)); |
| 227 |
|
| 228 |
if (svc == RPCSEC_GSS_SVC_INTEGRITY) { |
| 229 |
|
| 230 |
if (!xdr_rpc_gss_buf(xdrs, &databuf, (u_int)-1)) { |
| 231 |
LIBTIRPC_DEBUG(1, ("xdr_rpc_gss_unwrap_data: decode databody_integ failed")); |
| 232 |
return (FALSE); |
| 233 |
} |
| 234 |
|
| 235 |
if (!xdr_rpc_gss_buf(xdrs, &wrapbuf, (u_int)-1)) { |
| 236 |
gss_release_buffer(&min_stat, &databuf); |
| 237 |
LIBTIRPC_DEBUG(1, ("xdr_rpc_gss_unwrap_data: decode checksum failed")); |
| 238 |
return (FALSE); |
| 239 |
} |
| 240 |
|
| 241 |
maj_stat = gss_verify_mic(&min_stat, ctx, &databuf, |
| 242 |
&wrapbuf, &qop_state); |
| 243 |
gss_release_buffer(&min_stat, &wrapbuf); |
| 244 |
|
| 245 |
if (maj_stat != GSS_S_COMPLETE || qop_state != qop) { |
| 246 |
gss_release_buffer(&min_stat, &databuf); |
| 247 |
gss_log_status("xdr_rpc_gss_unwrap_data: gss_verify_mic", |
| 248 |
maj_stat, min_stat); |
| 249 |
return (FALSE); |
| 250 |
} |
| 251 |
} |
| 252 |
else if (svc == RPCSEC_GSS_SVC_PRIVACY) { |
| 253 |
|
| 254 |
if (!xdr_rpc_gss_buf(xdrs, &wrapbuf, (u_int)-1)) { |
| 255 |
LIBTIRPC_DEBUG(1, ("xdr_rpc_gss_unwrap_data: decode databody_priv failed")); |
| 256 |
return (FALSE); |
| 257 |
} |
| 258 |
|
| 259 |
maj_stat = gss_unwrap(&min_stat, ctx, &wrapbuf, &databuf, |
| 260 |
&conf_state, &qop_state); |
| 261 |
|
| 262 |
gss_release_buffer(&min_stat, &wrapbuf); |
| 263 |
|
| 264 |
|
| 265 |
if (maj_stat != GSS_S_COMPLETE || qop_state != qop || |
| 266 |
conf_state != TRUE) { |
| 267 |
gss_release_buffer(&min_stat, &databuf); |
| 268 |
gss_log_status("xdr_rpc_gss_unwrap_data: gss_unwrap", |
| 269 |
maj_stat, min_stat); |
| 270 |
return (FALSE); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
xdrmem_create(&tmpxdrs, databuf.value, databuf.length, XDR_DECODE); |
| 275 |
xdr_stat = (xdr_u_int(&tmpxdrs, &seq_num) && |
| 276 |
(*xdr_func)(&tmpxdrs, xdr_ptr)); |
| 277 |
XDR_DESTROY(&tmpxdrs); |
| 278 |
gss_release_buffer(&min_stat, &databuf); |
| 279 |
|
| 280 |
|
| 281 |
if (xdr_stat == TRUE && seq_num != seq) { |
| 282 |
LIBTIRPC_DEBUG(1, |
| 283 |
("xdr_rpc_gss_unwrap_data: wrong sequence number in databody")); |
| 284 |
return (FALSE); |
| 285 |
} |
| 286 |
return (xdr_stat); |
| 287 |
} |
| 288 |
|
| 289 |
bool_t |
| 290 |
xdr_rpc_gss_data(XDR *xdrs, xdrproc_t xdr_func, caddr_t xdr_ptr, |
| 291 |
gss_ctx_id_t ctx, gss_qop_t qop, |
| 292 |
rpc_gss_svc_t svc, u_int seq) |
| 293 |
{ |
| 294 |
switch (xdrs->x_op) { |
| 295 |
|
| 296 |
case XDR_ENCODE: |
| 297 |
return (xdr_rpc_gss_wrap_data(xdrs, xdr_func, xdr_ptr, |
| 298 |
ctx, qop, svc, seq)); |
| 299 |
case XDR_DECODE: |
| 300 |
return (xdr_rpc_gss_unwrap_data(xdrs, xdr_func, xdr_ptr, |
| 301 |
ctx, qop,svc, seq)); |
| 302 |
case XDR_FREE: |
| 303 |
return (TRUE); |
| 304 |
} |
| 305 |
return (FALSE); |
| 306 |
} |
| 307 |
|
| 308 |
void |
| 309 |
gss_log_debug(const char *fmt, ...) |
| 310 |
{ |
| 311 |
va_list ap; |
| 312 |
|
| 313 |
va_start(ap, fmt); |
| 314 |
vlibtirpc_log_dbg(2, fmt, ap); |
| 315 |
va_end(ap); |
| 316 |
} |
| 317 |
|
| 318 |
void |
| 319 |
gss_log_status(char *m, OM_uint32 maj_stat, OM_uint32 min_stat) |
| 320 |
{ |
| 321 |
OM_uint32 min, maj; |
| 322 |
gss_buffer_desc maj_msg, min_msg; |
| 323 |
u_int32_t msg_ctx = 0; |
| 324 |
|
| 325 |
gss_display_status(&maj, maj_stat, GSS_C_GSS_CODE, GSS_C_NULL_OID, |
| 326 |
&msg_ctx, &maj_msg); |
| 327 |
gss_display_status(&min, min_stat, GSS_C_MECH_CODE, GSS_C_NULL_OID, |
| 328 |
&msg_ctx, &min_msg); |
| 329 |
|
| 330 |
LIBTIRPC_DEBUG(1, ("%s: %s - %s", m, (char *)maj_msg.value, (char *)min_msg.value)); |
| 331 |
|
| 332 |
gss_release_buffer(&maj, &maj_msg); |
| 333 |
gss_release_buffer(&min, &min_msg); |
| 334 |
} |
| 335 |
|
| 336 |
void |
| 337 |
gss_log_hexdump(const u_char *buf, int len, int offset) |
| 338 |
{ |
| 339 |
u_int i, j, jm; |
| 340 |
int c; |
| 341 |
|
| 342 |
if (libtirpc_debug_level < 4 || log_stderr == 0) |
| 343 |
return; |
| 344 |
|
| 345 |
fprintf(stderr, "\n"); |
| 346 |
for (i = 0; i < len; i += 0x10) { |
| 347 |
fprintf(stderr, " %04x: ", (u_int)(i + offset)); |
| 348 |
jm = len - i; |
| 349 |
jm = jm > 16 ? 16 : jm; |
| 350 |
|
| 351 |
for (j = 0; j < jm; j++) { |
| 352 |
if ((j % 2) == 1) |
| 353 |
fprintf(stderr, "%02x ", (u_int) buf[i+j]); |
| 354 |
else |
| 355 |
fprintf(stderr, "%02x", (u_int) buf[i+j]); |
| 356 |
} |
| 357 |
for (; j < 16; j++) { |
| 358 |
if ((j % 2) == 1) printf(" "); |
| 359 |
else fprintf(stderr, " "); |
| 360 |
} |
| 361 |
fprintf(stderr, " "); |
| 362 |
|
| 363 |
for (j = 0; j < jm; j++) { |
| 364 |
c = buf[i+j]; |
| 365 |
c = isprint(c) ? c : '.'; |
| 366 |
fprintf(stderr, "%c", c); |
| 367 |
} |
| 368 |
fprintf(stderr, "\n"); |
| 369 |
} |
| 370 |
} |
| 371 |
|