add UNLEASHED_OBJ to unleashed.mk
[unleashed/tickless.git] / usr / src / cmd / ypcmd / ypserv_proc.c
blob5b56511809ba1e293a1c6c9ba6fec292a8efea6c
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright (c) 2016 by Delphix. All rights reserved.
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 * Portions of this source code were derived from Berkeley 4.3 BSD
33 * under license from the Regents of the University of California.
38 * This contains YP server code which supplies the set of functions
39 * requested using rpc. The top level functions in this module
40 * are those which have symbols of the form YPPROC_xxxx defined in
41 * yp_prot.h, and symbols of the form YPOLDPROC_xxxx defined in ypsym.h.
42 * The latter exist to provide compatibility to the old version of the yp
43 * protocol/server, and may emulate the behavior of the previous software
44 * by invoking some other program.
46 * This module also contains functions which are used by (and only by) the
47 * top-level functions here.
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <dirent.h>
55 #include <limits.h>
56 #include <sys/systeminfo.h>
57 #include <rpc/rpc.h>
58 #include <string.h>
59 #include <malloc.h>
60 #include <stdlib.h>
61 #include <unistd.h>
62 #include <stdio.h>
63 #include "ypsym.h"
64 #include "ypdefs.h"
65 #include <ctype.h>
67 /* Use shim version of DBM calls */
68 #include "shim.h"
69 #include "shim_hooks.h"
71 USE_YP_PREFIX
72 USE_YP_SECURE
73 USE_YP_INTERDOMAIN
75 #ifndef YPXFR_PROC
76 #define YPXFR_PROC "/usr/lib/netsvc/yp/ypxfr"
77 #endif
78 static char ypxfr_proc[] = YPXFR_PROC;
79 #ifndef YPPUSH_PROC
80 #define YPPUSH_PROC "/usr/lib/netsvc/yp/yppush"
81 #endif
82 static char yppush_proc[] = YPPUSH_PROC;
83 struct yppriv_sym {
84 char *sym;
85 unsigned len;
87 static char err_fork[] = "ypserv: %s fork failure.\n";
88 #define FORK_ERR logprintf(err_fork, fun)
89 static char err_execl[] = "ypserv: %s execl failure.\n";
90 #define EXEC_ERR logprintf(err_execl, fun)
91 static char err_respond[] = "ypserv: %s can't respond to rpc request.\n";
92 #define RESPOND_ERR logprintf(err_respond, fun)
93 static char err_free[] = "ypserv: %s can't free args.\n";
94 #define FREE_ERR logprintf(err_free, fun)
95 static char err_map[] = "ypserv: %s no such map or access denied.\n";
96 #define MAP_ERR logprintf(err_map, fun)
97 static char err_vers[] = "ypserv: %s version not supported.\n";
98 #define VERS_ERR logprintf(err_vers, fun)
100 static void ypfilter(DBM *fdb, datum *inkey, datum *outkey, datum *val,
101 uint_t *status, bool_t update);
102 static bool isypsym(datum *key);
103 static bool xdrypserv_ypall(XDR *xdrs, struct ypreq_nokey *req);
104 static int multihomed(struct ypreq_key req, struct ypresp_val *resp,
105 SVCXPRT *xprt, DBM *fdb);
106 static int omultihomed(struct yprequest req, struct ypresponse *resp,
107 SVCXPRT *xprt, DBM *fdb);
110 /* For DNS forwarding */
111 extern bool dnsforward;
112 extern bool client_setup_failure;
113 extern int resolv_pid;
114 extern CLIENT *resolv_client;
115 extern char *resolv_tp;
118 * This determines whether or not a passed domain is served by this
119 * server, and returns a boolean. Used by both old and new protocol
120 * versions.
122 void
123 ypdomain(SVCXPRT *transp, bool always_respond)
125 char domain_name[YPMAXDOMAIN + 1];
126 char *pdomain_name = domain_name;
127 bool isserved;
128 char *fun = "ypdomain";
129 struct netbuf *nbuf;
130 sa_family_t af;
132 memset(domain_name, 0, sizeof (domain_name));
134 if (!svc_getargs(transp, (xdrproc_t)xdr_ypdomain_wrap_string,
135 (caddr_t)&pdomain_name)) {
136 svcerr_decode(transp);
137 return;
141 * If the file /var/yp/securenets is present on the server, and if
142 * the hostname is present in the file, then let the client bind to
143 * the server.
145 nbuf = svc_getrpccaller(transp);
146 af = ((struct sockaddr_storage *)nbuf->buf)->ss_family;
147 if (af != AF_INET && af != AF_INET6) {
148 logprintf("Protocol incorrect\n");
149 return;
152 if (!(check_secure_net_ti(nbuf, fun))) {
153 MAP_ERR;
154 return;
157 isserved = ypcheck_domain(domain_name);
159 if (isserved || always_respond) {
161 if (!svc_sendreply(transp, xdr_bool, (char *)&isserved)) {
162 RESPOND_ERR;
164 if (!isserved)
165 logprintf("Domain %s not supported\n",
166 domain_name);
168 } else {
170 * This case is the one in which the domain is not
171 * supported, and in which we are not to respond in the
172 * unsupported case. We are going to make an error happen
173 * to allow the portmapper to end its wait without the
174 * normal timeout period. The assumption here is that
175 * the only process in the world which is using the function
176 * in its no-answer-if-nack form is the portmapper, which is
177 * doing the krock for pseudo-broadcast. If some poor fool
178 * calls this function as a single-cast message, the nack
179 * case will look like an incomprehensible error. Sigh...
180 * (The traditional Unix disclaimer)
183 svcerr_decode(transp);
184 logprintf("Domain %s not supported (broadcast)\n",
185 domain_name);
190 * This implements the yp "match" function.
192 void
193 ypmatch(SVCXPRT *transp, struct svc_req *rqstp)
195 struct ypreq_key req;
196 struct ypresp_val resp;
197 char *fun = "ypmatch";
198 DBM *fdb;
200 memset(&req, 0, sizeof (req));
201 memset(&resp, 0, sizeof (resp));
202 resp.status = (unsigned)YP_NOKEY;
204 if (!svc_getargs(transp, (xdrproc_t)xdr_ypreq_key, (char *)&req)) {
205 svcerr_decode(transp);
206 return;
210 * sanity check the map name and to a DBM lookup
211 * also perform an access check...
213 if ((fdb = ypset_current_map(req.map, req.domain,
214 &resp.status)) != NULL &&
215 yp_map_access(transp, &resp.status, fdb)) {
217 /* Check with the DBM database */
218 resp.valdat = dbm_fetch(fdb, req.keydat);
219 if (resp.valdat.dptr != NULL) {
220 resp.status = YP_TRUE;
221 if (!silent)
222 printf("%s: dbm: %40.40s\n",
223 fun, resp.valdat.dptr);
224 goto send_reply;
228 * If we're being asked to match YP_SECURE or YP_INTERDOMAIN
229 * and we haven't found it in the dbm file, then we don't
230 * really want to waste any more time. Specifically, we don't
231 * want to ask DNS
233 if (req.keydat.dsize == 0 ||
234 req.keydat.dptr == NULL ||
235 req.keydat.dptr[0] == '\0' ||
236 strncmp(req.keydat.dptr,
237 yp_secure, req.keydat.dsize) == 0 ||
238 strncmp(req.keydat.dptr, yp_interdomain,
239 req.keydat.dsize) == 0) {
240 goto send_reply;
243 /* Let's try the YP_MULTI_ hack... */
244 #ifdef MINUS_C_OPTION
245 if (multiflag == TRUE && multihomed(req, &resp, transp, fdb))
246 goto send_reply;
247 #else
248 if (multihomed(req, &resp, transp, fdb))
249 goto send_reply;
250 #endif
253 * Let's try DNS, but if client_setup_failure is set,
254 * we have tried DNS in the past and failed, there is
255 * no reason in forcing an infinite loop by turning
256 * off DNS in setup_resolv() only to turn it back on
257 * again here.
259 if (!dnsforward && !client_setup_failure) {
260 datum idkey, idval;
261 idkey.dptr = yp_interdomain;
262 idkey.dsize = yp_interdomain_sz;
263 idval = dbm_fetch(fdb, idkey);
264 if (idval.dptr)
265 dnsforward = TRUE;
268 if (dnsforward) {
269 if (!resolv_pid || !resolv_client) {
270 setup_resolv(&dnsforward, &resolv_pid,
271 &resolv_client, resolv_tp, 0);
272 if (resolv_client == NULL)
273 client_setup_failure = TRUE;
276 if (resolv_req(&dnsforward, &resolv_client,
277 &resolv_pid, resolv_tp,
278 rqstp->rq_xprt, &req,
279 req.map) == TRUE)
280 goto free_args;
283 send_reply:
285 if (!svc_sendreply(transp, (xdrproc_t)xdr_ypresp_val,
286 (caddr_t)&resp)) {
287 RESPOND_ERR;
290 free_args:
292 if (!svc_freeargs(transp, (xdrproc_t)xdr_ypreq_key,
293 (char *)&req)) {
294 FREE_ERR;
300 * This implements the yp "get first" function.
302 void
303 ypfirst(SVCXPRT *transp)
305 struct ypreq_nokey req;
306 struct ypresp_key_val resp;
307 char *fun = "ypfirst";
308 DBM *fdb;
310 memset(&req, 0, sizeof (req));
311 memset(&resp, 0, sizeof (resp));
313 if (!svc_getargs(transp,
314 (xdrproc_t)xdr_ypreq_nokey,
315 (char *)&req)) {
316 svcerr_decode(transp);
317 return;
320 if ((fdb = ypset_current_map(req.map, req.domain,
321 &resp.status)) != NULL &&
322 yp_map_access(transp, &resp.status, fdb)) {
323 ypfilter(fdb, NULL,
324 &resp.keydat, &resp.valdat, &resp.status, FALSE);
327 if (!svc_sendreply(transp,
328 (xdrproc_t)xdr_ypresp_key_val,
329 (char *)&resp)) {
330 RESPOND_ERR;
333 if (!svc_freeargs(transp, (xdrproc_t)xdr_ypreq_nokey,
334 (char *)&req)) {
335 FREE_ERR;
340 * This implements the yp "get next" function.
342 void
343 ypnext(SVCXPRT *transp)
345 struct ypreq_key req;
346 struct ypresp_key_val resp;
347 char *fun = "ypnext";
348 DBM *fdb;
350 memset(&req, 0, sizeof (req));
351 memset(&resp, 0, sizeof (resp));
353 if (!svc_getargs(transp, (xdrproc_t)xdr_ypreq_key, (char *)&req)) {
354 svcerr_decode(transp);
355 return;
358 if ((fdb = ypset_current_map(req.map, req.domain,
359 &resp.status)) != NULL &&
360 yp_map_access(transp, &resp.status, fdb)) {
361 ypfilter(fdb, &req.keydat,
362 &resp.keydat, &resp.valdat, &resp.status, FALSE);
365 if (!svc_sendreply(transp,
366 (xdrproc_t)xdr_ypresp_key_val,
367 (char *)&resp)) {
368 RESPOND_ERR;
371 if (!svc_freeargs(transp,
372 (xdrproc_t)xdr_ypreq_key,
373 (char *)&req)) {
374 FREE_ERR;
379 * This implements the "transfer map" function. It takes the domain
380 * and map names and the callback information provided by the
381 * requester (yppush on some node), and execs a ypxfr process to do
382 * the actual transfer.
384 void
385 ypxfr(SVCXPRT *transp, int prog)
387 struct ypreq_newxfr newreq;
388 struct ypreq_xfr oldreq;
389 struct ypresp_val resp; /* not returned to the caller */
390 char transid[32];
391 char proto[32];
392 char name[256];
393 char *pdomain, *pmap;
394 pid_t pid = -1;
395 char *fun = "ypxfr";
396 DBM *fdb;
398 if (prog == YPPROC_NEWXFR) {
399 memset(&newreq, 0, sizeof (newreq));
400 if (!svc_getargs(transp, (xdrproc_t)xdr_ypreq_newxfr,
401 (char *)&newreq)) {
402 svcerr_decode(transp);
403 return;
406 #ifdef OPCOM_DEBUG
407 fprintf(stderr, "newreq:\n"
408 "\tmap_parms:\n"
409 "\t\tdomain: %s\n"
410 "\t\tmap: %s\n"
411 "\t\tordernum: %u\n"
412 "\t\towner: %s\n"
413 "\ttransid: %u\n"
414 "\tproto: %u\n"
415 "\tname: %s\n\n",
416 newreq.map_parms.domain,
417 newreq.map_parms.map,
418 newreq.map_parms.ordernum,
419 newreq.map_parms.owner,
420 newreq.transid,
421 newreq.proto,
422 newreq.name);
423 #endif
424 sprintf(transid, "%u", newreq.transid);
425 sprintf(proto, "%u", newreq.proto);
426 sprintf(name, "%s", newreq.ypxfr_owner);
427 pdomain = newreq.ypxfr_domain;
428 pmap = newreq.ypxfr_map;
429 } else if (prog == YPPROC_XFR) {
430 memset(&oldreq, 0, sizeof (oldreq));
431 if (!svc_getargs(transp,
432 (xdrproc_t)xdr_ypreq_xfr,
433 (char *)&oldreq)) {
434 svcerr_decode(transp);
435 return;
438 #ifdef OPCOM_DEBUG
439 fprintf(stderr, "oldreq:\n"
440 "\tmap_parms:\n"
441 "\t\tdomain: %s\n"
442 "\t\tmap: %s\n"
443 "\t\tordernum: %u\n"
444 "\t\towner: %s\n"
445 "\ttransid: %u\n"
446 "\tproto: %u\n"
447 "\tport: %u\n\n",
448 oldreq.map_parms.domain,
449 oldreq.map_parms.map,
450 oldreq.map_parms.ordernum,
451 oldreq.map_parms.owner,
452 oldreq.transid,
453 oldreq.proto,
454 oldreq.port);
455 #endif
457 sprintf(transid, "%u", oldreq.transid);
458 sprintf(proto, "%u", oldreq.proto);
459 sprintf(name, "%s", oldreq.ypxfr_owner);
460 pdomain = oldreq.ypxfr_domain;
461 pmap = oldreq.ypxfr_map;
462 } else {
463 VERS_ERR;
466 /* Check that the map exists and is accessible */
467 if ((fdb = ypset_current_map(pmap, pdomain, &resp.status)) != NULL &&
468 yp_map_access(transp, &resp.status, fdb)) {
470 pid = vfork();
471 if (pid == -1) {
472 FORK_ERR;
473 } else if (pid == 0) {
474 if (prog == YPPROC_NEWXFR || prog == YPPROC_XFR) {
475 #ifdef OPCOM_DEBUG
476 fprintf(stderr,
477 "EXECL: %s, -d, %s, -C, %s, %s, %s, %s\n",
478 ypxfr_proc, pdomain,
479 transid, proto, name, pmap);
480 #endif
481 if (execl(ypxfr_proc, "ypxfr", "-d",
482 pdomain, "-C", transid, proto,
483 name, pmap, NULL))
484 EXEC_ERR;
485 } else {
486 VERS_ERR;
488 _exit(1);
491 } else {
492 MAP_ERR;
494 if (!svc_sendreply(transp, xdr_void, 0)) {
495 RESPOND_ERR;
498 if (prog == YPPROC_NEWXFR) {
499 if (!svc_freeargs(transp,
500 (xdrproc_t)xdr_ypreq_newxfr,
501 (char *)&newreq)) {
502 FREE_ERR;
508 * This implements the "get all" function.
510 void
511 ypall(SVCXPRT *transp)
513 struct ypreq_nokey req;
514 struct ypresp_val resp; /* not returned to the caller */
515 pid_t pid;
516 char *fun = "ypall";
517 DBM *fdb;
519 req.domain = req.map = NULL;
521 memset((char *)&req, 0, sizeof (req));
523 if (!svc_getargs(transp,
524 (xdrproc_t)xdr_ypreq_nokey,
525 (char *)&req)) {
526 svcerr_decode(transp);
527 return;
530 pid = fork1();
532 if (pid) {
534 if (pid == -1) {
535 FORK_ERR;
538 if (!svc_freeargs(transp,
539 (xdrproc_t)xdr_ypreq_nokey,
540 (char *)&req)) {
541 FREE_ERR;
544 return;
548 * access control hack: If denied then invalidate the map name.
550 ypclr_current_map();
551 if ((fdb = ypset_current_map(req.map,
552 req.domain, &resp.status)) != NULL &&
553 !yp_map_access(transp, &resp.status, fdb)) {
555 req.map[0] = '-';
559 * This is the child process. The work gets done by xdrypserv_ypall/
560 * we must clear the "current map" first so that we do not
561 * share a seek pointer with the parent server.
564 if (!svc_sendreply(transp,
565 (xdrproc_t)xdrypserv_ypall,
566 (char *)&req)) {
567 RESPOND_ERR;
570 if (!svc_freeargs(transp,
571 (xdrproc_t)xdr_ypreq_nokey,
572 (char *)&req)) {
573 FREE_ERR;
577 * In yptol mode we may start a cache update thread within a child
578 * process. It is thus important that child processes do not exit,
579 * killing any such threads, before the thread has completed.
581 if (yptol_mode) {
582 thr_join(0, NULL, NULL);
585 exit(0);
589 * This implements the "get master name" function.
591 void
592 ypmaster(SVCXPRT *transp)
594 struct ypreq_nokey req;
595 struct ypresp_master resp;
596 char *nullstring = "";
597 char *fun = "ypmaster";
598 DBM *fdb;
600 memset((char *)&req, 0, sizeof (req));
601 resp.master = nullstring;
602 resp.status = YP_TRUE;
604 if (!svc_getargs(transp,
605 (xdrproc_t)xdr_ypreq_nokey,
606 (char *)&req)) {
607 svcerr_decode(transp);
608 return;
611 if ((fdb = ypset_current_map(req.map,
612 req.domain, &resp.status)) != NULL &&
613 yp_map_access(transp, &resp.status, fdb)) {
615 if (!ypget_map_master(&resp.master, fdb)) {
616 resp.status = (unsigned)YP_BADDB;
620 if (!svc_sendreply(transp,
621 (xdrproc_t)xdr_ypresp_master,
622 (char *)&resp)) {
623 RESPOND_ERR;
626 if (!svc_freeargs(transp,
627 (xdrproc_t)xdr_ypreq_nokey,
628 (char *)&req)) {
629 FREE_ERR;
634 * This implements the "get order number" function.
636 void
637 yporder(SVCXPRT *transp)
639 struct ypreq_nokey req;
640 struct ypresp_order resp;
641 char *fun = "yporder";
642 DBM *fdb;
644 req.domain = req.map = NULL;
645 resp.status = YP_TRUE;
646 resp.ordernum = 0;
648 memset((char *)&req, 0, sizeof (req));
650 if (!svc_getargs(transp,
651 (xdrproc_t)xdr_ypreq_nokey,
652 (char *)&req)) {
653 svcerr_decode(transp);
654 return;
657 resp.ordernum = 0;
659 if ((fdb = ypset_current_map(req.map,
660 req.domain,
661 &resp.status)) != NULL &&
662 yp_map_access(transp, &resp.status, fdb)) {
664 if (!ypget_map_order(req.map, req.domain, &resp.ordernum)) {
665 resp.status = (unsigned)YP_BADDB;
669 if (!svc_sendreply(transp,
670 (xdrproc_t)xdr_ypresp_order,
671 (char *)&resp)) {
672 RESPOND_ERR;
675 if (!svc_freeargs(transp,
676 (xdrproc_t)xdr_ypreq_nokey,
677 (char *)&req)) {
678 FREE_ERR;
682 void
683 ypmaplist(SVCXPRT *transp)
685 char domain_name[YPMAXDOMAIN + 1];
686 char *pdomain = domain_name;
687 char *fun = "ypmaplist";
688 struct ypresp_maplist maplist;
689 struct ypmaplist *tmp;
691 maplist.list = NULL;
693 memset(domain_name, 0, sizeof (domain_name));
695 if (!svc_getargs(transp,
696 (xdrproc_t)xdr_ypdomain_wrap_string,
697 (caddr_t)&pdomain)) {
698 svcerr_decode(transp);
699 return;
702 maplist.status = yplist_maps(domain_name, &maplist.list);
704 if (!svc_sendreply(transp,
705 (xdrproc_t)xdr_ypresp_maplist,
706 (char *)&maplist)) {
707 RESPOND_ERR;
710 while (maplist.list) {
711 tmp = maplist.list->ypml_next;
712 free((char *)maplist.list);
713 maplist.list = tmp;
718 * Ancillary functions used by the top-level functions within this
719 * module
723 * This returns TRUE if a given key is a yp-private symbol, otherwise
724 * FALSE
726 static bool
727 isypsym(datum *key)
729 if ((key->dptr == NULL) ||
730 (key->dsize < yp_prefix_sz) ||
731 memcmp(yp_prefix, key->dptr, yp_prefix_sz) ||
732 (!memcmp(key->dptr, "YP_MULTI_", 9))) {
733 return (FALSE);
735 return (TRUE);
739 * This provides private-symbol filtration for the enumeration functions.
741 static void
742 ypfilter(DBM *fdb, datum *inkey, datum *outkey, datum *val, uint_t *status,
743 bool_t update)
745 datum k;
747 if (inkey) {
749 if (isypsym(inkey)) {
750 *status = (unsigned)YP_BADARGS;
751 return;
754 k = dbm_do_nextkey(fdb, *inkey);
755 } else {
756 k = dbm_firstkey(fdb);
759 while (k.dptr && isypsym(&k)) {
760 k = dbm_nextkey(fdb);
763 if (k.dptr == NULL) {
764 *status = YP_NOMORE;
765 return;
768 *outkey = k;
771 * In N2L mode we must call a version of dbm_fetch() that either does
772 * or does not check for entry updates. In non N2L mode both of these
773 * will end up doing a normal dbm_fetch().
775 if (update)
776 *val = shim_dbm_fetch(fdb, k);
777 else
778 *val = shim_dbm_fetch_noupdate(fdb, k);
780 if (val->dptr != NULL) {
781 *status = YP_TRUE;
782 } else {
783 *status = (unsigned)YP_BADDB;
788 * Serializes a stream of struct ypresp_key_val's. This is used
789 * only by the ypserv side of the transaction.
791 static bool
792 xdrypserv_ypall(XDR *xdrs, struct ypreq_nokey *req)
794 bool_t more = TRUE;
795 struct ypresp_key_val resp;
796 DBM *fdb;
798 resp.keydat.dptr = resp.valdat.dptr = NULL;
799 resp.keydat.dsize = resp.valdat.dsize = 0;
801 if ((fdb = ypset_current_map(req->map, req->domain,
802 &resp.status)) != NULL) {
803 ypfilter(fdb, (datum *) NULL, &resp.keydat, &resp.valdat,
804 &resp.status, FALSE);
806 while (resp.status == YP_TRUE) {
807 if (!xdr_bool(xdrs, &more)) {
808 return (FALSE);
811 if (!xdr_ypresp_key_val(xdrs, &resp)) {
812 return (FALSE);
815 ypfilter(fdb, &resp.keydat, &resp.keydat, &resp.valdat,
816 &resp.status, FALSE);
820 if (!xdr_bool(xdrs, &more)) {
821 return (FALSE);
824 if (!xdr_ypresp_key_val(xdrs, &resp)) {
825 return (FALSE);
828 more = FALSE;
830 if (!xdr_bool(xdrs, &more)) {
831 return (FALSE);
834 return (TRUE);
838 * Additions for sparc cluster support
842 * Check for special multihomed host cookie in the key. If there,
843 * collect the addresses from the comma separated list and return
844 * the one that's nearest the client.
846 static int
847 multihomed(struct ypreq_key req, struct ypresp_val *resp,
848 SVCXPRT *xprt, DBM *fdb)
850 char *cp, *bp;
851 ulong_t bestaddr, call_addr;
852 struct netbuf *nbuf;
853 char name[PATH_MAX];
854 static char localbuf[_PBLKSIZ]; /* buffer for multihomed IPv6 addr */
856 if (strcmp(req.map, "hosts.byname") &&
857 strcmp(req.map, "ipnodes.byname"))
858 /* default status is YP_NOKEY */
859 return (0);
861 if (strncmp(req.keydat.dptr, "YP_MULTI_", 9)) {
862 datum tmpname;
864 strncpy(name, "YP_MULTI_", 9);
865 strncpy(name + 9, req.keydat.dptr, req.keydat.dsize);
866 tmpname.dsize = req.keydat.dsize + 9;
867 tmpname.dptr = name;
868 resp->valdat = dbm_fetch(fdb, tmpname);
869 } else {
871 * Return whole line (for debugging) if YP_MULTI_hostnam
872 * is specified.
874 resp->valdat = dbm_fetch(fdb, req.keydat);
875 if (resp->valdat.dptr != NULL)
876 return (1);
879 if (resp->valdat.dptr == NULL)
880 return (0);
882 strncpy(name, req.keydat.dptr, req.keydat.dsize);
883 name[req.keydat.dsize] = 0;
885 if (strcmp(req.map, "ipnodes.byname") == 0) {
887 * This section handles multihomed IPv6 addresses.
888 * It returns all the IPv6 addresses one per line and only
889 * the requested hostname is returned. NO aliases will be
890 * returned. This is done exactly the same way DNS forwarding
891 * daemon handles multihomed hosts.
892 * New IPv6 enabled clients should be able to handle this
893 * information returned. The sorting is also the client's
894 * responsibility.
897 char *buf, *endbuf;
899 if ((buf = strdup(resp->valdat.dptr)) == NULL) /* no memory */
900 return (0);
901 if ((bp = strtok(buf, " \t")) == NULL) { /* no address field */
902 free(buf);
903 return (0);
905 if ((cp = strtok(NULL, "")) == NULL) { /* no host field */
906 free(buf);
907 return (0);
909 if ((cp = strtok(bp, ",")) != NULL) { /* multihomed host */
910 int bsize;
912 localbuf[0] = '\0';
913 bsize = sizeof (localbuf);
914 endbuf = localbuf;
916 while (cp) {
917 if ((strlen(cp) + strlen(name)) >= bsize) {
918 /* out of range */
919 break;
921 sprintf(endbuf, "%s %s\n", cp, name);
922 cp = strtok(NULL, ",");
923 endbuf = &endbuf[strlen(endbuf)];
924 bsize = &localbuf[sizeof (localbuf)] - endbuf;
926 resp->valdat.dptr = localbuf;
927 resp->valdat.dsize = strlen(localbuf);
930 free(buf);
931 /* remove trailing newline */
932 if (resp->valdat.dsize &&
933 resp->valdat.dptr[resp->valdat.dsize-1] == '\n') {
934 resp->valdat.dptr[resp->valdat.dsize-1] = '\0';
935 resp->valdat.dsize -= 1;
938 resp->status = YP_TRUE;
939 return (1);
941 nbuf = svc_getrpccaller(xprt);
943 * OK, now I have a netbuf structure which I'm supposed to
944 * treat as opaque... I hate transport independance!
945 * So, we're just gonna doit wrong... By wrong I mean that
946 * we assume that the buf part of the netbuf structure is going
947 * to be a sockaddr_in. We'll then check the assumed family
948 * member and hope that we find AF_INET in there... if not
949 * then we can't continue.
951 if (((struct sockaddr_in *)(nbuf->buf))->sin_family != AF_INET)
952 return (0);
954 call_addr = ((struct sockaddr_in *)(nbuf->buf))->sin_addr.s_addr;
956 cp = resp->valdat.dptr;
957 if ((bp = strtok(cp, " \t")) == NULL) /* no address field */
958 return (0);
959 if ((cp = strtok(NULL, "")) == NULL) /* no host field */
960 return (0);
961 bp = strtok(bp, ",");
963 bestaddr = inet_addr(bp);
964 while (cp = strtok(NULL, ",")) {
965 ulong_t taddr;
967 taddr = inet_addr(cp);
968 if (ntohl(call_addr ^ taddr) < ntohl(call_addr ^ bestaddr))
969 bestaddr = taddr;
971 cp = resp->valdat.dptr;
972 sprintf(cp, "%s %s", inet_ntoa(*(struct in_addr *)&bestaddr), name);
973 resp->valdat.dsize = strlen(cp);
975 resp->status = YP_TRUE;
977 return (1);
980 /* V1 dispatch routines */
981 void
982 ypoldmatch(SVCXPRT *transp, struct svc_req *rqstp)
984 bool dbmop_ok = TRUE;
985 struct yprequest req;
986 struct ypreq_key nrq;
987 struct ypresponse resp;
988 char *fun = "ypoldmatch";
989 DBM *fdb;
991 memset(&req, 0, sizeof (req));
992 memset(&resp, 0, sizeof (resp));
994 if (!svc_getargs(transp,
995 (xdrproc_t)_xdr_yprequest,
996 (caddr_t)&req)) {
997 svcerr_decode(transp);
998 return;
1001 if (req.yp_reqtype != YPMATCH_REQTYPE) {
1002 resp.ypmatch_resp_status = (unsigned)YP_BADARGS;
1003 dbmop_ok = FALSE;
1006 if (dbmop_ok &&
1007 (((fdb = ypset_current_map(req.ypmatch_req_map,
1008 req.ypmatch_req_domain,
1009 &resp.ypmatch_resp_status))
1010 != NULL) &&
1011 yp_map_access(transp,
1012 &resp.ypmatch_resp_status,
1013 fdb))) {
1015 /* Check with the DBM database */
1016 resp.ypmatch_resp_valdat = dbm_fetch(fdb,
1017 req.ypmatch_req_keydat);
1019 if (resp.ypmatch_resp_valptr != NULL) {
1020 resp.ypmatch_resp_status = YP_TRUE;
1021 if (!silent)
1022 printf("%s: dbm: %s\n",
1023 fun, resp.ypmatch_resp_valptr);
1024 goto send_oldreply;
1028 * If we're being asked to match YP_SECURE or YP_INTERDOMAIN
1029 * and we haven't found it in the dbm file, then we don't
1030 * really want to waste any more time. Specifically, we don't
1031 * want to ask DNS
1033 if (req.ypmatch_req_keysize == 0 ||
1034 req.ypmatch_req_keyptr == NULL ||
1035 req.ypmatch_req_keyptr[0] == '\0' ||
1036 strncmp(req.ypmatch_req_keyptr, "YP_SECURE", 9) == 0 ||
1037 strncmp(req.ypmatch_req_keyptr, "YP_INTERDOMAIN", 14) == 0)
1039 goto send_oldreply;
1041 /* Let's try the YP_MULTI_ hack... */
1042 #ifdef MINUS_C_OPTION
1043 if (multiflag == TRUE && omultihomed(req, &resp, transp, fdb))
1044 goto send_oldreply;
1045 #else
1046 if (omultihomed(req, &resp, transp, fdb))
1047 goto send_oldreply;
1048 #endif
1050 /* Let's try DNS */
1051 if (!dnsforward) {
1052 USE_YP_INTERDOMAIN
1053 datum idkey, idval;
1054 idkey.dptr = yp_interdomain;
1055 idkey.dsize = yp_interdomain_sz;
1056 idval = dbm_fetch(fdb, idkey);
1057 if (idval.dptr)
1058 dnsforward = TRUE;
1061 if (dnsforward) {
1062 if (!resolv_pid)
1063 setup_resolv(&dnsforward, &resolv_pid,
1064 &resolv_client,
1065 resolv_tp, 0);
1067 if (req.yp_reqtype == YPREQ_KEY) {
1068 nrq = req.yp_reqbody.yp_req_keytype;
1070 resolv_req(&dnsforward, &resolv_client,
1071 &resolv_pid,
1072 resolv_tp, rqstp->rq_xprt,
1073 &nrq, nrq.map);
1075 return;
1079 send_oldreply:
1081 if (!svc_sendreply(transp,
1082 (xdrproc_t)_xdr_ypresponse,
1083 (caddr_t)&resp)) {
1084 RESPOND_ERR;
1087 if (!svc_freeargs(transp,
1088 (xdrproc_t)_xdr_yprequest,
1089 (char *)&req)) {
1090 FREE_ERR;
1094 void
1095 ypoldfirst(SVCXPRT *transp)
1097 bool dbmop_ok = TRUE;
1098 struct yprequest req;
1099 struct ypresponse resp;
1100 char *fun = "ypoldfirst";
1101 DBM *fdb;
1103 memset(&req, 0, sizeof (req));
1104 memset(&resp, 0, sizeof (resp));
1106 if (!svc_getargs(transp,
1107 (xdrproc_t)_xdr_yprequest,
1108 (caddr_t)&req)) {
1109 svcerr_decode(transp);
1110 return;
1113 if (req.yp_reqtype != YPFIRST_REQTYPE) {
1114 resp.ypfirst_resp_status = (unsigned)YP_BADARGS;
1115 dbmop_ok = FALSE;
1118 if (dbmop_ok &&
1119 ((fdb = ypset_current_map(req.ypfirst_req_map,
1120 req.ypfirst_req_domain,
1121 &resp.ypfirst_resp_status))
1122 != NULL) &&
1123 yp_map_access(transp,
1124 &resp.ypfirst_resp_status,
1125 fdb)) {
1127 resp.ypfirst_resp_keydat = dbm_firstkey(fdb);
1129 if (resp.ypfirst_resp_keyptr != NULL) {
1130 resp.ypfirst_resp_valdat =
1131 dbm_fetch(fdb, resp.ypfirst_resp_keydat);
1133 if (resp.ypfirst_resp_valptr != NULL) {
1134 resp.ypfirst_resp_status = YP_TRUE;
1135 } else {
1136 resp.ypfirst_resp_status = (unsigned)YP_BADDB;
1138 } else {
1139 resp.ypfirst_resp_status = (unsigned)YP_NOKEY;
1143 resp.yp_resptype = YPFIRST_RESPTYPE;
1145 if (!svc_sendreply(transp,
1146 (xdrproc_t)_xdr_ypresponse,
1147 (caddr_t)&resp)) {
1148 RESPOND_ERR;
1151 if (!svc_freeargs(transp,
1152 (xdrproc_t)_xdr_yprequest,
1153 (caddr_t)&req)) {
1154 FREE_ERR;
1158 void
1159 ypoldnext(SVCXPRT *transp)
1161 bool dbmop_ok = TRUE;
1162 struct yprequest req;
1163 struct ypresponse resp;
1164 char *fun = "ypoldnext";
1165 DBM *fdb;
1167 memset(&req, 0, sizeof (req));
1168 memset(&resp, 0, sizeof (resp));
1170 if (!svc_getargs(transp,
1171 (xdrproc_t)_xdr_yprequest,
1172 (caddr_t)&req)) {
1173 svcerr_decode(transp);
1174 return;
1177 if (req.yp_reqtype != YPNEXT_REQTYPE) {
1178 resp.ypnext_resp_status = (unsigned)YP_BADARGS;
1179 dbmop_ok = FALSE;
1182 if (dbmop_ok &&
1183 ((fdb = ypset_current_map(req.ypnext_req_map,
1184 req.ypnext_req_domain,
1185 &resp.ypnext_resp_status)) != NULL &&
1186 yp_map_access(transp, &resp.ypnext_resp_status, fdb))) {
1188 resp.ypnext_resp_keydat = dbm_nextkey(fdb);
1190 if (resp.ypnext_resp_keyptr != NULL) {
1191 resp.ypnext_resp_valdat =
1192 dbm_fetch(fdb, resp.ypnext_resp_keydat);
1194 if (resp.ypnext_resp_valptr != NULL) {
1195 resp.ypnext_resp_status = YP_TRUE;
1196 } else {
1197 resp.ypnext_resp_status = (unsigned)YP_BADDB;
1199 } else {
1200 resp.ypnext_resp_status = (unsigned)YP_NOMORE;
1204 resp.yp_resptype = YPNEXT_RESPTYPE;
1206 if (!svc_sendreply(transp,
1207 (xdrproc_t)_xdr_ypresponse,
1208 (caddr_t)&resp)) {
1209 RESPOND_ERR;
1212 if (!svc_freeargs(transp,
1213 (xdrproc_t)_xdr_yprequest,
1214 (caddr_t)&req)) {
1215 FREE_ERR;
1220 * This retrieves the order number and master peer name from the map.
1221 * The conditions for the various message fields are: domain is filled
1222 * in iff the domain exists. map is filled in iff the map exists.
1223 * order number is filled in iff it's in the map. owner is filled in
1224 * iff the master peer is in the map.
1226 void
1227 ypoldpoll(SVCXPRT *transp)
1229 struct yprequest req;
1230 struct ypresponse resp;
1231 char *map = "";
1232 char *domain = "";
1233 char *owner = "";
1234 uint_t error;
1235 char *fun = "ypoldpoll";
1236 DBM *fdb;
1238 memset(&req, 0, sizeof (req));
1239 memset(&resp, 0, sizeof (resp));
1241 if (!svc_getargs(transp,
1242 (xdrproc_t)_xdr_yprequest,
1243 (caddr_t)&req)) {
1244 svcerr_decode(transp);
1245 return;
1248 if (req.yp_reqtype == YPPOLL_REQTYPE) {
1249 if (strcmp(req.yppoll_req_domain, "yp_private") == 0 ||
1250 strcmp(req.yppoll_req_map, "ypdomains") == 0 ||
1251 strcmp(req.yppoll_req_map, "ypmaps") == 0) {
1254 * Backward comatibility for 2.0 NIS servers
1256 domain = req.yppoll_req_domain;
1257 map = req.yppoll_req_map;
1258 } else if ((fdb = ypset_current_map(req.yppoll_req_map,
1259 req.yppoll_req_domain,
1260 &error)) != NULL) {
1261 domain = req.yppoll_req_domain;
1262 map = req.yppoll_req_map;
1263 ypget_map_order(map, domain,
1264 &resp.yppoll_resp_ordernum);
1265 ypget_map_master(&owner, fdb);
1266 } else {
1267 switch ((int)error) {
1268 case YP_BADDB:
1269 map = req.yppoll_req_map;
1270 /* Fall through to set the domain too. */
1272 case YP_NOMAP:
1273 domain = req.yppoll_req_domain;
1274 break;
1279 resp.yp_resptype = YPPOLL_RESPTYPE;
1280 resp.yppoll_resp_domain = domain;
1281 resp.yppoll_resp_map = map;
1282 resp.yppoll_resp_owner = owner;
1284 if (!svc_sendreply(transp,
1285 (xdrproc_t)_xdr_ypresponse,
1286 (caddr_t)&resp)) {
1287 RESPOND_ERR;
1290 if (!svc_freeargs(transp,
1291 (xdrproc_t)_xdr_yprequest,
1292 (caddr_t)&req)) {
1293 FREE_ERR;
1297 void
1298 ypoldpush(SVCXPRT *transp)
1300 struct yprequest req;
1301 struct ypresp_val resp;
1302 pid_t pid = -1;
1303 char *fun = "ypoldpush";
1304 DBM *fdb;
1306 memset(&req, 0, sizeof (req));
1308 if (!svc_getargs(transp,
1309 (xdrproc_t)_xdr_yprequest,
1310 (caddr_t)&req)) {
1311 svcerr_decode(transp);
1312 return;
1315 if (((fdb = ypset_current_map(req.yppush_req_map,
1316 req.yppush_req_domain,
1317 &resp.status)) != NULL) &&
1318 (yp_map_access(transp, &resp.status, fdb))) {
1320 pid = vfork();
1323 if (pid == -1) {
1324 FORK_ERR;
1325 } else if (pid == 0) {
1326 ypclr_current_map();
1328 if (execl(yppush_proc, "yppush", "-d", req.yppush_req_domain,
1329 req.yppush_req_map, NULL)) {
1330 EXEC_ERR;
1332 _exit(1);
1335 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL)) {
1336 RESPOND_ERR;
1339 if (!svc_freeargs(transp,
1340 (xdrproc_t)_xdr_yprequest,
1341 (caddr_t)&req)) {
1342 FREE_ERR;
1346 void
1347 ypoldpull(SVCXPRT *transp)
1349 struct yprequest req;
1350 struct ypresp_val resp;
1351 pid_t pid = -1;
1352 char *fun = "ypoldpull";
1353 DBM *fdb;
1355 memset(&req, 0, sizeof (req));
1357 if (!svc_getargs(transp,
1358 (xdrproc_t)_xdr_yprequest,
1359 (caddr_t)&req)) {
1360 svcerr_decode(transp);
1361 return;
1364 if (req.yp_reqtype == YPPULL_REQTYPE) {
1366 if (((fdb = ypset_current_map(req.yppull_req_map,
1367 req.yppull_req_domain,
1368 &resp.status)) == NULL) ||
1369 (yp_map_access(transp, &resp.status, fdb))) {
1370 pid = vfork();
1373 if (pid == -1) {
1374 FORK_ERR;
1375 } else if (pid == 0) {
1376 ypclr_current_map();
1378 if (execl(ypxfr_proc, "ypxfr", "-d",
1379 req.yppull_req_domain,
1380 req.yppull_req_map, NULL)) {
1381 EXEC_ERR;
1383 _exit(1);
1387 if (!svc_freeargs(transp,
1388 (xdrproc_t)_xdr_yprequest,
1389 (caddr_t)&req)) {
1390 FREE_ERR;
1394 void
1395 ypoldget(SVCXPRT *transp)
1397 struct yprequest req;
1398 struct ypresp_val resp;
1399 pid_t pid = -1;
1400 char *fun = "ypoldget";
1401 DBM *fdb;
1403 memset(&req, 0, sizeof (req));
1405 if (!svc_getargs(transp,
1406 (xdrproc_t)_xdr_yprequest,
1407 (caddr_t)&req)) {
1408 svcerr_decode(transp);
1409 return;
1412 if (!svc_sendreply(transp, xdr_void, 0)) {
1413 RESPOND_ERR;
1416 if (req.yp_reqtype == YPGET_REQTYPE) {
1418 if (((fdb = ypset_current_map(req.ypget_req_map,
1419 req.ypget_req_domain,
1420 &resp.status)) == NULL) ||
1421 (yp_map_access(transp, &resp.status, fdb))) {
1423 pid = vfork();
1426 if (pid == -1) {
1427 FORK_ERR;
1428 } else if (pid == 0) {
1430 ypclr_current_map();
1432 if (execl(ypxfr_proc, "ypxfr", "-d",
1433 req.ypget_req_domain, "-h",
1434 req.ypget_req_owner,
1435 req.ypget_req_map, NULL)) {
1437 EXEC_ERR;
1439 _exit(1);
1443 if (!svc_freeargs(transp,
1444 (xdrproc_t)_xdr_yprequest,
1445 (caddr_t)&req)) {
1446 RESPOND_ERR;
1450 static int
1451 omultihomed(struct yprequest req,
1452 struct ypresponse *resp, SVCXPRT *xprt, DBM *fdb)
1454 char *cp, *bp;
1455 char name[PATH_MAX];
1456 struct netbuf *nbuf;
1457 ulong_t bestaddr, call_addr;
1459 if (strcmp(req.ypmatch_req_map, "hosts.byname"))
1460 return (0);
1462 if (strncmp(req.ypmatch_req_keyptr, "YP_MULTI_", 9)) {
1463 datum tmpname;
1465 strncpy(name, "YP_MULTI_", 9);
1466 strncpy(name + 9, req.ypmatch_req_keyptr,
1467 req.ypmatch_req_keysize);
1468 tmpname.dsize = req.ypmatch_req_keysize + 9;
1469 tmpname.dptr = name;
1470 resp->ypmatch_resp_valdat = dbm_fetch(fdb, tmpname);
1471 } else {
1472 resp->ypmatch_resp_valdat =
1473 dbm_fetch(fdb, req.ypmatch_req_keydat);
1474 if (resp->ypmatch_resp_valptr != NULL)
1475 return (1);
1478 if (resp->ypmatch_resp_valptr == NULL)
1479 return (0);
1481 strncpy(name, req.ypmatch_req_keyptr, req.ypmatch_req_keysize);
1482 name[req.ypmatch_req_keysize] = 0;
1484 nbuf = svc_getrpccaller(xprt);
1487 * OK, now I have a netbuf structure which I'm supposed to treat
1488 * as opaque... I hate transport independance! So, we're just
1489 * gonna doit wrong... By wrong I mean that we assume that the
1490 * buf part of the netbuf structure is going to be a sockaddr_in.
1491 * We'll then check the assumed family member and hope that we
1492 * find AF_INET in there... if not then we can't continue.
1494 if (((struct sockaddr_in *)(nbuf->buf))->sin_family != AF_INET)
1495 return (0);
1497 call_addr = ((struct sockaddr_in *)(nbuf->buf))->sin_addr.s_addr;
1499 cp = resp->ypmatch_resp_valptr;
1500 if ((bp = strtok(cp, "\t")) == NULL) /* No address field */
1501 return (0);
1502 if ((cp = strtok(NULL, "")) == NULL) /* No host field */
1503 return (0);
1504 bp = strtok(bp, ",");
1506 bestaddr = inet_addr(bp);
1507 while (cp = strtok(NULL, ",")) {
1508 ulong_t taddr;
1510 taddr = inet_addr(cp);
1511 if (ntohl(call_addr ^ taddr) < ntohl(call_addr ^ bestaddr))
1512 bestaddr = taddr;
1515 cp = resp->ypmatch_resp_valptr;
1516 sprintf(cp, "%s %s", inet_ntoa(*(struct in_addr *)&bestaddr), name);
1517 resp->ypmatch_resp_valsize = strlen(cp);
1519 resp->ypmatch_resp_status = YP_TRUE;
1521 return (1);