File indexing completed on 2025-05-11 08:24:19
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifdef HAVE_CONFIG_H
0010 #include "config.h"
0011 #endif
0012
0013 #include <errno.h>
0014 #include <limits.h>
0015 #include <stdio.h>
0016 #include <string.h>
0017 #include <unistd.h>
0018
0019 #include <rtems/shellconfig.h>
0020
0021 #include "internal.h"
0022
0023 static int usage(void)
0024 {
0025 puts(rtems_shell_CMDCHOWN_Command.usage);
0026
0027 return -1;
0028 }
0029
0030 static void error(const char *s, int eno)
0031 {
0032 printf("%s: %s\n", s, strerror(eno));
0033 }
0034
0035 static int rtems_shell_main_cmdchown(int argc, char **argv)
0036 {
0037 if (argc >= 2) {
0038 const char *s = argv[1];
0039 unsigned new_uid = UINT_MAX;
0040 unsigned new_gid = UINT_MAX;
0041 bool change_uid = false;
0042 bool change_gid = false;
0043 uid_t task_uid;
0044 int i;
0045
0046 if (strcmp(s, ":") != 0) {
0047 int n = sscanf(argv[1], "%u:%u", &new_uid, &new_gid);
0048
0049 if (n == 2) {
0050 change_uid = true;
0051 change_gid = true;
0052 } else if (n == 1) {
0053 change_uid = true;
0054 } else {
0055 n = sscanf(argv[1], ":%u", &new_gid);
0056
0057 if (n == 1) {
0058 change_gid = true;
0059 } else {
0060 return usage();
0061 }
0062 }
0063 }
0064
0065 task_uid = getuid();
0066
0067 for (i = 2; i < argc; ++i) {
0068 const char *cmd = argv[i];
0069 rtems_shell_cmd_t *shell_cmd = rtems_shell_lookup_cmd(cmd);
0070
0071 if (shell_cmd != NULL) {
0072 if (task_uid == 0 || task_uid == shell_cmd->uid) {
0073 if (change_uid) {
0074 shell_cmd->uid = new_uid;
0075 }
0076
0077 if (change_gid) {
0078 shell_cmd->gid = new_gid;
0079 }
0080 } else if (rtems_shell_can_see_cmd(shell_cmd)) {
0081 error(cmd, EACCES);
0082 } else {
0083 error(cmd, ENOENT);
0084 }
0085 } else {
0086 error(cmd, ENOENT);
0087 }
0088 }
0089 } else {
0090 return usage();
0091 }
0092
0093 return 0;
0094 }
0095
0096 rtems_shell_cmd_t rtems_shell_CMDCHOWN_Command = {
0097 .name = "cmdchown",
0098 .usage = "cmdchown [OWNER][:[GROUP]] COMMAND...",
0099 .topic = "misc",
0100 .command = rtems_shell_main_cmdchown
0101 };