2000-10-27 11:50:33 +02:00
|
|
|
/* fhandler_socket.cc. See fhandler.h for a description of the fhandler classes.
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2018-02-28 18:56:13 +01:00
|
|
|
#include <sys/socket.h>
|
2012-07-06 15:52:19 +02:00
|
|
|
#include <asm/byteorder.h>
|
2002-10-21 03:00:58 +02:00
|
|
|
#include <unistd.h>
|
2012-03-08 10:36:11 +01:00
|
|
|
#include <sys/param.h>
|
2018-02-14 10:20:42 +01:00
|
|
|
#include <sys/statvfs.h>
|
2016-01-06 18:41:36 +01:00
|
|
|
#include <cygwin/acl.h>
|
2018-02-28 18:56:13 +01:00
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
2018-02-21 21:40:01 +01:00
|
|
|
#include "tls_pbuf.h"
|
2000-10-27 11:50:33 +02:00
|
|
|
|
2002-06-26 21:25:09 +02:00
|
|
|
extern "C" {
|
2018-02-21 21:40:01 +01:00
|
|
|
int sscanf (const char *, const char *, ...);
|
2002-06-26 21:25:09 +02:00
|
|
|
} /* End of "C" section */
|
|
|
|
|
2000-10-27 11:50:33 +02:00
|
|
|
/**********************************************************************/
|
|
|
|
/* fhandler_socket */
|
|
|
|
|
2004-04-09 10:43:29 +02:00
|
|
|
fhandler_socket::fhandler_socket () :
|
|
|
|
fhandler_base (),
|
2018-02-21 21:40:01 +01:00
|
|
|
uid (myself->uid),
|
|
|
|
gid (myself->gid),
|
|
|
|
mode (S_IFSOCK | S_IRWXU | S_IRWXG | S_IRWXO),
|
2018-02-07 16:16:51 +01:00
|
|
|
_rcvtimeo (INFINITE),
|
2018-03-12 15:26:12 +01:00
|
|
|
_sndtimeo (INFINITE)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_socket::~fhandler_socket ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:54:08 +01:00
|
|
|
char *
|
|
|
|
fhandler_socket::get_proc_fd_name (char *buf)
|
2005-02-19 21:03:18 +01:00
|
|
|
{
|
2018-02-22 16:54:08 +01:00
|
|
|
__small_sprintf (buf, "socket:[%lu]", get_plain_ino ());
|
|
|
|
return buf;
|
2005-02-19 21:03:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-02-22 16:54:08 +01:00
|
|
|
fhandler_socket::getpeereid (pid_t *pid, uid_t *euid, gid_t *egid)
|
2005-02-19 21:03:18 +01:00
|
|
|
{
|
2018-02-22 16:54:08 +01:00
|
|
|
set_errno (EINVAL);
|
2007-02-27 13:58:56 +01:00
|
|
|
return -1;
|
2005-02-19 21:03:18 +01:00
|
|
|
}
|
|
|
|
|
2007-01-21 23:54:05 +01:00
|
|
|
/* Definitions of old ifreq stuff used prior to Cygwin 1.7.0. */
|
|
|
|
#define OLD_SIOCGIFFLAGS _IOW('s', 101, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFADDR _IOW('s', 102, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFBRDADDR _IOW('s', 103, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFNETMASK _IOW('s', 104, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFHWADDR _IOW('s', 105, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFMETRIC _IOW('s', 106, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFMTU _IOW('s', 107, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFINDEX _IOW('s', 108, struct __old_ifreq)
|
|
|
|
|
|
|
|
#define CONV_OLD_TO_NEW_SIO(old) (((old)&0xff00ffff)|(((long)sizeof(struct ifreq)&IOCPARM_MASK)<<16))
|
|
|
|
|
|
|
|
struct __old_ifreq {
|
2011-06-06 07:02:13 +02:00
|
|
|
#define __OLD_IFNAMSIZ 16
|
2007-01-21 23:54:05 +01:00
|
|
|
union {
|
|
|
|
char ifrn_name[__OLD_IFNAMSIZ]; /* if name, e.g. "en0" */
|
|
|
|
} ifr_ifrn;
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct sockaddr ifru_addr;
|
|
|
|
struct sockaddr ifru_broadaddr;
|
|
|
|
struct sockaddr ifru_netmask;
|
|
|
|
struct sockaddr ifru_hwaddr;
|
|
|
|
short ifru_flags;
|
|
|
|
int ifru_metric;
|
|
|
|
int ifru_mtu;
|
|
|
|
int ifru_ifindex;
|
|
|
|
} ifr_ifru;
|
|
|
|
};
|
|
|
|
|
2000-10-27 11:50:33 +02:00
|
|
|
int
|
|
|
|
fhandler_socket::ioctl (unsigned int cmd, void *p)
|
|
|
|
{
|
2007-06-21 17:57:54 +02:00
|
|
|
extern int get_ifconf (struct ifconf *ifc, int what); /* net.cc */
|
2000-10-27 11:50:33 +02:00
|
|
|
int res;
|
2001-02-07 23:50:50 +01:00
|
|
|
struct ifconf ifc, *ifcp;
|
2007-01-21 23:54:05 +01:00
|
|
|
struct ifreq *ifrp;
|
2002-06-26 21:25:09 +02:00
|
|
|
|
2000-10-27 11:50:33 +02:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case SIOCGIFCONF:
|
2001-02-07 23:50:50 +01:00
|
|
|
ifcp = (struct ifconf *) p;
|
|
|
|
if (!ifcp)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-01-21 23:54:05 +01:00
|
|
|
if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
|
|
|
|
{
|
|
|
|
ifc.ifc_len = ifcp->ifc_len / sizeof (struct __old_ifreq)
|
2007-02-20 01:16:18 +01:00
|
|
|
* sizeof (struct ifreq);
|
2007-01-21 23:54:05 +01:00
|
|
|
ifc.ifc_buf = (caddr_t) alloca (ifc.ifc_len);
|
|
|
|
}
|
|
|
|
else
|
2007-02-20 01:16:18 +01:00
|
|
|
{
|
2007-01-21 23:54:05 +01:00
|
|
|
ifc.ifc_len = ifcp->ifc_len;
|
|
|
|
ifc.ifc_buf = ifcp->ifc_buf;
|
|
|
|
}
|
2007-06-21 17:57:54 +02:00
|
|
|
res = get_ifconf (&ifc, cmd);
|
2000-10-27 11:50:33 +02:00
|
|
|
if (res)
|
2002-09-30 06:35:18 +02:00
|
|
|
debug_printf ("error in get_ifconf");
|
2007-01-21 23:54:05 +01:00
|
|
|
if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
|
2007-02-20 01:16:18 +01:00
|
|
|
{
|
2007-01-21 23:54:05 +01:00
|
|
|
struct __old_ifreq *ifr = (struct __old_ifreq *) ifcp->ifc_buf;
|
|
|
|
for (ifrp = ifc.ifc_req;
|
|
|
|
(caddr_t) ifrp < ifc.ifc_buf + ifc.ifc_len;
|
|
|
|
++ifrp, ++ifr)
|
|
|
|
{
|
|
|
|
memcpy (&ifr->ifr_ifrn, &ifrp->ifr_ifrn, sizeof ifr->ifr_ifrn);
|
|
|
|
ifr->ifr_name[__OLD_IFNAMSIZ - 1] = '\0';
|
|
|
|
memcpy (&ifr->ifr_ifru, &ifrp->ifr_ifru, sizeof ifr->ifr_ifru);
|
|
|
|
}
|
|
|
|
ifcp->ifc_len = ifc.ifc_len / sizeof (struct ifreq)
|
|
|
|
* sizeof (struct __old_ifreq);
|
|
|
|
}
|
|
|
|
else
|
2007-02-20 01:16:18 +01:00
|
|
|
ifcp->ifc_len = ifc.ifc_len;
|
2000-10-27 11:50:33 +02:00
|
|
|
break;
|
2007-01-21 23:54:05 +01:00
|
|
|
case OLD_SIOCGIFFLAGS:
|
|
|
|
case OLD_SIOCGIFADDR:
|
|
|
|
case OLD_SIOCGIFBRDADDR:
|
|
|
|
case OLD_SIOCGIFNETMASK:
|
|
|
|
case OLD_SIOCGIFHWADDR:
|
|
|
|
case OLD_SIOCGIFMETRIC:
|
|
|
|
case OLD_SIOCGIFMTU:
|
|
|
|
case OLD_SIOCGIFINDEX:
|
|
|
|
cmd = CONV_OLD_TO_NEW_SIO (cmd);
|
|
|
|
/*FALLTHRU*/
|
2000-10-27 11:50:33 +02:00
|
|
|
case SIOCGIFFLAGS:
|
|
|
|
case SIOCGIFBRDADDR:
|
|
|
|
case SIOCGIFNETMASK:
|
|
|
|
case SIOCGIFADDR:
|
2001-02-07 23:50:50 +01:00
|
|
|
case SIOCGIFHWADDR:
|
|
|
|
case SIOCGIFMETRIC:
|
|
|
|
case SIOCGIFMTU:
|
2007-01-16 13:01:36 +01:00
|
|
|
case SIOCGIFINDEX:
|
2007-01-21 23:54:05 +01:00
|
|
|
case SIOCGIFFRNDLYNAM:
|
2007-06-14 18:02:32 +02:00
|
|
|
case SIOCGIFDSTADDR:
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
2007-01-21 23:54:05 +01:00
|
|
|
if (!p)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
2002-09-30 06:35:18 +02:00
|
|
|
debug_printf ("ifr == NULL");
|
2000-10-27 11:50:33 +02:00
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-01-21 23:54:05 +01:00
|
|
|
if (cmd > SIOCGIFINDEX && CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
|
|
|
|
{
|
|
|
|
debug_printf ("cmd not supported on this platform");
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ifc.ifc_len = 64 * sizeof (struct ifreq);
|
|
|
|
ifc.ifc_buf = (caddr_t) alloca (ifc.ifc_len);
|
|
|
|
if (cmd == SIOCGIFFRNDLYNAM)
|
|
|
|
{
|
|
|
|
struct ifreq_frndlyname *iff = (struct ifreq_frndlyname *)
|
2007-02-20 01:16:18 +01:00
|
|
|
alloca (64 * sizeof (struct ifreq_frndlyname));
|
2007-01-21 23:54:05 +01:00
|
|
|
for (int i = 0; i < 64; ++i)
|
|
|
|
ifc.ifc_req[i].ifr_frndlyname = &iff[i];
|
|
|
|
}
|
|
|
|
|
2007-06-21 17:57:54 +02:00
|
|
|
res = get_ifconf (&ifc, cmd);
|
2000-10-27 11:50:33 +02:00
|
|
|
if (res)
|
|
|
|
{
|
2002-09-30 06:35:18 +02:00
|
|
|
debug_printf ("error in get_ifconf");
|
2000-10-27 11:50:33 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-01-21 23:54:05 +01:00
|
|
|
if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
2007-01-21 23:54:05 +01:00
|
|
|
struct __old_ifreq *ifr = (struct __old_ifreq *) p;
|
|
|
|
debug_printf (" name: %s", ifr->ifr_name);
|
|
|
|
for (ifrp = ifc.ifc_req;
|
|
|
|
(caddr_t) ifrp < ifc.ifc_buf + ifc.ifc_len;
|
|
|
|
++ifrp)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
2007-01-21 23:54:05 +01:00
|
|
|
debug_printf ("testname: %s", ifrp->ifr_name);
|
|
|
|
if (! strcmp (ifrp->ifr_name, ifr->ifr_name))
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
2007-01-21 23:54:05 +01:00
|
|
|
memcpy (&ifr->ifr_ifru, &ifrp->ifr_ifru,
|
|
|
|
sizeof ifr->ifr_ifru);
|
2001-02-07 23:50:50 +01:00
|
|
|
break;
|
2007-01-21 23:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct ifreq *ifr = (struct ifreq *) p;
|
|
|
|
debug_printf (" name: %s", ifr->ifr_name);
|
|
|
|
for (ifrp = ifc.ifc_req;
|
|
|
|
(caddr_t) ifrp < ifc.ifc_buf + ifc.ifc_len;
|
|
|
|
++ifrp)
|
|
|
|
{
|
|
|
|
debug_printf ("testname: %s", ifrp->ifr_name);
|
|
|
|
if (! strcmp (ifrp->ifr_name, ifr->ifr_name))
|
|
|
|
{
|
|
|
|
if (cmd == SIOCGIFFRNDLYNAM)
|
|
|
|
/* The application has to care for the space. */
|
|
|
|
memcpy (ifr->ifr_frndlyname, ifrp->ifr_frndlyname,
|
|
|
|
sizeof (struct ifreq_frndlyname));
|
|
|
|
else
|
|
|
|
memcpy (&ifr->ifr_ifru, &ifrp->ifr_ifru,
|
|
|
|
sizeof ifr->ifr_ifru);
|
2007-01-16 13:01:36 +01:00
|
|
|
break;
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((caddr_t) ifrp >= ifc.ifc_buf + ifc.ifc_len)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2018-02-22 16:30:08 +01:00
|
|
|
res = fhandler_base::ioctl (cmd, p);
|
2000-10-27 11:50:33 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
syscall_printf ("%d = ioctl_socket(%x, %p)", res, cmd, p);
|
2000-10-27 11:50:33 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-04-23 11:44:36 +02:00
|
|
|
fhandler_socket::fcntl (int cmd, intptr_t arg)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
int request, current;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case F_SETFL:
|
|
|
|
{
|
2001-09-07 23:32:07 +02:00
|
|
|
/* Carefully test for the O_NONBLOCK or deprecated OLD_O_NDELAY flag.
|
2001-08-07 02:01:42 +02:00
|
|
|
Set only the flag that has been passed in. If both are set, just
|
|
|
|
record O_NONBLOCK. */
|
2013-04-23 11:44:36 +02:00
|
|
|
int new_flags = arg & O_NONBLOCK_MASK;
|
2001-08-07 02:01:42 +02:00
|
|
|
if ((new_flags & OLD_O_NDELAY) && (new_flags & O_NONBLOCK))
|
|
|
|
new_flags = O_NONBLOCK;
|
2001-09-07 23:32:07 +02:00
|
|
|
current = get_flags () & O_NONBLOCK_MASK;
|
2001-08-14 09:41:45 +02:00
|
|
|
request = new_flags ? 1 : 0;
|
2001-09-07 23:32:07 +02:00
|
|
|
if (!!current != !!new_flags && (res = ioctl (FIONBIO, &request)))
|
|
|
|
break;
|
2001-08-14 09:41:45 +02:00
|
|
|
set_flags ((get_flags () & ~O_NONBLOCK_MASK) | new_flags);
|
2001-09-07 23:32:07 +02:00
|
|
|
break;
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
res = fhandler_base::fcntl (cmd, arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:54:08 +01:00
|
|
|
int
|
|
|
|
fhandler_socket::open (int flags, mode_t mode)
|
2001-06-22 22:53:45 +02:00
|
|
|
{
|
2018-02-22 16:54:08 +01:00
|
|
|
set_errno (ENXIO);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __reg2
|
|
|
|
fhandler_socket::fstat (struct stat *buf)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
2018-02-27 15:30:00 +01:00
|
|
|
res = fhandler_base::fstat (buf);
|
2018-02-22 16:54:08 +01:00
|
|
|
if (!res)
|
2009-11-23 13:21:39 +01:00
|
|
|
{
|
2018-02-23 13:32:51 +01:00
|
|
|
buf->st_dev = FHDEV (DEV_SOCK_MAJOR, 0);
|
2018-02-22 16:54:08 +01:00
|
|
|
if (!(buf->st_ino = get_plain_ino ()))
|
|
|
|
sscanf (get_name (), "/proc/%*d/fd/socket:[%lld]",
|
|
|
|
(long long *) &buf->st_ino);
|
|
|
|
buf->st_uid = uid;
|
|
|
|
buf->st_gid = gid;
|
|
|
|
buf->st_mode = mode;
|
|
|
|
buf->st_size = 0;
|
2009-11-23 13:21:39 +01:00
|
|
|
}
|
2018-02-22 16:54:08 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __reg2
|
|
|
|
fhandler_socket::fstatvfs (struct statvfs *sfs)
|
|
|
|
{
|
|
|
|
memset (sfs, 0, sizeof (*sfs));
|
|
|
|
sfs->f_bsize = sfs->f_frsize = 4096;
|
|
|
|
sfs->f_namemax = NAME_MAX;
|
|
|
|
return 0;
|
2001-06-22 22:53:45 +02:00
|
|
|
}
|
2002-01-01 17:25:31 +01:00
|
|
|
|
2005-02-23 18:39:46 +01:00
|
|
|
int
|
2018-02-22 16:54:08 +01:00
|
|
|
fhandler_socket::fchmod (mode_t newmode)
|
2005-02-23 18:39:46 +01:00
|
|
|
{
|
2018-02-22 16:54:08 +01:00
|
|
|
mode = (newmode & ~S_IFMT) | S_IFSOCK;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket::fchown (uid_t newuid, gid_t newgid)
|
|
|
|
{
|
|
|
|
bool perms = check_token_membership (&well_known_admins_sid);
|
|
|
|
|
|
|
|
/* Admin rulez */
|
|
|
|
if (!perms)
|
|
|
|
{
|
|
|
|
/* Otherwise, new uid == old uid or current uid is fine */
|
|
|
|
if (newuid == ILLEGAL_UID || newuid == uid || newuid == myself->uid)
|
|
|
|
perms = true;
|
|
|
|
/* Otherwise, new gid == old gid or current gid is fine */
|
|
|
|
else if (newgid == ILLEGAL_GID || newgid == gid || newgid == myself->gid)
|
|
|
|
perms = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Last but not least, newgid in supplementary group list is fine */
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
gid_t *gids = (gid_t *) tp.w_get ();
|
|
|
|
int num = getgroups (65536 / sizeof (*gids), gids);
|
|
|
|
|
|
|
|
for (int idx = 0; idx < num; ++idx)
|
|
|
|
if (newgid == gids[idx])
|
|
|
|
{
|
|
|
|
perms = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (perms)
|
|
|
|
{
|
|
|
|
if (newuid != ILLEGAL_UID)
|
|
|
|
uid = newuid;
|
|
|
|
if (newgid != ILLEGAL_GID)
|
|
|
|
gid = newgid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
set_errno (EPERM);
|
2014-08-22 11:21:33 +02:00
|
|
|
return -1;
|
2005-02-23 18:39:46 +01:00
|
|
|
}
|
2018-02-22 16:54:08 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket::facl (int cmd, int nentries, aclent_t *aclbufp)
|
|
|
|
{
|
|
|
|
set_errno (EOPNOTSUPP);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket::link (const char *newpath)
|
|
|
|
{
|
|
|
|
return fhandler_base::link (newpath);
|
|
|
|
}
|