namequery.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. name query routines
  5. Copyright (C) Andrew Tridgell 1994-1998
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include "includes.h"
  19. extern pstring scope;
  20. extern int DEBUGLEVEL;
  21. /* nmbd.c sets this to True. */
  22. const BOOL global_in_nmbd = False;
  23. #if 0
  24. /****************************************************************************
  25. interpret a node status response
  26. ****************************************************************************/
  27. static void _interpret_node_status(char *p, char *master,char *rname)
  28. {
  29. int numnames = CVAL(p,0);
  30. DEBUG(1,("received %d names\n",numnames));
  31. if (rname) *rname = 0;
  32. if (master) *master = 0;
  33. p += 1;
  34. while (numnames--)
  35. {
  36. char qname[17];
  37. int type;
  38. fstring flags;
  39. int i;
  40. *flags = 0;
  41. StrnCpy(qname,p,15);
  42. type = CVAL(p,15);
  43. p += 16;
  44. fstrcat(flags, (p[0] & 0x80) ? "<GROUP> " : " ");
  45. if ((p[0] & 0x60) == 0x00) fstrcat(flags,"B ");
  46. if ((p[0] & 0x60) == 0x20) fstrcat(flags,"P ");
  47. if ((p[0] & 0x60) == 0x40) fstrcat(flags,"M ");
  48. if ((p[0] & 0x60) == 0x60) fstrcat(flags,"H ");
  49. if (p[0] & 0x10) fstrcat(flags,"<DEREGISTERING> ");
  50. if (p[0] & 0x08) fstrcat(flags,"<CONFLICT> ");
  51. if (p[0] & 0x04) fstrcat(flags,"<ACTIVE> ");
  52. if (p[0] & 0x02) fstrcat(flags,"<PERMANENT> ");
  53. if (master && !*master && type == 0x1d) {
  54. StrnCpy(master,qname,15);
  55. trim_string(master,NULL," ");
  56. }
  57. if (rname && !*rname && type == 0x20 && !(p[0]&0x80)) {
  58. StrnCpy(rname,qname,15);
  59. trim_string(rname,NULL," ");
  60. }
  61. for (i = strlen( qname) ; --i >= 0 ; ) {
  62. if (!isprint((int)qname[i])) qname[i] = '.';
  63. }
  64. DEBUG(1,("\t%-15s <%02x> - %s\n",qname,type,flags));
  65. p+=2;
  66. }
  67. DEBUG(1,("num_good_sends=%d num_good_receives=%d\n",
  68. IVAL(p,20),IVAL(p,24)));
  69. }
  70. #endif /* 0 */
  71. /****************************************************************************
  72. do a netbios name query to find someones IP
  73. returns an array of IP addresses or NULL if none
  74. *count will be set to the number of addresses returned
  75. ****************************************************************************/
  76. struct in_addr *name_query(int fd,const char *name,int name_type, BOOL bcast,BOOL recurse,
  77. struct in_addr to_ip, int *count, void (*fn)(struct packet_struct *))
  78. {
  79. BOOL found=False;
  80. int i, retries = 3;
  81. int retry_time = bcast?250:2000;
  82. struct timeval tval;
  83. struct packet_struct p;
  84. struct packet_struct *p2;
  85. struct nmb_packet *nmb = &p.packet.nmb;
  86. static int name_trn_id = 0;
  87. struct in_addr *ip_list = NULL;
  88. memset((char *)&p,'\0',sizeof(p));
  89. (*count) = 0;
  90. if (!name_trn_id) name_trn_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) +
  91. ((unsigned)getpid()%(unsigned)100);
  92. name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
  93. nmb->header.name_trn_id = name_trn_id;
  94. nmb->header.opcode = 0;
  95. nmb->header.response = False;
  96. nmb->header.nm_flags.bcast = bcast;
  97. nmb->header.nm_flags.recursion_available = False;
  98. nmb->header.nm_flags.recursion_desired = recurse;
  99. nmb->header.nm_flags.trunc = False;
  100. nmb->header.nm_flags.authoritative = False;
  101. nmb->header.rcode = 0;
  102. nmb->header.qdcount = 1;
  103. nmb->header.ancount = 0;
  104. nmb->header.nscount = 0;
  105. nmb->header.arcount = 0;
  106. make_nmb_name(&nmb->question.question_name,name,name_type);
  107. nmb->question.question_type = 0x20;
  108. nmb->question.question_class = 0x1;
  109. p.ip = to_ip;
  110. p.port = NMB_PORT;
  111. p.fd = fd;
  112. p.timestamp = time(NULL);
  113. p.packet_type = NMB_PACKET;
  114. GetTimeOfDay(&tval);
  115. if (!send_packet(&p))
  116. return NULL;
  117. retries--;
  118. while (1)
  119. {
  120. struct timeval tval2;
  121. GetTimeOfDay(&tval2);
  122. if (TvalDiff(&tval,&tval2) > retry_time)
  123. {
  124. if (!retries)
  125. break;
  126. if (!found && !send_packet(&p))
  127. return NULL;
  128. GetTimeOfDay(&tval);
  129. retries--;
  130. }
  131. if ((p2=receive_packet(fd,NMB_PACKET,90)))
  132. {
  133. struct nmb_packet *nmb2 = &p2->packet.nmb;
  134. debug_nmb_packet(p2);
  135. if (nmb->header.name_trn_id != nmb2->header.name_trn_id ||
  136. !nmb2->header.response)
  137. {
  138. /*
  139. * Its not for us - maybe deal with it later
  140. * (put it on the queue?).
  141. */
  142. if (fn)
  143. fn(p2);
  144. else
  145. free_packet(p2);
  146. continue;
  147. }
  148. if (nmb2->header.opcode != 0 ||
  149. nmb2->header.nm_flags.bcast ||
  150. nmb2->header.rcode ||
  151. !nmb2->header.ancount)
  152. {
  153. /*
  154. * XXXX what do we do with this? Could be a redirect, but
  155. * we'll discard it for the moment.
  156. */
  157. free_packet(p2);
  158. continue;
  159. }
  160. ip_list = (struct in_addr *)Realloc(ip_list, sizeof(ip_list[0]) *
  161. ((*count)+nmb2->answers->rdlength/6));
  162. if (ip_list)
  163. {
  164. DEBUG(fn?3:2,("Got a positive name query response from %s ( ",
  165. inet_ntoa(p2->ip)));
  166. for (i=0;i<nmb2->answers->rdlength/6;i++)
  167. {
  168. putip((char *)&ip_list[(*count)],&nmb2->answers->rdata[2+i*6]);
  169. DEBUG(fn?3:2,("%s ",inet_ntoa(ip_list[(*count)])));
  170. (*count)++;
  171. }
  172. DEBUG(fn?3:2,(")\n"));
  173. }
  174. found=True;
  175. retries=0;
  176. free_packet(p2);
  177. if (fn)
  178. break;
  179. /*
  180. * If we're doing a unicast lookup we only
  181. * expect one reply. Don't wait the full 2
  182. * seconds if we got one. JRA.
  183. */
  184. if(!bcast && found)
  185. break;
  186. }
  187. }
  188. return ip_list;
  189. }
  190. /********************************************************
  191. Start parsing the lmhosts file.
  192. *********************************************************/
  193. FILE *startlmhosts(const char *fname)
  194. {
  195. FILE *fp = sys_fopen(fname,"r");
  196. if (!fp) {
  197. DEBUG(4,("startlmhosts: Cannot open lmhosts file %s. Error was %s\n",
  198. fname, strerror(errno)));
  199. return NULL;
  200. }
  201. return fp;
  202. }
  203. /********************************************************
  204. Parse the next line in the lmhosts file.
  205. *********************************************************/
  206. BOOL getlmhostsent( FILE *fp, pstring name, int *name_type, struct in_addr *ipaddr)
  207. {
  208. pstring line;
  209. while(!feof(fp) && !ferror(fp)) {
  210. pstring ip,flags,extra;
  211. char *ptr;
  212. int count = 0;
  213. *name_type = -1;
  214. if (!fgets_slash(line,sizeof(pstring),fp))
  215. continue;
  216. if (*line == '#')
  217. continue;
  218. pstrcpy(ip,"");
  219. pstrcpy(name,"");
  220. pstrcpy(flags,"");
  221. ptr = line;
  222. if (next_token(&ptr,ip ,NULL,sizeof(ip)))
  223. ++count;
  224. if (next_token(&ptr,name ,NULL, sizeof(pstring)))
  225. ++count;
  226. if (next_token(&ptr,flags,NULL, sizeof(flags)))
  227. ++count;
  228. if (next_token(&ptr,extra,NULL, sizeof(extra)))
  229. ++count;
  230. if (count <= 0)
  231. continue;
  232. if (count > 0 && count < 2)
  233. {
  234. DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n",line));
  235. continue;
  236. }
  237. if (count >= 4)
  238. {
  239. DEBUG(0,("getlmhostsent: too many columns in lmhosts file (obsolete syntax)\n"));
  240. continue;
  241. }
  242. DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n", ip, name, flags));
  243. if (strchr(flags,'G') || strchr(flags,'S'))
  244. {
  245. DEBUG(0,("getlmhostsent: group flag in lmhosts ignored (obsolete)\n"));
  246. continue;
  247. }
  248. *ipaddr = *interpret_addr2(ip);
  249. /* Extra feature. If the name ends in '#XX', where XX is a hex number,
  250. then only add that name type. */
  251. if((ptr = strchr(name, '#')) != NULL)
  252. {
  253. char *endptr;
  254. ptr++;
  255. *name_type = (int)strtol(ptr, &endptr, 16);
  256. if(!*ptr || (endptr == ptr))
  257. {
  258. DEBUG(0,("getlmhostsent: invalid name %s containing '#'.\n", name));
  259. continue;
  260. }
  261. *(--ptr) = '\0'; /* Truncate at the '#' */
  262. }
  263. return True;
  264. }
  265. return False;
  266. }
  267. /********************************************************
  268. Finish parsing the lmhosts file.
  269. *********************************************************/
  270. void endlmhosts(FILE *fp)
  271. {
  272. fclose(fp);
  273. }
  274. /********************************************************
  275. resolve via "bcast" method
  276. *********************************************************/
  277. static BOOL resolve_bcast(const char *name, struct in_addr *return_ip, int name_type)
  278. {
  279. int sock, i;
  280. /*
  281. * "bcast" means do a broadcast lookup on all the local interfaces.
  282. */
  283. DEBUG(3,("resolve_name: Attempting broadcast lookup for name %s<0x%x>\n", name, name_type));
  284. sock = open_socket_in( SOCK_DGRAM, 0, 3,
  285. interpret_addr(lp_socket_address()), True );
  286. if (sock != -1) {
  287. struct in_addr *iplist = NULL;
  288. int count;
  289. int num_interfaces = iface_count();
  290. static char so_broadcast[] = "SO_BROADCAST";
  291. set_socket_options(sock, so_broadcast);
  292. /*
  293. * Lookup the name on all the interfaces, return on
  294. * the first successful match.
  295. */
  296. for( i = 0; i < num_interfaces; i++) {
  297. struct in_addr sendto_ip;
  298. /* Done this way to fix compiler error on IRIX 5.x */
  299. sendto_ip = *iface_bcast(*iface_n_ip(i));
  300. iplist = name_query(sock, name, name_type, True,
  301. True, sendto_ip, &count, NULL);
  302. if(iplist != NULL) {
  303. *return_ip = iplist[0];
  304. free((char *)iplist);
  305. close(sock);
  306. return True;
  307. }
  308. }
  309. close(sock);
  310. }
  311. return False;
  312. }
  313. /********************************************************
  314. resolve via "wins" method
  315. *********************************************************/
  316. static BOOL resolve_wins(const char *name, struct in_addr *return_ip, int name_type)
  317. {
  318. int sock;
  319. struct in_addr wins_ip;
  320. BOOL wins_ismyip;
  321. /*
  322. * "wins" means do a unicast lookup to the WINS server.
  323. * Ignore if there is no WINS server specified or if the
  324. * WINS server is one of our interfaces (if we're being
  325. * called from within nmbd - we can't do this call as we
  326. * would then block).
  327. */
  328. DEBUG(3,("resolve_name: Attempting wins lookup for name %s<0x%x>\n", name, name_type));
  329. if(!*lp_wins_server()) {
  330. DEBUG(3,("resolve_name: WINS server resolution selected and no WINS server present.\n"));
  331. return False;
  332. }
  333. wins_ip = *interpret_addr2(lp_wins_server());
  334. wins_ismyip = ismyip(wins_ip);
  335. if((wins_ismyip && !global_in_nmbd) || !wins_ismyip) {
  336. sock = open_socket_in( SOCK_DGRAM, 0, 3,
  337. interpret_addr(lp_socket_address()), True );
  338. if (sock != -1) {
  339. struct in_addr *iplist = NULL;
  340. int count;
  341. iplist = name_query(sock, name, name_type, False,
  342. True, wins_ip, &count, NULL);
  343. if(iplist != NULL) {
  344. *return_ip = iplist[0];
  345. free((char *)iplist);
  346. close(sock);
  347. return True;
  348. }
  349. close(sock);
  350. }
  351. }
  352. return False;
  353. }
  354. /********************************************************
  355. resolve via "lmhosts" method
  356. *********************************************************/
  357. static BOOL resolve_lmhosts(const char *name, struct in_addr *return_ip, int name_type)
  358. {
  359. /*
  360. * "lmhosts" means parse the local lmhosts file.
  361. */
  362. FILE *fp;
  363. pstring lmhost_name;
  364. int name_type2;
  365. DEBUG(3,("resolve_name: Attempting lmhosts lookup for name %s<0x%x>\n", name, name_type));
  366. fp = startlmhosts( LMHOSTSFILE );
  367. if(fp) {
  368. while (getlmhostsent(fp, lmhost_name, &name_type2, return_ip)) {
  369. if (strequal(name, lmhost_name) &&
  370. ((name_type2 == -1) || (name_type == name_type2))
  371. ) {
  372. endlmhosts(fp);
  373. return True;
  374. }
  375. }
  376. endlmhosts(fp);
  377. }
  378. return False;
  379. }
  380. /********************************************************
  381. resolve via "hosts" method
  382. *********************************************************/
  383. static BOOL resolve_hosts(const char *name, struct in_addr *return_ip)
  384. {
  385. /*
  386. * "host" means do a localhost, or dns lookup.
  387. */
  388. struct hostent *hp;
  389. DEBUG(3,("resolve_name: Attempting host lookup for name %s<0x20>\n", name));
  390. if (((hp = Get_Hostbyname(name)) != NULL) && (hp->h_addr != NULL)) {
  391. putip((char *)return_ip,(char *)hp->h_addr);
  392. return True;
  393. }
  394. return False;
  395. }
  396. /********************************************************
  397. Resolve a name into an IP address. Use this function if
  398. the string is either an IP address, DNS or host name
  399. or NetBIOS name. This uses the name switch in the
  400. smb.conf to determine the order of name resolution.
  401. *********************************************************/
  402. BOOL resolve_name(const char *name, struct in_addr *return_ip, int name_type)
  403. {
  404. int i;
  405. BOOL pure_address = True;
  406. pstring name_resolve_list;
  407. fstring tok;
  408. char *ptr;
  409. if (strcmp(name,"0.0.0.0") == 0) {
  410. return_ip->s_addr = 0;
  411. return True;
  412. }
  413. if (strcmp(name,"255.255.255.255") == 0) {
  414. return_ip->s_addr = 0xFFFFFFFF;
  415. return True;
  416. }
  417. for (i=0; pure_address && name[i]; i++)
  418. if (!(isdigit((int)name[i]) || name[i] == '.'))
  419. pure_address = False;
  420. /* if it's in the form of an IP address then get the lib to interpret it */
  421. if (pure_address) {
  422. return_ip->s_addr = inet_addr(name);
  423. return True;
  424. }
  425. pstrcpy(name_resolve_list, lp_name_resolve_order());
  426. if (name_resolve_list == NULL || *name_resolve_list == '\0')
  427. pstrcpy(name_resolve_list, "host");
  428. ptr = name_resolve_list;
  429. while (next_token(&ptr, tok, LIST_SEP, sizeof(tok))) {
  430. if((strequal(tok, "host") || strequal(tok, "hosts"))) {
  431. if (name_type == 0x20 && resolve_hosts(name, return_ip)) {
  432. return True;
  433. }
  434. } else if(strequal( tok, "lmhosts")) {
  435. if (resolve_lmhosts(name, return_ip, name_type)) {
  436. return True;
  437. }
  438. } else if(strequal( tok, "wins")) {
  439. /* don't resolve 1D via WINS */
  440. if (name_type != 0x1D &&
  441. resolve_wins(name, return_ip, name_type)) {
  442. return True;
  443. }
  444. } else if(strequal( tok, "bcast")) {
  445. if (resolve_bcast(name, return_ip, name_type)) {
  446. return True;
  447. }
  448. } else {
  449. DEBUG(0,("resolve_name: unknown name switch type %s\n", tok));
  450. }
  451. }
  452. return False;
  453. }
  454. #if 0
  455. /********************************************************
  456. find the IP address of the master browser or DMB for a workgroup
  457. *********************************************************/
  458. BOOL find_master_ip(char *group, struct in_addr *master_ip)
  459. {
  460. if (resolve_name(group, master_ip, 0x1D)) return True;
  461. return resolve_name(group, master_ip, 0x1B);
  462. }
  463. #endif /* 0 */