Avoid using HOST_NAME_MAX directly and use sysconf(_SC_HOST_NAME_MAX)

instead.
This commit is contained in:
Todd C. Miller
2015-02-19 20:28:02 -07:00
parent 9b514ed83c
commit be8dbeb22e
13 changed files with 119 additions and 89 deletions

View File

@@ -91,6 +91,7 @@ lib/util/fnmatch.c
lib/util/getaddrinfo.c
lib/util/getcwd.c
lib/util/getgrouplist.c
lib/util/gethostname.c
lib/util/getline.c
lib/util/getopt_long.c
lib/util/gidlist.c

View File

@@ -97,10 +97,6 @@
don't. */
#undef HAVE_DECL_GETRESUID
/* Define to 1 if you have the declaration of `HOST_NAME_MAX', and to 0 if you
don't. */
#undef HAVE_DECL_HOST_NAME_MAX
/* Define to 1 if you have the declaration of `h_errno', and to 0 if you
don't. */
#undef HAVE_DECL_H_ERRNO
@@ -173,10 +169,6 @@
don't. */
#undef HAVE_DECL__INNETGR
/* Define to 1 if you have the declaration of `_POSIX_HOST_NAME_MAX', and to 0
if you don't. */
#undef HAVE_DECL__POSIX_HOST_NAME_MAX
/* Define to 1 if you have the declaration of `_POSIX_PATH_MAX', and to 0 if
you don't. */
#undef HAVE_DECL__POSIX_PATH_MAX

31
configure vendored
View File

@@ -21337,20 +21337,6 @@ fi
cat >>confdefs.h <<_ACEOF
#define HAVE_DECL_PATH_MAX $ac_have_decl
_ACEOF
ac_fn_c_check_decl "$LINENO" "HOST_NAME_MAX" "ac_cv_have_decl_HOST_NAME_MAX" "
#include <sys/types.h>
#include <limits.h>
"
if test "x$ac_cv_have_decl_HOST_NAME_MAX" = xyes; then :
ac_have_decl=1
else
ac_have_decl=0
fi
cat >>confdefs.h <<_ACEOF
#define HAVE_DECL_HOST_NAME_MAX $ac_have_decl
_ACEOF
ac_fn_c_check_decl "$LINENO" "SIZE_MAX" "ac_cv_have_decl_SIZE_MAX" "
#include <sys/types.h>
@@ -21457,23 +21443,6 @@ cat >>confdefs.h <<_ACEOF
#define HAVE_DECL__POSIX_PATH_MAX $ac_have_decl
_ACEOF
fi
if test "$ac_cv_have_decl_HOST_NAME_MAX" != "yes"; then
ac_fn_c_check_decl "$LINENO" "_POSIX_HOST_NAME_MAX" "ac_cv_have_decl__POSIX_HOST_NAME_MAX" "
#include <sys/types.h>
#include <limits.h>
"
if test "x$ac_cv_have_decl__POSIX_HOST_NAME_MAX" = xyes; then :
ac_have_decl=1
else
ac_have_decl=0
fi
cat >>confdefs.h <<_ACEOF
#define HAVE_DECL__POSIX_HOST_NAME_MAX $ac_have_decl
_ACEOF
fi
for ac_func in strsignal

View File

@@ -3010,7 +3010,7 @@ dnl We need to add OSDEFS to CFLAGS to expose LLONG_MAX et al on glibc.
dnl
_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $OSDEFS"
AC_CHECK_DECLS([LLONG_MAX, LLONG_MIN, ULLONG_MAX, PATH_MAX, HOST_NAME_MAX], [], [], [
AC_CHECK_DECLS([LLONG_MAX, LLONG_MIN, ULLONG_MAX, PATH_MAX], [], [], [
#include <sys/types.h>
#include <limits.h>
])
@@ -3057,12 +3057,6 @@ if test "$ac_cv_have_decl_PATH_MAX" != "yes"; then
#include <limits.h>
]])
fi
if test "$ac_cv_have_decl_HOST_NAME_MAX" != "yes"; then
AC_CHECK_DECLS([_POSIX_HOST_NAME_MAX], [], [], [[
#include <sys/types.h>
#include <limits.h>
]])
fi
dnl
dnl Check for strsignal() or sys_siglist

View File

@@ -160,14 +160,6 @@
# endif
#endif
#if defined(HAVE_DECL_HOST_NAME_MAX) && !HAVE_DECL_HOST_NAME_MAX
# if defined(HAVE_DECL__POSIX_HOST_NAME_MAX) && HAVE_DECL__POSIX_HOST_NAME_MAX
# define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
# else
# define HOST_NAME_MAX 255
# endif
#endif
/*
* Posix versions for those without...
*/

View File

@@ -144,6 +144,10 @@ __dso_public int aix_restoreauthdb_v1(void);
__dso_public int aix_setauthdb_v1(char *user);
#define aix_setauthdb(_a) aix_setauthdb_v1((_a))
/* gethostname.c */
__dso_public char *sudo_gethostname_v1(void);
#define sudo_gethostname() sudo_gethostname_v1()
/* gidlist.c */
__dso_public int sudo_parse_gids_v1(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp);
#define sudo_parse_gids(_a, _b, _c) sudo_parse_gids_v1((_a), (_b), (_c))

View File

@@ -96,8 +96,8 @@ DEVEL = @DEVEL@
SHELL = @SHELL@
LTOBJS = alloc.lo event.lo fatal.lo key_val.lo gidlist.lo lbuf.lo \
locking.lo parseln.lo progname.lo secure_path.lo setgroups.lo \
LTOBJS = alloc.lo event.lo fatal.lo key_val.lo gethostname.lo gidlist.lo \
lbuf.lo locking.lo parseln.lo progname.lo secure_path.lo setgroups.lo \
strtobool.lo strtoid.lo strtomode.lo sudo_conf.lo sudo_debug.lo \
sudo_dso.lo term.lo ttysize.lo @COMMON_OBJS@ @LTLIBOBJS@
@@ -361,6 +361,10 @@ getgrouplist.lo: $(srcdir)/getgrouplist.c $(incdir)/compat/nss_dbdefs.h \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(DEFS) $(srcdir)/getgrouplist.c
gethostname.lo: $(srcdir)/gethostname.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_util.h \
$(top_builddir)/config.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(DEFS) $(srcdir)/gethostname.c
getline.lo: $(srcdir)/getline.c $(incdir)/sudo_compat.h $(top_builddir)/config.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(DEFS) $(srcdir)/getline.c
getopt_long.lo: $(srcdir)/getopt_long.c $(incdir)/compat/getopt.h \

64
lib/util/gethostname.c Normal file
View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2015 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
#include <sys/types.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif /* STDC_HEADERS */
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
# include <malloc.h>
#endif /* HAVE_MALLOC_H && !STDC_HEADERS */
#include <unistd.h>
#include "sudo_compat.h"
#include "sudo_util.h"
/*
* Return a malloc()ed copy of the system hostname, or NULL if
* malloc() or gethostname() fails.
*/
char *
sudo_gethostname_v1(void)
{
char *hname;
size_t host_name_max;
#ifdef _SC_HOST_NAME_MAX
host_name_max = (size_t)sysconf(_SC_HOST_NAME_MAX);
if (host_name_max == (size_t)-1)
#endif
host_name_max = 255; /* POSIX and historic BSD */
hname = malloc(host_name_max + 1);
if (hname != NULL) {
if (gethostname(hname, host_name_max + 1) == 0) {
/* Old gethostname() may not NUL-terminate if there is no room. */
hname[host_name_max] = '\0';
} else {
free(hname);
hname = NULL;
}
}
return hname;
}

View File

@@ -65,6 +65,7 @@ sudo_fatal_callback_register_v1
sudo_fatal_nodebug_v1
sudo_fatalx_nodebug_v1
sudo_get_ttysize_v1
sudo_gethostname_v1
sudo_lbuf_append_quoted_v1
sudo_lbuf_append_v1
sudo_lbuf_destroy_v1

View File

@@ -911,40 +911,57 @@ done:
/*
* Get NIS-style domain name and copy from static storage or NULL if none.
*/
#if defined(HAVE_GETDOMAINNAME) || defined(SI_SRPC_DOMAIN)
const char *
sudo_getdomainname(void)
{
char *domain = NULL;
#if defined(HAVE_GETDOMAINNAME) || defined(SI_SRPC_DOMAIN)
static char buf[HOST_NAME_MAX + 1];
static char *domain;
static bool initialized;
debug_decl(sudo_getdomainname, SUDOERS_DEBUG_MATCH)
if (!initialized) {
size_t host_name_max;
int rval;
# ifdef SI_SRPC_DOMAIN
buf[0] = '\0';
rval = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf));
# else
rval = getdomainname(buf, sizeof(buf));
# ifdef _SC_HOST_NAME_MAX
host_name_max = (size_t)sysconf(_SC_HOST_NAME_MAX);
if (host_name_max == (size_t)-1)
# endif
if (rval != -1 && buf[0] != '\0') {
char *cp;
host_name_max = 255; /* POSIX and historic BSD */
domain = buf;
for (cp = buf; *cp != '\0'; cp++) {
domain = malloc(host_name_max + 1);
if (domain != NULL) {
# ifdef SI_SRPC_DOMAIN
domain[0] = '\0';
rval = sysinfo(SI_SRPC_DOMAIN, domain, host_name_max + 1);
# else
rval = getdomainname(domain, host_name_max + 1);
# endif
if (rval != -1 && domain[0] != '\0') {
const char *cp;
for (cp = domain; *cp != '\0'; cp++) {
/* Check for illegal characters, Linux may use "(none)". */
if (*cp == '(' || *cp == ')' || *cp == ',' || *cp == ' ') {
sudo_efree(domain);
domain = NULL;
break;
}
}
}
}
initialized = true;
}
#endif /* HAVE_GETDOMAINNAME || SI_SRPC_DOMAIN */
return domain;
debug_return_str(domain);
}
#else
const char *
sudo_getdomainname(void)
{
debug_decl(sudo_getdomainname, SUDOERS_DEBUG_MATCH)
debug_return_ptr(NULL);
}
#endif /* HAVE_GETDOMAINNAME || SI_SRPC_DOMAIN */
/*
* Returns true if "host" and "user" belong to the netgroup "netgr",

View File

@@ -119,7 +119,6 @@ main(int argc, char *argv[])
struct privilege *priv;
struct userspec *us;
char *p, *grfile, *pwfile;
char hbuf[HOST_NAME_MAX + 1];
const char *errstr;
int match, host_match, runas_match, cmnd_match;
int ch, dflag, exitcode = 0;
@@ -212,10 +211,8 @@ main(int argc, char *argv[])
sudo_fatalx(U_("unknown user: %s"), user_name);
if (user_host == NULL) {
if (gethostname(hbuf, sizeof(hbuf)) != 0)
if ((user_host = sudo_gethostname()) == NULL)
sudo_fatal("gethostname");
hbuf[sizeof(hbuf) - 1] = '\0';
user_host = hbuf;
}
if ((p = strchr(user_host, '.'))) {
*p = '\0';

View File

@@ -1062,13 +1062,10 @@ get_args(char *cmnd)
static void
get_hostname(void)
{
char *p, thost[HOST_NAME_MAX + 1];
char *p;
debug_decl(get_hostname, SUDOERS_DEBUG_UTIL)
if (gethostname(thost, sizeof(thost)) != -1) {
thost[sizeof(thost) - 1] = '\0';
user_host = sudo_estrdup(thost);
if ((user_host = sudo_gethostname()) != NULL) {
if ((p = strchr(user_host, '.'))) {
*p = '\0';
user_shost = sudo_estrdup(user_host);

View File

@@ -447,7 +447,7 @@ get_user_groups(struct user_details *ud)
static char **
get_user_info(struct user_details *ud)
{
char *cp, **user_info, cwd[PATH_MAX], host[HOST_NAME_MAX + 1];
char *cp, **user_info, cwd[PATH_MAX];
struct passwd *pw;
int fd, i = 0;
debug_decl(get_user_info, SUDO_DEBUG_UTIL)
@@ -515,14 +515,12 @@ get_user_info(struct user_details *ud)
sudo_efree(cp);
}
if (gethostname(host, sizeof(host)) == 0)
host[sizeof(host) - 1] = '\0';
else
strlcpy(host, "localhost", sizeof(host));
user_info[++i] = sudo_new_key_val("host", host);
cp = sudo_gethostname();
user_info[++i] = sudo_new_key_val("host", cp ? cp : "localhost");
if (user_info[i] == NULL)
sudo_fatal(NULL);
ud->host = user_info[i] + sizeof("host=") - 1;
sudo_efree(cp);
sudo_get_ttysize(&ud->ts_lines, &ud->ts_cols);
sudo_easprintf(&user_info[++i], "lines=%d", ud->ts_lines);