~ubuntu-branches/ubuntu/precise/libcgroup/precise-proposed

« back to all changes in this revision

Viewing changes to tests/setuid.c

  • Committer: Bazaar Package Importer
  • Author(s): Dustin Kirkland
  • Date: 2009-08-26 11:29:17 UTC
  • Revision ID: james.westby@ubuntu.com-20090826112917-402ews2uj6v350d2
Tags: upstream-0.34
ImportĀ upstreamĀ versionĀ 0.34

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright Red Hat Inc. 2008
 
3
 *
 
4
 * Author:      Steve Olivieri <sjo@redhat.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify it
 
7
 * under the terms of version 2.1 of the GNU Lesser General Public License
 
8
 * as published by the Free Software Foundation.
 
9
 *
 
10
 * This program is distributed in the hope that it would be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
13
 *
 
14
 */
 
15
 
 
16
#include <stdio.h>
 
17
#include <stdlib.h>
 
18
#include <sys/types.h>
 
19
#include <unistd.h>
 
20
#include <pwd.h>
 
21
#include <grp.h>
 
22
#include <errno.h>
 
23
#include <string.h>
 
24
 
 
25
/* 
 
26
 * This is just a simple program for changing a UID or a GID.  Comment out
 
27
 * whichever block you don't want to use.
 
28
 */
 
29
int main(int argc, char *argv[])
 
30
{
 
31
        /* User data */
 
32
        struct passwd *pwd;
 
33
 
 
34
        /* UID of user */
 
35
        uid_t uid;
 
36
 
 
37
        /* Group data */
 
38
        struct group *grp;
 
39
 
 
40
        /* GID of group */
 
41
        gid_t gid;
 
42
 
 
43
        /* Return codes */
 
44
        int ret;
 
45
 
 
46
        if (argc < 2) {
 
47
                printf("Usage: %s <uid_value> \n", argv[0]);
 
48
                goto finished;
 
49
        }
 
50
 
 
51
        pwd = getpwnam(argv[1]);
 
52
        uid = pwd->pw_uid;
 
53
        fprintf(stdout, "Setting UID to %s (%d).\n", pwd->pw_name, uid);
 
54
        if ((ret = setuid(uid))) {
 
55
                fprintf(stderr, "Call to setuid() failed with error: %s\n",
 
56
                                strerror(errno));
 
57
                ret = -errno;
 
58
                goto finished;
 
59
        }
 
60
 
 
61
//      while(1) {
 
62
//              grp = getgrnam("root");
 
63
//              gid = grp->gr_gid;
 
64
//              fprintf(stdout, "Setting GID to %s (%d).\n",
 
65
//                              grp->gr_name, gid);
 
66
//              if ((ret = setgid(gid))) {
 
67
//                      fprintf(stderr, "Call to setgid() failed with error:"
 
68
//                                      " %s\n", strerror(errno));
 
69
//                      ret = -errno;
 
70
//                      goto finished;
 
71
//              }
 
72
//      }
 
73
 
 
74
        while (1) {
 
75
                usleep(3000000);
 
76
        }
 
77
 
 
78
finished:
 
79
        return ret;
 
80
}