summaryrefslogtreecommitdiff
path: root/email-core/email-core-container.c
blob: da2102857efcaff9789c6c4b736d5915eb33d7d6 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
*  email-service
*
* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

/**
 *
 * This file contains functionality related to KNOX
 * to interact with email-service.
 * @file		email-core-container.c
 * @author	
 * @version	0.1
 * @brief 		This file contains functionality to provide KNOX support in email-service. 
 */

#include <vasum.h>

#include "email-utilities.h"
#include "email-core-container.h"
#include "email-debug-log.h"

int vsm_state_handle = 0;
int vsm_event_handle = 0;
vsm_context_h container;

enum {
	CONTAINER_DISABLE = 0,
	CONTAINER_ENABLE = 1
};

static int container_status = CONTAINER_DISABLE;

static int container_state_listener(vsm_zone_h zone, vsm_zone_state_t state, void *user_data)
{
    EM_DEBUG_FUNC_BEGIN();
    EM_DEBUG_LOG("Container name : [%s]", vsm_get_zone_name(zone));
    EM_DEBUG_LOG("Container state : [%d]", state);
    EM_DEBUG_FUNC_END();
    return 0;
}

static int container_event_listener(vsm_zone_h zone, vsm_zone_event_t event, void *user_data)
{
    EM_DEBUG_FUNC_BEGIN();
    EM_DEBUG_LOG("Container name : [%s]", vsm_get_zone_name(zone));
    EM_DEBUG_LOG("Container event : [%d]", event);
    EM_DEBUG_FUNC_END();
    return 0;
}

INTERNAL_FUNC void emcore_create_container()
{
    EM_DEBUG_FUNC_BEGIN();

    container = NULL;

    container = vsm_create_context();
    if (container == NULL) {
        EM_DEBUG_EXCEPTION("vsm_create_context failed");
        return;
    }

    vsm_state_handle = vsm_add_state_changed_callback(container, container_state_listener, NULL);
	vsm_event_handle = vsm_add_event_callback(container, container_event_listener, NULL);

    EM_DEBUG_FUNC_END();
}

INTERNAL_FUNC void emcore_destroy_container()
{
    EM_DEBUG_FUNC_BEGIN();

    if (container) {
        vsm_del_state_changed_callback(container, vsm_state_handle);

		vsm_del_event_callback(container, vsm_event_handle);

        vsm_cleanup_context(container);

        container = NULL;
    }

    EM_DEBUG_FUNC_END();
}

static gboolean mainloop_callback(GIOChannel *channel, GIOCondition condition, void *data) 
{
    EM_DEBUG_FUNC_BEGIN();

    vsm_context_h ctx = (vsm_context_h)data;

    EM_DEBUG_LOG("Enter event loop");
    vsm_enter_eventloop(ctx, 0, 0);
    EM_DEBUG_LOG("Finish event loop");

    EM_DEBUG_FUNC_END();
    return TRUE;
}

INTERNAL_FUNC void emcore_bind_vsm_context()
{
	EM_DEBUG_FUNC_BEGIN();

	int fd;

	GIOChannel *domain_channel = NULL;

	fd = vsm_get_poll_fd(container);
	if (fd < 0) {
		EM_DEBUG_EXCEPTION("vsm_get_poll_fd failed");
		return;
	}

	domain_channel = g_io_channel_unix_new(fd);
	if (domain_channel == NULL) {
		EM_DEBUG_EXCEPTION("Channel allocation failed");
		return;
	}

	g_io_add_watch(domain_channel, G_IO_IN, mainloop_callback, container);

	EM_DEBUG_FUNC_END();
}

INTERNAL_FUNC int emcore_get_container_path(char *multi_user, char **container_path)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;
	
	if (multi_user == NULL) {
		EM_DEBUG_LOG("user_name is NULL");
		return EMAIL_ERROR_INVALID_PARAM;
	}

	if (container_status == CONTAINER_DISABLE) {
		EM_DEBUG_LOG("Single container");
		return err;
	}

	if (container == NULL) {
		EM_DEBUG_LOG("Container not intialize");
		return EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION;
	}

	vsm_zone_h zone = NULL;
	char *p_container_path = NULL;
	char errno_buf[ERRNO_BUF_SIZE] = {0};

	if (EM_SAFE_STRLEN(multi_user) == 0) {
		EM_DEBUG_LOG("multi_user name is NULL");
		goto FINISH_OFF;
	}

	EM_DEBUG_LOG("name : [%s], container : [%p]", multi_user, container);
	zone = vsm_lookup_zone_by_name(container, multi_user);
	if (zone == NULL) {
		EM_DEBUG_EXCEPTION("NULL returned : %s, %d", EM_STRERROR(errno_buf), errno);
		err = EMAIL_ERROR_CONTAINER_GET_DOMAIN;
		goto FINISH_OFF;
	}

	p_container_path = (char *)vsm_get_zone_rootpath(zone);

FINISH_OFF:

	if (container_path)
		*container_path = EM_SAFE_STRDUP(p_container_path);

	EM_DEBUG_FUNC_END();
	return err;
}

INTERNAL_FUNC int emcore_get_user_name(int pid, char **multi_user)
{
	EM_DEBUG_FUNC_BEGIN();

	int err = EMAIL_ERROR_NONE;

	if (pid <= 0) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		return EMAIL_ERROR_INVALID_PARAM;
	}

	if (container_status == CONTAINER_DISABLE) {
		EM_DEBUG_LOG("Single container");
		return err;
	}

	if (container == NULL) {
		EM_DEBUG_LOG("Container not intialize");
		return EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION;
	}

	char *user_name = NULL;
	vsm_zone_h zone = NULL;
	char errno_buf[ERRNO_BUF_SIZE] = {0};

	EM_DEBUG_LOG("pid : [%d], container : [%p]", pid, container);
	zone = vsm_lookup_zone_by_pid(container, pid);
	if (zone == NULL) {
		EM_DEBUG_EXCEPTION("NULL returned : %s, %d", EM_STRERROR(errno_buf), errno);
		err = EMAIL_ERROR_CONTAINER_GET_DOMAIN;
		goto FINISH_OFF;
	}

	user_name = (char *)vsm_get_zone_name(zone);
	EM_DEBUG_LOG("user_name : [%s]", user_name);

FINISH_OFF:

	if (multi_user)
		*multi_user = EM_SAFE_STRDUP(user_name);

	EM_DEBUG_FUNC_END();
	return err;
}

INTERNAL_FUNC int emcore_lookup_zone_by_name(char *user_name)
{
	EM_DEBUG_FUNC_BEGIN();
	int ret = false;

	if (EM_SAFE_STRLEN(user_name) == 0) {
		EM_DEBUG_LOG("user_name is NULL");
		return ret;
	}

	if (container_status == CONTAINER_DISABLE) {
		EM_DEBUG_LOG("Single container");
		return true;
	}

	if (container == NULL) {
		EM_DEBUG_LOG("Container not intialize");
		return ret;
	}

	vsm_zone_h zone = NULL;
	char errno_buf[ERRNO_BUF_SIZE] = {0};

	EM_DEBUG_LOG("name : [%s], container : [%p]", user_name, container);
	zone = vsm_lookup_zone_by_name(container, user_name);
	if (zone == NULL) {
		EM_DEBUG_EXCEPTION("NULL returned : %s, %d", EM_STRERROR(errno_buf), errno);
		goto FINISH_OFF;
	}

	ret = true;

FINISH_OFF:

	EM_DEBUG_FUNC_END();
	return ret;
}

INTERNAL_FUNC void emcore_set_declare_link(const char *file_path)
{
    EM_DEBUG_FUNC_BEGIN();

	if (container_status == CONTAINER_DISABLE) {
		EM_DEBUG_LOG("Single container");
		return;
	}

    if (container == NULL) {
        EM_DEBUG_EXCEPTION("Not initialize");
        return;
    }

    int ret = 0;
    if ((ret = vsm_declare_link(container, file_path /* src path */, file_path /* dst path */)) != 0) {
        EM_DEBUG_EXCEPTION("vsm_declare_link failed : [%s] [%d]", file_path, ret);
    }

    EM_DEBUG_FUNC_END();
}

void iterate_callback(vsm_zone_h zone, void *user_data)
{
    EM_DEBUG_FUNC_BEGIN();
    char *zone_name = NULL;
    GList *zone_name_list = (GList *)user_data;

    zone_name = EM_SAFE_STRDUP(vsm_get_zone_name(zone));
	if (EM_SAFE_STRLEN(zone_name) > 0)
	    zone_name_list = g_list_append(zone_name_list, zone_name);
    
    EM_DEBUG_FUNC_END();
}

INTERNAL_FUNC int emcore_get_zone_name_list(GList **output_name_list)
{
    EM_DEBUG_FUNC_BEGIN();
    int err = EMAIL_ERROR_NONE;
    int ret = 0;
    GList *zone_name_list = NULL;
    GList *node = NULL;

    if (output_name_list == NULL) {
        EM_DEBUG_EXCEPTION("Invalid parameter");
        err = EMAIL_ERROR_INVALID_PARAM;
        goto FINISH_OFF;
    }

	if (container_status == CONTAINER_DISABLE) {
		EM_DEBUG_LOG("Single container");
		goto FINISH_OFF;
	}

    if (container == NULL) {
        EM_DEBUG_EXCEPTION("Not initialize");
        err = EMAIL_ERROR_CONTAINER_CREATE_FAILED;
        goto FINISH_OFF;
    }

    zone_name_list = g_list_alloc();
    if (zone_name_list == NULL) {
        EM_DEBUG_EXCEPTION("g_list_alloc failed");
        err = EMAIL_ERROR_OUT_OF_MEMORY;
        goto FINISH_OFF;
    }

    if ((ret = vsm_iterate_zone(container, iterate_callback, (void *)zone_name_list)) < 0) {
        EM_DEBUG_EXCEPTION("vsm_iterate_domain failed : [%d]", ret);
        err = EMAIL_ERROR_CONTAINER_ITERATE_DOMAIN;
        goto FINISH_OFF;
    }

FINISH_OFF:

    if (err != EMAIL_ERROR_NONE) {
        node = g_list_first(zone_name_list);
        while (node != NULL) {
            /* Free the domain name */
            EM_SAFE_FREE(node->data);                   
            node = g_list_next(node);
        }
        g_list_free(zone_name_list);
        zone_name_list = NULL;
    }

    if (output_name_list)
        *output_name_list = zone_name_list;

    EM_DEBUG_FUNC_END();
	return err;
}

INTERNAL_FUNC int emcore_get_canonicalize_path(char *db_path, char **output_path)
{
    EM_DEBUG_FUNC_BEGIN();
    int err = EMAIL_ERROR_NONE;

	int ret = 0;
	ret = vsm_canonicalize_path(db_path, output_path);
	if (ret < 0) {
		EM_DEBUG_EXCEPTION("vsm_canonicalize_path failed : [%d]", ret);
		err = EMAIL_ERROR_FILE_NOT_FOUND;
	}

    EM_DEBUG_FUNC_END();
    return err;
}

INTERNAL_FUNC int emcore_set_join_zone(char *multi_user_name, void **join_zone)
{
    EM_DEBUG_FUNC_BEGIN();
    int err = EMAIL_ERROR_NONE;
	char errno_buf[ERRNO_BUF_SIZE] = {0};
	vsm_zone_h temp_zone = NULL;
	vsm_zone_h p_join_zone = NULL;

	if (container_status == CONTAINER_DISABLE) {
		EM_DEBUG_LOG("Single container");
		goto FINISH_OFF;
	}

    if (container == NULL) {
        EM_DEBUG_EXCEPTION("Not initialize");
        err = EMAIL_ERROR_CONTAINER_CREATE_FAILED;
        goto FINISH_OFF;
    }

	if (EM_SAFE_STRLEN(multi_user_name) == 0) {
		EM_DEBUG_LOG("multi_user_name is NULL");
		return err;
	}

	temp_zone = vsm_lookup_zone_by_name(container, multi_user_name);
	if (temp_zone == NULL) {
		EM_DEBUG_EXCEPTION("NULL returned : %s %d", EM_STRERROR(errno_buf), errno);
		err = EMAIL_ERROR_CONTAINER_LOOKUP_ZONE_FAILED;
		goto FINISH_OFF;
	}

	p_join_zone = vsm_join_zone(temp_zone);
	if (p_join_zone == NULL) {
		EM_DEBUG_EXCEPTION("NULL returned : %s %d", EM_STRERROR(errno_buf), errno);
		err = EMAIL_ERROR_CONTAINER_JOIN_ZONE_FAILED;
		goto FINISH_OFF;
	}

FINISH_OFF:

	if (join_zone)
		*join_zone = (void *)p_join_zone;

    EM_DEBUG_FUNC_END();
    return err;
}

INTERNAL_FUNC void emcore_unset_join_zone(void *join_zone)
{
    EM_DEBUG_FUNC_BEGIN();

	char errno_buf[ERRNO_BUF_SIZE] = {0};
	vsm_zone_h temp_zone = NULL;

	temp_zone = vsm_join_zone((vsm_zone_h)join_zone);
	if (temp_zone == NULL) {
		EM_DEBUG_EXCEPTION("NULL returned : %s %d", EM_STRERROR(errno_buf), errno);
	}

    EM_DEBUG_FUNC_END();
}