2014-03-10 10:22:56 +05:30
|
|
|
/* ifchd.c - interface change daemon
|
|
|
|
*
|
2020-10-20 13:53:55 +05:30
|
|
|
* Copyright 2004-2020 Nicholas J. Kain <njkain at gmail dot com>
|
2014-03-10 10:22:56 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2020-10-20 14:39:26 +05:30
|
|
|
#include <poll.h>
|
2014-03-11 00:14:12 +05:30
|
|
|
#include <sys/prctl.h>
|
2014-03-10 10:22:56 +05:30
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
2014-03-31 02:32:48 +05:30
|
|
|
#include "nk/log.h"
|
2020-10-20 16:14:31 +05:30
|
|
|
#include "nk/privs.h"
|
2014-03-31 02:32:48 +05:30
|
|
|
#include "nk/io.h"
|
2014-03-10 10:22:56 +05:30
|
|
|
|
|
|
|
#include "ifchd.h"
|
2014-03-13 01:54:02 +05:30
|
|
|
#include "ndhc.h"
|
2014-03-11 04:59:19 +05:30
|
|
|
#include "ifchd-parse.h"
|
2014-03-13 02:21:10 +05:30
|
|
|
#include "sys.h"
|
2014-03-10 10:22:56 +05:30
|
|
|
#include "ifset.h"
|
|
|
|
|
|
|
|
struct ifchd_client cl;
|
|
|
|
|
2014-03-13 01:44:48 +05:30
|
|
|
static int resolv_conf_fd = -1;
|
2014-03-10 10:22:56 +05:30
|
|
|
/* int ntp_conf_fd = -1; */
|
2016-05-06 16:11:15 +05:30
|
|
|
static int resolv_conf_head_fd = -1;
|
|
|
|
static int resolv_conf_tail_fd = -1;
|
2014-03-10 10:22:56 +05:30
|
|
|
|
|
|
|
/* If true, allow HOSTNAME changes from dhcp server. */
|
|
|
|
int allow_hostname = 0;
|
|
|
|
|
|
|
|
uid_t ifch_uid = 0;
|
|
|
|
gid_t ifch_gid = 0;
|
|
|
|
|
2022-01-12 09:05:19 +05:30
|
|
|
static void writeordie(int fd, const char *buf, size_t len)
|
2014-03-10 10:22:56 +05:30
|
|
|
{
|
2014-04-06 16:01:40 +05:30
|
|
|
ssize_t r = safe_write(fd, buf, len);
|
2014-04-04 13:31:49 +05:30
|
|
|
if (r < 0 || (size_t)r != len)
|
2020-11-25 07:32:51 +05:30
|
|
|
suicide("%s: (%s) write failed: %zd", client_config.interface,
|
2014-04-04 13:31:49 +05:30
|
|
|
__func__, r);
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
|
2022-01-12 09:05:19 +05:30
|
|
|
static int write_append_fd(int to_fd, int from_fd, const char *descr)
|
2016-05-06 16:11:15 +05:30
|
|
|
{
|
|
|
|
if (from_fd < 0) return 0;
|
|
|
|
if (to_fd < 0) return -1;
|
|
|
|
|
|
|
|
const off_t lse = lseek(from_fd, 0, SEEK_END);
|
|
|
|
if (lse < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) lseek(SEEK_END) failed %s",
|
|
|
|
client_config.interface, __func__, descr);
|
2016-05-06 16:11:15 +05:30
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if (lseek(from_fd, 0, SEEK_SET) < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) lseek(SEEK_SET) failed %s",
|
|
|
|
client_config.interface, __func__, descr);
|
2016-05-06 16:11:15 +05:30
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[4096];
|
2018-02-09 13:09:46 +05:30
|
|
|
size_t from_fd_len = (size_t)lse;
|
2016-05-06 16:11:15 +05:30
|
|
|
while (from_fd_len > 0) {
|
|
|
|
const size_t to_read = from_fd_len <= sizeof buf ? from_fd_len : sizeof buf;
|
|
|
|
ssize_t r = safe_read(from_fd, buf, to_read);
|
|
|
|
if (r < 0 || (size_t)r != to_read)
|
|
|
|
suicide("%s: (%s) read failed %s", client_config.interface, __func__, descr);
|
|
|
|
r = safe_write(to_fd, buf, to_read);
|
|
|
|
if (r < 0 || (size_t)r != to_read)
|
|
|
|
suicide("%s: (%s) write failed %s", client_config.interface, __func__, descr);
|
|
|
|
from_fd_len -= to_read;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-10 10:22:56 +05:30
|
|
|
/* Writes a new resolv.conf based on the information we have received. */
|
2015-02-15 12:59:37 +05:30
|
|
|
static int write_resolve_conf(void)
|
2014-03-10 10:22:56 +05:30
|
|
|
{
|
2014-03-11 05:10:38 +05:30
|
|
|
static const char ns_str[] = "nameserver ";
|
|
|
|
static const char dom_str[] = "domain ";
|
|
|
|
static const char srch_str[] = "search ";
|
2014-03-10 10:22:56 +05:30
|
|
|
off_t off;
|
|
|
|
char buf[MAX_BUF];
|
|
|
|
|
2014-03-13 06:28:27 +05:30
|
|
|
if (resolv_conf_fd < 0)
|
2015-02-15 12:59:37 +05:30
|
|
|
return 0;
|
2014-03-10 10:22:56 +05:30
|
|
|
if (strlen(cl.namesvrs) == 0)
|
2015-02-15 12:59:37 +05:30
|
|
|
return -1;
|
2014-03-10 10:22:56 +05:30
|
|
|
|
2014-04-06 16:03:14 +05:30
|
|
|
if (lseek(resolv_conf_fd, 0, SEEK_SET) < 0)
|
2015-02-15 12:59:37 +05:30
|
|
|
return -1;
|
2014-03-10 10:22:56 +05:30
|
|
|
|
2016-05-06 16:11:15 +05:30
|
|
|
write_append_fd(resolv_conf_fd, resolv_conf_head_fd, "prepending resolv_conf head");
|
|
|
|
|
2014-03-10 10:22:56 +05:30
|
|
|
char *p = cl.namesvrs;
|
|
|
|
while (p && (*p != '\0')) {
|
|
|
|
char *q = strchr(p, ',');
|
|
|
|
if (!q)
|
|
|
|
q = strchr(p, '\0');
|
|
|
|
else
|
|
|
|
*q++ = '\0';
|
2014-03-20 05:02:45 +05:30
|
|
|
ssize_t sl = snprintf(buf, sizeof buf, "%s", p);
|
|
|
|
if (sl < 0 || (size_t)sl >= sizeof buf) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) snprintf failed appending nameservers",
|
|
|
|
client_config.interface, __func__);
|
2014-03-20 05:02:45 +05:30
|
|
|
}
|
2014-03-10 10:22:56 +05:30
|
|
|
|
|
|
|
writeordie(resolv_conf_fd, ns_str, strlen(ns_str));
|
|
|
|
writeordie(resolv_conf_fd, buf, strlen(buf));
|
|
|
|
writeordie(resolv_conf_fd, "\n", 1);
|
|
|
|
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = cl.domains;
|
|
|
|
int numdoms = 0;
|
|
|
|
while (p && (*p != '\0')) {
|
|
|
|
char *q = strchr(p, ',');
|
|
|
|
if (!q)
|
|
|
|
q = strchr(p, '\0');
|
|
|
|
else
|
|
|
|
*q++ = '\0';
|
2014-03-20 05:02:45 +05:30
|
|
|
ssize_t sl = snprintf(buf, sizeof buf, "%s", p);
|
|
|
|
if (sl < 0 || (size_t)sl >= sizeof buf) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) snprintf failed appending domains",
|
|
|
|
client_config.interface, __func__);
|
2014-03-20 05:02:45 +05:30
|
|
|
}
|
2014-03-10 10:22:56 +05:30
|
|
|
|
|
|
|
if (numdoms == 0) {
|
|
|
|
writeordie(resolv_conf_fd, dom_str, strlen(dom_str));
|
|
|
|
writeordie(resolv_conf_fd, buf, strlen(buf));
|
|
|
|
} else {
|
|
|
|
if (numdoms == 1) {
|
|
|
|
writeordie(resolv_conf_fd, "\n", 1);
|
|
|
|
writeordie(resolv_conf_fd, srch_str, strlen(srch_str));
|
|
|
|
writeordie(resolv_conf_fd, buf, strlen(buf));
|
|
|
|
} else {
|
|
|
|
writeordie(resolv_conf_fd, " ", 1);
|
|
|
|
writeordie(resolv_conf_fd, buf, strlen(buf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++numdoms;
|
|
|
|
p = q;
|
|
|
|
if (numdoms > 6)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
writeordie(resolv_conf_fd, "\n", 1);
|
|
|
|
|
2016-05-06 16:11:15 +05:30
|
|
|
write_append_fd(resolv_conf_fd, resolv_conf_tail_fd, "appending resolv_conf tail");
|
|
|
|
|
2014-03-10 10:22:56 +05:30
|
|
|
off = lseek(resolv_conf_fd, 0, SEEK_CUR);
|
2014-04-06 16:03:14 +05:30
|
|
|
if (off < 0) {
|
2020-11-07 05:17:51 +05:30
|
|
|
log_line("%s: (%s) lseek returned error: %s", client_config.interface,
|
|
|
|
__func__, strerror(errno));
|
2015-02-15 12:59:37 +05:30
|
|
|
return -1;
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
2020-11-07 05:17:51 +05:30
|
|
|
if (safe_ftruncate(resolv_conf_fd, off) < 0) {
|
|
|
|
log_line("%s: (%s) ftruncate returned error: %s", client_config.interface,
|
|
|
|
__func__, strerror(errno));
|
2015-02-15 12:59:37 +05:30
|
|
|
return -1;
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
2015-02-15 12:59:37 +05:30
|
|
|
if (fsync(resolv_conf_fd) < 0) {
|
2020-11-07 05:17:51 +05:30
|
|
|
log_line("%s: (%s) fsync returned error: %s", client_config.interface,
|
|
|
|
__func__, strerror(errno));
|
2015-02-15 12:59:37 +05:30
|
|
|
return -1;
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
2015-02-15 12:59:37 +05:30
|
|
|
return 0;
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: addme */
|
2022-01-12 09:05:19 +05:30
|
|
|
int perform_timezone(const char *str, size_t len)
|
2014-03-10 10:22:56 +05:30
|
|
|
{
|
2014-03-11 05:10:38 +05:30
|
|
|
(void)len;
|
2014-03-10 10:22:56 +05:30
|
|
|
log_line("Timezone setting NYI: '%s'", str);
|
2015-02-15 12:59:37 +05:30
|
|
|
return 0;
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a dns server to the /etc/resolv.conf -- we already have a fd. */
|
2022-01-12 09:05:19 +05:30
|
|
|
int perform_dns(const char *str, size_t len)
|
2014-03-10 10:22:56 +05:30
|
|
|
{
|
2015-02-14 09:44:08 +05:30
|
|
|
if (resolv_conf_fd < 0)
|
2015-02-15 12:59:37 +05:30
|
|
|
return 0;
|
|
|
|
int ret = -1;
|
2014-03-11 05:10:38 +05:30
|
|
|
if (len > sizeof cl.namesvrs) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("DNS server list is too long: %zu > %zu", len, sizeof cl.namesvrs);
|
2015-02-15 12:59:37 +05:30
|
|
|
return ret;
|
2014-03-11 05:10:38 +05:30
|
|
|
}
|
2014-03-20 05:02:45 +05:30
|
|
|
ssize_t sl = snprintf(cl.namesvrs, sizeof cl.namesvrs, "%s", str);
|
|
|
|
if (sl < 0 || (size_t)sl >= sizeof cl.namesvrs) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) snprintf failed",
|
|
|
|
client_config.interface, __func__);
|
2014-03-20 05:02:45 +05:30
|
|
|
}
|
2015-02-15 12:59:37 +05:30
|
|
|
ret = write_resolve_conf();
|
|
|
|
if (ret >= 0)
|
|
|
|
log_line("Added DNS server: '%s'", str);
|
|
|
|
return ret;
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Updates for print daemons are too non-standard to be useful. */
|
2022-01-12 09:05:19 +05:30
|
|
|
int perform_lprsvr(const char *str, size_t len)
|
2014-03-10 10:22:56 +05:30
|
|
|
{
|
2014-03-11 05:10:38 +05:30
|
|
|
(void)len;
|
2014-03-10 10:22:56 +05:30
|
|
|
log_line("Line printer server setting NYI: '%s'", str);
|
2015-02-15 12:59:37 +05:30
|
|
|
return 0;
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets machine hostname. */
|
2022-01-12 09:05:19 +05:30
|
|
|
int perform_hostname(const char *str, size_t len)
|
2014-03-10 10:22:56 +05:30
|
|
|
{
|
2015-02-14 09:44:08 +05:30
|
|
|
if (!allow_hostname)
|
2015-02-15 12:59:37 +05:30
|
|
|
return 0;
|
|
|
|
if (sethostname(str, len) < 0) {
|
2014-03-10 10:22:56 +05:30
|
|
|
log_line("sethostname returned %s", strerror(errno));
|
2015-02-15 12:59:37 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
log_line("Set hostname: '%s'", str);
|
|
|
|
return 0;
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* update "domain" and "search" in /etc/resolv.conf */
|
2022-01-12 09:05:19 +05:30
|
|
|
int perform_domain(const char *str, size_t len)
|
2014-03-10 10:22:56 +05:30
|
|
|
{
|
2015-02-14 09:44:08 +05:30
|
|
|
if (resolv_conf_fd < 0)
|
2015-02-15 12:59:37 +05:30
|
|
|
return 0;
|
|
|
|
int ret = -1;
|
2014-03-11 05:10:38 +05:30
|
|
|
if (len > sizeof cl.domains) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("DNS domain list is too long: %zu > %zu", len, sizeof cl.namesvrs);
|
2015-02-15 12:59:37 +05:30
|
|
|
return ret;
|
2014-03-11 05:10:38 +05:30
|
|
|
}
|
2014-03-20 05:02:45 +05:30
|
|
|
ssize_t sl = snprintf(cl.domains, sizeof cl.domains, "%s", str);
|
|
|
|
if (sl < 0 || (size_t)sl >= sizeof cl.domains) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) snprintf failed",
|
|
|
|
client_config.interface, __func__);
|
2014-03-20 05:02:45 +05:30
|
|
|
}
|
2015-02-15 12:59:37 +05:30
|
|
|
ret = write_resolve_conf();
|
2021-04-25 14:32:56 +05:30
|
|
|
if (ret == 0)
|
2015-02-15 12:59:37 +05:30
|
|
|
log_line("Added DNS domain: '%s'", str);
|
|
|
|
return ret;
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* I don't think this can be done without a netfilter extension
|
|
|
|
* that isn't in the mainline kernels. */
|
2022-01-12 09:05:19 +05:30
|
|
|
int perform_ipttl(const char *str, size_t len)
|
2014-03-10 10:22:56 +05:30
|
|
|
{
|
2014-03-11 05:10:38 +05:30
|
|
|
(void)len;
|
2014-03-10 10:22:56 +05:30
|
|
|
log_line("TTL setting NYI: '%s'", str);
|
2015-02-15 12:59:37 +05:30
|
|
|
return 0;
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: addme */
|
2022-01-12 09:05:19 +05:30
|
|
|
int perform_ntpsrv(const char *str, size_t len)
|
2014-03-10 10:22:56 +05:30
|
|
|
{
|
2014-03-11 05:10:38 +05:30
|
|
|
(void)len;
|
2014-03-10 10:22:56 +05:30
|
|
|
log_line("NTP server setting NYI: '%s'", str);
|
2015-02-15 12:59:37 +05:30
|
|
|
return 0;
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Maybe Samba cares about this feature? I don't know. */
|
2022-01-12 09:05:19 +05:30
|
|
|
int perform_wins(const char *str, size_t len)
|
2014-03-11 05:10:38 +05:30
|
|
|
{
|
|
|
|
(void)str;
|
|
|
|
(void)len;
|
2015-02-15 12:59:37 +05:30
|
|
|
return 0;
|
2014-03-11 05:10:38 +05:30
|
|
|
}
|
2014-03-10 10:22:56 +05:30
|
|
|
|
2014-03-17 15:03:00 +05:30
|
|
|
static void inform_execute(char c)
|
2014-03-17 12:25:47 +05:30
|
|
|
{
|
2014-04-07 13:14:02 +05:30
|
|
|
ssize_t r = safe_write(ifchSock[1], &c, sizeof c);
|
2014-03-17 12:25:47 +05:30
|
|
|
if (r == 0) {
|
|
|
|
// Remote end hung up.
|
|
|
|
exit(EXIT_SUCCESS);
|
2014-04-04 13:31:49 +05:30
|
|
|
} else if (r < 0)
|
2014-04-16 03:31:01 +05:30
|
|
|
suicide("%s: (%s) error writing to ifch -> ndhc socket: %s",
|
2014-03-31 02:51:27 +05:30
|
|
|
client_config.interface, __func__, strerror(errno));
|
2014-03-17 12:25:47 +05:30
|
|
|
}
|
|
|
|
|
2014-04-16 03:31:01 +05:30
|
|
|
static void process_client_socket(void)
|
2014-03-10 10:22:56 +05:30
|
|
|
{
|
|
|
|
char buf[MAX_BUF];
|
|
|
|
|
|
|
|
memset(buf, '\0', sizeof buf);
|
2014-04-07 13:14:02 +05:30
|
|
|
ssize_t r = safe_recv(ifchSock[1], buf, sizeof buf - 1, MSG_DONTWAIT);
|
2014-03-10 10:22:56 +05:30
|
|
|
if (r == 0) {
|
|
|
|
// Remote end hung up.
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else if (r < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
return;
|
2014-04-16 03:31:01 +05:30
|
|
|
suicide("%s: (%s) error reading from ndhc -> ifch socket: %s",
|
2014-03-31 02:51:27 +05:30
|
|
|
client_config.interface, __func__, strerror(errno));
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
|
2015-07-03 10:19:13 +05:30
|
|
|
int ebr = execute_buffer(buf);
|
|
|
|
if (ebr < 0) {
|
|
|
|
if (ebr == -99)
|
2021-04-25 14:32:56 +05:30
|
|
|
suicide("%s: (%s) unrecoverable failure in command sequence: '%s'",
|
2015-07-03 10:19:13 +05:30
|
|
|
client_config.interface, __func__, buf);
|
2021-04-25 14:32:56 +05:30
|
|
|
inform_execute('-');
|
2014-03-17 12:25:47 +05:30
|
|
|
} else
|
2014-03-17 15:03:00 +05:30
|
|
|
inform_execute('+');
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
|
2014-04-04 13:31:49 +05:30
|
|
|
static void do_ifch_work(void)
|
2014-03-10 10:22:56 +05:30
|
|
|
{
|
2014-03-17 15:03:00 +05:30
|
|
|
cl.state = STATE_NOTHING;
|
|
|
|
memset(cl.ibuf, 0, sizeof cl.ibuf);
|
|
|
|
memset(cl.namesvrs, 0, sizeof cl.namesvrs);
|
|
|
|
memset(cl.domains, 0, sizeof cl.domains);
|
2014-03-10 10:22:56 +05:30
|
|
|
|
2020-10-20 14:39:26 +05:30
|
|
|
struct pollfd pfds[2] = {0};
|
|
|
|
pfds[0].fd = ifchSock[1];
|
|
|
|
pfds[0].events = POLLIN|POLLHUP|POLLERR|POLLRDHUP;
|
|
|
|
pfds[1].fd = ifchStream[1];
|
|
|
|
pfds[1].events = POLLHUP|POLLERR|POLLRDHUP;
|
2014-03-10 10:22:56 +05:30
|
|
|
|
|
|
|
for (;;) {
|
2020-10-20 14:39:26 +05:30
|
|
|
if (poll(pfds, 2, -1) < 0) {
|
2020-11-07 05:02:38 +05:30
|
|
|
if (errno != EINTR) suicide("poll failed");
|
2020-10-20 14:39:26 +05:30
|
|
|
}
|
|
|
|
if (pfds[0].revents & POLLIN) {
|
|
|
|
process_client_socket();
|
|
|
|
}
|
|
|
|
if (pfds[0].revents & (POLLHUP|POLLERR|POLLRDHUP)) {
|
|
|
|
suicide("ifchSock closed unexpectedly");
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
2020-10-20 14:39:26 +05:30
|
|
|
if (pfds[1].revents & (POLLHUP|POLLERR|POLLRDHUP)) {
|
|
|
|
exit(EXIT_SUCCESS);
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 16:14:31 +05:30
|
|
|
// If we are requested to update resolv.conf, preopen the fd before we drop
|
|
|
|
// root, making sure that if we create resolv.conf, it will be world-readable.
|
2016-05-06 16:11:15 +05:30
|
|
|
static void setup_resolv_conf(void)
|
2014-03-11 05:10:38 +05:30
|
|
|
{
|
2014-03-10 10:22:56 +05:30
|
|
|
if (strncmp(resolv_conf_d, "", sizeof resolv_conf_d)) {
|
|
|
|
umask(022);
|
2016-05-06 16:11:15 +05:30
|
|
|
resolv_conf_fd = open(resolv_conf_d, O_RDWR|O_CREAT|O_CLOEXEC, 644);
|
2014-03-10 10:22:56 +05:30
|
|
|
umask(077);
|
2014-04-06 16:03:14 +05:30
|
|
|
if (resolv_conf_fd < 0) {
|
2014-03-10 10:22:56 +05:30
|
|
|
suicide("FATAL - unable to open resolv.conf");
|
|
|
|
}
|
2016-05-06 16:11:15 +05:30
|
|
|
char buf[PATH_MAX];
|
|
|
|
|
|
|
|
ssize_t sl = snprintf(buf, sizeof buf, "%s.head", resolv_conf_d);
|
|
|
|
if (sl < 0 || (size_t)sl >= sizeof buf)
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("snprintf failed appending resolv_conf_head; path too long?");
|
2016-05-06 16:11:15 +05:30
|
|
|
else
|
|
|
|
resolv_conf_head_fd = open(buf, O_RDONLY|O_CLOEXEC, 0);
|
|
|
|
|
|
|
|
sl = snprintf(buf, sizeof buf, "%s.tail", resolv_conf_d);
|
|
|
|
if (sl < 0 || (size_t)sl >= sizeof buf)
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("snprintf failed appending resolv_conf_tail; path too long?");
|
2016-05-06 16:11:15 +05:30
|
|
|
else
|
|
|
|
resolv_conf_tail_fd = open(buf, O_RDONLY|O_CLOEXEC, 0);
|
|
|
|
|
|
|
|
memset(buf, '\0', sizeof buf);
|
2014-03-10 10:22:56 +05:30
|
|
|
}
|
|
|
|
memset(resolv_conf_d, '\0', sizeof resolv_conf_d);
|
2016-05-06 16:11:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ifch_main(void)
|
|
|
|
{
|
|
|
|
prctl(PR_SET_NAME, "ndhc: ifch");
|
|
|
|
umask(077);
|
2020-10-20 13:53:55 +05:30
|
|
|
setup_signals_subprocess();
|
2016-05-06 16:11:15 +05:30
|
|
|
setup_resolv_conf();
|
2014-03-10 10:22:56 +05:30
|
|
|
|
2014-03-31 02:32:48 +05:30
|
|
|
nk_set_chroot(chroot_dir);
|
2014-03-10 10:22:56 +05:30
|
|
|
memset(chroot_dir, '\0', sizeof chroot_dir);
|
2014-04-08 00:35:34 +05:30
|
|
|
unsigned char keepcaps[] = { CAP_NET_ADMIN };
|
|
|
|
nk_set_uidgid(ifch_uid, ifch_gid, keepcaps, sizeof keepcaps);
|
2014-03-10 10:22:56 +05:30
|
|
|
do_ifch_work();
|
|
|
|
}
|
|
|
|
|