1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | gain/lose root privileges
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 2004
|
---|
7 |
|
---|
8 | This program is free software; you can redistribute it and/or modify
|
---|
9 | it under the terms of the GNU General Public License as published by
|
---|
10 | the Free Software Foundation; either version 3 of the License, or
|
---|
11 | (at your option) any later version.
|
---|
12 |
|
---|
13 | This program is distributed in the hope that it will be useful,
|
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | GNU 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, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "includes.h"
|
---|
23 | #include "system/passwd.h"
|
---|
24 | #include "../lib/util/unix_privs.h"
|
---|
25 |
|
---|
26 | #if defined(UID_WRAPPER)
|
---|
27 | #if !defined(UID_WRAPPER_REPLACE) && !defined(UID_WRAPPER_NOT_REPLACE)
|
---|
28 | #define UID_WRAPPER_REPLACE
|
---|
29 | #include "../uid_wrapper/uid_wrapper.h"
|
---|
30 | #endif
|
---|
31 | #else
|
---|
32 | #define uwrap_enabled() 0
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @file
|
---|
37 | * @brief Gaining/losing root privileges
|
---|
38 | */
|
---|
39 |
|
---|
40 | /*
|
---|
41 | there are times when smbd needs to temporarily gain root privileges
|
---|
42 | to do some operation. To do this you call root_privileges(), which
|
---|
43 | returns a talloc handle. To restore your previous privileges
|
---|
44 | talloc_free() this pointer.
|
---|
45 |
|
---|
46 | Note that this call is considered successful even if it does not
|
---|
47 | manage to gain root privileges, but it will call smb_abort() if it
|
---|
48 | fails to restore the privileges afterwards. The logic is that
|
---|
49 | failing to gain root access can be caught by whatever operation
|
---|
50 | needs to be run as root failing, but failing to lose the root
|
---|
51 | privileges is dangerous.
|
---|
52 |
|
---|
53 | This also means that this code is safe to be called from completely
|
---|
54 | unprivileged processes.
|
---|
55 | */
|
---|
56 |
|
---|
57 | struct saved_state {
|
---|
58 | uid_t uid;
|
---|
59 | };
|
---|
60 |
|
---|
61 | static int privileges_destructor(struct saved_state *s)
|
---|
62 | {
|
---|
63 | if (geteuid() != s->uid &&
|
---|
64 | seteuid(s->uid) != 0) {
|
---|
65 | smb_panic("Failed to restore privileges");
|
---|
66 | }
|
---|
67 | return 0;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Obtain root privileges for the current process.
|
---|
72 | *
|
---|
73 | * The privileges can be dropped by talloc_free()-ing the
|
---|
74 | * token returned by this function
|
---|
75 | */
|
---|
76 | void *root_privileges(void)
|
---|
77 | {
|
---|
78 | struct saved_state *s;
|
---|
79 | s = talloc(NULL, struct saved_state);
|
---|
80 | if (!s) return NULL;
|
---|
81 | s->uid = geteuid();
|
---|
82 | if (s->uid != 0) {
|
---|
83 | seteuid(0);
|
---|
84 | }
|
---|
85 | talloc_set_destructor(s, privileges_destructor);
|
---|
86 | return s;
|
---|
87 | }
|
---|
88 |
|
---|
89 | uid_t root_privileges_original_uid(void *s)
|
---|
90 | {
|
---|
91 | struct saved_state *saved = talloc_get_type_abort(s, struct saved_state);
|
---|
92 | return saved->uid;
|
---|
93 | }
|
---|