blob: 06d91f2d017e8aa9b68a6d95fbdf45b251c27f32
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 | |
13 | //config:config HOSTNAME |
14 | //config: bool "hostname" |
15 | //config: default y |
16 | //config: help |
17 | //config: Show or set the system's host name. |
18 | //config: |
19 | //config:config DNSDOMAINNAME |
20 | //config: bool "dnsdomainname" |
21 | //config: default y |
22 | //config: help |
23 | //config: Alias to "hostname -d". |
24 | |
25 | //applet:IF_DNSDOMAINNAME(APPLET_ODDNAME(dnsdomainname, hostname, BB_DIR_BIN, BB_SUID_DROP, dnsdomainname)) |
26 | //applet:IF_HOSTNAME(APPLET(hostname, BB_DIR_BIN, BB_SUID_DROP)) |
27 | |
28 | //kbuild: lib-$(CONFIG_HOSTNAME) += hostname.o |
29 | //kbuild: lib-$(CONFIG_DNSDOMAINNAME) += hostname.o |
30 | |
31 | //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" |
35 | //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 | |
48 | #include "libbb.h" |
49 | |
50 | static void do_sethostname(char *s, int isfile) |
51 | { |
52 | // if (!s) |
53 | // return; |
54 | if (isfile) { |
55 | parser_t *parser = config_open2(s, xfopen_for_read); |
56 | while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) { |
57 | do_sethostname(s, 0); |
58 | } |
59 | if (ENABLE_FEATURE_CLEAN_UP) |
60 | config_close(parser); |
61 | } else if (sethostname(s, strlen(s))) { |
62 | // if (errno == EPERM) |
63 | // bb_error_msg_and_die("%s", bb_msg_perm_denied_are_you_root); |
64 | bb_perror_msg_and_die("sethostname"); |
65 | } |
66 | } |
67 | |
68 | /* Manpage circa 2009: |
69 | * |
70 | * hostname [-v] [-a] [--alias] [-d] [--domain] [-f] [--fqdn] [--long] |
71 | * [-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 |
116 | * starting with a `#') are ignored. |
117 | */ |
118 | int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
119 | int hostname_main(int argc UNUSED_PARAM, char **argv) |
120 | { |
121 | enum { |
122 | OPT_d = 0x1, |
123 | OPT_f = 0x2, |
124 | OPT_i = 0x4, |
125 | OPT_s = 0x8, |
126 | OPT_F = 0x10, |
127 | OPT_dfi = 0x7, |
128 | }; |
129 | |
130 | unsigned opts; |
131 | char *buf; |
132 | char *hostname_str; |
133 | |
134 | #if ENABLE_LONG_OPTS |
135 | applet_long_options = |
136 | "domain\0" No_argument "d" |
137 | "fqdn\0" No_argument "f" |
138 | //Enable if seen in active use in some distro: |
139 | // "long\0" No_argument "f" |
140 | // "ip-address\0" No_argument "i" |
141 | // "short\0" No_argument "s" |
142 | // "verbose\0" No_argument "v" |
143 | "file\0" No_argument "F" |
144 | ; |
145 | |
146 | #endif |
147 | /* dnsdomainname from net-tools 1.60, hostname 1.100 (2001-04-14), |
148 | * supports hostname's options too (not just -v as manpage says) */ |
149 | opts = getopt32(argv, "dfisF:v", &hostname_str); |
150 | argv += optind; |
151 | buf = safe_gethostname(); |
152 | if (ENABLE_DNSDOMAINNAME) { |
153 | if (!ENABLE_HOSTNAME || applet_name[0] == 'd') { |
154 | /* dnsdomainname */ |
155 | opts = OPT_d; |
156 | } |
157 | } |
158 | |
159 | if (opts & OPT_dfi) { |
160 | /* Cases when we need full hostname (or its part) */ |
161 | struct hostent *hp; |
162 | char *p; |
163 | |
164 | hp = xgethostbyname(buf); |
165 | p = strchrnul(hp->h_name, '.'); |
166 | if (opts & OPT_f) { |
167 | puts(hp->h_name); |
168 | } else if (opts & OPT_s) { |
169 | *p = '\0'; |
170 | puts(hp->h_name); |
171 | } else if (opts & OPT_d) { |
172 | if (*p) |
173 | puts(p + 1); |
174 | } else /*if (opts & OPT_i)*/ { |
175 | if (hp->h_length == sizeof(struct in_addr)) { |
176 | struct in_addr **h_addr_list = (struct in_addr **)hp->h_addr_list; |
177 | while (*h_addr_list) { |
178 | printf(h_addr_list[1] ? "%s " : "%s", inet_ntoa(**h_addr_list)); |
179 | h_addr_list++; |
180 | } |
181 | bb_putchar('\n'); |
182 | } |
183 | } |
184 | } else if (opts & OPT_s) { |
185 | strchrnul(buf, '.')[0] = '\0'; |
186 | puts(buf); |
187 | } else if (opts & OPT_F) { |
188 | /* Set the hostname */ |
189 | do_sethostname(hostname_str, 1); |
190 | } else if (argv[0]) { |
191 | /* Set the hostname */ |
192 | do_sethostname(argv[0], 0); |
193 | } else { |
194 | /* Just print the current hostname */ |
195 | puts(buf); |
196 | } |
197 | |
198 | if (ENABLE_FEATURE_CLEAN_UP) |
199 | free(buf); |
200 | return EXIT_SUCCESS; |
201 | } |
202 |