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