1 | /* |
---|
2 | * Copyright (c) 2014 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <sys/types.h> |
---|
21 | #include <errno.h> |
---|
22 | #include <grp.h> |
---|
23 | #include <pwd.h> |
---|
24 | #include <stdio.h> |
---|
25 | #include <unistd.h> |
---|
26 | |
---|
27 | #include <rtems/shell.h> |
---|
28 | #include <rtems/userenv.h> |
---|
29 | |
---|
30 | #include "tmacros.h" |
---|
31 | |
---|
32 | const char rtems_test_name[] = "SHELL 1"; |
---|
33 | |
---|
34 | static void create_file(const char *name, const char *content) |
---|
35 | { |
---|
36 | FILE *fp; |
---|
37 | int rv; |
---|
38 | |
---|
39 | fp = fopen(name, "wx"); |
---|
40 | rtems_test_assert(fp != NULL); |
---|
41 | |
---|
42 | rv = fputs(content, fp); |
---|
43 | rtems_test_assert(rv == 0); |
---|
44 | |
---|
45 | rv = fclose(fp); |
---|
46 | rtems_test_assert(rv == 0); |
---|
47 | } |
---|
48 | |
---|
49 | static void test(void) |
---|
50 | { |
---|
51 | rtems_user_env_t *uenv; |
---|
52 | bool ok; |
---|
53 | int rv; |
---|
54 | |
---|
55 | rv = mkdir("/etc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
---|
56 | rtems_test_assert(rv == 0); |
---|
57 | |
---|
58 | create_file( |
---|
59 | "/etc/passwd", |
---|
60 | "moop:foo:1:3:blob:a::c\n" |
---|
61 | "no:*:2:4::::\n" |
---|
62 | "zero::3:5::::\n" |
---|
63 | "shadow:x:4:6::::\n" |
---|
64 | ); |
---|
65 | |
---|
66 | create_file( |
---|
67 | "/etc/group", |
---|
68 | "A::1:moop,u,v,w,zero\n" |
---|
69 | "B::2:moop\n" |
---|
70 | "blub:bar:3:moop\n" |
---|
71 | "C::4:l,m,n,moop\n" |
---|
72 | "D::5:moop,moop\n" |
---|
73 | "E::6:x\n" |
---|
74 | "E::7:y,z\n" |
---|
75 | "F::8:s,moop,t\n" |
---|
76 | ); |
---|
77 | |
---|
78 | uenv = rtems_current_user_env_get(); |
---|
79 | |
---|
80 | rv = setuid(0); |
---|
81 | rtems_test_assert(rv == 0); |
---|
82 | |
---|
83 | rv = seteuid(0); |
---|
84 | rtems_test_assert(rv == 0); |
---|
85 | |
---|
86 | ok = rtems_shell_login_check("inv", NULL); |
---|
87 | rtems_test_assert(!ok); |
---|
88 | |
---|
89 | ok = rtems_shell_login_check("no", NULL); |
---|
90 | rtems_test_assert(!ok); |
---|
91 | |
---|
92 | ok = rtems_shell_login_check("shadow", NULL); |
---|
93 | rtems_test_assert(!ok); |
---|
94 | |
---|
95 | ok = rtems_shell_login_check("moop", "false"); |
---|
96 | rtems_test_assert(!ok); |
---|
97 | |
---|
98 | rtems_test_assert(getuid() == 0); |
---|
99 | rtems_test_assert(geteuid() == 0); |
---|
100 | rtems_test_assert(getgid() == 0); |
---|
101 | rtems_test_assert(getegid() == 0); |
---|
102 | rtems_test_assert(uenv->ngroups == 0); |
---|
103 | |
---|
104 | ok = rtems_shell_login_check("zero", NULL); |
---|
105 | rtems_test_assert(ok); |
---|
106 | rtems_test_assert(getuid() == 3); |
---|
107 | rtems_test_assert(geteuid() == 3); |
---|
108 | rtems_test_assert(getgid() == 5); |
---|
109 | rtems_test_assert(getegid() == 5); |
---|
110 | rtems_test_assert(uenv->ngroups == 1); |
---|
111 | rtems_test_assert(uenv->groups[0] == 1); |
---|
112 | |
---|
113 | ok = rtems_shell_login_check("moop", "foo"); |
---|
114 | rtems_test_assert(ok); |
---|
115 | rtems_test_assert(getuid() == 1); |
---|
116 | rtems_test_assert(geteuid() == 1); |
---|
117 | rtems_test_assert(getgid() == 3); |
---|
118 | rtems_test_assert(getegid() == 3); |
---|
119 | rtems_test_assert(uenv->ngroups == 5); |
---|
120 | rtems_test_assert(uenv->groups[0] == 1); |
---|
121 | rtems_test_assert(uenv->groups[1] == 2); |
---|
122 | rtems_test_assert(uenv->groups[2] == 4); |
---|
123 | rtems_test_assert(uenv->groups[3] == 5); |
---|
124 | rtems_test_assert(uenv->groups[4] == 8); |
---|
125 | } |
---|
126 | |
---|
127 | static void Init(rtems_task_argument arg) |
---|
128 | { |
---|
129 | TEST_BEGIN(); |
---|
130 | |
---|
131 | test(); |
---|
132 | |
---|
133 | TEST_END(); |
---|
134 | rtems_test_exit(0); |
---|
135 | } |
---|
136 | |
---|
137 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
138 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
139 | |
---|
140 | #define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM |
---|
141 | |
---|
142 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4 |
---|
143 | |
---|
144 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
145 | #define CONFIGURE_MAXIMUM_POSIX_KEYS 1 |
---|
146 | #define CONFIGURE_MAXIMUM_POSIX_KEY_VALUE_PAIRS 1 |
---|
147 | |
---|
148 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
149 | |
---|
150 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
151 | |
---|
152 | #define CONFIGURE_INIT |
---|
153 | |
---|
154 | #include <rtems/confdefs.h> |
---|
155 | |
---|
156 | #define CONFIGURE_SHELL_COMMANDS_INIT |
---|
157 | |
---|
158 | #include <rtems/shellconfig.h> |
---|