]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/priv.c
Implement support of keeping readall capabilities after UID/GID switch
[bacula/bacula] / bacula / src / lib / priv.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
5
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
11    in the file LICENSE.
12
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.
17
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
21    02110-1301, USA.
22
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.
27 */
28
29 #include "bacula.h"
30
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
38 # endif
39 #endif
40
41 #ifdef HAVE_AIX_OS
42 extern "C" int initgroups(const char *,int);
43 #endif
44
45 /*
46  * Lower privileges by switching to new UID and GID if non-NULL.
47  * If requested, keep readall capabilities after switch.
48  */
49 void drop(char *uname, char *gname, bool keep_readall_caps)
50 {
51    struct passwd *passw = NULL;
52    struct group *group = NULL;
53    gid_t gid;
54    uid_t uid;
55    char username[1000];
56
57    Dmsg2(900, "uname=%s gname=%s\n", uname?uname:"NONE", gname?gname:"NONE");
58    if (!uname && !gname) {
59       return;                            /* Nothing to do */
60    }
61
62    if (uname) {
63       if ((passw = getpwnam(uname)) == NULL) {
64          berrno be;
65          Emsg2(M_ERROR_TERM, 0, _("Could not find userid=%s: ERR=%s\n"), uname,
66             be.bstrerror());
67       }
68    } else {
69       if ((passw = getpwuid(getuid())) == NULL) {
70          berrno be;
71          Emsg1(M_ERROR_TERM, 0, _("Could not find password entry. ERR=%s\n"),
72             be.bstrerror());
73       } else {
74          uname = passw->pw_name;
75       }
76    }
77    /* Any OS uname pointer may get overwritten, so save name, uid, and gid */
78    bstrncpy(username, uname, sizeof(username));
79    uid = passw->pw_uid;
80    gid = passw->pw_gid;
81    if (gname) {
82       if ((group = getgrnam(gname)) == NULL) {
83          berrno be;
84          Emsg2(M_ERROR_TERM, 0, _("Could not find group=%s: ERR=%s\n"), gname,
85             be.bstrerror());
86       }
87       gid = group->gr_gid;
88    }
89    if (initgroups(username, gid)) {
90       berrno be;
91       if (gname) {
92          Emsg3(M_ERROR_TERM, 0, _("Could not initgroups for group=%s, userid=%s: ERR=%s\n"),
93             gname, username, be.bstrerror());
94       } else {
95          Emsg2(M_ERROR_TERM, 0, _("Could not initgroups for userid=%s: ERR=%s\n"),
96             username, be.bstrerror());
97       }
98    }
99    if (gname) {
100       if (setgid(gid)) {
101          berrno be;
102          Emsg2(M_ERROR_TERM, 0, _("Could not set group=%s: ERR=%s\n"), gname,
103             be.bstrerror());
104       }
105    }
106    if (keep_readall_caps) {
107 #ifdef ENABLE_KEEP_READALL_CAPS_SUPPORT
108       cap_t caps;
109
110       if (prctl(PR_SET_KEEPCAPS, 1)) {
111          berrno be;
112          Emsg1(M_ERROR_TERM, 0, _("prctl failed: ERR=%s\n"), be.bstrerror());
113       }
114       if (setreuid(uid, uid)) {
115          berrno be;
116          Emsg1(M_ERROR_TERM, 0, _("setreuid failed: ERR=%s\n"), be.bstrerror());
117       }
118       if (!(caps = cap_from_text("cap_dac_read_search=ep"))) {
119          berrno be;
120          Emsg1(M_ERROR_TERM, 0, _("cap_from_text failed: ERR=%s\n"), be.bstrerror());
121       }
122       if (cap_set_proc(caps) < 0) {
123          berrno be;
124          Emsg1(M_ERROR_TERM, 0, _("cap_set_proc failed: ERR=%s\n"), be.bstrerror());
125       }
126       cap_free(caps);
127 #else
128       Emsg0(M_ERROR_TERM, 0, _("Keep readall capabilities is not implemented on this platform yet\n"));
129 #endif
130    } else if (setuid(uid)) {
131       berrno be;
132       Emsg1(M_ERROR_TERM, 0, _("Could not set specified userid: %s\n"), username);
133    }
134 }