first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/samba.git] / source3 / utils / nmblookup.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT client - used to lookup netbios names
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 */
22
23 #define NO_SYSLOG
24
25 #include "includes.h"
26
27 extern int DEBUGLEVEL;
28
29 extern pstring scope;
30
31 extern struct in_addr ipzero;
32
33 static BOOL use_bcast = True;
34 static BOOL got_bcast = False;
35 static struct in_addr bcast_addr;
36 static BOOL recursion_desired = False;
37 static BOOL translate_addresses = False;
38 static int ServerFD= -1;
39 static int RootPort = 0;
40 static BOOL find_status=False;
41
42 /****************************************************************************
43   open the socket communication
44   **************************************************************************/
45 static BOOL open_sockets(void)
46 {
47   ServerFD = open_socket_in( SOCK_DGRAM,
48                              (RootPort ? 137 :0),
49                              3,
50                              interpret_addr(lp_socket_address()), True );
51
52   if (ServerFD == -1)
53     return(False);
54
55   set_socket_options(ServerFD,"SO_BROADCAST");
56
57   DEBUG(3, ("Socket opened.\n"));
58   return True;
59 }
60
61
62 /****************************************************************************
63 usage on the program
64 ****************************************************************************/
65 static void usage(void)
66 {
67   printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
68   printf("Version %s\n",VERSION);
69   printf("\t-d debuglevel         set the debuglevel\n");
70   printf("\t-B broadcast address  the address to use for broadcasts\n");
71   printf("\t-U unicast   address  the address to use for unicast\n");
72   printf("\t-M                    searches for a master browser\n");
73   printf("\t-R                    set recursion desired in packet\n");
74   printf("\t-S                    lookup node status as well\n");
75   printf("\t-T                    translate IP addresses into names\n");
76   printf("\t-r                    Use root port 137 (Win95 only replies to this)\n");
77   printf("\t-A                    Do a node status on <name> as an IP Address\n");
78   printf("\t-i NetBIOS scope      Use the given NetBIOS scope for name queries\n");
79   printf("\t-s smb.conf file      Use the given path to the smb.conf file\n");
80   printf("\t-h                    Print this help message.\n");
81   printf("\n  If you specify -M and name is \"-\", nmblookup looks up __MSBROWSE__<01>\n");
82   printf("\n");
83 }
84
85
86 /****************************************************************************
87 send out one query
88 ****************************************************************************/
89 static BOOL query_one(char *lookup, unsigned int lookup_type)
90 {
91         int j, count;
92         struct in_addr *ip_list=NULL;
93
94         if (got_bcast) {
95                 printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
96                 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
97                                      use_bcast?True:recursion_desired,
98                                      bcast_addr,&count,NULL);
99         } else {
100                 struct in_addr *bcast;
101                 for (j=iface_count() - 1;
102                      !ip_list && j >= 0;
103                      j--) {
104                         bcast = iface_n_bcast(j);
105                         printf("querying %s on %s\n", 
106                                lookup, inet_ntoa(*bcast));
107                         ip_list = name_query(ServerFD,lookup,lookup_type,
108                                              use_bcast,
109                                              use_bcast?True:recursion_desired,
110                                              *bcast,&count,NULL);
111                 }
112         }
113
114         if (!ip_list) return False;
115
116         for (j=0;j<count;j++) {
117                 if (translate_addresses) {
118                         struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
119                         if (host) {
120                                 printf("%s, ", host -> h_name);
121                         }
122                 }
123                 printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
124         }
125
126         /* We can only do find_status if the ip address returned
127            was valid - ie. name_query returned true.
128         */
129         if (find_status) {
130                 printf("Looking up status of %s\n",inet_ntoa(ip_list[0]));
131                 name_status(ServerFD,lookup,lookup_type,True,ip_list[0],NULL,NULL,NULL);
132                 printf("\n");
133         }
134
135         return (ip_list != NULL);
136 }
137
138
139 /****************************************************************************
140   main program
141 ****************************************************************************/
142 int main(int argc,char *argv[])
143 {
144   int opt;
145   unsigned int lookup_type = 0x0;
146   pstring lookup;
147   extern int optind;
148   extern char *optarg;
149   BOOL find_master=False;
150   int i;
151   static pstring servicesf = CONFIGFILE;
152   BOOL lookup_by_ip = False;
153   int commandline_debuglevel = -2;
154
155   DEBUGLEVEL = 1;
156   *lookup = 0;
157
158   TimeInit();
159
160   setup_logging(argv[0],True);
161
162   charset_initialise();
163
164   while ((opt = getopt(argc, argv, "d:B:U:i:s:SMrhART")) != EOF)
165     switch (opt)
166       {
167       case 'B':
168         bcast_addr = *interpret_addr2(optarg);
169         got_bcast = True;
170         use_bcast = True;
171         break;
172       case 'U':
173         bcast_addr = *interpret_addr2(optarg);
174         got_bcast = True;
175         use_bcast = False;
176         break;
177       case 'T':
178         translate_addresses = !translate_addresses;
179         break;
180       case 'i':
181         fstrcpy(scope,optarg);
182         strupper(scope);
183         break;
184       case 'M':
185         find_master = True;
186         break;
187       case 'S':
188         find_status = True;
189         break;
190       case 'R':
191         recursion_desired = True;
192         break;
193       case 'd':
194         commandline_debuglevel = DEBUGLEVEL = atoi(optarg);
195         break;
196       case 's':
197         pstrcpy(servicesf, optarg);
198         break;
199       case 'r':
200         RootPort = -1;
201         break;
202       case 'h':
203         usage();
204         exit(0);
205         break;
206       case 'A':
207         lookup_by_ip = True;
208         break;
209       default:
210         usage();
211         exit(1);
212       }
213
214   if (argc < 2) {
215     usage();
216     exit(1);
217   }
218
219   if (!lp_load(servicesf,True,False,False)) {
220     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
221   }
222
223   /*
224    * Ensure we reset DEBUGLEVEL if someone specified it
225    * on the command line.
226    */
227
228   if(commandline_debuglevel != -2)
229     DEBUGLEVEL = commandline_debuglevel;
230
231   load_interfaces();
232   if (!open_sockets()) return(1);
233
234   for (i=optind;i<argc;i++)
235   {
236       char *p;
237       struct in_addr ip;
238
239       fstrcpy(lookup,argv[i]);
240
241       if(lookup_by_ip)
242       {
243         fstrcpy(lookup,"*");
244         ip = *interpret_addr2(argv[i]);
245         printf("Looking up status of %s\n",inet_ntoa(ip));
246         name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
247         printf("\n");
248         continue;
249       }
250
251       if (find_master) {
252         if (*lookup == '-') {
253           fstrcpy(lookup,"\01\02__MSBROWSE__\02");
254           lookup_type = 1;
255         } else {
256           lookup_type = 0x1d;
257         }
258       }
259
260       p = strchr(lookup,'#');
261
262       if (p) {
263         *p = 0;
264         sscanf(p+1,"%x",&lookup_type);
265       }
266
267       if (!query_one(lookup, lookup_type)) {
268               printf("name_query failed to find name %s\n", lookup);
269       }
270   }
271   
272   return(0);
273 }