r3443: the next stage in the include files re-organisation.
[samba.git] / source4 / 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 #include "includes.h"
23
24 extern BOOL AllowDebugChange;
25
26 static BOOL give_flags = False;
27 static BOOL use_bcast = True;
28 static BOOL got_bcast = False;
29 static struct ipv4_addr bcast_addr;
30 static BOOL recursion_desired = False;
31 static BOOL translate_addresses = False;
32 static int ServerFD= -1;
33 static int RootPort = False;
34 static BOOL find_status=False;
35
36 /****************************************************************************
37   open the socket communication
38   **************************************************************************/
39 static BOOL open_sockets(void)
40 {
41   ServerFD = open_socket_in( SOCK_DGRAM,
42                              (RootPort ? 137 : 0),
43                              (RootPort ?   0 : 3),
44                              interpret_addr(lp_socket_address()), True );
45
46   if (ServerFD == -1)
47     return(False);
48
49   set_socket_options( ServerFD, "SO_BROADCAST" );
50
51   DEBUG(3, ("Socket opened.\n"));
52   return True;
53 }
54
55
56 /****************************************************************************
57 usage on the program
58 ****************************************************************************/
59 static void usage(void)
60 {
61   d_printf("Usage: nmblookup [options] name\n");
62   d_printf("Version %s\n",VERSION);
63   d_printf("\t-d debuglevel         set the debuglevel\n");
64   d_printf("\t-B broadcast address  the address to use for broadcasts\n");
65   d_printf("\t-f                    list the NMB flags returned\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(uint8_t 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 turn the NMB Query flags into a string
103 ****************************************************************************/
104 static char *query_flags(int flags)
105 {
106         static fstring ret1;
107         fstrcpy(ret1, "");
108
109         if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
110         if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
111         if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
112         if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
113         if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
114         if (flags & NM_FLAGS_B)  fstrcat(ret1, "Broadcast ");
115
116         return ret1;
117 }
118
119 /****************************************************************************
120 do a node status query
121 ****************************************************************************/
122 static void do_node_status(int fd, const char *name, int type, struct ipv4_addr ip)
123 {
124         struct nmb_name nname;
125         int count, i, j;
126         struct node_status *status;
127         fstring cleanname;
128
129         d_printf("Looking up status of %s\n",inet_ntoa(ip));
130         make_nmb_name(&nname, name, type);
131         status = node_status_query(fd,&nname,ip, &count);
132         if (status) {
133                 for (i=0;i<count;i++) {
134                         fstrcpy(cleanname, status[i].name);
135                         for (j=0;cleanname[j];j++) {
136                                 if (!isprint((int)cleanname[j])) cleanname[j] = '.';
137                         }
138                         d_printf("\t%-15s <%02x> - %s\n",
139                                cleanname,status[i].type,
140                                node_status_flags(status[i].flags));
141                 }
142                 SAFE_FREE(status);
143         }
144         d_printf("\n");
145 }
146
147
148 /****************************************************************************
149 send out one query
150 ****************************************************************************/
151 static BOOL query_one(const char *lookup, uint_t lookup_type)
152 {
153         int j, count, flags = 0;
154         struct ipv4_addr *ip_list=NULL;
155
156         if (got_bcast) {
157                 d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
158                 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
159                                      use_bcast?True:recursion_desired,
160                                      bcast_addr,&count, &flags, NULL);
161         } else {
162                 struct ipv4_addr *bcast;
163                 for (j=iface_count() - 1;
164                      !ip_list && j >= 0;
165                      j--) {
166                         bcast = iface_n_bcast(j);
167                         d_printf("querying %s on %s\n", 
168                                lookup, inet_ntoa(*bcast));
169                         ip_list = name_query(ServerFD,lookup,lookup_type,
170                                              use_bcast,
171                                              use_bcast?True:recursion_desired,
172                                              *bcast,&count, &flags, NULL);
173                 }
174         }
175
176         if (!ip_list) return False;
177
178         if (give_flags)
179           d_printf("Flags: %s\n", query_flags(flags));
180
181         for (j=0;j<count;j++) {
182                 if (translate_addresses) {
183                         struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
184                         if (host) {
185                                 d_printf("%s, ", host -> h_name);
186                         }
187                 }
188                 d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
189         }
190
191         /* We can only do find_status if the ip address returned
192            was valid - ie. name_query returned true.
193         */
194         if (find_status) {
195                 do_node_status(ServerFD, lookup, lookup_type, ip_list[0]);
196         }
197
198         safe_free(ip_list);
199
200         return (ip_list != NULL);
201 }
202
203
204 /****************************************************************************
205   main program
206 ****************************************************************************/
207 int main(int argc,char *argv[])
208 {
209   int opt;
210   uint_t lookup_type = 0x0;
211   fstring lookup;
212   extern int optind;
213   extern char *optarg;
214   BOOL find_master=False;
215   int i;
216   BOOL lookup_by_ip = False;
217   int commandline_debuglevel = -2;
218
219   DEBUGLEVEL = 1;
220   /* Prevent smb.conf setting from overridding */
221   AllowDebugChange = False;
222
223   *lookup = 0;
224
225   setup_logging(argv[0], DEBUG_STDOUT);
226
227   while ((opt = getopt(argc, argv, "d:fB:U:i:s:SMrhART")) != EOF)
228     switch (opt)
229       {
230       case 'B':
231         bcast_addr = interpret_addr2(optarg);
232         got_bcast = True;
233         use_bcast = True;
234         break;
235       case 'f':
236         give_flags = True;
237         break;
238       case 'U':
239         bcast_addr = interpret_addr2(optarg);
240         got_bcast = True;
241         use_bcast = False;
242         break;
243       case 'T':
244         translate_addresses = !translate_addresses;
245         break;
246       case 'i':
247               lp_set_cmdline("netbios scope", optarg);
248         break;
249       case 'M':
250         find_master = True;
251         break;
252       case 'S':
253         find_status = True;
254         break;
255       case 'R':
256         recursion_desired = True;
257         break;
258       case 'd':
259         commandline_debuglevel = DEBUGLEVEL = atoi(optarg);
260         break;
261       case 's':
262         pstrcpy(dyn_CONFIGFILE, optarg);
263         break;
264       case 'r':
265         RootPort = True;
266         break;
267       case 'h':
268         usage();
269         exit(0);
270         break;
271       case 'A':
272         lookup_by_ip = True;
273         break;
274       default:
275         usage();
276         exit(1);
277       }
278
279   if (argc < 2) {
280     usage();
281     exit(1);
282   }
283
284   if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
285     fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
286   }
287
288   /*
289    * Ensure we reset DEBUGLEVEL if someone specified it
290    * on the command line.
291    */
292
293   if(commandline_debuglevel != -2)
294     DEBUGLEVEL = commandline_debuglevel;
295
296   load_interfaces();
297   if (!open_sockets()) return(1);
298
299   for (i=optind;i<argc;i++)
300   {
301       char *p;
302       struct ipv4_addr ip;
303
304       fstrcpy(lookup,argv[i]);
305
306       if(lookup_by_ip)
307       {
308         fstrcpy(lookup,"*");
309         ip = interpret_addr2(argv[i]);
310         do_node_status(ServerFD, lookup, lookup_type, ip);
311         continue;
312       }
313
314       if (find_master) {
315         if (*lookup == '-') {
316           fstrcpy(lookup,"\01\02__MSBROWSE__\02");
317           lookup_type = 1;
318         } else {
319           lookup_type = 0x1d;
320         }
321       }
322
323       p = strchr_m(lookup,'#');
324       if (p) {
325         *p = '\0';
326         sscanf(++p,"%x",&lookup_type);
327       }
328
329       if (!query_one(lookup, lookup_type)) {
330         d_printf( "name_query failed to find name %s", lookup );
331         if( 0 != lookup_type )
332           d_printf( "#%02x", lookup_type );
333         d_printf( "\n" );
334       }
335   }
336   
337   return(0);
338 }