| 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 |
#include <pthread.h> |
| 35 |
#include <reentrant.h> |
| 36 |
#include <err.h> |
| 37 |
#include <errno.h> |
| 38 |
#include <string.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <unistd.h> |
| 41 |
#include <rpc/des_crypt.h> |
| 42 |
#include <syslog.h> |
| 43 |
#include <rpc/types.h> |
| 44 |
#include <rpc/auth.h> |
| 45 |
#include <rpc/auth_des.h> |
| 46 |
#include <rpc/clnt.h> |
| 47 |
#include <rpc/xdr.h> |
| 48 |
#include <sys/socket.h> |
| 49 |
|
| 50 |
#include "nis.h" |
| 51 |
|
| 52 |
#if defined(LIBC_SCCS) && !defined(lint) |
| 53 |
#endif |
| 54 |
|
| 55 |
#include "debug.h" |
| 56 |
|
| 57 |
#define USEC_PER_SEC 1000000 |
| 58 |
#define RTIME_TIMEOUT 5 |
| 59 |
|
| 60 |
#define AUTH_PRIVATE(auth) (struct ad_private *) auth->ah_private |
| 61 |
#define ALLOC(object_type) (object_type *) mem_alloc(sizeof(object_type)) |
| 62 |
#define FREE(ptr, size) mem_free((char *)(ptr), (int) size) |
| 63 |
#define ATTEMPT(xdr_op) if (!(xdr_op)) return (FALSE) |
| 64 |
|
| 65 |
extern bool_t xdr_authdes_cred( XDR *, struct authdes_cred *); |
| 66 |
extern bool_t xdr_authdes_verf( XDR *, struct authdes_verf *); |
| 67 |
extern int key_encryptsession_pk( char *, netobj *, des_block *); |
| 68 |
|
| 69 |
extern bool_t __rpc_get_time_offset(struct timeval *, nis_server *, char *, |
| 70 |
char **, char **); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
static void authdes_nextverf(AUTH *); |
| 76 |
static bool_t authdes_marshal(AUTH *, XDR *); |
| 77 |
static bool_t authdes_validate(AUTH *, struct opaque_auth *); |
| 78 |
static bool_t authdes_refresh(AUTH *, void *); |
| 79 |
static void authdes_destroy(AUTH *); |
| 80 |
|
| 81 |
static struct auth_ops *authdes_ops(void); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
struct ad_private { |
| 87 |
char *ad_fullname; |
| 88 |
u_int ad_fullnamelen; |
| 89 |
char *ad_servername; |
| 90 |
u_int ad_servernamelen; |
| 91 |
u_int ad_window; |
| 92 |
bool_t ad_dosync; |
| 93 |
struct netbuf ad_syncaddr; |
| 94 |
char *ad_timehost; |
| 95 |
struct timeval ad_timediff; |
| 96 |
u_int ad_nickname; |
| 97 |
struct authdes_cred ad_cred; |
| 98 |
struct authdes_verf ad_verf; |
| 99 |
struct timeval ad_timestamp; |
| 100 |
des_block ad_xkey; |
| 101 |
u_char ad_pkey[1024]; |
| 102 |
char *ad_netid; |
| 103 |
char *ad_uaddr; |
| 104 |
nis_server *ad_nis_srvr; |
| 105 |
}; |
| 106 |
|
| 107 |
AUTH *authdes_pk_seccreate(const char *, netobj *, u_int, const char *, |
| 108 |
const des_block *, nis_server *); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
AUTH * |
| 121 |
authdes_seccreate(const char *servername, const u_int win, |
| 122 |
const char *timehost, const des_block *ckey) |
| 123 |
{ |
| 124 |
u_char pkey_data[1024]; |
| 125 |
netobj pkey; |
| 126 |
AUTH *dummy; |
| 127 |
|
| 128 |
if (! getpublickey(servername, (char *) pkey_data)) { |
| 129 |
syslog(LOG_ERR, |
| 130 |
"authdes_seccreate: no public key found for %s", |
| 131 |
servername); |
| 132 |
return (NULL); |
| 133 |
} |
| 134 |
|
| 135 |
pkey.n_bytes = (char *) pkey_data; |
| 136 |
pkey.n_len = (u_int)strlen((char *)pkey_data) + 1; |
| 137 |
dummy = authdes_pk_seccreate(servername, &pkey, win, timehost, |
| 138 |
ckey, NULL); |
| 139 |
return (dummy); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
AUTH * |
| 148 |
authdes_pk_seccreate(const char *servername, netobj *pkey, u_int window, |
| 149 |
const char *timehost, const des_block *ckey, nis_server *srvr) |
| 150 |
{ |
| 151 |
AUTH *auth; |
| 152 |
struct ad_private *ad; |
| 153 |
char namebuf[MAXNETNAMELEN+1]; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
auth = ALLOC(AUTH); |
| 159 |
if (auth == NULL) { |
| 160 |
syslog(LOG_ERR, "authdes_pk_seccreate: out of memory"); |
| 161 |
return (NULL); |
| 162 |
} |
| 163 |
ad = ALLOC(struct ad_private); |
| 164 |
if (ad == NULL) { |
| 165 |
syslog(LOG_ERR, "authdes_pk_seccreate: out of memory"); |
| 166 |
goto failed; |
| 167 |
} |
| 168 |
ad->ad_fullname = ad->ad_servername = NULL; |
| 169 |
ad->ad_timehost = NULL; |
| 170 |
ad->ad_netid = NULL; |
| 171 |
ad->ad_uaddr = NULL; |
| 172 |
ad->ad_nis_srvr = NULL; |
| 173 |
ad->ad_timediff.tv_sec = 0; |
| 174 |
ad->ad_timediff.tv_usec = 0; |
| 175 |
memcpy(ad->ad_pkey, pkey->n_bytes, pkey->n_len); |
| 176 |
if (!getnetname(namebuf)) |
| 177 |
goto failed; |
| 178 |
ad->ad_fullnamelen = RNDUP((u_int) strlen(namebuf)); |
| 179 |
ad->ad_fullname = (char *)mem_alloc(ad->ad_fullnamelen + 1); |
| 180 |
ad->ad_servernamelen = strlen(servername); |
| 181 |
ad->ad_servername = (char *)mem_alloc(ad->ad_servernamelen + 1); |
| 182 |
|
| 183 |
if (ad->ad_fullname == NULL || ad->ad_servername == NULL) { |
| 184 |
syslog(LOG_ERR, "authdes_seccreate: out of memory"); |
| 185 |
goto failed; |
| 186 |
} |
| 187 |
if (timehost != NULL) { |
| 188 |
ad->ad_timehost = (char *)mem_alloc(strlen(timehost) + 1); |
| 189 |
if (ad->ad_timehost == NULL) { |
| 190 |
syslog(LOG_ERR, "authdes_seccreate: out of memory"); |
| 191 |
goto failed; |
| 192 |
} |
| 193 |
memcpy(ad->ad_timehost, timehost, strlen(timehost) + 1); |
| 194 |
ad->ad_dosync = TRUE; |
| 195 |
} else if (srvr != NULL) { |
| 196 |
ad->ad_nis_srvr = srvr; |
| 197 |
ad->ad_dosync = TRUE; |
| 198 |
} else { |
| 199 |
ad->ad_dosync = FALSE; |
| 200 |
} |
| 201 |
memcpy(ad->ad_fullname, namebuf, ad->ad_fullnamelen + 1); |
| 202 |
memcpy(ad->ad_servername, servername, ad->ad_servernamelen + 1); |
| 203 |
ad->ad_window = window; |
| 204 |
if (ckey == NULL) { |
| 205 |
if (key_gendes(&auth->ah_key) < 0) { |
| 206 |
syslog(LOG_ERR, |
| 207 |
"authdes_seccreate: keyserv(1m) is unable to generate session key"); |
| 208 |
goto failed; |
| 209 |
} |
| 210 |
} else { |
| 211 |
auth->ah_key = *ckey; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
auth->ah_cred.oa_flavor = AUTH_DES; |
| 218 |
auth->ah_verf.oa_flavor = AUTH_DES; |
| 219 |
auth->ah_ops = authdes_ops(); |
| 220 |
auth->ah_private = (caddr_t)ad; |
| 221 |
|
| 222 |
if (!authdes_refresh(auth, NULL)) { |
| 223 |
goto failed; |
| 224 |
} |
| 225 |
ad->ad_nis_srvr = NULL; |
| 226 |
return (auth); |
| 227 |
|
| 228 |
failed: |
| 229 |
if (auth) |
| 230 |
FREE(auth, sizeof (AUTH)); |
| 231 |
if (ad) { |
| 232 |
if (ad->ad_fullname) |
| 233 |
FREE(ad->ad_fullname, ad->ad_fullnamelen + 1); |
| 234 |
if (ad->ad_servername) |
| 235 |
FREE(ad->ad_servername, ad->ad_servernamelen + 1); |
| 236 |
if (ad->ad_timehost) |
| 237 |
FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1); |
| 238 |
if (ad->ad_netid) |
| 239 |
FREE(ad->ad_netid, strlen(ad->ad_netid) + 1); |
| 240 |
if (ad->ad_uaddr) |
| 241 |
FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1); |
| 242 |
FREE(ad, sizeof (struct ad_private)); |
| 243 |
} |
| 244 |
return (NULL); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
static void |
| 257 |
authdes_nextverf(AUTH *auth) |
| 258 |
{ |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
static bool_t |
| 267 |
authdes_marshal(AUTH *auth, XDR *xdrs) |
| 268 |
{ |
| 269 |
|
| 270 |
struct ad_private *ad = AUTH_PRIVATE(auth); |
| 271 |
struct authdes_cred *cred = &ad->ad_cred; |
| 272 |
struct authdes_verf *verf = &ad->ad_verf; |
| 273 |
des_block cryptbuf[2]; |
| 274 |
des_block ivec; |
| 275 |
int status; |
| 276 |
int len; |
| 277 |
rpc_inline_t *ixdr; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
(void) gettimeofday(&ad->ad_timestamp, (struct timezone *)NULL); |
| 284 |
ad->ad_timestamp.tv_sec += ad->ad_timediff.tv_sec; |
| 285 |
ad->ad_timestamp.tv_usec += ad->ad_timediff.tv_usec; |
| 286 |
while (ad->ad_timestamp.tv_usec >= USEC_PER_SEC) { |
| 287 |
ad->ad_timestamp.tv_usec -= USEC_PER_SEC; |
| 288 |
ad->ad_timestamp.tv_sec++; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
ixdr = (rpc_inline_t *)cryptbuf; |
| 296 |
IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_sec); |
| 297 |
IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_usec); |
| 298 |
if (ad->ad_cred.adc_namekind == ADN_FULLNAME) { |
| 299 |
IXDR_PUT_U_INT32(ixdr, ad->ad_window); |
| 300 |
IXDR_PUT_U_INT32(ixdr, ad->ad_window - 1); |
| 301 |
ivec.key.high = ivec.key.low = 0; |
| 302 |
status = cbc_crypt((char *)&auth->ah_key, (char *)cryptbuf, |
| 303 |
(u_int) 2 * sizeof (des_block), |
| 304 |
DES_ENCRYPT | DES_HW, (char *)&ivec); |
| 305 |
} else { |
| 306 |
status = ecb_crypt((char *)&auth->ah_key, (char *)cryptbuf, |
| 307 |
(u_int) sizeof (des_block), |
| 308 |
DES_ENCRYPT | DES_HW); |
| 309 |
} |
| 310 |
if (DES_FAILED(status)) { |
| 311 |
syslog(LOG_ERR, "authdes_marshal: DES encryption failure"); |
| 312 |
return (FALSE); |
| 313 |
} |
| 314 |
ad->ad_verf.adv_xtimestamp = cryptbuf[0]; |
| 315 |
if (ad->ad_cred.adc_namekind == ADN_FULLNAME) { |
| 316 |
ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high; |
| 317 |
ad->ad_verf.adv_winverf = cryptbuf[1].key.low; |
| 318 |
} else { |
| 319 |
ad->ad_cred.adc_nickname = ad->ad_nickname; |
| 320 |
ad->ad_verf.adv_winverf = 0; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
if (ad->ad_cred.adc_namekind == ADN_FULLNAME) { |
| 328 |
len = ((1 + 1 + 2 + 1)*BYTES_PER_XDR_UNIT + ad->ad_fullnamelen); |
| 329 |
} else { |
| 330 |
len = (1 + 1)*BYTES_PER_XDR_UNIT; |
| 331 |
} |
| 332 |
|
| 333 |
if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) { |
| 334 |
IXDR_PUT_INT32(ixdr, AUTH_DES); |
| 335 |
IXDR_PUT_INT32(ixdr, len); |
| 336 |
} else { |
| 337 |
ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_cred.oa_flavor)); |
| 338 |
ATTEMPT(xdr_putint32(xdrs, &len)); |
| 339 |
} |
| 340 |
ATTEMPT(xdr_authdes_cred(xdrs, cred)); |
| 341 |
|
| 342 |
len = (2 + 1)*BYTES_PER_XDR_UNIT; |
| 343 |
if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) { |
| 344 |
IXDR_PUT_INT32(ixdr, AUTH_DES); |
| 345 |
IXDR_PUT_INT32(ixdr, len); |
| 346 |
} else { |
| 347 |
ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_verf.oa_flavor)); |
| 348 |
ATTEMPT(xdr_putint32(xdrs, &len)); |
| 349 |
} |
| 350 |
ATTEMPT(xdr_authdes_verf(xdrs, verf)); |
| 351 |
return (TRUE); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
static bool_t |
| 359 |
authdes_validate(AUTH *auth, struct opaque_auth *rverf) |
| 360 |
{ |
| 361 |
|
| 362 |
struct ad_private *ad = AUTH_PRIVATE(auth); |
| 363 |
struct authdes_verf verf; |
| 364 |
int status; |
| 365 |
uint32_t *ixdr; |
| 366 |
des_block buf; |
| 367 |
|
| 368 |
if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT) { |
| 369 |
return (FALSE); |
| 370 |
} |
| 371 |
|
| 372 |
ixdr = (uint32_t *)rverf->oa_base; |
| 373 |
buf.key.high = (uint32_t)*ixdr++; |
| 374 |
buf.key.low = (uint32_t)*ixdr++; |
| 375 |
verf.adv_int_u = (uint32_t)*ixdr++; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
status = ecb_crypt((char *)&auth->ah_key, (char *)&buf, |
| 381 |
(u_int)sizeof (des_block), DES_DECRYPT | DES_HW); |
| 382 |
|
| 383 |
if (DES_FAILED(status)) { |
| 384 |
syslog(LOG_ERR, "authdes_validate: DES decryption failure"); |
| 385 |
return (FALSE); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
ixdr = (uint32_t *)buf.c; |
| 393 |
verf.adv_timestamp.tv_sec = IXDR_GET_INT32(ixdr) + 1; |
| 394 |
verf.adv_timestamp.tv_usec = IXDR_GET_INT32(ixdr); |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
if (memcmp((char *)&ad->ad_timestamp, (char *)&verf.adv_timestamp, |
| 400 |
sizeof(struct timeval)) != 0) { |
| 401 |
LIBTIRPC_DEBUG(1, ("authdes_validate: verifier mismatch")); |
| 402 |
return (FALSE); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
ad->ad_nickname = verf.adv_nickname; |
| 409 |
ad->ad_cred.adc_namekind = ADN_NICKNAME; |
| 410 |
return (TRUE); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
static bool_t |
| 418 |
authdes_refresh(AUTH *auth, void *dummy) |
| 419 |
{ |
| 420 |
|
| 421 |
struct ad_private *ad = AUTH_PRIVATE(auth); |
| 422 |
struct authdes_cred *cred = &ad->ad_cred; |
| 423 |
int ok; |
| 424 |
netobj pkey; |
| 425 |
|
| 426 |
if (ad->ad_dosync) { |
| 427 |
ok = __rpc_get_time_offset(&ad->ad_timediff, ad->ad_nis_srvr, |
| 428 |
ad->ad_timehost, &(ad->ad_uaddr), |
| 429 |
&(ad->ad_netid)); |
| 430 |
if (! ok) { |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
ad->ad_dosync = 0; |
| 435 |
LIBTIRPC_DEBUG(1, ("authdes_refresh: unable to synchronize clock")); |
| 436 |
} |
| 437 |
} |
| 438 |
ad->ad_xkey = auth->ah_key; |
| 439 |
pkey.n_bytes = (char *)(ad->ad_pkey); |
| 440 |
pkey.n_len = (u_int)strlen((char *)ad->ad_pkey) + 1; |
| 441 |
if (key_encryptsession_pk(ad->ad_servername, &pkey, &ad->ad_xkey) < 0) { |
| 442 |
LIBTIRPC_DEBUG(1, |
| 443 |
("authdes_refresh: keyserv(1m) is unable to encrypt session key")); |
| 444 |
return (FALSE); |
| 445 |
} |
| 446 |
cred->adc_fullname.key = ad->ad_xkey; |
| 447 |
cred->adc_namekind = ADN_FULLNAME; |
| 448 |
cred->adc_fullname.name = ad->ad_fullname; |
| 449 |
return (TRUE); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
static void |
| 457 |
authdes_destroy(AUTH *auth) |
| 458 |
{ |
| 459 |
|
| 460 |
struct ad_private *ad = AUTH_PRIVATE(auth); |
| 461 |
|
| 462 |
FREE(ad->ad_fullname, ad->ad_fullnamelen + 1); |
| 463 |
FREE(ad->ad_servername, ad->ad_servernamelen + 1); |
| 464 |
if (ad->ad_timehost) |
| 465 |
FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1); |
| 466 |
if (ad->ad_netid) |
| 467 |
FREE(ad->ad_netid, strlen(ad->ad_netid) + 1); |
| 468 |
if (ad->ad_uaddr) |
| 469 |
FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1); |
| 470 |
FREE(ad, sizeof (struct ad_private)); |
| 471 |
FREE(auth, sizeof(AUTH)); |
| 472 |
} |
| 473 |
|
| 474 |
static bool_t |
| 475 |
authdes_wrap(AUTH *auth, XDR *xdrs, xdrproc_t xfunc, caddr_t xwhere) |
| 476 |
{ |
| 477 |
return ((*xfunc)(xdrs, xwhere)); |
| 478 |
} |
| 479 |
|
| 480 |
static struct auth_ops * |
| 481 |
authdes_ops(void) |
| 482 |
{ |
| 483 |
static struct auth_ops ops; |
| 484 |
extern mutex_t authdes_ops_lock; |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
mutex_lock(&authdes_ops_lock); |
| 489 |
if (ops.ah_nextverf == NULL) { |
| 490 |
ops.ah_nextverf = authdes_nextverf; |
| 491 |
ops.ah_marshal = authdes_marshal; |
| 492 |
ops.ah_validate = authdes_validate; |
| 493 |
ops.ah_refresh = authdes_refresh; |
| 494 |
ops.ah_destroy = authdes_destroy; |
| 495 |
ops.ah_wrap = authdes_wrap; |
| 496 |
ops.ah_unwrap = authdes_wrap; |
| 497 |
} |
| 498 |
mutex_unlock(&authdes_ops_lock); |
| 499 |
return (&ops); |
| 500 |
} |