summaryrefslogtreecommitdiff
path: root/src/global-api.c
blob: 4dedf59f80d47e5cd5bdbc1b61d5aac3dc8df022 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright (C) 2013-2014 Intel Corporation.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors:
 *	 José Bollo <jose.bollo@open.eurogiciel.org>
 *	 Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
 *	 Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
 *
 */
#define _GNU_SOURCE

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include "tzplatform_variables.h"
#include "tzplatform_config.h"

#include "shared-api.h"
#include "isadmin.h"
#include "signup.inc"

#define TZ_UID_START	5000
#define TZ_UID_MAX	5000
#define uid2idx(x)	((x > TZ_UID_START) ? (x - TZ_UID_START) : x)
static struct tzplatform_context *_context[TZ_UID_MAX] = {0};

int init_internal_context(uid_t uid)
{
	int ret;
	int idx;
	static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

	if (uid <= TZ_UID_START)
		assert(uid == 0);

	idx = uid2idx(uid);
	assert(idx < TZ_UID_MAX);

	if (_context[idx]) return 0;

	pthread_mutex_lock(&mutex);

	ret = tzplatform_context_create(&_context[idx]);
	if (ret < 0) {
		pthread_mutex_unlock(&mutex);
		return ret;
	}

	ret = tzplatform_context_set_user(_context[idx], uid);
	if (ret < 0) {
		tzplatform_context_destroy(_context[idx]);
		_context[idx] = NULL;

		pthread_mutex_unlock(&mutex);
		return ret;
	}

	pthread_mutex_unlock(&mutex);

	return 0;
}

int tzplatform_getcount()
{
	return _TZPLATFORM_VARIABLES_COUNT_;
}

const char* tzplatform_getname(enum tzplatform_variable id)
{
	return _getname_tzplatform_(id, tizen_platform_config_signup);
}

enum tzplatform_variable tzplatform_getid(const char *name)
{
	return _getid_tzplatform_(name, tizen_platform_config_signup);
}

const char* tzplatform_getenv(enum tzplatform_variable id)
{
	return _getenv_tzplatform_(id, tizen_platform_config_signup);
}

const char* tzplatform_uid_getenv(uid_t uid, enum tzplatform_variable id)
{
	int ret, idx;

	ret = init_internal_context(uid);
	if (ret < 0)
		return NULL;

	idx = uid2idx(uid);
	assert(idx < TZ_UID_MAX);

	return _context_getenv_tzplatform_(id, tizen_platform_config_signup, _context[idx]);
}

const char* tzplatform_context_getenv(struct tzplatform_context *context, enum tzplatform_variable id)
{
	return _context_getenv_tzplatform_(id, tizen_platform_config_signup, context);
}

int tzplatform_getenv_int(enum tzplatform_variable id)
{
	return _getenv_int_tzplatform_(id, tizen_platform_config_signup);
}

int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzplatform_variable id)
{
	return _context_getenv_int_tzplatform_(id, tizen_platform_config_signup, context);
}

const char* tzplatform_mkstr(enum tzplatform_variable id, const char *str)
{
	return _mkstr_tzplatform_(id, str, tizen_platform_config_signup);
}

const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tzplatform_variable id, const char *str)
{
	return _context_mkstr_tzplatform_(id, str, tizen_platform_config_signup, context);
}

const char* tzplatform_mkpath(enum tzplatform_variable id, const char *path)
{
	return _mkpath_tzplatform_(id, path, tizen_platform_config_signup);
}

const char* tzplatform_uid_mkpath(uid_t uid, enum tzplatform_variable id, const char *path)
{
	int ret, idx;

	ret = init_internal_context(uid);
	if (ret < 0)
		return NULL;

	idx = uid2idx(uid);
	assert(idx < TZ_UID_MAX);

	return _context_mkpath_tzplatform_(id, path, tizen_platform_config_signup, _context[idx]);
}

const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum tzplatform_variable id, const char *path)
{
	return _context_mkpath_tzplatform_(id, path, tizen_platform_config_signup, context);
}

const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path, const char* path2)
{
	return _mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup);
}

const char* tzplatform_uid_mkpath3(uid_t uid, enum tzplatform_variable id, const char *path, const char *path2)
{
	int ret, idx;

	ret = init_internal_context(uid);
	if (ret < 0)
		return NULL;

	idx = uid2idx(uid);
	assert(idx < TZ_UID_MAX);

	return _context_mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup, _context[idx]);
}
const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2)
{
	return _context_mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup, context);
}

const char* tzplatform_mkpath4(enum tzplatform_variable id, const char * path, const char* path2, const char *path3)
{
	return _mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup);
}

const char* tzplatform_uid_mkpath4(uid_t uid, enum tzplatform_variable id, const char *path, const char *path2, const char *path3)
{
	int ret, idx;

	ret = init_internal_context(uid);
	if (ret < 0)
		return NULL;

	idx = uid2idx(uid);
	assert(idx < TZ_UID_MAX);

	return _context_mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup, _context[idx]);
}

const char* tzplatform_context_mkpath4(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2, const char *path3)
{
	return _context_mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup, context);
}

uid_t tzplatform_getuid(enum tzplatform_variable id)
{
	return _getuid_tzplatform_(id, tizen_platform_config_signup);
}

uid_t tzplatform_context_getuid(struct tzplatform_context *context, enum tzplatform_variable id)
{
	return _context_getuid_tzplatform_(id, tizen_platform_config_signup, context);
}

gid_t tzplatform_getgid(enum tzplatform_variable id)
{
	return _getgid_tzplatform_(id, tizen_platform_config_signup);
}

gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatform_variable id)
{
	return _context_getgid_tzplatform_(id, tizen_platform_config_signup, context);
}

int tzplatform_has_system_group(uid_t uid)
{
	return _has_system_group_static_(uid);
}

#ifdef TEST
#include <stdlib.h>
#include <stdio.h>

int main()
{
	int i;
	struct tzplatform_context *context;
	enum tzplatform_variable id;
	const char *name;
	const char *value;
	int xid;
	uid_t uid;

	i = 0;
	while (i != tzplatform_getcount()) {
		id = (enum tzplatform_variable)i;
		name = tzplatform_getname(id);
		value = tzplatform_getenv(id);
		xid = (int)tzplatform_getid(name);
		printf("%d=%d\t%s=%s\n", i, xid, name, value ? value : "<null>");
		i++;
	}

	printf("------------------------\n");
	i = tzplatform_context_create(&context);
	if (i) {
		printf("error while creating context %d\n", i);
		return 1;
	}

	uid = (uid_t)0;
	i = tzplatform_context_set_user(context, uid);
	if (i) {
		printf("error %d while switching to user %d\n", i, (int)uid);
		return 1;
	}
	i = 0;
	while (i != tzplatform_getcount()) {
		id = (enum tzplatform_variable)i;
		name = tzplatform_getname(id);
		value = tzplatform_context_getenv(context, id);
		xid = (int)tzplatform_getid(name);
		printf("%d=%d\t%s=%s\n", i, xid, name, value ? value : "<null>");
		i++;
	}
	tzplatform_context_destroy(context);

	return 0;
}
#endif