2 Unix SMB/CIFS implementation.
4 gain/lose root privileges
6 Copyright (C) Andrew Tridgell 2004
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 2 of the License, or
11 (at your option) any later version.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/filesys.h"
25 #include "system/passwd.h"
28 there are times when smbd needs to temporarily gain root privileges
29 to do some operation. To do this you call root_privileges(), which
30 returns a talloc handle. To restore your previous privileges
31 talloc_free() this pointer.
33 Note that this call is considered successful even if it does not
34 manage to gain root privileges, but it will call smb_abort() if it
35 fails to restore the privileges afterwards. The logic is that
36 failing to gain root access can be caught by whatever operation
37 needs to be run as root failing, but failing to lose the root
38 privileges is dangerous.
40 This also means that this code is safe to be called from completely
41 unprivileged processes.
48 static int privileges_destructor(void *ptr)
50 struct saved_state *s = ptr;
51 if (geteuid() != s->uid &&
52 seteuid(s->uid) != 0) {
53 smb_panic("Failed to restore privileges");
58 void *root_privileges(void)
60 struct saved_state *s;
61 s = talloc(NULL, struct saved_state);
67 talloc_set_destructor(s, privileges_destructor);