File: compute_user.c

package info (click to toggle)
libselinux 2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,172 kB
  • ctags: 2,529
  • sloc: ansic: 16,149; makefile: 339; sh: 20
file content (110 lines) | stat: -rw-r--r-- 1,843 bytes parent folder | download | duplicates (3)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "selinux_internal.h"
#include "policy.h"
#include <limits.h>

int security_compute_user_raw(const char * scon,
			      const char *user, char *** con)
{
	char path[PATH_MAX];
	char **ary;
	char *buf, *ptr;
	size_t size;
	int fd, ret;
	unsigned int i, nel;

	if (!selinux_mnt) {
		errno = ENOENT;
		return -1;
	}

	snprintf(path, sizeof path, "%s/user", selinux_mnt);
	fd = open(path, O_RDWR);
	if (fd < 0)
		return -1;

	size = selinux_page_size;
	buf = malloc(size);
	if (!buf) {
		ret = -1;
		goto out;
	}
	snprintf(buf, size, "%s %s", scon, user);

	ret = write(fd, buf, strlen(buf));
	if (ret < 0)
		goto out2;

	memset(buf, 0, size);
	ret = read(fd, buf, size - 1);
	if (ret < 0)
		goto out2;

	if (sscanf(buf, "%u", &nel) != 1) {
		ret = -1;
		goto out2;
	}

	ary = malloc((nel + 1) * sizeof(char *));
	if (!ary) {
		ret = -1;
		goto out2;
	}

	ptr = buf + strlen(buf) + 1;
	for (i = 0; i < nel; i++) {
		ary[i] = strdup(ptr);
		if (!ary[i]) {
			freeconary(ary);
			ret = -1;
			goto out2;
		}
		ptr += strlen(ptr) + 1;
	}
	ary[nel] = NULL;
	*con = ary;
	ret = 0;
      out2:
	free(buf);
      out:
	close(fd);
	return ret;
}

hidden_def(security_compute_user_raw)

int security_compute_user(const char * scon,
			  const char *user, char *** con)
{
	int ret;
	char * rscon;

	if (selinux_trans_to_raw_context(scon, &rscon))
		return -1;

	ret = security_compute_user_raw(rscon, user, con);

	freecon(rscon);
	if (!ret) {
		char **ptr, *tmpcon;
		for (ptr = *con; *ptr; ptr++) {
			if (selinux_raw_to_trans_context(*ptr, &tmpcon)) {
				freeconary(*con);
				*con = NULL;
				return -1;
			}
			freecon(*ptr);
			*ptr = tmpcon;
		}
	}

	return ret;
}

hidden_def(security_compute_user)