#include <sys/stat.h>
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-#ifdef HAVE_SYS_UN_H
-#include <sys/un.h>
-#endif
-#ifdef HAVE_NETINET_IN_H
-#include <netinet/in.h>
-#endif
-#ifdef HAVE_NETINET_TCP_H
-#include <netinet/tcp.h>
-#endif
-#ifdef HAVE_ARPA_INET_H
-#include <arpa/inet.h>
-#endif
-#ifdef HAVE_ARPA_INET_H
-#include <sys/resource.h>
-#endif
-
-#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
* libc compat functions.
*/
-#ifndef HAVE_GETPEEREID
-int getpeereid(int fd, uid_t *uid_p, gid_t *gid_p) _MUSTCHECK;
-#endif
#ifndef HAVE_CRYPT
static inline char *crypt(const char *p, const char *s) { return NULL; }
#endif
-#ifndef HAVE_INET_NTOP
-const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
-#endif
#ifndef HAVE_LSTAT
static inline int lstat(const char *path, struct stat *st) { return stat(path, st); }
#endif
#include <grp.h>
#endif
-/*
- * Get other side's uid for UNIX socket.
- *
- * Standardise on getpeereid() from BSDs.
- */
-#ifndef HAVE_GETPEEREID
-int getpeereid(int fd, uid_t *uid_p, gid_t *gid_p)
-{
-#ifdef SO_PEERCRED
- struct ucred cred;
- socklen_t len = sizeof(cred);
- if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) >= 0) {
- *uid_p = cred.uid;
- *gid_p = cred.gid;
- return 0;
- }
-#else /* !SO_PEERCRED */
-#ifdef HAVE_GETPEERUCRED
- ucred_t *cred = NULL;
- if (getpeerucred(fd, &cred) >= 0) {
- *uid_p = ucred_geteuid(cred);
- *gid_p = ucred_getegid(cred);
- ucred_free(cred);
- if (*uid_p >= 0 && *gid_p >= 0)
- return 0;
- }
-#endif /* HAVE_GETPEERUCRED */
-#endif /* !SO_PEERCRED */
- return -1;
-}
-#endif /* !HAVE_GETPEEREID */
-
void change_user(const char *user)
{
const struct passwd *pw;
fatal("setuid() failed to work");
}
-#ifndef HAVE_INET_NTOP
-const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
-{
- const unsigned char *p = src;
- if (af != AF_INET)
- return NULL;
- snprintf(dst, cnt, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
- return dst;
-}
-#endif
-
-
return res;
}
-static const char *sa2str(const struct sockaddr *sa)
-{
- static char buf[256];
-
- if (sa->sa_family == AF_INET) {
- struct sockaddr_in *in = (struct sockaddr_in *)sa;
- snprintf(buf, sizeof(buf), "%s:%d", inet_ntoa(in->sin_addr), ntohs(in->sin_port));
- } if (sa->sa_family == AF_UNIX) {
- struct sockaddr_un *un = (struct sockaddr_un *)sa;
- snprintf(buf, sizeof(buf), "unix:%s", un->sun_path);
- } else {
- snprintf(buf, sizeof(buf), "sa2str: unknown proto");
- }
- return buf;
-}
-
int safe_connect(int fd, const struct sockaddr *sa, socklen_t sa_len)
{
int res;
+ char buf[128];
loop:
res = connect(fd, sa, sa_len);
if (res < 0 && errno == EINTR)
goto loop;
if (res < 0 && (errno != EINPROGRESS || cf_verbose > 2))
- log_noise("connect(%d, %s) = %s", fd, sa2str(sa), strerror(errno));
+ log_noise("connect(%d, %s) = %s", fd, sa2str(sa, buf, sizeof(buf)), strerror(errno));
else if (cf_verbose > 2)
- log_noise("connect(%d, %s) = %d", fd, sa2str(sa), res);
+ log_noise("connect(%d, %s) = %d", fd, sa2str(sa, buf, sizeof(buf)), res);
return res;
}
int safe_accept(int fd, struct sockaddr *sa, socklen_t *sa_len_p)
{
int res;
+ char buf[128];
loop:
res = accept(fd, sa, sa_len_p);
if (res < 0 && errno == EINTR)
if (res < 0)
log_noise("safe_accept(%d) = %s", fd, strerror(errno));
else if (cf_verbose > 2)
- log_noise("safe_accept(%d) = %d (%s)", fd, res, sa2str(sa));
+ log_noise("safe_accept(%d) = %d (%s)", fd, res, sa2str(sa, buf, sizeof(buf)));
return res;
}
dest[i] = random() & 255;
}
-void socket_set_nonblocking(int fd, int val)
-{
- int flags, res;
-
- /* get old flags */
- flags = fcntl(fd, F_GETFL, 0);
- if (flags < 0)
- fatal_perror("fcntl(F_GETFL)");
-
- /* flip O_NONBLOCK */
- if (val)
- flags |= O_NONBLOCK;
- else
- flags &= ~O_NONBLOCK;
-
- /* set new flags */
- res = fcntl(fd, F_SETFL, flags);
- if (res < 0)
- fatal_perror("fcntl(F_SETFL)");
-}
-
/* set needed socket options */
void tune_socket(int sock, bool is_unix)
{