Upstream version 11.39.256.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / tizen / xwalk_tizen_user.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "xwalk/application/tools/tizen/xwalk_tizen_user.h"
6
7 #include <errno.h>
8 #include <grp.h>
9 #include <pwd.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16
17 #include <tzplatform_config.h>
18
19 int xwalk_tizen_check_group_users(void) {
20   char* buffer = NULL;
21   int err = 0;
22   struct group grp;
23   struct group* current_g;
24   int64_t len = sysconf(_SC_GETGR_R_SIZE_MAX);
25   if (len == -1)
26     len = 1024;
27   buffer = reinterpret_cast<char*>(malloc((size_t)len));
28   if (!buffer)
29     return -EINVAL;
30
31   err = getgrgid_r(getgid(), &grp, buffer, len, &current_g);
32   if (err) {
33     fprintf(stderr, "group can't be determined");
34     fprintf(stderr, "launching an application will not work\n");
35     free(buffer);
36     return -EINVAL;
37   }
38
39   if ((!current_g) ||
40       strcmp(current_g->gr_name, "users")) {
41       fprintf(stderr, "group '%s' is not allowed :",
42               current_g ? current_g->gr_name : "<NULL>");
43       fprintf(stderr, "launching an application will not work\n");
44       free(buffer);
45       return -EINVAL;
46   }
47   return 0;
48 }
49
50 int xwalk_tizen_check_user_for_xwalk_backend(void) {
51   char* buffer = NULL;
52   int err = 0;
53   struct group grp;
54   struct group* current_g;
55   int64_t len = sysconf(_SC_GETGR_R_SIZE_MAX);
56   if (len == -1)
57     len = 1024;
58   buffer = reinterpret_cast<char*>(malloc((size_t)len));
59   if (!buffer)
60     return -EINVAL;
61
62   err = getgrgid_r(getgid(), &grp, buffer, len, &current_g);
63   if (err) {
64     fprintf(stderr, "group can't be determined");
65     fprintf(stderr, "launching an application will not work\n");
66     free(buffer);
67     return -EINVAL;
68   }
69
70   if ((!current_g) || (
71       strcmp(current_g->gr_name, "users") &&
72       getuid() != tzplatform_getuid(TZ_SYS_GLOBALAPP_USER))) {
73     fprintf(stderr, "group '%s' is not allowed :",
74             current_g ? current_g->gr_name : "<NULL>");
75     fprintf(stderr, "launching an application will not work\n");
76     free(buffer);
77     return -EINVAL;
78   }
79   return 0;
80 }