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