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