safe_gethostname.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 source tree.
  8. */
  9. /*
  10. * SUSv2 guarantees that "Host names are limited to 255 bytes"
  11. * POSIX.1-2001 guarantees that "Host names (not including the terminating
  12. * null byte) are limited to HOST_NAME_MAX bytes" (64 bytes on my box).
  13. *
  14. * RFC1123 says:
  15. *
  16. * The syntax of a legal Internet host name was specified in RFC-952
  17. * [DNS:4]. One aspect of host name syntax is hereby changed: the
  18. * restriction on the first character is relaxed to allow either a
  19. * letter or a digit. Host software MUST support this more liberal
  20. * syntax.
  21. *
  22. * Host software MUST handle host names of up to 63 characters and
  23. * SHOULD handle host names of up to 255 characters.
  24. */
  25. #include "libbb.h"
  26. #include <sys/utsname.h>
  27. /*
  28. * On success return the current malloced and NUL terminated hostname.
  29. * On error return malloced and NUL terminated string "?".
  30. * This is an illegal first character for a hostname.
  31. * The returned malloced string must be freed by the caller.
  32. */
  33. char* FAST_FUNC safe_gethostname(void)
  34. {
  35. struct utsname uts;
  36. /* The length of the arrays in a struct utsname is unspecified;
  37. * the fields are terminated by a null byte.
  38. * Note that there is no standard that says that the hostname
  39. * set by sethostname(2) is the same string as the nodename field of the
  40. * struct returned by uname (indeed, some systems allow a 256-byte host-
  41. * name and an 8-byte nodename), but this is true on Linux. The same holds
  42. * for setdomainname(2) and the domainname field.
  43. */
  44. /* Uname can fail only if you pass a bad pointer to it. */
  45. uname(&uts);
  46. return xstrndup(!uts.nodename[0] ? "?" : uts.nodename, sizeof(uts.nodename));
  47. }
  48. /*
  49. * On success return the current malloced and NUL terminated domainname.
  50. * On error return malloced and NUL terminated string "?".
  51. * This is an illegal first character for a domainname.
  52. * The returned malloced string must be freed by the caller.
  53. */
  54. char* FAST_FUNC safe_getdomainname(void)
  55. {
  56. #if defined(__linux__)
  57. /* The field domainname of struct utsname is Linux specific. */
  58. struct utsname uts;
  59. uname(&uts);
  60. return xstrndup(!uts.domainname[0] ? "?" : uts.domainname, sizeof(uts.domainname));
  61. #else
  62. /* We really don't care about people with domain names wider than most screens */
  63. char buf[256];
  64. int r = getdomainname(buf, sizeof(buf));
  65. buf[sizeof(buf)-1] = '\0';
  66. return xstrdup(r < 0 ? "?" : buf);
  67. #endif
  68. }