localhost.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include <stdio.h>
  13. #include <sys/types.h>
  14. #include <string.h>
  15. #include <sys/param.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include <netdb.h>
  20. #include <arpa/nameser.h>
  21. #include <resolv.h>
  22. #include <errno.h>
  23. #include "slap.h"
  24. #if defined(USE_SYSCONF) || defined(LINUX)
  25. #include <unistd.h>
  26. #endif /* USE_SYSCONF */
  27. #include <ssl.h>
  28. #include "fe.h"
  29. #ifndef _PATH_RESCONF /* usually defined in <resolv.h> */
  30. #define _PATH_RESCONF "/etc/resolv.conf"
  31. #endif
  32. #if defined (__hpux)
  33. #if (MAXHOSTNAMELEN < 256)
  34. # undef MAXHOSTNAMELEN
  35. # define MAXHOSTNAMELEN 256
  36. #endif
  37. #endif
  38. static char*
  39. find_localhost_DNS()
  40. {
  41. /* This implementation could (and should) be entirely replaced by:
  42. dns_ip2host ("127.0.0.1", 1); defined in ldapserver/lib/base/dns.c
  43. */
  44. char hostname [MAXHOSTNAMELEN + 1];
  45. struct hostent *hp;
  46. #ifdef GETHOSTBYNAME_BUF_T
  47. struct hostent hent;
  48. GETHOSTBYNAME_BUF_T hbuf;
  49. int err;
  50. #endif
  51. char** alias;
  52. FILE* f;
  53. char* cp;
  54. char* domain;
  55. char line [MAXHOSTNAMELEN + 8];
  56. if (gethostname (hostname, MAXHOSTNAMELEN)) {
  57. int oserr = errno;
  58. LDAPDebug (LDAP_DEBUG_ANY, "gethostname() failed, error %d (%s)\n",
  59. oserr, slapd_system_strerror( oserr ), 0 );
  60. return NULL;
  61. }
  62. hp = GETHOSTBYNAME (hostname, &hent, hbuf, sizeof(hbuf), &err);
  63. if (hp == NULL) {
  64. int oserr = errno;
  65. LDAPDebug( LDAP_DEBUG_ANY,
  66. "gethostbyname(\"%s\") failed, error %d (%s)\n",
  67. hostname, oserr, slapd_system_strerror( oserr ));
  68. return NULL;
  69. }
  70. if (hp->h_name == NULL) {
  71. LDAPDebug (LDAP_DEBUG_ANY, "gethostbyname(\"%s\")->h_name == NULL\n", hostname, 0, 0);
  72. return NULL;
  73. }
  74. if (strchr (hp->h_name, '.') != NULL) {
  75. LDAPDebug (LDAP_DEBUG_CONFIG, "h_name == %s\n", hp->h_name, 0, 0);
  76. return slapi_ch_strdup (hp->h_name);
  77. } else if (hp->h_aliases != NULL) {
  78. for (alias = hp->h_aliases; *alias != NULL; ++alias) {
  79. if (strchr (*alias, '.') != NULL &&
  80. strncmp (*alias, hp->h_name, strlen (hp->h_name))) {
  81. LDAPDebug (LDAP_DEBUG_CONFIG, "h_alias == %s\n", *alias, 0, 0);
  82. return slapi_ch_strdup (*alias);
  83. }
  84. }
  85. }
  86. /* The following is copied from dns_guess_domain(),
  87. in ldapserver/lib/base/dnsdmain.c */
  88. domain = NULL;
  89. f = fopen (_PATH_RESCONF, "r"); /* This fopen() will fail on NT, as expected */
  90. if (f != NULL) {
  91. while (fgets (line, sizeof(line), f)) {
  92. if (strncasecmp (line, "domain", 6) == 0 && isspace (line[6])) {
  93. LDAPDebug (LDAP_DEBUG_CONFIG, "%s: %s\n", _PATH_RESCONF, line, 0);
  94. for (cp = &line[7]; *cp && isspace(*cp); ++cp);
  95. if (*cp) {
  96. domain = cp;
  97. /* ignore subsequent whitespace: */
  98. for (; *cp && ! isspace (*cp); ++cp);
  99. if (*cp) {
  100. *cp = '\0';
  101. }
  102. }
  103. break;
  104. }
  105. }
  106. fclose (f);
  107. }
  108. #ifndef NO_DOMAINNAME
  109. if (domain == NULL) {
  110. /* No domain found. Try getdomainname. */
  111. getdomainname (line, sizeof(line));
  112. LDAPDebug (LDAP_DEBUG_CONFIG, "getdomainname(%s)\n", line, 0, 0);
  113. if (line[0] != 0) {
  114. domain = &line[0];
  115. }
  116. }
  117. #endif
  118. if (domain == NULL) {
  119. return NULL;
  120. }
  121. PL_strncpyz (hostname, hp->h_name, sizeof(hostname));
  122. if (domain[0] == '.') ++domain;
  123. if (domain[0]) {
  124. PL_strcatn (hostname, sizeof(hostname), ".");
  125. PL_strcatn (hostname, sizeof(hostname), domain);
  126. }
  127. LDAPDebug (LDAP_DEBUG_CONFIG, "hostname == %s\n", hostname, 0, 0);
  128. return slapi_ch_strdup (hostname);
  129. }
  130. static const char* const RDN = "dc=";
  131. static char*
  132. convert_DNS_to_DN (char* DNS)
  133. {
  134. char* DN;
  135. char* dot;
  136. size_t components;
  137. if (*DNS == '\0') {
  138. return slapi_ch_strdup ("");
  139. }
  140. components = 1;
  141. for (dot = strchr (DNS, '.'); dot != NULL; dot = strchr (dot + 1, '.')) {
  142. ++components;
  143. }
  144. DN = slapi_ch_malloc (strlen (DNS) + (components * strlen(RDN)) + 1);
  145. strcpy (DN, RDN);
  146. for (dot = strchr (DNS, '.'); dot != NULL; dot = strchr (dot + 1, '.')) {
  147. *dot = '\0';
  148. strcat (DN, DNS);
  149. strcat (DN, ",");
  150. strcat (DN, RDN);
  151. DNS = dot + 1;
  152. *dot = '.';
  153. }
  154. strcat (DN, DNS);
  155. slapi_dn_normalize (DN);
  156. return DN;
  157. }
  158. static char* localhost_DN = NULL;
  159. char*
  160. get_localhost_DNS()
  161. {
  162. char *retVal;
  163. if ( (retVal = config_get_localhost()) == NULL) {
  164. /* find_localhost_DNS() returns strdup result */
  165. retVal = find_localhost_DNS();
  166. }
  167. return retVal;
  168. }
  169. static void
  170. set_localhost_DN()
  171. {
  172. char *localhost_DNS = config_get_localhost();
  173. if (localhost_DNS != NULL) {
  174. localhost_DN = convert_DNS_to_DN (localhost_DNS);
  175. LDAPDebug (LDAP_DEBUG_CONFIG, "DNS %s -> DN %s\n", localhost_DNS, localhost_DN, 0);
  176. }
  177. slapi_ch_free_string(&localhost_DNS);
  178. }
  179. char*
  180. get_localhost_DN()
  181. /* Return the Distinguished Name of the local host; that is,
  182. its DNS name converted to a DN according to RFC 1279.
  183. The caller should _not_ free this pointer. */
  184. {
  185. if (localhost_DN == NULL) {
  186. set_localhost_DN();
  187. }
  188. return localhost_DN;
  189. }
  190. static char* config_DN = NULL;
  191. char *
  192. get_config_DN()
  193. {
  194. char *c;
  195. char *host;
  196. if ( config_DN == NULL )
  197. {
  198. host = get_localhost_DN();
  199. if ( host )
  200. c = slapi_ch_malloc (20 + strlen (host));
  201. else {
  202. LDAPDebug (LDAP_DEBUG_CONFIG, "get_locahost_DN() returned \"\"\n",
  203. 0, 0, 0);
  204. c = slapi_ch_malloc (20);
  205. }
  206. sprintf (c, "cn=ldap://%s:%d", host ? host : "", config_get_port());
  207. config_DN = c;
  208. }
  209. return config_DN;
  210. }