File: setuid.c

package info (click to toggle)
libcgroup 3.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,764 kB
  • sloc: ansic: 14,997; cpp: 9,957; python: 8,340; sh: 5,194; yacc: 470; makefile: 400; lex: 38
file content (74 lines) | stat: -rw-r--r-- 1,394 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: LGPL-2.1-only
/*
 * Copyright Red Hat Inc. 2008
 *
 * Author:      Steve Olivieri <sjo@redhat.com>
 */

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>

#include <sys/types.h>

/*
 * This is just a simple program for changing a UID or a GID.  Comment out
 * whichever block you don't want to use.
 */
int main(int argc, char *argv[])
{
	/* User data */
	struct passwd *pwd;

	/* UID of user */
	uid_t uid;

	/* Return codes */
	int ret = 0;

	if (argc < 2) {
		printf("Usage: %s <uid_value>\n", argv[0]);
		goto finished;
	}

	pwd = getpwnam(argv[1]);
	if (!pwd) {
		fprintf(stderr, "getpwnam() failed: %s\n",
			strerror(errno));
		ret = -errno;
		goto finished;
	}

	uid = pwd->pw_uid;
	fprintf(stdout, "Setting UID to %s (%d).\n", pwd->pw_name, uid);
	ret = setuid(uid);
	if (ret != 0) {
		fprintf(stderr, "Call to setuid() failed with error: %s\n",
			strerror(errno));
		ret = -errno;
		goto finished;
	}

//	while(1) {
//		grp = getgrnam("root");
//		gid = grp->gr_gid;
//		fprintf(stdout, "Setting GID to %s (%d).\n",
//				grp->gr_name, gid);
//		if ((ret = setgid(gid))) {
//			fprintf(stderr, "Call to setgid() failed with error:"
//					" %s\n", strerror(errno));
//			ret = -errno;
//			goto finished;
//		}
//	}

	while (1)
		usleep(3000000);

finished:
	return ret;
}