/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2015, 2025 Todd C. Miller <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*/1718#include <config.h>1920#include <stdlib.h>21#include <unistd.h>2223#include <sudo_compat.h>24#include <sudo_util.h>2526/*27* Return a malloc()ed copy of the system hostname, or NULL if28* malloc() or gethostname() fails.29*/30char *31sudo_gethostname_v1(void)32{33const size_t host_name_max = sudo_host_name_max();34char *hname = malloc(host_name_max + 1);35if (hname != NULL) {36if (gethostname(hname, host_name_max + 1) == 0 && *hname != '\0') {37/* Old gethostname() may not NUL-terminate if there is no room. */38hname[host_name_max] = '\0';39} else {40free(hname);41hname = NULL;42}43}44return hname;45}4647size_t48sudo_host_name_max_v1(void)49{50static size_t maxval;5152if (maxval == 0) {53long lval;5455#ifdef _SC_HOST_NAME_MAX56lval = sysconf(_SC_HOST_NAME_MAX);57if (lval <= 0)58#endif59lval = 255; /* POSIX and historic BSD */60maxval = (size_t)lval;61}6263return maxval;64}656667