2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
31 #undef ENABLE_KEEP_READALL_CAPS_SUPPORT
32 #if defined(HAVE_SYS_PRCTL_H) && defined(HAVE_SYS_CAPABILITY_H) && \
33 defined(HAVE_PRCTL) && defined(HAVE_SETREUID) && defined(HAVE_LIBCAP)
34 # include <sys/prctl.h>
35 # include <sys/capability.h>
36 # if defined(PR_SET_KEEPCAPS)
37 # define ENABLE_KEEP_READALL_CAPS_SUPPORT
42 extern "C" int initgroups(const char *,int);
46 * Lower privileges by switching to new UID and GID if non-NULL.
47 * If requested, keep readall capabilities after switch.
49 void drop(char *uname, char *gname, bool keep_readall_caps)
51 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
52 struct passwd *passw = NULL;
53 struct group *group = NULL;
58 Dmsg2(900, "uname=%s gname=%s\n", uname?uname:"NONE", gname?gname:"NONE");
59 if (!uname && !gname) {
60 return; /* Nothing to do */
64 if ((passw = getpwnam(uname)) == NULL) {
66 Emsg2(M_ERROR_TERM, 0, _("Could not find userid=%s: ERR=%s\n"), uname,
70 if ((passw = getpwuid(getuid())) == NULL) {
72 Emsg1(M_ERROR_TERM, 0, _("Could not find password entry. ERR=%s\n"),
75 uname = passw->pw_name;
78 /* Any OS uname pointer may get overwritten, so save name, uid, and gid */
79 bstrncpy(username, uname, sizeof(username));
83 if ((group = getgrnam(gname)) == NULL) {
85 Emsg2(M_ERROR_TERM, 0, _("Could not find group=%s: ERR=%s\n"), gname,
90 if (initgroups(username, gid)) {
93 Emsg3(M_ERROR_TERM, 0, _("Could not initgroups for group=%s, userid=%s: ERR=%s\n"),
94 gname, username, be.bstrerror());
96 Emsg2(M_ERROR_TERM, 0, _("Could not initgroups for userid=%s: ERR=%s\n"),
97 username, be.bstrerror());
103 Emsg2(M_ERROR_TERM, 0, _("Could not set group=%s: ERR=%s\n"), gname,
107 if (keep_readall_caps) {
108 #ifdef ENABLE_KEEP_READALL_CAPS_SUPPORT
111 if (prctl(PR_SET_KEEPCAPS, 1)) {
113 Emsg1(M_ERROR_TERM, 0, _("prctl failed: ERR=%s\n"), be.bstrerror());
115 if (setreuid(uid, uid)) {
117 Emsg1(M_ERROR_TERM, 0, _("setreuid failed: ERR=%s\n"), be.bstrerror());
119 if (!(caps = cap_from_text("cap_dac_read_search=ep"))) {
121 Emsg1(M_ERROR_TERM, 0, _("cap_from_text failed: ERR=%s\n"), be.bstrerror());
123 if (cap_set_proc(caps) < 0) {
125 Emsg1(M_ERROR_TERM, 0, _("cap_set_proc failed: ERR=%s\n"), be.bstrerror());
129 Emsg0(M_ERROR_TERM, 0, _("Keep readall caps not implemented this OS or missing libraries.\n"));
131 } else if (setuid(uid)) {
133 Emsg1(M_ERROR_TERM, 0, _("Could not set specified userid: %s\n"), username);