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