/****************************************************************************
- * Copyright 2019-2020,2021 Thomas E. Dickey *
+ * Copyright 2019-2021,2023 Thomas E. Dickey *
* Copyright 1998-2011,2012 Free Software Foundation, Inc. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
#endif
#endif
+#if HAVE_GETAUXVAL && HAVE_SYS_AUXV_H && defined(__GLIBC__) && (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19)
+#include <sys/auxv.h>
+#define USE_GETAUXVAL 1
+#else
+#define USE_GETAUXVAL 0
+#endif
+
#include <tic.h>
-MODULE_ID("$Id: access.c,v 1.29 2021/06/26 23:50:02 tom Exp $")
+MODULE_ID("$Id: access.c,v 1.37 2023/06/24 21:55:09 tom Exp $")
#define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
static char *temp;
char *s;
- temp = strdup(result);
- result = temp;
+ if ((temp = strdup(result)) != 0)
+ result = temp;
#if !MIXEDCASE_FILENAMES
for (s = result; *s != '\0'; ++s) {
*s = (char) LOWERCASE(*s);
return result;
}
+#if HAVE_GETEUID && HAVE_GETEGID
+#define is_posix_elevated() \
+ (getuid() != geteuid() \
+ || getgid() != getegid())
+#else
+#define is_posix_elevated() FALSE
+#endif
+
#if HAVE_ISSETUGID
#define is_elevated() issetugid()
-#elif HAVE_GETEUID && HAVE_GETEGID
+#elif USE_GETAUXVAL && defined(AT_SECURE)
#define is_elevated() \
- (getuid() != geteuid() \
- || getgid() != getegid())
+ (getauxval(AT_SECURE) \
+ ? TRUE \
+ : (errno != ENOENT \
+ ? FALSE \
+ : is_posix_elevated()))
#else
-#define is_elevated() FALSE
+#define is_elevated() is_posix_elevated()
#endif
#if HAVE_SETFSUID
#define resume_elevation() /* nothing */
#endif
-#ifndef USE_ROOT_ENVIRON
/*
- * Returns true if we allow application to use environment variables that are
- * used for searching lists of directories, etc.
+ * Returns true if not running as root or setuid. We use this check to allow
+ * applications to use environment variables that are used for searching lists
+ * of directories, etc.
*/
NCURSES_EXPORT(int)
_nc_env_access(void)
{
int result = TRUE;
+#if HAVE_GETUID && HAVE_GETEUID
+#if !defined(USE_SETUID_ENVIRON)
if (is_elevated()) {
result = FALSE;
- } else if ((getuid() != ROOT_UID) && (geteuid() != ROOT_UID)) {
+ }
+#endif
+#if !defined(USE_ROOT_ENVIRON)
+ if ((getuid() == ROOT_UID) || (geteuid() == ROOT_UID)) {
result = FALSE;
}
+#endif
+#endif /* HAVE_GETUID && HAVE_GETEUID */
return result;
}
-#endif /* USE_ROOT_ENVIRON */
#ifndef USE_ROOT_ACCESS
/*
#endif
return result;
}
-#endif /* USE_ROOT_ENVIRON */
+#endif /* USE_ROOT_ACCESS */