xref: /onnv-gate/usr/src/cmd/fs.d/smbclnt/smbutil/lookup.c (revision 10023:71bf38dba3d6)
16007Sthurlow /*
26007Sthurlow  * Copyright (c) 2000, Boris Popov
36007Sthurlow  * All rights reserved.
46007Sthurlow  *
56007Sthurlow  * Redistribution and use in source and binary forms, with or without
66007Sthurlow  * modification, are permitted provided that the following conditions
76007Sthurlow  * are met:
86007Sthurlow  * 1. Redistributions of source code must retain the above copyright
96007Sthurlow  *    notice, this list of conditions and the following disclaimer.
106007Sthurlow  * 2. Redistributions in binary form must reproduce the above copyright
116007Sthurlow  *    notice, this list of conditions and the following disclaimer in the
126007Sthurlow  *    documentation and/or other materials provided with the distribution.
136007Sthurlow  * 3. All advertising materials mentioning features or use of this software
146007Sthurlow  *    must display the following acknowledgement:
156007Sthurlow  *    This product includes software developed by Boris Popov.
166007Sthurlow  * 4. Neither the name of the author nor the names of any co-contributors
176007Sthurlow  *    may be used to endorse or promote products derived from this software
186007Sthurlow  *    without specific prior written permission.
196007Sthurlow  *
206007Sthurlow  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
216007Sthurlow  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226007Sthurlow  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236007Sthurlow  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
246007Sthurlow  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
256007Sthurlow  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
266007Sthurlow  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
276007Sthurlow  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
286007Sthurlow  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
296007Sthurlow  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
306007Sthurlow  * SUCH DAMAGE.
316007Sthurlow  *
326007Sthurlow  * $Id: lookup.c,v 1.1.1.1 2001/06/09 00:28:13 zarzycki Exp $
336007Sthurlow  */
346007Sthurlow 
35*10023SGordon.Ross@Sun.COM /*
36*10023SGordon.Ross@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37*10023SGordon.Ross@Sun.COM  * Use is subject to license terms.
38*10023SGordon.Ross@Sun.COM  */
396007Sthurlow 
406007Sthurlow #include <sys/param.h>
416007Sthurlow #include <sys/errno.h>
426007Sthurlow #include <sys/stat.h>
436007Sthurlow #include <sys/socket.h>
446007Sthurlow #include <stdio.h>
456007Sthurlow #include <err.h>
466007Sthurlow #include <unistd.h>
476007Sthurlow #include <strings.h>
486007Sthurlow #include <stdlib.h>
496007Sthurlow #include <sysexits.h>
506007Sthurlow #include <libintl.h>
516007Sthurlow 
526007Sthurlow #include <netinet/in.h>
536007Sthurlow #include <arpa/inet.h>
546007Sthurlow 
556007Sthurlow #include <cflib.h>
566007Sthurlow 
576007Sthurlow #include <netsmb/netbios.h>
586007Sthurlow #include <netsmb/smb_lib.h>
596007Sthurlow #include <netsmb/nb_lib.h>
606007Sthurlow 
616007Sthurlow #include "common.h"
626007Sthurlow 
636007Sthurlow 
646007Sthurlow int
cmd_lookup(int argc,char * argv[])656007Sthurlow cmd_lookup(int argc, char *argv[])
666007Sthurlow {
676007Sthurlow 	struct nb_ctx *ctx;
686007Sthurlow 	struct sockaddr *sap;
696007Sthurlow 	char *hostname;
706007Sthurlow 	int error, opt;
716007Sthurlow 
726007Sthurlow 	if (argc < 2)
736007Sthurlow 		lookup_usage();
746007Sthurlow 	error = nb_ctx_create(&ctx);
756007Sthurlow 	if (error) {
766007Sthurlow 		smb_error(gettext("unable to create nbcontext"), error);
776007Sthurlow 		exit(1);
786007Sthurlow 	}
796007Sthurlow 	if (smb_open_rcfile(NULL) == 0) {
80*10023SGordon.Ross@Sun.COM 		if (nb_ctx_readrcsection(NULL, ctx, "default", 0) != 0)
816007Sthurlow 			exit(1);
82*10023SGordon.Ross@Sun.COM 		smb_close_rcfile();
836007Sthurlow 	}
846007Sthurlow 	if ((ctx->nb_flags & NBCF_NS_ENABLE) == 0) {
856007Sthurlow 		fprintf(stderr,
866007Sthurlow 		    gettext("nbns_enable=false, cannot do lookup\n"));
876007Sthurlow 		exit(1);
886007Sthurlow 	}
896007Sthurlow 	while ((opt = getopt(argc, argv, "w:")) != EOF) {
906007Sthurlow 		switch (opt) {
916007Sthurlow 		case 'w':
926007Sthurlow 			nb_ctx_setns(ctx, optarg);
936007Sthurlow 			break;
946007Sthurlow 		default:
956007Sthurlow 			lookup_usage();
966007Sthurlow 			/*NOTREACHED*/
976007Sthurlow 		}
986007Sthurlow 	}
996007Sthurlow 	if (optind >= argc)
1006007Sthurlow 		lookup_usage();
1016007Sthurlow 	if (nb_ctx_resolve(ctx) != 0)
1026007Sthurlow 		exit(1);
1036007Sthurlow 	hostname = argv[argc - 1];
1046007Sthurlow 	error = nbns_resolvename(hostname, ctx, &sap);
1056007Sthurlow 	if (error) {
1066007Sthurlow 		smb_error(gettext("unable to resolve %s"), error, hostname);
1076007Sthurlow 		exit(1);
1086007Sthurlow 	}
1096007Sthurlow 	printf(gettext("Got response from %s\n"),
1106007Sthurlow 	    inet_ntoa(ctx->nb_lastns.sin_addr));
1116007Sthurlow 	printf(gettext("IP address of %s: %s\n"), hostname,
1126007Sthurlow 	    inet_ntoa(((struct sockaddr_in *)sap)->sin_addr));
1136007Sthurlow 	return (0);
1146007Sthurlow }
1156007Sthurlow 
1166007Sthurlow 
1176007Sthurlow void
lookup_usage(void)1186007Sthurlow lookup_usage(void)
1196007Sthurlow {
1206007Sthurlow 	printf(gettext("usage: smbutil lookup [-w host] name\n"));
1216007Sthurlow 	exit(1);
1226007Sthurlow }
123