namequery.c 15 KB

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