| 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 <sys/types.h> |
| 38 |
|
| 39 |
#include <unistd.h> |
| 40 |
#include <stdio.h> |
| 41 |
#include <stdlib.h> |
| 42 |
#include <string.h> |
| 43 |
#include <pwd.h> |
| 44 |
#include <grp.h> |
| 45 |
|
| 46 |
#include <rpc/rpc.h> |
| 47 |
#include <rpc/auth_gss.h> |
| 48 |
#include <rpc/svc_auth_gss.h> |
| 49 |
#include <rpc/rpcsec_gss.h> |
| 50 |
|
| 51 |
#ifdef HAVE_GSSAPI_GSSAPI_EXT_H |
| 52 |
|
| 53 |
#include <gssapi/gssapi_ext.h> |
| 54 |
#else |
| 55 |
|
| 56 |
#include <gssapi/gssapi.h> |
| 57 |
#endif |
| 58 |
|
| 59 |
#include <reentrant.h> |
| 60 |
|
| 61 |
#define UNUSED(x) UNUSED_ ## x __attribute__((unused)) |
| 62 |
|
| 63 |
extern SVCAUTH svc_auth_none; |
| 64 |
|
| 65 |
|
| 66 |
bool_t rpc_gss_oid_to_mech(rpc_gss_OID, char **); |
| 67 |
bool_t rpc_gss_num_to_qop(char *, u_int, char **); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
typedef struct gss_union_ctx_id_t { |
| 74 |
gss_OID mech_type; |
| 75 |
gss_ctx_id_t internal_ctx_id; |
| 76 |
} gss_union_ctx_id_desc, *gss_union_ctx_id_t; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static bool_t svcauth_gss_wrap(SVCAUTH *, XDR *, xdrproc_t, caddr_t); |
| 81 |
static bool_t svcauth_gss_unwrap(SVCAUTH *, XDR *, xdrproc_t, caddr_t); |
| 82 |
static bool_t svcauth_gss_destroy(SVCAUTH *); |
| 83 |
|
| 84 |
static struct svc_auth_ops svc_auth_gss_ops = { |
| 85 |
svcauth_gss_wrap, |
| 86 |
svcauth_gss_unwrap, |
| 87 |
svcauth_gss_destroy |
| 88 |
}; |
| 89 |
|
| 90 |
struct svcauth_gss_cache_entry { |
| 91 |
SVCXPRT *xprt; |
| 92 |
struct svc_rpc_gss_data *gd; |
| 93 |
time_t time_to_die; |
| 94 |
struct svcauth_gss_cache_entry *next; |
| 95 |
}; |
| 96 |
|
| 97 |
extern pthread_mutex_t svcauth_gss_cache_lock; |
| 98 |
static struct svcauth_gss_cache_entry *svcauth_gss_cache = NULL; |
| 99 |
|
| 100 |
struct svc_rpc_gss_callback { |
| 101 |
struct svc_rpc_gss_callback *cb_next; |
| 102 |
rpc_gss_callback_t cb_args; |
| 103 |
}; |
| 104 |
|
| 105 |
extern pthread_mutex_t svcauth_cb_lock; |
| 106 |
static struct svc_rpc_gss_callback *_svcauth_callbacks = NULL; |
| 107 |
|
| 108 |
struct svc_rpc_gss_data { |
| 109 |
bool_t established; |
| 110 |
bool_t locked; |
| 111 |
gss_ctx_id_t ctx; |
| 112 |
gss_cred_id_t deleg; |
| 113 |
struct rpc_gss_sec sec; |
| 114 |
gss_buffer_desc cname; |
| 115 |
u_int seq; |
| 116 |
u_int win; |
| 117 |
u_int seqlast; |
| 118 |
u_int32_t seqmask; |
| 119 |
gss_name_t client_name; |
| 120 |
rpc_gss_rawcred_t rcred; |
| 121 |
rpc_gss_rawcred_t scratch; |
| 122 |
rpc_gss_ucred_t ucred; |
| 123 |
gid_t gids[NGRPS]; |
| 124 |
bool_t callback_done; |
| 125 |
void * cookie; |
| 126 |
}; |
| 127 |
|
| 128 |
#define SVCAUTH_PRIVATE(auth) \ |
| 129 |
((struct svc_rpc_gss_data *)(auth)->svc_ah_private) |
| 130 |
|
| 131 |
|
| 132 |
static u_int _svcauth_req_time = 0; |
| 133 |
static gss_OID_set_desc _svcauth_oid_set = {1, GSS_C_NULL_OID }; |
| 134 |
static gss_cred_id_t _svcauth_gss_creds; |
| 135 |
static gss_name_t _svcauth_gss_name = GSS_C_NO_NAME; |
| 136 |
static char * _svcauth_svc_name = NULL; |
| 137 |
|
| 138 |
bool_t |
| 139 |
svcauth_gss_set_svc_name(gss_name_t name) |
| 140 |
{ |
| 141 |
OM_uint32 maj_stat, min_stat; |
| 142 |
|
| 143 |
gss_log_debug("in svcauth_gss_set_svc_name()"); |
| 144 |
|
| 145 |
if (_svcauth_gss_name != GSS_C_NO_NAME) { |
| 146 |
maj_stat = gss_release_name(&min_stat, &_svcauth_gss_name); |
| 147 |
|
| 148 |
if (maj_stat != GSS_S_COMPLETE) { |
| 149 |
gss_log_status("svcauth_gss_set_svc_name: gss_release_name", |
| 150 |
maj_stat, min_stat); |
| 151 |
return (FALSE); |
| 152 |
} |
| 153 |
_svcauth_gss_name = GSS_C_NO_NAME; |
| 154 |
} |
| 155 |
maj_stat = gss_duplicate_name(&min_stat, name, &_svcauth_gss_name); |
| 156 |
|
| 157 |
if (maj_stat != GSS_S_COMPLETE) { |
| 158 |
gss_log_status("svcauth_gss_set_svc_name: gss_duplicate_name", |
| 159 |
maj_stat, min_stat); |
| 160 |
return (FALSE); |
| 161 |
} |
| 162 |
|
| 163 |
return (TRUE); |
| 164 |
} |
| 165 |
|
| 166 |
static bool_t |
| 167 |
svcauth_gss_import_name(char *service) |
| 168 |
{ |
| 169 |
gss_name_t name; |
| 170 |
gss_buffer_desc namebuf; |
| 171 |
OM_uint32 maj_stat, min_stat; |
| 172 |
bool_t result; |
| 173 |
|
| 174 |
gss_log_debug("in svcauth_gss_import_name()"); |
| 175 |
|
| 176 |
namebuf.value = service; |
| 177 |
namebuf.length = strlen(service); |
| 178 |
|
| 179 |
maj_stat = gss_import_name(&min_stat, &namebuf, |
| 180 |
(gss_OID)GSS_C_NT_HOSTBASED_SERVICE, &name); |
| 181 |
|
| 182 |
if (maj_stat != GSS_S_COMPLETE) { |
| 183 |
gss_log_status("svcauth_gss_import_name: gss_import_name", |
| 184 |
maj_stat, min_stat); |
| 185 |
return (FALSE); |
| 186 |
} |
| 187 |
result = svcauth_gss_set_svc_name(name); |
| 188 |
gss_release_name(&min_stat, &name); |
| 189 |
return result; |
| 190 |
} |
| 191 |
|
| 192 |
static bool_t |
| 193 |
svcauth_gss_acquire_cred(void) |
| 194 |
{ |
| 195 |
OM_uint32 maj_stat, min_stat; |
| 196 |
|
| 197 |
gss_log_debug("in svcauth_gss_acquire_cred()"); |
| 198 |
|
| 199 |
maj_stat = gss_acquire_cred(&min_stat, _svcauth_gss_name, |
| 200 |
_svcauth_req_time, &_svcauth_oid_set, |
| 201 |
GSS_C_ACCEPT, |
| 202 |
&_svcauth_gss_creds, NULL, NULL); |
| 203 |
|
| 204 |
if (maj_stat != GSS_S_COMPLETE) { |
| 205 |
gss_log_status("svcauth_gss_acquire_cred: gss_acquire_cred", |
| 206 |
maj_stat, min_stat); |
| 207 |
return (FALSE); |
| 208 |
} |
| 209 |
return (TRUE); |
| 210 |
} |
| 211 |
|
| 212 |
static bool_t |
| 213 |
svcauth_gss_release_cred(void) |
| 214 |
{ |
| 215 |
OM_uint32 maj_stat, min_stat; |
| 216 |
|
| 217 |
gss_log_debug("in svcauth_gss_release_cred()"); |
| 218 |
|
| 219 |
maj_stat = gss_release_cred(&min_stat, &_svcauth_gss_creds); |
| 220 |
|
| 221 |
if (maj_stat != GSS_S_COMPLETE) { |
| 222 |
gss_log_status("svcauth_gss_release_cred: gss_release_cred", |
| 223 |
maj_stat, min_stat); |
| 224 |
return (FALSE); |
| 225 |
} |
| 226 |
|
| 227 |
_svcauth_gss_creds = NULL; |
| 228 |
|
| 229 |
return (TRUE); |
| 230 |
} |
| 231 |
|
| 232 |
static rpc_gss_service_t |
| 233 |
_rpc_gss_svc_to_service(rpc_gss_svc_t svc) |
| 234 |
{ |
| 235 |
switch (svc) { |
| 236 |
case RPCSEC_GSS_SVC_NONE: |
| 237 |
return rpcsec_gss_svc_none; |
| 238 |
case RPCSEC_GSS_SVC_INTEGRITY: |
| 239 |
return rpcsec_gss_svc_integrity; |
| 240 |
case RPCSEC_GSS_SVC_PRIVACY: |
| 241 |
return rpcsec_gss_svc_privacy; |
| 242 |
} |
| 243 |
return rpcsec_gss_svc_default; |
| 244 |
} |
| 245 |
|
| 246 |
static bool_t |
| 247 |
_rpc_gss_fill_in_creds(struct svc_rpc_gss_data *gd, struct rpc_gss_cred *gc) |
| 248 |
{ |
| 249 |
rpc_gss_rawcred_t *rcred = &gd->rcred; |
| 250 |
|
| 251 |
rcred->version = gc->gc_v; |
| 252 |
if (!rpc_gss_oid_to_mech(gd->sec.mech, &rcred->mechanism)) |
| 253 |
return FALSE; |
| 254 |
rcred->service = _rpc_gss_svc_to_service(gd->sec.svc); |
| 255 |
|
| 256 |
rcred->client_principal = calloc(1, sizeof(rpc_gss_principal_t) + |
| 257 |
gd->cname.length); |
| 258 |
if (rcred->client_principal == NULL) |
| 259 |
return FALSE; |
| 260 |
|
| 261 |
rcred->client_principal->len = gd->cname.length; |
| 262 |
(void)memcpy(rcred->client_principal->name, |
| 263 |
gd->cname.value, gd->cname.length); |
| 264 |
|
| 265 |
rcred->svc_principal = _svcauth_svc_name; |
| 266 |
|
| 267 |
return TRUE; |
| 268 |
} |
| 269 |
|
| 270 |
static bool_t |
| 271 |
svcauth_gss_accept_sec_context(struct svc_req *rqst, |
| 272 |
struct rpc_gss_init_res *gr) |
| 273 |
{ |
| 274 |
struct svc_rpc_gss_data *gd; |
| 275 |
struct rpc_gss_cred *gc; |
| 276 |
gss_buffer_desc recv_tok, seqbuf, checksum; |
| 277 |
gss_OID mech; |
| 278 |
OM_uint32 maj_stat = 0, min_stat = 0, ret_flags, seq; |
| 279 |
|
| 280 |
gss_log_debug("in svcauth_gss_accept_context()"); |
| 281 |
|
| 282 |
gd = SVCAUTH_PRIVATE(&SVC_XP_AUTH(rqst->rq_xprt)); |
| 283 |
gc = (struct rpc_gss_cred *)rqst->rq_clntcred; |
| 284 |
memset(gr, 0, sizeof(*gr)); |
| 285 |
|
| 286 |
|
| 287 |
memset(&recv_tok, 0, sizeof(recv_tok)); |
| 288 |
|
| 289 |
if (!svc_getargs(rqst->rq_xprt, (xdrproc_t)xdr_rpc_gss_init_args, |
| 290 |
(caddr_t)&recv_tok)) |
| 291 |
return (FALSE); |
| 292 |
|
| 293 |
gr->gr_major = gss_accept_sec_context(&gr->gr_minor, |
| 294 |
&gd->ctx, |
| 295 |
_svcauth_gss_creds, |
| 296 |
&recv_tok, |
| 297 |
GSS_C_NO_CHANNEL_BINDINGS, |
| 298 |
&gd->client_name, |
| 299 |
&mech, |
| 300 |
&gr->gr_token, |
| 301 |
&ret_flags, |
| 302 |
NULL, |
| 303 |
&gd->deleg); |
| 304 |
|
| 305 |
xdr_free((xdrproc_t)xdr_rpc_gss_init_args, (caddr_t)&recv_tok); |
| 306 |
|
| 307 |
if (gr->gr_major != GSS_S_COMPLETE && |
| 308 |
gr->gr_major != GSS_S_CONTINUE_NEEDED) { |
| 309 |
gss_log_status("svcauth_gss_accept_sec_context: accept_sec_context", |
| 310 |
gr->gr_major, gr->gr_minor); |
| 311 |
gd->ctx = GSS_C_NO_CONTEXT; |
| 312 |
gss_release_buffer(&min_stat, &gr->gr_token); |
| 313 |
return (FALSE); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
if ((gr->gr_ctx.value = mem_alloc(sizeof(gss_union_ctx_id_desc))) == NULL) { |
| 318 |
fprintf(stderr, "svcauth_gss_accept_context: out of memory\n"); |
| 319 |
return (FALSE); |
| 320 |
} |
| 321 |
memcpy(gr->gr_ctx.value, gd->ctx, sizeof(gss_union_ctx_id_desc)); |
| 322 |
gr->gr_ctx.length = sizeof(gss_union_ctx_id_desc); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
gr->gr_win = 0x00000005; |
| 328 |
|
| 329 |
|
| 330 |
gd->sec.mech = mech; |
| 331 |
gd->sec.qop = GSS_C_QOP_DEFAULT; |
| 332 |
gd->sec.svc = gc->gc_svc; |
| 333 |
gd->seq = gc->gc_seq; |
| 334 |
gd->win = gr->gr_win; |
| 335 |
gd->callback_done = FALSE; |
| 336 |
|
| 337 |
if (gr->gr_major == GSS_S_COMPLETE) { |
| 338 |
maj_stat = gss_display_name(&min_stat, gd->client_name, |
| 339 |
&gd->cname, &gd->sec.mech); |
| 340 |
if (maj_stat != GSS_S_COMPLETE) { |
| 341 |
gss_log_status("svcauth_gss_accept_sec_context: display_name", |
| 342 |
maj_stat, min_stat); |
| 343 |
return (FALSE); |
| 344 |
} |
| 345 |
if (!_rpc_gss_fill_in_creds(gd, gc)) |
| 346 |
return (FALSE); |
| 347 |
|
| 348 |
seq = htonl(gr->gr_win); |
| 349 |
seqbuf.value = &seq; |
| 350 |
seqbuf.length = sizeof(seq); |
| 351 |
|
| 352 |
maj_stat = gss_sign(&min_stat, gd->ctx, GSS_C_QOP_DEFAULT, |
| 353 |
&seqbuf, &checksum); |
| 354 |
|
| 355 |
if (maj_stat != GSS_S_COMPLETE) |
| 356 |
return (FALSE); |
| 357 |
|
| 358 |
rqst->rq_xprt->xp_verf.oa_flavor = RPCSEC_GSS; |
| 359 |
memcpy(rqst->rq_xprt->xp_verf.oa_base, checksum.value, |
| 360 |
checksum.length); |
| 361 |
rqst->rq_xprt->xp_verf.oa_length = checksum.length; |
| 362 |
|
| 363 |
gss_release_buffer(&min_stat, &checksum); |
| 364 |
} |
| 365 |
return (TRUE); |
| 366 |
} |
| 367 |
|
| 368 |
static bool_t |
| 369 |
svcauth_gss_validate(struct svc_rpc_gss_data *gd, struct rpc_msg *msg, |
| 370 |
gss_qop_t *qop) |
| 371 |
{ |
| 372 |
struct opaque_auth *oa; |
| 373 |
gss_buffer_desc rpcbuf, checksum; |
| 374 |
OM_uint32 maj_stat, min_stat, qop_state; |
| 375 |
u_char *rpchdr; |
| 376 |
int32_t *buf; |
| 377 |
|
| 378 |
gss_log_debug("in svcauth_gss_validate()"); |
| 379 |
|
| 380 |
|
| 381 |
oa = &msg->rm_call.cb_cred; |
| 382 |
if (oa->oa_length > MAX_AUTH_BYTES) |
| 383 |
return (FALSE); |
| 384 |
|
| 385 |
rpchdr = (u_char *)calloc(((8 * BYTES_PER_XDR_UNIT) + |
| 386 |
RNDUP(oa->oa_length)), 1); |
| 387 |
if (rpchdr == NULL) |
| 388 |
return (FALSE); |
| 389 |
|
| 390 |
buf = (int32_t *)rpchdr; |
| 391 |
IXDR_PUT_LONG(buf, msg->rm_xid); |
| 392 |
IXDR_PUT_ENUM(buf, msg->rm_direction); |
| 393 |
IXDR_PUT_LONG(buf, msg->rm_call.cb_rpcvers); |
| 394 |
IXDR_PUT_LONG(buf, msg->rm_call.cb_prog); |
| 395 |
IXDR_PUT_LONG(buf, msg->rm_call.cb_vers); |
| 396 |
IXDR_PUT_LONG(buf, msg->rm_call.cb_proc); |
| 397 |
IXDR_PUT_ENUM(buf, oa->oa_flavor); |
| 398 |
IXDR_PUT_LONG(buf, oa->oa_length); |
| 399 |
if (oa->oa_length) { |
| 400 |
memcpy((caddr_t)buf, oa->oa_base, oa->oa_length); |
| 401 |
buf += RNDUP(oa->oa_length) / sizeof(int32_t); |
| 402 |
} |
| 403 |
rpcbuf.value = rpchdr; |
| 404 |
rpcbuf.length = (u_char *)buf - rpchdr; |
| 405 |
|
| 406 |
checksum.value = msg->rm_call.cb_verf.oa_base; |
| 407 |
checksum.length = msg->rm_call.cb_verf.oa_length; |
| 408 |
|
| 409 |
maj_stat = gss_verify_mic(&min_stat, gd->ctx, &rpcbuf, &checksum, |
| 410 |
&qop_state); |
| 411 |
|
| 412 |
free(rpchdr); |
| 413 |
|
| 414 |
if (maj_stat != GSS_S_COMPLETE) { |
| 415 |
gss_log_status("svcauth_gss_validate: gss_verify_mic", |
| 416 |
maj_stat, min_stat); |
| 417 |
return (FALSE); |
| 418 |
} |
| 419 |
*qop = qop_state; |
| 420 |
return (TRUE); |
| 421 |
} |
| 422 |
|
| 423 |
static bool_t |
| 424 |
svcauth_gss_nextverf(struct svc_req *rqst, u_int num) |
| 425 |
{ |
| 426 |
struct svc_rpc_gss_data *gd; |
| 427 |
gss_buffer_desc signbuf, checksum; |
| 428 |
OM_uint32 maj_stat, min_stat; |
| 429 |
|
| 430 |
gss_log_debug("in svcauth_gss_nextverf()"); |
| 431 |
|
| 432 |
gd = SVCAUTH_PRIVATE(&SVC_XP_AUTH(rqst->rq_xprt)); |
| 433 |
|
| 434 |
signbuf.value = # |
| 435 |
signbuf.length = sizeof(num); |
| 436 |
|
| 437 |
maj_stat = gss_get_mic(&min_stat, gd->ctx, gd->sec.qop, |
| 438 |
&signbuf, &checksum); |
| 439 |
|
| 440 |
if (maj_stat != GSS_S_COMPLETE) { |
| 441 |
gss_log_status("svcauth_gss_nextverf: gss_get_mic", |
| 442 |
maj_stat, min_stat); |
| 443 |
return (FALSE); |
| 444 |
} |
| 445 |
|
| 446 |
rqst->rq_xprt->xp_verf.oa_flavor = RPCSEC_GSS; |
| 447 |
memcpy(rqst->rq_xprt->xp_verf.oa_base, checksum.value, checksum.length); |
| 448 |
rqst->rq_xprt->xp_verf.oa_length = (u_int)checksum.length; |
| 449 |
|
| 450 |
gss_release_buffer(&min_stat, &checksum); |
| 451 |
|
| 452 |
return (TRUE); |
| 453 |
} |
| 454 |
|
| 455 |
static bool_t |
| 456 |
svcauth_gss_do_call(struct svc_req *rqst, struct svc_rpc_gss_data *gd, |
| 457 |
struct svc_rpc_gss_callback *cb) |
| 458 |
{ |
| 459 |
rpc_gss_lock_t lock; |
| 460 |
bool_t result; |
| 461 |
|
| 462 |
lock.locked = FALSE; |
| 463 |
lock.raw_cred = &gd->rcred; |
| 464 |
result = cb->cb_args.callback(rqst, gd->deleg, gd->ctx, |
| 465 |
&lock, &gd->cookie); |
| 466 |
if (result) { |
| 467 |
gd->locked = lock.locked; |
| 468 |
gd->deleg = GSS_C_NO_CREDENTIAL; |
| 469 |
} |
| 470 |
return result; |
| 471 |
} |
| 472 |
|
| 473 |
static bool_t |
| 474 |
svcauth_gss_callback(struct svc_req *rqst, struct svc_rpc_gss_data *gd) |
| 475 |
{ |
| 476 |
struct svc_rpc_gss_callback *cb; |
| 477 |
bool_t result; |
| 478 |
|
| 479 |
result = TRUE; |
| 480 |
mutex_lock(&svcauth_cb_lock); |
| 481 |
|
| 482 |
for (cb = _svcauth_callbacks; cb != NULL; cb = cb->cb_next) { |
| 483 |
if (cb->cb_args.program == rqst->rq_prog && |
| 484 |
cb->cb_args.version == rqst->rq_vers) { |
| 485 |
result = svcauth_gss_do_call(rqst, gd, cb); |
| 486 |
break; |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
if (gd->deleg != GSS_C_NO_CREDENTIAL) { |
| 491 |
OM_uint32 min_stat; |
| 492 |
(void)gss_release_cred(&min_stat, &gd->deleg); |
| 493 |
} |
| 494 |
|
| 495 |
mutex_unlock(&svcauth_cb_lock); |
| 496 |
return result; |
| 497 |
} |
| 498 |
|
| 499 |
static void |
| 500 |
destroy_gd(struct svc_rpc_gss_data *gd) |
| 501 |
{ |
| 502 |
OM_uint32 min_stat; |
| 503 |
|
| 504 |
gss_delete_sec_context(&min_stat, &gd->ctx, GSS_C_NO_BUFFER); |
| 505 |
gss_release_buffer(&min_stat, &gd->cname); |
| 506 |
|
| 507 |
if (gd->client_name) |
| 508 |
gss_release_name(&min_stat, &gd->client_name); |
| 509 |
if (gd->rcred.client_principal != NULL) |
| 510 |
free(gd->rcred.client_principal); |
| 511 |
|
| 512 |
mem_free(gd, sizeof(*gd)); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
static struct svcauth_gss_cache_entry ** |
| 517 |
lookup_cache_entry(struct svc_req *rqst) |
| 518 |
{ |
| 519 |
struct svcauth_gss_cache_entry **ce; |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
for (ce = &svcauth_gss_cache; *ce; ce = &(*ce)->next) { |
| 527 |
if ((*ce)->xprt == rqst->rq_xprt) |
| 528 |
break; |
| 529 |
} |
| 530 |
|
| 531 |
return (ce); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
static void |
| 536 |
use_cache_entry(struct svcauth_gss_cache_entry **ce, time_t now) |
| 537 |
{ |
| 538 |
struct svcauth_gss_cache_entry *ce_new_head; |
| 539 |
|
| 540 |
(*ce)->time_to_die = now + 300; |
| 541 |
|
| 542 |
if (ce == &svcauth_gss_cache) |
| 543 |
return; |
| 544 |
|
| 545 |
ce_new_head = *ce; |
| 546 |
*ce = (*ce)->next; |
| 547 |
ce_new_head->next = svcauth_gss_cache; |
| 548 |
svcauth_gss_cache = ce_new_head; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
static void |
| 553 |
destroy_cold_cache_entries(time_t now) |
| 554 |
{ |
| 555 |
struct svcauth_gss_cache_entry **ce; |
| 556 |
|
| 557 |
for (ce = &svcauth_gss_cache; *ce; ce = &(*ce)->next) { |
| 558 |
if (now > (*ce)->time_to_die) |
| 559 |
break; |
| 560 |
} |
| 561 |
|
| 562 |
while (*ce) { |
| 563 |
struct svcauth_gss_cache_entry *ce_next = (*ce)->next; |
| 564 |
destroy_gd((*ce)->gd); |
| 565 |
mem_free(*ce, sizeof(**ce)); |
| 566 |
*ce = ce_next; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
enum auth_stat |
| 571 |
_svcauth_gss(struct svc_req *rqst, struct rpc_msg *msg, bool_t *no_dispatch) |
| 572 |
{ |
| 573 |
XDR xdrs; |
| 574 |
struct svc_rpc_gss_data *gd; |
| 575 |
struct rpc_gss_cred *gc; |
| 576 |
struct rpc_gss_init_res gr; |
| 577 |
int call_stat, offset; |
| 578 |
gss_qop_t qop; |
| 579 |
struct svcauth_gss_cache_entry **ce; |
| 580 |
time_t now; |
| 581 |
enum auth_stat result = AUTH_OK; |
| 582 |
OM_uint32 min_stat; |
| 583 |
|
| 584 |
gss_log_debug("in svcauth_gss()"); |
| 585 |
|
| 586 |
|
| 587 |
rqst->rq_xprt->xp_verf = _null_auth; |
| 588 |
|
| 589 |
now = time(NULL); |
| 590 |
mutex_lock(&svcauth_gss_cache_lock); |
| 591 |
ce = lookup_cache_entry(rqst); |
| 592 |
if (!*ce) { |
| 593 |
|
| 594 |
if ((*ce = calloc(sizeof(**ce), 1)) == NULL) { |
| 595 |
fprintf(stderr, "svcauth_gss: out_of_memory\n"); |
| 596 |
mutex_unlock(&svcauth_gss_cache_lock); |
| 597 |
return (AUTH_FAILED); |
| 598 |
} |
| 599 |
if ((gd = calloc(sizeof(*gd), 1)) == NULL) { |
| 600 |
free(*ce); |
| 601 |
fprintf(stderr, "svcauth_gss: out_of_memory\n"); |
| 602 |
mutex_unlock(&svcauth_gss_cache_lock); |
| 603 |
return (AUTH_FAILED); |
| 604 |
} |
| 605 |
gd->locked = FALSE; |
| 606 |
(*ce)->xprt = rqst->rq_xprt; |
| 607 |
(*ce)->gd = gd; |
| 608 |
(*ce)->next = NULL; |
| 609 |
} |
| 610 |
else |
| 611 |
gd = (*ce)->gd; |
| 612 |
use_cache_entry(ce, now); |
| 613 |
destroy_cold_cache_entries(now); |
| 614 |
mutex_unlock(&svcauth_gss_cache_lock); |
| 615 |
|
| 616 |
SVC_XP_AUTH(rqst->rq_xprt).svc_ah_ops = &svc_auth_gss_ops; |
| 617 |
SVC_XP_AUTH(rqst->rq_xprt).svc_ah_private = (caddr_t) gd; |
| 618 |
|
| 619 |
|
| 620 |
if (rqst->rq_cred.oa_length <= 0) |
| 621 |
return (AUTH_BADCRED); |
| 622 |
|
| 623 |
gc = (struct rpc_gss_cred *)rqst->rq_clntcred; |
| 624 |
memset(gc, 0, sizeof(*gc)); |
| 625 |
|
| 626 |
xdrmem_create(&xdrs, rqst->rq_cred.oa_base, |
| 627 |
rqst->rq_cred.oa_length, XDR_DECODE); |
| 628 |
|
| 629 |
if (!xdr_rpc_gss_cred(&xdrs, gc)) { |
| 630 |
XDR_DESTROY(&xdrs); |
| 631 |
return (AUTH_BADCRED); |
| 632 |
} |
| 633 |
XDR_DESTROY(&xdrs); |
| 634 |
|
| 635 |
|
| 636 |
if (gc->gc_v != RPCSEC_GSS_VERSION) { |
| 637 |
result = AUTH_BADCRED; |
| 638 |
goto out; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
if (gc->gc_svc != RPCSEC_GSS_SVC_NONE && |
| 643 |
gc->gc_svc != RPCSEC_GSS_SVC_INTEGRITY && |
| 644 |
gc->gc_svc != RPCSEC_GSS_SVC_PRIVACY) { |
| 645 |
result = AUTH_BADCRED; |
| 646 |
goto out; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
if (gd->established) { |
| 651 |
if (gc->gc_seq > MAXSEQ) { |
| 652 |
result = RPCSEC_GSS_CTXPROBLEM; |
| 653 |
goto out; |
| 654 |
} |
| 655 |
|
| 656 |
if ((offset = gd->seqlast - gc->gc_seq) < 0) { |
| 657 |
gd->seqlast = gc->gc_seq; |
| 658 |
offset = 0 - offset; |
| 659 |
gd->seqmask <<= offset; |
| 660 |
offset = 0; |
| 661 |
} |
| 662 |
else if (offset >= gd->win || (gd->seqmask & (1 << offset))) { |
| 663 |
*no_dispatch = 1; |
| 664 |
result = RPCSEC_GSS_CTXPROBLEM; |
| 665 |
goto out; |
| 666 |
} |
| 667 |
gd->seq = gc->gc_seq; |
| 668 |
gd->seqmask |= (1 << offset); |
| 669 |
} |
| 670 |
|
| 671 |
if (gd->established) { |
| 672 |
rqst->rq_clntname = (char *)gd->client_name; |
| 673 |
rqst->rq_svcname = (char *)gd->ctx; |
| 674 |
} |
| 675 |
|
| 676 |
rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base; |
| 677 |
|
| 678 |
|
| 679 |
switch (gc->gc_proc) { |
| 680 |
|
| 681 |
case RPCSEC_GSS_INIT: |
| 682 |
case RPCSEC_GSS_CONTINUE_INIT: |
| 683 |
if (rqst->rq_proc != NULLPROC) { |
| 684 |
result = AUTH_FAILED; |
| 685 |
break; |
| 686 |
} |
| 687 |
|
| 688 |
if (_svcauth_gss_name == GSS_C_NO_NAME) { |
| 689 |
if (!svcauth_gss_import_name("nfs")) { |
| 690 |
result = AUTH_FAILED; |
| 691 |
break; |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
if (!svcauth_gss_acquire_cred()) { |
| 696 |
result = AUTH_FAILED; |
| 697 |
break; |
| 698 |
} |
| 699 |
|
| 700 |
if (!svcauth_gss_accept_sec_context(rqst, &gr)) { |
| 701 |
result = AUTH_REJECTEDCRED; |
| 702 |
break; |
| 703 |
} |
| 704 |
|
| 705 |
if (!svcauth_gss_nextverf(rqst, htonl(gr.gr_win))) { |
| 706 |
result = AUTH_FAILED; |
| 707 |
break; |
| 708 |
} |
| 709 |
|
| 710 |
*no_dispatch = TRUE; |
| 711 |
|
| 712 |
call_stat = svc_sendreply(rqst->rq_xprt, |
| 713 |
(xdrproc_t)xdr_rpc_gss_init_res, (caddr_t)&gr); |
| 714 |
|
| 715 |
gss_release_buffer(&min_stat, &gr.gr_token); |
| 716 |
free(gr.gr_ctx.value); |
| 717 |
|
| 718 |
if (!call_stat) { |
| 719 |
result = AUTH_FAILED; |
| 720 |
break; |
| 721 |
} |
| 722 |
|
| 723 |
if (gr.gr_major == GSS_S_COMPLETE) |
| 724 |
gd->established = TRUE; |
| 725 |
|
| 726 |
break; |
| 727 |
|
| 728 |
case RPCSEC_GSS_DATA: |
| 729 |
if (!svcauth_gss_validate(gd, msg, &qop)) { |
| 730 |
result = RPCSEC_GSS_CREDPROBLEM; |
| 731 |
break; |
| 732 |
} |
| 733 |
|
| 734 |
if (!svcauth_gss_nextverf(rqst, htonl(gc->gc_seq))) { |
| 735 |
result = AUTH_FAILED; |
| 736 |
break; |
| 737 |
} |
| 738 |
|
| 739 |
if (!gd->callback_done) { |
| 740 |
gd->callback_done = TRUE; |
| 741 |
gd->sec.qop = qop; |
| 742 |
(void)rpc_gss_num_to_qop(gd->rcred.mechanism, |
| 743 |
gd->sec.qop, &gd->rcred.qop); |
| 744 |
if (!svcauth_gss_callback(rqst, gd)) { |
| 745 |
result = AUTH_REJECTEDCRED; |
| 746 |
break; |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
if (gd->locked) { |
| 751 |
if (gd->rcred.service != |
| 752 |
_rpc_gss_svc_to_service(gc->gc_svc)) { |
| 753 |
result = AUTH_FAILED; |
| 754 |
break; |
| 755 |
} |
| 756 |
if (gd->sec.qop != qop) { |
| 757 |
result = AUTH_BADVERF; |
| 758 |
break; |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
if (gd->sec.qop != qop) { |
| 763 |
gd->sec.qop = qop; |
| 764 |
(void)rpc_gss_num_to_qop(gd->rcred.mechanism, |
| 765 |
qop, &gd->rcred.qop); |
| 766 |
} |
| 767 |
|
| 768 |
gd->rcred.service = _rpc_gss_svc_to_service(gc->gc_svc); |
| 769 |
|
| 770 |
break; |
| 771 |
|
| 772 |
case RPCSEC_GSS_DESTROY: |
| 773 |
if (rqst->rq_proc != NULLPROC) { |
| 774 |
result = AUTH_FAILED; |
| 775 |
break; |
| 776 |
} |
| 777 |
|
| 778 |
if (!svcauth_gss_validate(gd, msg, &qop)) { |
| 779 |
result = RPCSEC_GSS_CREDPROBLEM; |
| 780 |
break; |
| 781 |
} |
| 782 |
|
| 783 |
if (!svcauth_gss_nextverf(rqst, htonl(gc->gc_seq))) { |
| 784 |
result = AUTH_FAILED; |
| 785 |
break; |
| 786 |
} |
| 787 |
|
| 788 |
if (!svcauth_gss_release_cred()) { |
| 789 |
result = AUTH_FAILED; |
| 790 |
break; |
| 791 |
} |
| 792 |
|
| 793 |
SVCAUTH_DESTROY(&SVC_XP_AUTH(rqst->rq_xprt)); |
| 794 |
SVC_XP_AUTH(rqst->rq_xprt).svc_ah_ops = svc_auth_none.svc_ah_ops; |
| 795 |
SVC_XP_AUTH(rqst->rq_xprt).svc_ah_private = NULL; |
| 796 |
|
| 797 |
break; |
| 798 |
|
| 799 |
default: |
| 800 |
result = AUTH_REJECTEDCRED; |
| 801 |
break; |
| 802 |
} |
| 803 |
out: |
| 804 |
xdr_free((xdrproc_t)xdr_rpc_gss_cred, (caddr_t)gc); |
| 805 |
return result; |
| 806 |
} |
| 807 |
|
| 808 |
static bool_t |
| 809 |
svcauth_gss_destroy(SVCAUTH *auth) |
| 810 |
{ |
| 811 |
struct svc_rpc_gss_data *gd; |
| 812 |
struct svcauth_gss_cache_entry **ce; |
| 813 |
|
| 814 |
gss_log_debug("in svcauth_gss_destroy()"); |
| 815 |
|
| 816 |
gd = SVCAUTH_PRIVATE(auth); |
| 817 |
|
| 818 |
mutex_lock(&svcauth_gss_cache_lock); |
| 819 |
for (ce = &svcauth_gss_cache; *ce; ce = &(*ce)->next) { |
| 820 |
if ((*ce)->gd == gd) { |
| 821 |
struct svcauth_gss_cache_entry *ce_destroy = *ce; |
| 822 |
*ce = (*ce)->next; |
| 823 |
mem_free(ce_destroy, sizeof(*ce_destroy)); |
| 824 |
break; |
| 825 |
} |
| 826 |
} |
| 827 |
mutex_unlock(&svcauth_gss_cache_lock); |
| 828 |
|
| 829 |
destroy_gd(gd); |
| 830 |
|
| 831 |
return (TRUE); |
| 832 |
} |
| 833 |
|
| 834 |
static bool_t |
| 835 |
svcauth_gss_wrap(SVCAUTH *auth, XDR *xdrs, xdrproc_t xdr_func, caddr_t xdr_ptr) |
| 836 |
{ |
| 837 |
struct svc_rpc_gss_data *gd; |
| 838 |
|
| 839 |
gss_log_debug("in svcauth_gss_wrap()"); |
| 840 |
|
| 841 |
gd = SVCAUTH_PRIVATE(auth); |
| 842 |
|
| 843 |
if (!gd->established || gd->sec.svc == RPCSEC_GSS_SVC_NONE) { |
| 844 |
return ((*xdr_func)(xdrs, xdr_ptr)); |
| 845 |
} |
| 846 |
return (xdr_rpc_gss_data(xdrs, xdr_func, xdr_ptr, |
| 847 |
gd->ctx, gd->sec.qop, |
| 848 |
gd->sec.svc, gd->seq)); |
| 849 |
} |
| 850 |
|
| 851 |
static bool_t |
| 852 |
svcauth_gss_unwrap(SVCAUTH *auth, XDR *xdrs, xdrproc_t xdr_func, caddr_t xdr_ptr) |
| 853 |
{ |
| 854 |
struct svc_rpc_gss_data *gd; |
| 855 |
|
| 856 |
gss_log_debug("in svcauth_gss_unwrap()"); |
| 857 |
|
| 858 |
gd = SVCAUTH_PRIVATE(auth); |
| 859 |
|
| 860 |
if (!gd->established || gd->sec.svc == RPCSEC_GSS_SVC_NONE) { |
| 861 |
return ((*xdr_func)(xdrs, xdr_ptr)); |
| 862 |
} |
| 863 |
return (xdr_rpc_gss_data(xdrs, xdr_func, xdr_ptr, |
| 864 |
gd->ctx, gd->sec.qop, |
| 865 |
gd->sec.svc, gd->seq)); |
| 866 |
} |
| 867 |
|
| 868 |
char * |
| 869 |
svcauth_gss_get_principal(SVCAUTH *auth) |
| 870 |
{ |
| 871 |
struct svc_rpc_gss_data *gd; |
| 872 |
char *pname; |
| 873 |
|
| 874 |
gd = SVCAUTH_PRIVATE(auth); |
| 875 |
|
| 876 |
if (gd->cname.length == 0) |
| 877 |
return (NULL); |
| 878 |
|
| 879 |
if ((pname = malloc(gd->cname.length + 1)) == NULL) |
| 880 |
return (NULL); |
| 881 |
|
| 882 |
memcpy(pname, gd->cname.value, gd->cname.length); |
| 883 |
pname[gd->cname.length] = '\0'; |
| 884 |
|
| 885 |
return (pname); |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
int |
| 899 |
rpc_gss_svc_max_data_length(struct svc_req *rqst, int maxlen) |
| 900 |
{ |
| 901 |
OM_uint32 max_input_size, maj_stat, min_stat; |
| 902 |
struct svc_rpc_gss_data *gd; |
| 903 |
int conf_req_flag; |
| 904 |
int result; |
| 905 |
|
| 906 |
if (!rqst) |
| 907 |
return 0; |
| 908 |
|
| 909 |
gd = SVCAUTH_PRIVATE(&SVC_XP_AUTH(rqst->rq_xprt)); |
| 910 |
|
| 911 |
switch (gd->rcred.service) { |
| 912 |
case rpcsec_gss_svc_none: |
| 913 |
return maxlen; |
| 914 |
case rpcsec_gss_svc_default: |
| 915 |
case rpcsec_gss_svc_integrity: |
| 916 |
conf_req_flag = 0; |
| 917 |
break; |
| 918 |
case rpcsec_gss_svc_privacy: |
| 919 |
conf_req_flag = 1; |
| 920 |
break; |
| 921 |
default: |
| 922 |
return 0; |
| 923 |
} |
| 924 |
|
| 925 |
result = 0; |
| 926 |
maj_stat = gss_wrap_size_limit(&min_stat, gd->ctx, conf_req_flag, |
| 927 |
gd->sec.qop, maxlen, &max_input_size); |
| 928 |
if (maj_stat == GSS_S_COMPLETE) |
| 929 |
if ((int)max_input_size > 0) |
| 930 |
result = (int)max_input_size; |
| 931 |
return result; |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
bool_t |
| 946 |
rpc_gss_set_svc_name(char *principal, char *mechanism, u_int req_time, |
| 947 |
u_int UNUSED(program), u_int UNUSED(version)) |
| 948 |
{ |
| 949 |
rpc_gss_OID oid; |
| 950 |
char *save; |
| 951 |
|
| 952 |
if (principal == NULL) |
| 953 |
return FALSE; |
| 954 |
save = strdup(principal); |
| 955 |
if (save == NULL) |
| 956 |
return FALSE; |
| 957 |
|
| 958 |
if (!rpc_gss_mech_to_oid(mechanism, &oid)) |
| 959 |
goto out_err; |
| 960 |
|
| 961 |
if (!svcauth_gss_import_name(principal)) |
| 962 |
goto out_err; |
| 963 |
|
| 964 |
_svcauth_req_time = req_time; |
| 965 |
_svcauth_oid_set.count = 1; |
| 966 |
_svcauth_oid_set.elements = (gss_OID)oid; |
| 967 |
free(_svcauth_svc_name); |
| 968 |
_svcauth_svc_name = save; |
| 969 |
return TRUE; |
| 970 |
|
| 971 |
out_err: |
| 972 |
free(save); |
| 973 |
return FALSE; |
| 974 |
} |
| 975 |
|
| 976 |
static void |
| 977 |
_rpc_gss_fill_in_ucreds(struct svc_rpc_gss_data *gd) |
| 978 |
{ |
| 979 |
rpc_gss_ucred_t *ucred = &gd->ucred; |
| 980 |
OM_uint32 maj_stat, min_stat; |
| 981 |
struct passwd pwd, *pw; |
| 982 |
long buflen; |
| 983 |
uid_t uid; |
| 984 |
char *buf; |
| 985 |
int len; |
| 986 |
|
| 987 |
|
| 988 |
ucred->uid = 65534; |
| 989 |
ucred->gid = 65534; |
| 990 |
ucred->gidlen = 0; |
| 991 |
ucred->gidlist = gd->gids; |
| 992 |
|
| 993 |
maj_stat = gss_pname_to_uid(&min_stat, gd->client_name, |
| 994 |
gd->sec.mech, &uid); |
| 995 |
if (maj_stat != GSS_S_COMPLETE) |
| 996 |
return; |
| 997 |
|
| 998 |
buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 999 |
if (buflen == -1) |
| 1000 |
return; |
| 1001 |
buf = malloc((size_t)buflen); |
| 1002 |
if (buf == NULL) |
| 1003 |
return; |
| 1004 |
|
| 1005 |
(void)getpwuid_r(uid, &pwd, buf, buflen, &pw); |
| 1006 |
if (pw == NULL) { |
| 1007 |
free(buf); |
| 1008 |
return; |
| 1009 |
} |
| 1010 |
|
| 1011 |
ucred->uid = pw->pw_uid; |
| 1012 |
ucred->gid = pw->pw_gid; |
| 1013 |
len = NGRPS; |
| 1014 |
(void)getgrouplist(pw->pw_name, pw->pw_gid, ucred->gidlist, &len); |
| 1015 |
ucred->gidlen = len; |
| 1016 |
|
| 1017 |
free(buf); |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
bool_t |
| 1033 |
rpc_gss_getcred(struct svc_req *rqst, rpc_gss_rawcred_t **rcred, |
| 1034 |
rpc_gss_ucred_t **ucred, void **cookie) |
| 1035 |
{ |
| 1036 |
struct svc_rpc_gss_data *gd; |
| 1037 |
|
| 1038 |
if (rqst == NULL) |
| 1039 |
return FALSE; |
| 1040 |
|
| 1041 |
if (rqst->rq_xprt->xp_verf.oa_flavor != RPCSEC_GSS) |
| 1042 |
return FALSE; |
| 1043 |
|
| 1044 |
gd = SVCAUTH_PRIVATE(&SVC_XP_AUTH(rqst->rq_xprt)); |
| 1045 |
|
| 1046 |
if (rcred != NULL) { |
| 1047 |
gd->scratch = gd->rcred; |
| 1048 |
gd->scratch.service = _rpc_gss_svc_to_service(gd->sec.svc); |
| 1049 |
(void)rpc_gss_num_to_qop(gd->scratch.mechanism, gd->sec.qop, |
| 1050 |
&gd->scratch.qop); |
| 1051 |
*rcred = &gd->scratch; |
| 1052 |
} |
| 1053 |
|
| 1054 |
if (ucred != NULL) { |
| 1055 |
_rpc_gss_fill_in_ucreds(gd); |
| 1056 |
*ucred = &gd->ucred; |
| 1057 |
} |
| 1058 |
|
| 1059 |
if (cookie != NULL) |
| 1060 |
*cookie = gd->cookie; |
| 1061 |
|
| 1062 |
return TRUE; |
| 1063 |
} |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
bool_t |
| 1076 |
rpc_gss_set_callback(rpc_gss_callback_t *callback) |
| 1077 |
{ |
| 1078 |
struct svc_rpc_gss_callback *new; |
| 1079 |
|
| 1080 |
if (callback == NULL) |
| 1081 |
return FALSE; |
| 1082 |
|
| 1083 |
|
| 1084 |
new = malloc(sizeof(*new)); |
| 1085 |
if (new == NULL) |
| 1086 |
return FALSE; |
| 1087 |
|
| 1088 |
new->cb_args = *callback; |
| 1089 |
|
| 1090 |
mutex_lock(&svcauth_cb_lock); |
| 1091 |
new->cb_next = _svcauth_callbacks; |
| 1092 |
_svcauth_callbacks = new; |
| 1093 |
mutex_unlock(&svcauth_cb_lock); |
| 1094 |
|
| 1095 |
return TRUE; |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
bool_t |
| 1111 |
rpc_gss_get_principal_name(rpc_gss_principal_t *principal, char *mechanism, |
| 1112 |
char *user_name, char *node, char *secdomain) |
| 1113 |
{ |
| 1114 |
OM_uint32 maj_stat, min_stat; |
| 1115 |
rpc_gss_principal_t result; |
| 1116 |
size_t nodelen, secdomlen; |
| 1117 |
gss_name_t name, mechname; |
| 1118 |
gss_buffer_desc namebuf; |
| 1119 |
rpc_gss_OID oid; |
| 1120 |
|
| 1121 |
|
| 1122 |
if (principal == NULL || user_name == NULL || strlen(user_name) == 0) |
| 1123 |
return FALSE; |
| 1124 |
if (!rpc_gss_mech_to_oid(mechanism, &oid)) |
| 1125 |
return FALSE; |
| 1126 |
|
| 1127 |
nodelen = 0; |
| 1128 |
if (node != NULL) |
| 1129 |
nodelen = strlen(node) + 1; |
| 1130 |
secdomlen = 0; |
| 1131 |
if (secdomain != NULL) |
| 1132 |
secdomlen = strlen(secdomain) + 1; |
| 1133 |
namebuf.length = strlen(user_name) + nodelen + secdomlen; |
| 1134 |
namebuf.value = calloc(1, namebuf.length); |
| 1135 |
if (namebuf.value == NULL) |
| 1136 |
return FALSE; |
| 1137 |
(void)strcpy(namebuf.value, user_name); |
| 1138 |
if (nodelen > 0) { |
| 1139 |
(void)strcat(namebuf.value, "/"); |
| 1140 |
(void)strcat(namebuf.value, node); |
| 1141 |
} |
| 1142 |
if (secdomlen > 0) { |
| 1143 |
(void)strcat(namebuf.value, "@"); |
| 1144 |
(void)strcat(namebuf.value, secdomain); |
| 1145 |
} |
| 1146 |
|
| 1147 |
maj_stat = gss_import_name(&min_stat, &namebuf, |
| 1148 |
GSS_C_NT_USER_NAME, &name); |
| 1149 |
free(namebuf.value); |
| 1150 |
if (maj_stat != GSS_S_COMPLETE) { |
| 1151 |
gss_log_status("gss_import_name", maj_stat, min_stat); |
| 1152 |
return FALSE; |
| 1153 |
} |
| 1154 |
|
| 1155 |
maj_stat = gss_canonicalize_name(&min_stat, name, oid, &mechname); |
| 1156 |
(void)gss_release_name(&min_stat, &name); |
| 1157 |
if (maj_stat != GSS_S_COMPLETE) { |
| 1158 |
gss_log_status("gss_canonicalize_name", maj_stat, min_stat); |
| 1159 |
return FALSE; |
| 1160 |
} |
| 1161 |
|
| 1162 |
maj_stat = gss_export_name(&min_stat, mechname, &namebuf); |
| 1163 |
(void)gss_release_name(&min_stat, &mechname); |
| 1164 |
if (maj_stat != GSS_S_COMPLETE) { |
| 1165 |
gss_log_status("gss_export_name", maj_stat, min_stat); |
| 1166 |
return FALSE; |
| 1167 |
} |
| 1168 |
|
| 1169 |
result = calloc(1, sizeof(*result) + namebuf.length); |
| 1170 |
if (result == NULL) { |
| 1171 |
(void)gss_release_buffer(&min_stat, &namebuf); |
| 1172 |
return FALSE; |
| 1173 |
} |
| 1174 |
result->len = namebuf.length; |
| 1175 |
(void)memcpy(result->name, namebuf.value, namebuf.length); |
| 1176 |
(void)gss_release_buffer(&min_stat, &namebuf); |
| 1177 |
|
| 1178 |
*principal = result; |
| 1179 |
return TRUE; |
| 1180 |
} |