localhost.c 7.6 KB

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