2018-02-27 19:20:58 +00:00
|
|
|
/*-
|
2015-02-24 18:19:20 +00:00
|
|
|
* SSLsplit - transparent SSL/TLS interception
|
2018-02-27 19:20:58 +00:00
|
|
|
* https://www.roe.ch/SSLsplit
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2018, Daniel Roethlisberger <daniel@roe.ch>.
|
2012-04-13 12:47:30 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2018-02-27 19:20:58 +00:00
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
2012-04-13 12:47:30 +00:00
|
|
|
*
|
2018-02-27 19:20:58 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2012-04-13 12:47:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sys.h"
|
|
|
|
|
|
|
|
#include "log.h"
|
2014-11-25 22:45:40 +00:00
|
|
|
#include "defaults.h"
|
2012-04-13 12:47:30 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2018-11-03 15:23:31 +00:00
|
|
|
#include <sys/ioctl.h>
|
2014-11-24 21:49:02 +00:00
|
|
|
#include <sys/uio.h>
|
2012-04-13 12:47:30 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
2014-11-24 21:01:52 +00:00
|
|
|
#include <sys/un.h>
|
2018-11-03 15:23:31 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <net/if.h>
|
2012-04-13 12:47:30 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <fts.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifndef _SC_NPROCESSORS_ONLN
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif /* !_SC_NPROCESSORS_ONLN */
|
|
|
|
|
2014-10-18 19:46:44 +00:00
|
|
|
#if HAVE_DARWIN_LIBPROC
|
|
|
|
#include <libproc.h>
|
|
|
|
#endif
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
#include <event2/util.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Permanently drop from root privileges to an unprivileged user account.
|
|
|
|
* Sets the real, effective and stored user and group ID and the list of
|
|
|
|
* ancillary groups. This is only safe if the effective user ID is 0.
|
|
|
|
* If username is unset and the effective uid != uid, drop privs to uid.
|
|
|
|
* This is to support setuid bit configurations.
|
2014-10-18 06:34:51 +00:00
|
|
|
* If groupname is set, it will be used instead of the user's default primary
|
|
|
|
* group.
|
2012-04-13 12:47:30 +00:00
|
|
|
* If jaildir is set, also chroot to jaildir after reading system files
|
|
|
|
* but before dropping privileges.
|
|
|
|
* Returns 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
2014-10-18 06:34:51 +00:00
|
|
|
sys_privdrop(const char *username, const char *groupname, const char *jaildir)
|
2012-04-13 12:47:30 +00:00
|
|
|
{
|
|
|
|
struct passwd *pw = NULL;
|
2014-10-18 06:34:51 +00:00
|
|
|
struct group *gr = NULL;
|
2012-04-13 12:47:30 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2014-10-18 06:34:51 +00:00
|
|
|
if (groupname) {
|
2014-11-23 21:52:09 +00:00
|
|
|
errno = 0;
|
2014-10-18 06:34:51 +00:00
|
|
|
if (!(gr = getgrnam(groupname))) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to getgrnam group '%s': %s\n",
|
2014-10-18 06:34:51 +00:00
|
|
|
groupname, strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
if (username) {
|
2014-11-23 21:52:09 +00:00
|
|
|
errno = 0;
|
2012-04-13 12:47:30 +00:00
|
|
|
if (!(pw = getpwnam(username))) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to getpwnam user '%s': %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
username, strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
2014-10-18 06:34:51 +00:00
|
|
|
|
|
|
|
if (gr != NULL) {
|
|
|
|
pw->pw_gid = gr->gr_gid;
|
|
|
|
}
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
if (initgroups(username, pw->pw_gid) == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to initgroups user '%s': %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
username, strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jaildir) {
|
|
|
|
if (chroot(jaildir) == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to chroot to '%s': %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
jaildir, strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (chdir("/") == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to chdir to '/': %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (username) {
|
|
|
|
if (setgid(pw->pw_gid) == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to setgid to %i: %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
pw->pw_gid, strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (setuid(pw->pw_uid) == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to setuid to %i: %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
pw->pw_uid, strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (getuid() != geteuid()) {
|
|
|
|
if (setuid(getuid()) == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to setuid(getuid()): %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
error:
|
|
|
|
if (pw) {
|
|
|
|
endpwent();
|
|
|
|
}
|
2018-11-03 15:23:31 +00:00
|
|
|
if (gr) {
|
|
|
|
endgrent();
|
|
|
|
}
|
2012-04-13 12:47:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-23 21:52:09 +00:00
|
|
|
/*
|
2018-11-03 15:23:31 +00:00
|
|
|
* If the user exists and on successful lookup, return 0 and if uid != NULL,
|
|
|
|
* write the uid of *username* to the value pointed to by uid.
|
|
|
|
* Return -1 on failure or if the user does not exist.
|
2014-11-23 21:52:09 +00:00
|
|
|
*/
|
|
|
|
int
|
2018-11-03 15:23:31 +00:00
|
|
|
sys_uid(const char *username, uid_t *uid)
|
2014-11-23 21:52:09 +00:00
|
|
|
{
|
2018-11-03 15:23:31 +00:00
|
|
|
struct passwd *pw;
|
|
|
|
int rv;
|
|
|
|
|
2014-11-23 21:52:09 +00:00
|
|
|
errno = 0;
|
2018-11-03 15:23:31 +00:00
|
|
|
if (!(pw = getpwnam(username))) {
|
2014-11-23 21:52:09 +00:00
|
|
|
if (errno != 0 && errno != ENOENT) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to load user '%s': %s (%i)\n",
|
2014-11-23 21:52:09 +00:00
|
|
|
username, strerror(errno), errno);
|
|
|
|
}
|
2018-11-03 15:23:31 +00:00
|
|
|
rv = -1;
|
|
|
|
} else {
|
|
|
|
if (uid)
|
|
|
|
*uid = pw->pw_uid;
|
|
|
|
rv = 0;
|
2014-11-23 21:52:09 +00:00
|
|
|
}
|
|
|
|
endpwent();
|
2018-11-03 15:23:31 +00:00
|
|
|
return rv;
|
2014-11-23 21:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-11-03 15:23:31 +00:00
|
|
|
* Returns 1 if username can be loaded from user database, 0 otherwise.
|
2014-11-23 21:52:09 +00:00
|
|
|
*/
|
|
|
|
int
|
2018-11-03 15:23:31 +00:00
|
|
|
sys_isuser(const char *username)
|
|
|
|
{
|
|
|
|
return sys_uid(username, NULL) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the group exists and on successful lookup, return 0 and if gid != NULL,
|
|
|
|
* write the gid of *groupname* to the value pointed to by gid.
|
|
|
|
* Return -1 on failure or if the group does not exist.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sys_gid(const char *groupname, gid_t *gid)
|
2014-11-23 21:52:09 +00:00
|
|
|
{
|
2018-11-03 15:23:31 +00:00
|
|
|
struct group *gr;
|
|
|
|
int rv;
|
|
|
|
|
2014-11-23 21:52:09 +00:00
|
|
|
errno = 0;
|
2018-11-03 15:23:31 +00:00
|
|
|
if (!(gr = getgrnam(groupname))) {
|
2014-11-23 21:52:09 +00:00
|
|
|
if (errno != 0 && errno != ENOENT) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to load group '%s': %s (%i)\n",
|
2014-11-23 21:52:09 +00:00
|
|
|
groupname, strerror(errno), errno);
|
|
|
|
}
|
2018-11-03 15:23:31 +00:00
|
|
|
rv = -1;
|
|
|
|
} else {
|
|
|
|
if (gid)
|
|
|
|
*gid = gr->gr_gid;
|
|
|
|
rv = 0;
|
2014-11-23 21:52:09 +00:00
|
|
|
}
|
2018-11-03 15:23:31 +00:00
|
|
|
endgrent();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns 1 if groupname can be loaded from group database, 0 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sys_isgroup(const char *groupname)
|
|
|
|
{
|
|
|
|
return sys_gid(groupname, NULL) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns 1 if username is equivalent to the current effective UID.
|
|
|
|
* Returns 0 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sys_isgeteuid(const char *username)
|
|
|
|
{
|
|
|
|
uid_t uid;
|
|
|
|
|
|
|
|
if (sys_uid(username, &uid) == -1)
|
|
|
|
return 0;
|
|
|
|
if (uid == geteuid())
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2014-11-23 21:52:09 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
/*
|
|
|
|
* Open and lock process ID file fn.
|
|
|
|
* Returns open file descriptor on success or -1 on errors.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sys_pidf_open(const char *fn)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2014-11-25 22:45:40 +00:00
|
|
|
if ((fd = open(fn, O_RDWR|O_CREAT, DFLT_PIDFMODE)) == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to open '%s': %s\n", fn,
|
2012-04-13 12:47:30 +00:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to lock '%s': %s\n", fn,
|
2012-04-13 12:47:30 +00:00
|
|
|
strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write process ID to open process ID file descriptor fd.
|
|
|
|
* Returns 0 on success, -1 on errors.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sys_pidf_write(int fd)
|
|
|
|
{
|
|
|
|
char pidbuf[4*sizeof(pid_t)];
|
|
|
|
int rv;
|
2018-01-18 00:18:53 +00:00
|
|
|
ssize_t n;
|
2012-04-13 12:47:30 +00:00
|
|
|
|
|
|
|
rv = snprintf(pidbuf, sizeof(pidbuf), "%d\n", getpid());
|
|
|
|
if (rv == -1 || rv >= (int)sizeof(pidbuf))
|
|
|
|
return -1;
|
|
|
|
|
2018-01-18 00:18:53 +00:00
|
|
|
n = write(fd, pidbuf, strlen(pidbuf));
|
|
|
|
if (n < (ssize_t)strlen(pidbuf))
|
|
|
|
return -1;
|
2012-04-13 12:47:30 +00:00
|
|
|
|
2018-01-18 00:18:53 +00:00
|
|
|
rv = fsync(fd);
|
|
|
|
if (rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rv = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
|
|
|
|
if (rv == -1)
|
|
|
|
return -1;
|
2012-04-13 12:47:30 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close and remove open process ID file before quitting.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sys_pidf_close(int fd, const char *fn)
|
|
|
|
{
|
|
|
|
unlink(fn);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2014-10-18 20:16:50 +00:00
|
|
|
/*
|
|
|
|
* Converts a local uid into a printable string representation.
|
|
|
|
* Returns an allocated buffer which must be freed by caller, or NULL on error.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
sys_user_str(uid_t uid)
|
|
|
|
{
|
2014-11-19 19:23:01 +00:00
|
|
|
static int bufsize = 0;
|
|
|
|
|
|
|
|
if (!bufsize) {
|
|
|
|
/* on some platforms this compiles, but does not succeed */
|
|
|
|
if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) {
|
|
|
|
bufsize = 64;
|
|
|
|
}
|
2014-10-18 20:16:50 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 15:19:36 +00:00
|
|
|
char *buf, *newbuf;
|
2014-10-18 20:16:50 +00:00
|
|
|
struct passwd pwd, *result = NULL;
|
2014-11-19 19:23:01 +00:00
|
|
|
int rv;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (!(buf = malloc(bufsize)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
rv = getpwuid_r(uid, &pwd, buf, bufsize, &result);
|
|
|
|
if (rv == 0) {
|
|
|
|
if (result) {
|
|
|
|
name = strdup(pwd.pw_name);
|
|
|
|
free(buf);
|
|
|
|
return name;
|
|
|
|
}
|
2014-11-21 15:19:36 +00:00
|
|
|
free(buf);
|
2014-11-19 19:23:01 +00:00
|
|
|
|
|
|
|
/* no entry found; return the integer representation */
|
|
|
|
if (asprintf(&name, "%llu", (long long) uid) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
bufsize *= 2;
|
2014-11-21 15:19:36 +00:00
|
|
|
if (!(newbuf = realloc(buf, bufsize))) {
|
|
|
|
free(buf);
|
2014-10-18 20:16:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-21 15:19:36 +00:00
|
|
|
buf = newbuf;
|
2014-11-19 19:23:01 +00:00
|
|
|
} while (rv == ERANGE);
|
2014-10-18 20:16:50 +00:00
|
|
|
|
2014-11-21 15:19:36 +00:00
|
|
|
free(buf);
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to lookup uid: %s (%i)\n", strerror(rv), rv);
|
2014-11-19 19:23:01 +00:00
|
|
|
return NULL;
|
2014-10-18 20:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts a local gid into a printable string representation.
|
|
|
|
* Returns an allocated buffer which must be freed by caller, or NULL on error.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
sys_group_str(gid_t gid)
|
|
|
|
{
|
2014-11-19 19:23:01 +00:00
|
|
|
static int bufsize = 0;
|
|
|
|
|
|
|
|
if (!bufsize) {
|
|
|
|
/* on some platforms this compiles, but does not succeed */
|
|
|
|
if ((bufsize = sysconf(_SC_GETGR_R_SIZE_MAX)) == -1) {
|
|
|
|
bufsize = 64;
|
|
|
|
}
|
2014-10-18 20:16:50 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 15:19:36 +00:00
|
|
|
char *buf, *newbuf;
|
2014-10-18 20:16:50 +00:00
|
|
|
struct group grp, *result = NULL;
|
2014-11-19 19:23:01 +00:00
|
|
|
int rv;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (!(buf = malloc(bufsize)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
rv = getgrgid_r(gid, &grp, buf, bufsize, &result);
|
|
|
|
if (rv == 0) {
|
|
|
|
if (result) {
|
|
|
|
name = strdup(grp.gr_name);
|
|
|
|
free(buf);
|
|
|
|
return name;
|
|
|
|
}
|
2014-11-21 15:19:36 +00:00
|
|
|
free(buf);
|
2014-11-19 19:23:01 +00:00
|
|
|
|
|
|
|
/* no entry found; return the integer representation */
|
|
|
|
if (asprintf(&name, "%llu", (long long) gid) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
bufsize *= 2;
|
2014-11-21 15:19:36 +00:00
|
|
|
if (!(newbuf = realloc(buf, bufsize))) {
|
|
|
|
free(buf);
|
2014-10-18 20:16:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-21 15:19:36 +00:00
|
|
|
buf = newbuf;
|
2014-11-19 19:23:01 +00:00
|
|
|
} while (rv == ERANGE);
|
2014-10-18 20:16:50 +00:00
|
|
|
|
2014-11-21 15:19:36 +00:00
|
|
|
free(buf);
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to lookup gid: %s (%i)\n", strerror(rv), rv);
|
2014-11-19 19:23:01 +00:00
|
|
|
return NULL;
|
2014-10-18 20:16:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 23:51:26 +00:00
|
|
|
/*
|
|
|
|
* Determine address family of addr
|
|
|
|
*/
|
|
|
|
int
|
2018-11-03 15:23:31 +00:00
|
|
|
sys_get_af(const char *addr)
|
2018-09-14 23:51:26 +00:00
|
|
|
{
|
|
|
|
if (strstr(addr, ":"))
|
|
|
|
return AF_INET6;
|
|
|
|
else if (!strpbrk(addr, "abcdefghijklmnopqrstu"
|
|
|
|
"vwxyzABCDEFGHIJKLMNOP"
|
|
|
|
"QRSTUVWXYZ-"))
|
|
|
|
return AF_INET;
|
|
|
|
else
|
|
|
|
return AF_UNSPEC;
|
|
|
|
}
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
/*
|
|
|
|
* Parse an ascii host/IP and port tuple into a sockaddr_storage.
|
|
|
|
* On success, returns address family and fills in addr, addrlen.
|
2012-12-06 15:03:30 +00:00
|
|
|
* Returns -1 on error.
|
2012-04-13 12:47:30 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
sys_sockaddr_parse(struct sockaddr_storage *addr, socklen_t *addrlen,
|
|
|
|
char *naddr, char *nport, int af, int flags)
|
|
|
|
{
|
|
|
|
struct evutil_addrinfo hints;
|
|
|
|
struct evutil_addrinfo *ai;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = af;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
hints.ai_flags = EVUTIL_AI_ADDRCONFIG | flags;
|
|
|
|
rv = evutil_getaddrinfo(naddr, nport, &hints, &ai);
|
|
|
|
if (rv != 0) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Cannot resolve address '%s' port '%s': %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
naddr, nport, gai_strerror(rv));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(addr, ai->ai_addr, ai->ai_addrlen);
|
|
|
|
*addrlen = ai->ai_addrlen;
|
|
|
|
af = ai->ai_family;
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
return af;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-03-15 16:10:25 +00:00
|
|
|
* Converts an IPv4/IPv6 sockaddr into printable string representations of the
|
|
|
|
* host and the service (port) part. Writes allocated buffers to *host and
|
|
|
|
* *serv which must both be freed by the caller. Neither *host nor *port are
|
|
|
|
* freed by this function before newly allocating.
|
|
|
|
* Returns 0 on success, -1 otherwise. When -1 is returned, pointers in *host
|
|
|
|
* and *serv are invalid and must not be used nor freed by the caller.
|
2012-04-13 12:47:30 +00:00
|
|
|
*/
|
2015-03-15 16:10:25 +00:00
|
|
|
int
|
|
|
|
sys_sockaddr_str(struct sockaddr *addr, socklen_t addrlen,
|
|
|
|
char **host, char **serv)
|
2012-04-13 12:47:30 +00:00
|
|
|
{
|
2015-03-15 16:10:25 +00:00
|
|
|
char tmphost[INET6_ADDRSTRLEN];
|
2012-04-13 12:47:30 +00:00
|
|
|
int rv;
|
2015-03-15 16:10:25 +00:00
|
|
|
size_t hostsz;
|
2012-04-13 12:47:30 +00:00
|
|
|
|
2015-03-15 16:10:25 +00:00
|
|
|
*serv = malloc(6); /* max decimal digits of short plus terminator */
|
|
|
|
if (!*serv) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Cannot allocate memory\n");
|
2015-03-15 16:10:25 +00:00
|
|
|
return -1;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
2015-03-15 16:10:25 +00:00
|
|
|
rv = getnameinfo(addr, addrlen,
|
|
|
|
tmphost, sizeof(tmphost),
|
|
|
|
*serv, 6,
|
2012-04-13 12:47:30 +00:00
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
if (rv != 0) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Cannot get nameinfo for socket address: %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
gai_strerror(rv));
|
2015-03-15 16:10:25 +00:00
|
|
|
free(*serv);
|
|
|
|
return -1;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
2015-03-15 16:10:25 +00:00
|
|
|
hostsz = strlen(tmphost) + 1; /* including terminator */
|
|
|
|
*host = malloc(hostsz);
|
|
|
|
if (!*host) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Cannot allocate memory\n");
|
2015-03-15 16:10:25 +00:00
|
|
|
free(*serv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(*host, tmphost, hostsz);
|
|
|
|
return 0;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-15 16:52:04 +00:00
|
|
|
/*
|
|
|
|
* Sanitizes a valid IPv4 or IPv6 address for use in a filename, i.e. removes
|
|
|
|
* characters that are invalid on NTFS and replaces them with more innocent
|
|
|
|
* characters. The function assumes that the input is a valid IPv4 or IPv6
|
|
|
|
* address; it is not a generic filename sanitizer.
|
|
|
|
*
|
|
|
|
* Returns a copy of string s that must be freed by the caller.
|
|
|
|
*
|
|
|
|
* Invalid NTFS characters are < > : " / \ | ? * according to
|
|
|
|
* https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247.aspx
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
sys_ip46str_sanitize(const char *s)
|
|
|
|
{
|
|
|
|
char *copy, *p;
|
|
|
|
|
|
|
|
copy = strdup(s);
|
|
|
|
if (!copy)
|
|
|
|
return NULL;
|
|
|
|
p = copy;
|
|
|
|
while (*p) {
|
2015-03-15 19:08:11 +00:00
|
|
|
switch (*p) {
|
|
|
|
case ':':
|
|
|
|
case '%':
|
2015-03-15 16:52:04 +00:00
|
|
|
*p = '_';
|
2015-03-15 19:08:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-15 16:52:04 +00:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2018-11-03 15:23:31 +00:00
|
|
|
/*
|
|
|
|
* Returns the MTU of the interface with name *ifname* or 0 on errors.
|
|
|
|
*/
|
|
|
|
size_t
|
|
|
|
sys_get_mtu(const char *ifname)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
size_t ifnamelen;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
ifnamelen = strlen(ifname);
|
|
|
|
if (ifnamelen > sizeof(ifr.ifr_name) + 1)
|
|
|
|
return 0;
|
|
|
|
memcpy(ifr.ifr_name, ifname, ifnamelen);
|
|
|
|
ifr.ifr_name[ifnamelen] = '\0';
|
|
|
|
|
|
|
|
s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
|
|
|
|
if (s == -1)
|
|
|
|
return 0;
|
|
|
|
if (ioctl(s, SIOCGIFMTU, &ifr) == -1) {
|
|
|
|
close(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
return ifr.ifr_mtu;
|
|
|
|
}
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
/*
|
|
|
|
* Returns 1 if path points to an existing directory node in the filesystem.
|
|
|
|
* Returns 0 if path is NULL, does not exist, or points to a file of some kind.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sys_isdir(const char *path)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
|
2014-11-23 21:52:09 +00:00
|
|
|
if (stat(path, &s) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Error stating file: %s (%i)\n",
|
2014-11-23 21:52:09 +00:00
|
|
|
strerror(errno), errno);
|
|
|
|
}
|
2012-04-13 12:47:30 +00:00
|
|
|
return 0;
|
2014-11-23 21:52:09 +00:00
|
|
|
}
|
2012-04-13 12:47:30 +00:00
|
|
|
if (s.st_mode & S_IFDIR)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-16 20:53:47 +00:00
|
|
|
/*
|
|
|
|
* Create directory including parent directories with mode_t.
|
|
|
|
* Mode of existing parent directories is not changed.
|
|
|
|
* Returns 0 on success, -1 and sets errno on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sys_mkpath(const char *path, mode_t mode)
|
|
|
|
{
|
|
|
|
char parent[strlen(path)+1];
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
memcpy(parent, path, sizeof(parent));
|
|
|
|
|
|
|
|
p = parent;
|
|
|
|
do {
|
|
|
|
/* skip leading '/' characters */
|
|
|
|
while (*p == '/') p++;
|
|
|
|
p = strchr(p, '/');
|
|
|
|
if (p) {
|
|
|
|
/* overwrite '/' to terminate the string at the next
|
|
|
|
* parent directory */
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat sbuf;
|
|
|
|
if (stat(parent, &sbuf) == -1) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
if (mkdir(parent, mode) != 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (!S_ISDIR(sbuf.st_mode)) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
/* replace the overwritten slash */
|
|
|
|
*p = '/';
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
} while (p);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-03 15:23:31 +00:00
|
|
|
/*
|
|
|
|
* Return realpath(dirname(path)) + / + basename(path) in a newly allocated
|
|
|
|
* string. Returns NULL on failure and sets errno to ENOENT if the directory
|
|
|
|
* part does not exist.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
sys_realdir(const char *path)
|
|
|
|
{
|
|
|
|
char *sep, *udir, *rdir, *p;
|
|
|
|
int rerrno, rv;
|
|
|
|
|
|
|
|
if (path[0] == '\0') {
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
udir = strdup(path);
|
|
|
|
if (!udir)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sep = strrchr(udir, '/');
|
|
|
|
if (!sep) {
|
|
|
|
free(udir);
|
|
|
|
rv = asprintf(&udir, "./%s", path);
|
|
|
|
if (rv == -1)
|
|
|
|
return NULL;
|
|
|
|
sep = udir + 1;
|
|
|
|
} else if (sep == udir) {
|
|
|
|
return udir;
|
|
|
|
}
|
|
|
|
*sep = '\0';
|
|
|
|
rdir = realpath(udir, NULL);
|
|
|
|
if (!rdir) {
|
|
|
|
rerrno = errno;
|
|
|
|
free(udir);
|
|
|
|
errno = rerrno;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
rv = asprintf(&p, "%s/%s", rdir, sep + 1);
|
|
|
|
rerrno = errno;
|
|
|
|
free(rdir);
|
|
|
|
free(udir);
|
|
|
|
errno = rerrno;
|
|
|
|
if (rv == -1)
|
|
|
|
return NULL;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
/*
|
|
|
|
* Iterate over all files in a directory hierarchy, calling the callback
|
|
|
|
* cb for each file, passing the filename and arg as arguments. Files and
|
|
|
|
* directories beginning with a dot are skipped, symlinks are followed.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sys_dir_eachfile(const char *dirname, sys_dir_eachfile_cb_t cb, void *arg)
|
|
|
|
{
|
|
|
|
FTS *tree;
|
|
|
|
FTSENT *node;
|
|
|
|
char * paths[2];
|
2017-07-25 13:07:39 +00:00
|
|
|
int rv = 0;
|
2012-04-13 12:47:30 +00:00
|
|
|
|
|
|
|
paths[1] = NULL;
|
|
|
|
paths[0] = strdup(dirname);
|
|
|
|
if (!paths[0])
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, NULL);
|
|
|
|
if (!tree) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Cannot open directory '%s': %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
dirname, strerror(errno));
|
2017-07-25 13:07:39 +00:00
|
|
|
rv = -1;
|
|
|
|
goto out1;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while ((node = fts_read(tree))) {
|
|
|
|
if (node->fts_level > 0 && node->fts_name[0] == '.')
|
|
|
|
fts_set(tree, node, FTS_SKIP);
|
|
|
|
else if (node->fts_info & FTS_F) {
|
2017-07-25 13:07:39 +00:00
|
|
|
rv = cb(node->fts_path, arg);
|
|
|
|
if (rv == -1)
|
|
|
|
goto out2;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (errno) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Error reading directory entry: %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
strerror(errno));
|
2017-07-25 13:07:39 +00:00
|
|
|
rv = -1;
|
|
|
|
goto out2;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
2017-07-25 13:07:39 +00:00
|
|
|
|
|
|
|
out2:
|
2012-04-13 12:47:30 +00:00
|
|
|
fts_close(tree);
|
|
|
|
|
2017-07-25 13:07:39 +00:00
|
|
|
out1:
|
2012-04-13 12:47:30 +00:00
|
|
|
free(paths[0]);
|
2017-07-25 13:07:39 +00:00
|
|
|
return rv;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Portably get the number of CPU cores online in the system.
|
|
|
|
*/
|
|
|
|
uint32_t
|
|
|
|
sys_get_cpu_cores(void)
|
|
|
|
{
|
|
|
|
#ifdef _SC_NPROCESSORS_ONLN
|
|
|
|
return sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
#else /* !_SC_NPROCESSORS_ONLN */
|
|
|
|
int mib[2];
|
|
|
|
uint32_t n;
|
|
|
|
size_t len = sizeof(n);
|
|
|
|
|
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_AVAILCPU;
|
|
|
|
sysctl(mib, sizeof(mib)/sizeof(int), &n, &len, NULL, 0);
|
|
|
|
|
|
|
|
if (n < 1) {
|
|
|
|
mib[1] = HW_NCPU;
|
|
|
|
sysctl(mib, sizeof(mib)/sizeof(int), &n, &len, NULL, 0);
|
|
|
|
if (n < 1) {
|
|
|
|
n = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
#endif /* !_SC_NPROCESSORS_ONLN */
|
|
|
|
}
|
|
|
|
|
2014-11-21 22:39:51 +00:00
|
|
|
/*
|
|
|
|
* Send a message and optional file descriptor on a connected AF_UNIX
|
|
|
|
* SOCKET_DGRAM socket s. Returns the return value of sendmsg().
|
|
|
|
* If fd is -1, no file descriptor is passed.
|
|
|
|
*/
|
|
|
|
ssize_t
|
|
|
|
sys_sendmsgfd(int sock, void *buf, size_t bufsz, int fd)
|
|
|
|
{
|
|
|
|
struct iovec iov;
|
|
|
|
struct msghdr msg;
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
char cmsgbuf[CMSG_SPACE(sizeof(int))];
|
2014-11-23 14:49:03 +00:00
|
|
|
ssize_t n;
|
2014-11-21 22:39:51 +00:00
|
|
|
|
|
|
|
iov.iov_base = buf;
|
|
|
|
iov.iov_len = bufsz;
|
|
|
|
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
|
|
|
if (fd != -1) {
|
|
|
|
msg.msg_control = cmsgbuf;
|
|
|
|
msg.msg_controllen = sizeof(cmsgbuf);
|
|
|
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
2014-11-24 21:01:52 +00:00
|
|
|
if (!cmsg)
|
|
|
|
return -1;
|
2014-11-21 22:39:51 +00:00
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
|
|
|
|
*((int *) CMSG_DATA(cmsg)) = fd;
|
|
|
|
} else {
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
}
|
2014-11-23 14:49:03 +00:00
|
|
|
do {
|
2014-11-21 22:39:51 +00:00
|
|
|
#ifdef MSG_NOSIGNAL
|
2014-11-23 14:49:03 +00:00
|
|
|
n = sendmsg(sock, &msg, MSG_NOSIGNAL);
|
2014-11-21 22:39:51 +00:00
|
|
|
#else /* !MSG_NOSIGNAL */
|
2014-11-23 14:49:03 +00:00
|
|
|
n = sendmsg(sock, &msg, 0);
|
2014-11-21 22:39:51 +00:00
|
|
|
#endif /* !MSG_NOSIGNAL */
|
2014-11-23 14:49:03 +00:00
|
|
|
} while (n == -1 && errno == EINTR);
|
|
|
|
return n;
|
2014-11-21 22:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a message and optional file descriptor on a connected AF_UNIX
|
|
|
|
* SOCKET_DGRAM socket s. Returns the return value of recvmsg()/recv()
|
|
|
|
* and sets errno to EINVAL if the received message is malformed.
|
|
|
|
* If pfd is NULL, no file descriptor is received; if a file descriptor was
|
|
|
|
* part of the received message and pfd is NULL, then the kernel will close it.
|
|
|
|
*/
|
|
|
|
ssize_t
|
|
|
|
sys_recvmsgfd(int sock, void *buf, size_t bufsz, int *pfd)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
if (pfd) {
|
|
|
|
struct iovec iov;
|
|
|
|
struct msghdr msg;
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
unsigned char cmsgbuf[CMSG_SPACE(sizeof(int))];
|
|
|
|
|
|
|
|
iov.iov_base = buf;
|
|
|
|
iov.iov_len = bufsz;
|
|
|
|
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = cmsgbuf;
|
|
|
|
msg.msg_controllen = sizeof(cmsgbuf);
|
2014-11-23 14:49:03 +00:00
|
|
|
do {
|
|
|
|
n = recvmsg(sock, &msg, 0);
|
|
|
|
} while (n == -1 && errno == EINTR);
|
|
|
|
if (n <= 0)
|
2014-11-21 22:39:51 +00:00
|
|
|
return n;
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
|
|
|
if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
|
|
|
|
if (cmsg->cmsg_level != SOL_SOCKET) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (cmsg->cmsg_type != SCM_RIGHTS) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*pfd = *((int *) CMSG_DATA(cmsg));
|
|
|
|
} else {
|
|
|
|
*pfd = -1;
|
|
|
|
}
|
|
|
|
} else {
|
2014-11-23 14:49:03 +00:00
|
|
|
do {
|
|
|
|
n = recv(sock, buf, bufsz, 0);
|
|
|
|
} while (n == -1 && errno == EINTR);
|
2014-11-21 22:39:51 +00:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-11-24 21:01:52 +00:00
|
|
|
/*
|
|
|
|
* Format AF_UNIX socket address into printable string.
|
|
|
|
* Returns newly allocated string that must be freed by caller.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
sys_afunix_str(struct sockaddr *addr, socklen_t addrlen)
|
|
|
|
{
|
|
|
|
struct sockaddr_un *sun = (struct sockaddr_un *)addr;
|
|
|
|
char *name;
|
2018-01-18 00:18:53 +00:00
|
|
|
int rv;
|
2014-11-24 21:01:52 +00:00
|
|
|
|
|
|
|
if (addrlen == sizeof(sa_family_t)) {
|
2018-01-18 00:18:53 +00:00
|
|
|
rv = asprintf(&name, "unnmd");
|
2014-11-24 21:01:52 +00:00
|
|
|
} else if (sun->sun_path[0] == '\0') {
|
|
|
|
/* abstract sockets is a Linux feature */
|
2018-01-18 00:18:53 +00:00
|
|
|
rv = asprintf(&name, "abstr:%02x:%02x:%02x:%02x",
|
|
|
|
sun->sun_path[1],
|
|
|
|
sun->sun_path[2],
|
|
|
|
sun->sun_path[3],
|
|
|
|
sun->sun_path[4]);
|
2014-11-24 21:01:52 +00:00
|
|
|
} else {
|
2018-01-18 00:18:53 +00:00
|
|
|
rv = asprintf(&name, "pname:%s", sun->sun_path);
|
2014-11-24 21:01:52 +00:00
|
|
|
}
|
2018-01-18 00:18:53 +00:00
|
|
|
if (rv == -1)
|
|
|
|
name = NULL;
|
2014-11-24 21:01:52 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump all open file descriptors to stdout - poor man's lsof/fstat/sockstat
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sys_dump_fds(void)
|
|
|
|
{
|
|
|
|
int maxfd = 0;
|
|
|
|
|
|
|
|
#ifdef F_MAXFD
|
|
|
|
if (!maxfd && ((maxfd = fcntl(0, F_MAXFD)) == -1)) {
|
|
|
|
fprintf(stderr, "fcntl(0, F_MAXFD) failed: %s (%i)\n",
|
|
|
|
strerror(errno), errno);
|
|
|
|
}
|
|
|
|
#endif /* F_MAXFD */
|
|
|
|
#ifdef _SC_OPEN_MAX
|
|
|
|
if (!maxfd && ((maxfd = sysconf(_SC_OPEN_MAX)) == -1)) {
|
|
|
|
fprintf(stderr, "sysconf(_SC_OPEN_MAX) failed: %s (%i)\n",
|
|
|
|
strerror(errno), errno);
|
|
|
|
}
|
|
|
|
#endif /* _SC_OPEN_MAX */
|
|
|
|
if (!maxfd)
|
|
|
|
maxfd = 65535;
|
|
|
|
|
|
|
|
for (int fd = 0; fd <= maxfd; fd++) {
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstat(fd, &st) == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%5d:", fd);
|
|
|
|
switch (st.st_mode & S_IFMT) {
|
|
|
|
case S_IFBLK: printf(" blkdev"); break;
|
|
|
|
case S_IFCHR: printf(" chrdev"); break;
|
|
|
|
case S_IFDIR: printf(" dir "); break;
|
|
|
|
case S_IFIFO: printf(" fifo "); break;
|
|
|
|
case S_IFLNK: printf(" lnkfil"); break;
|
|
|
|
case S_IFREG: printf(" regfil"); break;
|
|
|
|
case S_IFSOCK: printf(" socket"); break;
|
|
|
|
default: printf(" unknwn"); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((st.st_mode & S_IFMT) == S_IFSOCK) {
|
2018-01-18 00:18:53 +00:00
|
|
|
int lrv, frv, arv;
|
2014-11-24 21:01:52 +00:00
|
|
|
struct sockaddr_storage lss, fss;
|
|
|
|
socklen_t lsslen = sizeof(lss);
|
|
|
|
socklen_t fsslen = sizeof(fss);
|
|
|
|
char *laddrstr, *faddrstr;
|
|
|
|
|
|
|
|
lrv = getsockname(fd, (struct sockaddr *)&lss, &lsslen);
|
|
|
|
frv = getpeername(fd, (struct sockaddr *)&fss, &fsslen);
|
|
|
|
|
|
|
|
switch (lss.ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
case AF_INET6: {
|
|
|
|
if (lrv == 0) {
|
2015-03-15 16:10:25 +00:00
|
|
|
char *host, *port;
|
|
|
|
if (sys_sockaddr_str(
|
|
|
|
(struct sockaddr *)&lss,
|
|
|
|
lsslen,
|
|
|
|
&host, &port) != 0) {
|
|
|
|
laddrstr = strdup("?");
|
|
|
|
} else {
|
2018-01-18 00:18:53 +00:00
|
|
|
arv = asprintf(&laddrstr,
|
|
|
|
"[%s]:%s",
|
|
|
|
host, port);
|
|
|
|
if (arv == -1)
|
|
|
|
laddrstr = NULL;
|
2015-03-15 16:10:25 +00:00
|
|
|
free(host);
|
|
|
|
free(port);
|
|
|
|
}
|
2014-11-24 21:01:52 +00:00
|
|
|
} else {
|
|
|
|
laddrstr = strdup("n/a");
|
|
|
|
}
|
|
|
|
if (frv == 0) {
|
2015-03-15 16:10:25 +00:00
|
|
|
char *host, *port;
|
|
|
|
if (sys_sockaddr_str(
|
|
|
|
(struct sockaddr *)&fss,
|
|
|
|
fsslen,
|
|
|
|
&host, &port) != 0) {
|
|
|
|
faddrstr = strdup("?");
|
|
|
|
} else {
|
2018-01-18 00:18:53 +00:00
|
|
|
arv = asprintf(&faddrstr,
|
|
|
|
"[%s]:%s",
|
|
|
|
host, port);
|
|
|
|
if (arv == -1)
|
|
|
|
faddrstr = NULL;
|
2015-03-15 16:10:25 +00:00
|
|
|
free(host);
|
|
|
|
free(port);
|
|
|
|
}
|
2014-11-24 21:01:52 +00:00
|
|
|
} else {
|
|
|
|
faddrstr = strdup("n/a");
|
|
|
|
}
|
|
|
|
printf(" %-6s %s -> %s",
|
|
|
|
lss.ss_family == AF_INET ? "in" : "in6",
|
|
|
|
laddrstr, faddrstr);
|
|
|
|
free(laddrstr);
|
|
|
|
free(faddrstr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AF_UNIX: {
|
|
|
|
if (lrv == 0) {
|
|
|
|
laddrstr = sys_afunix_str((struct sockaddr *)&lss, lsslen);
|
|
|
|
} else {
|
|
|
|
laddrstr = strdup("n/a");
|
|
|
|
}
|
|
|
|
if (frv == 0) {
|
|
|
|
faddrstr = sys_afunix_str((struct sockaddr *)&fss, fsslen);
|
|
|
|
} else {
|
|
|
|
faddrstr = strdup("n/a");
|
|
|
|
}
|
|
|
|
printf(" unix %s -> %s", laddrstr, faddrstr);
|
|
|
|
free(laddrstr);
|
|
|
|
free(faddrstr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AF_UNSPEC: {
|
|
|
|
printf(" unspec");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
printf(" (%i)", lss.ss_family);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 15:23:31 +00:00
|
|
|
static int sys_rand_seeded = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
sys_rand_seed(void) {
|
|
|
|
struct timeval seed;
|
|
|
|
|
|
|
|
if (gettimeofday(&seed, NULL) == -1) {
|
|
|
|
srandom((unsigned)time(NULL));
|
|
|
|
} else {
|
|
|
|
srandom((unsigned)(seed.tv_sec ^ seed.tv_usec));
|
|
|
|
}
|
|
|
|
sys_rand_seeded = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
sys_rand16(void) {
|
|
|
|
if (unlikely(!sys_rand_seeded))
|
|
|
|
sys_rand_seed();
|
|
|
|
return random();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
sys_rand32(void) {
|
|
|
|
if (unlikely(!sys_rand_seeded))
|
|
|
|
sys_rand_seed();
|
|
|
|
return random();
|
|
|
|
}
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
/* vim: set noet ft=c: */
|
2014-11-21 22:39:51 +00:00
|
|
|
|