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 * Copyright 1995 Sun Microsystems, Inc. All rights reserved. 23*0Sstevel@tonic-gate * Use is subject to license terms. 24*0Sstevel@tonic-gate */ 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 27*0Sstevel@tonic-gate /* All Rights Reserved */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 31*0Sstevel@tonic-gate * under license from the Regents of the University of 32*0Sstevel@tonic-gate * California. 33*0Sstevel@tonic-gate */ 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate /* 38*0Sstevel@tonic-gate * This is a user command which asks a particular ypserv which version of a 39*0Sstevel@tonic-gate * map it is using. Usage is: 40*0Sstevel@tonic-gate * 41*0Sstevel@tonic-gate * yppoll [-h <host>] [-d <domainname>] mapname 42*0Sstevel@tonic-gate * 43*0Sstevel@tonic-gate * If the host is ommitted, the local host will be used. If host is specified 44*0Sstevel@tonic-gate * as an internet address, no yp services need to be locally available. 45*0Sstevel@tonic-gate * 46*0Sstevel@tonic-gate */ 47*0Sstevel@tonic-gate #include <stdio.h> 48*0Sstevel@tonic-gate #include <ctype.h> 49*0Sstevel@tonic-gate #include <rpc/rpc.h> 50*0Sstevel@tonic-gate #include <rpcsvc/ypclnt.h> 51*0Sstevel@tonic-gate #include <rpcsvc/yp_prot.h> 52*0Sstevel@tonic-gate #include <netdir.h> 53*0Sstevel@tonic-gate #include <arpa/inet.h> 54*0Sstevel@tonic-gate #include "yp_b.h" 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate #ifdef NULL 57*0Sstevel@tonic-gate #undef NULL 58*0Sstevel@tonic-gate #endif 59*0Sstevel@tonic-gate #define NULL 0 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #define TIMEOUT 30 /* Total seconds for timeout */ 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate static int status = 0; /* exit status */ 64*0Sstevel@tonic-gate static char *domain = NULL; 65*0Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN]; 66*0Sstevel@tonic-gate static char *map = NULL; 67*0Sstevel@tonic-gate static char *host = NULL; 68*0Sstevel@tonic-gate static char default_host_name[256]; 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate static char err_usage[] = 71*0Sstevel@tonic-gate "Usage:\n\ 72*0Sstevel@tonic-gate yppoll [ -h host ] [ -d domainname ] mapname\n\n"; 73*0Sstevel@tonic-gate static char err_bad_args[] = 74*0Sstevel@tonic-gate "Bad %s argument.\n"; 75*0Sstevel@tonic-gate static char err_cant_get_kname[] = 76*0Sstevel@tonic-gate "Can't get %s back from system call.\n"; 77*0Sstevel@tonic-gate static char err_null_kname[] = 78*0Sstevel@tonic-gate "%s hasn't been set on this machine.\n"; 79*0Sstevel@tonic-gate static char err_bad_hostname[] = "hostname"; 80*0Sstevel@tonic-gate static char err_bad_mapname[] = "mapname"; 81*0Sstevel@tonic-gate static char err_bad_domainname[] = "domainname"; 82*0Sstevel@tonic-gate static char err_bad_resp[] = 83*0Sstevel@tonic-gate "Ill-formed response returned from ypserv on host %s.\n"; 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate static void get_command_line_args(); 86*0Sstevel@tonic-gate static void getdomain(); 87*0Sstevel@tonic-gate static void getlochost(); 88*0Sstevel@tonic-gate static void getmapparms(); 89*0Sstevel@tonic-gate static void newresults(); 90*0Sstevel@tonic-gate static void getypserv(); 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate extern void exit(); 93*0Sstevel@tonic-gate extern int getdomainname(); 94*0Sstevel@tonic-gate extern int gethostname(); 95*0Sstevel@tonic-gate extern unsigned int strlen(); 96*0Sstevel@tonic-gate extern int strcmp(); 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate /* 99*0Sstevel@tonic-gate * This is the mainline for the yppoll process. 100*0Sstevel@tonic-gate */ 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate void 103*0Sstevel@tonic-gate main(argc, argv) 104*0Sstevel@tonic-gate int argc; 105*0Sstevel@tonic-gate char **argv; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate { 108*0Sstevel@tonic-gate get_command_line_args(argc, argv); 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate if (!domain) { 111*0Sstevel@tonic-gate getdomain(); 112*0Sstevel@tonic-gate } 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate if (!host) { 115*0Sstevel@tonic-gate getypserv(); 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate getmapparms(); 119*0Sstevel@tonic-gate exit(status); 120*0Sstevel@tonic-gate /* NOTREACHED */ 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate /* 124*0Sstevel@tonic-gate * This does the command line argument processing. 125*0Sstevel@tonic-gate */ 126*0Sstevel@tonic-gate static void 127*0Sstevel@tonic-gate get_command_line_args(argc, argv) 128*0Sstevel@tonic-gate int argc; 129*0Sstevel@tonic-gate char **argv; 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate { 132*0Sstevel@tonic-gate argv++; 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate while (--argc) { 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate if ((*argv)[0] == '-') { 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate switch ((*argv)[1]) { 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate case 'h': 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate if (argc > 1) { 143*0Sstevel@tonic-gate argv++; 144*0Sstevel@tonic-gate argc--; 145*0Sstevel@tonic-gate host = *argv; 146*0Sstevel@tonic-gate argv++; 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate if ((int)strlen(host) > 256) { 149*0Sstevel@tonic-gate (void) fprintf(stderr, 150*0Sstevel@tonic-gate err_bad_args, 151*0Sstevel@tonic-gate err_bad_hostname); 152*0Sstevel@tonic-gate exit(1); 153*0Sstevel@tonic-gate } 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate } else { 156*0Sstevel@tonic-gate (void) fprintf(stderr, err_usage); 157*0Sstevel@tonic-gate exit(1); 158*0Sstevel@tonic-gate } 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate break; 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate case 'd': 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate if (argc > 1) { 165*0Sstevel@tonic-gate argv++; 166*0Sstevel@tonic-gate argc--; 167*0Sstevel@tonic-gate domain = *argv; 168*0Sstevel@tonic-gate argv++; 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate if ((int)strlen(domain) > YPMAXDOMAIN) { 171*0Sstevel@tonic-gate (void) fprintf(stderr, 172*0Sstevel@tonic-gate err_bad_args, 173*0Sstevel@tonic-gate err_bad_domainname); 174*0Sstevel@tonic-gate exit(1); 175*0Sstevel@tonic-gate } 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate } else { 178*0Sstevel@tonic-gate (void) fprintf(stderr, err_usage); 179*0Sstevel@tonic-gate exit(1); 180*0Sstevel@tonic-gate } 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate break; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate default: 185*0Sstevel@tonic-gate (void) fprintf(stderr, err_usage); 186*0Sstevel@tonic-gate exit(1); 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate } else { 191*0Sstevel@tonic-gate if (!map) { 192*0Sstevel@tonic-gate map = *argv; 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate if ((int)strlen(map) > YPMAXMAP) { 195*0Sstevel@tonic-gate (void) fprintf(stderr, err_bad_args, 196*0Sstevel@tonic-gate err_bad_mapname); 197*0Sstevel@tonic-gate exit(1); 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate } else { 201*0Sstevel@tonic-gate (void) fprintf(stderr, err_usage); 202*0Sstevel@tonic-gate exit(1); 203*0Sstevel@tonic-gate } 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate if (!map) { 208*0Sstevel@tonic-gate (void) fprintf(stderr, err_usage); 209*0Sstevel@tonic-gate exit(1); 210*0Sstevel@tonic-gate } 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate /* 214*0Sstevel@tonic-gate * This gets the local default domainname, and makes sure that it's set 215*0Sstevel@tonic-gate * to something reasonable. domain is set here. 216*0Sstevel@tonic-gate */ 217*0Sstevel@tonic-gate static void 218*0Sstevel@tonic-gate getdomain() 219*0Sstevel@tonic-gate { 220*0Sstevel@tonic-gate if (!getdomainname(default_domain_name, YPMAXDOMAIN)) { 221*0Sstevel@tonic-gate domain = default_domain_name; 222*0Sstevel@tonic-gate } else { 223*0Sstevel@tonic-gate (void) fprintf(stderr, err_cant_get_kname, err_bad_domainname); 224*0Sstevel@tonic-gate exit(1); 225*0Sstevel@tonic-gate } 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate if ((int)strlen(domain) == 0) { 228*0Sstevel@tonic-gate (void) fprintf(stderr, err_null_kname, err_bad_domainname); 229*0Sstevel@tonic-gate exit(1); 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate /* 234*0Sstevel@tonic-gate * This gets the local hostname back from the kernel 235*0Sstevel@tonic-gate */ 236*0Sstevel@tonic-gate static void 237*0Sstevel@tonic-gate getlochost() 238*0Sstevel@tonic-gate { 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate if (! gethostname(default_host_name, 256)) { 241*0Sstevel@tonic-gate host = default_host_name; 242*0Sstevel@tonic-gate } else { 243*0Sstevel@tonic-gate (void) fprintf(stderr, err_cant_get_kname, err_bad_hostname); 244*0Sstevel@tonic-gate exit(1); 245*0Sstevel@tonic-gate } 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate static void 249*0Sstevel@tonic-gate getmapparms() 250*0Sstevel@tonic-gate { 251*0Sstevel@tonic-gate CLIENT * map_clnt; 252*0Sstevel@tonic-gate struct ypresp_order oresp; 253*0Sstevel@tonic-gate struct ypreq_nokey req; 254*0Sstevel@tonic-gate struct ypresp_master mresp; 255*0Sstevel@tonic-gate struct ypresp_master *mresults = (struct ypresp_master *) NULL; 256*0Sstevel@tonic-gate struct ypresp_order *oresults = (struct ypresp_order *) NULL; 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate struct timeval timeout; 259*0Sstevel@tonic-gate enum clnt_stat s; 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate if ((map_clnt = clnt_create(host, YPPROG, YPVERS, 262*0Sstevel@tonic-gate "netpath")) == NULL) { 263*0Sstevel@tonic-gate (void) fprintf(stderr, 264*0Sstevel@tonic-gate "Can't create connection to %s.\n", host); 265*0Sstevel@tonic-gate clnt_pcreateerror("Reason"); 266*0Sstevel@tonic-gate exit(1); 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate timeout.tv_sec = TIMEOUT; 270*0Sstevel@tonic-gate timeout.tv_usec = 0; 271*0Sstevel@tonic-gate req.domain = domain; 272*0Sstevel@tonic-gate req.map = map; 273*0Sstevel@tonic-gate mresp.master = NULL; 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate if (clnt_call(map_clnt, YPPROC_MASTER, (xdrproc_t)xdr_ypreq_nokey, 276*0Sstevel@tonic-gate (caddr_t) &req, (xdrproc_t)xdr_ypresp_master, 277*0Sstevel@tonic-gate (caddr_t) &mresp, timeout) == RPC_SUCCESS) { 278*0Sstevel@tonic-gate mresults = &mresp; 279*0Sstevel@tonic-gate s = (enum clnt_stat) clnt_call(map_clnt, YPPROC_ORDER, 280*0Sstevel@tonic-gate (xdrproc_t)xdr_ypreq_nokey, (char *)&req, 281*0Sstevel@tonic-gate (xdrproc_t)xdr_ypresp_order, (char *)&oresp, timeout); 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate if (s == RPC_SUCCESS) { 284*0Sstevel@tonic-gate oresults = &oresp; 285*0Sstevel@tonic-gate newresults(mresults, oresults); 286*0Sstevel@tonic-gate } else { 287*0Sstevel@tonic-gate (void) fprintf(stderr, 288*0Sstevel@tonic-gate "Can't make YPPROC_ORDER call to ypserv at %s.\n ", 289*0Sstevel@tonic-gate host); 290*0Sstevel@tonic-gate clnt_perror(map_clnt, "Reason"); 291*0Sstevel@tonic-gate exit(1); 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate } else { 295*0Sstevel@tonic-gate clnt_destroy(map_clnt); 296*0Sstevel@tonic-gate } 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate static void 300*0Sstevel@tonic-gate newresults(m, o) 301*0Sstevel@tonic-gate struct ypresp_master *m; 302*0Sstevel@tonic-gate struct ypresp_order *o; 303*0Sstevel@tonic-gate { 304*0Sstevel@tonic-gate char *s_domok = "Domain %s is supported.\n"; 305*0Sstevel@tonic-gate char *s_ook = "Map %s has order number %d.\n"; 306*0Sstevel@tonic-gate char *s_mok = "The master server is %s.\n"; 307*0Sstevel@tonic-gate char *s_mbad = "Can't get master for map %s.\n Reason: %s\n"; 308*0Sstevel@tonic-gate char *s_obad = "Can't get order number for map %s.\n Reason: %s\n"; 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate if (m->status == YP_TRUE && o->status == YP_TRUE) { 311*0Sstevel@tonic-gate (void) printf(s_domok, domain); 312*0Sstevel@tonic-gate (void) printf(s_ook, map, o->ordernum); 313*0Sstevel@tonic-gate (void) printf(s_mok, m->master); 314*0Sstevel@tonic-gate } else if (o->status == YP_TRUE) { 315*0Sstevel@tonic-gate (void) printf(s_domok, domain); 316*0Sstevel@tonic-gate (void) printf(s_ook, map, o->ordernum); 317*0Sstevel@tonic-gate (void) fprintf(stderr, s_mbad, map, 318*0Sstevel@tonic-gate yperr_string(ypprot_err(m->status))); 319*0Sstevel@tonic-gate status = 1; 320*0Sstevel@tonic-gate } else if (m->status == YP_TRUE) { 321*0Sstevel@tonic-gate (void) printf(s_domok, domain); 322*0Sstevel@tonic-gate (void) fprintf(stderr, s_obad, map, 323*0Sstevel@tonic-gate yperr_string(ypprot_err(o->status))); 324*0Sstevel@tonic-gate (void) printf(s_mok, m->master); 325*0Sstevel@tonic-gate status = 1; 326*0Sstevel@tonic-gate } else { 327*0Sstevel@tonic-gate (void) fprintf(stderr, 328*0Sstevel@tonic-gate "Can't get any map parameter information.\n"); 329*0Sstevel@tonic-gate (void) fprintf(stderr, s_obad, map, 330*0Sstevel@tonic-gate yperr_string(ypprot_err(o->status))); 331*0Sstevel@tonic-gate (void) fprintf(stderr, s_mbad, map, 332*0Sstevel@tonic-gate yperr_string(ypprot_err(m->status))); 333*0Sstevel@tonic-gate status = 1; 334*0Sstevel@tonic-gate } 335*0Sstevel@tonic-gate } 336*0Sstevel@tonic-gate 337*0Sstevel@tonic-gate static void 338*0Sstevel@tonic-gate getypserv() 339*0Sstevel@tonic-gate { 340*0Sstevel@tonic-gate struct ypbind_resp response; 341*0Sstevel@tonic-gate struct ypbind_domain ypdomain; 342*0Sstevel@tonic-gate struct ypbind_binding *binding; 343*0Sstevel@tonic-gate static char hostbuf[256]; 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate getlochost(); 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gate (void) memset((char *)&response, 0, sizeof (response)); 348*0Sstevel@tonic-gate ypdomain.ypbind_domainname = domain; 349*0Sstevel@tonic-gate ypdomain.ypbind_vers = YPBINDVERS; 350*0Sstevel@tonic-gate (void) rpc_call(host, YPBINDPROG, YPBINDVERS, YPBINDPROC_DOMAIN, 351*0Sstevel@tonic-gate xdr_ypbind_domain, (char *)&ypdomain, xdr_ypbind_resp, 352*0Sstevel@tonic-gate (char *)&response, "netpath"); 353*0Sstevel@tonic-gate if (response.ypbind_status != YPBIND_SUCC_VAL) { 354*0Sstevel@tonic-gate (void) fprintf(stderr, "couldn't get yp server - status %u\n", 355*0Sstevel@tonic-gate response.ypbind_status); 356*0Sstevel@tonic-gate exit(1); 357*0Sstevel@tonic-gate } 358*0Sstevel@tonic-gate binding = response.ypbind_resp_u.ypbind_bindinfo; 359*0Sstevel@tonic-gate host = binding->ypbind_servername; 360*0Sstevel@tonic-gate 361*0Sstevel@tonic-gate /* 362*0Sstevel@tonic-gate * When ypbind is running in broadcast mode, it sets the 363*0Sstevel@tonic-gate * servername to "". To get the real name of the server, 364*0Sstevel@tonic-gate * we need to do a host lookup on the svcaddr. This code 365*0Sstevel@tonic-gate * is similar to code in ypwhich. 366*0Sstevel@tonic-gate */ 367*0Sstevel@tonic-gate if (strcmp(host, "") == 0) { 368*0Sstevel@tonic-gate struct nd_hostservlist *nhs; 369*0Sstevel@tonic-gate struct netconfig *nconf = binding->ypbind_nconf; 370*0Sstevel@tonic-gate struct netbuf *svcaddr = binding->ypbind_svcaddr; 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate if (netdir_getbyaddr(nconf, &nhs, svcaddr) != ND_OK) { 373*0Sstevel@tonic-gate struct sockaddr_in *sa; 374*0Sstevel@tonic-gate 375*0Sstevel@tonic-gate sa = (struct sockaddr_in *)svcaddr->buf; 376*0Sstevel@tonic-gate 377*0Sstevel@tonic-gate strcpy(hostbuf, inet_ntoa(sa->sin_addr)); 378*0Sstevel@tonic-gate } else { 379*0Sstevel@tonic-gate sprintf(hostbuf, "%s", nhs->h_hostservs->h_host); 380*0Sstevel@tonic-gate } 381*0Sstevel@tonic-gate host = hostbuf; 382*0Sstevel@tonic-gate netdir_free((char *)nhs, ND_HOSTSERVLIST); 383*0Sstevel@tonic-gate } 384*0Sstevel@tonic-gate } 385