Patch from metze and me that adds dummy smb_register_*() functions so
[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    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 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 #include "module_dummy.h"
28
29 extern BOOL AllowDebugChange;
30
31 static BOOL give_flags = False;
32 static BOOL use_bcast = True;
33 static BOOL got_bcast = False;
34 static struct in_addr bcast_addr;
35 static BOOL recursion_desired = False;
36 static BOOL translate_addresses = False;
37 static int ServerFD= -1;
38 static int RootPort = False;
39 static BOOL find_status=False;
40
41 /****************************************************************************
42   open the socket communication
43   **************************************************************************/
44 static BOOL open_sockets(void)
45 {
46   ServerFD = open_socket_in( SOCK_DGRAM,
47                              (RootPort ? 137 : 0),
48                              (RootPort ?   0 : 3),
49                              interpret_addr(lp_socket_address()), True );
50
51   if (ServerFD == -1)
52     return(False);
53
54   set_socket_options( ServerFD, "SO_BROADCAST" );
55
56   DEBUG(3, ("Socket opened.\n"));
57   return True;
58 }
59
60 /****************************************************************************
61 turn a node status flags field into a string
62 ****************************************************************************/
63 static char *node_status_flags(unsigned char flags)
64 {
65         static fstring ret;
66         fstrcpy(ret,"");
67         
68         fstrcat(ret, (flags & 0x80) ? "<GROUP> " : "        ");
69         if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
70         if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
71         if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
72         if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
73         if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
74         if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
75         if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
76         if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
77         
78         return ret;
79 }
80
81 /****************************************************************************
82 turn the NMB Query flags into a string
83 ****************************************************************************/
84 static char *query_flags(int flags)
85 {
86         static fstring ret1;
87         fstrcpy(ret1, "");
88
89         if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
90         if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
91         if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
92         if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
93         if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
94         if (flags & NM_FLAGS_B)  fstrcat(ret1, "Broadcast ");
95
96         return ret1;
97 }
98
99 /****************************************************************************
100 do a node status query
101 ****************************************************************************/
102 static void do_node_status(int fd, const char *name, int type, struct in_addr ip)
103 {
104         struct nmb_name nname;
105         int count, i, j;
106         struct node_status *status;
107         fstring cleanname;
108
109         d_printf("Looking up status of %s\n",inet_ntoa(ip));
110         make_nmb_name(&nname, name, type);
111         status = node_status_query(fd,&nname,ip, &count);
112         if (status) {
113                 for (i=0;i<count;i++) {
114                         fstrcpy(cleanname, status[i].name);
115                         for (j=0;cleanname[j];j++) {
116                                 if (!isprint((int)cleanname[j])) cleanname[j] = '.';
117                         }
118                         d_printf("\t%-15s <%02x> - %s\n",
119                                cleanname,status[i].type,
120                                node_status_flags(status[i].flags));
121                 }
122                 SAFE_FREE(status);
123         }
124         d_printf("\n");
125 }
126
127
128 /****************************************************************************
129 send out one query
130 ****************************************************************************/
131 static BOOL query_one(const char *lookup, unsigned int lookup_type)
132 {
133         int j, count, flags = 0;
134         struct in_addr *ip_list=NULL;
135
136         if (got_bcast) {
137                 d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
138                 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
139                                      use_bcast?True:recursion_desired,
140                                      bcast_addr,&count, &flags, NULL);
141         } else {
142                 struct in_addr *bcast;
143                 for (j=iface_count() - 1;
144                      !ip_list && j >= 0;
145                      j--) {
146                         bcast = iface_n_bcast(j);
147                         d_printf("querying %s on %s\n", 
148                                lookup, inet_ntoa(*bcast));
149                         ip_list = name_query(ServerFD,lookup,lookup_type,
150                                              use_bcast,
151                                              use_bcast?True:recursion_desired,
152                                              *bcast,&count, &flags, NULL);
153                 }
154         }
155
156         if (!ip_list) return False;
157
158         if (give_flags)
159           d_printf("Flags: %s\n", query_flags(flags));
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   fstring lookup;
192   static BOOL find_master=False;
193   static BOOL lookup_by_ip = False;
194   poptContext pc;
195
196   struct poptOption long_options[] = {
197           POPT_AUTOHELP
198           { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
199           { "flags", 'f', POPT_ARG_VAL, &give_flags, True, "List the NMB flags returned" },
200           { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" },
201           { "master-browser", 'M', POPT_ARG_VAL, &find_master, True, "Search for a master browser" },
202           { "recursion", 'R', POPT_ARG_VAL, &recursion_desired, True, "Set recursion desired in package" },
203           { "status", 'S', POPT_ARG_VAL, &find_status, True, "Lookup node status as well" },
204           { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" },
205           { "root-port", 'r', POPT_ARG_VAL, &RootPort, True, "Use root port 137 (Win95 only replies to this)" },
206           { "lookup-by-ip", 'A', POPT_ARG_VAL, &lookup_by_ip, True, "Do a node status on <name> as an IP Address" },
207           POPT_COMMON_SAMBA
208           POPT_COMMON_CONNECTION
209           { 0, 0, 0, 0 }
210   };
211         
212   *lookup = 0;
213
214   setup_logging(argv[0],True);
215
216   pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options, 
217                                           POPT_CONTEXT_KEEP_FIRST);
218
219   poptSetOtherOptionHelp(pc, "<NODE> ...");
220
221   while ((opt = poptGetNextOpt(pc)) != -1) {
222           switch (opt) {
223           case 'B':
224                   bcast_addr = *interpret_addr2(poptGetOptArg(pc));
225                   got_bcast = True;
226                   use_bcast = True;
227                   break;
228           case 'U':
229                   bcast_addr = *interpret_addr2(poptGetOptArg(pc));
230                   got_bcast = True;
231                   use_bcast = False;
232                   break;
233           case 'T':
234                   translate_addresses = !translate_addresses;
235                   break;
236           }
237   }
238
239   poptGetArg(pc); /* Remove argv[0] */
240
241   if(!poptPeekArg(pc)) { 
242           poptPrintUsage(pc, stderr, 0);
243           exit(1);
244   }
245
246   if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
247           fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
248   }
249
250   load_interfaces();
251   if (!open_sockets()) return(1);
252
253   while(poptPeekArg(pc))
254   {
255           char *p;
256           struct in_addr ip;
257
258           fstrcpy(lookup,poptGetArg(pc));
259
260           if(lookup_by_ip)
261           {
262                   ip = *interpret_addr2(lookup);
263                   fstrcpy(lookup,"*");
264                   do_node_status(ServerFD, lookup, lookup_type, ip);
265                   continue;
266           }
267
268           if (find_master) {
269                   if (*lookup == '-') {
270                           fstrcpy(lookup,"\01\02__MSBROWSE__\02");
271                           lookup_type = 1;
272                   } else {
273                           lookup_type = 0x1d;
274                   }
275           }
276
277           p = strchr_m(lookup,'#');
278           if (p) {
279                   *p = '\0';
280                   sscanf(++p,"%x",&lookup_type);
281           }
282
283           if (!query_one(lookup, lookup_type)) {
284                   d_printf( "name_query failed to find name %s", lookup );
285                   if( 0 != lookup_type )
286                           d_printf( "#%02x", lookup_type );
287                   d_printf( "\n" );
288           }
289   }
290
291   poptFreeContext(pc);
292
293   return(0);
294 }