17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*a506a34cSth160488 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*a506a34cSth160488 * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <stdio.h> 307c478bd9Sstevel@tonic-gate #include <errno.h> 317c478bd9Sstevel@tonic-gate #include <netconfig.h> 327c478bd9Sstevel@tonic-gate #include <netdir.h> 337c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 347c478bd9Sstevel@tonic-gate #include <sys/file.h> 357c478bd9Sstevel@tonic-gate #include <sys/param.h> 367c478bd9Sstevel@tonic-gate #include "ypxfrd.h" 377c478bd9Sstevel@tonic-gate #include <ndbm.h> 387c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h> 397c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h> /* for ENDIAN defines */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN) 447c478bd9Sstevel@tonic-gate #define DOSWAB 1 457c478bd9Sstevel@tonic-gate #endif 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate static struct timeval TIMEOUT = {25, 0}; 487c478bd9Sstevel@tonic-gate static DBM *db; 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate extern bool secure_map; 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate /* delete the dbm file with name file */ 53*a506a34cSth160488 static int 547c478bd9Sstevel@tonic-gate dbm_deletefile(file) 557c478bd9Sstevel@tonic-gate char *file; 567c478bd9Sstevel@tonic-gate { 577c478bd9Sstevel@tonic-gate char pag1[MAXPATHLEN]; 587c478bd9Sstevel@tonic-gate char dir1[MAXPATHLEN]; 597c478bd9Sstevel@tonic-gate int err; 607c478bd9Sstevel@tonic-gate strcpy(pag1, file); 617c478bd9Sstevel@tonic-gate strcat(pag1, ".pag"); 627c478bd9Sstevel@tonic-gate strcpy(dir1, file); 637c478bd9Sstevel@tonic-gate strcat(dir1, ".dir"); 647c478bd9Sstevel@tonic-gate err = 0; 657c478bd9Sstevel@tonic-gate if (unlink(pag1) < 0) { 667c478bd9Sstevel@tonic-gate perror("unlinkpag"); 677c478bd9Sstevel@tonic-gate err = -1; 687c478bd9Sstevel@tonic-gate } 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate if (unlink(dir1) < 0) { 717c478bd9Sstevel@tonic-gate perror("unlinkdir"); 727c478bd9Sstevel@tonic-gate return (-1); 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate return (err); 757c478bd9Sstevel@tonic-gate } 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* xdr just the .pag file of a dbm file */ 787c478bd9Sstevel@tonic-gate static bool_t 797c478bd9Sstevel@tonic-gate xdr_pages(xdrs, objp) 807c478bd9Sstevel@tonic-gate XDR *xdrs; 817c478bd9Sstevel@tonic-gate { 827c478bd9Sstevel@tonic-gate static struct pag res; 837c478bd9Sstevel@tonic-gate struct pag *PAG; 847c478bd9Sstevel@tonic-gate #ifdef DOSWAB 857c478bd9Sstevel@tonic-gate short *s; 867c478bd9Sstevel@tonic-gate int i; 877c478bd9Sstevel@tonic-gate #endif 887c478bd9Sstevel@tonic-gate bool_t more; 897c478bd9Sstevel@tonic-gate bool_t goteof; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate goteof = FALSE; 927c478bd9Sstevel@tonic-gate if (!xdr_pag(xdrs, &res)) 937c478bd9Sstevel@tonic-gate return (FALSE); 947c478bd9Sstevel@tonic-gate PAG = &res; 957c478bd9Sstevel@tonic-gate while (1) { 967c478bd9Sstevel@tonic-gate if (PAG->status == OK) { 977c478bd9Sstevel@tonic-gate #ifdef DOSWAB 987c478bd9Sstevel@tonic-gate s = (short *)PAG->pag_u.ok.blkdat; 997c478bd9Sstevel@tonic-gate s[0] = ntohs(s[0]); 1007c478bd9Sstevel@tonic-gate for (i = 1; i <= s[0]; i++) 1017c478bd9Sstevel@tonic-gate s[i] = ntohs(s[i]); 1027c478bd9Sstevel@tonic-gate #endif 1037c478bd9Sstevel@tonic-gate errno = 0; 1047c478bd9Sstevel@tonic-gate lseek(db->dbm_pagf, 1057c478bd9Sstevel@tonic-gate PAG->pag_u.ok.blkno * PBLKSIZ, L_SET); 1067c478bd9Sstevel@tonic-gate if (errno != 0) { 1077c478bd9Sstevel@tonic-gate perror("seek"); 1087c478bd9Sstevel@tonic-gate exit(-1); 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate if (write(db->dbm_pagf, 1117c478bd9Sstevel@tonic-gate PAG->pag_u.ok.blkdat, PBLKSIZ) < 0) { 1127c478bd9Sstevel@tonic-gate perror("write"); 1137c478bd9Sstevel@tonic-gate exit(-1); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate } else if (PAG->status == GETDBM_ERROR) { 1167c478bd9Sstevel@tonic-gate printf("clnt call getpag GETDBM_ERROR\n"); 1177c478bd9Sstevel@tonic-gate exit(-1); 1187c478bd9Sstevel@tonic-gate } else if (PAG->status == GETDBM_EOF) 1197c478bd9Sstevel@tonic-gate goteof = TRUE; 1207c478bd9Sstevel@tonic-gate if (!xdr_bool(xdrs, &more)) 1217c478bd9Sstevel@tonic-gate return (FALSE); 1227c478bd9Sstevel@tonic-gate if (more == FALSE) 1237c478bd9Sstevel@tonic-gate return (goteof); 1247c478bd9Sstevel@tonic-gate if (!xdr_pag(xdrs, &res)) 1257c478bd9Sstevel@tonic-gate return (FALSE); 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate /* xdr just the .dir part of a dbm file */ 1297c478bd9Sstevel@tonic-gate static bool_t 1307c478bd9Sstevel@tonic-gate xdr_dirs(xdrs, objp) 1317c478bd9Sstevel@tonic-gate XDR *xdrs; 1327c478bd9Sstevel@tonic-gate { 1337c478bd9Sstevel@tonic-gate static struct dir res; 1347c478bd9Sstevel@tonic-gate struct dir *DIR; 1357c478bd9Sstevel@tonic-gate bool_t more; 1367c478bd9Sstevel@tonic-gate bool_t goteof; 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate goteof = FALSE; 1397c478bd9Sstevel@tonic-gate if (!xdr_dir(xdrs, &res)) 1407c478bd9Sstevel@tonic-gate return (FALSE); 1417c478bd9Sstevel@tonic-gate DIR = &res; 1427c478bd9Sstevel@tonic-gate while (1) { 1437c478bd9Sstevel@tonic-gate if (DIR->status == OK) { 1447c478bd9Sstevel@tonic-gate errno = 0; 1457c478bd9Sstevel@tonic-gate lseek(db->dbm_dirf, 1467c478bd9Sstevel@tonic-gate DIR->dir_u.ok.blkno * DBLKSIZ, L_SET); 1477c478bd9Sstevel@tonic-gate if (errno != 0) { 1487c478bd9Sstevel@tonic-gate perror("seek"); 1497c478bd9Sstevel@tonic-gate exit(-1); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate if (write(db->dbm_dirf, 1527c478bd9Sstevel@tonic-gate DIR->dir_u.ok.blkdat, DBLKSIZ) < 0) { 1537c478bd9Sstevel@tonic-gate perror("write"); 1547c478bd9Sstevel@tonic-gate exit(-1); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate } else if (DIR->status == GETDBM_ERROR) { 1577c478bd9Sstevel@tonic-gate printf("clnt call getdir GETDBM_ERROR\n"); 1587c478bd9Sstevel@tonic-gate exit(-1); 1597c478bd9Sstevel@tonic-gate } else if (DIR->status == GETDBM_EOF) 1607c478bd9Sstevel@tonic-gate goteof = TRUE; 1617c478bd9Sstevel@tonic-gate if (!xdr_bool(xdrs, &more)) 1627c478bd9Sstevel@tonic-gate return (FALSE); 1637c478bd9Sstevel@tonic-gate if (more == FALSE) 1647c478bd9Sstevel@tonic-gate return (goteof); 1657c478bd9Sstevel@tonic-gate if (!xdr_dir(xdrs, &res)) 1667c478bd9Sstevel@tonic-gate return (FALSE); 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate /* 1717c478bd9Sstevel@tonic-gate * xdr a dbm file from ypxfrd 1727c478bd9Sstevel@tonic-gate * note that if the client or server do not support ndbm 1737c478bd9Sstevel@tonic-gate * we may not use this optional protocol 1747c478bd9Sstevel@tonic-gate */ 1757c478bd9Sstevel@tonic-gate 176*a506a34cSth160488 int 1777c478bd9Sstevel@tonic-gate xdr_myfyl(xdrs, objp) 1787c478bd9Sstevel@tonic-gate XDR *xdrs; 1797c478bd9Sstevel@tonic-gate int *objp; 1807c478bd9Sstevel@tonic-gate { 1817c478bd9Sstevel@tonic-gate if (!xdr_answer(xdrs, (answer *)objp)) 1827c478bd9Sstevel@tonic-gate return (FALSE); 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate if (*objp != OK) 1857c478bd9Sstevel@tonic-gate return (TRUE); 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate if (!xdr_pages(xdrs, NULL)) 1887c478bd9Sstevel@tonic-gate return (FALSE); 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate if (!xdr_dirs(xdrs, NULL)) 1917c478bd9Sstevel@tonic-gate return (FALSE); 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate return (TRUE); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 196*a506a34cSth160488 int 1977c478bd9Sstevel@tonic-gate ypxfrd_getdbm(tempmap, master, domain, map) 1987c478bd9Sstevel@tonic-gate char *tempmap; 1997c478bd9Sstevel@tonic-gate char *master; 2007c478bd9Sstevel@tonic-gate char *domain; 2017c478bd9Sstevel@tonic-gate char *map; 2027c478bd9Sstevel@tonic-gate { 2037c478bd9Sstevel@tonic-gate hosereq rmap; 2047c478bd9Sstevel@tonic-gate CLIENT *clnt; 2057c478bd9Sstevel@tonic-gate int res; 2067c478bd9Sstevel@tonic-gate int recvsiz = 24 * 1024; 2077c478bd9Sstevel@tonic-gate struct netconfig *nconf; 2087c478bd9Sstevel@tonic-gate int fd; 2097c478bd9Sstevel@tonic-gate struct netbuf *svcaddr; 2107c478bd9Sstevel@tonic-gate struct t_bind *tbind; 2117c478bd9Sstevel@tonic-gate char *netid[] = { "tcp6", "tcp" }; 2127c478bd9Sstevel@tonic-gate int i, lastnetid = (sizeof (netid)/sizeof (netid[0])) - 1; 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate for (i = 0; i <= lastnetid; i++) { 2157c478bd9Sstevel@tonic-gate if ((nconf = getnetconfigent(netid[i])) == NULL) { 2167c478bd9Sstevel@tonic-gate if (i != lastnetid) 2177c478bd9Sstevel@tonic-gate continue; 2187c478bd9Sstevel@tonic-gate logprintf("ypxfr: tcp transport not supported\n"); 2197c478bd9Sstevel@tonic-gate return (-1); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) { 2227c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 2237c478bd9Sstevel@tonic-gate if (i != lastnetid) 2247c478bd9Sstevel@tonic-gate continue; 2257c478bd9Sstevel@tonic-gate logprintf("ypxfr: TLI problems\n"); 2267c478bd9Sstevel@tonic-gate return (-1); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate if (secure_map == TRUE) { 2297c478bd9Sstevel@tonic-gate if (netdir_options(nconf, ND_SET_RESERVEDPORT, fd, 2307c478bd9Sstevel@tonic-gate NULL) == -1) { 2317c478bd9Sstevel@tonic-gate (void) close(fd); 2327c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 2337c478bd9Sstevel@tonic-gate if (i != lastnetid) 2347c478bd9Sstevel@tonic-gate continue; 2357c478bd9Sstevel@tonic-gate logprintf( 2367c478bd9Sstevel@tonic-gate "ypxfr: cannot bind to reserved port for %s\n%s\n", 2377c478bd9Sstevel@tonic-gate netid[i], netdir_sperror("")); 2387c478bd9Sstevel@tonic-gate return (-1); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) == 2437c478bd9Sstevel@tonic-gate NULL) { 2447c478bd9Sstevel@tonic-gate (void) close(fd); 2457c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 2467c478bd9Sstevel@tonic-gate if (i != lastnetid) 2477c478bd9Sstevel@tonic-gate continue; 2487c478bd9Sstevel@tonic-gate logprintf("ypxfr: TLI problems\n"); 2497c478bd9Sstevel@tonic-gate return (-1); 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate svcaddr = &(tbind->addr); 2527c478bd9Sstevel@tonic-gate if (rpcb_getaddr(YPXFRD, 1, nconf, svcaddr, master) 2537c478bd9Sstevel@tonic-gate == FALSE) { 2547c478bd9Sstevel@tonic-gate (void) t_free((char *)tbind, T_BIND); 2557c478bd9Sstevel@tonic-gate (void) close(fd); 2567c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 2577c478bd9Sstevel@tonic-gate if (i != lastnetid) 2587c478bd9Sstevel@tonic-gate continue; 2597c478bd9Sstevel@tonic-gate logprintf("ypxfr: couldnot get %s address\n", master); 2607c478bd9Sstevel@tonic-gate return (-1); 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate if ((clnt = __nis_clnt_create(fd, nconf, 0, svcaddr, 0, 2637c478bd9Sstevel@tonic-gate YPXFRD, 1, recvsiz, 0)) == 0) { 2647c478bd9Sstevel@tonic-gate (void) t_free((char *)tbind, T_BIND); 2657c478bd9Sstevel@tonic-gate (void) close(fd); 2667c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 2677c478bd9Sstevel@tonic-gate if (i != lastnetid) 2687c478bd9Sstevel@tonic-gate continue; 2697c478bd9Sstevel@tonic-gate clnt_pcreateerror( 2707c478bd9Sstevel@tonic-gate "ypxfr (get_map) - TCP channel create failure"); 2717c478bd9Sstevel@tonic-gate return (-1); 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate (void) t_free((char *)tbind, T_BIND); 2747c478bd9Sstevel@tonic-gate break; 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate (void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, (char *)NULL); 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate rmap.map = map; 2797c478bd9Sstevel@tonic-gate rmap.domain = domain; 2807c478bd9Sstevel@tonic-gate memset((char *)&res, 0, sizeof (res)); 2817c478bd9Sstevel@tonic-gate db = dbm_open(tempmap, O_RDWR + O_CREAT + O_TRUNC, 0777); 2827c478bd9Sstevel@tonic-gate if (db == NULL) { 2837c478bd9Sstevel@tonic-gate logprintf("dbm_open failed %s\n", tempmap); 2847c478bd9Sstevel@tonic-gate perror(tempmap); 2857c478bd9Sstevel@tonic-gate return (-2); 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate if (clnt_call(clnt, getdbm, xdr_hosereq, (char *)&rmap, xdr_myfyl, 2897c478bd9Sstevel@tonic-gate (char *)&res, TIMEOUT) != RPC_SUCCESS) { 2907c478bd9Sstevel@tonic-gate logprintf("clnt call to ypxfrd getdbm failed.\n"); 2917c478bd9Sstevel@tonic-gate clnt_perror(clnt, "getdbm"); 2927c478bd9Sstevel@tonic-gate dbm_deletefile(tempmap); 2937c478bd9Sstevel@tonic-gate return (-3); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate if (res != OK) { 2967c478bd9Sstevel@tonic-gate logprintf("clnt call %s ypxfrd getdbm NOTOK %s %s code=%d\n", 2977c478bd9Sstevel@tonic-gate master, domain, map, res); 2987c478bd9Sstevel@tonic-gate dbm_deletefile(tempmap); 2997c478bd9Sstevel@tonic-gate return (-4); 3007c478bd9Sstevel@tonic-gate } 3017c478bd9Sstevel@tonic-gate return (0); 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate } 304