PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ug.c

http://bdremote-ng.googlecode.com/
C | 94 lines | 38 code | 17 blank | 39 comment | 8 complexity | aec4c504b42733643cd7b7bdb920f211 MD5 | raw file
 1/*
 2 *  bdremoteng - helper daemon for Sony(R) BD Remote Control
 3 *  Based on bdremoted, written by Anton Starikov <antst@mail.ru>.
 4 *
 5 *  Copyright (C) 2009  Michael Wojciechowski <wojci@wojci.dk>
 6 *
 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 2 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, write to the Free Software
20 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 */
23
24/** \ingroup UID
25 *  @{
26 */
27
28/*! \file ug.c
29  \brief Change UID/GID implemention.
30
31  A function used to resolve user/group into UID/GID and then to
32  change the current UID/GID into the specified ones.
33
34*/
35
36#include "ug.h"
37
38#include <sys/types.h>
39#include <pwd.h>
40#include <grp.h>
41#include <unistd.h>
42
43#include <globaldefs.h>
44
45/* The following function was taken from BTG (btg.berlios.de), also
46 * written by me.
47 */
48
49int changeUIDAndGID(const char* _user,
50                    const char* _group)
51{
52  int result              = BDREMOTE_OK;
53  struct passwd* s_passwd = NULL;
54  uid_t uid               = -1;
55  struct group* s_group   = NULL;
56  gid_t gid               = -1;
57  /* Resolve the user and group into uid/gid. */
58
59  /* User. */
60  s_passwd = getpwnam(_user);
61
62  if (s_passwd == 0)
63    {
64      result = BDREMOTE_FAIL;
65      return result;
66    }
67
68  uid = s_passwd->pw_uid;
69
70  /* Group. */
71  s_group = getgrnam(_group);
72
73  if (s_group == 0)
74    {
75      result = BDREMOTE_FAIL;
76      return result;
77    }
78
79  gid = s_group->gr_gid;
80
81  /* Do the change. */
82
83  if (setgid(gid) != 0)
84    {
85      result = BDREMOTE_FAIL;
86    }
87
88  if (setuid(uid) != 0)
89    {
90      result = BDREMOTE_FAIL;
91    }
92
93  return result;
94}