Thursday, October 22, 2009
Simple shell implementation
Just done a simple exercise in C programming and wrote a trivial implementation of Unix-style "shell" utility in C.
The source is here: http://code.google.com/p/inet-lab/source/browse/trunk/utils/Misc/myshell.c
All it does is basically accepts a user's command, parses it into "utility" and "arguments", does a basic sanity check, finds utility in $PATH, forks a child process and executes an utility with arguments via execv().
It also optionally supports GNU readline library and could be compiled e.g. with gcc like that :
gcc -g --pedantic –Wall [-DRL] -o myshell myshell.c [-lreadline]
Double quotes can be used as part of command line to pass a single argument with spaces. Other than that, no other shell substitution or expansion is done; in particular, there is no way to escape a double quote to pass it as part of an argument.
Why would anyone need this primitive shell?
Well, other that a simple training in a few basic C-programming concepts (also serving as a basic GNU readline example), it allows one to create a "back door" bypassing system security. Indeed, if such a "shell" is owned by root and is granted setuid privilege, it allows a regular user to execute any administrative/privileged command, a goal which cannot be accomplished with either regular system shells (which all have built-in protection against setuid flag) or any tools written in a scripting language; it must be a native system executable.