| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#include <pthread.h> |
| 48 |
#include <reentrant.h> |
| 49 |
#include <string.h> |
| 50 |
#include <stdlib.h> |
| 51 |
#include <stdio.h> |
| 52 |
#include <unistd.h> |
| 53 |
#include <rpc/des_crypt.h> |
| 54 |
#include <sys/param.h> |
| 55 |
#include <netinet/in.h> |
| 56 |
#include <rpc/types.h> |
| 57 |
#include <rpc/xdr.h> |
| 58 |
#include <rpc/auth.h> |
| 59 |
#include <rpc/auth_des.h> |
| 60 |
#include <rpc/svc.h> |
| 61 |
#include <rpc/rpc_msg.h> |
| 62 |
#include <rpc/svc_auth.h> |
| 63 |
#if defined(__FreeBSD__) || defined(__NetBSD__) |
| 64 |
#include <libc_private.h> |
| 65 |
#endif |
| 66 |
|
| 67 |
#include "debug.h" |
| 68 |
|
| 69 |
extern int key_decryptsession_pk(const char *, netobj *, des_block *); |
| 70 |
|
| 71 |
#define USEC_PER_SEC ((u_long) 1000000L) |
| 72 |
#define BEFORE(t1, t2) timercmp(t1, t2, <) |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#define AUTHDES_CACHESZ 64 |
| 78 |
struct cache_entry { |
| 79 |
des_block key; |
| 80 |
char *rname; |
| 81 |
u_int window; |
| 82 |
struct timeval laststamp; |
| 83 |
char *localcred; |
| 84 |
}; |
| 85 |
static struct cache_entry *authdes_cache; |
| 86 |
static short *authdes_lru; |
| 87 |
|
| 88 |
static void cache_init(); |
| 89 |
static short cache_spot(des_block *key, char *name, struct timeval *timestamp); |
| 90 |
static void cache_ref(short sid); |
| 91 |
|
| 92 |
static void invalidate(char *cred); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
static struct { |
| 98 |
u_long ncachehits; |
| 99 |
u_long ncachereplays; |
| 100 |
u_long ncachemisses; |
| 101 |
} svcauthdes_stats; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
enum auth_stat |
| 107 |
_svcauth_des(rqst, msg) |
| 108 |
struct svc_req *rqst; |
| 109 |
struct rpc_msg *msg; |
| 110 |
{ |
| 111 |
|
| 112 |
long *ixdr; |
| 113 |
des_block cryptbuf[2]; |
| 114 |
struct authdes_cred *cred; |
| 115 |
struct authdes_verf verf; |
| 116 |
int status; |
| 117 |
struct cache_entry *entry; |
| 118 |
short sid = 0; |
| 119 |
des_block *sessionkey; |
| 120 |
des_block ivec; |
| 121 |
u_int window; |
| 122 |
struct timeval timestamp; |
| 123 |
u_long namelen; |
| 124 |
struct area { |
| 125 |
struct authdes_cred area_cred; |
| 126 |
char area_netname[MAXNETNAMELEN+1]; |
| 127 |
} *area; |
| 128 |
|
| 129 |
if (authdes_cache == NULL) { |
| 130 |
cache_init(); |
| 131 |
} |
| 132 |
|
| 133 |
area = (struct area *)rqst->rq_clntcred; |
| 134 |
cred = (struct authdes_cred *)&area->area_cred; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
ixdr = (long *)msg->rm_call.cb_cred.oa_base; |
| 140 |
cred->adc_namekind = IXDR_GET_ENUM(ixdr, enum authdes_namekind); |
| 141 |
switch (cred->adc_namekind) { |
| 142 |
case ADN_FULLNAME: |
| 143 |
namelen = IXDR_GET_U_LONG(ixdr); |
| 144 |
if (namelen > MAXNETNAMELEN) { |
| 145 |
return (AUTH_BADCRED); |
| 146 |
} |
| 147 |
cred->adc_fullname.name = area->area_netname; |
| 148 |
memcpy(cred->adc_fullname.name, (char *)ixdr, |
| 149 |
(u_int)namelen); |
| 150 |
cred->adc_fullname.name[namelen] = 0; |
| 151 |
ixdr += (RNDUP(namelen) / BYTES_PER_XDR_UNIT); |
| 152 |
cred->adc_fullname.key.key.high = (u_long)*ixdr++; |
| 153 |
cred->adc_fullname.key.key.low = (u_long)*ixdr++; |
| 154 |
cred->adc_fullname.window = (u_long)*ixdr++; |
| 155 |
break; |
| 156 |
case ADN_NICKNAME: |
| 157 |
cred->adc_nickname = (u_long)*ixdr++; |
| 158 |
break; |
| 159 |
default: |
| 160 |
return (AUTH_BADCRED); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
ixdr = (long *)msg->rm_call.cb_verf.oa_base; |
| 167 |
verf.adv_xtimestamp.key.high = (u_long)*ixdr++; |
| 168 |
verf.adv_xtimestamp.key.low = (u_long)*ixdr++; |
| 169 |
verf.adv_int_u = (u_long)*ixdr++; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if (cred->adc_namekind == ADN_FULLNAME) { |
| 176 |
netobj pkey; |
| 177 |
char pkey_data[1024]; |
| 178 |
|
| 179 |
sessionkey = &cred->adc_fullname.key; |
| 180 |
if (! getpublickey(cred->adc_fullname.name, pkey_data)) { |
| 181 |
LIBTIRPC_DEBUG(1, ("_svcauth_des: getpublickey failed")); |
| 182 |
return(AUTH_BADCRED); |
| 183 |
} |
| 184 |
pkey.n_bytes = pkey_data; |
| 185 |
pkey.n_len = strlen(pkey_data) + 1; |
| 186 |
if (key_decryptsession_pk(cred->adc_fullname.name, &pkey, |
| 187 |
sessionkey) < 0) { |
| 188 |
LIBTIRPC_DEBUG(1, ("_svcauth_des: key_decryptsessionkey failed")); |
| 189 |
return (AUTH_BADCRED); |
| 190 |
} |
| 191 |
} else { |
| 192 |
sid = (short)cred->adc_nickname; |
| 193 |
if (sid < 0 || sid >= AUTHDES_CACHESZ) { |
| 194 |
LIBTIRPC_DEBUG(1, ("_svcauth_des: bad nickname")); |
| 195 |
return (AUTH_BADCRED); |
| 196 |
} |
| 197 |
sessionkey = &authdes_cache[sid].key; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
cryptbuf[0] = verf.adv_xtimestamp; |
| 205 |
if (cred->adc_namekind == ADN_FULLNAME) { |
| 206 |
cryptbuf[1].key.high = cred->adc_fullname.window; |
| 207 |
cryptbuf[1].key.low = verf.adv_winverf; |
| 208 |
ivec.key.high = ivec.key.low = 0; |
| 209 |
status = cbc_crypt((char *)sessionkey, (char *)cryptbuf, |
| 210 |
2*sizeof(des_block), DES_DECRYPT | DES_HW, |
| 211 |
(char *)&ivec); |
| 212 |
} else { |
| 213 |
status = ecb_crypt((char *)sessionkey, (char *)cryptbuf, |
| 214 |
sizeof(des_block), DES_DECRYPT | DES_HW); |
| 215 |
} |
| 216 |
if (DES_FAILED(status)) { |
| 217 |
LIBTIRPC_DEBUG(1, ("_svcauth_des: decryption failure")); |
| 218 |
return (AUTH_FAILED); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
ixdr = (long *)cryptbuf; |
| 225 |
timestamp.tv_sec = IXDR_GET_LONG(ixdr); |
| 226 |
timestamp.tv_usec = IXDR_GET_LONG(ixdr); |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
{ |
| 235 |
struct timeval current; |
| 236 |
int nick; |
| 237 |
int winverf; |
| 238 |
|
| 239 |
if (cred->adc_namekind == ADN_FULLNAME) { |
| 240 |
window = IXDR_GET_U_LONG(ixdr); |
| 241 |
winverf = IXDR_GET_U_LONG(ixdr); |
| 242 |
if (winverf != window - 1) { |
| 243 |
LIBTIRPC_DEBUG(1, ("_svcauth_des: window verifier mismatch")); |
| 244 |
return (AUTH_BADCRED); |
| 245 |
} |
| 246 |
sid = cache_spot(sessionkey, cred->adc_fullname.name, |
| 247 |
×tamp); |
| 248 |
if (sid < 0) { |
| 249 |
LIBTIRPC_DEBUG(1, ("_svcauth_des: replayed credential")); |
| 250 |
return (AUTH_REJECTEDCRED); |
| 251 |
} |
| 252 |
nick = 0; |
| 253 |
} else { |
| 254 |
window = authdes_cache[sid].window; |
| 255 |
nick = 1; |
| 256 |
} |
| 257 |
|
| 258 |
if ((u_long)timestamp.tv_usec >= USEC_PER_SEC) { |
| 259 |
LIBTIRPC_DEBUG(1, ("_svcauth_des: invalid usecs")); |
| 260 |
|
| 261 |
return (nick ? AUTH_REJECTEDVERF : AUTH_BADVERF); |
| 262 |
} |
| 263 |
if (nick && BEFORE(×tamp, |
| 264 |
&authdes_cache[sid].laststamp)) { |
| 265 |
LIBTIRPC_DEBUG(1, ("_svcauth_des: timestamp before last seen")); |
| 266 |
return (AUTH_REJECTEDVERF); |
| 267 |
} |
| 268 |
(void) gettimeofday(¤t, (struct timezone *)NULL); |
| 269 |
current.tv_sec -= window; |
| 270 |
if (!BEFORE(¤t, ×tamp)) { |
| 271 |
LIBTIRPC_DEBUG(1, ("_svcauth_des: timestamp expired")); |
| 272 |
|
| 273 |
return (nick ? AUTH_REJECTEDVERF : AUTH_BADCRED); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
verf.adv_nickname = (u_long)sid; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
ixdr = (long *)cryptbuf; |
| 286 |
IXDR_PUT_LONG(ixdr, timestamp.tv_sec - 1); |
| 287 |
IXDR_PUT_LONG(ixdr, timestamp.tv_usec); |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
status = ecb_crypt((char *)sessionkey, (char *)cryptbuf, |
| 293 |
sizeof(des_block), DES_ENCRYPT | DES_HW); |
| 294 |
if (DES_FAILED(status)) { |
| 295 |
LIBTIRPC_DEBUG(1, ("_svcauth_des: encryption failure")); |
| 296 |
return (AUTH_FAILED); |
| 297 |
} |
| 298 |
verf.adv_xtimestamp = cryptbuf[0]; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
ixdr = (long *)msg->rm_call.cb_verf.oa_base; |
| 304 |
*ixdr++ = (long)verf.adv_xtimestamp.key.high; |
| 305 |
*ixdr++ = (long)verf.adv_xtimestamp.key.low; |
| 306 |
*ixdr++ = (long)verf.adv_int_u; |
| 307 |
|
| 308 |
rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES; |
| 309 |
rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base; |
| 310 |
rqst->rq_xprt->xp_verf.oa_length = |
| 311 |
(char *)ixdr - msg->rm_call.cb_verf.oa_base; |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
entry = &authdes_cache[sid]; |
| 318 |
entry->laststamp = timestamp; |
| 319 |
cache_ref(sid); |
| 320 |
if (cred->adc_namekind == ADN_FULLNAME) { |
| 321 |
cred->adc_fullname.window = window; |
| 322 |
cred->adc_nickname = (u_long)sid; |
| 323 |
if (entry->rname != NULL) { |
| 324 |
mem_free(entry->rname, strlen(entry->rname) + 1); |
| 325 |
} |
| 326 |
entry->rname = (char *)mem_alloc((u_int)strlen(cred->adc_fullname.name) |
| 327 |
+ 1); |
| 328 |
if (entry->rname != NULL) { |
| 329 |
(void) strcpy(entry->rname, cred->adc_fullname.name); |
| 330 |
} else { |
| 331 |
LIBTIRPC_DEBUG(1, ("_svcauth_des: out of memory")); |
| 332 |
} |
| 333 |
entry->key = *sessionkey; |
| 334 |
entry->window = window; |
| 335 |
invalidate(entry->localcred); |
| 336 |
} else { |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
cred->adc_namekind = ADN_FULLNAME; |
| 341 |
cred->adc_fullname.name = entry->rname; |
| 342 |
cred->adc_fullname.key = entry->key; |
| 343 |
cred->adc_fullname.window = entry->window; |
| 344 |
} |
| 345 |
return (AUTH_OK); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
static void |
| 353 |
cache_init() |
| 354 |
{ |
| 355 |
int i; |
| 356 |
|
| 357 |
authdes_cache = (struct cache_entry *) |
| 358 |
mem_alloc(sizeof(struct cache_entry) * AUTHDES_CACHESZ); |
| 359 |
memset(authdes_cache, 0, |
| 360 |
sizeof(struct cache_entry) * AUTHDES_CACHESZ); |
| 361 |
|
| 362 |
authdes_lru = (short *)mem_alloc(sizeof(short) * AUTHDES_CACHESZ); |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
for (i = 0; i < AUTHDES_CACHESZ; i++) { |
| 367 |
authdes_lru[i] = i; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
static short |
| 376 |
cache_victim() |
| 377 |
{ |
| 378 |
return (authdes_lru[AUTHDES_CACHESZ-1]); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
static void |
| 385 |
cache_ref(sid) |
| 386 |
short sid; |
| 387 |
{ |
| 388 |
int i; |
| 389 |
short curr; |
| 390 |
short prev; |
| 391 |
|
| 392 |
prev = authdes_lru[0]; |
| 393 |
authdes_lru[0] = sid; |
| 394 |
for (i = 1; prev != sid; i++) { |
| 395 |
curr = authdes_lru[i]; |
| 396 |
authdes_lru[i] = prev; |
| 397 |
prev = curr; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
static short |
| 408 |
cache_spot(key, name, timestamp) |
| 409 |
des_block *key; |
| 410 |
char *name; |
| 411 |
struct timeval *timestamp; |
| 412 |
{ |
| 413 |
struct cache_entry *cp; |
| 414 |
int i; |
| 415 |
u_long hi; |
| 416 |
|
| 417 |
hi = key->key.high; |
| 418 |
for (cp = authdes_cache, i = 0; i < AUTHDES_CACHESZ; i++, cp++) { |
| 419 |
if (cp->key.key.high == hi && |
| 420 |
cp->key.key.low == key->key.low && |
| 421 |
cp->rname != NULL && |
| 422 |
memcmp(cp->rname, name, strlen(name) + 1) == 0) { |
| 423 |
if (BEFORE(timestamp, &cp->laststamp)) { |
| 424 |
svcauthdes_stats.ncachereplays++; |
| 425 |
return (-1); |
| 426 |
} |
| 427 |
svcauthdes_stats.ncachehits++; |
| 428 |
return (i); |
| 429 |
} |
| 430 |
} |
| 431 |
svcauthdes_stats.ncachemisses++; |
| 432 |
return (cache_victim()); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
#define UNKNOWN -2 |
| 442 |
#define INVALID -1 |
| 443 |
|
| 444 |
struct bsdcred { |
| 445 |
short uid; |
| 446 |
short gid; |
| 447 |
short grouplen; |
| 448 |
short groups[NGROUPS]; |
| 449 |
}; |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
int |
| 458 |
authdes_getucred(adc, uid, gid, grouplen, groups) |
| 459 |
struct authdes_cred *adc; |
| 460 |
uid_t *uid; |
| 461 |
gid_t *gid; |
| 462 |
int *grouplen; |
| 463 |
gid_t *groups; |
| 464 |
{ |
| 465 |
unsigned sid; |
| 466 |
int i; |
| 467 |
uid_t i_uid; |
| 468 |
gid_t i_gid; |
| 469 |
int i_grouplen; |
| 470 |
struct bsdcred *cred; |
| 471 |
|
| 472 |
sid = adc->adc_nickname; |
| 473 |
if (sid >= AUTHDES_CACHESZ) { |
| 474 |
LIBTIRPC_DEBUG(1, ("authdes_getucred: invalid nickname")); |
| 475 |
return (0); |
| 476 |
} |
| 477 |
cred = (struct bsdcred *)authdes_cache[sid].localcred; |
| 478 |
if (cred == NULL) { |
| 479 |
cred = (struct bsdcred *)mem_alloc(sizeof(struct bsdcred)); |
| 480 |
authdes_cache[sid].localcred = (char *)cred; |
| 481 |
cred->grouplen = INVALID; |
| 482 |
} |
| 483 |
if (cred->grouplen == INVALID) { |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
if (!netname2user(adc->adc_fullname.name, &i_uid, &i_gid, |
| 488 |
&i_grouplen, groups)) |
| 489 |
{ |
| 490 |
LIBTIRPC_DEBUG(1, ("authdes_getucred: unknown netname")); |
| 491 |
cred->grouplen = UNKNOWN; |
| 492 |
return (0); |
| 493 |
} |
| 494 |
LIBTIRPC_DEBUG(1, ("authdes_getucred: missed ucred cache")); |
| 495 |
*uid = cred->uid = i_uid; |
| 496 |
*gid = cred->gid = i_gid; |
| 497 |
*grouplen = cred->grouplen = i_grouplen; |
| 498 |
for (i = i_grouplen - 1; i >= 0; i--) { |
| 499 |
cred->groups[i] = groups[i]; |
| 500 |
} |
| 501 |
return (1); |
| 502 |
} else if (cred->grouplen == UNKNOWN) { |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
return (0); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
*uid = cred->uid; |
| 513 |
*gid = cred->gid; |
| 514 |
*grouplen = cred->grouplen; |
| 515 |
for (i = cred->grouplen - 1; i >= 0; i--) { |
| 516 |
groups[i] = cred->groups[i]; |
| 517 |
} |
| 518 |
return (1); |
| 519 |
} |
| 520 |
|
| 521 |
static void |
| 522 |
invalidate(cred) |
| 523 |
char *cred; |
| 524 |
{ |
| 525 |
if (cred == NULL) { |
| 526 |
return; |
| 527 |
} |
| 528 |
((struct bsdcred *)cred)->grouplen = INVALID; |
| 529 |
} |