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