Update To 11.40.268.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 #include "base/memory/scoped_ptr.h"
20
21 int xwalk_tizen_check_group_users(void) {
22   int err = 0;
23   struct group grp;
24   struct group* current_g;
25   int64_t len = sysconf(_SC_GETGR_R_SIZE_MAX);
26   if (len == -1)
27     len = 1024;
28
29   scoped_ptr<char[]> buffer(new char[len]);
30   err = getgrgid_r(getgid(), &grp, buffer.get(), len, &current_g);
31   if (err) {
32     fprintf(stderr, "group can't be determined");
33     fprintf(stderr, "launching an application will not work\n");
34     return -EINVAL;
35   }
36
37   if ((!current_g) ||
38       strcmp(current_g->gr_name, "users")) {
39       fprintf(stderr, "group '%s' is not allowed: ",
40               current_g ? current_g->gr_name : "<NULL>");
41       fprintf(stderr, "launching an application will not work\n");
42       return -EINVAL;
43   }
44   return 0;
45 }
46
47 int xwalk_tizen_check_user_for_xwalk_backend(void) {
48   int err = 0;
49   struct group grp;
50   struct group* current_g;
51   int64_t len = sysconf(_SC_GETGR_R_SIZE_MAX);
52   if (len == -1)
53     len = 1024;
54
55   scoped_ptr<char[]> buffer(new char[len]);
56   err = getgrgid_r(getgid(), &grp, buffer.get(), len, &current_g);
57   if (err) {
58     fprintf(stderr, "group can't be determined");
59     fprintf(stderr, "launching an application will not work\n");
60     return -EINVAL;
61   }
62
63   if ((!current_g) || (
64       strcmp(current_g->gr_name, "users") &&
65       getuid() != tzplatform_getuid(TZ_SYS_GLOBALAPP_USER))) {
66     fprintf(stderr, "group '%s' is not allowed: ",
67             current_g ? current_g->gr_name : "<NULL>");
68     fprintf(stderr, "launching an application will not work\n");
69     return -EINVAL;
70   }
71   return 0;
72 }