hostname.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. //usage:#define hostname_trivial_usage
  13. //usage: "[OPTIONS] [HOSTNAME | -F FILE]"
  14. //usage:#define hostname_full_usage "\n\n"
  15. //usage: "Get or set hostname or DNS domain name\n"
  16. //usage: "\n -s Short"
  17. //usage: "\n -i Addresses for the hostname"
  18. //usage: "\n -d DNS domain name"
  19. //usage: "\n -f Fully qualified domain name"
  20. //usage: "\n -F FILE Use FILE's content as hostname"
  21. //usage:
  22. //usage:#define hostname_example_usage
  23. //usage: "$ hostname\n"
  24. //usage: "sage\n"
  25. //usage:
  26. //usage:#define dnsdomainname_trivial_usage NOUSAGE_STR
  27. //usage:#define dnsdomainname_full_usage ""
  28. #include "libbb.h"
  29. static void do_sethostname(char *s, int isfile)
  30. {
  31. // if (!s)
  32. // return;
  33. if (isfile) {
  34. parser_t *parser = config_open2(s, xfopen_for_read);
  35. while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
  36. do_sethostname(s, 0);
  37. }
  38. if (ENABLE_FEATURE_CLEAN_UP)
  39. config_close(parser);
  40. } else if (sethostname(s, strlen(s))) {
  41. // if (errno == EPERM)
  42. // bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
  43. bb_perror_msg_and_die("sethostname");
  44. }
  45. }
  46. /* Manpage circa 2009:
  47. *
  48. * hostname [-v] [-a] [--alias] [-d] [--domain] [-f] [--fqdn] [--long]
  49. * [-i] [--ip-address] [-s] [--short] [-y] [--yp] [--nis]
  50. *
  51. * hostname [-v] [-F filename] [--file filename] / [hostname]
  52. *
  53. * domainname [-v] [-F filename] [--file filename] / [name]
  54. * { bbox: not supported }
  55. *
  56. * nodename [-v] [-F filename] [--file filename] / [name]
  57. * { bbox: not supported }
  58. *
  59. * dnsdomainname [-v]
  60. * { bbox: supported: Linux kernel build needs this }
  61. * nisdomainname [-v]
  62. * { bbox: not supported }
  63. * ypdomainname [-v]
  64. * { bbox: not supported }
  65. *
  66. * -a, --alias
  67. * Display the alias name of the host (if used).
  68. * { bbox: not supported }
  69. * -d, --domain
  70. * Display the name of the DNS domain. Don't use the command
  71. * domainname to get the DNS domain name because it will show the
  72. * NIS domain name and not the DNS domain name. Use dnsdomainname
  73. * instead.
  74. * -f, --fqdn, --long
  75. * Display the FQDN (Fully Qualified Domain Name). A FQDN consists
  76. * of a short host name and the DNS domain name. Unless you are
  77. * using bind or NIS for host lookups you can change the FQDN and
  78. * the DNS domain name (which is part of the FQDN) in the
  79. * /etc/hosts file.
  80. * -i, --ip-address
  81. * Display the IP address(es) of the host.
  82. * -s, --short
  83. * Display the short host name. This is the host name cut at the
  84. * first dot.
  85. * -v, --verbose
  86. * Be verbose and tell what's going on.
  87. * { bbox: supported but ignored }
  88. * -y, --yp, --nis
  89. * Display the NIS domain name. If a parameter is given (or --file
  90. * name ) then root can also set a new NIS domain.
  91. * { bbox: not supported }
  92. * -F, --file filename
  93. * Read the host name from the specified file. Comments (lines
  94. * starting with a `#') are ignored.
  95. */
  96. int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  97. int hostname_main(int argc UNUSED_PARAM, char **argv)
  98. {
  99. enum {
  100. OPT_d = 0x1,
  101. OPT_f = 0x2,
  102. OPT_i = 0x4,
  103. OPT_s = 0x8,
  104. OPT_F = 0x10,
  105. OPT_dfis = 0xf,
  106. };
  107. unsigned opts;
  108. char *buf;
  109. char *hostname_str;
  110. #if ENABLE_LONG_OPTS
  111. applet_long_options =
  112. "domain\0" No_argument "d"
  113. "fqdn\0" No_argument "f"
  114. //Enable if seen in active use in some distro:
  115. // "long\0" No_argument "f"
  116. // "ip-address\0" No_argument "i"
  117. // "short\0" No_argument "s"
  118. // "verbose\0" No_argument "v"
  119. "file\0" No_argument "F"
  120. ;
  121. #endif
  122. /* dnsdomainname from net-tools 1.60, hostname 1.100 (2001-04-14),
  123. * supports hostname's options too (not just -v as manpage says) */
  124. opts = getopt32(argv, "dfisF:v", &hostname_str);
  125. argv += optind;
  126. buf = safe_gethostname();
  127. if (applet_name[0] == 'd') /* dnsdomainname? */
  128. opts = OPT_d;
  129. if (opts & OPT_dfis) {
  130. /* Cases when we need full hostname (or its part) */
  131. struct hostent *hp;
  132. char *p;
  133. hp = xgethostbyname(buf);
  134. p = strchrnul(hp->h_name, '.');
  135. if (opts & OPT_f) {
  136. puts(hp->h_name);
  137. } else if (opts & OPT_s) {
  138. *p = '\0';
  139. puts(hp->h_name);
  140. } else if (opts & OPT_d) {
  141. if (*p)
  142. puts(p + 1);
  143. } else /*if (opts & OPT_i)*/ {
  144. if (hp->h_length == sizeof(struct in_addr)) {
  145. struct in_addr **h_addr_list = (struct in_addr **)hp->h_addr_list;
  146. while (*h_addr_list) {
  147. printf(h_addr_list[1] ? "%s " : "%s", inet_ntoa(**h_addr_list));
  148. h_addr_list++;
  149. }
  150. bb_putchar('\n');
  151. }
  152. }
  153. } else if (opts & OPT_F) {
  154. /* Set the hostname */
  155. do_sethostname(hostname_str, 1);
  156. } else if (argv[0]) {
  157. /* Set the hostname */
  158. do_sethostname(argv[0], 0);
  159. } else {
  160. /* Just print the current hostname */
  161. puts(buf);
  162. }
  163. if (ENABLE_FEATURE_CLEAN_UP)
  164. free(buf);
  165. return EXIT_SUCCESS;
  166. }