dnsdmain.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /*
  39. * dnsdmain.c: DNS domain guessing stuff moved out of dns.c because of the
  40. * string ball problems
  41. */
  42. #include "netsite.h"
  43. #include <string.h>
  44. #include <stdio.h>
  45. #ifdef XP_UNIX
  46. #include <unistd.h>
  47. #endif
  48. #include <ctype.h>
  49. #include "util.h"
  50. /* Under NT, this is taken care of by net.h including winsock.h */
  51. #ifdef XP_UNIX
  52. #include <netdb.h> /* struct hostent */
  53. #endif
  54. extern "C" {
  55. #include <nspr.h>
  56. }
  57. /* This is contained in resolv.h on most systems. */
  58. #define _PATH_RESCONF "/etc/resolv.conf"
  59. #ifdef XP_UNIX
  60. NSPR_BEGIN_EXTERN_C
  61. #ifdef Linux
  62. extern int getdomainname(char *, size_t);
  63. #else
  64. extern int getdomainname(char *, int);
  65. #endif /* Linux */
  66. #if defined(HPUX) || defined (UnixWare) || defined(Linux) || defined(IRIX6_5)
  67. extern int gethostname (char *name, size_t namelen);
  68. #else
  69. #ifndef AIX
  70. extern int gethostname (char *name, int namelen);
  71. #endif
  72. #endif
  73. NSPR_END_EXTERN_C
  74. #endif
  75. /* ---------------------------- dns_guess_domain -------------------------- */
  76. extern "C" NSAPI_PUBLIC char *dns_guess_domain(char * hname)
  77. {
  78. FILE *f;
  79. char * cp;
  80. int hnlen;
  81. char line[256];
  82. static int dnlen = 0;
  83. static char * domain = 0;
  84. PRHostEnt hent;
  85. char buf[PR_NETDB_BUF_SIZE];
  86. PRStatus err;
  87. /* Sanity check */
  88. if (strchr(hname, '.')) {
  89. return STRDUP(hname);
  90. }
  91. if (dnlen == 0) {
  92. /* First try a little trick that seems to often work... */
  93. /*
  94. * Get the local host name, even it doesn't come back
  95. * fully qualified.
  96. */
  97. line[0] = 0;
  98. gethostname(line, sizeof(line));
  99. if (line[0] != 0) {
  100. /* Is it fully qualified? */
  101. domain = strchr(line, '.');
  102. if (domain == 0) {
  103. /* No, try gethostbyname() */
  104. err = PR_GetHostByName(line,
  105. buf,
  106. PR_NETDB_BUF_SIZE,
  107. &hent);
  108. if (err == PR_SUCCESS) {
  109. /* See if h_name is fully-qualified */
  110. if (hent.h_name) domain = strchr(hent.h_name, '.');
  111. /* Otherwise look for a fully qualified alias */
  112. if ((domain == 0) &&
  113. (hent.h_aliases && hent.h_aliases[0])) {
  114. char **p;
  115. for (p = hent.h_aliases; *p; ++p) {
  116. domain = strchr(*p, '.');
  117. if (domain) break;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. /* Still no luck? */
  124. if (domain == 0) {
  125. f = fopen(_PATH_RESCONF, "r");
  126. /* See if there's a domain entry in their resolver configuration */
  127. if(f) {
  128. while(fgets(line, sizeof(line), f)) {
  129. if(!strncasecmp(line, "domain ", 7)) {
  130. for (cp = &line[7]; *cp && isspace(*cp); ++cp) ;
  131. if (*cp) {
  132. domain = cp;
  133. for (; *cp && !isspace(*cp); ++cp) ;
  134. *cp = 0;
  135. }
  136. break;
  137. }
  138. }
  139. fclose(f);
  140. }
  141. }
  142. #ifndef NO_DOMAINNAME
  143. if (domain == 0) {
  144. /* No domain found. Try getdomainname. */
  145. getdomainname(line, sizeof(line));
  146. if (line[0] != 0) domain = &line[0];
  147. }
  148. #endif
  149. if (domain != 0) {
  150. if (domain[0] == '.') ++domain;
  151. domain = STRDUP(domain);
  152. dnlen = strlen(domain);
  153. }
  154. else dnlen = -1;
  155. }
  156. if (domain != 0) {
  157. hnlen = strlen(hname);
  158. if ((hnlen + dnlen + 2) <= sizeof(line)) {
  159. strcpy(line, hname);
  160. line[hnlen] = '.';
  161. strcpy(&line[hnlen+1], domain);
  162. return STRDUP(line);
  163. }
  164. }
  165. return 0;
  166. }