Remove setuid bit
[platform/core/appfw/ail.git] / tool / src / createdb.c
1 /*
2  * ail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  * Copyright (C) 2013-2014 Intel Corporation.
6  *
7  * Contact: Sabera Djelti <sabera.djelti@open.eurogiciel.org>,
8  * Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #define _GNU_SOURCE
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <dirent.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <errno.h>
34 #include <sys/smack.h>
35
36 #include "ail.h"
37 #include "ail_private.h"
38 #include "ail_db.h"
39
40
41 #ifdef _E
42 #undef _E
43 #endif
44 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
45
46 #ifdef _D
47 #undef _D
48 #endif
49 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
50
51 #define SET_DEFAULT_LABEL(x) \
52         do { \
53                 if (smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) \
54                         _E("failed chsmack -a \"*\" %s", x); \
55                 else \
56                         _D("chsmack -a \"*\" %s", x); \
57         } while (0)
58
59 static int createdb_change_perm(const char *db_file)
60 {
61         char journal_file[BUFSZE];
62         char *files[3];
63         int ret, i;
64
65         files[0] = (char *)db_file;
66         files[1] = journal_file;
67         files[2] = NULL;
68
69         retv_if(!db_file, AIL_ERROR_FAIL);
70
71         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
72
73         for (i = 0; files[i]; i++) {
74                 ret = chown(files[i], GLOBAL_USER, OWNER_ROOT);
75                 if (ret == -1) {
76                         _E("FAIL : chown %s %d.%d, because %d", db_file, OWNER_ROOT, OWNER_ROOT, errno);
77                         return AIL_ERROR_FAIL;
78                 }
79
80                 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
81                 if (ret == -1) {
82                         _E("FAIL : chmod %s 0664, because %d", db_file, errno);
83                         return AIL_ERROR_FAIL;
84                 }
85         }
86
87         return AIL_ERROR_OK;
88 }
89
90
91 static int __is_authorized(void)
92 {
93         /* ail_init db should be called by as root privilege. */
94         uid_t uid = getuid();
95         /* euid need to be root to allow smack label changes during initialization */
96         /* uid_t euid = geteuid(); */
97         if ((uid_t) OWNER_ROOT == uid)
98                 return 1;
99         else
100                 return 0;
101 }
102
103 int xsystem(const char *argv[])
104 {
105         int status = 0;
106         pid_t pid;
107         pid = fork();
108         switch (pid) {
109         case -1:
110                 perror("fork failed");
111                 return -1;
112         case 0:
113                 /* child */
114                 execvp(argv[0], (char *const *)argv);
115                 _exit(-1);
116         default:
117                 /* parent */
118                 break;
119         }
120         if (waitpid(pid, &status, 0) == -1) {
121                 perror("waitpid failed");
122                 return -1;
123         }
124         if (WIFSIGNALED(status)) {
125                 perror("signal");
126                 return -1;
127         }
128         if (!WIFEXITED(status)) {
129                 /* shouldn't happen */
130                 perror("should not happen");
131                 return -1;
132         }
133         return WEXITSTATUS(status);
134 }
135
136 int main(int argc, char *argv[])
137 {
138         int ret;
139
140         if (!__is_authorized()) {
141                 fprintf(stderr, "You are not an authorized user!\n");
142                 _D("You are not root user!\n");
143         } else {
144                 if (remove(APP_INFO_DB_FILE))
145                         _E(" %s is not removed", APP_INFO_DB_FILE);
146                 if (remove(APP_INFO_DB_FILE_JOURNAL))
147                         _E(" %s is not removed", APP_INFO_DB_FILE_JOURNAL);
148         }
149
150         ret = setenv("AIL_INITDB", "1", 1);
151         _D("AIL_INITDB : %d", ret);
152
153         if (setresuid(GLOBAL_USER, GLOBAL_USER, OWNER_ROOT) != 0)
154                 _E("setresuid() is failed");
155
156         if (db_open(DB_OPEN_RW, GLOBAL_USER) != AIL_ERROR_OK) {
157                 _E("Fail to create system databases");
158                 return AIL_ERROR_DB_FAILED;
159         }
160
161         if (setuid(OWNER_ROOT) != 0)
162                 _E("setuid() is failed.");
163
164         ret = createdb_change_perm(APP_INFO_DB_FILE);
165         if (ret == AIL_ERROR_FAIL)
166                 _E("cannot chown.");
167
168         SET_DEFAULT_LABEL(APP_INFO_DB_FILE);
169         SET_DEFAULT_LABEL(APP_INFO_DB_FILE_JOURNAL);
170
171         return AIL_ERROR_OK;
172 }