blob: 5f663908cf5524a686a09d3ecd97da0d33123d1a [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 */
Pere Orga5bc8c002011-04-11 03:29:49 +020012
13//usage:#define hostname_trivial_usage
14//usage: "[OPTIONS] [HOSTNAME | -F FILE]"
15//usage:#define hostname_full_usage "\n\n"
16//usage: "Get or set hostname or DNS domain name\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020017//usage: "\n -s Short"
18//usage: "\n -i Addresses for the hostname"
19//usage: "\n -d DNS domain name"
20//usage: "\n -f Fully qualified domain name"
21//usage: "\n -F FILE Use FILE's content as hostname"
22//usage:
23//usage:#define hostname_example_usage
24//usage: "$ hostname\n"
25//usage: "sage\n"
26//usage:
27//usage:#define dnsdomainname_trivial_usage NOUSAGE_STR
28//usage:#define dnsdomainname_full_usage ""
29
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000030#include "libbb.h"
Eric Andersen3d61b102001-10-31 09:59:57 +000031
Eric Andersen3e6ff902001-03-09 21:24:12 +000032static void do_sethostname(char *s, int isfile)
Eric Andersen485b9551999-12-07 23:14:59 +000033{
Denys Vlasenkod30b89c2009-06-26 01:55:45 +020034// if (!s)
35// return;
Denis Vlasenko5415c852008-07-21 23:05:26 +000036 if (isfile) {
37 parser_t *parser = config_open2(s, xfopen_for_read);
Denis Vlasenko084266e2008-07-26 23:08:31 +000038 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
Denis Vlasenko5415c852008-07-21 23:05:26 +000039 do_sethostname(s, 0);
Eric Andersen3d61b102001-10-31 09:59:57 +000040 }
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000041 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko5415c852008-07-21 23:05:26 +000042 config_close(parser);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +020043 } else if (sethostname(s, strlen(s))) {
44// if (errno == EPERM)
45// bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
Denis Vlasenko5415c852008-07-21 23:05:26 +000046 bb_perror_msg_and_die("sethostname");
Eric Andersen485b9551999-12-07 23:14:59 +000047 }
Eric Andersen485b9551999-12-07 23:14:59 +000048}
49
Denys Vlasenkod30b89c2009-06-26 01:55:45 +020050/* Manpage circa 2009:
51 *
Denys Vlasenko9038d6f2009-07-15 20:02:19 +020052 * hostname [-v] [-a] [--alias] [-d] [--domain] [-f] [--fqdn] [--long]
Denys Vlasenkod30b89c2009-06-26 01:55:45 +020053 * [-i] [--ip-address] [-s] [--short] [-y] [--yp] [--nis]
54 *
55 * hostname [-v] [-F filename] [--file filename] / [hostname]
56 *
57 * domainname [-v] [-F filename] [--file filename] / [name]
58 * { bbox: not supported }
59 *
60 * nodename [-v] [-F filename] [--file filename] / [name]
61 * { bbox: not supported }
62 *
63 * dnsdomainname [-v]
64 * { bbox: supported: Linux kernel build needs this }
65 * nisdomainname [-v]
66 * { bbox: not supported }
67 * ypdomainname [-v]
68 * { bbox: not supported }
69 *
70 * -a, --alias
71 * Display the alias name of the host (if used).
72 * { bbox: not supported }
73 * -d, --domain
74 * Display the name of the DNS domain. Don't use the command
75 * domainname to get the DNS domain name because it will show the
76 * NIS domain name and not the DNS domain name. Use dnsdomainname
77 * instead.
78 * -f, --fqdn, --long
79 * Display the FQDN (Fully Qualified Domain Name). A FQDN consists
80 * of a short host name and the DNS domain name. Unless you are
81 * using bind or NIS for host lookups you can change the FQDN and
82 * the DNS domain name (which is part of the FQDN) in the
83 * /etc/hosts file.
84 * -i, --ip-address
85 * Display the IP address(es) of the host.
86 * -s, --short
87 * Display the short host name. This is the host name cut at the
88 * first dot.
89 * -v, --verbose
90 * Be verbose and tell what's going on.
91 * { bbox: supported but ignored }
92 * -y, --yp, --nis
93 * Display the NIS domain name. If a parameter is given (or --file
94 * name ) then root can also set a new NIS domain.
95 * { bbox: not supported }
96 * -F, --file filename
97 * Read the host name from the specified file. Comments (lines
98 * starting with a `#') are ignored.
99 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000100int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200101int hostname_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen485b9551999-12-07 23:14:59 +0000102{
Denis Vlasenko8514fc52006-09-22 08:53:14 +0000103 enum {
104 OPT_d = 0x1,
105 OPT_f = 0x2,
106 OPT_i = 0x4,
107 OPT_s = 0x8,
Denis Vlasenkobc5262d2007-01-26 07:02:56 +0000108 OPT_F = 0x10,
Denis Vlasenko8514fc52006-09-22 08:53:14 +0000109 OPT_dfis = 0xf,
110 };
111
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200112 unsigned opts;
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000113 char *buf;
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000114 char *hostname_str;
Eric Andersen485b9551999-12-07 23:14:59 +0000115
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200116#if ENABLE_LONG_OPTS
117 applet_long_options =
118 "domain\0" No_argument "d"
119 "fqdn\0" No_argument "f"
120 //Enable if seen in active use in some distro:
121 // "long\0" No_argument "f"
122 // "ip-address\0" No_argument "i"
123 // "short\0" No_argument "s"
124 // "verbose\0" No_argument "v"
125 "file\0" No_argument "F"
126 ;
Eric Andersen485b9551999-12-07 23:14:59 +0000127
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200128#endif
129 /* dnsdomainname from net-tools 1.60, hostname 1.100 (2001-04-14),
130 * supports hostname's options too (not just -v as manpage says) */
131 opts = getopt32(argv, "dfisF:v", &hostname_str);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000132 argv += optind;
133 buf = safe_gethostname();
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200134 if (applet_name[0] == 'd') /* dnsdomainname? */
135 opts = OPT_d;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200137 if (opts & OPT_dfis) {
138 /* Cases when we need full hostname (or its part) */
Denis Vlasenko8514fc52006-09-22 08:53:14 +0000139 struct hostent *hp;
140 char *p;
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200141
Eric Andersen3d61b102001-10-31 09:59:57 +0000142 hp = xgethostbyname(buf);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200143 p = strchrnul(hp->h_name, '.');
144 if (opts & OPT_f) {
Eric Andersen3d61b102001-10-31 09:59:57 +0000145 puts(hp->h_name);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200146 } else if (opts & OPT_s) {
147 *p = '\0';
Glenn L McGrath3ff3f4a2002-11-10 22:07:48 +0000148 puts(hp->h_name);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200149 } else if (opts & OPT_d) {
150 if (*p)
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +0000151 puts(p + 1);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200152 } else /*if (opts & OPT_i)*/ {
Denys Vlasenko99069332010-02-27 19:38:19 +0100153 if (hp->h_length == sizeof(struct in_addr)) {
154 struct in_addr **h_addr_list = (struct in_addr **)hp->h_addr_list;
155 while (*h_addr_list) {
156 printf("%s ", inet_ntoa(**h_addr_list));
157 h_addr_list++;
158 }
159 bb_putchar('\n');
Eric Andersen3d61b102001-10-31 09:59:57 +0000160 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000161 }
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200162 } else if (opts & OPT_F) {
163 /* Set the hostname */
Denis Vlasenko8514fc52006-09-22 08:53:14 +0000164 do_sethostname(hostname_str, 1);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000165 } else if (argv[0]) {
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200166 /* Set the hostname */
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000167 do_sethostname(argv[0], 0);
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200168 } else {
169 /* Just print the current hostname */
Eric Andersen3d61b102001-10-31 09:59:57 +0000170 puts(buf);
171 }
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200172
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000173 if (ENABLE_FEATURE_CLEAN_UP)
174 free(buf);
Denis Vlasenko5415c852008-07-21 23:05:26 +0000175 return EXIT_SUCCESS;
Eric Andersen485b9551999-12-07 23:14:59 +0000176}