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 5*cab25e88SMichen Chang * Common Development and Distribution License (the "License"). 6*cab25e88SMichen Chang * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*cab25e88SMichen Chang * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23a506a34cSth160488 * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <stdio.h> 27*cab25e88SMichen Chang #include <stdlib.h> 287c478bd9Sstevel@tonic-gate #include <errno.h> 29*cab25e88SMichen Chang #include <unistd.h> 307c478bd9Sstevel@tonic-gate #include <netconfig.h> 317c478bd9Sstevel@tonic-gate #include <netdir.h> 327c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 337c478bd9Sstevel@tonic-gate #include <sys/file.h> 347c478bd9Sstevel@tonic-gate #include <sys/param.h> 357c478bd9Sstevel@tonic-gate #include "ypxfrd.h" 367c478bd9Sstevel@tonic-gate #include <ndbm.h> 377c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h> 387c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h> 39*cab25e88SMichen Chang #include <strings.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; 51*cab25e88SMichen Chang extern void logprintf(char *, ...); 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* delete the dbm file with name file */ 54a506a34cSth160488 static int 557c478bd9Sstevel@tonic-gate dbm_deletefile(file) 567c478bd9Sstevel@tonic-gate char *file; 577c478bd9Sstevel@tonic-gate { 587c478bd9Sstevel@tonic-gate char pag1[MAXPATHLEN]; 597c478bd9Sstevel@tonic-gate char dir1[MAXPATHLEN]; 607c478bd9Sstevel@tonic-gate int err; 617c478bd9Sstevel@tonic-gate strcpy(pag1, file); 627c478bd9Sstevel@tonic-gate strcat(pag1, ".pag"); 637c478bd9Sstevel@tonic-gate strcpy(dir1, file); 647c478bd9Sstevel@tonic-gate strcat(dir1, ".dir"); 657c478bd9Sstevel@tonic-gate err = 0; 667c478bd9Sstevel@tonic-gate if (unlink(pag1) < 0) { 677c478bd9Sstevel@tonic-gate perror("unlinkpag"); 687c478bd9Sstevel@tonic-gate err = -1; 697c478bd9Sstevel@tonic-gate } 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate if (unlink(dir1) < 0) { 727c478bd9Sstevel@tonic-gate perror("unlinkdir"); 737c478bd9Sstevel@tonic-gate return (-1); 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate return (err); 767c478bd9Sstevel@tonic-gate } 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* xdr just the .pag file of a dbm file */ 797c478bd9Sstevel@tonic-gate static bool_t 80*cab25e88SMichen Chang xdr_pages(xdrs) 817c478bd9Sstevel@tonic-gate XDR *xdrs; 827c478bd9Sstevel@tonic-gate { 837c478bd9Sstevel@tonic-gate static struct pag res; 847c478bd9Sstevel@tonic-gate struct pag *PAG; 857c478bd9Sstevel@tonic-gate #ifdef DOSWAB 867c478bd9Sstevel@tonic-gate short *s; 877c478bd9Sstevel@tonic-gate int i; 887c478bd9Sstevel@tonic-gate #endif 897c478bd9Sstevel@tonic-gate bool_t more; 907c478bd9Sstevel@tonic-gate bool_t goteof; 91*cab25e88SMichen Chang off64_t where; 92*cab25e88SMichen Chang int true = 1; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate goteof = FALSE; 957c478bd9Sstevel@tonic-gate if (!xdr_pag(xdrs, &res)) 967c478bd9Sstevel@tonic-gate return (FALSE); 977c478bd9Sstevel@tonic-gate PAG = &res; 98*cab25e88SMichen Chang while (true) { 997c478bd9Sstevel@tonic-gate if (PAG->status == OK) { 1007c478bd9Sstevel@tonic-gate #ifdef DOSWAB 1017c478bd9Sstevel@tonic-gate s = (short *)PAG->pag_u.ok.blkdat; 1027c478bd9Sstevel@tonic-gate s[0] = ntohs(s[0]); 1037c478bd9Sstevel@tonic-gate for (i = 1; i <= s[0]; i++) 1047c478bd9Sstevel@tonic-gate s[i] = ntohs(s[i]); 1057c478bd9Sstevel@tonic-gate #endif 1067c478bd9Sstevel@tonic-gate errno = 0; 107*cab25e88SMichen Chang where = (((off64_t)PAG->pag_u.ok.blkno) * PBLKSIZ); 108*cab25e88SMichen Chang (void) lseek64(db->dbm_pagf, where, L_SET); 1097c478bd9Sstevel@tonic-gate if (errno != 0) { 1107c478bd9Sstevel@tonic-gate perror("seek"); 1117c478bd9Sstevel@tonic-gate exit(-1); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate if (write(db->dbm_pagf, 1147c478bd9Sstevel@tonic-gate PAG->pag_u.ok.blkdat, PBLKSIZ) < 0) { 1157c478bd9Sstevel@tonic-gate perror("write"); 1167c478bd9Sstevel@tonic-gate exit(-1); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate } else if (PAG->status == GETDBM_ERROR) { 119*cab25e88SMichen Chang (void) printf("clnt call getpag GETDBM_ERROR\n"); 1207c478bd9Sstevel@tonic-gate exit(-1); 1217c478bd9Sstevel@tonic-gate } else if (PAG->status == GETDBM_EOF) 1227c478bd9Sstevel@tonic-gate goteof = TRUE; 1237c478bd9Sstevel@tonic-gate if (!xdr_bool(xdrs, &more)) 1247c478bd9Sstevel@tonic-gate return (FALSE); 1257c478bd9Sstevel@tonic-gate if (more == FALSE) 1267c478bd9Sstevel@tonic-gate return (goteof); 1277c478bd9Sstevel@tonic-gate if (!xdr_pag(xdrs, &res)) 1287c478bd9Sstevel@tonic-gate return (FALSE); 1297c478bd9Sstevel@tonic-gate } 130*cab25e88SMichen Chang /*NOTREACHED*/ 131*cab25e88SMichen Chang return (TRUE); 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate /* xdr just the .dir part of a dbm file */ 1347c478bd9Sstevel@tonic-gate static bool_t 135*cab25e88SMichen Chang xdr_dirs(xdrs) 1367c478bd9Sstevel@tonic-gate XDR *xdrs; 1377c478bd9Sstevel@tonic-gate { 1387c478bd9Sstevel@tonic-gate static struct dir res; 1397c478bd9Sstevel@tonic-gate struct dir *DIR; 1407c478bd9Sstevel@tonic-gate bool_t more; 1417c478bd9Sstevel@tonic-gate bool_t goteof; 142*cab25e88SMichen Chang off64_t where; 143*cab25e88SMichen Chang int true = 1; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate goteof = FALSE; 1467c478bd9Sstevel@tonic-gate if (!xdr_dir(xdrs, &res)) 1477c478bd9Sstevel@tonic-gate return (FALSE); 1487c478bd9Sstevel@tonic-gate DIR = &res; 149*cab25e88SMichen Chang while (true) { 1507c478bd9Sstevel@tonic-gate if (DIR->status == OK) { 1517c478bd9Sstevel@tonic-gate errno = 0; 152*cab25e88SMichen Chang where = (((off64_t)DIR->dir_u.ok.blkno) * DBLKSIZ); 153*cab25e88SMichen Chang (void) lseek64(db->dbm_dirf, where, L_SET); 1547c478bd9Sstevel@tonic-gate if (errno != 0) { 1557c478bd9Sstevel@tonic-gate perror("seek"); 1567c478bd9Sstevel@tonic-gate exit(-1); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate if (write(db->dbm_dirf, 1597c478bd9Sstevel@tonic-gate DIR->dir_u.ok.blkdat, DBLKSIZ) < 0) { 1607c478bd9Sstevel@tonic-gate perror("write"); 1617c478bd9Sstevel@tonic-gate exit(-1); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate } else if (DIR->status == GETDBM_ERROR) { 164*cab25e88SMichen Chang (void) printf("clnt call getdir GETDBM_ERROR\n"); 1657c478bd9Sstevel@tonic-gate exit(-1); 1667c478bd9Sstevel@tonic-gate } else if (DIR->status == GETDBM_EOF) 1677c478bd9Sstevel@tonic-gate goteof = TRUE; 1687c478bd9Sstevel@tonic-gate if (!xdr_bool(xdrs, &more)) 1697c478bd9Sstevel@tonic-gate return (FALSE); 1707c478bd9Sstevel@tonic-gate if (more == FALSE) 1717c478bd9Sstevel@tonic-gate return (goteof); 1727c478bd9Sstevel@tonic-gate if (!xdr_dir(xdrs, &res)) 1737c478bd9Sstevel@tonic-gate return (FALSE); 1747c478bd9Sstevel@tonic-gate } 175*cab25e88SMichen Chang /*NOTREACHED*/ 176*cab25e88SMichen Chang return (TRUE); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate /* 1807c478bd9Sstevel@tonic-gate * xdr a dbm file from ypxfrd 1817c478bd9Sstevel@tonic-gate * note that if the client or server do not support ndbm 1827c478bd9Sstevel@tonic-gate * we may not use this optional protocol 1837c478bd9Sstevel@tonic-gate */ 1847c478bd9Sstevel@tonic-gate 185a506a34cSth160488 int 1867c478bd9Sstevel@tonic-gate xdr_myfyl(xdrs, objp) 1877c478bd9Sstevel@tonic-gate XDR *xdrs; 1887c478bd9Sstevel@tonic-gate int *objp; 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate if (!xdr_answer(xdrs, (answer *)objp)) 1917c478bd9Sstevel@tonic-gate return (FALSE); 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate if (*objp != OK) 1947c478bd9Sstevel@tonic-gate return (TRUE); 1957c478bd9Sstevel@tonic-gate 196*cab25e88SMichen Chang if (!xdr_pages(xdrs)) 1977c478bd9Sstevel@tonic-gate return (FALSE); 1987c478bd9Sstevel@tonic-gate 199*cab25e88SMichen Chang if (!xdr_dirs(xdrs)) 2007c478bd9Sstevel@tonic-gate return (FALSE); 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate return (TRUE); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 205a506a34cSth160488 int 2067c478bd9Sstevel@tonic-gate ypxfrd_getdbm(tempmap, master, domain, map) 2077c478bd9Sstevel@tonic-gate char *tempmap; 2087c478bd9Sstevel@tonic-gate char *master; 2097c478bd9Sstevel@tonic-gate char *domain; 2107c478bd9Sstevel@tonic-gate char *map; 2117c478bd9Sstevel@tonic-gate { 2127c478bd9Sstevel@tonic-gate hosereq rmap; 2137c478bd9Sstevel@tonic-gate CLIENT *clnt; 2147c478bd9Sstevel@tonic-gate int res; 2157c478bd9Sstevel@tonic-gate int recvsiz = 24 * 1024; 2167c478bd9Sstevel@tonic-gate struct netconfig *nconf; 2177c478bd9Sstevel@tonic-gate int fd; 2187c478bd9Sstevel@tonic-gate struct netbuf *svcaddr; 2197c478bd9Sstevel@tonic-gate struct t_bind *tbind; 2207c478bd9Sstevel@tonic-gate char *netid[] = { "tcp6", "tcp" }; 2217c478bd9Sstevel@tonic-gate int i, lastnetid = (sizeof (netid)/sizeof (netid[0])) - 1; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate for (i = 0; i <= lastnetid; i++) { 2247c478bd9Sstevel@tonic-gate if ((nconf = getnetconfigent(netid[i])) == NULL) { 2257c478bd9Sstevel@tonic-gate if (i != lastnetid) 2267c478bd9Sstevel@tonic-gate continue; 2277c478bd9Sstevel@tonic-gate logprintf("ypxfr: tcp transport not supported\n"); 2287c478bd9Sstevel@tonic-gate return (-1); 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) { 2317c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 2327c478bd9Sstevel@tonic-gate if (i != lastnetid) 2337c478bd9Sstevel@tonic-gate continue; 2347c478bd9Sstevel@tonic-gate logprintf("ypxfr: TLI problems\n"); 2357c478bd9Sstevel@tonic-gate return (-1); 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate if (secure_map == TRUE) { 2387c478bd9Sstevel@tonic-gate if (netdir_options(nconf, ND_SET_RESERVEDPORT, fd, 2397c478bd9Sstevel@tonic-gate NULL) == -1) { 2407c478bd9Sstevel@tonic-gate (void) close(fd); 2417c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 2427c478bd9Sstevel@tonic-gate if (i != lastnetid) 2437c478bd9Sstevel@tonic-gate continue; 2447c478bd9Sstevel@tonic-gate logprintf( 2457c478bd9Sstevel@tonic-gate "ypxfr: cannot bind to reserved port for %s\n%s\n", 246*cab25e88SMichen Chang netid[i], netdir_sperror()); 2477c478bd9Sstevel@tonic-gate return (-1); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 251*cab25e88SMichen Chang /* LINTED pointer alignment */ 2527c478bd9Sstevel@tonic-gate if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) == 2537c478bd9Sstevel@tonic-gate NULL) { 2547c478bd9Sstevel@tonic-gate (void) close(fd); 2557c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 2567c478bd9Sstevel@tonic-gate if (i != lastnetid) 2577c478bd9Sstevel@tonic-gate continue; 2587c478bd9Sstevel@tonic-gate logprintf("ypxfr: TLI problems\n"); 2597c478bd9Sstevel@tonic-gate return (-1); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate svcaddr = &(tbind->addr); 2627c478bd9Sstevel@tonic-gate if (rpcb_getaddr(YPXFRD, 1, nconf, svcaddr, master) 2637c478bd9Sstevel@tonic-gate == FALSE) { 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 logprintf("ypxfr: couldnot get %s address\n", master); 2707c478bd9Sstevel@tonic-gate return (-1); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate if ((clnt = __nis_clnt_create(fd, nconf, 0, svcaddr, 0, 2737c478bd9Sstevel@tonic-gate YPXFRD, 1, recvsiz, 0)) == 0) { 2747c478bd9Sstevel@tonic-gate (void) t_free((char *)tbind, T_BIND); 2757c478bd9Sstevel@tonic-gate (void) close(fd); 2767c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 2777c478bd9Sstevel@tonic-gate if (i != lastnetid) 2787c478bd9Sstevel@tonic-gate continue; 2797c478bd9Sstevel@tonic-gate clnt_pcreateerror( 2807c478bd9Sstevel@tonic-gate "ypxfr (get_map) - TCP channel create failure"); 2817c478bd9Sstevel@tonic-gate return (-1); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate (void) t_free((char *)tbind, T_BIND); 2847c478bd9Sstevel@tonic-gate break; 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate (void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, (char *)NULL); 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate rmap.map = map; 2897c478bd9Sstevel@tonic-gate rmap.domain = domain; 290*cab25e88SMichen Chang (void) memset((char *)&res, 0, sizeof (res)); 2917c478bd9Sstevel@tonic-gate db = dbm_open(tempmap, O_RDWR + O_CREAT + O_TRUNC, 0777); 2927c478bd9Sstevel@tonic-gate if (db == NULL) { 2937c478bd9Sstevel@tonic-gate logprintf("dbm_open failed %s\n", tempmap); 2947c478bd9Sstevel@tonic-gate perror(tempmap); 2957c478bd9Sstevel@tonic-gate return (-2); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate if (clnt_call(clnt, getdbm, xdr_hosereq, (char *)&rmap, xdr_myfyl, 2997c478bd9Sstevel@tonic-gate (char *)&res, TIMEOUT) != RPC_SUCCESS) { 3007c478bd9Sstevel@tonic-gate logprintf("clnt call to ypxfrd getdbm failed.\n"); 3017c478bd9Sstevel@tonic-gate clnt_perror(clnt, "getdbm"); 302*cab25e88SMichen Chang (void) dbm_deletefile(tempmap); 3037c478bd9Sstevel@tonic-gate return (-3); 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate if (res != OK) { 3067c478bd9Sstevel@tonic-gate logprintf("clnt call %s ypxfrd getdbm NOTOK %s %s code=%d\n", 3077c478bd9Sstevel@tonic-gate master, domain, map, res); 308*cab25e88SMichen Chang (void) dbm_deletefile(tempmap); 3097c478bd9Sstevel@tonic-gate return (-4); 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate return (0); 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate } 314