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 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 13 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 14 | static void do_sethostname(char *s, int isfile) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 15 | { |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 16 | // if (!s) |
| 17 | // return; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 18 | if (isfile) { |
| 19 | parser_t *parser = config_open2(s, xfopen_for_read); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 20 | while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 21 | do_sethostname(s, 0); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 22 | } |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 23 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 24 | config_close(parser); |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 25 | } else if (sethostname(s, strlen(s))) { |
| 26 | // if (errno == EPERM) |
| 27 | // bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 28 | bb_perror_msg_and_die("sethostname"); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 29 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 32 | /* Manpage circa 2009: |
| 33 | * |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 34 | * hostname [-v] [-a] [--alias] [-d] [--domain] [-f] [--fqdn] [--long] |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 35 | * [-i] [--ip-address] [-s] [--short] [-y] [--yp] [--nis] |
| 36 | * |
| 37 | * hostname [-v] [-F filename] [--file filename] / [hostname] |
| 38 | * |
| 39 | * domainname [-v] [-F filename] [--file filename] / [name] |
| 40 | * { bbox: not supported } |
| 41 | * |
| 42 | * nodename [-v] [-F filename] [--file filename] / [name] |
| 43 | * { bbox: not supported } |
| 44 | * |
| 45 | * dnsdomainname [-v] |
| 46 | * { bbox: supported: Linux kernel build needs this } |
| 47 | * nisdomainname [-v] |
| 48 | * { bbox: not supported } |
| 49 | * ypdomainname [-v] |
| 50 | * { bbox: not supported } |
| 51 | * |
| 52 | * -a, --alias |
| 53 | * Display the alias name of the host (if used). |
| 54 | * { bbox: not supported } |
| 55 | * -d, --domain |
| 56 | * Display the name of the DNS domain. Don't use the command |
| 57 | * domainname to get the DNS domain name because it will show the |
| 58 | * NIS domain name and not the DNS domain name. Use dnsdomainname |
| 59 | * instead. |
| 60 | * -f, --fqdn, --long |
| 61 | * Display the FQDN (Fully Qualified Domain Name). A FQDN consists |
| 62 | * of a short host name and the DNS domain name. Unless you are |
| 63 | * using bind or NIS for host lookups you can change the FQDN and |
| 64 | * the DNS domain name (which is part of the FQDN) in the |
| 65 | * /etc/hosts file. |
| 66 | * -i, --ip-address |
| 67 | * Display the IP address(es) of the host. |
| 68 | * -s, --short |
| 69 | * Display the short host name. This is the host name cut at the |
| 70 | * first dot. |
| 71 | * -v, --verbose |
| 72 | * Be verbose and tell what's going on. |
| 73 | * { bbox: supported but ignored } |
| 74 | * -y, --yp, --nis |
| 75 | * Display the NIS domain name. If a parameter is given (or --file |
| 76 | * name ) then root can also set a new NIS domain. |
| 77 | * { bbox: not supported } |
| 78 | * -F, --file filename |
| 79 | * Read the host name from the specified file. Comments (lines |
| 80 | * starting with a `#') are ignored. |
| 81 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 82 | int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 83 | int hostname_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 84 | { |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 85 | enum { |
| 86 | OPT_d = 0x1, |
| 87 | OPT_f = 0x2, |
| 88 | OPT_i = 0x4, |
| 89 | OPT_s = 0x8, |
Denis Vlasenko | bc5262d | 2007-01-26 07:02:56 +0000 | [diff] [blame] | 90 | OPT_F = 0x10, |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 91 | OPT_dfis = 0xf, |
| 92 | }; |
| 93 | |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 94 | unsigned opts; |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 95 | char *buf; |
Denis Vlasenko | 90ec4dc | 2007-01-25 19:44:38 +0000 | [diff] [blame] | 96 | char *hostname_str; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 97 | |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 98 | #if ENABLE_LONG_OPTS |
| 99 | applet_long_options = |
| 100 | "domain\0" No_argument "d" |
| 101 | "fqdn\0" No_argument "f" |
| 102 | //Enable if seen in active use in some distro: |
| 103 | // "long\0" No_argument "f" |
| 104 | // "ip-address\0" No_argument "i" |
| 105 | // "short\0" No_argument "s" |
| 106 | // "verbose\0" No_argument "v" |
| 107 | "file\0" No_argument "F" |
| 108 | ; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 109 | |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 110 | #endif |
| 111 | /* dnsdomainname from net-tools 1.60, hostname 1.100 (2001-04-14), |
| 112 | * supports hostname's options too (not just -v as manpage says) */ |
| 113 | opts = getopt32(argv, "dfisF:v", &hostname_str); |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 114 | argv += optind; |
| 115 | buf = safe_gethostname(); |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 116 | if (applet_name[0] == 'd') /* dnsdomainname? */ |
| 117 | opts = OPT_d; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 119 | if (opts & OPT_dfis) { |
| 120 | /* Cases when we need full hostname (or its part) */ |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 121 | struct hostent *hp; |
| 122 | char *p; |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 123 | |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 124 | hp = xgethostbyname(buf); |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 125 | p = strchrnul(hp->h_name, '.'); |
| 126 | if (opts & OPT_f) { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 127 | puts(hp->h_name); |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 128 | } else if (opts & OPT_s) { |
| 129 | *p = '\0'; |
Glenn L McGrath | 3ff3f4a | 2002-11-10 22:07:48 +0000 | [diff] [blame] | 130 | puts(hp->h_name); |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 131 | } else if (opts & OPT_d) { |
| 132 | if (*p) |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 133 | puts(p + 1); |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 134 | } else /*if (opts & OPT_i)*/ { |
Denys Vlasenko | 9906933 | 2010-02-27 19:38:19 +0100 | [diff] [blame] | 135 | if (hp->h_length == sizeof(struct in_addr)) { |
| 136 | struct in_addr **h_addr_list = (struct in_addr **)hp->h_addr_list; |
| 137 | while (*h_addr_list) { |
| 138 | printf("%s ", inet_ntoa(**h_addr_list)); |
| 139 | h_addr_list++; |
| 140 | } |
| 141 | bb_putchar('\n'); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 142 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 143 | } |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 144 | } else if (opts & OPT_F) { |
| 145 | /* Set the hostname */ |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 146 | do_sethostname(hostname_str, 1); |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 147 | } else if (argv[0]) { |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 148 | /* Set the hostname */ |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 149 | do_sethostname(argv[0], 0); |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 150 | } else { |
| 151 | /* Just print the current hostname */ |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 152 | puts(buf); |
| 153 | } |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 154 | |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 155 | if (ENABLE_FEATURE_CLEAN_UP) |
| 156 | free(buf); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 157 | return EXIT_SUCCESS; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 158 | } |