Merge from HEAD - make Samba compile with -Wwrite-strings without additional
[nivanova/samba-autobuild/.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 give_flags = False;
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 [options] 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-f                    list the NMB flags returned\n");
68   d_printf("\t-U unicast   address  the address to use for unicast\n");
69   d_printf("\t-M                    searches for a master browser\n");
70   d_printf("\t-R                    set recursion desired in packet\n");
71   d_printf("\t-S                    lookup node status as well\n");
72   d_printf("\t-T                    translate IP addresses into names\n");
73   d_printf("\t-r                    Use root port 137 (Win95 only replies to this)\n");
74   d_printf("\t-A                    Do a node status on <name> as an IP Address\n");
75   d_printf("\t-i NetBIOS scope      Use the given NetBIOS scope for name queries\n");
76   d_printf("\t-s smb.conf file      Use the given path to the smb.conf file\n");
77   d_printf("\t-h                    Print this help message.\n");
78   d_printf("\n  If you specify -M and name is \"-\", nmblookup looks up __MSBROWSE__<01>\n");
79   d_printf("\n");
80 }
81
82 /****************************************************************************
83 turn a node status flags field into a string
84 ****************************************************************************/
85 static char *node_status_flags(unsigned char flags)
86 {
87         static fstring ret;
88         fstrcpy(ret,"");
89         
90         fstrcat(ret, (flags & 0x80) ? "<GROUP> " : "        ");
91         if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
92         if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
93         if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
94         if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
95         if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
96         if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
97         if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
98         if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
99         
100         return ret;
101 }
102
103 /****************************************************************************
104 turn the NMB Query flags into a string
105 ****************************************************************************/
106 static char *query_flags(int flags)
107 {
108         static fstring ret1;
109         fstrcpy(ret1, "");
110
111         if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
112         if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
113         if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
114         if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
115         if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
116         if (flags & NM_FLAGS_B)  fstrcat(ret1, "Broadcast ");
117
118         return ret1;
119 }
120
121 /****************************************************************************
122 do a node status query
123 ****************************************************************************/
124 static void do_node_status(int fd, const char *name, int type, struct in_addr ip)
125 {
126         struct nmb_name nname;
127         int count, i, j;
128         struct node_status *status;
129         fstring cleanname;
130
131         d_printf("Looking up status of %s\n",inet_ntoa(ip));
132         make_nmb_name(&nname, name, type);
133         status = node_status_query(fd,&nname,ip, &count);
134         if (status) {
135                 for (i=0;i<count;i++) {
136                         fstrcpy(cleanname, status[i].name);
137                         for (j=0;cleanname[j];j++) {
138                                 if (!isprint((int)cleanname[j])) cleanname[j] = '.';
139                         }
140                         d_printf("\t%-15s <%02x> - %s\n",
141                                cleanname,status[i].type,
142                                node_status_flags(status[i].flags));
143                 }
144                 SAFE_FREE(status);
145         }
146         d_printf("\n");
147 }
148
149
150 /****************************************************************************
151 send out one query
152 ****************************************************************************/
153 static BOOL query_one(const char *lookup, unsigned int lookup_type)
154 {
155         int j, count, flags = 0;
156         struct in_addr *ip_list=NULL;
157
158         if (got_bcast) {
159                 d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
160                 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
161                                      use_bcast?True:recursion_desired,
162                                      bcast_addr,&count, &flags, NULL);
163         } else {
164                 struct in_addr *bcast;
165                 for (j=iface_count() - 1;
166                      !ip_list && j >= 0;
167                      j--) {
168                         bcast = iface_n_bcast(j);
169                         d_printf("querying %s on %s\n", 
170                                lookup, inet_ntoa(*bcast));
171                         ip_list = name_query(ServerFD,lookup,lookup_type,
172                                              use_bcast,
173                                              use_bcast?True:recursion_desired,
174                                              *bcast,&count, &flags, NULL);
175                 }
176         }
177
178         if (!ip_list) return False;
179
180         if (give_flags)
181           d_printf("Flags: %s\n", query_flags(flags));
182
183         for (j=0;j<count;j++) {
184                 if (translate_addresses) {
185                         struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
186                         if (host) {
187                                 d_printf("%s, ", host -> h_name);
188                         }
189                 }
190                 d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
191         }
192
193         /* We can only do find_status if the ip address returned
194            was valid - ie. name_query returned true.
195         */
196         if (find_status) {
197                 do_node_status(ServerFD, lookup, lookup_type, ip_list[0]);
198         }
199
200         safe_free(ip_list);
201
202         return (ip_list != NULL);
203 }
204
205
206 /****************************************************************************
207   main program
208 ****************************************************************************/
209 int main(int argc,char *argv[])
210 {
211   int opt;
212   unsigned int lookup_type = 0x0;
213   pstring lookup;
214   extern int optind;
215   extern char *optarg;
216   BOOL find_master=False;
217   int i;
218   BOOL lookup_by_ip = False;
219   int commandline_debuglevel = -2;
220
221   DEBUGLEVEL = 1;
222   /* Prevent smb.conf setting from overridding */
223   AllowDebugChange = False;
224
225   *lookup = 0;
226
227   setup_logging(argv[0],True);
228
229   while ((opt = getopt(argc, argv, "d:fB:U:i:s:SMrhART")) != EOF)
230     switch (opt)
231       {
232       case 'B':
233         bcast_addr = *interpret_addr2(optarg);
234         got_bcast = True;
235         use_bcast = True;
236         break;
237       case 'f':
238         give_flags = True;
239         break;
240       case 'U':
241         bcast_addr = *interpret_addr2(optarg);
242         got_bcast = True;
243         use_bcast = False;
244         break;
245       case 'T':
246         translate_addresses = !translate_addresses;
247         break;
248       case 'i':
249         set_global_scope(optarg);
250         break;
251       case 'M':
252         find_master = True;
253         break;
254       case 'S':
255         find_status = True;
256         break;
257       case 'R':
258         recursion_desired = True;
259         break;
260       case 'd':
261         commandline_debuglevel = DEBUGLEVEL = atoi(optarg);
262         break;
263       case 's':
264         pstrcpy(dyn_CONFIGFILE, optarg);
265         break;
266       case 'r':
267         RootPort = True;
268         break;
269       case 'h':
270         usage();
271         exit(0);
272         break;
273       case 'A':
274         lookup_by_ip = True;
275         break;
276       default:
277         usage();
278         exit(1);
279       }
280
281   if (argc < 2) {
282     usage();
283     exit(1);
284   }
285
286   if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
287     fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
288   }
289
290   /*
291    * Ensure we reset DEBUGLEVEL if someone specified it
292    * on the command line.
293    */
294
295   if(commandline_debuglevel != -2)
296     DEBUGLEVEL = commandline_debuglevel;
297
298   load_interfaces();
299   if (!open_sockets()) return(1);
300
301   for (i=optind;i<argc;i++)
302   {
303       char *p;
304       struct in_addr ip;
305
306       fstrcpy(lookup,argv[i]);
307
308       if(lookup_by_ip)
309       {
310         fstrcpy(lookup,"*");
311         ip = *interpret_addr2(argv[i]);
312         do_node_status(ServerFD, lookup, lookup_type, ip);
313         continue;
314       }
315
316       if (find_master) {
317         if (*lookup == '-') {
318           fstrcpy(lookup,"\01\02__MSBROWSE__\02");
319           lookup_type = 1;
320         } else {
321           lookup_type = 0x1d;
322         }
323       }
324
325       p = strchr_m(lookup,'#');
326       if (p) {
327         *p = '\0';
328         sscanf(++p,"%x",&lookup_type);
329       }
330
331       if (!query_one(lookup, lookup_type)) {
332         d_printf( "name_query failed to find name %s", lookup );
333         if( 0 != lookup_type )
334           d_printf( "#%02x", lookup_type );
335         d_printf( "\n" );
336       }
337   }
338   
339   return(0);
340 }