source: rtems/cpukit/libmisc/shell/shell_cmdset.c @ 7eada71

4.115
Last change on this file since 7eada71 was 7eada71, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 18, 2014 at 6:35:30 AM

shell: Add mode, UID and GID to shell commands

Use this information to determine if a command is visible to the current
user and if the current user is allowed to execute this command.

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 *
3 *  Shell Command Set Management
4 *
5 *  Author:
6 *   WORK: fernando.ruiz@ctv.es
7 *   HOME: correo@fernando-ruiz.com
8 *
9 *  The license and distribution terms for this file may be
10 *  found in the file LICENSE in this distribution or at
11 *  http://www.rtems.org/license/LICENSE.
12 */
13
14#ifdef HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <stdio.h>
19#include <time.h>
20#include <termios.h>
21#include <string.h>
22#include <stdlib.h>
23#include <ctype.h>
24#include <sys/stat.h>
25#include <unistd.h>
26#include <errno.h>
27
28
29#include <rtems.h>
30#include <rtems/shell.h>
31#include <rtems/shellconfig.h>
32#include <rtems/libio_.h>
33#include "internal.h"
34
35/*
36 * Common linked list of shell commands.
37 *
38 * Because the help report is very long, there is a topic for each command.
39 *
40 * Help list the topics
41 *   help [topic] list the commands for the topic
42 *   help [command] help for the command
43 *
44 */
45
46rtems_shell_cmd_t   * rtems_shell_first_cmd;
47rtems_shell_topic_t * rtems_shell_first_topic;
48
49/*
50 *  Find the topic from the set of topics registered.
51 */
52rtems_shell_topic_t * rtems_shell_lookup_topic(const char * topic) {
53  rtems_shell_topic_t * shell_topic;
54  shell_topic=rtems_shell_first_topic;
55
56  while (shell_topic) {
57    if (!strcmp(shell_topic->topic,topic))
58      return shell_topic;
59    shell_topic=shell_topic->next;
60  }
61  return NULL;
62}
63
64/*
65 *  Add a new topic to the list of topics
66 */
67static rtems_shell_topic_t * rtems_shell_add_topic(const char * topic) {
68  rtems_shell_topic_t * current,*aux;
69
70  if (!rtems_shell_first_topic) {
71    aux = malloc(sizeof(rtems_shell_topic_t));
72    aux->topic = topic;
73    aux->next  = NULL;
74    return rtems_shell_first_topic = aux;
75  }
76  current=rtems_shell_first_topic;
77  if (!strcmp(topic,current->topic))
78    return current;
79
80  while (current->next) {
81    if (!strcmp(topic,current->next->topic))
82      return current->next;
83    current=current->next;
84  }
85  aux = malloc(sizeof(rtems_shell_topic_t));
86  aux->topic = topic;
87  aux->next = NULL;
88  current->next = aux;
89  return aux;
90}
91
92/*
93 *  Find the command in the set
94 */
95rtems_shell_cmd_t * rtems_shell_lookup_cmd(const char * cmd) {
96  rtems_shell_cmd_t * shell_cmd;
97  shell_cmd=rtems_shell_first_cmd;
98  while (shell_cmd) {
99   if (!strcmp(shell_cmd->name,cmd)) return shell_cmd;
100   shell_cmd=shell_cmd->next;
101  };
102  return NULL;
103}
104
105/*
106 *  Add a command structure to the set of known commands
107 */
108rtems_shell_cmd_t *rtems_shell_add_cmd_struct(
109  rtems_shell_cmd_t *shell_cmd
110)
111{
112  rtems_shell_cmd_t **next_ptr = &rtems_shell_first_cmd;
113  rtems_shell_cmd_t *existing;
114
115  /*
116   * Iterate through all commands and check if a command with this name is
117   * already present.
118   */
119  while ((existing = *next_ptr) != NULL) {
120    if (strcmp(existing->name, shell_cmd->name) == 0)
121      return NULL;
122
123    next_ptr = &existing->next;
124  }
125
126  /* Ensure that the user can read and execute commands */
127  shell_cmd->mode |= S_IRUSR | S_IXUSR;
128
129  /* Append */
130  *next_ptr = shell_cmd;
131
132  rtems_shell_add_topic( shell_cmd->topic );
133
134  return shell_cmd;
135}
136
137/*
138 *  Add a command as a set of arguments to the set and
139 *  allocate the command structure on the fly.
140 */
141rtems_shell_cmd_t * rtems_shell_add_cmd(
142  const char            *name,
143  const char            *topic,
144  const char            *usage,
145  rtems_shell_command_t  command
146)
147{
148  rtems_shell_cmd_t *shell_cmd = NULL;
149  char *my_name = NULL;
150  char *my_topic = NULL;
151  char *my_usage = NULL;
152
153  /* Reject empty commands */
154  if (name == NULL || command == NULL) {
155    return NULL;
156  }
157
158  /* Allocate command stucture */
159  shell_cmd = (rtems_shell_cmd_t *) calloc(1, sizeof(*shell_cmd));
160  if (shell_cmd == NULL) {
161    return NULL;
162  }
163
164  /* Allocate strings */
165  my_name  = strdup(name);
166  my_topic = strdup(topic);
167  my_usage = strdup(usage);
168
169  /* Assign values */
170  shell_cmd->name    = my_name;
171  shell_cmd->topic   = my_topic;
172  shell_cmd->usage   = my_usage;
173  shell_cmd->command = command;
174
175  if (rtems_shell_add_cmd_struct(shell_cmd) == NULL) {
176    /* Something is wrong, free allocated resources */
177    free(my_usage);
178    free(my_topic);
179    free(my_name);
180    free(shell_cmd);
181
182    return NULL;
183  }
184
185  return shell_cmd;
186}
187
188/* ----------------------------------------------- *
189 * you can make an alias for every command.
190 * ----------------------------------------------- */
191rtems_shell_cmd_t *rtems_shell_alias_cmd(
192  const char *cmd,
193  const char *alias
194)
195{
196  rtems_shell_cmd_t *shell_cmd, *shell_aux;
197
198  shell_aux = (rtems_shell_cmd_t *) NULL;
199
200  if (alias) {
201    shell_aux = rtems_shell_lookup_cmd(alias);
202    if (shell_aux != NULL) {
203      return NULL;
204    }
205    shell_cmd = rtems_shell_lookup_cmd(cmd);
206    if (shell_cmd != NULL) {
207      shell_aux = rtems_shell_add_cmd(
208         alias,
209         shell_cmd->topic,
210         shell_cmd->usage,
211         shell_cmd->command
212      );
213      if (shell_aux) {
214        shell_aux->alias = shell_cmd;
215        shell_aux->mode = shell_cmd->mode;
216        shell_aux->uid = shell_cmd->uid;
217        shell_aux->gid = shell_cmd->gid;
218      }
219    }
220  }
221  return shell_aux;
222}
223
224bool rtems_shell_can_see_cmd(const rtems_shell_cmd_t *shell_cmd)
225{
226  return rtems_filesystem_check_access(
227    RTEMS_FS_PERMS_READ,
228    shell_cmd->mode,
229    shell_cmd->uid,
230    shell_cmd->gid
231  );
232}
233
234static bool rtems_shell_can_execute_cmd(const rtems_shell_cmd_t *shell_cmd)
235{
236  return rtems_filesystem_check_access(
237    RTEMS_FS_PERMS_EXEC,
238    shell_cmd->mode,
239    shell_cmd->uid,
240    shell_cmd->gid
241  );
242}
243
244int rtems_shell_execute_cmd(const char *cmd, int argc, char *argv[])
245{
246  rtems_shell_cmd_t *shell_cmd;
247
248  if (argv[0] == NULL) {
249    return -1;
250  }
251
252  shell_cmd = rtems_shell_lookup_cmd(argv[0]);
253
254  if (shell_cmd != NULL && !rtems_shell_can_see_cmd(shell_cmd)) {
255    shell_cmd = NULL;
256  }
257
258  if (shell_cmd == NULL) {
259    return rtems_shell_script_file(argc, argv);
260  } else if (rtems_shell_can_execute_cmd(shell_cmd)) {
261    return shell_cmd->command(argc, argv);
262  } else {
263    fprintf(stderr, "%s: Permission denied\n", cmd);
264
265    return -1;
266  }
267}
Note: See TracBrowser for help on using the repository browser.