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