xref: /onnv-gate/usr/src/cmd/ypcmd/ypxfrd_client.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1986-1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <errno.h>
31*0Sstevel@tonic-gate #include <netconfig.h>
32*0Sstevel@tonic-gate #include <netdir.h>
33*0Sstevel@tonic-gate #include <rpc/rpc.h>
34*0Sstevel@tonic-gate #include <sys/file.h>
35*0Sstevel@tonic-gate #include <sys/param.h>
36*0Sstevel@tonic-gate #include "ypxfrd.h"
37*0Sstevel@tonic-gate #include <ndbm.h>
38*0Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
39*0Sstevel@tonic-gate #include <rpcsvc/nis.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <sys/isa_defs.h>	/* for ENDIAN defines */
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
44*0Sstevel@tonic-gate #define	DOSWAB 1
45*0Sstevel@tonic-gate #endif
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static struct timeval TIMEOUT = {25, 0};
48*0Sstevel@tonic-gate static	DBM	*db;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate extern bool secure_map;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /* delete the dbm file with name file */
53*0Sstevel@tonic-gate static
54*0Sstevel@tonic-gate dbm_deletefile(file)
55*0Sstevel@tonic-gate char *file;
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate 	char	pag1[MAXPATHLEN];
58*0Sstevel@tonic-gate 	char	dir1[MAXPATHLEN];
59*0Sstevel@tonic-gate 	int err;
60*0Sstevel@tonic-gate 	strcpy(pag1, file);
61*0Sstevel@tonic-gate 	strcat(pag1, ".pag");
62*0Sstevel@tonic-gate 	strcpy(dir1, file);
63*0Sstevel@tonic-gate 	strcat(dir1, ".dir");
64*0Sstevel@tonic-gate 	err = 0;
65*0Sstevel@tonic-gate 	if (unlink(pag1) < 0) {
66*0Sstevel@tonic-gate 		perror("unlinkpag");
67*0Sstevel@tonic-gate 		err = -1;
68*0Sstevel@tonic-gate 	}
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	if (unlink(dir1) < 0) {
71*0Sstevel@tonic-gate 		perror("unlinkdir");
72*0Sstevel@tonic-gate 		return (-1);
73*0Sstevel@tonic-gate 	}
74*0Sstevel@tonic-gate 	return (err);
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate /* xdr just the .pag file of a dbm file */
78*0Sstevel@tonic-gate static	bool_t
79*0Sstevel@tonic-gate xdr_pages(xdrs, objp)
80*0Sstevel@tonic-gate 	XDR	*xdrs;
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	static struct pag res;
83*0Sstevel@tonic-gate 	struct pag	*PAG;
84*0Sstevel@tonic-gate #ifdef DOSWAB
85*0Sstevel@tonic-gate 	short	*s;
86*0Sstevel@tonic-gate 	int		i;
87*0Sstevel@tonic-gate #endif
88*0Sstevel@tonic-gate 	bool_t	more;
89*0Sstevel@tonic-gate 	bool_t	goteof;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	goteof = FALSE;
92*0Sstevel@tonic-gate 	if (!xdr_pag(xdrs, &res))
93*0Sstevel@tonic-gate 		return (FALSE);
94*0Sstevel@tonic-gate 	PAG = &res;
95*0Sstevel@tonic-gate 	while (1) {
96*0Sstevel@tonic-gate 		if (PAG->status == OK) {
97*0Sstevel@tonic-gate #ifdef DOSWAB
98*0Sstevel@tonic-gate 		s = (short *)PAG->pag_u.ok.blkdat;
99*0Sstevel@tonic-gate 		s[0] = ntohs(s[0]);
100*0Sstevel@tonic-gate 		for (i = 1; i <= s[0]; i++)
101*0Sstevel@tonic-gate 			s[i] = ntohs(s[i]);
102*0Sstevel@tonic-gate #endif
103*0Sstevel@tonic-gate 			errno = 0;
104*0Sstevel@tonic-gate 			lseek(db->dbm_pagf,
105*0Sstevel@tonic-gate 				PAG->pag_u.ok.blkno * PBLKSIZ, L_SET);
106*0Sstevel@tonic-gate 			if (errno != 0) {
107*0Sstevel@tonic-gate 				perror("seek");
108*0Sstevel@tonic-gate 				exit(-1);
109*0Sstevel@tonic-gate 			}
110*0Sstevel@tonic-gate 			if (write(db->dbm_pagf,
111*0Sstevel@tonic-gate 				PAG->pag_u.ok.blkdat, PBLKSIZ) < 0) {
112*0Sstevel@tonic-gate 				perror("write");
113*0Sstevel@tonic-gate 				exit(-1);
114*0Sstevel@tonic-gate 			}
115*0Sstevel@tonic-gate 		} else if (PAG->status == GETDBM_ERROR) {
116*0Sstevel@tonic-gate 			printf("clnt call getpag GETDBM_ERROR\n");
117*0Sstevel@tonic-gate 			exit(-1);
118*0Sstevel@tonic-gate 		} else if (PAG->status == GETDBM_EOF)
119*0Sstevel@tonic-gate 			goteof = TRUE;
120*0Sstevel@tonic-gate 		if (!xdr_bool(xdrs, &more))
121*0Sstevel@tonic-gate 			return (FALSE);
122*0Sstevel@tonic-gate 		if (more == FALSE)
123*0Sstevel@tonic-gate 			return (goteof);
124*0Sstevel@tonic-gate 		if (!xdr_pag(xdrs, &res))
125*0Sstevel@tonic-gate 			return (FALSE);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate /* xdr  just the .dir part of a dbm file */
129*0Sstevel@tonic-gate static	bool_t
130*0Sstevel@tonic-gate xdr_dirs(xdrs, objp)
131*0Sstevel@tonic-gate 	XDR	*xdrs;
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate 	static	struct dir res;
134*0Sstevel@tonic-gate 	struct	dir	*DIR;
135*0Sstevel@tonic-gate 	bool_t	more;
136*0Sstevel@tonic-gate 	bool_t	goteof;
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	goteof = FALSE;
139*0Sstevel@tonic-gate 	if (!xdr_dir(xdrs, &res))
140*0Sstevel@tonic-gate 		return (FALSE);
141*0Sstevel@tonic-gate 	DIR = &res;
142*0Sstevel@tonic-gate 	while (1) {
143*0Sstevel@tonic-gate 		if (DIR->status == OK) {
144*0Sstevel@tonic-gate 			errno = 0;
145*0Sstevel@tonic-gate 			lseek(db->dbm_dirf,
146*0Sstevel@tonic-gate 				DIR->dir_u.ok.blkno * DBLKSIZ, L_SET);
147*0Sstevel@tonic-gate 			if (errno != 0) {
148*0Sstevel@tonic-gate 				perror("seek");
149*0Sstevel@tonic-gate 				exit(-1);
150*0Sstevel@tonic-gate 			}
151*0Sstevel@tonic-gate 			if (write(db->dbm_dirf,
152*0Sstevel@tonic-gate 				DIR->dir_u.ok.blkdat, DBLKSIZ) < 0) {
153*0Sstevel@tonic-gate 				perror("write");
154*0Sstevel@tonic-gate 				exit(-1);
155*0Sstevel@tonic-gate 			}
156*0Sstevel@tonic-gate 		} else if (DIR->status == GETDBM_ERROR) {
157*0Sstevel@tonic-gate 			printf("clnt call getdir GETDBM_ERROR\n");
158*0Sstevel@tonic-gate 			exit(-1);
159*0Sstevel@tonic-gate 		} else if (DIR->status == GETDBM_EOF)
160*0Sstevel@tonic-gate 			goteof = TRUE;
161*0Sstevel@tonic-gate 		if (!xdr_bool(xdrs, &more))
162*0Sstevel@tonic-gate 			return (FALSE);
163*0Sstevel@tonic-gate 		if (more == FALSE)
164*0Sstevel@tonic-gate 			return (goteof);
165*0Sstevel@tonic-gate 		if (!xdr_dir(xdrs, &res))
166*0Sstevel@tonic-gate 			return (FALSE);
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate /*
171*0Sstevel@tonic-gate  * xdr a dbm file from ypxfrd
172*0Sstevel@tonic-gate  * note that if the client or server do not support ndbm
173*0Sstevel@tonic-gate  * we may not use this optional protocol
174*0Sstevel@tonic-gate  */
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate xdr_myfyl(xdrs, objp)
177*0Sstevel@tonic-gate 	XDR *xdrs;
178*0Sstevel@tonic-gate 	int *objp;
179*0Sstevel@tonic-gate {
180*0Sstevel@tonic-gate 	if (!xdr_answer(xdrs, (answer *)objp))
181*0Sstevel@tonic-gate 		return (FALSE);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	if (*objp != OK)
184*0Sstevel@tonic-gate 		return (TRUE);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	if (!xdr_pages(xdrs, NULL))
187*0Sstevel@tonic-gate 		return (FALSE);
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	if (!xdr_dirs(xdrs, NULL))
190*0Sstevel@tonic-gate 		return (FALSE);
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	return (TRUE);
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate ypxfrd_getdbm(tempmap, master, domain, map)
196*0Sstevel@tonic-gate 	char *tempmap;
197*0Sstevel@tonic-gate 	char *master;
198*0Sstevel@tonic-gate 	char *domain;
199*0Sstevel@tonic-gate 	char *map;
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate 	hosereq	rmap;
202*0Sstevel@tonic-gate 	CLIENT	*clnt;
203*0Sstevel@tonic-gate 	int		res;
204*0Sstevel@tonic-gate 	int	recvsiz = 24 * 1024;
205*0Sstevel@tonic-gate 	struct netconfig *nconf;
206*0Sstevel@tonic-gate 	int fd;
207*0Sstevel@tonic-gate 	struct netbuf *svcaddr;
208*0Sstevel@tonic-gate 	struct t_bind *tbind;
209*0Sstevel@tonic-gate 	char *netid[] = { "tcp6", "tcp" };
210*0Sstevel@tonic-gate 	int i, lastnetid = (sizeof (netid)/sizeof (netid[0])) - 1;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	for (i = 0; i <= lastnetid; i++) {
213*0Sstevel@tonic-gate 		if ((nconf = getnetconfigent(netid[i])) == NULL) {
214*0Sstevel@tonic-gate 			if (i != lastnetid)
215*0Sstevel@tonic-gate 				continue;
216*0Sstevel@tonic-gate 			logprintf("ypxfr: tcp transport not supported\n");
217*0Sstevel@tonic-gate 			return (-1);
218*0Sstevel@tonic-gate 		}
219*0Sstevel@tonic-gate 		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
220*0Sstevel@tonic-gate 			freenetconfigent(nconf);
221*0Sstevel@tonic-gate 			if (i != lastnetid)
222*0Sstevel@tonic-gate 				continue;
223*0Sstevel@tonic-gate 			logprintf("ypxfr: TLI problems\n");
224*0Sstevel@tonic-gate 			return (-1);
225*0Sstevel@tonic-gate 		}
226*0Sstevel@tonic-gate 		if (secure_map == TRUE) {
227*0Sstevel@tonic-gate 			if (netdir_options(nconf, ND_SET_RESERVEDPORT, fd,
228*0Sstevel@tonic-gate 					NULL) == -1) {
229*0Sstevel@tonic-gate 				(void) close(fd);
230*0Sstevel@tonic-gate 				freenetconfigent(nconf);
231*0Sstevel@tonic-gate 				if (i != lastnetid)
232*0Sstevel@tonic-gate 					continue;
233*0Sstevel@tonic-gate 				logprintf(
234*0Sstevel@tonic-gate 			"ypxfr: cannot bind to reserved port for %s\n%s\n",
235*0Sstevel@tonic-gate 					netid[i], netdir_sperror(""));
236*0Sstevel@tonic-gate 				return (-1);
237*0Sstevel@tonic-gate 			}
238*0Sstevel@tonic-gate 		}
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 		if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) ==
241*0Sstevel@tonic-gate 			NULL) {
242*0Sstevel@tonic-gate 			(void) close(fd);
243*0Sstevel@tonic-gate 			freenetconfigent(nconf);
244*0Sstevel@tonic-gate 			if (i != lastnetid)
245*0Sstevel@tonic-gate 				continue;
246*0Sstevel@tonic-gate 			logprintf("ypxfr: TLI problems\n");
247*0Sstevel@tonic-gate 			return (-1);
248*0Sstevel@tonic-gate 		}
249*0Sstevel@tonic-gate 		svcaddr = &(tbind->addr);
250*0Sstevel@tonic-gate 		if (rpcb_getaddr(YPXFRD, 1, nconf, svcaddr, master)
251*0Sstevel@tonic-gate 			== FALSE) {
252*0Sstevel@tonic-gate 			(void) t_free((char *)tbind, T_BIND);
253*0Sstevel@tonic-gate 			(void) close(fd);
254*0Sstevel@tonic-gate 			freenetconfigent(nconf);
255*0Sstevel@tonic-gate 			if (i != lastnetid)
256*0Sstevel@tonic-gate 				continue;
257*0Sstevel@tonic-gate 			logprintf("ypxfr: couldnot get %s address\n", master);
258*0Sstevel@tonic-gate 			return (-1);
259*0Sstevel@tonic-gate 		}
260*0Sstevel@tonic-gate 		if ((clnt = __nis_clnt_create(fd, nconf, 0, svcaddr, 0,
261*0Sstevel@tonic-gate 						YPXFRD, 1, recvsiz, 0)) == 0) {
262*0Sstevel@tonic-gate 			(void) t_free((char *)tbind, T_BIND);
263*0Sstevel@tonic-gate 			(void) close(fd);
264*0Sstevel@tonic-gate 			freenetconfigent(nconf);
265*0Sstevel@tonic-gate 			if (i != lastnetid)
266*0Sstevel@tonic-gate 				continue;
267*0Sstevel@tonic-gate 			clnt_pcreateerror(
268*0Sstevel@tonic-gate 				"ypxfr (get_map) - TCP channel create failure");
269*0Sstevel@tonic-gate 			return (-1);
270*0Sstevel@tonic-gate 		}
271*0Sstevel@tonic-gate 		(void) t_free((char *)tbind, T_BIND);
272*0Sstevel@tonic-gate 		break;
273*0Sstevel@tonic-gate 	}
274*0Sstevel@tonic-gate 	(void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, (char *)NULL);
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	rmap.map = map;
277*0Sstevel@tonic-gate 	rmap.domain = domain;
278*0Sstevel@tonic-gate 	memset((char *) &res, 0, sizeof (res));
279*0Sstevel@tonic-gate 	db = dbm_open(tempmap, O_RDWR + O_CREAT + O_TRUNC, 0777);
280*0Sstevel@tonic-gate 	if (db == NULL) {
281*0Sstevel@tonic-gate 		logprintf("dbm_open failed %s\n", tempmap);
282*0Sstevel@tonic-gate 		perror(tempmap);
283*0Sstevel@tonic-gate 		return (-2);
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	if (clnt_call(clnt, getdbm, xdr_hosereq, (char *)&rmap, xdr_myfyl,
287*0Sstevel@tonic-gate 		(char *)&res, TIMEOUT) != RPC_SUCCESS) {
288*0Sstevel@tonic-gate 		logprintf("clnt call to ypxfrd getdbm failed.\n");
289*0Sstevel@tonic-gate 		clnt_perror(clnt, "getdbm");
290*0Sstevel@tonic-gate 		dbm_deletefile(tempmap);
291*0Sstevel@tonic-gate 		return (-3);
292*0Sstevel@tonic-gate 	}
293*0Sstevel@tonic-gate 	if (res != OK) {
294*0Sstevel@tonic-gate 		logprintf("clnt call %s ypxfrd getdbm NOTOK %s %s code=%d\n",
295*0Sstevel@tonic-gate 			master, domain, map, res);
296*0Sstevel@tonic-gate 		dbm_deletefile(tempmap);
297*0Sstevel@tonic-gate 		return (-4);
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 	return (0);
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate }
302