|
Packit |
8f70b4 |
/* Copyright (C) 2011-2018 Free Software Foundation, Inc.
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
This program is free software: you can redistribute it and/or modify
|
|
Packit |
8f70b4 |
it under the terms of the GNU General Public License as published by
|
|
Packit |
8f70b4 |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit |
8f70b4 |
(at your option) any later version.
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
This program is distributed in the hope that it will be useful,
|
|
Packit |
8f70b4 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
8f70b4 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
8f70b4 |
GNU General Public License for more details.
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
You should have received a copy of the GNU General Public License
|
|
Packit |
8f70b4 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
#include <config.h>
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
/* Specification. */
|
|
Packit |
8f70b4 |
#include "passfd.h"
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
#include <errno.h>
|
|
Packit |
8f70b4 |
#include <fcntl.h>
|
|
Packit |
8f70b4 |
#include <stddef.h>
|
|
Packit |
8f70b4 |
#include <stdlib.h>
|
|
Packit |
8f70b4 |
#include <string.h>
|
|
Packit |
8f70b4 |
#include <sys/types.h>
|
|
Packit |
8f70b4 |
#include <unistd.h>
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
#include <sys/socket.h>
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
#include "cloexec.h"
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
/* The code that uses CMSG_FIRSTHDR is enabled on
|
|
Packit |
8f70b4 |
Linux, Mac OS X, FreeBSD, OpenBSD, NetBSD, AIX, OSF/1, Cygwin.
|
|
Packit |
8f70b4 |
The code that uses HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS is enabled on
|
|
Packit |
8f70b4 |
HP-UX, IRIX, Solaris. */
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
/* MSG_CMSG_CLOEXEC is defined only on Linux, as of 2011. */
|
|
Packit |
8f70b4 |
#ifndef MSG_CMSG_CLOEXEC
|
|
Packit |
8f70b4 |
# define MSG_CMSG_CLOEXEC 0
|
|
Packit |
8f70b4 |
#endif
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
#if HAVE_SENDMSG
|
|
Packit |
8f70b4 |
/* sendfd sends the file descriptor fd along the socket
|
|
Packit |
8f70b4 |
to a process calling recvfd on the other end.
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
Return 0 on success, or -1 with errno set in case of error.
|
|
Packit |
8f70b4 |
*/
|
|
Packit |
8f70b4 |
int
|
|
Packit |
8f70b4 |
sendfd (int sock, int fd)
|
|
Packit |
8f70b4 |
{
|
|
Packit |
8f70b4 |
char byte = 0;
|
|
Packit |
8f70b4 |
struct iovec iov;
|
|
Packit |
8f70b4 |
struct msghdr msg;
|
|
Packit |
8f70b4 |
# ifdef CMSG_FIRSTHDR
|
|
Packit |
8f70b4 |
struct cmsghdr *cmsg;
|
|
Packit |
8f70b4 |
char buf[CMSG_SPACE (sizeof fd)];
|
|
Packit |
8f70b4 |
# endif
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
/* send at least one char */
|
|
Packit |
8f70b4 |
memset (&msg, 0, sizeof msg);
|
|
Packit |
8f70b4 |
iov.iov_base = &byte;
|
|
Packit |
8f70b4 |
iov.iov_len = 1;
|
|
Packit |
8f70b4 |
msg.msg_iov = &iov;
|
|
Packit |
8f70b4 |
msg.msg_iovlen = 1;
|
|
Packit |
8f70b4 |
msg.msg_name = NULL;
|
|
Packit |
8f70b4 |
msg.msg_namelen = 0;
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
# ifdef CMSG_FIRSTHDR
|
|
Packit |
8f70b4 |
msg.msg_control = buf;
|
|
Packit |
8f70b4 |
msg.msg_controllen = sizeof buf;
|
|
Packit |
8f70b4 |
cmsg = CMSG_FIRSTHDR (&msg;;
|
|
Packit |
8f70b4 |
cmsg->cmsg_level = SOL_SOCKET;
|
|
Packit |
8f70b4 |
cmsg->cmsg_type = SCM_RIGHTS;
|
|
Packit |
8f70b4 |
cmsg->cmsg_len = CMSG_LEN (sizeof fd);
|
|
Packit |
8f70b4 |
/* Initialize the payload: */
|
|
Packit |
8f70b4 |
memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
|
|
Packit |
8f70b4 |
msg.msg_controllen = cmsg->cmsg_len;
|
|
Packit |
8f70b4 |
# elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
|
|
Packit |
8f70b4 |
msg.msg_accrights = &fd;
|
|
Packit |
8f70b4 |
msg.msg_accrightslen = sizeof fd;
|
|
Packit |
8f70b4 |
# else
|
|
Packit |
8f70b4 |
errno = ENOSYS;
|
|
Packit |
8f70b4 |
return -1;
|
|
Packit |
8f70b4 |
# endif
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
if (sendmsg (sock, &msg, 0) != iov.iov_len)
|
|
Packit |
8f70b4 |
return -1;
|
|
Packit |
8f70b4 |
return 0;
|
|
Packit |
8f70b4 |
}
|
|
Packit |
8f70b4 |
#else
|
|
Packit |
8f70b4 |
int
|
|
Packit |
8f70b4 |
sendfd (int sock _GL_UNUSED, int fd _GL_UNUSED)
|
|
Packit |
8f70b4 |
{
|
|
Packit |
8f70b4 |
errno = ENOSYS;
|
|
Packit |
8f70b4 |
return -1;
|
|
Packit |
8f70b4 |
}
|
|
Packit |
8f70b4 |
#endif
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
#if HAVE_RECVMSG
|
|
Packit |
8f70b4 |
/* recvfd receives a file descriptor through the socket.
|
|
Packit |
8f70b4 |
The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>).
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
Return the fd on success, or -1 with errno set in case of error.
|
|
Packit |
8f70b4 |
*/
|
|
Packit |
8f70b4 |
int
|
|
Packit |
8f70b4 |
recvfd (int sock, int flags)
|
|
Packit |
8f70b4 |
{
|
|
Packit |
8f70b4 |
char byte = 0;
|
|
Packit |
8f70b4 |
struct iovec iov;
|
|
Packit |
8f70b4 |
struct msghdr msg;
|
|
Packit |
8f70b4 |
int fd = -1;
|
|
Packit |
8f70b4 |
ssize_t len;
|
|
Packit |
8f70b4 |
# ifdef CMSG_FIRSTHDR
|
|
Packit |
8f70b4 |
struct cmsghdr *cmsg;
|
|
Packit |
8f70b4 |
char buf[CMSG_SPACE (sizeof fd)];
|
|
Packit |
8f70b4 |
int flags_recvmsg = flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0;
|
|
Packit |
8f70b4 |
# endif
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
if ((flags & ~O_CLOEXEC) != 0)
|
|
Packit |
8f70b4 |
{
|
|
Packit |
8f70b4 |
errno = EINVAL;
|
|
Packit |
8f70b4 |
return -1;
|
|
Packit |
8f70b4 |
}
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
/* send at least one char */
|
|
Packit |
8f70b4 |
memset (&msg, 0, sizeof msg);
|
|
Packit |
8f70b4 |
iov.iov_base = &byte;
|
|
Packit |
8f70b4 |
iov.iov_len = 1;
|
|
Packit |
8f70b4 |
msg.msg_iov = &iov;
|
|
Packit |
8f70b4 |
msg.msg_iovlen = 1;
|
|
Packit |
8f70b4 |
msg.msg_name = NULL;
|
|
Packit |
8f70b4 |
msg.msg_namelen = 0;
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
# ifdef CMSG_FIRSTHDR
|
|
Packit |
8f70b4 |
msg.msg_control = buf;
|
|
Packit |
8f70b4 |
msg.msg_controllen = sizeof buf;
|
|
Packit |
8f70b4 |
cmsg = CMSG_FIRSTHDR (&msg;;
|
|
Packit |
8f70b4 |
cmsg->cmsg_level = SOL_SOCKET;
|
|
Packit |
8f70b4 |
cmsg->cmsg_type = SCM_RIGHTS;
|
|
Packit |
8f70b4 |
cmsg->cmsg_len = CMSG_LEN (sizeof fd);
|
|
Packit |
8f70b4 |
/* Initialize the payload: */
|
|
Packit |
8f70b4 |
memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
|
|
Packit |
8f70b4 |
msg.msg_controllen = cmsg->cmsg_len;
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
len = recvmsg (sock, &msg, flags_recvmsg);
|
|
Packit |
8f70b4 |
if (len < 0)
|
|
Packit |
8f70b4 |
return -1;
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
cmsg = CMSG_FIRSTHDR (&msg;;
|
|
Packit |
8f70b4 |
/* be paranoiac */
|
|
Packit |
8f70b4 |
if (len == 0 || cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof fd)
|
|
Packit |
8f70b4 |
|| cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
|
|
Packit |
8f70b4 |
{
|
|
Packit |
8f70b4 |
/* fake errno: at end the file is not available */
|
|
Packit |
8f70b4 |
errno = len ? EACCES : ENOTCONN;
|
|
Packit |
8f70b4 |
return -1;
|
|
Packit |
8f70b4 |
}
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
memcpy (&fd, CMSG_DATA (cmsg), sizeof fd);
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
/* set close-on-exec flag */
|
|
Packit |
8f70b4 |
if (!MSG_CMSG_CLOEXEC && (flags & O_CLOEXEC))
|
|
Packit |
8f70b4 |
{
|
|
Packit |
8f70b4 |
if (set_cloexec_flag (fd, true) < 0)
|
|
Packit |
8f70b4 |
{
|
|
Packit |
8f70b4 |
int saved_errno = errno;
|
|
Packit |
8f70b4 |
(void) close (fd);
|
|
Packit |
8f70b4 |
errno = saved_errno;
|
|
Packit |
8f70b4 |
return -1;
|
|
Packit |
8f70b4 |
}
|
|
Packit |
8f70b4 |
}
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
# elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
|
|
Packit |
8f70b4 |
msg.msg_accrights = &fd;
|
|
Packit |
8f70b4 |
msg.msg_accrightslen = sizeof fd;
|
|
Packit |
8f70b4 |
if (recvmsg (sock, &msg, 0) < 0)
|
|
Packit |
8f70b4 |
return -1;
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
/* set close-on-exec flag */
|
|
Packit |
8f70b4 |
if (flags & O_CLOEXEC)
|
|
Packit |
8f70b4 |
{
|
|
Packit |
8f70b4 |
if (set_cloexec_flag (fd, true) < 0)
|
|
Packit |
8f70b4 |
{
|
|
Packit |
8f70b4 |
int saved_errno = errno;
|
|
Packit |
8f70b4 |
close (fd);
|
|
Packit |
8f70b4 |
errno = saved_errno;
|
|
Packit |
8f70b4 |
return -1;
|
|
Packit |
8f70b4 |
}
|
|
Packit |
8f70b4 |
}
|
|
Packit |
8f70b4 |
# else
|
|
Packit |
8f70b4 |
errno = ENOSYS;
|
|
Packit |
8f70b4 |
# endif
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
return fd;
|
|
Packit |
8f70b4 |
}
|
|
Packit |
8f70b4 |
#else
|
|
Packit |
8f70b4 |
int
|
|
Packit |
8f70b4 |
recvfd (int sock _GL_UNUSED, int flags _GL_UNUSED)
|
|
Packit |
8f70b4 |
{
|
|
Packit |
8f70b4 |
errno = ENOSYS;
|
|
Packit |
8f70b4 |
return -1;
|
|
Packit |
8f70b4 |
}
|
|
Packit |
8f70b4 |
#endif
|