localhost.c 7.6 KB

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