Back to index

lightdm  1.3.2
privileges.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010-2011 Robert Ancell.
00003  * Author: Robert Ancell <robert.ancell@canonical.com>
00004  * 
00005  * This program is free software: you can redistribute it and/or modify it under
00006  * the terms of the GNU General Public License as published by the Free Software
00007  * Foundation, either version 3 of the License, or (at your option) any later
00008  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
00009  * license.
00010  */
00011 
00012 /* for setres*id() */
00013 #define _GNU_SOURCE
00014 
00015 #include <config.h>
00016 #include "privileges.h"
00017 
00018 void
00019 privileges_drop (User *user)
00020 {
00021     g_return_if_fail (user != NULL);
00022 
00023     g_debug ("Dropping privileges to uid %i", user_get_uid (user));
00024 #ifdef HAVE_SETRESGID
00025     g_debug ("Calling setresgid");
00026     g_assert (setresgid (user_get_gid (user), user_get_gid (user), -1) == 0);
00027 #else
00028     g_assert (setgid (user_get_gid (user)) == 0);
00029     g_assert (setegid (user_get_gid (user)) == 0);
00030 #endif
00031 #ifdef HAVE_SETRESUID
00032     g_debug ("Calling setresuid");
00033     g_assert (setresuid (user_get_uid (user), user_get_uid (user), -1) == 0);
00034 #else
00035     g_assert (setuid (user_get_uid (user)) == 0);
00036     g_assert (seteuid (user_get_uid (user)) == 0);
00037 #endif
00038 }
00039 
00040 void
00041 privileges_reclaim (void)
00042 {
00043     g_debug ("Restoring privileges");
00044 #ifdef HAVE_SETRESUID
00045     g_debug ("Calling setresuid");
00046     g_assert (setresuid (0, 0, -1) == 0);
00047 #else
00048     g_assert (setuid (0) == 0);
00049     g_assert (seteuid (0) == 0);
00050 #endif
00051 #ifdef HAVE_SETRESGID
00052     g_debug ("Calling setresgid");
00053     g_assert (setresgid (0, 0, -1) == 0);
00054 #else
00055     g_assert (setgid (0) == 0);
00056     g_assert (setegid (0) == 0);
00057 #endif
00058 }