]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/priv.c
Tweak mutex order for SD
[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 #if   defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
52    struct passwd *passw = NULL;
53    struct group *group = NULL;
54    gid_t gid;
55    uid_t uid;
56    char username[1000];
57
58    Dmsg2(900, "uname=%s gname=%s\n", uname?uname:"NONE", gname?gname:"NONE");
59    if (!uname && !gname) {
60       return;                            /* Nothing to do */
61    }
62
63    if (uname) {
64       if ((passw = getpwnam(uname)) == NULL) {
65          berrno be;
66          Emsg2(M_ERROR_TERM, 0, _("Could not find userid=%s: ERR=%s\n"), uname,
67             be.bstrerror());
68       }
69    } else {
70       if ((passw = getpwuid(getuid())) == NULL) {
71          berrno be;
72          Emsg1(M_ERROR_TERM, 0, _("Could not find password entry. ERR=%s\n"),
73             be.bstrerror());
74       } else {
75          uname = passw->pw_name;
76       }
77    }
78    /* Any OS uname pointer may get overwritten, so save name, uid, and gid */
79    bstrncpy(username, uname, sizeof(username));
80    uid = passw->pw_uid;
81    gid = passw->pw_gid;
82    if (gname) {
83       if ((group = getgrnam(gname)) == NULL) {
84          berrno be;
85          Emsg2(M_ERROR_TERM, 0, _("Could not find group=%s: ERR=%s\n"), gname,
86             be.bstrerror());
87       }
88       gid = group->gr_gid;
89    }
90    if (initgroups(username, gid)) {
91       berrno be;
92       if (gname) {
93          Emsg3(M_ERROR_TERM, 0, _("Could not initgroups for group=%s, userid=%s: ERR=%s\n"),
94             gname, username, be.bstrerror());
95       } else {
96          Emsg2(M_ERROR_TERM, 0, _("Could not initgroups for userid=%s: ERR=%s\n"),
97             username, be.bstrerror());
98       }
99    }
100    if (gname) {
101       if (setgid(gid)) {
102          berrno be;
103          Emsg2(M_ERROR_TERM, 0, _("Could not set group=%s: ERR=%s\n"), gname,
104             be.bstrerror());
105       }
106    }
107    if (keep_readall_caps) {
108 #ifdef ENABLE_KEEP_READALL_CAPS_SUPPORT
109       cap_t caps;
110
111       if (prctl(PR_SET_KEEPCAPS, 1)) {
112          berrno be;
113          Emsg1(M_ERROR_TERM, 0, _("prctl failed: ERR=%s\n"), be.bstrerror());
114       }
115       if (setreuid(uid, uid)) {
116          berrno be;
117          Emsg1(M_ERROR_TERM, 0, _("setreuid failed: ERR=%s\n"), be.bstrerror());
118       }
119       if (!(caps = cap_from_text("cap_dac_read_search=ep"))) {
120          berrno be;
121          Emsg1(M_ERROR_TERM, 0, _("cap_from_text failed: ERR=%s\n"), be.bstrerror());
122       }
123       if (cap_set_proc(caps) < 0) {
124          berrno be;
125          Emsg1(M_ERROR_TERM, 0, _("cap_set_proc failed: ERR=%s\n"), be.bstrerror());
126       }
127       cap_free(caps);
128 #else
129       Emsg0(M_ERROR_TERM, 0, _("Keep readall caps not implemented this OS or missing libraries.\n"));
130 #endif
131    } else if (setuid(uid)) {
132       berrno be;
133       Emsg1(M_ERROR_TERM, 0, _("Could not set specified userid: %s\n"), username);
134    }
135 #endif
136 }