PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/release/src/router/samba3/source/utils/nmblookup.c

https://gitlab.com/envieidoc/advancedtomato2
C | 298 lines | 212 code | 44 blank | 42 comment | 51 complexity | 4a717d7c55522cd574a0db2322becd20 MD5 | raw file
  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. 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. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "includes.h"
  19. extern BOOL AllowDebugChange;
  20. static BOOL give_flags = False;
  21. static BOOL use_bcast = True;
  22. static BOOL got_bcast = False;
  23. static struct in_addr bcast_addr;
  24. static BOOL recursion_desired = False;
  25. static BOOL translate_addresses = False;
  26. static int ServerFD= -1;
  27. static int RootPort = False;
  28. static BOOL find_status=False;
  29. /****************************************************************************
  30. open the socket communication
  31. **************************************************************************/
  32. static BOOL open_sockets(void)
  33. {
  34. ServerFD = open_socket_in( SOCK_DGRAM,
  35. (RootPort ? 137 : 0),
  36. (RootPort ? 0 : 3),
  37. interpret_addr(lp_socket_address()), True );
  38. if (ServerFD == -1)
  39. return(False);
  40. set_socket_options( ServerFD, "SO_BROADCAST" );
  41. DEBUG(3, ("Socket opened.\n"));
  42. return True;
  43. }
  44. /****************************************************************************
  45. turn a node status flags field into a string
  46. ****************************************************************************/
  47. static char *node_status_flags(unsigned char flags)
  48. {
  49. static fstring ret;
  50. fstrcpy(ret,"");
  51. fstrcat(ret, (flags & 0x80) ? "<GROUP> " : " ");
  52. if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
  53. if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
  54. if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
  55. if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
  56. if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
  57. if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
  58. if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
  59. if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
  60. return ret;
  61. }
  62. /****************************************************************************
  63. turn the NMB Query flags into a string
  64. ****************************************************************************/
  65. static char *query_flags(int flags)
  66. {
  67. static fstring ret1;
  68. fstrcpy(ret1, "");
  69. if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
  70. if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
  71. if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
  72. if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
  73. if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
  74. if (flags & NM_FLAGS_B) fstrcat(ret1, "Broadcast ");
  75. return ret1;
  76. }
  77. /****************************************************************************
  78. do a node status query
  79. ****************************************************************************/
  80. static void do_node_status(int fd, const char *name, int type, struct in_addr ip)
  81. {
  82. struct nmb_name nname;
  83. int count, i, j;
  84. NODE_STATUS_STRUCT *status;
  85. struct node_status_extra extra;
  86. fstring cleanname;
  87. d_printf("Looking up status of %s\n",inet_ntoa(ip));
  88. make_nmb_name(&nname, name, type);
  89. status = node_status_query(fd,&nname,ip, &count, &extra);
  90. if (status) {
  91. for (i=0;i<count;i++) {
  92. pull_ascii_fstring(cleanname, status[i].name);
  93. for (j=0;cleanname[j];j++) {
  94. if (!isprint((int)cleanname[j])) cleanname[j] = '.';
  95. }
  96. d_printf("\t%-15s <%02x> - %s\n",
  97. cleanname,status[i].type,
  98. node_status_flags(status[i].flags));
  99. }
  100. d_printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
  101. extra.mac_addr[0], extra.mac_addr[1],
  102. extra.mac_addr[2], extra.mac_addr[3],
  103. extra.mac_addr[4], extra.mac_addr[5]);
  104. d_printf("\n");
  105. SAFE_FREE(status);
  106. } else {
  107. d_printf("No reply from %s\n\n",inet_ntoa(ip));
  108. }
  109. }
  110. /****************************************************************************
  111. send out one query
  112. ****************************************************************************/
  113. static BOOL query_one(const char *lookup, unsigned int lookup_type)
  114. {
  115. int j, count, flags = 0;
  116. struct in_addr *ip_list=NULL;
  117. if (got_bcast) {
  118. d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
  119. ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
  120. use_bcast?True:recursion_desired,
  121. bcast_addr,&count, &flags, NULL);
  122. } else {
  123. struct in_addr *bcast;
  124. for (j=iface_count() - 1;
  125. !ip_list && j >= 0;
  126. j--) {
  127. bcast = iface_n_bcast(j);
  128. d_printf("querying %s on %s\n",
  129. lookup, inet_ntoa(*bcast));
  130. ip_list = name_query(ServerFD,lookup,lookup_type,
  131. use_bcast,
  132. use_bcast?True:recursion_desired,
  133. *bcast,&count, &flags, NULL);
  134. }
  135. }
  136. if (!ip_list) return False;
  137. if (give_flags)
  138. d_printf("Flags: %s\n", query_flags(flags));
  139. for (j=0;j<count;j++) {
  140. if (translate_addresses) {
  141. struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
  142. if (host) {
  143. d_printf("%s, ", host -> h_name);
  144. }
  145. }
  146. d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
  147. /* We can only do find_status if the ip address returned
  148. was valid - ie. name_query returned true.
  149. */
  150. if (find_status) {
  151. do_node_status(ServerFD, lookup, lookup_type, ip_list[j]);
  152. }
  153. }
  154. safe_free(ip_list);
  155. return (ip_list != NULL);
  156. }
  157. /****************************************************************************
  158. main program
  159. ****************************************************************************/
  160. int main(int argc,char *argv[])
  161. {
  162. int opt;
  163. unsigned int lookup_type = 0x0;
  164. fstring lookup;
  165. static BOOL find_master=False;
  166. static BOOL lookup_by_ip = False;
  167. poptContext pc;
  168. struct poptOption long_options[] = {
  169. POPT_AUTOHELP
  170. { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
  171. { "flags", 'f', POPT_ARG_VAL, &give_flags, True, "List the NMB flags returned" },
  172. { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" },
  173. { "master-browser", 'M', POPT_ARG_VAL, &find_master, True, "Search for a master browser" },
  174. { "recursion", 'R', POPT_ARG_VAL, &recursion_desired, True, "Set recursion desired in package" },
  175. { "status", 'S', POPT_ARG_VAL, &find_status, True, "Lookup node status as well" },
  176. { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" },
  177. { "root-port", 'r', POPT_ARG_VAL, &RootPort, True, "Use root port 137 (Win95 only replies to this)" },
  178. { "lookup-by-ip", 'A', POPT_ARG_VAL, &lookup_by_ip, True, "Do a node status on <name> as an IP Address" },
  179. POPT_COMMON_SAMBA
  180. POPT_COMMON_CONNECTION
  181. { 0, 0, 0, 0 }
  182. };
  183. *lookup = 0;
  184. load_case_tables();
  185. setup_logging(argv[0],True);
  186. pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options,
  187. POPT_CONTEXT_KEEP_FIRST);
  188. poptSetOtherOptionHelp(pc, "<NODE> ...");
  189. while ((opt = poptGetNextOpt(pc)) != -1) {
  190. switch (opt) {
  191. case 'B':
  192. bcast_addr = *interpret_addr2(poptGetOptArg(pc));
  193. got_bcast = True;
  194. use_bcast = True;
  195. break;
  196. case 'U':
  197. bcast_addr = *interpret_addr2(poptGetOptArg(pc));
  198. got_bcast = True;
  199. use_bcast = False;
  200. break;
  201. case 'T':
  202. translate_addresses = !translate_addresses;
  203. break;
  204. }
  205. }
  206. poptGetArg(pc); /* Remove argv[0] */
  207. if(!poptPeekArg(pc)) {
  208. poptPrintUsage(pc, stderr, 0);
  209. exit(1);
  210. }
  211. if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
  212. fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
  213. }
  214. load_interfaces();
  215. if (!open_sockets()) return(1);
  216. while(poptPeekArg(pc))
  217. {
  218. char *p;
  219. struct in_addr ip;
  220. fstrcpy(lookup,poptGetArg(pc));
  221. if(lookup_by_ip)
  222. {
  223. ip = *interpret_addr2(lookup);
  224. fstrcpy(lookup,"*");
  225. do_node_status(ServerFD, lookup, lookup_type, ip);
  226. continue;
  227. }
  228. if (find_master) {
  229. if (*lookup == '-') {
  230. fstrcpy(lookup,"\01\02__MSBROWSE__\02");
  231. lookup_type = 1;
  232. } else {
  233. lookup_type = 0x1d;
  234. }
  235. }
  236. p = strchr_m(lookup,'#');
  237. if (p) {
  238. *p = '\0';
  239. sscanf(++p,"%x",&lookup_type);
  240. }
  241. if (!query_one(lookup, lookup_type)) {
  242. d_printf( "name_query failed to find name %s", lookup );
  243. if( 0 != lookup_type )
  244. d_printf( "#%02x", lookup_type );
  245. d_printf( "\n" );
  246. }
  247. }
  248. poptFreeContext(pc);
  249. return(0);
  250. }