1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) 2014, 2019 embedded brains GmbH & Co. KG |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #ifdef HAVE_CONFIG_H |
---|
29 | #include "config.h" |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <sys/stat.h> |
---|
33 | #include <sys/types.h> |
---|
34 | #include <errno.h> |
---|
35 | #include <grp.h> |
---|
36 | #include <pwd.h> |
---|
37 | #include <stdio.h> |
---|
38 | #include <unistd.h> |
---|
39 | |
---|
40 | #include <rtems/imfs.h> |
---|
41 | #include <rtems/shell.h> |
---|
42 | #include <rtems/userenv.h> |
---|
43 | |
---|
44 | #include "tmacros.h" |
---|
45 | |
---|
46 | const char rtems_test_name[] = "SHELL 1"; |
---|
47 | |
---|
48 | static const char etc_passwd[] = |
---|
49 | "moop:foo:1:3:blob:a::c\n" |
---|
50 | "no:*:2:4::::\n" |
---|
51 | "zero::3:5::::\n" |
---|
52 | "shadow:x:4:6::::\n" |
---|
53 | "invchroot::5:7:::/inv:\n" |
---|
54 | "chroot::6:8:::/chroot:\n"; |
---|
55 | |
---|
56 | static const char etc_group[] = |
---|
57 | "A::1:moop,u,v,w,zero\n" |
---|
58 | "B::2:moop\n" |
---|
59 | "blub:bar:3:moop\n" |
---|
60 | "C::4:l,m,n,moop\n" |
---|
61 | "D::5:moop,moop\n" |
---|
62 | "E::6:x\n" |
---|
63 | "E::7:y,z\n" |
---|
64 | "F::8:s,moop,t\n"; |
---|
65 | |
---|
66 | static const char joel_in[] = |
---|
67 | "#! joel\n" |
---|
68 | "jtst hello world\n" |
---|
69 | "jtst 1 2 3 4 5\n"; |
---|
70 | |
---|
71 | static const char joel_out_1[] = |
---|
72 | " 3 'jtst hello world'\n" |
---|
73 | " 6 'jtst 1 2 3 4 5'\n"; |
---|
74 | |
---|
75 | static const char joel_out_2[] = |
---|
76 | "\n" |
---|
77 | "RTEMS Shell on (null). Use 'help' to list commands.\n" |
---|
78 | " 3 'jtst hello world'\n" |
---|
79 | " 6 'jtst 1 2 3 4 5'\n"; |
---|
80 | |
---|
81 | static int joel_test_command(int argc, char** argv) |
---|
82 | { |
---|
83 | int i; |
---|
84 | fprintf(stdout, "%2d '", argc); |
---|
85 | for (i = 0; i < argc; ++i) { |
---|
86 | fprintf(stdout, argv[i]); |
---|
87 | if (i < (argc - 1)) |
---|
88 | fprintf(stdout, " "); |
---|
89 | } |
---|
90 | fprintf(stdout, "'\n"); |
---|
91 | return 0; |
---|
92 | } |
---|
93 | |
---|
94 | static void test_joel(void) |
---|
95 | { |
---|
96 | /* |
---|
97 | * Running a joel script tests the shell main loop. |
---|
98 | */ |
---|
99 | char buf[sizeof(joel_out_2) + 1]; |
---|
100 | rtems_shell_cmd_t* cmd; |
---|
101 | FILE *in; |
---|
102 | FILE *out; |
---|
103 | FILE *current_stdout = stdout; |
---|
104 | FILE *current_stdin = stdin; |
---|
105 | size_t len; |
---|
106 | rtems_status_code sc; |
---|
107 | |
---|
108 | /* |
---|
109 | * Use a private command due to the way the testsuite maps printk onto printf. |
---|
110 | */ |
---|
111 | cmd = rtems_shell_add_cmd("jtst", "misc", "joel test", joel_test_command); |
---|
112 | rtems_test_assert(cmd != NULL); |
---|
113 | |
---|
114 | len = strlen(joel_in); |
---|
115 | |
---|
116 | in = fopen("/jin", "w"); |
---|
117 | rtems_test_assert(in != NULL); |
---|
118 | rtems_test_assert(fwrite(joel_in, sizeof(char), len, in) == len); |
---|
119 | rtems_test_assert(fclose(in) == 0); |
---|
120 | |
---|
121 | /* |
---|
122 | * The shell opens the input and output files. |
---|
123 | */ |
---|
124 | sc = rtems_shell_script( |
---|
125 | "JTST", |
---|
126 | 8 * 1024, |
---|
127 | 1, |
---|
128 | "/jin", |
---|
129 | "/jout", |
---|
130 | false, |
---|
131 | true, |
---|
132 | false); |
---|
133 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
134 | |
---|
135 | out = fopen("/jout", "r"); |
---|
136 | rtems_test_assert(out != NULL); |
---|
137 | rtems_test_assert(!feof(out)); |
---|
138 | memset(buf, 0, sizeof(buf)); |
---|
139 | len = fread(buf, sizeof(char), sizeof(buf), out); |
---|
140 | rtems_test_assert(len > 0); |
---|
141 | rtems_test_assert(strcmp(joel_out_1, buf) == 0); |
---|
142 | rtems_test_assert(fclose(out) == 0); |
---|
143 | |
---|
144 | /* |
---|
145 | * The shell task inherits the parent stdin and stdout |
---|
146 | */ |
---|
147 | in = fopen("/jin", "r"); |
---|
148 | rtems_test_assert(in != NULL); |
---|
149 | out = fopen("/jout", "w"); |
---|
150 | rtems_test_assert(out != NULL); |
---|
151 | |
---|
152 | stdin = in; |
---|
153 | stdout = out; |
---|
154 | |
---|
155 | sc = rtems_shell_script( |
---|
156 | "JTST", |
---|
157 | 8 * 1024, |
---|
158 | 1, |
---|
159 | "stdin", |
---|
160 | "stdout", |
---|
161 | false, |
---|
162 | true, |
---|
163 | false); |
---|
164 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
165 | |
---|
166 | stdout = current_stdout; |
---|
167 | stdin = current_stdin; |
---|
168 | |
---|
169 | rtems_test_assert(fclose(in) == 0); |
---|
170 | rtems_test_assert(fclose(out) == 0); |
---|
171 | |
---|
172 | out = fopen("/jout", "r"); |
---|
173 | rtems_test_assert(out != NULL); |
---|
174 | rtems_test_assert(!feof(out)); |
---|
175 | memset(buf, 0, sizeof(buf)); |
---|
176 | len = fread(buf, sizeof(char), sizeof(buf), out); |
---|
177 | rtems_test_assert(len > 0); |
---|
178 | rtems_test_assert(strcmp(joel_out_2, buf) == 0); |
---|
179 | rtems_test_assert(fclose(out) == 0); |
---|
180 | } |
---|
181 | |
---|
182 | static void test(void) |
---|
183 | { |
---|
184 | rtems_user_env_t *uenv; |
---|
185 | rtems_status_code sc; |
---|
186 | struct stat st_chroot; |
---|
187 | struct stat st_workdir; |
---|
188 | bool ok; |
---|
189 | int rv; |
---|
190 | |
---|
191 | rv = mkdir("/etc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
---|
192 | rtems_test_assert(rv == 0); |
---|
193 | |
---|
194 | rv = mkdir("/chroot", S_IRWXU | S_IRWXG | S_IRWXO); |
---|
195 | rtems_test_assert(rv == 0); |
---|
196 | |
---|
197 | rv = lstat("/chroot", &st_chroot); |
---|
198 | rtems_test_assert(rv == 0); |
---|
199 | |
---|
200 | rv = IMFS_make_linearfile( |
---|
201 | "/etc/passwd", |
---|
202 | S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH, |
---|
203 | etc_passwd, |
---|
204 | sizeof(etc_passwd) |
---|
205 | ); |
---|
206 | rtems_test_assert(rv == 0); |
---|
207 | |
---|
208 | rv = IMFS_make_linearfile( |
---|
209 | "/etc/group", |
---|
210 | S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH, |
---|
211 | etc_group, |
---|
212 | sizeof(etc_group) |
---|
213 | ); |
---|
214 | rtems_test_assert(rv == 0); |
---|
215 | |
---|
216 | sc = rtems_libio_set_private_env(); |
---|
217 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
218 | |
---|
219 | uenv = rtems_current_user_env_get(); |
---|
220 | |
---|
221 | rv = setuid(0); |
---|
222 | rtems_test_assert(rv == 0); |
---|
223 | |
---|
224 | rv = seteuid(0); |
---|
225 | rtems_test_assert(rv == 0); |
---|
226 | |
---|
227 | ok = rtems_shell_login_check("inv", NULL); |
---|
228 | rtems_test_assert(!ok); |
---|
229 | |
---|
230 | ok = rtems_shell_login_check("no", NULL); |
---|
231 | rtems_test_assert(!ok); |
---|
232 | |
---|
233 | ok = rtems_shell_login_check("shadow", NULL); |
---|
234 | rtems_test_assert(!ok); |
---|
235 | |
---|
236 | ok = rtems_shell_login_check("moop", "false"); |
---|
237 | rtems_test_assert(!ok); |
---|
238 | |
---|
239 | ok = rtems_shell_login_check("invchroot", NULL); |
---|
240 | rtems_test_assert(!ok); |
---|
241 | |
---|
242 | rtems_test_assert(getuid() == 0); |
---|
243 | rtems_test_assert(geteuid() == 0); |
---|
244 | rtems_test_assert(getgid() == 0); |
---|
245 | rtems_test_assert(getegid() == 0); |
---|
246 | rtems_test_assert(uenv->ngroups == 0); |
---|
247 | |
---|
248 | ok = rtems_shell_login_check("zero", NULL); |
---|
249 | rtems_test_assert(ok); |
---|
250 | rtems_test_assert(getuid() == 3); |
---|
251 | rtems_test_assert(geteuid() == 3); |
---|
252 | rtems_test_assert(getgid() == 5); |
---|
253 | rtems_test_assert(getegid() == 5); |
---|
254 | rtems_test_assert(uenv->ngroups == 1); |
---|
255 | rtems_test_assert(uenv->groups[0] == 1); |
---|
256 | |
---|
257 | ok = rtems_shell_login_check("moop", "foo"); |
---|
258 | rtems_test_assert(ok); |
---|
259 | rtems_test_assert(getuid() == 1); |
---|
260 | rtems_test_assert(geteuid() == 1); |
---|
261 | rtems_test_assert(getgid() == 3); |
---|
262 | rtems_test_assert(getegid() == 3); |
---|
263 | rtems_test_assert(uenv->ngroups == 5); |
---|
264 | rtems_test_assert(uenv->groups[0] == 1); |
---|
265 | rtems_test_assert(uenv->groups[1] == 2); |
---|
266 | rtems_test_assert(uenv->groups[2] == 4); |
---|
267 | rtems_test_assert(uenv->groups[3] == 5); |
---|
268 | rtems_test_assert(uenv->groups[4] == 8); |
---|
269 | |
---|
270 | rv = setuid(0); |
---|
271 | rtems_test_assert(rv == 0); |
---|
272 | |
---|
273 | rv = seteuid(0); |
---|
274 | rtems_test_assert(rv == 0); |
---|
275 | |
---|
276 | ok = rtems_shell_login_check("chroot", NULL); |
---|
277 | rtems_test_assert(ok); |
---|
278 | rtems_test_assert(getuid() == 6); |
---|
279 | rtems_test_assert(geteuid() == 6); |
---|
280 | rtems_test_assert(getgid() == 8); |
---|
281 | rtems_test_assert(getegid() == 8); |
---|
282 | |
---|
283 | rv = lstat(".", &st_workdir); |
---|
284 | rtems_test_assert(rv == 0); |
---|
285 | rtems_test_assert(memcmp(&st_chroot, &st_workdir, sizeof(st_chroot)) == 0); |
---|
286 | |
---|
287 | rtems_libio_use_global_env(); |
---|
288 | } |
---|
289 | |
---|
290 | static void Init(rtems_task_argument arg) |
---|
291 | { |
---|
292 | TEST_BEGIN(); |
---|
293 | |
---|
294 | test(); |
---|
295 | test_joel(); |
---|
296 | |
---|
297 | TEST_END(); |
---|
298 | rtems_test_exit(0); |
---|
299 | } |
---|
300 | |
---|
301 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
302 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
303 | |
---|
304 | #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 5 |
---|
305 | |
---|
306 | #define CONFIGURE_MAXIMUM_TASKS 3 |
---|
307 | #define CONFIGURE_MAXIMUM_POSIX_KEYS 2 |
---|
308 | #define CONFIGURE_MAXIMUM_POSIX_KEY_VALUE_PAIRS 2 |
---|
309 | |
---|
310 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
311 | |
---|
312 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
313 | |
---|
314 | #define CONFIGURE_INIT |
---|
315 | |
---|
316 | #include <rtems/confdefs.h> |
---|
317 | |
---|
318 | #define CONFIGURE_SHELL_COMMANDS_INIT |
---|
319 | |
---|
320 | #include <rtems/shellconfig.h> |
---|