Unleashed v1.4
[unleashed.git] / usr / src / cmd / ypcmd / ypserv_proc.c
blobfbbc7b3f4928e704043ddc4cc5f69cdc7eebb305
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 char *str;
218 /* Check with the DBM database */
219 resp.valdat = dbm_fetch(fdb, req.keydat);
220 if (resp.valdat.dptr != NULL) {
221 resp.status = YP_TRUE;
222 if (!silent)
223 printf("%s: dbm: %40.40s\n",
224 fun, resp.valdat.dptr);
225 goto send_reply;
229 * If we're being asked to match YP_SECURE or YP_INTERDOMAIN
230 * and we haven't found it in the dbm file, then we don't
231 * really want to waste any more time. Specifically, we don't
232 * want to ask DNS
234 str = req.keydat.dptr;
235 if (req.keydat.dsize == 0 ||
236 str == NULL ||
237 str[0] == '\0' ||
238 strncmp(str, yp_secure, req.keydat.dsize) == 0 ||
239 strncmp(str, yp_interdomain, 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, *str;
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 str = resp->valdat.dptr;
933 if (resp->valdat.dsize &&
934 str[resp->valdat.dsize-1] == '\n') {
935 str[resp->valdat.dsize-1] = '\0';
936 resp->valdat.dsize -= 1;
939 resp->status = YP_TRUE;
940 return (1);
942 nbuf = svc_getrpccaller(xprt);
944 * OK, now I have a netbuf structure which I'm supposed to
945 * treat as opaque... I hate transport independance!
946 * So, we're just gonna doit wrong... By wrong I mean that
947 * we assume that the buf part of the netbuf structure is going
948 * to be a sockaddr_in. We'll then check the assumed family
949 * member and hope that we find AF_INET in there... if not
950 * then we can't continue.
952 if (((struct sockaddr_in *)(nbuf->buf))->sin_family != AF_INET)
953 return (0);
955 call_addr = ((struct sockaddr_in *)(nbuf->buf))->sin_addr.s_addr;
957 cp = resp->valdat.dptr;
958 if ((bp = strtok(cp, " \t")) == NULL) /* no address field */
959 return (0);
960 if ((cp = strtok(NULL, "")) == NULL) /* no host field */
961 return (0);
962 bp = strtok(bp, ",");
964 bestaddr = inet_addr(bp);
965 while (cp = strtok(NULL, ",")) {
966 ulong_t taddr;
968 taddr = inet_addr(cp);
969 if (ntohl(call_addr ^ taddr) < ntohl(call_addr ^ bestaddr))
970 bestaddr = taddr;
972 cp = resp->valdat.dptr;
973 sprintf(cp, "%s %s", inet_ntoa(*(struct in_addr *)&bestaddr), name);
974 resp->valdat.dsize = strlen(cp);
976 resp->status = YP_TRUE;
978 return (1);
981 /* V1 dispatch routines */
982 void
983 ypoldmatch(SVCXPRT *transp, struct svc_req *rqstp)
985 bool dbmop_ok = TRUE;
986 struct yprequest req;
987 struct ypreq_key nrq;
988 struct ypresponse resp;
989 char *fun = "ypoldmatch";
990 DBM *fdb;
992 memset(&req, 0, sizeof (req));
993 memset(&resp, 0, sizeof (resp));
995 if (!svc_getargs(transp,
996 (xdrproc_t)_xdr_yprequest,
997 (caddr_t)&req)) {
998 svcerr_decode(transp);
999 return;
1002 if (req.yp_reqtype != YPMATCH_REQTYPE) {
1003 resp.ypmatch_resp_status = (unsigned)YP_BADARGS;
1004 dbmop_ok = FALSE;
1007 if (dbmop_ok &&
1008 (((fdb = ypset_current_map(req.ypmatch_req_map,
1009 req.ypmatch_req_domain,
1010 &resp.ypmatch_resp_status))
1011 != NULL) &&
1012 yp_map_access(transp,
1013 &resp.ypmatch_resp_status,
1014 fdb))) {
1016 char *str;
1017 /* Check with the DBM database */
1018 resp.ypmatch_resp_valdat = dbm_fetch(fdb,
1019 req.ypmatch_req_keydat);
1021 if (resp.ypmatch_resp_valptr != NULL) {
1022 resp.ypmatch_resp_status = YP_TRUE;
1023 if (!silent)
1024 printf("%s: dbm: %s\n",
1025 fun, resp.ypmatch_resp_valptr);
1026 goto send_oldreply;
1030 * If we're being asked to match YP_SECURE or YP_INTERDOMAIN
1031 * and we haven't found it in the dbm file, then we don't
1032 * really want to waste any more time. Specifically, we don't
1033 * want to ask DNS
1035 str = req.ypmatch_req_keyptr;
1036 if (req.ypmatch_req_keysize == 0 ||
1037 str == NULL ||
1038 str[0] == '\0' ||
1039 strncmp(str, "YP_SECURE", 9) == 0 ||
1040 strncmp(str, "YP_INTERDOMAIN", 14) == 0)
1042 goto send_oldreply;
1044 /* Let's try the YP_MULTI_ hack... */
1045 #ifdef MINUS_C_OPTION
1046 if (multiflag == TRUE && omultihomed(req, &resp, transp, fdb))
1047 goto send_oldreply;
1048 #else
1049 if (omultihomed(req, &resp, transp, fdb))
1050 goto send_oldreply;
1051 #endif
1053 /* Let's try DNS */
1054 if (!dnsforward) {
1055 USE_YP_INTERDOMAIN
1056 datum idkey, idval;
1057 idkey.dptr = yp_interdomain;
1058 idkey.dsize = yp_interdomain_sz;
1059 idval = dbm_fetch(fdb, idkey);
1060 if (idval.dptr)
1061 dnsforward = TRUE;
1064 if (dnsforward) {
1065 if (!resolv_pid)
1066 setup_resolv(&dnsforward, &resolv_pid,
1067 &resolv_client,
1068 resolv_tp, 0);
1070 if (req.yp_reqtype == YPREQ_KEY) {
1071 nrq = req.yp_reqbody.yp_req_keytype;
1073 resolv_req(&dnsforward, &resolv_client,
1074 &resolv_pid,
1075 resolv_tp, rqstp->rq_xprt,
1076 &nrq, nrq.map);
1078 return;
1082 send_oldreply:
1084 if (!svc_sendreply(transp,
1085 (xdrproc_t)_xdr_ypresponse,
1086 (caddr_t)&resp)) {
1087 RESPOND_ERR;
1090 if (!svc_freeargs(transp,
1091 (xdrproc_t)_xdr_yprequest,
1092 (char *)&req)) {
1093 FREE_ERR;
1097 void
1098 ypoldfirst(SVCXPRT *transp)
1100 bool dbmop_ok = TRUE;
1101 struct yprequest req;
1102 struct ypresponse resp;
1103 char *fun = "ypoldfirst";
1104 DBM *fdb;
1106 memset(&req, 0, sizeof (req));
1107 memset(&resp, 0, sizeof (resp));
1109 if (!svc_getargs(transp,
1110 (xdrproc_t)_xdr_yprequest,
1111 (caddr_t)&req)) {
1112 svcerr_decode(transp);
1113 return;
1116 if (req.yp_reqtype != YPFIRST_REQTYPE) {
1117 resp.ypfirst_resp_status = (unsigned)YP_BADARGS;
1118 dbmop_ok = FALSE;
1121 if (dbmop_ok &&
1122 ((fdb = ypset_current_map(req.ypfirst_req_map,
1123 req.ypfirst_req_domain,
1124 &resp.ypfirst_resp_status))
1125 != NULL) &&
1126 yp_map_access(transp,
1127 &resp.ypfirst_resp_status,
1128 fdb)) {
1130 resp.ypfirst_resp_keydat = dbm_firstkey(fdb);
1132 if (resp.ypfirst_resp_keyptr != NULL) {
1133 resp.ypfirst_resp_valdat =
1134 dbm_fetch(fdb, resp.ypfirst_resp_keydat);
1136 if (resp.ypfirst_resp_valptr != NULL) {
1137 resp.ypfirst_resp_status = YP_TRUE;
1138 } else {
1139 resp.ypfirst_resp_status = (unsigned)YP_BADDB;
1141 } else {
1142 resp.ypfirst_resp_status = (unsigned)YP_NOKEY;
1146 resp.yp_resptype = YPFIRST_RESPTYPE;
1148 if (!svc_sendreply(transp,
1149 (xdrproc_t)_xdr_ypresponse,
1150 (caddr_t)&resp)) {
1151 RESPOND_ERR;
1154 if (!svc_freeargs(transp,
1155 (xdrproc_t)_xdr_yprequest,
1156 (caddr_t)&req)) {
1157 FREE_ERR;
1161 void
1162 ypoldnext(SVCXPRT *transp)
1164 bool dbmop_ok = TRUE;
1165 struct yprequest req;
1166 struct ypresponse resp;
1167 char *fun = "ypoldnext";
1168 DBM *fdb;
1170 memset(&req, 0, sizeof (req));
1171 memset(&resp, 0, sizeof (resp));
1173 if (!svc_getargs(transp,
1174 (xdrproc_t)_xdr_yprequest,
1175 (caddr_t)&req)) {
1176 svcerr_decode(transp);
1177 return;
1180 if (req.yp_reqtype != YPNEXT_REQTYPE) {
1181 resp.ypnext_resp_status = (unsigned)YP_BADARGS;
1182 dbmop_ok = FALSE;
1185 if (dbmop_ok &&
1186 ((fdb = ypset_current_map(req.ypnext_req_map,
1187 req.ypnext_req_domain,
1188 &resp.ypnext_resp_status)) != NULL &&
1189 yp_map_access(transp, &resp.ypnext_resp_status, fdb))) {
1191 resp.ypnext_resp_keydat = dbm_nextkey(fdb);
1193 if (resp.ypnext_resp_keyptr != NULL) {
1194 resp.ypnext_resp_valdat =
1195 dbm_fetch(fdb, resp.ypnext_resp_keydat);
1197 if (resp.ypnext_resp_valptr != NULL) {
1198 resp.ypnext_resp_status = YP_TRUE;
1199 } else {
1200 resp.ypnext_resp_status = (unsigned)YP_BADDB;
1202 } else {
1203 resp.ypnext_resp_status = (unsigned)YP_NOMORE;
1207 resp.yp_resptype = YPNEXT_RESPTYPE;
1209 if (!svc_sendreply(transp,
1210 (xdrproc_t)_xdr_ypresponse,
1211 (caddr_t)&resp)) {
1212 RESPOND_ERR;
1215 if (!svc_freeargs(transp,
1216 (xdrproc_t)_xdr_yprequest,
1217 (caddr_t)&req)) {
1218 FREE_ERR;
1223 * This retrieves the order number and master peer name from the map.
1224 * The conditions for the various message fields are: domain is filled
1225 * in iff the domain exists. map is filled in iff the map exists.
1226 * order number is filled in iff it's in the map. owner is filled in
1227 * iff the master peer is in the map.
1229 void
1230 ypoldpoll(SVCXPRT *transp)
1232 struct yprequest req;
1233 struct ypresponse resp;
1234 char *map = "";
1235 char *domain = "";
1236 char *owner = "";
1237 uint_t error;
1238 char *fun = "ypoldpoll";
1239 DBM *fdb;
1241 memset(&req, 0, sizeof (req));
1242 memset(&resp, 0, sizeof (resp));
1244 if (!svc_getargs(transp,
1245 (xdrproc_t)_xdr_yprequest,
1246 (caddr_t)&req)) {
1247 svcerr_decode(transp);
1248 return;
1251 if (req.yp_reqtype == YPPOLL_REQTYPE) {
1252 if (strcmp(req.yppoll_req_domain, "yp_private") == 0 ||
1253 strcmp(req.yppoll_req_map, "ypdomains") == 0 ||
1254 strcmp(req.yppoll_req_map, "ypmaps") == 0) {
1257 * Backward comatibility for 2.0 NIS servers
1259 domain = req.yppoll_req_domain;
1260 map = req.yppoll_req_map;
1261 } else if ((fdb = ypset_current_map(req.yppoll_req_map,
1262 req.yppoll_req_domain,
1263 &error)) != NULL) {
1264 domain = req.yppoll_req_domain;
1265 map = req.yppoll_req_map;
1266 ypget_map_order(map, domain,
1267 &resp.yppoll_resp_ordernum);
1268 ypget_map_master(&owner, fdb);
1269 } else {
1270 switch ((int)error) {
1271 case YP_BADDB:
1272 map = req.yppoll_req_map;
1273 /* Fall through to set the domain too. */
1275 case YP_NOMAP:
1276 domain = req.yppoll_req_domain;
1277 break;
1282 resp.yp_resptype = YPPOLL_RESPTYPE;
1283 resp.yppoll_resp_domain = domain;
1284 resp.yppoll_resp_map = map;
1285 resp.yppoll_resp_owner = owner;
1287 if (!svc_sendreply(transp,
1288 (xdrproc_t)_xdr_ypresponse,
1289 (caddr_t)&resp)) {
1290 RESPOND_ERR;
1293 if (!svc_freeargs(transp,
1294 (xdrproc_t)_xdr_yprequest,
1295 (caddr_t)&req)) {
1296 FREE_ERR;
1300 void
1301 ypoldpush(SVCXPRT *transp)
1303 struct yprequest req;
1304 struct ypresp_val resp;
1305 pid_t pid = -1;
1306 char *fun = "ypoldpush";
1307 DBM *fdb;
1309 memset(&req, 0, sizeof (req));
1311 if (!svc_getargs(transp,
1312 (xdrproc_t)_xdr_yprequest,
1313 (caddr_t)&req)) {
1314 svcerr_decode(transp);
1315 return;
1318 if (((fdb = ypset_current_map(req.yppush_req_map,
1319 req.yppush_req_domain,
1320 &resp.status)) != NULL) &&
1321 (yp_map_access(transp, &resp.status, fdb))) {
1323 pid = vfork();
1326 if (pid == -1) {
1327 FORK_ERR;
1328 } else if (pid == 0) {
1329 ypclr_current_map();
1331 if (execl(yppush_proc, "yppush", "-d", req.yppush_req_domain,
1332 req.yppush_req_map, NULL)) {
1333 EXEC_ERR;
1335 _exit(1);
1338 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL)) {
1339 RESPOND_ERR;
1342 if (!svc_freeargs(transp,
1343 (xdrproc_t)_xdr_yprequest,
1344 (caddr_t)&req)) {
1345 FREE_ERR;
1349 void
1350 ypoldpull(SVCXPRT *transp)
1352 struct yprequest req;
1353 struct ypresp_val resp;
1354 pid_t pid = -1;
1355 char *fun = "ypoldpull";
1356 DBM *fdb;
1358 memset(&req, 0, sizeof (req));
1360 if (!svc_getargs(transp,
1361 (xdrproc_t)_xdr_yprequest,
1362 (caddr_t)&req)) {
1363 svcerr_decode(transp);
1364 return;
1367 if (req.yp_reqtype == YPPULL_REQTYPE) {
1369 if (((fdb = ypset_current_map(req.yppull_req_map,
1370 req.yppull_req_domain,
1371 &resp.status)) == NULL) ||
1372 (yp_map_access(transp, &resp.status, fdb))) {
1373 pid = vfork();
1376 if (pid == -1) {
1377 FORK_ERR;
1378 } else if (pid == 0) {
1379 ypclr_current_map();
1381 if (execl(ypxfr_proc, "ypxfr", "-d",
1382 req.yppull_req_domain,
1383 req.yppull_req_map, NULL)) {
1384 EXEC_ERR;
1386 _exit(1);
1390 if (!svc_freeargs(transp,
1391 (xdrproc_t)_xdr_yprequest,
1392 (caddr_t)&req)) {
1393 FREE_ERR;
1397 void
1398 ypoldget(SVCXPRT *transp)
1400 struct yprequest req;
1401 struct ypresp_val resp;
1402 pid_t pid = -1;
1403 char *fun = "ypoldget";
1404 DBM *fdb;
1406 memset(&req, 0, sizeof (req));
1408 if (!svc_getargs(transp,
1409 (xdrproc_t)_xdr_yprequest,
1410 (caddr_t)&req)) {
1411 svcerr_decode(transp);
1412 return;
1415 if (!svc_sendreply(transp, xdr_void, 0)) {
1416 RESPOND_ERR;
1419 if (req.yp_reqtype == YPGET_REQTYPE) {
1421 if (((fdb = ypset_current_map(req.ypget_req_map,
1422 req.ypget_req_domain,
1423 &resp.status)) == NULL) ||
1424 (yp_map_access(transp, &resp.status, fdb))) {
1426 pid = vfork();
1429 if (pid == -1) {
1430 FORK_ERR;
1431 } else if (pid == 0) {
1433 ypclr_current_map();
1435 if (execl(ypxfr_proc, "ypxfr", "-d",
1436 req.ypget_req_domain, "-h",
1437 req.ypget_req_owner,
1438 req.ypget_req_map, NULL)) {
1440 EXEC_ERR;
1442 _exit(1);
1446 if (!svc_freeargs(transp,
1447 (xdrproc_t)_xdr_yprequest,
1448 (caddr_t)&req)) {
1449 RESPOND_ERR;
1453 static int
1454 omultihomed(struct yprequest req,
1455 struct ypresponse *resp, SVCXPRT *xprt, DBM *fdb)
1457 char *cp, *bp;
1458 char name[PATH_MAX];
1459 struct netbuf *nbuf;
1460 ulong_t bestaddr, call_addr;
1462 if (strcmp(req.ypmatch_req_map, "hosts.byname"))
1463 return (0);
1465 if (strncmp(req.ypmatch_req_keyptr, "YP_MULTI_", 9)) {
1466 datum tmpname;
1468 strncpy(name, "YP_MULTI_", 9);
1469 strncpy(name + 9, req.ypmatch_req_keyptr,
1470 req.ypmatch_req_keysize);
1471 tmpname.dsize = req.ypmatch_req_keysize + 9;
1472 tmpname.dptr = name;
1473 resp->ypmatch_resp_valdat = dbm_fetch(fdb, tmpname);
1474 } else {
1475 resp->ypmatch_resp_valdat =
1476 dbm_fetch(fdb, req.ypmatch_req_keydat);
1477 if (resp->ypmatch_resp_valptr != NULL)
1478 return (1);
1481 if (resp->ypmatch_resp_valptr == NULL)
1482 return (0);
1484 strncpy(name, req.ypmatch_req_keyptr, req.ypmatch_req_keysize);
1485 name[req.ypmatch_req_keysize] = 0;
1487 nbuf = svc_getrpccaller(xprt);
1490 * OK, now I have a netbuf structure which I'm supposed to treat
1491 * as opaque... I hate transport independance! So, we're just
1492 * gonna doit wrong... By wrong I mean that we assume that the
1493 * buf part of the netbuf structure is going to be a sockaddr_in.
1494 * We'll then check the assumed family member and hope that we
1495 * find AF_INET in there... if not then we can't continue.
1497 if (((struct sockaddr_in *)(nbuf->buf))->sin_family != AF_INET)
1498 return (0);
1500 call_addr = ((struct sockaddr_in *)(nbuf->buf))->sin_addr.s_addr;
1502 cp = resp->ypmatch_resp_valptr;
1503 if ((bp = strtok(cp, "\t")) == NULL) /* No address field */
1504 return (0);
1505 if ((cp = strtok(NULL, "")) == NULL) /* No host field */
1506 return (0);
1507 bp = strtok(bp, ",");
1509 bestaddr = inet_addr(bp);
1510 while (cp = strtok(NULL, ",")) {
1511 ulong_t taddr;
1513 taddr = inet_addr(cp);
1514 if (ntohl(call_addr ^ taddr) < ntohl(call_addr ^ bestaddr))
1515 bestaddr = taddr;
1518 cp = resp->ypmatch_resp_valptr;
1519 sprintf(cp, "%s %s", inet_ntoa(*(struct in_addr *)&bestaddr), name);
1520 resp->ypmatch_resp_valsize = strlen(cp);
1522 resp->ypmatch_resp_status = YP_TRUE;
1524 return (1);