source: rtems/cpukit/libmisc/shell/shell.h @ ffd5285

4.115
Last change on this file since ffd5285 was ffd5285, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 18, 2014 at 10:07:36 AM

shell: Inherit UID and GID if no login check

Use the UID and GID of the executing user for the real and effective UID
and GID of the shell task in case no login check is present. This
prevents privilege escalation via shell scripts.

  • Property mode set to 100644
File size: 8.3 KB
Line 
1/**
2 * @file rtems/shell.h
3 *
4 * @brief Instantatiate a New Terminal Shell
5 */
6
7/*
8 *  Author:
9 *
10 *   WORK: fernando.ruiz@ctv.es
11 *   HOME: correo@fernando-ruiz.com
12 *
13 *   Thanks at:
14 *    Chris Johns
15 */
16
17#ifndef __RTEMS_SHELL_H__
18#define __RTEMS_SHELL_H__
19
20#include <sys/types.h>
21#include <rtems.h>
22#include <stdio.h>
23#include <termios.h>
24#include <rtems/fs.h>
25#include <rtems/libio.h>
26#include <rtems/chain.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/*
33 * Some key labels to define special keys.
34 */
35
36#define RTEMS_SHELL_KEYS_EXTENDED    (0x8000)
37#define RTEMS_SHELL_KEYS_NORMAL_MASK (0x00ff)
38#define RTEMS_SHELL_KEYS_INS         (0)
39#define RTEMS_SHELL_KEYS_DEL         (1)
40#define RTEMS_SHELL_KEYS_UARROW      (2)
41#define RTEMS_SHELL_KEYS_DARROW      (3)
42#define RTEMS_SHELL_KEYS_LARROW      (4)
43#define RTEMS_SHELL_KEYS_RARROW      (5)
44#define RTEMS_SHELL_KEYS_HOME        (6)
45#define RTEMS_SHELL_KEYS_END         (7)
46#define RTEMS_SHELL_KEYS_F1          (8)
47#define RTEMS_SHELL_KEYS_F2          (9)
48#define RTEMS_SHELL_KEYS_F3          (10)
49#define RTEMS_SHELL_KEYS_F4          (11)
50#define RTEMS_SHELL_KEYS_F5          (12)
51#define RTEMS_SHELL_KEYS_F6          (13)
52#define RTEMS_SHELL_KEYS_F7          (14)
53#define RTEMS_SHELL_KEYS_F8          (15)
54#define RTEMS_SHELL_KEYS_F9          (16)
55#define RTEMS_SHELL_KEYS_F10         (17)
56
57typedef bool (*rtems_shell_login_check_t)(
58  const char * /* user */,
59  const char * /* passphrase */
60);
61
62extern bool rtems_shell_login_prompt(
63  FILE *in,
64  FILE *out,
65  const char *device,
66  rtems_shell_login_check_t check
67);
68
69extern bool rtems_shell_login_check(
70  const char *user,
71  const char *passphrase
72);
73
74typedef int (*rtems_shell_command_t)(int argc, char **argv);
75
76struct rtems_shell_cmd_tt;
77typedef struct rtems_shell_cmd_tt rtems_shell_cmd_t;
78
79struct rtems_shell_cmd_tt {
80  const char            *name;
81  const char            *usage;
82  const char            *topic;
83  rtems_shell_command_t  command;
84  rtems_shell_cmd_t     *alias;
85  rtems_shell_cmd_t     *next;
86};
87
88typedef struct {
89  const char *name;
90  const char *alias;
91} rtems_shell_alias_t;
92
93/*
94 * The return value has RTEMS_SHELL_KEYS_EXTENDED set if the key
95 * is extended, ie a special key.
96 */
97extern unsigned int rtems_shell_getchar(FILE *in);
98
99extern rtems_shell_cmd_t * rtems_shell_lookup_cmd(const char *cmd);
100
101extern rtems_shell_cmd_t *rtems_shell_add_cmd_struct(
102  rtems_shell_cmd_t *shell_cmd
103);
104
105rtems_shell_cmd_t * rtems_shell_add_cmd(
106  const char            *cmd,
107  const char            *topic,
108  const char            *usage,
109  rtems_shell_command_t  command
110);
111
112extern rtems_shell_cmd_t * rtems_shell_alias_cmd(
113  const char *cmd,
114  const char *alias
115);
116
117extern int rtems_shell_make_args(
118  char  *commandLine,
119  int   *argc_p,
120  char **argv_p,
121  int    max_args
122);
123
124extern int rtems_shell_cat_file(
125  FILE *out,
126  const char *name
127);
128
129extern void rtems_shell_write_file(
130  const char *name,
131  const char *content
132);
133
134extern int rtems_shell_script_file(
135  int    argc,
136  char **argv
137);
138
139/**
140 * Initialise the shell creating tasks to login and run the shell
141 * sessions.
142 *
143 * @param task_name Name of the shell task.
144 * @param task_stacksize The size of the stack. If 0 the default size is used.
145 * @param task_priority The priority the shell runs at.
146 * @param forever Repeat logins.
147 * @param wait Caller should block until shell exits.
148 * @param login_check User login check function, NULL disables login checks.
149 *
150 */
151extern rtems_status_code rtems_shell_init(
152  const char *task_name,
153  size_t task_stacksize,
154  rtems_task_priority task_priority,
155  const char *devname,
156  bool forever,
157  bool wait,
158  rtems_shell_login_check_t login_check
159);
160
161/**
162 * Run a shell script creating a shell tasks to execute the command under.
163 *
164 * @param task_name Name of the shell task.
165 * @param task_stacksize The size of the stack. If 0 the default size is used.
166 * @param task_priority The priority the shell runs at.
167 * @param input The file of commands. Can be 'stdin' to use stdin.
168 * @param output The output file to write commands to. Can be 'stdout',
169 *              'stderr' or '/dev/null'.
170 * @param output_append Append the output to the file or truncate the file.
171 *                      Create if it does not exist.
172 * @param wait Wait for the script to finish.
173 */
174extern rtems_status_code rtems_shell_script(
175  const char          *task_name,
176  size_t               task_stacksize,  /* 0 default*/
177  rtems_task_priority  task_priority,
178  const char          *input,
179  const char          *output,
180  bool                 output_append,
181  bool                 wait,
182  bool                 echo
183);
184
185/**
186 *  Private environment associated with each shell instance.
187 */
188typedef struct {
189  /** 'S','E','N','V': Shell Environment */
190  rtems_name magic;
191  const char *devname;
192  const char *taskname;
193  bool exit_shell; /* logout */
194  bool forever; /* repeat login */
195  int errorlevel;
196  bool echo;
197  char cwd[256];
198  const char *input;
199  const char *output;
200  bool output_append;
201  rtems_id wake_on_end;
202  rtems_shell_login_check_t login_check;
203
204  /**
205   * @brief The real and effective UID of the shell task in case no login check
206   * is present.
207   */
208  uid_t uid;
209
210  /**
211   * @brief The real and effective GID of the shell task in case no login check
212   * is present.
213   */
214  gid_t gid;
215} rtems_shell_env_t;
216
217bool rtems_shell_main_loop(
218  rtems_shell_env_t *rtems_shell_env
219);
220
221extern const rtems_shell_env_t rtems_global_shell_env;
222
223rtems_shell_env_t *rtems_shell_get_current_env(void);
224void rtems_shell_dup_current_env(rtems_shell_env_t *);
225
226/*
227 * The types of file systems we can mount. We have them broken out
228 * out like this so they can be configured by shellconfig.h. The
229 * mount command needs special treatment due to some file systems
230 * being dependent on the network stack and some not. If we had
231 * all possible file systems being included it would force the
232 * networking stack into the applcation and this may not be
233 * required.
234 */
235struct rtems_shell_filesystems_tt;
236typedef struct rtems_shell_filesystems_tt rtems_shell_filesystems_t;
237
238typedef int (*rtems_shell_filesystems_mounter_t)(
239  const char*                driver,
240  const char*                path,
241  rtems_shell_filesystems_t* fs,
242  rtems_filesystem_options_t options
243);
244
245struct rtems_shell_filesystems_tt {
246  rtems_chain_node                         link;
247  const char                              *name;
248  int                                      driver_needed;
249  const rtems_filesystem_operations_table *fs_ops;
250  rtems_shell_filesystems_mounter_t        mounter;
251};
252
253/**
254 *  This method dynamically builds the command line prompt string
255 *  and places it in @a prompt.
256 *
257 *  @param[in] shell_env is the shell execution environment
258 *  @param[in] prompt is a pointer to a string buffer area
259 *  @param[in] size is length of the prompt buffer area
260 *
261 *  @return This method fills in the memory pointed to by @a prompt.
262 *
263 *  @note An application specific implementation can be provided
264 *        by the user.
265 */
266extern void rtems_shell_get_prompt(
267  rtems_shell_env_t *shell_env,
268  char              *prompt,
269  size_t             size
270);
271
272/**
273 * Helper for the mount command.
274 *
275 * @param[in] driver The path to the driver.
276 * @param[in] path The path to mount on.
277 * @param[in] fs The file system definition.
278 * @param[in] options Special file system options.
279 */
280extern int rtems_shell_libc_mounter(
281  const char*                driver,
282  const char*                path,
283  rtems_shell_filesystems_t* fs,
284  rtems_filesystem_options_t options
285);
286
287/**
288 * Add a new file system mount configuration to the mount command.
289 *
290 * @param[in] fs The file system mount data.
291 */
292extern void rtems_shell_mount_add_fsys(rtems_shell_filesystems_t* fs);
293
294/**
295 * Delete file system mount configuration from the mount command.
296 *
297 * @param[in] fs The file system mount data to remove.
298 */
299extern void rtems_shell_mount_del_fsys(rtems_shell_filesystems_t* fs);
300
301typedef void (*rtems_shell_wait_for_input_notification)(
302  int fd,
303  int seconds_remaining,
304  void *arg
305);
306
307/**
308 * @brief Waits for input.
309 *
310 * @retval RTEMS_SUCCESSFUL Input detected.
311 * @retval RTEMS_TIMEOUT Timeout expired.
312 * @retval RTEMS_UNSATISFIED Cannot change or restore termios attributes.
313 */
314extern rtems_status_code rtems_shell_wait_for_input(
315  int fd,
316  int timeout_in_seconds,
317  rtems_shell_wait_for_input_notification notification,
318  void *notification_arg
319);
320
321extern int rtems_shell_main_monitor(int argc, char **argv);
322
323
324#ifdef __cplusplus
325}
326#endif
327
328#endif
Note: See TracBrowser for help on using the repository browser.