1 /*
2 BAREOS® - Backup Archiving REcovery Open Sourced
3
4 Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
5 Copyright (C) 2013-2019 Bareos GmbH & Co. KG
6
7 This program is Free Software; you can redistribute it and/or
8 modify it under the terms of version three of the GNU Affero General Public
9 License as published by the Free Software Foundation and included
10 in the file LICENSE.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Affero General Public License for more details.
16
17 You should have received a copy of the GNU Affero General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.
21 */
22
23 #include "include/bareos.h"
24 #include "lib/berrno.h"
25
26 #undef ENABLE_KEEP_READALL_CAPS_SUPPORT
27 #if defined(HAVE_SYS_PRCTL_H) && defined(HAVE_SYS_CAPABILITY_H) \
28 && defined(HAVE_PRCTL) && defined(HAVE_SETREUID) && defined(HAVE_LIBCAP)
29 # include <sys/prctl.h>
30 # include <sys/capability.h>
31 # if defined(PR_SET_KEEPCAPS)
32 # define ENABLE_KEEP_READALL_CAPS_SUPPORT
33 # endif
34 #endif
35
36 #ifdef HAVE_AIX_OS
37 # ifndef _AIX51
38 extern "C" int initgroups(const char*, int);
39 # endif
40 #endif
41
42 /*
43 * Lower privileges by switching to new UID and GID if non-NULL.
44 * If requested, keep readall capabilities after switch.
45 */
drop(char * uname,char * gname,bool keep_readall_caps)46 void drop(char* uname, char* gname, bool keep_readall_caps)
47 {
48 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
49 struct passwd* passw = NULL;
50 struct group* group = NULL;
51 gid_t gid;
52 uid_t uid;
53 char username[1000];
54
55 Dmsg2(900, "uname=%s gname=%s\n", uname ? uname : "NONE",
56 gname ? gname : "NONE");
57 if (!uname && !gname) { return; /* Nothing to do */ }
58
59 if (uname) {
60 if ((passw = getpwnam(uname)) == NULL) {
61 BErrNo be;
62 Emsg2(M_ERROR_TERM, 0, _("Could not find userid=%s: ERR=%s\n"), uname,
63 be.bstrerror());
64 }
65 } else {
66 if ((passw = getpwuid(getuid())) == NULL) {
67 BErrNo be;
68 Emsg1(M_ERROR_TERM, 0, _("Could not find password entry. ERR=%s\n"),
69 be.bstrerror());
70 } else {
71 uname = passw->pw_name;
72 }
73 }
74 /* Any OS uname pointer may get overwritten, so save name, uid, and gid */
75 bstrncpy(username, uname, sizeof(username));
76 uid = passw->pw_uid;
77 gid = passw->pw_gid;
78 if (gname) {
79 if ((group = getgrnam(gname)) == NULL) {
80 BErrNo be;
81 Emsg2(M_ERROR_TERM, 0, _("Could not find group=%s: ERR=%s\n"), gname,
82 be.bstrerror());
83 }
84 gid = group->gr_gid;
85 }
86 if (initgroups(username, gid)) {
87 BErrNo be;
88 if (gname) {
89 Emsg3(M_ERROR_TERM, 0,
90 _("Could not initgroups for group=%s, userid=%s: ERR=%s\n"), gname,
91 username, be.bstrerror());
92 } else {
93 Emsg2(M_ERROR_TERM, 0, _("Could not initgroups for userid=%s: ERR=%s\n"),
94 username, be.bstrerror());
95 }
96 }
97 if (gname) {
98 if (setgid(gid)) {
99 BErrNo be;
100 Emsg2(M_ERROR_TERM, 0, _("Could not set group=%s: ERR=%s\n"), gname,
101 be.bstrerror());
102 }
103 }
104 if (keep_readall_caps) {
105 # ifdef ENABLE_KEEP_READALL_CAPS_SUPPORT
106 cap_t caps;
107
108 if (prctl(PR_SET_KEEPCAPS, 1)) {
109 BErrNo be;
110 Emsg1(M_ERROR_TERM, 0, _("prctl failed: ERR=%s\n"), be.bstrerror());
111 }
112 if (setreuid(uid, uid)) {
113 BErrNo be;
114 Emsg1(M_ERROR_TERM, 0, _("setreuid failed: ERR=%s\n"), be.bstrerror());
115 }
116 if (!(caps = cap_from_text("cap_dac_read_search=ep"))) {
117 BErrNo be;
118 Emsg1(M_ERROR_TERM, 0, _("cap_from_text failed: ERR=%s\n"),
119 be.bstrerror());
120 }
121 if (cap_set_proc(caps) < 0) {
122 BErrNo be;
123 Emsg1(M_ERROR_TERM, 0, _("cap_set_proc failed: ERR=%s\n"),
124 be.bstrerror());
125 }
126 cap_free(caps);
127 # else
128 Emsg0(
129 M_ERROR_TERM, 0,
130 _("Keep readall caps not implemented this OS or missing libraries.\n"));
131 # endif
132 } else if (setuid(uid)) {
133 BErrNo be;
134 Emsg1(M_ERROR_TERM, 0, _("Could not set specified userid: %s\n"), username);
135 }
136 #endif
137 }
138