xref: /onnv-gate/usr/src/cmd/ypcmd/ypxfrd_client.c (revision 11313:5a24da420284)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*11313SMichen.Chang@Sun.COM  * Common Development and Distribution License (the "License").
6*11313SMichen.Chang@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11313SMichen.Chang@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23702Sth160488  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <stdio.h>
27*11313SMichen.Chang@Sun.COM #include <stdlib.h>
280Sstevel@tonic-gate #include <errno.h>
29*11313SMichen.Chang@Sun.COM #include <unistd.h>
300Sstevel@tonic-gate #include <netconfig.h>
310Sstevel@tonic-gate #include <netdir.h>
320Sstevel@tonic-gate #include <rpc/rpc.h>
330Sstevel@tonic-gate #include <sys/file.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include "ypxfrd.h"
360Sstevel@tonic-gate #include <ndbm.h>
370Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
380Sstevel@tonic-gate #include <rpcsvc/nis.h>
39*11313SMichen.Chang@Sun.COM #include <strings.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/isa_defs.h>	/* for ENDIAN defines */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
440Sstevel@tonic-gate #define	DOSWAB 1
450Sstevel@tonic-gate #endif
460Sstevel@tonic-gate 
470Sstevel@tonic-gate static struct timeval TIMEOUT = {25, 0};
480Sstevel@tonic-gate static	DBM	*db;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate extern bool secure_map;
51*11313SMichen.Chang@Sun.COM extern void logprintf(char *, ...);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /* delete the dbm file with name file */
54702Sth160488 static int
dbm_deletefile(file)550Sstevel@tonic-gate dbm_deletefile(file)
560Sstevel@tonic-gate char *file;
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	char	pag1[MAXPATHLEN];
590Sstevel@tonic-gate 	char	dir1[MAXPATHLEN];
600Sstevel@tonic-gate 	int err;
610Sstevel@tonic-gate 	strcpy(pag1, file);
620Sstevel@tonic-gate 	strcat(pag1, ".pag");
630Sstevel@tonic-gate 	strcpy(dir1, file);
640Sstevel@tonic-gate 	strcat(dir1, ".dir");
650Sstevel@tonic-gate 	err = 0;
660Sstevel@tonic-gate 	if (unlink(pag1) < 0) {
670Sstevel@tonic-gate 		perror("unlinkpag");
680Sstevel@tonic-gate 		err = -1;
690Sstevel@tonic-gate 	}
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if (unlink(dir1) < 0) {
720Sstevel@tonic-gate 		perror("unlinkdir");
730Sstevel@tonic-gate 		return (-1);
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 	return (err);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /* xdr just the .pag file of a dbm file */
790Sstevel@tonic-gate static	bool_t
xdr_pages(xdrs)80*11313SMichen.Chang@Sun.COM xdr_pages(xdrs)
810Sstevel@tonic-gate 	XDR	*xdrs;
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	static struct pag res;
840Sstevel@tonic-gate 	struct pag	*PAG;
850Sstevel@tonic-gate #ifdef DOSWAB
860Sstevel@tonic-gate 	short	*s;
870Sstevel@tonic-gate 	int		i;
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate 	bool_t	more;
900Sstevel@tonic-gate 	bool_t	goteof;
91*11313SMichen.Chang@Sun.COM 	off64_t	where;
92*11313SMichen.Chang@Sun.COM 	int	true = 1;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	goteof = FALSE;
950Sstevel@tonic-gate 	if (!xdr_pag(xdrs, &res))
960Sstevel@tonic-gate 		return (FALSE);
970Sstevel@tonic-gate 	PAG = &res;
98*11313SMichen.Chang@Sun.COM 	while (true) {
990Sstevel@tonic-gate 		if (PAG->status == OK) {
1000Sstevel@tonic-gate #ifdef DOSWAB
1010Sstevel@tonic-gate 		s = (short *)PAG->pag_u.ok.blkdat;
1020Sstevel@tonic-gate 		s[0] = ntohs(s[0]);
1030Sstevel@tonic-gate 		for (i = 1; i <= s[0]; i++)
1040Sstevel@tonic-gate 			s[i] = ntohs(s[i]);
1050Sstevel@tonic-gate #endif
1060Sstevel@tonic-gate 			errno = 0;
107*11313SMichen.Chang@Sun.COM 			where = (((off64_t)PAG->pag_u.ok.blkno) * PBLKSIZ);
108*11313SMichen.Chang@Sun.COM 			(void) lseek64(db->dbm_pagf, where, L_SET);
1090Sstevel@tonic-gate 			if (errno != 0) {
1100Sstevel@tonic-gate 				perror("seek");
1110Sstevel@tonic-gate 				exit(-1);
1120Sstevel@tonic-gate 			}
1130Sstevel@tonic-gate 			if (write(db->dbm_pagf,
1140Sstevel@tonic-gate 				PAG->pag_u.ok.blkdat, PBLKSIZ) < 0) {
1150Sstevel@tonic-gate 				perror("write");
1160Sstevel@tonic-gate 				exit(-1);
1170Sstevel@tonic-gate 			}
1180Sstevel@tonic-gate 		} else if (PAG->status == GETDBM_ERROR) {
119*11313SMichen.Chang@Sun.COM 			(void) printf("clnt call getpag GETDBM_ERROR\n");
1200Sstevel@tonic-gate 			exit(-1);
1210Sstevel@tonic-gate 		} else if (PAG->status == GETDBM_EOF)
1220Sstevel@tonic-gate 			goteof = TRUE;
1230Sstevel@tonic-gate 		if (!xdr_bool(xdrs, &more))
1240Sstevel@tonic-gate 			return (FALSE);
1250Sstevel@tonic-gate 		if (more == FALSE)
1260Sstevel@tonic-gate 			return (goteof);
1270Sstevel@tonic-gate 		if (!xdr_pag(xdrs, &res))
1280Sstevel@tonic-gate 			return (FALSE);
1290Sstevel@tonic-gate 	}
130*11313SMichen.Chang@Sun.COM 	/*NOTREACHED*/
131*11313SMichen.Chang@Sun.COM 	return (TRUE);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate /* xdr  just the .dir part of a dbm file */
1340Sstevel@tonic-gate static	bool_t
xdr_dirs(xdrs)135*11313SMichen.Chang@Sun.COM xdr_dirs(xdrs)
1360Sstevel@tonic-gate 	XDR	*xdrs;
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	static	struct dir res;
1390Sstevel@tonic-gate 	struct	dir	*DIR;
1400Sstevel@tonic-gate 	bool_t	more;
1410Sstevel@tonic-gate 	bool_t	goteof;
142*11313SMichen.Chang@Sun.COM 	off64_t	where;
143*11313SMichen.Chang@Sun.COM 	int	true = 1;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	goteof = FALSE;
1460Sstevel@tonic-gate 	if (!xdr_dir(xdrs, &res))
1470Sstevel@tonic-gate 		return (FALSE);
1480Sstevel@tonic-gate 	DIR = &res;
149*11313SMichen.Chang@Sun.COM 	while (true) {
1500Sstevel@tonic-gate 		if (DIR->status == OK) {
1510Sstevel@tonic-gate 			errno = 0;
152*11313SMichen.Chang@Sun.COM 			where = (((off64_t)DIR->dir_u.ok.blkno) * DBLKSIZ);
153*11313SMichen.Chang@Sun.COM 			(void) lseek64(db->dbm_dirf, where, L_SET);
1540Sstevel@tonic-gate 			if (errno != 0) {
1550Sstevel@tonic-gate 				perror("seek");
1560Sstevel@tonic-gate 				exit(-1);
1570Sstevel@tonic-gate 			}
1580Sstevel@tonic-gate 			if (write(db->dbm_dirf,
1590Sstevel@tonic-gate 				DIR->dir_u.ok.blkdat, DBLKSIZ) < 0) {
1600Sstevel@tonic-gate 				perror("write");
1610Sstevel@tonic-gate 				exit(-1);
1620Sstevel@tonic-gate 			}
1630Sstevel@tonic-gate 		} else if (DIR->status == GETDBM_ERROR) {
164*11313SMichen.Chang@Sun.COM 			(void) printf("clnt call getdir GETDBM_ERROR\n");
1650Sstevel@tonic-gate 			exit(-1);
1660Sstevel@tonic-gate 		} else if (DIR->status == GETDBM_EOF)
1670Sstevel@tonic-gate 			goteof = TRUE;
1680Sstevel@tonic-gate 		if (!xdr_bool(xdrs, &more))
1690Sstevel@tonic-gate 			return (FALSE);
1700Sstevel@tonic-gate 		if (more == FALSE)
1710Sstevel@tonic-gate 			return (goteof);
1720Sstevel@tonic-gate 		if (!xdr_dir(xdrs, &res))
1730Sstevel@tonic-gate 			return (FALSE);
1740Sstevel@tonic-gate 	}
175*11313SMichen.Chang@Sun.COM 	/*NOTREACHED*/
176*11313SMichen.Chang@Sun.COM 	return (TRUE);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate  * xdr a dbm file from ypxfrd
1810Sstevel@tonic-gate  * note that if the client or server do not support ndbm
1820Sstevel@tonic-gate  * we may not use this optional protocol
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate 
185702Sth160488 int
xdr_myfyl(xdrs,objp)1860Sstevel@tonic-gate xdr_myfyl(xdrs, objp)
1870Sstevel@tonic-gate 	XDR *xdrs;
1880Sstevel@tonic-gate 	int *objp;
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	if (!xdr_answer(xdrs, (answer *)objp))
1910Sstevel@tonic-gate 		return (FALSE);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	if (*objp != OK)
1940Sstevel@tonic-gate 		return (TRUE);
1950Sstevel@tonic-gate 
196*11313SMichen.Chang@Sun.COM 	if (!xdr_pages(xdrs))
1970Sstevel@tonic-gate 		return (FALSE);
1980Sstevel@tonic-gate 
199*11313SMichen.Chang@Sun.COM 	if (!xdr_dirs(xdrs))
2000Sstevel@tonic-gate 		return (FALSE);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	return (TRUE);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
205702Sth160488 int
ypxfrd_getdbm(tempmap,master,domain,map)2060Sstevel@tonic-gate ypxfrd_getdbm(tempmap, master, domain, map)
2070Sstevel@tonic-gate 	char *tempmap;
2080Sstevel@tonic-gate 	char *master;
2090Sstevel@tonic-gate 	char *domain;
2100Sstevel@tonic-gate 	char *map;
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	hosereq	rmap;
2130Sstevel@tonic-gate 	CLIENT	*clnt;
2140Sstevel@tonic-gate 	int		res;
2150Sstevel@tonic-gate 	int	recvsiz = 24 * 1024;
2160Sstevel@tonic-gate 	struct netconfig *nconf;
2170Sstevel@tonic-gate 	int fd;
2180Sstevel@tonic-gate 	struct netbuf *svcaddr;
2190Sstevel@tonic-gate 	struct t_bind *tbind;
2200Sstevel@tonic-gate 	char *netid[] = { "tcp6", "tcp" };
2210Sstevel@tonic-gate 	int i, lastnetid = (sizeof (netid)/sizeof (netid[0])) - 1;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	for (i = 0; i <= lastnetid; i++) {
2240Sstevel@tonic-gate 		if ((nconf = getnetconfigent(netid[i])) == NULL) {
2250Sstevel@tonic-gate 			if (i != lastnetid)
2260Sstevel@tonic-gate 				continue;
2270Sstevel@tonic-gate 			logprintf("ypxfr: tcp transport not supported\n");
2280Sstevel@tonic-gate 			return (-1);
2290Sstevel@tonic-gate 		}
2300Sstevel@tonic-gate 		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
2310Sstevel@tonic-gate 			freenetconfigent(nconf);
2320Sstevel@tonic-gate 			if (i != lastnetid)
2330Sstevel@tonic-gate 				continue;
2340Sstevel@tonic-gate 			logprintf("ypxfr: TLI problems\n");
2350Sstevel@tonic-gate 			return (-1);
2360Sstevel@tonic-gate 		}
2370Sstevel@tonic-gate 		if (secure_map == TRUE) {
2380Sstevel@tonic-gate 			if (netdir_options(nconf, ND_SET_RESERVEDPORT, fd,
2390Sstevel@tonic-gate 					NULL) == -1) {
2400Sstevel@tonic-gate 				(void) close(fd);
2410Sstevel@tonic-gate 				freenetconfigent(nconf);
2420Sstevel@tonic-gate 				if (i != lastnetid)
2430Sstevel@tonic-gate 					continue;
2440Sstevel@tonic-gate 				logprintf(
2450Sstevel@tonic-gate 			"ypxfr: cannot bind to reserved port for %s\n%s\n",
246*11313SMichen.Chang@Sun.COM 					netid[i], netdir_sperror());
2470Sstevel@tonic-gate 				return (-1);
2480Sstevel@tonic-gate 			}
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 
251*11313SMichen.Chang@Sun.COM 		/* LINTED pointer alignment */
2520Sstevel@tonic-gate 		if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) ==
2530Sstevel@tonic-gate 			NULL) {
2540Sstevel@tonic-gate 			(void) close(fd);
2550Sstevel@tonic-gate 			freenetconfigent(nconf);
2560Sstevel@tonic-gate 			if (i != lastnetid)
2570Sstevel@tonic-gate 				continue;
2580Sstevel@tonic-gate 			logprintf("ypxfr: TLI problems\n");
2590Sstevel@tonic-gate 			return (-1);
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 		svcaddr = &(tbind->addr);
2620Sstevel@tonic-gate 		if (rpcb_getaddr(YPXFRD, 1, nconf, svcaddr, master)
2630Sstevel@tonic-gate 			== FALSE) {
2640Sstevel@tonic-gate 			(void) t_free((char *)tbind, T_BIND);
2650Sstevel@tonic-gate 			(void) close(fd);
2660Sstevel@tonic-gate 			freenetconfigent(nconf);
2670Sstevel@tonic-gate 			if (i != lastnetid)
2680Sstevel@tonic-gate 				continue;
2690Sstevel@tonic-gate 			logprintf("ypxfr: couldnot get %s address\n", master);
2700Sstevel@tonic-gate 			return (-1);
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 		if ((clnt = __nis_clnt_create(fd, nconf, 0, svcaddr, 0,
2730Sstevel@tonic-gate 						YPXFRD, 1, recvsiz, 0)) == 0) {
2740Sstevel@tonic-gate 			(void) t_free((char *)tbind, T_BIND);
2750Sstevel@tonic-gate 			(void) close(fd);
2760Sstevel@tonic-gate 			freenetconfigent(nconf);
2770Sstevel@tonic-gate 			if (i != lastnetid)
2780Sstevel@tonic-gate 				continue;
2790Sstevel@tonic-gate 			clnt_pcreateerror(
2800Sstevel@tonic-gate 				"ypxfr (get_map) - TCP channel create failure");
2810Sstevel@tonic-gate 			return (-1);
2820Sstevel@tonic-gate 		}
2830Sstevel@tonic-gate 		(void) t_free((char *)tbind, T_BIND);
2840Sstevel@tonic-gate 		break;
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 	(void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, (char *)NULL);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	rmap.map = map;
2890Sstevel@tonic-gate 	rmap.domain = domain;
290*11313SMichen.Chang@Sun.COM 	(void) memset((char *)&res, 0, sizeof (res));
2910Sstevel@tonic-gate 	db = dbm_open(tempmap, O_RDWR + O_CREAT + O_TRUNC, 0777);
2920Sstevel@tonic-gate 	if (db == NULL) {
2930Sstevel@tonic-gate 		logprintf("dbm_open failed %s\n", tempmap);
2940Sstevel@tonic-gate 		perror(tempmap);
2950Sstevel@tonic-gate 		return (-2);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	if (clnt_call(clnt, getdbm, xdr_hosereq, (char *)&rmap, xdr_myfyl,
2990Sstevel@tonic-gate 		(char *)&res, TIMEOUT) != RPC_SUCCESS) {
3000Sstevel@tonic-gate 		logprintf("clnt call to ypxfrd getdbm failed.\n");
3010Sstevel@tonic-gate 		clnt_perror(clnt, "getdbm");
302*11313SMichen.Chang@Sun.COM 		(void) dbm_deletefile(tempmap);
3030Sstevel@tonic-gate 		return (-3);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	if (res != OK) {
3060Sstevel@tonic-gate 		logprintf("clnt call %s ypxfrd getdbm NOTOK %s %s code=%d\n",
3070Sstevel@tonic-gate 			master, domain, map, res);
308*11313SMichen.Chang@Sun.COM 		(void) dbm_deletefile(tempmap);
3090Sstevel@tonic-gate 		return (-4);
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 	return (0);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate }
314