1*f1b9d127SSheldon Hearn /*
2*f1b9d127SSheldon Hearn * Copyright (c) 2000, Boris Popov
3*f1b9d127SSheldon Hearn * All rights reserved.
4*f1b9d127SSheldon Hearn *
5*f1b9d127SSheldon Hearn * Redistribution and use in source and binary forms, with or without
6*f1b9d127SSheldon Hearn * modification, are permitted provided that the following conditions
7*f1b9d127SSheldon Hearn * are met:
8*f1b9d127SSheldon Hearn * 1. Redistributions of source code must retain the above copyright
9*f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer.
10*f1b9d127SSheldon Hearn * 2. Redistributions in binary form must reproduce the above copyright
11*f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer in the
12*f1b9d127SSheldon Hearn * documentation and/or other materials provided with the distribution.
13*f1b9d127SSheldon Hearn * 3. All advertising materials mentioning features or use of this software
14*f1b9d127SSheldon Hearn * must display the following acknowledgement:
15*f1b9d127SSheldon Hearn * This product includes software developed by Boris Popov.
16*f1b9d127SSheldon Hearn * 4. Neither the name of the author nor the names of any co-contributors
17*f1b9d127SSheldon Hearn * may be used to endorse or promote products derived from this software
18*f1b9d127SSheldon Hearn * without specific prior written permission.
19*f1b9d127SSheldon Hearn *
20*f1b9d127SSheldon Hearn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21*f1b9d127SSheldon Hearn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*f1b9d127SSheldon Hearn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*f1b9d127SSheldon Hearn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24*f1b9d127SSheldon Hearn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*f1b9d127SSheldon Hearn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*f1b9d127SSheldon Hearn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*f1b9d127SSheldon Hearn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*f1b9d127SSheldon Hearn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*f1b9d127SSheldon Hearn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*f1b9d127SSheldon Hearn * SUCH DAMAGE.
31*f1b9d127SSheldon Hearn *
32*f1b9d127SSheldon Hearn * $Id: lookup.c,v 1.3 2000/10/15 14:26:49 bp Exp $
33*f1b9d127SSheldon Hearn */
34*f1b9d127SSheldon Hearn #include <sys/param.h>
35*f1b9d127SSheldon Hearn #include <sys/errno.h>
36*f1b9d127SSheldon Hearn #include <sys/stat.h>
37*f1b9d127SSheldon Hearn #include <sys/socket.h>
38*f1b9d127SSheldon Hearn #include <err.h>
39*f1b9d127SSheldon Hearn #include <stdio.h>
40*f1b9d127SSheldon Hearn #include <unistd.h>
41*f1b9d127SSheldon Hearn #include <strings.h>
42*f1b9d127SSheldon Hearn #include <stdlib.h>
43*f1b9d127SSheldon Hearn #include <sysexits.h>
44*f1b9d127SSheldon Hearn
45*f1b9d127SSheldon Hearn #include <netinet/in.h>
46*f1b9d127SSheldon Hearn #include <arpa/inet.h>
47*f1b9d127SSheldon Hearn
48*f1b9d127SSheldon Hearn #include <cflib.h>
49*f1b9d127SSheldon Hearn
50*f1b9d127SSheldon Hearn #include <netsmb/netbios.h>
51*f1b9d127SSheldon Hearn #include <netsmb/smb_lib.h>
52*f1b9d127SSheldon Hearn #include <netsmb/nb_lib.h>
53*f1b9d127SSheldon Hearn #include <netsmb/smb_conn.h>
54*f1b9d127SSheldon Hearn
55*f1b9d127SSheldon Hearn #include "common.h"
56*f1b9d127SSheldon Hearn
57*f1b9d127SSheldon Hearn
58*f1b9d127SSheldon Hearn int
cmd_lookup(int argc,char * argv[])59*f1b9d127SSheldon Hearn cmd_lookup(int argc, char *argv[])
60*f1b9d127SSheldon Hearn {
61*f1b9d127SSheldon Hearn struct nb_ctx *ctx;
62*f1b9d127SSheldon Hearn struct sockaddr *sap;
63*f1b9d127SSheldon Hearn char *hostname;
64*f1b9d127SSheldon Hearn int error, opt;
65*f1b9d127SSheldon Hearn
66*f1b9d127SSheldon Hearn if (argc < 2)
67*f1b9d127SSheldon Hearn lookup_usage();
68*f1b9d127SSheldon Hearn error = nb_ctx_create(&ctx);
69*f1b9d127SSheldon Hearn if (error) {
70*f1b9d127SSheldon Hearn smb_error("unable to create nbcontext", error);
71*f1b9d127SSheldon Hearn exit(1);
72*f1b9d127SSheldon Hearn }
73*f1b9d127SSheldon Hearn if (smb_open_rcfile() == 0) {
74*f1b9d127SSheldon Hearn if (nb_ctx_readrcsection(smb_rc, ctx, "default", 0) != 0)
75*f1b9d127SSheldon Hearn exit(1);
76*f1b9d127SSheldon Hearn rc_close(smb_rc);
77*f1b9d127SSheldon Hearn }
78*f1b9d127SSheldon Hearn while ((opt = getopt(argc, argv, "w:")) != EOF) {
79*f1b9d127SSheldon Hearn switch(opt){
80*f1b9d127SSheldon Hearn case 'w':
81*f1b9d127SSheldon Hearn nb_ctx_setns(ctx, optarg);
82*f1b9d127SSheldon Hearn break;
83*f1b9d127SSheldon Hearn default:
84*f1b9d127SSheldon Hearn lookup_usage();
85*f1b9d127SSheldon Hearn /*NOTREACHED*/
86*f1b9d127SSheldon Hearn }
87*f1b9d127SSheldon Hearn }
88*f1b9d127SSheldon Hearn if (optind >= argc)
89*f1b9d127SSheldon Hearn lookup_usage();
90*f1b9d127SSheldon Hearn if (nb_ctx_resolve(ctx) != 0)
91*f1b9d127SSheldon Hearn exit(1);
92*f1b9d127SSheldon Hearn hostname = argv[argc - 1];
93*f1b9d127SSheldon Hearn /* printf("Looking for %s...\n", hostname);*/
94*f1b9d127SSheldon Hearn error = nbns_resolvename(hostname, ctx, &sap);
95*f1b9d127SSheldon Hearn if (error) {
96*f1b9d127SSheldon Hearn smb_error("unable to resolve %s", error, hostname);
97*f1b9d127SSheldon Hearn exit(1);
98*f1b9d127SSheldon Hearn }
99*f1b9d127SSheldon Hearn printf("Got response from %s\n", inet_ntoa(ctx->nb_lastns.sin_addr));
100*f1b9d127SSheldon Hearn printf("IP address of %s: %s\n", hostname, inet_ntoa(((struct sockaddr_in*)sap)->sin_addr));
101*f1b9d127SSheldon Hearn return 0;
102*f1b9d127SSheldon Hearn }
103*f1b9d127SSheldon Hearn
104*f1b9d127SSheldon Hearn
105*f1b9d127SSheldon Hearn void
lookup_usage(void)106*f1b9d127SSheldon Hearn lookup_usage(void)
107*f1b9d127SSheldon Hearn {
108*f1b9d127SSheldon Hearn printf("usage: smbutil lookup [-w host] name\n");
109*f1b9d127SSheldon Hearn exit(1);
110*f1b9d127SSheldon Hearn }
111