summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nDPId.c2
-rw-r--r--nDPIsrvd.c17
-rw-r--r--utils.c17
-rw-r--r--utils.h5
4 files changed, 30 insertions, 11 deletions
diff --git a/nDPId.c b/nDPId.c
index ed7363236..d78852e9e 100644
--- a/nDPId.c
+++ b/nDPId.c
@@ -1957,7 +1957,7 @@ static int start_reader_threads(void)
openlog("nDPId", LOG_CONS | (log_to_stderr != 0 ? LOG_PERROR : 0), LOG_DAEMON);
errno = 0;
- if (change_user_group(user, group) != 0)
+ if (change_user_group(user, group, pidfile, NULL, NULL) != 0)
{
if (errno != 0)
{
diff --git a/nDPIsrvd.c b/nDPIsrvd.c
index d2aaeb52b..ca8f2560d 100644
--- a/nDPIsrvd.c
+++ b/nDPIsrvd.c
@@ -344,7 +344,7 @@ int main(int argc, char ** argv)
if (daemonize_with_pidfile(pidfile) != 0)
{
- return 1;
+ goto error;
}
closelog();
openlog("nDPIsrvd", LOG_CONS | (log_to_stderr != 0 ? LOG_PERROR : 0), LOG_DAEMON);
@@ -354,7 +354,7 @@ int main(int argc, char ** argv)
remotes.desc = (struct remote_desc *)malloc(remotes.desc_size * sizeof(*remotes.desc));
if (remotes.desc == NULL)
{
- return 1;
+ goto error;
}
for (size_t i = 0; i < remotes.desc_size; ++i)
{
@@ -365,7 +365,7 @@ int main(int argc, char ** argv)
if (create_listen_sockets() != 0)
{
- return 1;
+ goto error;
}
syslog(LOG_DAEMON, "collector listen on %s", json_sockpath);
switch (serv_type)
@@ -387,7 +387,7 @@ int main(int argc, char ** argv)
}
errno = 0;
- if (change_user_group(user, group) != 0)
+ if (change_user_group(user, group, pidfile, json_sockpath, serv_listen_path) != 0)
{
if (errno != 0)
{
@@ -397,7 +397,7 @@ int main(int argc, char ** argv)
{
syslog(LOG_DAEMON | LOG_ERR, "Change user/group failed.");
}
- return 1;
+ goto error;
}
signal(SIGINT, sighandler);
@@ -408,7 +408,7 @@ int main(int argc, char ** argv)
if (epollfd < 0)
{
syslog(LOG_DAEMON | LOG_ERR, "Error creating epoll: %s", strerror(errno));
- return 1;
+ goto error;
}
struct epoll_event accept_event = {};
@@ -417,14 +417,14 @@ int main(int argc, char ** argv)
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, json_sockfd, &accept_event) < 0)
{
syslog(LOG_DAEMON | LOG_ERR, "Error adding JSON fd to epoll: %s", strerror(errno));
- return 1;
+ goto error;
}
accept_event.data.fd = serv_sockfd;
accept_event.events = EPOLLIN;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, serv_sockfd, &accept_event) < 0)
{
syslog(LOG_DAEMON | LOG_ERR, "Error adding INET fd to epoll: %s", strerror(errno));
- return 1;
+ goto error;
}
struct epoll_event events[32];
@@ -729,6 +729,7 @@ int main(int argc, char ** argv)
}
}
+error:
close(json_sockfd);
close(serv_sockfd);
diff --git a/utils.c b/utils.c
index bb9aed179..cea51b4a1 100644
--- a/utils.c
+++ b/utils.c
@@ -5,6 +5,7 @@
#include <stdio.h>
#include <string.h>
#include <syslog.h>
+#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
@@ -114,7 +115,10 @@ int daemonize_shutdown(char const * const pidfile)
return 0;
}
-int change_user_group(char const * const user, char const * const group)
+int change_user_group(char const * const user, char const * const group,
+ char const * const pidfile,
+ char const * const uds_collector_path,
+ char const * const uds_distributor_path)
{
struct passwd * pwd;
struct group * grp;
@@ -143,5 +147,16 @@ int change_user_group(char const * const user, char const * const group)
gid = pwd->pw_gid;
}
+ if (uds_collector_path != NULL) {
+ chmod(uds_collector_path, S_IRUSR | S_IWUSR);
+ chown(uds_collector_path, pwd->pw_uid, gid);
+ }
+ if (uds_distributor_path != NULL) {
+ chmod(uds_distributor_path, S_IRUSR | S_IWUSR | S_IRGRP);
+ chown(uds_distributor_path, pwd->pw_uid, gid);
+ }
+ if (pidfile != NULL) {
+ chown(pidfile, pwd->pw_uid, gid);
+ }
return setregid(gid, gid) != 0 || setreuid(pwd->pw_uid, pwd->pw_uid);
}
diff --git a/utils.h b/utils.h
index be6c7cefd..f1765fc53 100644
--- a/utils.h
+++ b/utils.h
@@ -7,6 +7,9 @@ int daemonize_with_pidfile(char const * const pidfile);
int daemonize_shutdown(char const * const pidfile);
-int change_user_group(char const * const user, char const * const group);
+int change_user_group(char const * const user, char const * const group,
+ char const * const pidfile,
+ char const * const uds_collector_path,
+ char const * const uds_distributor_path);
#endif