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