blob: 248d8b65a7f9bb7c70ebd0046e906e21bc713ccd [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen485b9551999-12-07 23:14:59 +00002/*
Eric Andersen485b9551999-12-07 23:14:59 +00003 * Mini hostname implementation for busybox
4 *
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
6 *
Denys Vlasenkod30b89c2009-06-26 01:55:45 +02007 * Adjusted by Erik Andersen <andersen@codepoet.org> to remove
Eric Andersend29edf31999-12-08 04:13:44 +00008 * use of long options and GNU getopt. Improved the usage info.
9 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen485b9551999-12-07 23:14:59 +000011 */
Denys Vlasenko67fd2f22016-11-13 22:35:10 +010012//config:config HOSTNAME
Denys Vlasenkob097a842018-12-28 03:20:17 +010013//config: bool "hostname (5.5 kb)"
Denys Vlasenko67fd2f22016-11-13 22:35:10 +010014//config: default y
15//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: Show or set the system's host name.
Denys Vlasenko67fd2f22016-11-13 22:35:10 +010017//config:
18//config:config DNSDOMAINNAME
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020019//config: bool "dnsdomainname (3.6 kb)"
Denys Vlasenko67fd2f22016-11-13 22:35:10 +010020//config: default y
21//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020022//config: Alias to "hostname -d".
Denys Vlasenko67fd2f22016-11-13 22:35:10 +010023
Denys Vlasenkoed4393b2017-08-07 20:34:26 +020024// APPLET_NOEXEC:name main location suid_type help
25//applet:IF_DNSDOMAINNAME(APPLET_NOEXEC(dnsdomainname, hostname, BB_DIR_BIN, BB_SUID_DROP, dnsdomainname))
26//applet:IF_HOSTNAME( APPLET_NOEXEC(hostname, hostname, BB_DIR_BIN, BB_SUID_DROP, hostname ))
Denys Vlasenko67fd2f22016-11-13 22:35:10 +010027
28//kbuild: lib-$(CONFIG_HOSTNAME) += hostname.o
29//kbuild: lib-$(CONFIG_DNSDOMAINNAME) += hostname.o
30
Pere Orga5bc8c002011-04-11 03:29:49 +020031//usage:#define hostname_trivial_usage
32//usage: "[OPTIONS] [HOSTNAME | -F FILE]"
33//usage:#define hostname_full_usage "\n\n"
34//usage: "Get or set hostname or DNS domain name\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020035//usage: "\n -s Short"
36//usage: "\n -i Addresses for the hostname"
37//usage: "\n -d DNS domain name"
38//usage: "\n -f Fully qualified domain name"
39//usage: "\n -F FILE Use FILE's content as hostname"
40//usage:
41//usage:#define hostname_example_usage
42//usage: "$ hostname\n"
43//usage: "sage\n"
44//usage:
45//usage:#define dnsdomainname_trivial_usage NOUSAGE_STR
46//usage:#define dnsdomainname_full_usage ""
47
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000048#include "libbb.h"
Eric Andersen3d61b102001-10-31 09:59:57 +000049
Eric Andersen3e6ff902001-03-09 21:24:12 +000050static void do_sethostname(char *s, int isfile)
Eric Andersen485b9551999-12-07 23:14:59 +000051{
Denys Vlasenkod30b89c2009-06-26 01:55:45 +020052// if (!s)
53// return;
Denis Vlasenko5415c852008-07-21 23:05:26 +000054 if (isfile) {
55 parser_t *parser = config_open2(s, xfopen_for_read);
Denis Vlasenko084266e2008-07-26 23:08:31 +000056 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
Denis Vlasenko5415c852008-07-21 23:05:26 +000057 do_sethostname(s, 0);
Eric Andersen3d61b102001-10-31 09:59:57 +000058 }
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000059 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko5415c852008-07-21 23:05:26 +000060 config_close(parser);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +020061 } else if (sethostname(s, strlen(s))) {
62// if (errno == EPERM)
63// bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
Denis Vlasenko5415c852008-07-21 23:05:26 +000064 bb_perror_msg_and_die("sethostname");
Eric Andersen485b9551999-12-07 23:14:59 +000065 }
Eric Andersen485b9551999-12-07 23:14:59 +000066}
67
Denys Vlasenkod30b89c2009-06-26 01:55:45 +020068/* Manpage circa 2009:
69 *
Denys Vlasenko9038d6f2009-07-15 20:02:19 +020070 * hostname [-v] [-a] [--alias] [-d] [--domain] [-f] [--fqdn] [--long]
Denys Vlasenkod30b89c2009-06-26 01:55:45 +020071 * [-i] [--ip-address] [-s] [--short] [-y] [--yp] [--nis]
72 *
73 * hostname [-v] [-F filename] [--file filename] / [hostname]
74 *
75 * domainname [-v] [-F filename] [--file filename] / [name]
76 * { bbox: not supported }
77 *
78 * nodename [-v] [-F filename] [--file filename] / [name]
79 * { bbox: not supported }
80 *
81 * dnsdomainname [-v]
82 * { bbox: supported: Linux kernel build needs this }
83 * nisdomainname [-v]
84 * { bbox: not supported }
85 * ypdomainname [-v]
86 * { bbox: not supported }
87 *
88 * -a, --alias
89 * Display the alias name of the host (if used).
90 * { bbox: not supported }
91 * -d, --domain
92 * Display the name of the DNS domain. Don't use the command
93 * domainname to get the DNS domain name because it will show the
94 * NIS domain name and not the DNS domain name. Use dnsdomainname
95 * instead.
96 * -f, --fqdn, --long
97 * Display the FQDN (Fully Qualified Domain Name). A FQDN consists
98 * of a short host name and the DNS domain name. Unless you are
99 * using bind or NIS for host lookups you can change the FQDN and
100 * the DNS domain name (which is part of the FQDN) in the
101 * /etc/hosts file.
102 * -i, --ip-address
103 * Display the IP address(es) of the host.
104 * -s, --short
105 * Display the short host name. This is the host name cut at the
106 * first dot.
107 * -v, --verbose
108 * Be verbose and tell what's going on.
109 * { bbox: supported but ignored }
110 * -y, --yp, --nis
111 * Display the NIS domain name. If a parameter is given (or --file
112 * name ) then root can also set a new NIS domain.
113 * { bbox: not supported }
114 * -F, --file filename
115 * Read the host name from the specified file. Comments (lines
Denys Vlasenko95f79532017-08-02 14:26:33 +0200116 * starting with a '#') are ignored.
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200117 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000118int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200119int hostname_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen485b9551999-12-07 23:14:59 +0000120{
Denis Vlasenko8514fc52006-09-22 08:53:14 +0000121 enum {
122 OPT_d = 0x1,
123 OPT_f = 0x2,
124 OPT_i = 0x4,
125 OPT_s = 0x8,
Denis Vlasenkobc5262d2007-01-26 07:02:56 +0000126 OPT_F = 0x10,
Michael Tokareve9355c42013-12-17 19:13:45 +0100127 OPT_dfi = 0x7,
Denis Vlasenko8514fc52006-09-22 08:53:14 +0000128 };
129
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200130 unsigned opts;
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000131 char *buf;
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000132 char *hostname_str;
Eric Andersen485b9551999-12-07 23:14:59 +0000133
Denys Vlasenko036585a2017-08-08 16:38:18 +0200134 /* dnsdomainname from net-tools 1.60, hostname 1.100 (2001-04-14),
135 * supports hostname's options too (not just -v as manpage says) */
136 opts = getopt32(argv, "dfisF:v", &hostname_str,
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200137 "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"
Denys Vlasenko036585a2017-08-08 16:38:18 +0200145 );
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000146 argv += optind;
147 buf = safe_gethostname();
Denys Vlasenko67fd2f22016-11-13 22:35:10 +0100148 if (ENABLE_DNSDOMAINNAME) {
149 if (!ENABLE_HOSTNAME || applet_name[0] == 'd') {
150 /* dnsdomainname */
151 opts = OPT_d;
152 }
153 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154
Michael Tokareve9355c42013-12-17 19:13:45 +0100155 if (opts & OPT_dfi) {
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200156 /* Cases when we need full hostname (or its part) */
Denis Vlasenko8514fc52006-09-22 08:53:14 +0000157 struct hostent *hp;
158 char *p;
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200159
Eric Andersen3d61b102001-10-31 09:59:57 +0000160 hp = xgethostbyname(buf);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200161 p = strchrnul(hp->h_name, '.');
162 if (opts & OPT_f) {
Eric Andersen3d61b102001-10-31 09:59:57 +0000163 puts(hp->h_name);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200164 } else if (opts & OPT_s) {
165 *p = '\0';
Glenn L McGrath3ff3f4a2002-11-10 22:07:48 +0000166 puts(hp->h_name);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200167 } else if (opts & OPT_d) {
168 if (*p)
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +0000169 puts(p + 1);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200170 } else /*if (opts & OPT_i)*/ {
Denys Vlasenko99069332010-02-27 19:38:19 +0100171 if (hp->h_length == sizeof(struct in_addr)) {
172 struct in_addr **h_addr_list = (struct in_addr **)hp->h_addr_list;
173 while (*h_addr_list) {
Denys Vlasenko1d9e99b2012-04-01 16:52:21 +0200174 printf(h_addr_list[1] ? "%s " : "%s", inet_ntoa(**h_addr_list));
Denys Vlasenko99069332010-02-27 19:38:19 +0100175 h_addr_list++;
176 }
177 bb_putchar('\n');
Eric Andersen3d61b102001-10-31 09:59:57 +0000178 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000179 }
Michael Tokareve9355c42013-12-17 19:13:45 +0100180 } else if (opts & OPT_s) {
181 strchrnul(buf, '.')[0] = '\0';
182 puts(buf);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200183 } else if (opts & OPT_F) {
184 /* Set the hostname */
Denis Vlasenko8514fc52006-09-22 08:53:14 +0000185 do_sethostname(hostname_str, 1);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000186 } else if (argv[0]) {
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200187 /* Set the hostname */
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000188 do_sethostname(argv[0], 0);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200189 } else {
190 /* Just print the current hostname */
Eric Andersen3d61b102001-10-31 09:59:57 +0000191 puts(buf);
192 }
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200193
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000194 if (ENABLE_FEATURE_CLEAN_UP)
195 free(buf);
Denis Vlasenko5415c852008-07-21 23:05:26 +0000196 return EXIT_SUCCESS;
Eric Andersen485b9551999-12-07 23:14:59 +0000197}