Skip to content
Browse files

better detection and correct use of POSIX.1c getpwnam_r

  • Loading branch information...
1 parent 6e87939 commit 451a0e07f83d6599e8716c96d353c9737726a91b @postwait postwait committed
Showing with 37 additions and 12 deletions.
  1. +4 −4 aclocal.m4
  2. +2 −0 configure.in
  3. +5 −0 src/noit_config.h.in
  4. +26 −8 src/utils/noit_security.c
View
8 aclocal.m4
@@ -9,10 +9,10 @@ AC_DEFUN(GETPWNAM_R_POSIX,
HAVE_GETPWNAM_R_POSIX=""
AC_MSG_CHECKING(for POSIX.1c getpwnam_r)
-AC_TRY_COMPILE([
+AC_TRY_LINK([
#include <pwd.h>
#include <stdlib.h>],
- getpwnam_r(NULL,NULL,NULL,0);,
+ getpwnam_r(NULL,NULL,NULL,0,NULL);,
AC_DEFINE(HAVE_GETPWNAM_R_POSIX,1,POSIX.1c getpwnam_r)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))
@@ -27,10 +27,10 @@ AC_DEFUN(GETGRNAM_R_POSIX,
HAVE_GETGRNAM_R_POSIX=""
AC_MSG_CHECKING(for POSIX.1c getgrnam_r)
-AC_TRY_COMPILE([
+AC_TRY_LINK([
#include <grp.h>
#include <stdlib.h>],
- getgrnam_r(NULL,NULL,NULL,0);,
+ getgrnam_r(NULL,NULL,NULL,0,NULL);,
AC_DEFINE(HAVE_GETGRNAM_R_POSIX,1,POSIX.1c getgrnam_r)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))
View
2 configure.in
@@ -279,7 +279,9 @@ AC_CHECK_HEADERS(sys/file.h sys/types.h dirent.h sys/param.h fcntl.h errno.h lim
sys/ioctl_compat.h sys/filio.h util.h sys/time.h sys/mman.h \
stropts.h sys/stream.h alloca.h sys/wait.h)
+AC_CHECK_FUNCS(getpwnam_r)
GETPWNAM_R_POSIX
+AC_CHECK_FUNCS(getgrnam_r)
GETGRNAM_R_POSIX
AC_MSG_CHECKING(for tputs() function final argument type)
View
5 src/noit_config.h.in
@@ -80,6 +80,11 @@
#define IFS_CH '/'
#undef WORKING_SEM_INIT
+#undef HAVE_GETPWNAM_R
+#undef HAVE_GETPWNAM_R_POSIX
+#undef HAVE_GETGRNAM_R
+#undef HAVE_GETGRNAM_R_POSIX
+
#ifdef HAVE_STRING_H
#include <string.h>
#endif
View
34 src/utils/noit_security.c
@@ -43,22 +43,30 @@ static struct passwd *
__getpwnam_r(const char *user, struct passwd *pw,
char *buf, size_t len) {
#ifdef HAVE_GETPWNAM_R_POSIX
- return getpwnam_r(user, pw, buf, len);
-#else
struct passwd *r;
if(0 == getpwnam_r(user, pw, buf, len, &r)) return r;
return NULL;
+#else
+#if HAVE_GETPWNAM_R
+ return getpwnam_r(user, pw, buf, len);
+#else
+ return getpwnam(user);
+#endif
#endif
}
static struct group *
__getgrnam_r(const char *group, struct group *gr,
char *buf, size_t len) {
#ifdef HAVE_GETGRNAM_R_POSIX
- return getgrnam_r(group, gr, buf, len);
-#else
struct group *r;
if(0 == getgrnam_r(group, gr, buf, len, &r)) return r;
return NULL;
+#else
+#ifdef HAVE_GETGRNAM_R
+ return getgrnam_r(group, gr, buf, len);
+#else
+ return getgrnam(group);
+#endif
#endif
}
@@ -66,11 +74,21 @@ int
noit_security_usergroup(const char *user, const char *group) {
static long pwnam_buflen = 0;
static long grnam_buflen = 0;
- uid_t uid;
- gid_t gid;
+ uid_t uid = 0;
+ gid_t gid = 0;
- if(pwnam_buflen == 0) pwnam_buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
- if(grnam_buflen == 0) grnam_buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+ if(pwnam_buflen == 0)
+#ifdef _SC_GETPW_R_SIZE_MAX
+ pwnam_buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+#else
+ pwnam_buflen = 100; /* This shouldn't be used, so size is not important. */
+#endif
+ if(grnam_buflen == 0)
+#ifdef _SC_GETGR_R_SIZE_MAX
+ grnam_buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+#else
+ grnam_buflen = 100;
+#endif
if(user) {
if(ispositiveinteger(user)) uid = atoi(user);

0 comments on commit 451a0e0

Please sign in to comment.
Something went wrong with that request. Please try again.