8d6a1f4f86f9e04724fa4c58f05f417d3398e011
[sdk/target/sdbd.git] / src / default_plugin_basic.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdarg.h>
21 #include <unistd.h>
22
23 #include <tzplatform_config.h>
24
25 //#define TRACE_TAG TRACE_SDB
26 #define LOG_TAG "SDBD_TRACE_SDB"
27 #include "log.h"
28
29 #include <system_info.h>
30
31 #include "sdb.h"
32 #include "parameter.h"
33 #include "sdbd_plugin.h"
34 #include "sdktools.h"
35
36 #define LOG_DIRECTORY   "/home/owner/share/sdbdlog"
37
38 int get_buildtype() {
39     char* value = NULL;
40     int ret = -1;
41     int r = system_info_get_platform_string("http://tizen.org/system/build.type", &value);
42     if (r != SYSTEM_INFO_ERROR_NONE) {
43         E("fail to get build.type:%d\n", errno);
44         return -1;
45     }
46     else {
47         if (value != NULL) {
48             // check for "eng"
49             if (!strncmp(value, "eng", sizeof("eng") + 1))
50             {
51                 ret = 1;
52             }
53             else {  // expect "user"
54                 ret = 0;
55             }
56             free(value);
57         }
58
59     }
60     return ret;
61 }
62
63 int get_plugin_capability ( parameters* in, parameters* out )
64 {
65     int capability;
66
67     if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
68             || in->array_of_parameter[0].type != type_int32 ) {
69         E ( "Invalid argument\n" );
70         return PLUGIN_CMD_FAIL;
71     }
72
73     if ( out == NULL ) {
74         E ( "Invalid argument\n" );
75         return PLUGIN_CMD_FAIL;
76     }
77
78     out->number_of_parameter = 1;
79     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
80     if (out->array_of_parameter == NULL) {
81         E("failed to allocate memory for the parameter\n");
82         return PLUGIN_CMD_FAIL;
83     }
84
85     capability = in->array_of_parameter[0].v_int32;
86
87     if ( capability == CAPABILITY_SECURE ) {
88         make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_DISABLED );
89     } else if ( capability == CAPABILITY_INTER_SHELL ) {
90         make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED );
91     } else if ( capability == CAPABILITY_FILESYNC ) {
92         make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_PUSHPULL );
93     } else if ( capability == CAPABILITY_USB_PROTOCOL ) {
94         make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED );
95     } else if ( capability == CAPABILITY_SOCK_PROTOCOL ) {
96         make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED );
97     } else if ( capability == CAPABILITY_ROOT_ONOFF ) {
98         if ( access ( "/bin/su", F_OK ) == 0 ) {
99             if ( get_buildtype() == 1 ) {
100                 make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED );
101             }
102             else {
103                 make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_DISABLED );
104             }
105         } else {
106             make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_DISABLED );
107         }
108     } else if ( capability == CAPABILITY_CAN_LAUNCH ) {
109         make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_UNKNOWN );
110     } else if ( capability == CAPABILITY_PLUGIN_VER ) {
111         make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_UNKNOWN );
112     } else if ( capability == CAPABILITY_PRODUCT_VER ) {
113         make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_UNKNOWN );
114     } else if ( capability == CAPABILITY_LOG_ENABLE ) {
115         make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_DISABLED );
116     } else if ( capability == CAPABILITY_LOG_PATH ) {
117         const char* sdkhome = tzplatform_getenv(TZ_SDK_HOME);
118         if (sdkhome != NULL) {
119             make_string_parameter ( & ( out->array_of_parameter[0] ), "%s/share/sdbdlog", sdkhome );
120         } else {
121             make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", LOG_DIRECTORY );
122         }
123     } else if ( capability == CAPABILITY_APPCMD ) {
124         make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED );
125     } else if (capability == CAPABILITY_DEBUGMODE ) {
126         make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED );
127     } else {
128         out->number_of_parameter = 0;
129         free ( out->array_of_parameter );
130         out->array_of_parameter = NULL;
131         return PLUGIN_CMD_NOT_SUPPORT;
132     }
133
134     return PLUGIN_CMD_SUCCESS;
135 }
136
137 int verify_shell_cmd ( parameters* in, parameters* out )
138 {
139     if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
140             || in->array_of_parameter[0].type != type_string ) {
141         E ( "Invalid argument\n" );
142         return PLUGIN_CMD_FAIL;
143     }
144
145     if ( out == NULL ) {
146         E ( "Invalid argument\n" );
147         return PLUGIN_CMD_FAIL;
148     }
149
150     D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data );
151
152     out->number_of_parameter = 1;
153     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
154     if (out->array_of_parameter == NULL) {
155         E("failed to allocate memory for the parameter\n");
156         return PLUGIN_CMD_FAIL;
157     }
158     out->array_of_parameter[0].type = type_int32;
159     out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID;
160
161     return PLUGIN_CMD_SUCCESS;
162 }
163
164 int convert_shell_cmd ( parameters* in, parameters* out )
165 {
166     if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
167             || in->array_of_parameter[0].type != type_string ) {
168         E ( "Invalid argument\n" );
169         return PLUGIN_CMD_FAIL;
170     }
171
172     if ( out == NULL ) {
173         E ( "Invalid argument\n" );
174         return PLUGIN_CMD_FAIL;
175     }
176
177     D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data );
178
179     out->number_of_parameter = 1;
180     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
181     if (out->array_of_parameter == NULL) {
182         E("failed to allocate memory for the parameter\n");
183         return PLUGIN_CMD_FAIL;
184     }
185
186     make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", in->array_of_parameter[0].v_string.data );
187     return PLUGIN_CMD_SUCCESS;
188 }
189
190 int verify_peer_ip ( parameters* in, parameters* out )
191 {
192     if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
193             || in->array_of_parameter[0].type != type_string ) {
194         E ( "Invalid argument\n" );
195         return PLUGIN_CMD_FAIL;
196     }
197
198     if ( out == NULL ) {
199         E ( "Invalid argument\n" );
200         return PLUGIN_CMD_FAIL;
201     }
202
203     D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data );
204
205     out->number_of_parameter = 1;
206     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
207     if (out->array_of_parameter == NULL) {
208         E("failed to allocate memory for the parameter\n");
209         return PLUGIN_CMD_FAIL;
210     }
211     out->array_of_parameter[0].type = type_int32;
212     out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID;
213
214     return PLUGIN_CMD_SUCCESS;
215 }
216
217 int verify_peer_ipv6 ( parameters* in, parameters* out )
218 {
219     if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
220             || in->array_of_parameter[0].type != type_string ) {
221         D ( "Invalid argument\n" );
222         return PLUGIN_CMD_FAIL;
223     }
224
225     if ( out == NULL ) {
226         D ( "Invalid argument\n" );
227         return PLUGIN_CMD_FAIL;
228     }
229
230     D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data );
231
232     out->number_of_parameter = 1;
233     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
234     if (out->array_of_parameter == NULL) {
235         D("failed to allocate memory for the parameter\n");
236         return PLUGIN_CMD_FAIL;
237     }
238     out->array_of_parameter[0].type = type_int32;
239     out->array_of_parameter[0].v_int32 = PLUGIN_RET_INVALID;
240
241     return PLUGIN_CMD_SUCCESS;
242 }
243
244 int verify_sdbd_launch ( parameters* in, parameters* out )
245 {
246     if ( out == NULL ) {
247         E ( "Invalid argument\n" );
248         return PLUGIN_CMD_FAIL;
249     }
250
251     out->number_of_parameter = 1;
252     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
253     if (out->array_of_parameter == NULL) {
254         E("failed to allocate memory for the parameter\n");
255         return PLUGIN_CMD_FAIL;
256     }
257     out->array_of_parameter[0].type = type_int32;
258     out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID;
259
260     return PLUGIN_CMD_SUCCESS;
261 }
262
263 int verify_root_cmd ( parameters* in, parameters* out )
264 {
265     if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
266             || in->array_of_parameter[0].type != type_string ) {
267         E ( "Invalid argument\n" );
268         return PLUGIN_CMD_FAIL;
269     }
270
271     if ( out == NULL ) {
272         E ( "Invalid argument\n" );
273         return PLUGIN_CMD_FAIL;
274     }
275
276     D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data );
277
278     out->number_of_parameter = 1;
279     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
280     if (out->array_of_parameter == NULL) {
281         E("failed to allocate memory for the parameter\n");
282         return PLUGIN_CMD_FAIL;
283     }
284     out->array_of_parameter[0].type = type_int32;
285
286     if ( verify_root_commands ( in->array_of_parameter[0].v_string.data ) ) {
287         out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID;
288     } else {
289         out->array_of_parameter[0].v_int32 = PLUGIN_RET_INVALID;
290     }
291
292     return PLUGIN_CMD_SUCCESS;
293 }
294
295 int get_shell_env ( parameters* in, parameters* out )
296 {
297     if ( out == NULL ) {
298         E ( "Invalid argument\n" );
299         return PLUGIN_CMD_FAIL;
300     }
301
302     out->number_of_parameter = 1;
303     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
304     if (out->array_of_parameter == NULL) {
305         E("failed to allocate memory for the parameter\n");
306         return PLUGIN_CMD_FAIL;
307     }
308
309     make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", "" );
310     return PLUGIN_CMD_SUCCESS;
311 }
312
313 int verify_push ( parameters* in, parameters* out )
314 {
315     if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
316             || in->array_of_parameter[0].type != type_string ) {
317         E ( "Invalid argument\n" );
318         return PLUGIN_CMD_FAIL;
319     }
320
321     if ( out == NULL ) {
322         E ( "Invalid argument\n" );
323         return PLUGIN_CMD_FAIL;
324     }
325
326     out->number_of_parameter = 1;
327     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
328     if (out->array_of_parameter == NULL) {
329         E("failed to allocate memory for the parameter\n");
330         return PLUGIN_CMD_FAIL;
331     }
332     out->array_of_parameter[0].type = type_int32;
333     out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID;
334
335     return PLUGIN_CMD_SUCCESS;
336 }
337
338 int verify_pull ( parameters* in, parameters* out )
339 {
340     if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
341             || in->array_of_parameter[0].type != type_string ) {
342         E ( "Invalid argument\n" );
343         return PLUGIN_CMD_FAIL;
344     }
345
346     if ( out == NULL ) {
347         E ( "Invalid argument\n" );
348         return PLUGIN_CMD_FAIL;
349     }
350
351     out->number_of_parameter = 1;
352     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
353     if (out->array_of_parameter == NULL) {
354         E("failed to allocate memory for the parameter\n");
355         return PLUGIN_CMD_FAIL;
356     }
357     out->array_of_parameter[0].type = type_int32;
358     out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID;
359
360     return PLUGIN_CMD_SUCCESS;
361 }
362
363 int verify_handle_by_plugin ( parameters* in, parameters* out )
364 {
365     if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
366             || in->array_of_parameter[0].type != type_string ) {
367         D ( "Invalid argument\n" );
368         return PLUGIN_CMD_FAIL;
369     }
370
371     if ( out == NULL ) {
372         D ( "Invalid argument\n" );
373         return PLUGIN_CMD_FAIL;
374     }
375
376     D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data );
377
378     out->number_of_parameter = 1;
379     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
380     if (out->array_of_parameter == NULL) {
381         D("failed to allocate memory for the parameter\n");
382         return PLUGIN_CMD_FAIL;
383     }
384
385     //the data contains the string sample-echo followed by space and then the user string, hence checking for space.
386     if((in->array_of_parameter[0].v_string.data[11] == ' ')&&(!strncmp(in->array_of_parameter[0].v_string.data, "sample-echo", strlen("sample-echo")))) {
387         out->array_of_parameter[0].type = type_int32;
388         out->array_of_parameter[0].v_int32 = PLUGIN_RET_HANDLE;
389     }
390     else {
391         out->array_of_parameter[0].type = type_int32;
392         out->array_of_parameter[0].v_int32 = PLUGIN_RET_NOT_HANDLE;
393     }
394
395     return PLUGIN_CMD_SUCCESS;
396 }
397
398 int get_plugin_extra_capability ( parameters* in, parameters* out )
399 {
400     if ( out == NULL ) {
401         E ( "Invalid argument\n" );
402         return PLUGIN_CMD_FAIL;
403     }
404
405     out->number_of_parameter = 1;
406     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
407     if (out->array_of_parameter == NULL) {
408         E("failed to allocate memory for the parameter\n");
409         return PLUGIN_CMD_FAIL;
410     }
411
412     make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", "" );
413     return PLUGIN_CMD_SUCCESS;
414 }