11 #include <sys/types.h>
19 #include "appconfig.h"
21 #include "web_client.h"
22 #include "plugins_d.h"
28 char pidfile[FILENAME_MAX + 1] = "";
31 void sig_handler(int signo)
37 int become_user(const char *username)
39 struct passwd *pw = getpwnam(username);
41 error("User %s is not present.", username);
45 uid_t uid = pw->pw_uid;
46 gid_t gid = pw->pw_gid;
48 int ngroups = sysconf(_SC_NGROUPS_MAX);
49 gid_t *supplementary_groups = NULL;
51 supplementary_groups = malloc(sizeof(gid_t) * ngroups);
52 if(supplementary_groups) {
53 if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
54 error("Cannot get supplementary groups of user '%s'.", username);
55 free(supplementary_groups);
56 supplementary_groups = NULL;
60 else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
63 if(pidfile[0] && getuid() != uid) {
64 // we are dropping privileges
65 if(chown(pidfile, uid, gid) != 0)
66 error("Cannot chown pidfile '%s' to user '%s'", pidfile, username);
68 else if(pidfd != -1) {
69 // not need to keep it open
74 else if(pidfd != -1) {
75 // not need to keep it open
80 if(supplementary_groups && ngroups) {
81 if(setgroups(ngroups, supplementary_groups) == -1)
82 error("Cannot set supplementary groups for user '%s'", username);
84 free(supplementary_groups);
85 supplementary_groups = NULL;
89 if(setresgid(gid, gid, gid) != 0) {
90 error("Cannot switch to user's %s group (gid: %d).", username, gid);
94 if(setresuid(uid, uid, uid) != 0) {
95 error("Cannot switch to user %s (uid: %d).", username, uid);
99 if(setgid(gid) != 0) {
100 error("Cannot switch to user's %s group (gid: %d).", username, gid);
103 if(setegid(gid) != 0) {
104 error("Cannot effectively switch to user's %s group (gid: %d).", username, gid);
107 if(setuid(uid) != 0) {
108 error("Cannot switch to user %s (uid: %d).", username, uid);
111 if(seteuid(uid) != 0) {
112 error("Cannot effectively switch to user %s (uid: %d).", username, uid);
119 int become_daemon(int dont_fork, int close_all_files, const char *user, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp)
123 // open the files before forking
124 int input_fd = -1, output_fd = -1, error_fd = -1, dev_null;
126 if(input && *input) {
127 if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
128 error("Cannot open input file '%s'.", input);
133 if(output && *output) {
134 if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
135 error("Cannot open output log file '%s'", output);
136 if(input_fd != -1) close(input_fd);
141 if(error && *error) {
142 if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
143 error("Cannot open error log file '%s'.", error);
144 if(input_fd != -1) close(input_fd);
145 if(output_fd != -1) close(output_fd);
150 if(access && *access && access_fd) {
151 if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
152 error("Cannot open access log file '%s'", access);
153 if(input_fd != -1) close(input_fd);
154 if(output_fd != -1) close(output_fd);
155 if(error_fd != -1) close(error_fd);
160 *access_fp = fdopen(*access_fd, "w");
162 error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
163 if(input_fd != -1) close(input_fd);
164 if(output_fd != -1) close(output_fd);
165 if(error_fd != -1) close(error_fd);
173 if((dev_null = open("/dev/null", O_RDWR, 0666)) == -1) {
174 perror("Cannot open /dev/null");
175 if(input_fd != -1) close(input_fd);
176 if(output_fd != -1) close(output_fd);
177 if(error_fd != -1) close(error_fd);
178 if(access && access_fd && *access_fd != -1) {
195 perror("cannot fork");
199 exit(0); // the parent
202 // become session leader
204 perror("Cannot become session leader.");
213 perror("cannot fork");
217 exit(0); // the parent
221 // Set new file permissions
225 if(close_all_files) {
227 for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
229 ((access_fd && i != *access_fd) || !access_fd)
239 close(STDOUT_FILENO);
240 close(STDERR_FILENO);
243 // put the opened files
244 // to our standard file descriptors
246 if(input_fd != STDIN_FILENO) {
247 dup2(input_fd, STDIN_FILENO);
252 else dup2(dev_null, STDIN_FILENO);
254 if(output_fd != -1) {
255 if(output_fd != STDOUT_FILENO) {
256 dup2(output_fd, STDOUT_FILENO);
261 else dup2(dev_null, STDOUT_FILENO);
264 if(error_fd != STDERR_FILENO) {
265 dup2(error_fd, STDERR_FILENO);
270 else dup2(dev_null, STDERR_FILENO);
273 if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
276 // generate our pid file
278 pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
280 if(ftruncate(pidfd, 0) != 0)
281 error("Cannot truncate pidfile '%s'.", pidfile);
284 sprintf(b, "%d\n", getpid());
285 ssize_t i = write(pidfd, b, strlen(b));
287 error("Cannot write pidfile '%s'.", pidfile);
289 // don't close it, we might need it at exit
292 else error("Failed to open pidfile '%s'.", pidfile);
296 if(become_user(user) != 0) {
297 error("Cannot become user '%s'. Continuing as we are.", user);
299 else info("Successfully became user '%s'.", user);