diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/replace/libreplace_network.m4 | 24 | ||||
-rw-r--r-- | lib/replace/replace.c | 28 | ||||
-rw-r--r-- | lib/replace/replace.h | 9 | ||||
-rw-r--r-- | lib/replace/wscript | 10 | ||||
-rw-r--r-- | lib/util/samba_util.h | 2 | ||||
-rw-r--r-- | lib/util/system.c | 29 |
6 files changed, 71 insertions, 31 deletions
diff --git a/lib/replace/libreplace_network.m4 b/lib/replace/libreplace_network.m4 index eadcc6bfc1..bb2a84324e 100644 --- a/lib/replace/libreplace_network.m4 +++ b/lib/replace/libreplace_network.m4 @@ -473,6 +473,30 @@ fi LIBS=$old_LIBS CPPFLAGS="$libreplace_SAVE_CPPFLAGS" +AC_CACHE_CHECK([for SO_PEERCRED],libreplace_cv_HAVE_PEERCRED,[ +AC_TRY_COMPILE([#include <sys/types.h> +#include <sys/socket.h>], +[struct ucred cred; + socklen_t cred_len; + int ret = getsockopt(0, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len); +], +libreplace_cv_HAVE_PEERCRED=yes,libreplace_cv_HAVE_PEERCRED=no,libreplace_cv_HAVE_PEERCRED=cross)]) +if test x"$libreplace_cv_HAVE_PEERCRED" = x"yes"; then + AC_DEFINE(HAVE_PEERCRED,1,[Whether we can use SO_PEERCRED to get socket credentials]) +fi + +AC_CACHE_CHECK([for getpeereid],libreplace_cv_HAVE_GETPEEREID,[ +AC_TRY_LINK([#include <sys/types.h> +#include <unistd.h>], +[uid_t uid; gid_t gid; int ret; + ret = getpeereid(0, &uid, &gid); +], +libreplace_cv_HAVE_GETPEEREID=yes,libreplace_cv_HAVE_GETPEEREID=no)]) +if test x"$libreplace_cv_HAVE_GETPEEREID" = xyes; then + AC_DEFINE(HAVE_GETPEEREID,1, + [Whether we have getpeereid to get socket credentials]) +fi + LIBREPLACEOBJ="${LIBREPLACEOBJ} ${LIBREPLACE_NETWORK_OBJS}" echo "LIBREPLACE_NETWORK_CHECKS: END" diff --git a/lib/replace/replace.c b/lib/replace/replace.c index f1454cbcd6..d7f9cc1758 100644 --- a/lib/replace/replace.c +++ b/lib/replace/replace.c @@ -860,3 +860,31 @@ void *rep_memalign( size_t align, size_t size ) #endif } #endif + +#ifndef HAVE_GETPEEREID +int rep_getpeereid(int s, uid_t *uid, gid_t *gid) +{ +#if defined(HAVE_PEERCRED) + struct ucred cred; + socklen_t cred_len = sizeof(struct ucred); + int ret; + + ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len); + if (ret != 0) { + return -1; + } + + if (cred_len != sizeof(struct ucred)) { + errno = EINVAL; + return -1; + } + + *uid = cred.uid; + *gid = cred.gid; + return 0; +#else + errno = ENOSYS; + return -1; +#endif +} +#endif diff --git a/lib/replace/replace.h b/lib/replace/replace.h index 3f289d7f47..f2b1952376 100644 --- a/lib/replace/replace.h +++ b/lib/replace/replace.h @@ -112,6 +112,10 @@ #include <bsd/string.h> #endif +#ifdef HAVE_BSD_UNISTD_H +#include <bsd/unistd.h> +#endif + #ifdef HAVE_STRING_H #include <string.h> #endif @@ -826,4 +830,9 @@ char *rep_getpass(const char *prompt); #endif #endif +#ifndef HAVE_GETPEEREID +#define getpeereid rep_getpeereid +int rep_getpeereid(int s, uid_t *uid, gid_t *gid); +#endif + #endif /* _LIBREPLACE_REPLACE_H */ diff --git a/lib/replace/wscript b/lib/replace/wscript index e1dc1e6a30..025dda460d 100644 --- a/lib/replace/wscript +++ b/lib/replace/wscript @@ -174,6 +174,16 @@ def configure(conf): if not conf.CHECK_FUNCS('strlcpy strlcat'): conf.CHECK_FUNCS_IN('strlcpy strlcat', 'bsd', headers='bsd/string.h', checklibc=True) + if not conf.CHECK_FUNCS('getpeereid'): + conf.CHECK_FUNCS_IN('getpeereid', 'bsd', headers='sys/types.h bsd/unistd.h') + + conf.CHECK_CODE(''' + struct ucred cred; + socklen_t cred_len; + int ret = getsockopt(0, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len);''', + 'HAVE_PEERCRED', + msg="Checking whether we can use SO_PEERCRED to get socket credentials", + headers='sys/types.h sys/socket.h') #Some OS (ie. freebsd) return EINVAL if the convertion could not be done, it's not what we expect #Let's detect those cases diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index 0c3fd1aeaf..f989231102 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -123,8 +123,6 @@ _PUBLIC_ pid_t sys_fork(void); **/ _PUBLIC_ pid_t sys_getpid(void); -_PUBLIC_ int sys_getpeereid( int s, uid_t *uid); - struct sockaddr; _PUBLIC_ int sys_getnameinfo(const struct sockaddr *psa, diff --git a/lib/util/system.c b/lib/util/system.c index a7141bf9b0..f34fabd292 100644 --- a/lib/util/system.c +++ b/lib/util/system.c @@ -71,35 +71,6 @@ _PUBLIC_ pid_t sys_getpid(void) } -_PUBLIC_ int sys_getpeereid( int s, uid_t *uid) -{ -#if defined(HAVE_PEERCRED) - struct ucred cred; - socklen_t cred_len = sizeof(struct ucred); - int ret; - - ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len); - if (ret != 0) { - return -1; - } - - if (cred_len != sizeof(struct ucred)) { - errno = EINVAL; - return -1; - } - - *uid = cred.uid; - return 0; -#else -#if defined(HAVE_GETPEEREID) - gid_t gid; - return getpeereid(s, uid, &gid); -#endif - errno = ENOSYS; - return -1; -#endif -} - _PUBLIC_ int sys_getnameinfo(const struct sockaddr *psa, int salen, char *host, |