~lightdm-team/lightdm/1.4

« back to all changes in this revision

Viewing changes to src/privileges.c

  • Committer: Robert Ancell
  • Date: 2014-03-13 02:15:38 UTC
  • Revision ID: robert.ancell@canonical.com-20140313021538-u2mxfxrrfw5u58ic
Tags: 1.4.7
Releasing 1.4.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010-2011 Robert Ancell.
 
3
 * Author: Robert Ancell <robert.ancell@canonical.com>
 
4
 * 
 
5
 * This program is free software: you can redistribute it and/or modify it under
 
6
 * the terms of the GNU General Public License as published by the Free Software
 
7
 * Foundation, either version 3 of the License, or (at your option) any later
 
8
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 
9
 * license.
 
10
 */
 
11
 
 
12
/* for setres*id() */
 
13
#define _GNU_SOURCE
 
14
 
 
15
#include <config.h>
 
16
#include "privileges.h"
 
17
 
 
18
void
 
19
privileges_drop (User *user)
 
20
{
 
21
    g_return_if_fail (user != NULL);
 
22
 
 
23
    g_debug ("Dropping privileges to uid %i", user_get_uid (user));
 
24
#ifdef HAVE_SETRESGID
 
25
    g_debug ("Calling setresgid");
 
26
    g_assert (setresgid (user_get_gid (user), user_get_gid (user), -1) == 0);
 
27
#else
 
28
    g_assert (setgid (user_get_gid (user)) == 0);
 
29
    g_assert (setegid (user_get_gid (user)) == 0);
 
30
#endif
 
31
#ifdef HAVE_SETRESUID
 
32
    g_debug ("Calling setresuid");
 
33
    g_assert (setresuid (user_get_uid (user), user_get_uid (user), -1) == 0);
 
34
#else
 
35
    g_assert (setuid (user_get_uid (user)) == 0);
 
36
    g_assert (seteuid (user_get_uid (user)) == 0);
 
37
#endif
 
38
}
 
39
 
 
40
void
 
41
privileges_reclaim (void)
 
42
{
 
43
    g_debug ("Restoring privileges");
 
44
#ifdef HAVE_SETRESUID
 
45
    g_debug ("Calling setresuid");
 
46
    g_assert (setresuid (0, 0, -1) == 0);
 
47
#else
 
48
    g_assert (setuid (0) == 0);
 
49
    g_assert (seteuid (0) == 0);
 
50
#endif
 
51
#ifdef HAVE_SETRESGID
 
52
    g_debug ("Calling setresgid");
 
53
    g_assert (setresgid (0, 0, -1) == 0);
 
54
#else
 
55
    g_assert (setgid (0) == 0);
 
56
    g_assert (setegid (0) == 0);
 
57
#endif
 
58
}