revert it due to dlopen error
[sdk/target/sdbd.git] / src / sdktools.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <grp.h>
7 #include <regex.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/smack.h>
11 #include "sysdeps.h"
12 #include "sdktools.h"
13
14 //#define  TRACE_TAG  TRACE_SERVICES
15 #define LOG_TAG "SDBD_TRACE_SEVICES"
16 #include "log.h"
17
18 #include "sdb.h"
19 #include "sdktools.h"
20 #include "strutils.h"
21 #include "utils.h"
22
23 struct sudo_command root_commands[] = {
24     /* 0 */
25     { "profile", "/usr/bin/profile_command",
26         { "killmanager",
27         "runmanager",
28         "findunittest",
29         "process",
30         "getversion",
31         "killvalgrind",
32         "getprobemap",
33         NULL
34         }
35     },
36     /* end */
37     { NULL, NULL, {NULL} }
38 };
39
40 struct command_suffix
41 {
42     const char *name; // comments for human
43     const char *suffix; //pattern
44 };
45
46 static struct command_suffix CMD_SUFFIX_DENY_KEYWORD[] = {
47         /* 0 */   {"pipe", "|"},
48         /* 1 */   {"redirect", ">"},
49         /* 2 */   {"semicolon", ";"}, // separated list is executed
50         /* 3 */   {"and", "&"},
51         /* 4 */   {"command_substitution1", "$"},
52         /* 5 */   {"command_substitution2", "`"},
53         /* end */ {NULL, NULL}
54 };
55
56 /**
57  * return 1 if the arg is arrowed, otherwise 0 is denied
58  */
59 static int is_cmd_suffix_denied(const char* arg) {
60     int i;
61
62     for (i=0; CMD_SUFFIX_DENY_KEYWORD[i].name != NULL; i++) {
63         if (strstr(arg, CMD_SUFFIX_DENY_KEYWORD[i].suffix) != NULL) {
64             D("cmd suffix denied:%s\n", arg);
65             return 1;
66         }
67     }
68     D("cmd suffix arrowed:%s\n", arg);
69     return 0;
70 }
71
72 static int get_application_install_path(char* pkg_path) {
73     FILE *fp = NULL;
74     char ret_str[PATH_MAX+64] = {0,};
75     int len = 0;
76
77     fp = popen("/usr/bin/pkgcmd -a", "r");
78     if (fp == NULL) {
79         E("failed : popen pkgcmd -a\n");
80         return 0;
81     }
82     if (!fgets(ret_str, PATH_MAX+64, fp)) {
83         E("failed : fgets pkgcmd -a\n");
84         pclose(fp);
85         return 0;
86     }
87     pclose(fp);
88
89     len = strlen(ret_str);
90     while(ret_str[--len]=='\n');
91     ret_str[len + 1] = '\0';
92
93     if (sscanf(ret_str, "Tizen Application Installation Path: %4095s", pkg_path) != 1) {
94         E("failed : parsing fail (str:%s)\n", ret_str);
95         return 0;
96     }
97
98     D("Tizen install path: %s\n", pkg_path);
99     return 1;
100 }
101
102 int is_pkg_file_path(const char* path) {
103     regex_t regex;
104     int ret;
105     char pkg_path[PATH_MAX] = {0,};
106     char pkg_path_regx[PATH_MAX+64] = {0,};
107
108     if (!get_application_install_path(pkg_path)) {
109         E("failed to get application install path\n");
110         return 0;
111     }
112
113     snprintf(pkg_path_regx, sizeof(pkg_path_regx),
114         "^.*(%s/tmp/)+[a-zA-Z0-9_\\-\\.]*\\.(wgt|tpk),*[0-9]*$", pkg_path);
115
116     ret = regcomp(&regex, pkg_path_regx, REG_EXTENDED);
117     if (ret){
118         E("failed : recomp (error:%d)\n", ret);
119         return 0;
120     }
121
122     ret = regexec(&regex, path, 0, NULL, 0);
123     regfree(&regex);
124
125     if (ret){
126         E("This path is NOT package file: %s\n", path);
127         return 0;
128     }
129
130     D("This path is temporary package file: %s\n", path);
131     return 1;
132 }
133
134 /**
135  * Returns 1 if the command is root, otherwise 0.
136  */
137 int verify_root_commands(const char *arg1) {
138     char *tokens[MAX_TOKENS];
139     size_t cnt;
140     int ret = 0;
141     int index = -1;
142     int i = 0;
143
144     D("cmd processing......: %s\n", arg1);
145
146     cnt = tokenize(arg1, " ", tokens, MAX_TOKENS);
147     for (i=0; i<cnt; i++) {
148         D("tokenize: %dth: %s\n", i, tokens[i]);
149     }
150     if (cnt == 0 ) {
151         return 0; // just keep going to execute normal commands
152     }
153     index = is_root_commands(tokens[0]);
154     if (index == -1) {
155         return 0; // just keep going to execute normal commands
156     }
157
158     switch (index) {
159     // in case of profile_command
160     case 0: {
161         ret = 0;
162         if (!is_cmd_suffix_denied(arg1) && (cnt == 2)) {
163             // check if command is used with permitted arguments
164             for (i = 0; root_commands[0].arguments[i] != NULL; i++) {
165                 if (!strncmp(tokens[1], root_commands[0].arguments[i], strlen(tokens[1])+1)){
166                     D("found permitted arguments :%s\n", tokens[1]);
167                     ret = 1;
168                     break;
169                 }
170
171             }
172             if (ret == 0) {
173                 D("not found permitted arguments :%s\n", tokens[1]);
174             }
175         }
176         break;
177     }
178     default: {
179         break;
180     }
181     }
182
183     D("doing the cmd as a %s\n", ret == 1 ? "root" : SDK_USER_NAME);
184
185     if (cnt) {
186         free_strings(tokens, cnt);
187     }
188
189     return ret;
190 }
191
192 int regcmp(const char* pattern, const char* str) {
193     regex_t regex;
194     int ret;
195
196     ret = regcomp(&regex, pattern, REG_EXTENDED);
197     if(ret){ // not match
198         return 0;
199     }
200
201     // execute regular expression
202     ret = regexec(&regex, str, 0, NULL, 0);
203     if(!ret){
204         regfree(&regex);
205         return 1;
206     } else if( ret == REG_NOMATCH ){
207         //D("not valid application path\n");
208     } else{
209         //regerror(ret, &regex, buf, sizeof(buf));
210         //D("regex match failed: %s\n", buf);
211     }
212     regfree(&regex);
213     return 0;
214 }
215
216 int is_root_commands(const char *command) {
217     int i = -1;
218     for(i = 0; root_commands[i].path != NULL; i++) {
219         if(!strncmp(root_commands[i].path, command, strlen(root_commands[i].path)+1)) {
220             return i;
221         }
222     }
223     // not found
224     return -1;
225 }