1
0

dnsdmain.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * dnsdmain.c: DNS domain guessing stuff moved out of dns.c because of the
  43. * string ball problems
  44. */
  45. #include "netsite.h"
  46. #include <string.h>
  47. #include <stdio.h>
  48. #ifdef XP_UNIX
  49. #include <unistd.h>
  50. #endif
  51. #include <ctype.h>
  52. #include "util.h"
  53. /* Under NT, this is taken care of by net.h including winsock.h */
  54. #ifdef XP_UNIX
  55. #include <netdb.h> /* struct hostent */
  56. #endif
  57. extern "C" {
  58. #include <nspr.h>
  59. }
  60. /* This is contained in resolv.h on most systems. */
  61. #define _PATH_RESCONF "/etc/resolv.conf"
  62. #ifdef XP_UNIX
  63. NSPR_BEGIN_EXTERN_C
  64. #ifdef Linux
  65. extern int getdomainname(char *, size_t);
  66. #else
  67. extern int getdomainname(char *, int);
  68. #endif /* Linux */
  69. #if defined(HPUX) || defined (UnixWare) || defined(Linux) || defined(IRIX6_5) || defined(SOLARIS_GCC)
  70. extern int gethostname (char *name, size_t namelen);
  71. #else
  72. #ifndef AIX
  73. extern int gethostname (char *name, int namelen);
  74. #endif
  75. #endif
  76. NSPR_END_EXTERN_C
  77. #endif
  78. /* ---------------------------- dns_guess_domain -------------------------- */
  79. extern "C" NSAPI_PUBLIC char *dns_guess_domain(char * hname)
  80. {
  81. FILE *f;
  82. char * cp;
  83. int hnlen;
  84. char line[256];
  85. static int dnlen = 0;
  86. static char * domain = 0;
  87. PRHostEnt hent;
  88. char buf[PR_NETDB_BUF_SIZE];
  89. PRStatus err;
  90. /* Sanity check */
  91. if (strchr(hname, '.')) {
  92. return STRDUP(hname);
  93. }
  94. if (dnlen == 0) {
  95. /* First try a little trick that seems to often work... */
  96. /*
  97. * Get the local host name, even it doesn't come back
  98. * fully qualified.
  99. */
  100. line[0] = 0;
  101. gethostname(line, sizeof(line));
  102. if (line[0] != 0) {
  103. /* Is it fully qualified? */
  104. domain = strchr(line, '.');
  105. if (domain == 0) {
  106. /* No, try gethostbyname() */
  107. err = PR_GetHostByName(line,
  108. buf,
  109. PR_NETDB_BUF_SIZE,
  110. &hent);
  111. if (err == PR_SUCCESS) {
  112. /* See if h_name is fully-qualified */
  113. if (hent.h_name) domain = strchr(hent.h_name, '.');
  114. /* Otherwise look for a fully qualified alias */
  115. if ((domain == 0) &&
  116. (hent.h_aliases && hent.h_aliases[0])) {
  117. char **p;
  118. for (p = hent.h_aliases; *p; ++p) {
  119. domain = strchr(*p, '.');
  120. if (domain) break;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. /* Still no luck? */
  127. if (domain == 0) {
  128. f = fopen(_PATH_RESCONF, "r");
  129. /* See if there's a domain entry in their resolver configuration */
  130. if(f) {
  131. while(fgets(line, sizeof(line), f)) {
  132. if(!strncasecmp(line, "domain ", 7)) {
  133. for (cp = &line[7]; *cp && isspace(*cp); ++cp) ;
  134. if (*cp) {
  135. domain = cp;
  136. for (; *cp && !isspace(*cp); ++cp) ;
  137. *cp = 0;
  138. }
  139. break;
  140. }
  141. }
  142. fclose(f);
  143. }
  144. }
  145. #ifndef NO_DOMAINNAME
  146. if (domain == 0) {
  147. /* No domain found. Try getdomainname. */
  148. getdomainname(line, sizeof(line));
  149. if (line[0] != 0) domain = &line[0];
  150. }
  151. #endif
  152. if (domain != 0) {
  153. if (domain[0] == '.') ++domain;
  154. domain = STRDUP(domain);
  155. dnlen = strlen(domain);
  156. }
  157. else dnlen = -1;
  158. }
  159. if (domain != 0) {
  160. hnlen = strlen(hname);
  161. if ((size_t)(hnlen + dnlen + 2) <= sizeof(line)) {
  162. strcpy(line, hname);
  163. line[hnlen] = '.';
  164. strcpy(&line[hnlen+1], domain);
  165. return STRDUP(line);
  166. }
  167. }
  168. return 0;
  169. }