2 * Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: os.c,v 1.66.18.11 2006/02/03 23:51:38 marka Exp $ */
25 #include <sys/types.h> /* dev_t FreeBSD 2.1 */
31 #include <grp.h> /* Required for initgroups() on IRIX. */
42 #include <isc/buffer.h>
44 #include <isc/print.h>
45 #include <isc/result.h>
46 #include <isc/strerror.h>
47 #include <isc/string.h>
49 #include <named/main.h>
52 #include <named/ns_smf_globals.h>
55 static char *pidfile = NULL;
56 static int devnullfd = -1;
59 #define ISC_FACILITY LOG_DAEMON
63 * If there's no <linux/capability.h>, we don't care about <sys/prctl.h>
65 #ifndef HAVE_LINUX_CAPABILITY_H
66 #undef HAVE_SYS_PRCTL_H
71 * (T) HAVE_LINUXTHREADS
72 * (C) HAVE_LINUX_CAPABILITY_H
73 * (P) HAVE_SYS_PRCTL_H
74 * The possible cases are:
75 * none: setuid() normally
77 * C: setuid() normally, drop caps (keep CAP_SETUID)
78 * T+C: no setuid(), drop caps (don't keep CAP_SETUID)
79 * T+C+P: setuid() early, drop caps (keep CAP_SETUID)
80 * C+P: setuid() normally, drop caps (keep CAP_SETUID)
85 * caps = BIND_SERVICE + CHROOT + SETGID
86 * if ((T && C && P) || !T)
91 * if (T && C && P && -u)
98 * if (C && (P || !-u))
103 * It will be nice when Linux threads work properly with setuid().
106 #ifdef HAVE_LINUXTHREADS
107 static pid_t mainpid = 0;
110 static struct passwd *runas_pw = NULL;
111 static isc_boolean_t done_setuid = ISC_FALSE;
112 static int dfd[2] = { -1, -1 };
114 #ifdef HAVE_LINUX_CAPABILITY_H
116 static isc_boolean_t non_root = ISC_FALSE;
117 static isc_boolean_t non_root_caps = ISC_FALSE;
120 * We define _LINUX_FS_H to prevent it from being included. We don't need
121 * anything from it, and the files it includes cause warnings with 2.2
122 * kernels, and compilation failures (due to conflicts between <linux/string.h>
123 * and <string.h>) on 2.3 kernels.
127 #include <sys/syscall.h> /* Required for syscall(). */
128 #include <linux/capability.h> /* Required for _LINUX_CAPABILITY_VERSION. */
130 #ifdef HAVE_SYS_PRCTL_H
131 #include <sys/prctl.h> /* Required for prctl(). */
134 * If the value of PR_SET_KEEPCAPS is not in <sys/prctl.h>, define it
135 * here. This allows setuid() to work on systems running a new enough
136 * kernel but with /usr/include/linux pointing to "standard" kernel
139 #ifndef PR_SET_KEEPCAPS
140 #define PR_SET_KEEPCAPS 8
143 #endif /* HAVE_SYS_PRCTL_H */
147 #include <asm/unistd.h> /* Slackware 4.0 needs this. */
149 #define SYS_capset __NR_capset
153 linux_setcaps(unsigned int caps) {
154 struct __user_cap_header_struct caphead;
155 struct __user_cap_data_struct cap;
156 char strbuf[ISC_STRERRORSIZE];
158 if ((getuid() != 0 && !non_root_caps) || non_root)
161 memset(&caphead, 0, sizeof(caphead));
162 caphead.version = _LINUX_CAPABILITY_VERSION;
164 memset(&cap, 0, sizeof(cap));
165 cap.effective = caps;
166 cap.permitted = caps;
168 if (syscall(SYS_capset, &caphead, &cap) < 0) {
169 isc__strerror(errno, strbuf, sizeof(strbuf));
170 ns_main_earlyfatal("capset failed: %s:"
171 " please ensure that the capset kernel"
172 " module is loaded. see insmod(8)",
178 linux_initialprivs(void) {
182 * We don't need most privileges, so we drop them right away.
183 * Later on linux_minprivs() will be called, which will drop our
184 * capabilities to the minimum needed to run the server.
190 * We need to be able to bind() to privileged ports, notably port 53!
192 caps |= (1 << CAP_NET_BIND_SERVICE);
195 * We need chroot() initially too.
197 caps |= (1 << CAP_SYS_CHROOT);
199 #if defined(HAVE_SYS_PRCTL_H) || !defined(HAVE_LINUXTHREADS)
201 * We can setuid() only if either the kernel supports keeping
202 * capabilities after setuid() (which we don't know until we've
203 * tried) or we're not using threads. If either of these is
204 * true, we want the setuid capability.
206 caps |= (1 << CAP_SETUID);
210 * Since we call initgroups, we need this.
212 caps |= (1 << CAP_SETGID);
215 * Without this, we run into problems reading a configuration file
216 * owned by a non-root user and non-world-readable on startup.
218 caps |= (1 << CAP_DAC_READ_SEARCH);
221 * XXX We might want to add CAP_SYS_RESOURCE, though it's not
222 * clear it would work right given the way linuxthreads work.
223 * XXXDCL But since we need to be able to set the maximum number
224 * of files, the stack size, data size, and core dump size to
225 * support named.conf options, this is now being added to test.
227 caps |= (1 << CAP_SYS_RESOURCE);
233 linux_minprivs(void) {
237 * Drop all privileges except the ability to bind() to privileged
240 * It's important that we drop CAP_SYS_CHROOT. If we didn't, it
241 * chroot() could be used to escape from the chrooted area.
245 caps |= (1 << CAP_NET_BIND_SERVICE);
248 * XXX We might want to add CAP_SYS_RESOURCE, though it's not
249 * clear it would work right given the way linuxthreads work.
250 * XXXDCL But since we need to be able to set the maximum number
251 * of files, the stack size, data size, and core dump size to
252 * support named.conf options, this is now being added to test.
254 caps |= (1 << CAP_SYS_RESOURCE);
259 #ifdef HAVE_SYS_PRCTL_H
261 linux_keepcaps(void) {
262 char strbuf[ISC_STRERRORSIZE];
264 * Ask the kernel to allow us to keep our capabilities after we
268 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) {
269 if (errno != EINVAL) {
270 isc__strerror(errno, strbuf, sizeof(strbuf));
271 ns_main_earlyfatal("prctl() failed: %s", strbuf);
274 non_root_caps = ISC_TRUE;
281 #endif /* HAVE_LINUX_CAPABILITY_H */
285 setup_syslog(const char *progname) {
290 options |= LOG_NDELAY;
292 openlog(isc_file_basename(progname), options, ISC_FACILITY);
296 ns_os_init(const char *progname) {
297 setup_syslog(progname);
298 #ifdef HAVE_LINUX_CAPABILITY_H
299 linux_initialprivs();
301 #ifdef HAVE_LINUXTHREADS
305 signal(SIGXFSZ, SIG_IGN);
310 ns_os_daemonize(void) {
312 char strbuf[ISC_STRERRORSIZE];
314 if (pipe(dfd) == -1) {
315 isc__strerror(errno, strbuf, sizeof(strbuf));
316 ns_main_earlyfatal("pipe(): %s", strbuf);
321 isc__strerror(errno, strbuf, sizeof(strbuf));
322 ns_main_earlyfatal("fork(): %s", strbuf);
327 * Wait for the child to finish loading for the first time.
328 * This would be so much simpler if fork() worked once we
329 * were multi-threaded.
334 n = read(dfd[0], &buf, 1);
337 } while (n == -1 && errno == EINTR);
346 #ifdef HAVE_LINUXTHREADS
350 if (setsid() == -1) {
351 isc__strerror(errno, strbuf, sizeof(strbuf));
352 ns_main_earlyfatal("setsid(): %s", strbuf);
356 * Try to set stdin, stdout, and stderr to /dev/null, but press
357 * on even if it fails.
359 * XXXMLG The close() calls here are unneeded on all but NetBSD, but
360 * are harmless to include everywhere. dup2() is supposed to close
361 * the FD if it is in use, but unproven-pthreads-0.16 is broken
362 * and will end up closing the wrong FD. This will be fixed eventually,
363 * and these calls will be removed.
365 if (devnullfd != -1) {
366 if (devnullfd != STDIN_FILENO) {
367 (void)close(STDIN_FILENO);
368 (void)dup2(devnullfd, STDIN_FILENO);
370 if (devnullfd != STDOUT_FILENO) {
371 (void)close(STDOUT_FILENO);
372 (void)dup2(devnullfd, STDOUT_FILENO);
374 if (devnullfd != STDERR_FILENO) {
375 (void)close(STDERR_FILENO);
376 (void)dup2(devnullfd, STDERR_FILENO);
382 ns_os_started(void) {
386 * Signal to the parent that we stated successfully.
388 if (dfd[0] != -1 && dfd[1] != -1) {
389 write(dfd[1], &buf, 1);
391 dfd[0] = dfd[1] = -1;
396 ns_os_opendevnull(void) {
397 devnullfd = open("/dev/null", O_RDWR, 0);
401 ns_os_closedevnull(void) {
402 if (devnullfd != STDIN_FILENO &&
403 devnullfd != STDOUT_FILENO &&
404 devnullfd != STDERR_FILENO) {
411 all_digits(const char *s) {
415 if (!isdigit((*s)&0xff))
423 ns_os_chroot(const char *root) {
424 char strbuf[ISC_STRERRORSIZE];
429 if (chroot(root) < 0) {
430 isc__strerror(errno, strbuf, sizeof(strbuf));
431 ns_main_earlyfatal("chroot(): %s", strbuf);
433 if (chdir("/") < 0) {
434 isc__strerror(errno, strbuf, sizeof(strbuf));
435 ns_main_earlyfatal("chdir(/): %s", strbuf);
438 /* Set ns_smf_chroot flag on successful chroot. */
445 ns_os_inituserinfo(const char *username) {
446 char strbuf[ISC_STRERRORSIZE];
447 if (username == NULL)
450 if (all_digits(username))
451 runas_pw = getpwuid((uid_t)atoi(username));
453 runas_pw = getpwnam(username);
456 if (runas_pw == NULL)
457 ns_main_earlyfatal("user '%s' unknown", username);
460 if (initgroups(runas_pw->pw_name, runas_pw->pw_gid) < 0) {
461 isc__strerror(errno, strbuf, sizeof(strbuf));
462 ns_main_earlyfatal("initgroups(): %s", strbuf);
469 ns_os_changeuser(void) {
470 char strbuf[ISC_STRERRORSIZE];
471 if (runas_pw == NULL || done_setuid)
474 done_setuid = ISC_TRUE;
476 #ifdef HAVE_LINUXTHREADS
477 #ifdef HAVE_LINUX_CAPABILITY_H
479 ns_main_earlyfatal("-u with Linux threads not supported: "
480 "requires kernel support for "
481 "prctl(PR_SET_KEEPCAPS)");
483 ns_main_earlyfatal("-u with Linux threads not supported: "
484 "no capabilities support or capabilities "
485 "disabled at build time");
489 if (setgid(runas_pw->pw_gid) < 0) {
490 isc__strerror(errno, strbuf, sizeof(strbuf));
491 ns_main_earlyfatal("setgid(): %s", strbuf);
494 if (setuid(runas_pw->pw_uid) < 0) {
495 isc__strerror(errno, strbuf, sizeof(strbuf));
496 ns_main_earlyfatal("setuid(): %s", strbuf);
499 #if defined(HAVE_LINUX_CAPABILITY_H) && !defined(HAVE_LINUXTHREADS)
502 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_DUMPABLE)
504 * Restore the ability of named to drop core after the setuid()
505 * call has disabled it.
507 prctl(PR_SET_DUMPABLE,1,0,0,0);
512 ns_os_minprivs(void) {
513 #ifdef HAVE_SYS_PRCTL_H
517 #ifdef HAVE_LINUXTHREADS
518 ns_os_changeuser(); /* Call setuid() before threads are started */
521 #if defined(HAVE_LINUX_CAPABILITY_H) && defined(HAVE_LINUXTHREADS)
527 safe_open(const char *filename, isc_boolean_t append) {
531 if (stat(filename, &sb) == -1) {
534 } else if ((sb.st_mode & S_IFREG) == 0) {
540 fd = open(filename, O_WRONLY|O_CREAT|O_APPEND,
541 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
543 (void)unlink(filename);
544 fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
545 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
551 cleanup_pidfile(void) {
552 if (pidfile != NULL) {
553 (void)unlink(pidfile);
560 ns_os_writepidfile(const char *filename, isc_boolean_t first_time) {
565 char strbuf[ISC_STRERRORSIZE];
566 void (*report)(const char *, ...);
569 * The caller must ensure any required synchronization.
572 report = first_time ? ns_main_earlyfatal : ns_main_earlywarning;
576 if (filename == NULL)
579 len = strlen(filename);
580 pidfile = malloc(len + 1);
581 if (pidfile == NULL) {
582 isc__strerror(errno, strbuf, sizeof(strbuf));
583 (*report)("couldn't malloc '%s': %s", filename, strbuf);
587 strcpy(pidfile, filename);
589 fd = safe_open(filename, ISC_FALSE);
591 isc__strerror(errno, strbuf, sizeof(strbuf));
592 (*report)("couldn't open pid file '%s': %s", filename, strbuf);
597 lockfile = fdopen(fd, "w");
598 if (lockfile == NULL) {
599 isc__strerror(errno, strbuf, sizeof(strbuf));
600 (*report)("could not fdopen() pid file '%s': %s",
606 #ifdef HAVE_LINUXTHREADS
611 if (fprintf(lockfile, "%ld\n", (long)pid) < 0) {
612 (*report)("fprintf() to pid file '%s' failed", filename);
613 (void)fclose(lockfile);
617 if (fflush(lockfile) == EOF) {
618 (*report)("fflush() to pid file '%s' failed", filename);
619 (void)fclose(lockfile);
623 (void)fclose(lockfile);
627 ns_os_shutdown(void) {
633 ns_os_gethostname(char *buf, size_t len) {
636 n = gethostname(buf, len);
637 return ((n == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE);
641 next_token(char **stringp, const char *delim) {
645 res = strsep(stringp, delim);
648 } while (*res == '\0');
653 ns_os_shutdownmsg(char *command, isc_buffer_t *text) {
660 /* Skip the command name. */
661 ptr = next_token(&input, " \t");
665 ptr = next_token(&input, " \t");
669 if (strcmp(ptr, "-p") != 0)
672 #ifdef HAVE_LINUXTHREADS
678 n = snprintf((char *)isc_buffer_used(text),
679 isc_buffer_availablelength(text),
680 "pid: %ld", (long)pid);
681 /* Only send a message if it is complete. */
682 if (n < isc_buffer_availablelength(text))
683 isc_buffer_add(text, n);