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
43 extern "C" int initgroups(const char *,int);
48 * Lower privileges by switching to new UID and GID if non-NULL.
49 * If requested, keep readall capabilities after switch.
51 void drop(char *uname, char *gname, bool keep_readall_caps)
53 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
54 struct passwd *passw = NULL;
55 struct group *group = NULL;
60 Dmsg2(900, "uname=%s gname=%s\n", uname?uname:"NONE", gname?gname:"NONE");
61 if (!uname && !gname) {
62 return; /* Nothing to do */
66 if ((passw = getpwnam(uname)) == NULL) {
68 Emsg2(M_ERROR_TERM, 0, _("Could not find userid=%s: ERR=%s\n"), uname,
72 if ((passw = getpwuid(getuid())) == NULL) {
74 Emsg1(M_ERROR_TERM, 0, _("Could not find password entry. ERR=%s\n"),
77 uname = passw->pw_name;
80 /* Any OS uname pointer may get overwritten, so save name, uid, and gid */
81 bstrncpy(username, uname, sizeof(username));
85 if ((group = getgrnam(gname)) == NULL) {
87 Emsg2(M_ERROR_TERM, 0, _("Could not find group=%s: ERR=%s\n"), gname,
92 if (initgroups(username, gid)) {
95 Emsg3(M_ERROR_TERM, 0, _("Could not initgroups for group=%s, userid=%s: ERR=%s\n"),
96 gname, username, be.bstrerror());
98 Emsg2(M_ERROR_TERM, 0, _("Could not initgroups for userid=%s: ERR=%s\n"),
99 username, be.bstrerror());
105 Emsg2(M_ERROR_TERM, 0, _("Could not set group=%s: ERR=%s\n"), gname,
109 if (keep_readall_caps) {
110 #ifdef ENABLE_KEEP_READALL_CAPS_SUPPORT
113 if (prctl(PR_SET_KEEPCAPS, 1)) {
115 Emsg1(M_ERROR_TERM, 0, _("prctl failed: ERR=%s\n"), be.bstrerror());
117 if (setreuid(uid, uid)) {
119 Emsg1(M_ERROR_TERM, 0, _("setreuid failed: ERR=%s\n"), be.bstrerror());
121 if (!(caps = cap_from_text("cap_dac_read_search=ep"))) {
123 Emsg1(M_ERROR_TERM, 0, _("cap_from_text failed: ERR=%s\n"), be.bstrerror());
125 if (cap_set_proc(caps) < 0) {
127 Emsg1(M_ERROR_TERM, 0, _("cap_set_proc failed: ERR=%s\n"), be.bstrerror());
131 Emsg0(M_ERROR_TERM, 0, _("Keep readall caps not implemented this OS or missing libraries.\n"));
133 } else if (setuid(uid)) {
135 Emsg1(M_ERROR_TERM, 0, _("Could not set specified userid: %s\n"), username);