summaryrefslogtreecommitdiff
path: root/src/rua_util.c
blob: 067e299da3490e42e5245453087b3b7817cd12ad (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
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * 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.
 */

#include <linux/limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <grp.h>
#include <pwd.h>

#include <sys/stat.h>
#include <db-util.h>
#include <tzplatform_config.h>

#include "rua_util.h"
#include "rua_internal.h"

char *_rua_util_get_db_path(uid_t uid, char *db_name)
{
	char db_path[PATH_MAX];
	const char *db_path_prefix;

	if (strcmp(db_name, RUA_DB_NAME) == 0) {
		snprintf(db_path, sizeof(db_path), "/run/aul/dbspace/%d/%s",
			uid, db_name);
	} else {
		tzplatform_set_user(uid);
		db_path_prefix = tzplatform_getenv(TZ_USER_DB);
		tzplatform_reset_user();
		snprintf(db_path, sizeof(db_path), "%s/%s", db_path_prefix, db_name);
	}
	LOGD("db path %s", db_path);
	return strdup(db_path);
}

int _rua_util_open_db(sqlite3 **db, int flags, uid_t uid, char *db_name)
{
	int r;
	char *db_path;

	db_path = _rua_util_get_db_path(uid, db_name);
	if (db_path == NULL) {
		LOGE("out of memory _rua_util_get_db_path fail");
		return -1;
	}

	r = db_util_open_with_options(db_path, db, flags, NULL);
	if (r) {
		LOGE("db util open error(%d/%d/%d/%s)", r,
			sqlite3_errcode(*db),
			sqlite3_extended_errcode(*db),
			sqlite3_errmsg(*db));
		free(db_path);
		return -1;
	}

	free(db_path);
	return r;
}

int _rua_util_check_uid(uid_t target_uid)
{
	uid_t uid = getuid();

	if (uid > BASE_UID && uid != target_uid) {
		LOGE("Invalid UID : %d, target UID : %d", uid, target_uid);
		return -1;
	}
	return 0;
}