hostname.c 4.3 KB

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