OpenDNSSEC-enforcer  1.4.3
daemon.c
Go to the documentation of this file.
1 /*
2  * $Id: daemon.c 7385 2013-11-05 16:00:17Z sara $
3  *
4  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*
30  * daemon.c code needed to get a daemon up and running
31  *
32  * edit the DAEMONCONFIG and cmlParse function
33  * in daemon_util.[c|h] to add options specific
34  * to your app
35  *
36  * gcc -o daemon daemon_util.c daemon.c
37  *
38  * Most of this is based on stuff I have seen in NSD
39  */
40 
41 #include "config.h"
42 
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <syslog.h>
46 #include <stdarg.h>
47 #include <errno.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <signal.h>
53 
54 #include "daemon.h"
55 #include "daemon_util.h"
56 #include "privdrop.h"
57 
58 #include "ksm/ksm.h"
59 #include "ksm/dbsmsg.h"
60 #include "ksm/dbsdef.h"
61 #include "ksm/kmemsg.h"
62 #include "ksm/kmedef.h"
63 #include "ksm/message.h"
64 #include "ksm/string_util.h"
65 
66 #ifndef MAXPATHLEN
67 # define MAXPATHLEN 4096
68 #endif
69 
70 extern int server_init(DAEMONCONFIG *config);
71 extern void server_main(DAEMONCONFIG *config);
72 
74 
75 void
76 sig_handler (int sig)
77 {
78  switch (sig) {
79  case SIGCHLD:
80  return;
81  case SIGHUP:
82  return;
83  case SIGALRM:
84  break;
85  case SIGILL:
86  break;
87  case SIGUSR1:
88  break;
89  case SIGINT:
90  config.term = 2;
91  break;
92  case SIGTERM:
93  config.term = 1;
94  break;
95  default:
96  break;
97  }
98 }
99 
101 
102 void
104 {
105  /* Only unlink pidfile if its our pidfile */
106  if (daemon_our_pidfile) {
107  unlink(config.pidfile);
108  }
109 }
110 
111 int
112 main(int argc, char *argv[]){
113  int fd;
114  struct sigaction action;
115  const char* program; /* Temporary for program name */
116 
117  config.debug = false;
118  config.once = false;
119 
120  config.pidfile = NULL;
121  config.program = NULL;
122  config.host = NULL;
123  config.port = NULL;
124  config.user = (unsigned char *)calloc(MAX_USER_LENGTH, sizeof(char));
125  config.password = (unsigned char *)calloc(MAX_PASSWORD_LENGTH, sizeof(char));
126  config.schema = (unsigned char *)calloc(MAX_SCHEMA_LENGTH, sizeof(char));
127  config.DSSubmitCmd = (char *)calloc(MAXPATHLEN + 1024, sizeof(char));
128  config.policy = NULL;
129 
130  if (config.user == NULL || config.password == NULL || config.schema == NULL) {
131  log_msg(&config, LOG_ERR, "Malloc for config struct failed");
132  exit(1);
133  }
134  config.term = 0;
135 
136  /* Lets set up the logging first */
137  /* The program name is the last component of the program file name */
138  if ((program = strrchr(argv[0], '/'))) { /* EQUALS */
139  ++program; /* Point to character after last "/" */
140  }
141  else {
142  program = argv[0]; /* No slash, so use string given */
143  }
144  config.program = program;
146 
147  log_init(config.log_user, config.program);
148 
149  /* useful message */
150  log_msg(&config, LOG_INFO, "%s starting...", PACKAGE_NAME);
151 
152 #ifdef ENFORCER_TIMESHIFT
153  if (getenv("ENFORCER_TIMESHIFT")) {
154  log_msg(&config, LOG_INFO, "Timeshift mode detected, running once only!");
155  fprintf(stderr, "WARNING: Timeshift mode detected, running once only!\n");
156  config.once = true;
157  config.debug = true;
158  }
159 #endif /* ENFORCER_TIMESHIFT */
160 
161  /* Process command line */
162  cmdlParse(&config, &argc, argv);
163  if(config.debug) log_msg(&config, LOG_INFO, "%s DEBUG ON.", PACKAGE_NAME);
164 
165  /* If we dont debug then fork */
166  if(!config.debug){
167  /* Fork */
168  switch ((config.pid = fork())) {
169  case 0:
170  break;
171  case -1:
172  log_msg(&config, LOG_ERR, "fork failed: %s", strerror(errno));
173  unlink(config.pidfile);
174  exit(1);
175  default:
176  fprintf(stdout, "OpenDNSSEC ods-enforcerd started (version %s), pid %d\n", PACKAGE_VERSION, (int) config.pid);
177  log_msg(&config, LOG_INFO, "%s Parent exiting...", PACKAGE_NAME);
178  exit(0);
179  }
180 
181  /* Detach ourselves... */
182  if (setsid() == -1) {
183  log_msg(&config, LOG_ERR, "setsid() failed: %s", strerror(errno));
184  exit(1);
185  }
186 
187  if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
188  (void)dup2(fd, STDIN_FILENO);
189  (void)dup2(fd, STDOUT_FILENO);
190  (void)dup2(fd, STDERR_FILENO);
191  if (fd > 2)
192  (void)close(fd);
193  }
194  log_msg(&config, LOG_INFO, "%s forked OK...", PACKAGE_NAME);
195  } else {
196  log_msg(&config, LOG_INFO, "%s in debug mode - not forking...", PACKAGE_NAME);
197  }
198 
199  action.sa_handler = sig_handler;
200  sigfillset(&action.sa_mask);
201  action.sa_flags = 0;
202  sigaction(SIGTERM, &action, NULL);
203  sigaction(SIGHUP, &action, NULL);
204  sigaction(SIGINT, &action, NULL);
205  sigaction(SIGILL, &action, NULL);
206  sigaction(SIGUSR1, &action, NULL);
207  sigaction(SIGALRM, &action, NULL);
208  sigaction(SIGCHLD, &action, NULL);
209  action.sa_handler = SIG_IGN;
210  sigaction(SIGPIPE, &action, NULL);
211 
212  /* Get perms that we will be dropping to */
213  if (getPermsForDrop(&config) != 0) {
214  exit(1);
215  }
216 
217  /* Run the server specific code. You need to provide this function somewhere
218  this sets our pidfile */
219  if (server_init(&config) != 0) {
220  exit(1);
221  }
222 
223  /* make the directory for the pidfile if required; do this before we drop
224  privs */
225  if (createPidDir(&config) != 0) {
226  exit(1);
227  }
228 
229  /*
230  * Drop permissions.
231  * This function exits if something goes wrong
232  */
233  privdrop(config.username, config.groupname, NULL);
234 
235  config.uid = geteuid();
236  config.gid = getegid();
237  config.pid = getpid();
238 
239  atexit(exit_function);
240 
241  log_msg(&config, LOG_NOTICE, "%s started (version %s), pid %d", PACKAGE_NAME, PACKAGE_VERSION,
242  (int) config.pid);
243 
244  MsgInit();
247 
248  /* Do something. You need to provide this function somewhere */
249  server_main(&config);
250 
251  /* Free stuff here (exit from sigs pass through) */
252  MsgRundown();
253  if (config.host) free(config.host);
254  if (config.port) free(config.port);
255  free(config.user);
256  free(config.password);
257  free(config.schema);
258  free(config.DSSubmitCmd);
259 
260  StrFree(config.username);
261  StrFree(config.groupname);
262 #if 0
263  StrFree(config.chrootdir);
264 #endif
265 
266  exit(0);
267 
268 }
269 
void server_main(DAEMONCONFIG *config)
Definition: enforcer.c:83
unsigned char * password
Definition: daemon.h:108
int main(int argc, char *argv[])
Definition: daemon.c:112
unsigned char * schema
Definition: daemon.h:109
void exit_function(void)
Definition: daemon.c:103
bool once
Definition: daemon.h:91
#define DEFAULT_LOG_FACILITY
Definition: daemon.h:81
#define MAXPATHLEN
Definition: daemon.c:67
pid_t pid
Definition: daemon.h:92
#define StrFree(x)
Definition: string_util.h:68
void sig_handler(int sig)
Definition: daemon.c:76
char * pidfile
Definition: daemon.h:93
char * DSSubmitCmd
Definition: daemon.h:116
int privdrop(const char *username, const char *groupname, const char *newroot)
Definition: privdrop.c:50
int daemon_our_pidfile
Definition: daemon.c:100
int log_user
Definition: daemon.h:120
const char * program
Definition: daemon.h:89
char * username
Definition: daemon.h:96
int getPermsForDrop(DAEMONCONFIG *config)
Definition: daemon_util.c:93
uid_t uid
Definition: daemon.h:94
#define MAX_USER_LENGTH
Definition: daemon.h:69
#define DBS_MIN_VALUE
Definition: dbsmsg.h:6
void cmdlParse(DAEMONCONFIG *config, int *argc, char **argv)
Definition: daemon_util.c:625
void log_init(int facility, const char *program_name)
Definition: daemon_util.c:267
#define MAX_PASSWORD_LENGTH
Definition: daemon.h:71
unsigned char * host
Definition: daemon.h:107
void MsgRegister(int min, int max, const char **message, MSG_OUTPUT_FUNCTION output)
Definition: message.c:143
int term
Definition: daemon.h:102
#define KME_MAX_VALUE
Definition: kmemsg.h:7
#define DBS_MAX_VALUE
Definition: dbsmsg.h:7
void ksm_log_msg(const char *format)
Definition: daemon_util.c:315
bool debug
Definition: daemon.h:90
void MsgRundown(void)
Definition: message.c:414
int createPidDir(DAEMONCONFIG *config)
Definition: daemon_util.c:538
#define KME_MIN_VALUE
Definition: kmemsg.h:6
#define MAX_SCHEMA_LENGTH
Definition: daemon.h:72
char * policy
Definition: daemon.h:118
gid_t gid
Definition: daemon.h:95
DAEMONCONFIG config
Definition: daemon.c:73
char * groupname
Definition: daemon.h:97
void log_msg(DAEMONCONFIG *config, int priority, const char *format,...)
Definition: daemon_util.c:296
unsigned char * port
Definition: daemon.h:110
void MsgInit(void)
Definition: message.c:65
int server_init(DAEMONCONFIG *config)
Definition: enforcer.c:64
unsigned char * user
Definition: daemon.h:106