summaryrefslogtreecommitdiff
blob: 1526ca445dfb3445f69b3780eff56ab3cc8e72b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
diff --git a/src/knot/conf/conf.c b/src/knot/conf/conf.c
index 4bbf622..0785b04 100644
--- a/src/knot/conf/conf.c
+++ b/src/knot/conf/conf.c
@@ -309,6 +309,10 @@ static int conf_process(conf_t *conf)
 		strncat(dest, dbext, strlen(dbext));
 		zone->ixfr_db = dest;
 	}
+	
+	/* Update UID and GID. */
+	if (conf->uid < 0) conf->uid = getuid();
+	if (conf->gid < 0) conf->gid = getgid();
 
 	return ret;
 }
diff --git a/src/knot/ctl/knotc_main.c b/src/knot/ctl/knotc_main.c
index 97412dd..7f74bca 100644
--- a/src/knot/ctl/knotc_main.c
+++ b/src/knot/ctl/knotc_main.c
@@ -43,7 +43,8 @@ enum knotc_flag_t {
 	F_VERBOSE     = 1 << 1,
 	F_WAIT        = 1 << 2,
 	F_INTERACTIVE = 1 << 3,
-	F_AUTO        = 1 << 4
+	F_AUTO        = 1 << 4,
+	F_UNPRIVILEGED= 1 << 5
 };
 
 static inline unsigned has_flag(unsigned flags, enum knotc_flag_t f) {
@@ -142,10 +143,15 @@ pid_t wait_cmd(pid_t proc, int *rc)
 	return proc;
 }
 
-pid_t start_cmd(const char *argv[], int argc)
+pid_t start_cmd(const char *argv[], int argc, int flags)
 {
 	pid_t chproc = fork();
 	if (chproc == 0) {
+	
+		/* Alter privileges. */
+		if (flags & F_UNPRIVILEGED) {
+			proc_update_privileges(conf()->uid, conf()->gid);
+		}
 
 		/* Duplicate, it doesn't run from stack address anyway. */
 		char **args = malloc((argc + 1) * sizeof(char*));
@@ -180,7 +186,7 @@ pid_t start_cmd(const char *argv[], int argc)
 int exec_cmd(const char *argv[], int argc)
 {
 	int ret = 0;
-	pid_t proc = start_cmd(argv, argc);
+	pid_t proc = start_cmd(argv, argc, 0);
 	wait_cmd(proc, &ret);
 	return ret;
 }
@@ -291,17 +297,6 @@ int execute(const char *action, char **argv, int argc, pid_t pid,
 	int valid_cmd = 0;
 	int rc = 0;
 	if (strcmp(action, "start") == 0) {
-		// Check pidfile for w+
-		FILE* chkf = fopen(pidfile, "w+");
-		if (chkf == NULL) {
-			log_server_error("PID file '%s' is not writeable, "
-			                 "refusing to start\n", pidfile);
-			return 1;
-		} else {
-			fclose(chkf);
-			chkf = NULL;
-		}
-
 		// Check PID
 		valid_cmd = 1;
 //		if (pid < 0 && pid == KNOT_ERANGE) {
@@ -604,7 +599,7 @@ int execute(const char *action, char **argv, int argc, pid_t pid,
 			}
 			fflush(stdout);
 			fflush(stderr);
-			pid_t zcpid = start_cmd(args, ac);
+			pid_t zcpid = start_cmd(args, ac, F_UNPRIVILEGED);
 			zctask_add(tasks, jobs, zcpid, zone);
 			++running;
 		}
@@ -722,7 +717,7 @@ int main(int argc, char **argv)
 		log_levels_add(LOGT_STDOUT, LOG_ANY,
 		               LOG_MASK(LOG_INFO)|LOG_MASK(LOG_DEBUG));
 	}
-
+	
 	// Fetch PID
 	char* pidfile = pid_filename();
 	if (!pidfile) {
diff --git a/src/knot/ctl/process.c b/src/knot/ctl/process.c
index bb61804..8864cd0 100644
--- a/src/knot/ctl/process.c
+++ b/src/knot/ctl/process.c
@@ -21,6 +21,8 @@
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
+#include <grp.h>
+#include <unistd.h>
 
 #include "knot/common.h"
 #include "knot/ctl/process.h"
@@ -113,6 +115,7 @@ int pid_write(const char* fn)
 int pid_remove(const char* fn)
 {
 	if (unlink(fn) < 0) {
+		perror("unlink");
 		return KNOTD_EINVAL;
 	}
 
@@ -124,3 +127,45 @@ int pid_running(pid_t pid)
 	return kill(pid, 0) == 0;
 }
 
+void proc_update_privileges(int uid, int gid)
+{
+#ifdef HAVE_SETGROUPS
+	/* Drop supplementary groups. */
+	if (uid != getuid() || gid != getgid()) {
+		if (setgroups(0, NULL) < 0) {
+			log_server_warning("Failed to drop supplementary groups"
+			                   " for uid '%d' (%s).\n",
+			                   getuid(), strerror(errno));
+		}
+	}
+#endif
+	
+	/* Watch uid/gid. */
+	if (gid != getgid()) {
+		log_server_info("Changing group id to '%d'.\n", gid);
+		if (setregid(gid, gid) < 0) {
+			log_server_error("Failed to change gid to '%d'.\n",
+			                 gid);
+		}
+	}
+	if (uid != getuid()) {
+		log_server_info("Changing user id to '%d'.\n", uid);
+		if (setreuid(uid, uid) < 0) {
+			log_server_error("Failed to change uid to '%d'.\n",
+			                 uid);
+		}
+	}
+	
+	/* Check storage writeability. */
+	char *lfile = strcdup(conf()->storage, "/knot.lock");
+	assert(lfile != NULL);
+	FILE* fp = fopen(lfile, "w");
+	if (fp == NULL) {
+		log_server_warning("Storage directory '%s' is not writeable.\n",
+		                   conf()->storage);
+	} else {
+		fclose(fp);
+		unlink(lfile);
+	}
+	free(lfile);
+}
diff --git a/src/knot/ctl/process.h b/src/knot/ctl/process.h
index d8f2f4c..a387add 100644
--- a/src/knot/ctl/process.h
+++ b/src/knot/ctl/process.h
@@ -83,6 +83,15 @@ int pid_remove(const char* fn);
  */
 int pid_running(pid_t pid);
 
+/*!
+ * \brief Update process privileges to new UID/GID.
+ *
+ * \param uid New user ID.
+ * \param gid New group ID.
+ *
+ */
+void proc_update_privileges(int uid, int gid);
+
 #endif // _KNOTD_PROCESS_H_
 
 /*! @} */
diff --git a/src/knot/main.c b/src/knot/main.c
index 99ee1cf..a62230a 100644
--- a/src/knot/main.c
+++ b/src/knot/main.c
@@ -20,6 +20,7 @@
 #include <unistd.h>
 #include <getopt.h>
 #include <limits.h>
+
 #ifdef HAVE_CAP_NG_H
 #include <cap-ng.h>
 #endif /* HAVE_CAP_NG_H */
@@ -161,7 +162,6 @@ int main(int argc, char **argv)
 	conf_read_lock();
 	conf_add_hook(conf(), CONF_LOG, log_conf_hook, 0);
 	conf_add_hook(conf(), CONF_ALL, server_conf_hook, server);
-	conf_add_hook(conf(), CONF_ALL, zones_ns_conf_hook, server->nameserver);
 	conf_read_unlock();
 
 	// Find implicit configuration file
@@ -242,21 +242,28 @@ int main(int argc, char **argv)
 	}
 	log_server_info("\n");
 
-	// Create server instance
-	char* pidfile = pid_filename();
+	/* Alter privileges. */
+	proc_update_privileges(conf()->uid, conf()->gid);
+	
+	/* Load zones and add hook. */
+	zones_ns_conf_hook(conf(), server->nameserver);
+	conf_add_hook(conf(), CONF_ALL, zones_ns_conf_hook, server->nameserver);
 
 	// Run server
 	int res = 0;
+	int has_pid = 0;
+	char* pidfile = pid_filename();
 	log_server_info("Starting server...\n");
 	if ((server_start(server)) == KNOTD_EOK) {
 
 		// Save PID
-		int has_pid = 1;
+		has_pid = 1;
 		int rc = pid_write(pidfile);
 		if (rc < 0) {
 			has_pid = 0;
 			log_server_warning("Failed to create "
-					   "PID file '%s'.\n", pidfile);
+					   "PID file '%s' (%s).\n",
+					   pidfile, strerror(errno));
 		}
 
 		// Change directory if daemonized
@@ -370,7 +377,7 @@ int main(int argc, char **argv)
 	server_destroy(&server);
 
 	// Remove PID file
-	if (pid_remove(pidfile) < 0) {
+	if (has_pid && pid_remove(pidfile) < 0) {
 		log_server_warning("Failed to remove PID file.\n");
 	}
 
diff --git a/src/knot/server/server.c b/src/knot/server/server.c
index 5611a0c..5df7fe1 100644
--- a/src/knot/server/server.c
+++ b/src/knot/server/server.c
@@ -22,8 +22,6 @@
 #include <errno.h>
 #include <openssl/evp.h>
 #include <assert.h>
-#include <grp.h>
-
 
 #include "common/prng.h"
 #include "knot/common.h"
@@ -743,51 +741,9 @@ int server_conf_hook(const struct conf_t *conf, void *data)
 			                 "configured interfaces.\n");
 		}
 	}
-	
-	/* Lock configuration. */
-	conf_read_lock();
-	int priv_failed = 0;
-	
-#ifdef HAVE_SETGROUPS
-	/* Drop supplementary groups. */
-	if (conf->gid > -1 || conf->uid > -1) {
-		ret = setgroups(0, NULL);
-		
-		/* Collect results. */
-		if (ret < 0) {
-			log_server_error("Failed to set supplementary groups "
-			                 "for uid '%d' (%s).\n",
-			                 getuid(), strerror(errno));
-			priv_failed = 1;
-		}
-	}
-#endif
-	
-	/* Watch uid/gid. */
-	if (conf->gid > -1 && conf->gid != getgid()) {
-		log_server_info("Changing group id to '%d'.\n", conf->gid);
-		if (setregid(conf->gid, conf->gid) < 0) {
-			log_server_error("Failed to change gid to '%d'.\n",
-			                 conf->gid);
-			priv_failed = 1;
-		}
-	}
-	if (conf->uid > -1 && conf->uid != getuid()) {
-		log_server_info("Changing user id to '%d'.\n", conf->uid);
-		if (setreuid(conf->uid, conf->uid) < 0) {
-			log_server_error("Failed to change uid to '%d'.\n",
-			                 conf->uid);
-			priv_failed = 1;
-		}
-	}
-
-	if (priv_failed) {
-		ret = KNOTD_EACCES;
-	}
 
 	/* Exit if the server is not running. */
 	if (ret != KNOTD_EOK || !(server->state & ServerRunning)) {
-		conf_read_unlock();
 		return KNOTD_ENOTRUNNING;
 	}
 
@@ -807,9 +763,6 @@ int server_conf_hook(const struct conf_t *conf, void *data)
 		}
 	}
 
-	/* Unlock config. */
-	conf_read_unlock();
-
 	return ret;
 }
 
diff --git a/src/zcompile/zcompile.c b/src/zcompile/zcompile.c
index c4415d4..3c39004 100644
--- a/src/zcompile/zcompile.c
+++ b/src/zcompile/zcompile.c
@@ -570,7 +570,7 @@ int zone_read(const char *name, const char *zonefile, const char *outfile,
 	}
 
 	if (!knot_dname_is_fqdn(dname)) {
-		fprintf(stderr, "Error: given zone origin is not FQDN.\n");
+		log_zone_error("Error: given zone origin is not FQDN.\n");
 		knot_dname_release(dname);
 		return KNOTDZCOMPILE_EINVAL;
 	}
@@ -660,8 +660,7 @@ int zone_read(const char *name, const char *zonefile, const char *outfile,
 
 	if (found_orphans != parser->rrsig_orphan_count) {
 		/*! \todo This might be desired behaviour. */
-		fprintf(stderr,
-		        "There are unassigned RRSIGs in the zone!\n");
+		log_zone_error("There are unassigned RRSIGs in the zone!\n");
 		parser->errors++;
 	}