dnsdmain.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * dnsdmain.c: DNS domain guessing stuff moved out of dns.c because of the
  14. * string ball problems
  15. */
  16. #include "netsite.h"
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <ctype.h>
  21. #include "util.h"
  22. #include <netdb.h> /* struct hostent */
  23. extern "C" {
  24. #include <nspr.h>
  25. }
  26. /* This is contained in resolv.h on most systems. */
  27. #define _PATH_RESCONF "/etc/resolv.conf"
  28. NSPR_BEGIN_EXTERN_C
  29. #ifdef Linux
  30. extern int getdomainname(char *, size_t);
  31. #else
  32. extern int getdomainname(char *, int);
  33. #endif /* Linux */
  34. #if defined(HPUX) || defined(Linux) || defined(SOLARIS_GCC)
  35. extern int gethostname (char *name, size_t namelen);
  36. #else
  37. extern int gethostname (char *name, int namelen);
  38. #endif
  39. NSPR_END_EXTERN_C
  40. /* ---------------------------- dns_guess_domain -------------------------- */
  41. extern "C" NSAPI_PUBLIC char *dns_guess_domain(char * hname)
  42. {
  43. FILE *f;
  44. char * cp;
  45. int hnlen;
  46. char line[256];
  47. static int dnlen = 0;
  48. static char * domain = 0;
  49. PRHostEnt hent;
  50. char buf[PR_NETDB_BUF_SIZE];
  51. PRStatus err;
  52. /* Sanity check */
  53. if (strchr(hname, '.')) {
  54. return STRDUP(hname);
  55. }
  56. if (dnlen == 0) {
  57. /* First try a little trick that seems to often work... */
  58. /*
  59. * Get the local host name, even it doesn't come back
  60. * fully qualified.
  61. */
  62. line[0] = 0;
  63. gethostname(line, sizeof(line));
  64. if (line[0] != 0) {
  65. /* Is it fully qualified? */
  66. domain = strchr(line, '.');
  67. if (domain == 0) {
  68. /* No, try gethostbyname() */
  69. err = PR_GetHostByName(line,
  70. buf,
  71. PR_NETDB_BUF_SIZE,
  72. &hent);
  73. if (err == PR_SUCCESS) {
  74. /* See if h_name is fully-qualified */
  75. if (hent.h_name) domain = strchr(hent.h_name, '.');
  76. /* Otherwise look for a fully qualified alias */
  77. if ((domain == 0) &&
  78. (hent.h_aliases && hent.h_aliases[0])) {
  79. char **p;
  80. for (p = hent.h_aliases; *p; ++p) {
  81. domain = strchr(*p, '.');
  82. if (domain) break;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. /* Still no luck? */
  89. if (domain == 0) {
  90. f = fopen(_PATH_RESCONF, "r");
  91. /* See if there's a domain entry in their resolver configuration */
  92. if(f) {
  93. while(fgets(line, sizeof(line), f)) {
  94. if(!strncasecmp(line, "domain ", 7)) {
  95. for (cp = &line[7]; *cp && isspace(*cp); ++cp) ;
  96. if (*cp) {
  97. domain = cp;
  98. for (; *cp && !isspace(*cp); ++cp) ;
  99. *cp = 0;
  100. }
  101. break;
  102. }
  103. }
  104. fclose(f);
  105. }
  106. }
  107. #ifndef NO_DOMAINNAME
  108. if (domain == 0) {
  109. /* No domain found. Try getdomainname. */
  110. getdomainname(line, sizeof(line));
  111. if (line[0] != 0) domain = &line[0];
  112. }
  113. #endif
  114. if (domain != 0) {
  115. if (domain[0] == '.') ++domain;
  116. domain = STRDUP(domain);
  117. dnlen = strlen(domain);
  118. }
  119. else dnlen = -1;
  120. }
  121. if (domain != 0) {
  122. hnlen = strlen(hname);
  123. if ((size_t)(hnlen + dnlen + 2) <= sizeof(line)) {
  124. strcpy(line, hname);
  125. line[hnlen] = '.';
  126. strcpy(&line[hnlen+1], domain);
  127. return STRDUP(line);
  128. }
  129. }
  130. return 0;
  131. }