| 1 | /* Copyright (C) 1997-2021 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <string.h> |
| 22 | #include <libintl.h> |
| 23 | #include <rpc/rpc.h> |
| 24 | #include <rpc/auth.h> |
| 25 | #include <rpcsvc/nis.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <unistd.h> |
| 29 | #include <netinet/in.h> |
| 30 | #include <arpa/inet.h> |
| 31 | #include <libc-lock.h> |
| 32 | |
| 33 | #include "nis_xdr.h" |
| 34 | #include "nis_intern.h" |
| 35 | #include <libnsl.h> |
| 36 | #include <shlib-compat.h> |
| 37 | |
| 38 | static const struct timeval RPCTIMEOUT = {10, 0}; |
| 39 | static const struct timeval UDPTIMEOUT = {5, 0}; |
| 40 | |
| 41 | extern u_short __pmap_getnisport (struct sockaddr_in *address, u_long program, |
| 42 | u_long version, u_int protocol); |
| 43 | |
| 44 | unsigned long int |
| 45 | inetstr2int (const char *str) |
| 46 | { |
| 47 | size_t j = 0; |
| 48 | for (size_t i = 0; str[i] != '\0'; ++i) |
| 49 | if (str[i] == '.' && __builtin_expect (++j == 4, 0)) |
| 50 | { |
| 51 | char buffer[i + 1]; |
| 52 | buffer[i] = '\0'; |
| 53 | return inet_addr (memcpy (buffer, str, i)); |
| 54 | } |
| 55 | |
| 56 | return inet_addr (str); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | __nisbind_destroy (dir_binding *bind) |
| 61 | { |
| 62 | if (bind->clnt != NULL) |
| 63 | { |
| 64 | if (bind->use_auth) |
| 65 | auth_destroy (bind->clnt->cl_auth); |
| 66 | clnt_destroy (bind->clnt); |
| 67 | } |
| 68 | } |
| 69 | libnsl_hidden_nolink_def (__nisbind_destroy, GLIBC_2_1) |
| 70 | |
| 71 | nis_error |
| 72 | __nisbind_next (dir_binding *bind) |
| 73 | { |
| 74 | if (bind->clnt != NULL) |
| 75 | { |
| 76 | if (bind->use_auth) |
| 77 | auth_destroy (bind->clnt->cl_auth); |
| 78 | clnt_destroy (bind->clnt); |
| 79 | bind->clnt = NULL; |
| 80 | } |
| 81 | |
| 82 | if (bind->trys >= bind->server_len) |
| 83 | return NIS_FAIL; |
| 84 | |
| 85 | for (u_int j = bind->current_ep + 1; |
| 86 | j < bind->server_val[bind->server_used].ep.ep_len; ++j) |
| 87 | if (strcmp (bind->server_val[bind->server_used].ep.ep_val[j].family, |
| 88 | "inet" ) == 0) |
| 89 | if (bind->server_val[bind->server_used].ep.ep_val[j].proto[0] == '-') |
| 90 | { |
| 91 | bind->current_ep = j; |
| 92 | return NIS_SUCCESS; |
| 93 | } |
| 94 | |
| 95 | ++bind->trys; |
| 96 | ++bind->server_used; |
| 97 | if (bind->server_used >= bind->server_len) |
| 98 | bind->server_used = 0; |
| 99 | |
| 100 | for (u_int j = 0; j < bind->server_val[bind->server_used].ep.ep_len; ++j) |
| 101 | if (strcmp (bind->server_val[bind->server_used].ep.ep_val[j].family, |
| 102 | "inet" ) == 0) |
| 103 | if (bind->server_val[bind->server_used].ep.ep_val[j].proto[0] == '-') |
| 104 | { |
| 105 | bind->current_ep = j; |
| 106 | return NIS_SUCCESS; |
| 107 | } |
| 108 | |
| 109 | return NIS_FAIL; |
| 110 | } |
| 111 | libnsl_hidden_nolink_def (__nisbind_next, GLIBC_2_1) |
| 112 | |
| 113 | static struct ckey_cache_entry |
| 114 | { |
| 115 | struct in_addr inaddr; |
| 116 | in_port_t port; |
| 117 | unsigned int protocol; |
| 118 | des_block ckey; |
| 119 | } *ckey_cache; |
| 120 | static size_t ckey_cache_size; |
| 121 | static size_t ckey_cache_allocated; |
| 122 | static pid_t ckey_cache_pid; |
| 123 | static uid_t ckey_cache_euid; |
| 124 | __libc_lock_define_initialized (static, ckey_cache_lock) |
| 125 | |
| 126 | static bool_t |
| 127 | get_ckey (des_block *ckey, struct sockaddr_in *addr, unsigned int protocol) |
| 128 | { |
| 129 | size_t i; |
| 130 | pid_t pid = getpid (); |
| 131 | uid_t euid = geteuid (); |
| 132 | bool_t ret = FALSE; |
| 133 | |
| 134 | __libc_lock_lock (ckey_cache_lock); |
| 135 | |
| 136 | if (ckey_cache_pid != pid || ckey_cache_euid != euid) |
| 137 | { |
| 138 | ckey_cache_size = 0; |
| 139 | ckey_cache_pid = pid; |
| 140 | ckey_cache_euid = euid; |
| 141 | } |
| 142 | |
| 143 | for (i = 0; i < ckey_cache_size; ++i) |
| 144 | if (ckey_cache[i].port == addr->sin_port |
| 145 | && ckey_cache[i].protocol == protocol |
| 146 | && memcmp (&ckey_cache[i].inaddr, &addr->sin_addr, |
| 147 | sizeof (addr->sin_addr)) == 0) |
| 148 | { |
| 149 | *ckey = ckey_cache[i].ckey; |
| 150 | ret = TRUE; |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | if (!ret && key_gendes (ckey) >= 0) |
| 155 | { |
| 156 | ret = TRUE; |
| 157 | /* Don't grow the cache indefinitely. */ |
| 158 | if (ckey_cache_size == 256) |
| 159 | ckey_cache_size = 0; |
| 160 | if (ckey_cache_size == ckey_cache_allocated) |
| 161 | { |
| 162 | size_t size = ckey_cache_allocated ? ckey_cache_allocated * 2 : 16; |
| 163 | struct ckey_cache_entry *new_cache |
| 164 | = realloc (ckey_cache, size * sizeof (*ckey_cache)); |
| 165 | if (new_cache != NULL) |
| 166 | { |
| 167 | ckey_cache = new_cache; |
| 168 | ckey_cache_allocated = size; |
| 169 | } |
| 170 | } |
| 171 | ckey_cache[ckey_cache_size].inaddr = addr->sin_addr; |
| 172 | ckey_cache[ckey_cache_size].port = addr->sin_port; |
| 173 | ckey_cache[ckey_cache_size].protocol = protocol; |
| 174 | ckey_cache[ckey_cache_size++].ckey = *ckey; |
| 175 | } |
| 176 | |
| 177 | __libc_lock_unlock (ckey_cache_lock); |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | nis_error |
| 182 | __nisbind_connect (dir_binding *dbp) |
| 183 | { |
| 184 | nis_server *serv; |
| 185 | u_short port; |
| 186 | |
| 187 | if (dbp == NULL) |
| 188 | return NIS_FAIL; |
| 189 | |
| 190 | serv = &dbp->server_val[dbp->server_used]; |
| 191 | |
| 192 | memset (&dbp->addr, '\0', sizeof (dbp->addr)); |
| 193 | dbp->addr.sin_family = AF_INET; |
| 194 | |
| 195 | dbp->addr.sin_addr.s_addr = |
| 196 | inetstr2int (serv->ep.ep_val[dbp->current_ep].uaddr); |
| 197 | |
| 198 | if (dbp->addr.sin_addr.s_addr == INADDR_NONE) |
| 199 | return NIS_FAIL; |
| 200 | |
| 201 | /* Check, if the host is online and rpc.nisd is running. Much faster |
| 202 | then the clnt*_create functions: */ |
| 203 | port = __pmap_getnisport (&dbp->addr, NIS_PROG, NIS_VERSION, |
| 204 | dbp->use_udp ? IPPROTO_UDP : IPPROTO_TCP); |
| 205 | if (port == 0) |
| 206 | return NIS_RPCERROR; |
| 207 | |
| 208 | dbp->addr.sin_port = htons (port); |
| 209 | dbp->socket = RPC_ANYSOCK; |
| 210 | if (dbp->use_udp) |
| 211 | dbp->clnt = clntudp_create (&dbp->addr, NIS_PROG, NIS_VERSION, |
| 212 | UDPTIMEOUT, &dbp->socket); |
| 213 | else |
| 214 | dbp->clnt = clnttcp_create (&dbp->addr, NIS_PROG, NIS_VERSION, |
| 215 | &dbp->socket, 0, 0); |
| 216 | |
| 217 | if (dbp->clnt == NULL) |
| 218 | return NIS_RPCERROR; |
| 219 | |
| 220 | clnt_control (dbp->clnt, CLSET_TIMEOUT, (caddr_t) &RPCTIMEOUT); |
| 221 | /* If the program exists, close the socket */ |
| 222 | if (fcntl (dbp->socket, F_SETFD, 1) == -1) |
| 223 | perror ("fcntl: F_SETFD" ); |
| 224 | |
| 225 | if (dbp->use_auth) |
| 226 | { |
| 227 | if (serv->key_type == NIS_PK_DH) |
| 228 | { |
| 229 | char netname[MAXNETNAMELEN + 1]; |
| 230 | char *p; |
| 231 | des_block ckey; |
| 232 | |
| 233 | p = stpcpy (netname, "unix@" ); |
| 234 | strncpy (p, serv->name, MAXNETNAMELEN - 5); |
| 235 | netname[MAXNETNAMELEN] = '\0'; |
| 236 | dbp->clnt->cl_auth = NULL; |
| 237 | if (get_ckey (&ckey, &dbp->addr, |
| 238 | dbp->use_udp ? IPPROTO_UDP : IPPROTO_TCP)) |
| 239 | dbp->clnt->cl_auth = |
| 240 | authdes_pk_create (netname, &serv->pkey, 300, NULL, &ckey); |
| 241 | if (!dbp->clnt->cl_auth) |
| 242 | dbp->clnt->cl_auth = authunix_create_default (); |
| 243 | } |
| 244 | else |
| 245 | dbp->clnt->cl_auth = authunix_create_default (); |
| 246 | } |
| 247 | |
| 248 | return NIS_SUCCESS; |
| 249 | } |
| 250 | libnsl_hidden_nolink_def (__nisbind_connect, GLIBC_2_1) |
| 251 | |
| 252 | nis_error |
| 253 | __nisbind_create (dir_binding *dbp, const nis_server *serv_val, |
| 254 | unsigned int serv_len, unsigned int server_used, |
| 255 | unsigned int current_ep, unsigned int flags) |
| 256 | { |
| 257 | dbp->clnt = NULL; |
| 258 | |
| 259 | dbp->server_len = serv_len; |
| 260 | dbp->server_val = (nis_server *)serv_val; |
| 261 | |
| 262 | if (flags & USE_DGRAM) |
| 263 | dbp->use_udp = TRUE; |
| 264 | else |
| 265 | dbp->use_udp = FALSE; |
| 266 | |
| 267 | if (flags & NO_AUTHINFO) |
| 268 | dbp->use_auth = FALSE; |
| 269 | else |
| 270 | dbp->use_auth = TRUE; |
| 271 | |
| 272 | if (flags & MASTER_ONLY) |
| 273 | dbp->master_only = TRUE; |
| 274 | else |
| 275 | dbp->master_only = FALSE; |
| 276 | |
| 277 | /* We try the first server */ |
| 278 | dbp->trys = 1; |
| 279 | |
| 280 | dbp->class = -1; |
| 281 | if (server_used == ~0) |
| 282 | { |
| 283 | if (__nis_findfastest (dbp) < 1) |
| 284 | return NIS_NAMEUNREACHABLE; |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | dbp->server_used = server_used; |
| 289 | dbp->current_ep = current_ep; |
| 290 | } |
| 291 | |
| 292 | return NIS_SUCCESS; |
| 293 | } |
| 294 | libnsl_hidden_nolink_def (__nisbind_create, GLIBC_2_1) |
| 295 | |
| 296 | /* __nisbind_connect (dbp) must be run before calling this function ! |
| 297 | So we could use the same binding twice */ |
| 298 | nis_error |
| 299 | __do_niscall3 (dir_binding *dbp, u_long prog, xdrproc_t xargs, caddr_t req, |
| 300 | xdrproc_t xres, caddr_t resp, unsigned int flags, nis_cb *cb) |
| 301 | { |
| 302 | enum clnt_stat result; |
| 303 | nis_error retcode; |
| 304 | |
| 305 | if (dbp == NULL) |
| 306 | return NIS_NAMEUNREACHABLE; |
| 307 | |
| 308 | do |
| 309 | { |
| 310 | again: |
| 311 | result = clnt_call (dbp->clnt, prog, xargs, req, xres, resp, RPCTIMEOUT); |
| 312 | |
| 313 | if (result != RPC_SUCCESS) |
| 314 | retcode = NIS_RPCERROR; |
| 315 | else |
| 316 | { |
| 317 | switch (prog) |
| 318 | { |
| 319 | case NIS_IBLIST: |
| 320 | if ((((nis_result *)resp)->status == NIS_CBRESULTS) |
| 321 | && (cb != NULL)) |
| 322 | { |
| 323 | __nis_do_callback (dbp, &((nis_result *) resp)->cookie, cb); |
| 324 | break; |
| 325 | } |
| 326 | /* Yes, the missing break is correct. If we doesn't have to |
| 327 | start a callback, look if we have to search another server */ |
| 328 | /* Fall through. */ |
| 329 | case NIS_LOOKUP: |
| 330 | case NIS_ADD: |
| 331 | case NIS_MODIFY: |
| 332 | case NIS_REMOVE: |
| 333 | case NIS_IBADD: |
| 334 | case NIS_IBMODIFY: |
| 335 | case NIS_IBREMOVE: |
| 336 | case NIS_IBFIRST: |
| 337 | case NIS_IBNEXT: |
| 338 | if (((nis_result *)resp)->status == NIS_SYSTEMERROR |
| 339 | || ((nis_result *)resp)->status == NIS_NOSUCHNAME |
| 340 | || ((nis_result *)resp)->status == NIS_NOT_ME) |
| 341 | { |
| 342 | next_server: |
| 343 | if (__nisbind_next (dbp) == NIS_SUCCESS) |
| 344 | { |
| 345 | while (__nisbind_connect (dbp) != NIS_SUCCESS) |
| 346 | { |
| 347 | if (__nisbind_next (dbp) != NIS_SUCCESS) |
| 348 | return NIS_SUCCESS; |
| 349 | } |
| 350 | } |
| 351 | else |
| 352 | break; /* No more servers to search in */ |
| 353 | goto again; |
| 354 | } |
| 355 | break; |
| 356 | case NIS_FINDDIRECTORY: |
| 357 | if (((fd_result *)resp)->status == NIS_SYSTEMERROR |
| 358 | || ((fd_result *)resp)->status == NIS_NOSUCHNAME |
| 359 | || ((fd_result *)resp)->status == NIS_NOT_ME) |
| 360 | goto next_server; |
| 361 | break; |
| 362 | case NIS_DUMPLOG: /* log_result */ |
| 363 | case NIS_DUMP: |
| 364 | if (((log_result *)resp)->lr_status == NIS_SYSTEMERROR |
| 365 | || ((log_result *)resp)->lr_status == NIS_NOSUCHNAME |
| 366 | || ((log_result *)resp)->lr_status == NIS_NOT_ME) |
| 367 | goto next_server; |
| 368 | break; |
| 369 | default: |
| 370 | break; |
| 371 | } |
| 372 | retcode = NIS_SUCCESS; |
| 373 | } |
| 374 | } |
| 375 | while ((flags & HARD_LOOKUP) && retcode == NIS_RPCERROR); |
| 376 | |
| 377 | return retcode; |
| 378 | } |
| 379 | libnsl_hidden_nolink_def (__do_niscall3, GLIBC_PRIVATE) |
| 380 | |
| 381 | |
| 382 | nis_error |
| 383 | __do_niscall2 (const nis_server *server, u_int server_len, u_long prog, |
| 384 | xdrproc_t xargs, caddr_t req, xdrproc_t xres, caddr_t resp, |
| 385 | unsigned int flags, nis_cb *cb) |
| 386 | { |
| 387 | dir_binding dbp; |
| 388 | nis_error status; |
| 389 | |
| 390 | if (flags & MASTER_ONLY) |
| 391 | server_len = 1; |
| 392 | |
| 393 | status = __nisbind_create (&dbp, server, server_len, ~0, ~0, flags); |
| 394 | if (status != NIS_SUCCESS) |
| 395 | return status; |
| 396 | |
| 397 | while (__nisbind_connect (&dbp) != NIS_SUCCESS) |
| 398 | if (__nisbind_next (&dbp) != NIS_SUCCESS) |
| 399 | return NIS_NAMEUNREACHABLE; |
| 400 | |
| 401 | status = __do_niscall3 (&dbp, prog, xargs, req, xres, resp, flags, cb); |
| 402 | |
| 403 | __nisbind_destroy (&dbp); |
| 404 | |
| 405 | return status; |
| 406 | |
| 407 | } |
| 408 | |
| 409 | static directory_obj * |
| 410 | rec_dirsearch (const_nis_name name, directory_obj *dir, nis_error *status) |
| 411 | { |
| 412 | fd_result *fd_res; |
| 413 | XDR xdrs; |
| 414 | |
| 415 | switch (nis_dir_cmp (name, dir->do_name)) |
| 416 | { |
| 417 | case SAME_NAME: |
| 418 | *status = NIS_SUCCESS; |
| 419 | return dir; |
| 420 | case NOT_SEQUENTIAL: |
| 421 | /* NOT_SEQUENTIAL means, go one up and try it there ! */ |
| 422 | case HIGHER_NAME: |
| 423 | { /* We need data from a parent domain */ |
| 424 | directory_obj *obj; |
| 425 | const char *ndomain = __nis_domain_of (dir->do_name); |
| 426 | |
| 427 | /* The root server of our domain is a replica of the parent |
| 428 | domain ! (Now I understand why a root server must be a |
| 429 | replica of the parent domain) */ |
| 430 | fd_res = __nis_finddirectory (dir, ndomain); |
| 431 | if (fd_res == NULL) |
| 432 | { |
| 433 | nis_free_directory (dir); |
| 434 | *status = NIS_NOMEMORY; |
| 435 | return NULL; |
| 436 | } |
| 437 | *status = fd_res->status; |
| 438 | if (fd_res->status != NIS_SUCCESS) |
| 439 | { |
| 440 | /* Try the current directory obj, maybe it works */ |
| 441 | __free_fdresult (fd_res); |
| 442 | return dir; |
| 443 | } |
| 444 | nis_free_directory (dir); |
| 445 | obj = calloc (1, sizeof (directory_obj)); |
| 446 | if (obj == NULL) |
| 447 | { |
| 448 | __free_fdresult (fd_res); |
| 449 | *status = NIS_NOMEMORY; |
| 450 | return NULL; |
| 451 | } |
| 452 | xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val, |
| 453 | fd_res->dir_data.dir_data_len, XDR_DECODE); |
| 454 | _xdr_directory_obj (&xdrs, obj); |
| 455 | xdr_destroy (&xdrs); |
| 456 | __free_fdresult (fd_res); |
| 457 | |
| 458 | /* We have found a NIS+ server serving ndomain, now |
| 459 | let us search for "name" */ |
| 460 | return rec_dirsearch (name, obj, status); |
| 461 | } |
| 462 | break; |
| 463 | case LOWER_NAME: |
| 464 | { |
| 465 | directory_obj *obj; |
| 466 | size_t namelen = strlen (name); |
| 467 | char leaf[namelen + 3]; |
| 468 | char domain[namelen + 3]; |
| 469 | const char *ndomain; |
| 470 | char *cp; |
| 471 | |
| 472 | strcpy (domain, name); |
| 473 | |
| 474 | do |
| 475 | { |
| 476 | if (domain[0] == '\0') |
| 477 | { |
| 478 | nis_free_directory (dir); |
| 479 | return NULL; |
| 480 | } |
| 481 | nis_leaf_of_r (domain, leaf, sizeof (leaf)); |
| 482 | ndomain = __nis_domain_of (domain); |
| 483 | memmove (domain, ndomain, strlen (ndomain) + 1); |
| 484 | } |
| 485 | while (nis_dir_cmp (domain, dir->do_name) != SAME_NAME); |
| 486 | |
| 487 | cp = rawmemchr (leaf, '\0'); |
| 488 | *cp++ = '.'; |
| 489 | strcpy (cp, domain); |
| 490 | |
| 491 | fd_res = __nis_finddirectory (dir, leaf); |
| 492 | if (fd_res == NULL) |
| 493 | { |
| 494 | nis_free_directory (dir); |
| 495 | *status = NIS_NOMEMORY; |
| 496 | return NULL; |
| 497 | } |
| 498 | *status = fd_res->status; |
| 499 | if (fd_res->status != NIS_SUCCESS) |
| 500 | { |
| 501 | /* Try the current directory object, maybe it works */ |
| 502 | __free_fdresult (fd_res); |
| 503 | return dir; |
| 504 | } |
| 505 | nis_free_directory (dir); |
| 506 | obj = calloc (1, sizeof (directory_obj)); |
| 507 | if (obj == NULL) |
| 508 | { |
| 509 | __free_fdresult (fd_res); |
| 510 | *status = NIS_NOMEMORY; |
| 511 | return NULL; |
| 512 | } |
| 513 | xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val, |
| 514 | fd_res->dir_data.dir_data_len, XDR_DECODE); |
| 515 | _xdr_directory_obj (&xdrs, obj); |
| 516 | xdr_destroy (&xdrs); |
| 517 | __free_fdresult (fd_res); |
| 518 | /* We have found a NIS+ server serving ndomain, now |
| 519 | let us search for "name" */ |
| 520 | return rec_dirsearch (name, obj, status); |
| 521 | } |
| 522 | break; |
| 523 | case BAD_NAME: |
| 524 | nis_free_directory (dir); |
| 525 | *status = NIS_BADNAME; |
| 526 | return NULL; |
| 527 | } |
| 528 | nis_free_directory (dir); |
| 529 | *status = NIS_FAIL; |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | /* We try to query the current server for the searched object, |
| 534 | maybe he know about it ? */ |
| 535 | static directory_obj * |
| 536 | first_shoot (const_nis_name name, directory_obj *dir) |
| 537 | { |
| 538 | directory_obj *obj = NULL; |
| 539 | fd_result *fd_res; |
| 540 | XDR xdrs; |
| 541 | |
| 542 | if (nis_dir_cmp (name, dir->do_name) == SAME_NAME) |
| 543 | return dir; |
| 544 | |
| 545 | fd_res = __nis_finddirectory (dir, name); |
| 546 | if (fd_res == NULL) |
| 547 | return NULL; |
| 548 | if (fd_res->status == NIS_SUCCESS |
| 549 | && (obj = calloc (1, sizeof (directory_obj))) != NULL) |
| 550 | { |
| 551 | xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val, |
| 552 | fd_res->dir_data.dir_data_len, XDR_DECODE); |
| 553 | _xdr_directory_obj (&xdrs, obj); |
| 554 | xdr_destroy (&xdrs); |
| 555 | |
| 556 | if (strcmp (dir->do_name, obj->do_name) != 0) |
| 557 | { |
| 558 | nis_free_directory (obj); |
| 559 | obj = NULL; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | __free_fdresult (fd_res); |
| 564 | |
| 565 | if (obj != NULL) |
| 566 | nis_free_directory (dir); |
| 567 | |
| 568 | return obj; |
| 569 | } |
| 570 | |
| 571 | static struct nis_server_cache |
| 572 | { |
| 573 | int search_parent; |
| 574 | int uses; |
| 575 | unsigned int size; |
| 576 | unsigned int server_used; |
| 577 | unsigned int current_ep; |
| 578 | __time64_t expires; |
| 579 | char name[]; |
| 580 | } *nis_server_cache[16]; |
| 581 | static time_t nis_cold_start_mtime; |
| 582 | __libc_lock_define_initialized (static, nis_server_cache_lock) |
| 583 | |
| 584 | static directory_obj * |
| 585 | nis_server_cache_search (const_nis_name name, int search_parent, |
| 586 | unsigned int *server_used, unsigned int *current_ep, |
| 587 | struct __timespec64 *now) |
| 588 | { |
| 589 | directory_obj *ret = NULL; |
| 590 | int i; |
| 591 | char *addr; |
| 592 | XDR xdrs; |
| 593 | struct stat64 st; |
| 594 | |
| 595 | int saved_errno = errno; |
| 596 | if (stat64 ("/var/nis/NIS_COLD_START" , &st) < 0) |
| 597 | st.st_mtime = nis_cold_start_mtime + 1; |
| 598 | __set_errno (saved_errno); |
| 599 | |
| 600 | __libc_lock_lock (nis_server_cache_lock); |
| 601 | |
| 602 | for (i = 0; i < 16; ++i) |
| 603 | if (nis_server_cache[i] == NULL) |
| 604 | continue; |
| 605 | else if (st.st_mtime != nis_cold_start_mtime |
| 606 | || now->tv_sec > nis_server_cache[i]->expires) |
| 607 | { |
| 608 | free (nis_server_cache[i]); |
| 609 | nis_server_cache[i] = NULL; |
| 610 | } |
| 611 | else if (nis_server_cache[i]->search_parent == search_parent |
| 612 | && strcmp (nis_server_cache[i]->name, name) == 0) |
| 613 | { |
| 614 | ret = calloc (1, sizeof (directory_obj)); |
| 615 | if (ret == NULL) |
| 616 | break; |
| 617 | |
| 618 | addr = rawmemchr (nis_server_cache[i]->name, '\0') + 8; |
| 619 | addr = (char *) ((uintptr_t) addr & ~(uintptr_t) 7); |
| 620 | xdrmem_create (&xdrs, addr, nis_server_cache[i]->size, XDR_DECODE); |
| 621 | if (!_xdr_directory_obj (&xdrs, ret)) |
| 622 | { |
| 623 | xdr_destroy (&xdrs); |
| 624 | free (ret); |
| 625 | ret = NULL; |
| 626 | free (nis_server_cache[i]); |
| 627 | nis_server_cache[i] = NULL; |
| 628 | break; |
| 629 | } |
| 630 | xdr_destroy (&xdrs); |
| 631 | *server_used = nis_server_cache[i]->server_used; |
| 632 | *current_ep = nis_server_cache[i]->current_ep; |
| 633 | break; |
| 634 | } |
| 635 | |
| 636 | nis_cold_start_mtime = st.st_mtime; |
| 637 | |
| 638 | __libc_lock_unlock (nis_server_cache_lock); |
| 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | static void |
| 643 | nis_server_cache_add (const_nis_name name, int search_parent, |
| 644 | directory_obj *dir, unsigned int server_used, |
| 645 | unsigned int current_ep, struct __timespec64 *now) |
| 646 | { |
| 647 | struct nis_server_cache **loc; |
| 648 | struct nis_server_cache *new; |
| 649 | struct nis_server_cache *old; |
| 650 | int i; |
| 651 | char *addr; |
| 652 | unsigned int size; |
| 653 | XDR xdrs; |
| 654 | |
| 655 | if (dir == NULL) |
| 656 | return; |
| 657 | |
| 658 | size = xdr_sizeof ((xdrproc_t) _xdr_directory_obj, (char *) dir); |
| 659 | new = calloc (1, sizeof (*new) + strlen (name) + 8 + size); |
| 660 | if (new == NULL) |
| 661 | return; |
| 662 | new->search_parent = search_parent; |
| 663 | new->uses = 1; |
| 664 | new->expires = now->tv_sec + dir->do_ttl; |
| 665 | new->size = size; |
| 666 | new->server_used = server_used; |
| 667 | new->current_ep = current_ep; |
| 668 | addr = stpcpy (new->name, name) + 8; |
| 669 | addr = (char *) ((uintptr_t) addr & ~(uintptr_t) 7); |
| 670 | |
| 671 | xdrmem_create(&xdrs, addr, size, XDR_ENCODE); |
| 672 | if (!_xdr_directory_obj (&xdrs, dir)) |
| 673 | { |
| 674 | xdr_destroy (&xdrs); |
| 675 | free (new); |
| 676 | return; |
| 677 | } |
| 678 | xdr_destroy (&xdrs); |
| 679 | |
| 680 | __libc_lock_lock (nis_server_cache_lock); |
| 681 | |
| 682 | /* Choose which entry should be evicted from the cache. */ |
| 683 | loc = &nis_server_cache[0]; |
| 684 | if (*loc != NULL) |
| 685 | { |
| 686 | for (i = 1; i < 16; ++i) |
| 687 | if (nis_server_cache[i] == NULL) |
| 688 | { |
| 689 | loc = &nis_server_cache[i]; |
| 690 | break; |
| 691 | } |
| 692 | else if ((*loc)->uses > nis_server_cache[i]->uses |
| 693 | || ((*loc)->uses == nis_server_cache[i]->uses |
| 694 | && (*loc)->expires > nis_server_cache[i]->expires)) |
| 695 | loc = &nis_server_cache[i]; |
| 696 | } |
| 697 | old = *loc; |
| 698 | *loc = new; |
| 699 | |
| 700 | __libc_lock_unlock (nis_server_cache_lock); |
| 701 | free (old); |
| 702 | } |
| 703 | |
| 704 | nis_error |
| 705 | __nisfind_server (const_nis_name name, int search_parent, |
| 706 | directory_obj **dir, dir_binding *dbp, unsigned int flags) |
| 707 | { |
| 708 | nis_error result = NIS_SUCCESS; |
| 709 | nis_error status; |
| 710 | directory_obj *obj; |
| 711 | struct __timespec64 ts; |
| 712 | unsigned int server_used = ~0; |
| 713 | unsigned int current_ep = ~0; |
| 714 | |
| 715 | if (name == NULL) |
| 716 | return NIS_BADNAME; |
| 717 | |
| 718 | if (*dir != NULL) |
| 719 | return NIS_SUCCESS; |
| 720 | |
| 721 | __clock_gettime64 (CLOCK_REALTIME, &ts); |
| 722 | |
| 723 | if ((flags & NO_CACHE) == 0) |
| 724 | *dir = nis_server_cache_search (name, search_parent, &server_used, |
| 725 | ¤t_ep, &ts); |
| 726 | if (*dir != NULL) |
| 727 | { |
| 728 | unsigned int server_len = (*dir)->do_servers.do_servers_len; |
| 729 | if (flags & MASTER_ONLY) |
| 730 | { |
| 731 | server_len = 1; |
| 732 | if (server_used != 0) |
| 733 | { |
| 734 | server_used = ~0; |
| 735 | current_ep = ~0; |
| 736 | } |
| 737 | } |
| 738 | result = __nisbind_create (dbp, (*dir)->do_servers.do_servers_val, |
| 739 | server_len, server_used, current_ep, flags); |
| 740 | if (result != NIS_SUCCESS) |
| 741 | { |
| 742 | nis_free_directory (*dir); |
| 743 | *dir = NULL; |
| 744 | } |
| 745 | return result; |
| 746 | } |
| 747 | |
| 748 | int saved_errno = errno; |
| 749 | *dir = readColdStartFile (); |
| 750 | __set_errno (saved_errno); |
| 751 | if (*dir == NULL) |
| 752 | /* No /var/nis/NIS_COLD_START->no NIS+ installed. */ |
| 753 | return NIS_UNAVAIL; |
| 754 | |
| 755 | /* Try at first, if servers in "dir" know our object */ |
| 756 | const char *search_name = name; |
| 757 | if (search_parent) |
| 758 | search_name = __nis_domain_of (name); |
| 759 | obj = first_shoot (search_name, *dir); |
| 760 | if (obj == NULL) |
| 761 | { |
| 762 | obj = rec_dirsearch (search_name, *dir, &status); |
| 763 | if (obj == NULL) |
| 764 | result = status; |
| 765 | } |
| 766 | |
| 767 | if (result == NIS_SUCCESS) |
| 768 | { |
| 769 | unsigned int server_len = obj->do_servers.do_servers_len; |
| 770 | if (flags & MASTER_ONLY) |
| 771 | server_len = 1; |
| 772 | result = __nisbind_create (dbp, obj->do_servers.do_servers_val, |
| 773 | server_len, ~0, ~0, flags); |
| 774 | if (result == NIS_SUCCESS) |
| 775 | { |
| 776 | if ((flags & MASTER_ONLY) == 0 |
| 777 | || obj->do_servers.do_servers_len == 1) |
| 778 | { |
| 779 | server_used = dbp->server_used; |
| 780 | current_ep = dbp->current_ep; |
| 781 | } |
| 782 | if ((flags & NO_CACHE) == 0) |
| 783 | nis_server_cache_add (name, search_parent, obj, |
| 784 | server_used, current_ep, &ts); |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | nis_free_directory (obj); |
| 789 | obj = NULL; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | *dir = obj; |
| 794 | |
| 795 | return result; |
| 796 | } |
| 797 | |
| 798 | |
| 799 | nis_error |
| 800 | __prepare_niscall (const_nis_name name, directory_obj **dirp, |
| 801 | dir_binding *bptrp, unsigned int flags) |
| 802 | { |
| 803 | nis_error retcode = __nisfind_server (name, 1, dirp, bptrp, flags); |
| 804 | if (__glibc_unlikely (retcode != NIS_SUCCESS)) |
| 805 | return retcode; |
| 806 | |
| 807 | do |
| 808 | if (__nisbind_connect (bptrp) == NIS_SUCCESS) |
| 809 | return NIS_SUCCESS; |
| 810 | while (__nisbind_next (bptrp) == NIS_SUCCESS); |
| 811 | |
| 812 | __nisbind_destroy (bptrp); |
| 813 | memset (bptrp, '\0', sizeof (*bptrp)); |
| 814 | |
| 815 | retcode = NIS_NAMEUNREACHABLE; |
| 816 | nis_free_directory (*dirp); |
| 817 | *dirp = NULL; |
| 818 | |
| 819 | return retcode; |
| 820 | } |
| 821 | libnsl_hidden_nolink_def (__prepare_niscall, GLIBC_PRIVATE) |
| 822 | |
| 823 | |
| 824 | nis_error |
| 825 | __do_niscall (const_nis_name name, u_long prog, xdrproc_t xargs, |
| 826 | caddr_t req, xdrproc_t xres, caddr_t resp, unsigned int flags, |
| 827 | nis_cb *cb) |
| 828 | { |
| 829 | dir_binding bptr; |
| 830 | directory_obj *dir = NULL; |
| 831 | int saved_errno = errno; |
| 832 | |
| 833 | nis_error retcode = __prepare_niscall (name, &dir, &bptr, flags); |
| 834 | if (retcode == NIS_SUCCESS) |
| 835 | { |
| 836 | retcode = __do_niscall3 (&bptr, prog, xargs, req, xres, resp, flags, cb); |
| 837 | |
| 838 | __nisbind_destroy (&bptr); |
| 839 | |
| 840 | nis_free_directory (dir); |
| 841 | } |
| 842 | |
| 843 | __set_errno (saved_errno); |
| 844 | |
| 845 | return retcode; |
| 846 | } |
| 847 | |