Denis Vlasenko | d686482 | 2008-02-25 23:24:32 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Safe gethostname implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2008 Tito Ragusa <farmatito@tiscali.it> |
| 6 | * |
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * SUSv2 guarantees that "Host names are limited to 255 bytes" |
| 12 | * POSIX.1-2001 guarantees that "Host names (not including the terminating |
| 13 | * null byte) are limited to HOST_NAME_MAX bytes" (64 bytes on my box). |
| 14 | * |
| 15 | * RFC1123 says: |
| 16 | * |
| 17 | * The syntax of a legal Internet host name was specified in RFC-952 |
| 18 | * [DNS:4]. One aspect of host name syntax is hereby changed: the |
| 19 | * restriction on the first character is relaxed to allow either a |
| 20 | * letter or a digit. Host software MUST support this more liberal |
| 21 | * syntax. |
| 22 | * |
| 23 | * Host software MUST handle host names of up to 63 characters and |
| 24 | * SHOULD handle host names of up to 255 characters. |
| 25 | */ |
| 26 | |
| 27 | #include "libbb.h" |
| 28 | #include <sys/utsname.h> |
| 29 | |
| 30 | /* |
| 31 | * On success return the current malloced and NUL terminated hostname. |
| 32 | * On error return malloced and NUL terminated string "?". |
| 33 | * This is an illegal first character for a hostname. |
| 34 | * The returned malloced string must be freed by the caller. |
| 35 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 36 | char* FAST_FUNC safe_gethostname(void) |
Denis Vlasenko | d686482 | 2008-02-25 23:24:32 +0000 | [diff] [blame] | 37 | { |
| 38 | struct utsname uts; |
| 39 | |
| 40 | /* The length of the arrays in a struct utsname is unspecified; |
| 41 | * the fields are terminated by a null byte. |
| 42 | * Note that there is no standard that says that the hostname |
| 43 | * set by sethostname(2) is the same string as the nodename field of the |
| 44 | * struct returned by uname (indeed, some systems allow a 256-byte host- |
| 45 | * name and an 8-byte nodename), but this is true on Linux. The same holds |
| 46 | * for setdomainname(2) and the domainname field. |
| 47 | */ |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | d686482 | 2008-02-25 23:24:32 +0000 | [diff] [blame] | 49 | /* Uname can fail only if you pass a bad pointer to it. */ |
| 50 | uname(&uts); |
Denis Vlasenko | c94d356 | 2008-06-30 13:30:21 +0000 | [diff] [blame] | 51 | return xstrndup(!uts.nodename[0] ? "?" : uts.nodename, sizeof(uts.nodename)); |
| 52 | } |
Denis Vlasenko | d686482 | 2008-02-25 23:24:32 +0000 | [diff] [blame] | 53 | |
Denis Vlasenko | c94d356 | 2008-06-30 13:30:21 +0000 | [diff] [blame] | 54 | /* |
| 55 | * On success return the current malloced and NUL terminated domainname. |
| 56 | * On error return malloced and NUL terminated string "?". |
| 57 | * This is an illegal first character for a domainname. |
| 58 | * The returned malloced string must be freed by the caller. |
| 59 | */ |
| 60 | char* FAST_FUNC safe_getdomainname(void) |
| 61 | { |
| 62 | struct utsname uts; |
| 63 | |
| 64 | uname(&uts); |
| 65 | return xstrndup(!uts.domainname[0] ? "?" : uts.domainname, sizeof(uts.domainname)); |
Denis Vlasenko | d686482 | 2008-02-25 23:24:32 +0000 | [diff] [blame] | 66 | } |