f920106f7532fad06363b3e0c8f919ee6c7e3c14
[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 int DEBUGLEVEL;
28
29 extern pstring scope;
30
31 extern pstring myhostname;
32 extern struct in_addr ipzero;
33
34 int ServerFD= -1;
35
36 BOOL RootPort = False;
37
38 /****************************************************************************
39   open the socket communication
40   **************************************************************************/
41 static BOOL open_sockets(void)
42 {
43         if (RootPort)
44         {
45           ServerFD = open_socket_in( SOCK_DGRAM,
46                                      137,
47                                      3,
48                                      interpret_addr(lp_socket_address()) );
49
50           if (ServerFD == -1)
51         {
52             return(False);
53         }
54
55           set_socket_options(ServerFD,"SO_BROADCAST");
56   DEBUG(3, ("Socket opened.\n"));
57         }
58   return True;
59 }
60
61
62 /****************************************************************************
63   initialise connect, service and file structs
64 ****************************************************************************/
65 static BOOL init_structs(void )
66 {
67   if (!get_myname(myhostname,NULL))
68     return(False);
69
70   return True;
71 }
72
73 /****************************************************************************
74 usage on the program
75 ****************************************************************************/
76 static void usage(void)
77 {
78   printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
79   printf("Version %s\n",VERSION);
80   printf("\t-d debuglevel         set the debuglevel\n");
81   printf("\t-B broadcast address  the address to use for broadcasts\n");
82   printf("\t-U unicast   address  the address to use for unicast\n");
83   printf("\t-M                    searches for a master browser\n");
84   printf("\t-R                    set recursion desired in packet\n");
85   printf("\t-S                    lookup node status as well\n");
86   printf("\t-r                    Use root port 137 (Win95 only replies to this)\n");
87   printf("\t-A                    Do a node status on <name> as an IP Address\n");
88   printf("\t-i NetBIOS scope      Use the given NetBIOS scope for name queries\n");
89   printf("\t-s smb.conf file      Use the given path to the smb.conf file\n");
90   printf("\t-h                    Print this help message.\n");
91   printf("\n");
92 }
93
94
95 /****************************************************************************
96   main program
97 ****************************************************************************/
98 int main(int argc,char *argv[])
99 {
100   int opt;
101   unsigned int lookup_type = 0x0;
102   pstring lookup;
103   extern int optind;
104   extern char *optarg;
105   BOOL find_master=False;
106   BOOL find_status=False;
107   int i;
108   static pstring servicesf = CONFIGFILE;
109   struct in_addr bcast_addr;
110   BOOL use_bcast = True;
111   BOOL got_bcast = False;
112   BOOL lookup_by_ip = False;
113   BOOL recursion_desired = False;
114
115   DEBUGLEVEL = 1;
116   *lookup = 0;
117
118   TimeInit();
119
120   setup_logging(argv[0],True);
121
122   charset_initialise();
123
124   while ((opt = getopt(argc, argv, "d:B:U:i:s:SMrhAR")) != EOF)
125     switch (opt)
126       {
127       case 'B':
128         iface_set_default(NULL,optarg,NULL);
129         bcast_addr = *interpret_addr2(optarg);
130         got_bcast = True;
131         use_bcast = True;
132         break;
133       case 'U':
134         iface_set_default(NULL,optarg,NULL);
135         bcast_addr = *interpret_addr2(optarg);
136         got_bcast = True;
137         use_bcast = False;
138         break;
139       case 'i':
140         fstrcpy(scope,optarg);
141         strupper(scope);
142         break;
143       case 'M':
144         find_master = True;
145         break;
146       case 'S':
147         find_status = True;
148         break;
149       case 'R':
150         recursion_desired = True;
151         break;
152       case 'd':
153         DEBUGLEVEL = atoi(optarg);
154         break;
155       case 's':
156         pstrcpy(servicesf, optarg);
157         break;
158       case 'r':
159         RootPort = True;
160         break;
161       case 'h':
162         usage();
163         exit(0);
164         break;
165       case 'A':
166         lookup_by_ip = True;
167         break;
168       default:
169         usage();
170         exit(1);
171       }
172
173   if (argc < 2) {
174     usage();
175     exit(1);
176   }
177
178   init_structs();
179
180   if (!lp_load(servicesf,True,False,False)) {
181     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
182   }
183
184   load_interfaces();
185   if (!open_sockets()) return(1);
186
187   if (!got_bcast)
188     bcast_addr = *iface_bcast(ipzero);
189
190   DEBUG(1,("Sending queries to %s\n",inet_ntoa(bcast_addr)));
191
192
193   for (i=optind;i<argc;i++)
194   {
195       int j, count;
196       char *p;
197       struct in_addr ip;
198       struct in_addr *ip_list;
199
200       fstrcpy(lookup,argv[i]);
201
202       if(lookup_by_ip)
203       {
204         fstrcpy(lookup,"*");
205         ip = *interpret_addr2(argv[i]);
206         printf("Looking up status of %s\n",inet_ntoa(ip));
207         name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
208         printf("\n");
209         continue;
210       }
211
212       if (find_master) {
213         if (*lookup == '-') {
214           fstrcpy(lookup,"\01\02__MSBROWSE__\02");
215           lookup_type = 1;
216         } else {
217           lookup_type = 0x1d;
218         }
219       }
220
221       p = strchr(lookup,'#');
222
223       if (p) {
224         *p = 0;
225         sscanf(p+1,"%x",&lookup_type);
226       }
227
228       if ((ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
229                                 use_bcast?True:recursion_desired,
230                                 bcast_addr,&count,NULL))) {
231               for (j=0;j<count;j++)
232                       printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
233               
234               /* We can only do find_status if the ip address returned
235                  was valid - ie. name_query returned true.
236                  */
237               if (find_status) {
238                       printf("Looking up status of %s\n",inet_ntoa(ip_list[0]));
239                       name_status(ServerFD,lookup,lookup_type,True,ip_list[0],NULL,NULL,NULL);
240                       printf("\n");
241               }
242       } else {
243               printf("name_query failed to find name %s\n", lookup);
244       }
245   }
246   
247   return(0);
248 }