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 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <errno.h> |
---|
20 | #include <limits.h> |
---|
21 | #include <stdio.h> |
---|
22 | #include <string.h> |
---|
23 | #include <unistd.h> |
---|
24 | |
---|
25 | #include <rtems/shellconfig.h> |
---|
26 | |
---|
27 | #include "internal.h" |
---|
28 | |
---|
29 | static int usage(void) |
---|
30 | { |
---|
31 | puts(rtems_shell_CMDCHOWN_Command.usage); |
---|
32 | |
---|
33 | return -1; |
---|
34 | } |
---|
35 | |
---|
36 | static void error(const char *s, int eno) |
---|
37 | { |
---|
38 | printf("%s: %s\n", s, strerror(eno)); |
---|
39 | } |
---|
40 | |
---|
41 | static int rtems_shell_main_cmdchown(int argc, char **argv) |
---|
42 | { |
---|
43 | if (argc >= 2) { |
---|
44 | const char *s = argv[1]; |
---|
45 | unsigned new_uid = UINT_MAX; |
---|
46 | unsigned new_gid = UINT_MAX; |
---|
47 | bool change_uid = false; |
---|
48 | bool change_gid = false; |
---|
49 | uid_t task_uid; |
---|
50 | int i; |
---|
51 | |
---|
52 | if (strcmp(s, ":") != 0) { |
---|
53 | int n = sscanf(argv[1], "%u:%u", &new_uid, &new_gid); |
---|
54 | |
---|
55 | if (n == 2) { |
---|
56 | change_uid = true; |
---|
57 | change_gid = true; |
---|
58 | } else if (n == 1) { |
---|
59 | change_uid = true; |
---|
60 | } else { |
---|
61 | n = sscanf(argv[1], ":%u", &new_gid); |
---|
62 | |
---|
63 | if (n == 1) { |
---|
64 | change_gid = true; |
---|
65 | } else { |
---|
66 | return usage(); |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | task_uid = getuid(); |
---|
72 | |
---|
73 | for (i = 2; i < argc; ++i) { |
---|
74 | const char *cmd = argv[i]; |
---|
75 | rtems_shell_cmd_t *shell_cmd = rtems_shell_lookup_cmd(cmd); |
---|
76 | |
---|
77 | if (shell_cmd != NULL) { |
---|
78 | if (task_uid == 0 || task_uid == shell_cmd->uid) { |
---|
79 | if (change_uid) { |
---|
80 | shell_cmd->uid = new_uid; |
---|
81 | } |
---|
82 | |
---|
83 | if (change_gid) { |
---|
84 | shell_cmd->gid = new_gid; |
---|
85 | } |
---|
86 | } else if (rtems_shell_can_see_cmd(shell_cmd)) { |
---|
87 | error(cmd, EACCES); |
---|
88 | } else { |
---|
89 | error(cmd, ENOENT); |
---|
90 | } |
---|
91 | } else { |
---|
92 | error(cmd, ENOENT); |
---|
93 | } |
---|
94 | } |
---|
95 | } else { |
---|
96 | return usage(); |
---|
97 | } |
---|
98 | |
---|
99 | return 0; |
---|
100 | } |
---|
101 | |
---|
102 | rtems_shell_cmd_t rtems_shell_CMDCHOWN_Command = { |
---|
103 | .name = "cmdchown", |
---|
104 | .usage = "cmdchown [OWNER][:[GROUP]] COMMAND...", |
---|
105 | .topic = "misc", |
---|
106 | .command = rtems_shell_main_cmdchown |
---|
107 | }; |
---|