hostname.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.6 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_ODDNAME:name main location suid_type help
  24. //applet:IF_DNSDOMAINNAME(APPLET_ODDNAME(dnsdomainname, hostname, BB_DIR_BIN, BB_SUID_DROP, dnsdomainname))
  25. //applet:IF_HOSTNAME(APPLET(hostname, BB_DIR_BIN, BB_SUID_DROP))
  26. //kbuild: lib-$(CONFIG_HOSTNAME) += hostname.o
  27. //kbuild: lib-$(CONFIG_DNSDOMAINNAME) += hostname.o
  28. //usage:#define hostname_trivial_usage
  29. //usage: "[OPTIONS] [HOSTNAME | -F FILE]"
  30. //usage:#define hostname_full_usage "\n\n"
  31. //usage: "Get 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_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. #if ENABLE_LONG_OPTS
  127. applet_long_options =
  128. "domain\0" No_argument "d"
  129. "fqdn\0" No_argument "f"
  130. //Enable if seen in active use in some distro:
  131. // "long\0" No_argument "f"
  132. // "ip-address\0" No_argument "i"
  133. // "short\0" No_argument "s"
  134. // "verbose\0" No_argument "v"
  135. "file\0" No_argument "F"
  136. ;
  137. #endif
  138. /* dnsdomainname from net-tools 1.60, hostname 1.100 (2001-04-14),
  139. * supports hostname's options too (not just -v as manpage says) */
  140. opts = getopt32(argv, "dfisF:v", &hostname_str);
  141. argv += optind;
  142. buf = safe_gethostname();
  143. if (ENABLE_DNSDOMAINNAME) {
  144. if (!ENABLE_HOSTNAME || applet_name[0] == 'd') {
  145. /* dnsdomainname */
  146. opts = OPT_d;
  147. }
  148. }
  149. if (opts & OPT_dfi) {
  150. /* Cases when we need full hostname (or its part) */
  151. struct hostent *hp;
  152. char *p;
  153. hp = xgethostbyname(buf);
  154. p = strchrnul(hp->h_name, '.');
  155. if (opts & OPT_f) {
  156. puts(hp->h_name);
  157. } else if (opts & OPT_s) {
  158. *p = '\0';
  159. puts(hp->h_name);
  160. } else if (opts & OPT_d) {
  161. if (*p)
  162. puts(p + 1);
  163. } else /*if (opts & OPT_i)*/ {
  164. if (hp->h_length == sizeof(struct in_addr)) {
  165. struct in_addr **h_addr_list = (struct in_addr **)hp->h_addr_list;
  166. while (*h_addr_list) {
  167. printf(h_addr_list[1] ? "%s " : "%s", inet_ntoa(**h_addr_list));
  168. h_addr_list++;
  169. }
  170. bb_putchar('\n');
  171. }
  172. }
  173. } else if (opts & OPT_s) {
  174. strchrnul(buf, '.')[0] = '\0';
  175. puts(buf);
  176. } else if (opts & OPT_F) {
  177. /* Set the hostname */
  178. do_sethostname(hostname_str, 1);
  179. } else if (argv[0]) {
  180. /* Set the hostname */
  181. do_sethostname(argv[0], 0);
  182. } else {
  183. /* Just print the current hostname */
  184. puts(buf);
  185. }
  186. if (ENABLE_FEATURE_CLEAN_UP)
  187. free(buf);
  188. return EXIT_SUCCESS;
  189. }