Add start of IPv6 implementation. Currently most of this is avoiding
[vlendec/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    Copyright (C) Jelmer Vernooij 2003 (Conversion to popt)
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 3 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, see <http://www.gnu.org/licenses/>.
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 in_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 turn a node status flags field into a string
57 ****************************************************************************/
58 static char *node_status_flags(unsigned char flags)
59 {
60         static fstring ret;
61         fstrcpy(ret,"");
62         
63         fstrcat(ret, (flags & 0x80) ? "<GROUP> " : "        ");
64         if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
65         if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
66         if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
67         if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
68         if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
69         if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
70         if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
71         if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
72         
73         return ret;
74 }
75
76 /****************************************************************************
77 turn the NMB Query flags into a string
78 ****************************************************************************/
79 static char *query_flags(int flags)
80 {
81         static fstring ret1;
82         fstrcpy(ret1, "");
83
84         if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
85         if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
86         if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
87         if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
88         if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
89         if (flags & NM_FLAGS_B)  fstrcat(ret1, "Broadcast ");
90
91         return ret1;
92 }
93
94 /****************************************************************************
95 do a node status query
96 ****************************************************************************/
97 static void do_node_status(int fd, const char *name, int type, struct in_addr ip)
98 {
99         struct nmb_name nname;
100         int count, i, j;
101         NODE_STATUS_STRUCT *status;
102         struct node_status_extra extra;
103         fstring cleanname;
104
105         d_printf("Looking up status of %s\n",inet_ntoa(ip));
106         make_nmb_name(&nname, name, type);
107         status = node_status_query(fd,&nname,ip, &count, &extra);
108         if (status) {
109                 for (i=0;i<count;i++) {
110                         pull_ascii_fstring(cleanname, status[i].name);
111                         for (j=0;cleanname[j];j++) {
112                                 if (!isprint((int)cleanname[j])) cleanname[j] = '.';
113                         }
114                         d_printf("\t%-15s <%02x> - %s\n",
115                                cleanname,status[i].type,
116                                node_status_flags(status[i].flags));
117                 }
118                 d_printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
119                                 extra.mac_addr[0], extra.mac_addr[1],
120                                 extra.mac_addr[2], extra.mac_addr[3],
121                                 extra.mac_addr[4], extra.mac_addr[5]);
122                 d_printf("\n");
123                 SAFE_FREE(status);
124         } else {
125                 d_printf("No reply from %s\n\n",inet_ntoa(ip));
126         }
127 }
128
129
130 /****************************************************************************
131 send out one query
132 ****************************************************************************/
133 static BOOL query_one(const char *lookup, unsigned int lookup_type)
134 {
135         int j, count, flags = 0;
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, &flags, NULL);
143         } else {
144                 const struct in_addr *bcast;
145                 for (j=iface_count() - 1;
146                      !ip_list && j >= 0;
147                      j--) {
148                         bcast = iface_n_bcast_v4(j);
149                         if (!bcast) {
150                                 continue;
151                         }
152                         d_printf("querying %s on %s\n", 
153                                lookup, inet_ntoa(*bcast));
154                         ip_list = name_query(ServerFD,lookup,lookup_type,
155                                              use_bcast,
156                                              use_bcast?True:recursion_desired,
157                                              *bcast,&count, &flags, NULL);
158                 }
159         }
160
161         if (!ip_list) return False;
162
163         if (give_flags)
164           d_printf("Flags: %s\n", query_flags(flags));
165
166         for (j=0;j<count;j++) {
167                 if (translate_addresses) {
168                         struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
169                         if (host) {
170                                 d_printf("%s, ", host -> h_name);
171                         }
172                 }
173                 d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
174                 /* We can only do find_status if the ip address returned
175                    was valid - ie. name_query returned true.
176                  */
177                 if (find_status) {
178                         do_node_status(ServerFD, lookup, lookup_type, ip_list[j]);
179                 }
180         }
181
182         safe_free(ip_list);
183
184         return (ip_list != NULL);
185 }
186
187
188 /****************************************************************************
189   main program
190 ****************************************************************************/
191 int main(int argc,char *argv[])
192 {
193   int opt;
194   unsigned int lookup_type = 0x0;
195   fstring lookup;
196   static BOOL find_master=False;
197   static BOOL lookup_by_ip = False;
198   poptContext pc;
199   TALLOC_CTX *frame = talloc_stackframe();
200
201   struct poptOption long_options[] = {
202           POPT_AUTOHELP
203           { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
204           { "flags", 'f', POPT_ARG_VAL, &give_flags, True, "List the NMB flags returned" },
205           { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" },
206           { "master-browser", 'M', POPT_ARG_VAL, &find_master, True, "Search for a master browser" },
207           { "recursion", 'R', POPT_ARG_VAL, &recursion_desired, True, "Set recursion desired in package" },
208           { "status", 'S', POPT_ARG_VAL, &find_status, True, "Lookup node status as well" },
209           { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" },
210           { "root-port", 'r', POPT_ARG_VAL, &RootPort, True, "Use root port 137 (Win95 only replies to this)" },
211           { "lookup-by-ip", 'A', POPT_ARG_VAL, &lookup_by_ip, True, "Do a node status on <name> as an IP Address" },
212           POPT_COMMON_SAMBA
213           POPT_COMMON_CONNECTION
214           { 0, 0, 0, 0 }
215   };
216         
217   *lookup = 0;
218
219   load_case_tables();
220
221   setup_logging(argv[0],True);
222
223   pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options, 
224                                           POPT_CONTEXT_KEEP_FIRST);
225
226   poptSetOtherOptionHelp(pc, "<NODE> ...");
227
228   while ((opt = poptGetNextOpt(pc)) != -1) {
229           switch (opt) {
230           case 'B':
231                   bcast_addr = *interpret_addr2(poptGetOptArg(pc));
232                   got_bcast = True;
233                   use_bcast = True;
234                   break;
235           case 'U':
236                   bcast_addr = *interpret_addr2(poptGetOptArg(pc));
237                   got_bcast = True;
238                   use_bcast = False;
239                   break;
240           case 'T':
241                   translate_addresses = !translate_addresses;
242                   break;
243           }
244   }
245
246   poptGetArg(pc); /* Remove argv[0] */
247
248   if(!poptPeekArg(pc)) { 
249           poptPrintUsage(pc, stderr, 0);
250           exit(1);
251   }
252
253   if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
254           fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
255   }
256
257   load_interfaces();
258   if (!open_sockets()) return(1);
259
260   while(poptPeekArg(pc))
261   {
262           char *p;
263           struct in_addr ip;
264
265           fstrcpy(lookup,poptGetArg(pc));
266
267           if(lookup_by_ip)
268           {
269                   ip = *interpret_addr2(lookup);
270                   fstrcpy(lookup,"*");
271                   do_node_status(ServerFD, lookup, lookup_type, ip);
272                   continue;
273           }
274
275           if (find_master) {
276                   if (*lookup == '-') {
277                           fstrcpy(lookup,"\01\02__MSBROWSE__\02");
278                           lookup_type = 1;
279                   } else {
280                           lookup_type = 0x1d;
281                   }
282           }
283
284           p = strchr_m(lookup,'#');
285           if (p) {
286                   *p = '\0';
287                   sscanf(++p,"%x",&lookup_type);
288           }
289
290           if (!query_one(lookup, lookup_type)) {
291                   d_printf( "name_query failed to find name %s", lookup );
292                   if( 0 != lookup_type )
293                           d_printf( "#%02x", lookup_type );
294                   d_printf( "\n" );
295           }
296   }
297
298   poptFreeContext(pc);
299   TALLOC_FREE(frame);
300   return(0);
301 }