Add support for srtuct ucred on BSD.

This requires building with _GNU_SOURCE to get access to struct ucred on Linux. Previously the test failed on every platform except glibc <2.8 or so.

Building with _GNU_SOURCE causes TRUE to be defined so we can't use this as a variable anymore, and MIN/MAX so don't define our own.
This commit is contained in:
Daniel O'Connor 2012-05-21 12:22:50 +09:30
parent c5e490924f
commit 990a8f3057
7 changed files with 39 additions and 21 deletions

View File

@ -52,8 +52,8 @@ int packetSendFollowup(struct in_addr destination,
fprintf(stderr,"Could not create UDP socket.\n");
exit(-3);
}
int TRUE=1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &TRUE, sizeof(TRUE));
r=1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &r, sizeof(r));
}
r=sendto(sock,packet,packet_len,0,(struct sockaddr *)&peer_addr,sizeof(peer_addr));
@ -86,8 +86,8 @@ int packetSendRequest(int method,unsigned char *packet,int packet_len,int batchP
fprintf(stderr,"Could not create UDP socket.\n");
exit(-3);
}
int TRUE=1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &TRUE, sizeof(TRUE));
i=1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
}
/* Deal with special case */

View File

@ -17,7 +17,6 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE // For asprintf()
#include <sys/time.h>
#include <sys/wait.h>
#include <math.h>

View File

@ -1,6 +1,8 @@
dnl Process this file with autoconf to produce a configure script.
AC_INIT(dna.c)
CPPFLAGS=-D_GNU_SOURCE
dnl Set $host_os, which is needed by javac detection.
AC_CANONICAL_SYSTEM
@ -37,11 +39,16 @@ dnl Math library functions for spandsp
AC_CHECK_HEADERS([math.h], [INSERT_MATH_HEADER="#include <math.h>"])
AC_CHECK_HEADERS([float.h])
dnl Math library functions for spandsp
dnl Check for Linux version of struct ucred
AC_CHECK_MEMBER(struct ucred.uid,
[AC_DEFINE([HAVE_STRUCT_UCRED], 1)],,
[AC_DEFINE([HAVE_LINUX_STRUCT_UCRED], 1)],,
[#include <sys/socket.h>])
dnl Check for BSD version of struct ucred
AC_CHECK_MEMBER(struct xucred.cr_uid,
[AC_DEFINE([HAVE_BSD_STRUCT_UCRED], 1)],,
[#include <sys/ucred.h>])
dnl Check for a working Java compiler, keep going if unsuccessful.
dnl *** Kludge: override AC_MSG_ERROR because AC_PROG_JAVAC does not have
dnl *** [if-found] and [if-not-found] action parameters.
@ -75,9 +82,11 @@ AC_CHECK_HEADERS(
sys/socket.h \
sys/mman.h \
sys/time.h \
sys/ucred.h \
poll.h \
netdb.h \
linux/if.h \
linux/ioctl.h \
linux/netlink.h \
linux/rtnetlink.h \
net/if.h \

View File

@ -27,13 +27,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "rhizome.h"
#include <sys/stat.h>
#ifndef HAVE_STRUCT_UCRED
struct ucred {
pid_t pid;
uid_t uid;
gid_t gid;
};
#endif // HAVE_STRUCT_UCRED
#if defined(LOCAL_PEERCRED) && !defined(SO_PEERCRED)
#define SO_PEERCRED LOCAL_PEERCRED
#endif
/* really shouldn't need more than 2:
1 for rhizome
@ -202,7 +198,14 @@ int monitor_poll()
WHYF("ignored_length=%d",ignored_length);
int res = fcntl(s,F_SETFL, O_NONBLOCK);
if (res) { close(s); continue; }
#if defined(HAVE_LINUX_STRUCT_UCRED)
struct ucred ucred;
#elif defined(HAVE_BSD_STRUCT_UCRED)
struct xucred ucred;
#else
#error "Unknown ucred struct"
#endif
uid_t otheruid;
socklen_t len=sizeof(ucred);
res = getsockopt(s,SOL_SOCKET,SO_PEERCRED,&ucred,&len);
if (len>sizeof(ucred)) {
@ -211,9 +214,14 @@ int monitor_poll()
if (res) {
WHY("Failed to read credentials of monitor.socket client");
close(s); continue; }
if (ucred.uid&&(ucred.uid!=getuid())) {
#if defined(HAVE_LINUX_STRUCT_UCRED)
otheruid = ucred.uid;
#elif defined(HAVE_BSD_STRUCT_UCRED)
otheruid = ucred.cr_uid;
#endif
if (otheruid&&(otheruid!=getuid())) {
WHYF("monitor.socket client has wrong uid (%d versus %d)",
ucred.uid,getuid());
otheruid,getuid());
write(s,"\nCLOSE:Incorrect UID\n",strlen("\nCLOSE:Incorrect UID\n"));
close(s); continue;
}

View File

@ -11,8 +11,6 @@
#define NUM_BUFS (8)
#define ECHO_LEN (128)
#define ADAPT_MODE (ECHO_CAN_USE_ADAPTION | ECHO_CAN_USE_NLP | ECHO_CAN_USE_CNG)
#define MIN(x, y) ((x) > (y) ? y : x)
#define MAX(x, y) ((x) < (y) ? y : x)
#define CODEC2_BYTES_PER_FRAME ((CODEC2_BITS_PER_FRAME + 7) / 8)

View File

@ -59,6 +59,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_IFADDRS_H
#include <ifaddrs.h>
#endif
#ifdef HAVE_SYS_UCRED_H
#include <sys/ucred.h>
#endif
#endif
#if !defined(FORASTERISK) && !defined(s_addr)

View File

@ -791,11 +791,11 @@ int createServerSocket()
fcntl(sock, F_SETFL,
fcntl(sock, F_GETFL, NULL)|O_CLOEXEC);
int TRUE=1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &TRUE, sizeof(TRUE));
int i=1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
errno=0;
if(setsockopt(sock, IPPROTO_IP, IP_RECVTTL, &TRUE,sizeof(TRUE))<0)
if(setsockopt(sock, IPPROTO_IP, IP_RECVTTL, &i,sizeof(i))<0)
WHY_perror("setsockopt(IP_RECVTTL)");
bind_addr.sin_family = AF_INET;