3
0

hostname.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini hostname implementation for busybox
  4. *
  5. * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
  6. *
  7. * Adjusted by Erik Andersen <andersen@codepoet.org> to remove
  8. * use of long options and GNU getopt. Improved the usage info.
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11. */
  12. //config:config HOSTNAME
  13. //config: bool "hostname (5.5 kb)"
  14. //config: default y
  15. //config: help
  16. //config: Show or set the system's host name.
  17. //config:
  18. //config:config DNSDOMAINNAME
  19. //config: bool "dnsdomainname (3.6 kb)"
  20. //config: default y
  21. //config: help
  22. //config: Alias to "hostname -d".
  23. // APPLET_NOEXEC:name main location suid_type help
  24. //applet:IF_DNSDOMAINNAME(APPLET_NOEXEC(dnsdomainname, hostname, BB_DIR_BIN, BB_SUID_DROP, dnsdomainname))
  25. //applet:IF_HOSTNAME( APPLET_NOEXEC(hostname, hostname, BB_DIR_BIN, BB_SUID_DROP, hostname ))
  26. //kbuild: lib-$(CONFIG_HOSTNAME) += hostname.o
  27. //kbuild: lib-$(CONFIG_DNSDOMAINNAME) += hostname.o
  28. //usage:#define hostname_trivial_usage
  29. //usage: "[-sidf] [HOSTNAME | -F FILE]"
  30. //usage:#define hostname_full_usage "\n\n"
  31. //usage: "Show or set hostname or DNS domain name\n"
  32. //usage: "\n -s Short"
  33. //usage: "\n -i Addresses for the hostname"
  34. //usage: "\n -d DNS domain name"
  35. //usage: "\n -f Fully qualified domain name"
  36. //usage: "\n -F FILE Use FILE's content as hostname"
  37. //usage:
  38. //usage:#define hostname_example_usage
  39. //usage: "$ hostname\n"
  40. //usage: "sage\n"
  41. //usage:
  42. //usage:#define dnsdomainname_trivial_usage NOUSAGE_STR
  43. //usage:#define dnsdomainname_full_usage ""
  44. #include "libbb.h"
  45. static void do_sethostname(char *s, int isfile)
  46. {
  47. // if (!s)
  48. // return;
  49. if (isfile) {
  50. parser_t *parser = config_open2(s, xfopen_for_read);
  51. while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
  52. do_sethostname(s, 0);
  53. }
  54. if (ENABLE_FEATURE_CLEAN_UP)
  55. config_close(parser);
  56. } else if (sethostname(s, strlen(s))) {
  57. // if (errno == EPERM)
  58. // bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
  59. bb_simple_perror_msg_and_die("sethostname");
  60. }
  61. }
  62. /* Manpage circa 2009:
  63. *
  64. * hostname [-v] [-a] [--alias] [-d] [--domain] [-f] [--fqdn] [--long]
  65. * [-i] [--ip-address] [-s] [--short] [-y] [--yp] [--nis]
  66. *
  67. * hostname [-v] [-F filename] [--file filename] / [hostname]
  68. *
  69. * domainname [-v] [-F filename] [--file filename] / [name]
  70. * { bbox: not supported }
  71. *
  72. * nodename [-v] [-F filename] [--file filename] / [name]
  73. * { bbox: not supported }
  74. *
  75. * dnsdomainname [-v]
  76. * { bbox: supported: Linux kernel build needs this }
  77. * nisdomainname [-v]
  78. * { bbox: not supported }
  79. * ypdomainname [-v]
  80. * { bbox: not supported }
  81. *
  82. * -a, --alias
  83. * Display the alias name of the host (if used).
  84. * { bbox: not supported }
  85. * -d, --domain
  86. * Display the name of the DNS domain. Don't use the command
  87. * domainname to get the DNS domain name because it will show the
  88. * NIS domain name and not the DNS domain name. Use dnsdomainname
  89. * instead.
  90. * -f, --fqdn, --long
  91. * Display the FQDN (Fully Qualified Domain Name). A FQDN consists
  92. * of a short host name and the DNS domain name. Unless you are
  93. * using bind or NIS for host lookups you can change the FQDN and
  94. * the DNS domain name (which is part of the FQDN) in the
  95. * /etc/hosts file.
  96. * -i, --ip-address
  97. * Display the IP address(es) of the host.
  98. * -s, --short
  99. * Display the short host name. This is the host name cut at the
  100. * first dot.
  101. * -v, --verbose
  102. * Be verbose and tell what's going on.
  103. * { bbox: supported but ignored }
  104. * -y, --yp, --nis
  105. * Display the NIS domain name. If a parameter is given (or --file
  106. * name ) then root can also set a new NIS domain.
  107. * { bbox: not supported }
  108. * -F, --file filename
  109. * Read the host name from the specified file. Comments (lines
  110. * starting with a '#') are ignored.
  111. */
  112. int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  113. int hostname_main(int argc UNUSED_PARAM, char **argv)
  114. {
  115. enum {
  116. OPT_d = 0x1,
  117. OPT_f = 0x2,
  118. OPT_i = 0x4,
  119. OPT_s = 0x8,
  120. OPT_F = 0x10,
  121. OPT_dfi = 0x7,
  122. };
  123. unsigned opts;
  124. char *buf;
  125. char *hostname_str;
  126. /* dnsdomainname from net-tools 1.60, hostname 1.100 (2001-04-14),
  127. * supports hostname's options too (not just -v as manpage says) */
  128. opts = getopt32(argv, "dfisF:v", &hostname_str,
  129. "domain\0" No_argument "d"
  130. "fqdn\0" No_argument "f"
  131. //Enable if seen in active use in some distro:
  132. // "long\0" No_argument "f"
  133. // "ip-address\0" No_argument "i"
  134. // "short\0" No_argument "s"
  135. // "verbose\0" No_argument "v"
  136. "file\0" No_argument "F"
  137. );
  138. argv += optind;
  139. buf = safe_gethostname();
  140. if (ENABLE_DNSDOMAINNAME) {
  141. if (!ENABLE_HOSTNAME || applet_name[0] == 'd') {
  142. /* dnsdomainname */
  143. opts = OPT_d;
  144. }
  145. }
  146. if (opts & OPT_dfi) {
  147. /* Cases when we need full hostname (or its part) */
  148. struct hostent *hp;
  149. char *p;
  150. hp = xgethostbyname(buf);
  151. p = strchrnul(hp->h_name, '.');
  152. if (opts & OPT_f) {
  153. puts(hp->h_name);
  154. } else if (opts & OPT_s) {
  155. *p = '\0';
  156. puts(hp->h_name);
  157. } else if (opts & OPT_d) {
  158. if (*p)
  159. puts(p + 1);
  160. } else /*if (opts & OPT_i)*/ {
  161. if (hp->h_length == sizeof(struct in_addr)) {
  162. struct in_addr **h_addr_list = (struct in_addr **)hp->h_addr_list;
  163. while (*h_addr_list) {
  164. printf(h_addr_list[1] ? "%s " : "%s", inet_ntoa(**h_addr_list));
  165. h_addr_list++;
  166. }
  167. bb_putchar('\n');
  168. }
  169. }
  170. } else if (opts & OPT_s) {
  171. strchrnul(buf, '.')[0] = '\0';
  172. puts(buf);
  173. } else if (opts & OPT_F) {
  174. /* Set the hostname */
  175. do_sethostname(hostname_str, 1);
  176. } else if (argv[0]) {
  177. /* Set the hostname */
  178. do_sethostname(argv[0], 0);
  179. } else {
  180. /* Just print the current hostname */
  181. puts(buf);
  182. }
  183. if (ENABLE_FEATURE_CLEAN_UP)
  184. free(buf);
  185. return EXIT_SUCCESS;
  186. }