2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
1993-12-20 16:16:46 +00:00
|
|
|
* Copyright (c) 1993, David Greenman
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1995-09-08 13:24:33 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1993-12-20 16:16:46 +00:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-08-13 03:50:34 +00:00
|
|
|
#include <sys/param.h>
|
1998-10-16 03:55:01 +00:00
|
|
|
#include <sys/systm.h>
|
1995-10-08 00:06:22 +00:00
|
|
|
#include <sys/sysproto.h>
|
1994-05-25 09:21:21 +00:00
|
|
|
#include <sys/exec.h>
|
1994-08-13 03:50:34 +00:00
|
|
|
#include <sys/imgact.h>
|
|
|
|
#include <sys/kernel.h>
|
1993-12-20 16:16:46 +00:00
|
|
|
|
1993-12-20 19:31:41 +00:00
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
1993-12-20 16:16:46 +00:00
|
|
|
#define SHELLMAGIC 0x2123 /* #! */
|
1993-12-20 19:31:41 +00:00
|
|
|
#else
|
|
|
|
#define SHELLMAGIC 0x2321
|
|
|
|
#endif
|
|
|
|
|
1993-12-20 16:16:46 +00:00
|
|
|
/*
|
2003-01-01 18:49:04 +00:00
|
|
|
* Shell interpreter image activator. An interpreter name beginning
|
2005-01-29 23:12:00 +00:00
|
|
|
* at imgp->args->begin_argv is the minimal successful exit requirement.
|
1993-12-20 16:16:46 +00:00
|
|
|
*/
|
2000-04-26 20:58:40 +00:00
|
|
|
int
|
1995-11-06 12:52:37 +00:00
|
|
|
exec_shell_imgact(imgp)
|
|
|
|
struct image_params *imgp;
|
1993-12-20 16:16:46 +00:00
|
|
|
{
|
1995-11-06 12:52:37 +00:00
|
|
|
const char *image_header = imgp->image_header;
|
2005-01-29 23:12:00 +00:00
|
|
|
const char *ihp;
|
2005-01-30 06:43:17 +00:00
|
|
|
int error, offset;
|
|
|
|
size_t length;
|
1993-12-20 16:16:46 +00:00
|
|
|
|
|
|
|
/* a shell script? */
|
1996-08-31 16:52:44 +00:00
|
|
|
if (((const short *) image_header)[0] != SHELLMAGIC)
|
1993-12-20 16:16:46 +00:00
|
|
|
return(-1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't allow a shell script to be the shell for a shell
|
|
|
|
* script. :-)
|
|
|
|
*/
|
1995-11-06 12:52:37 +00:00
|
|
|
if (imgp->interpreted)
|
1993-12-20 16:16:46 +00:00
|
|
|
return(ENOEXEC);
|
|
|
|
|
1995-11-06 12:52:37 +00:00
|
|
|
imgp->interpreted = 1;
|
1993-12-20 16:16:46 +00:00
|
|
|
|
|
|
|
/*
|
2005-01-29 23:12:00 +00:00
|
|
|
* Figure out the number of bytes that need to be reserved in the
|
|
|
|
* argument string to copy the contents of the interpreter's command
|
|
|
|
* line into the argument string.
|
1993-12-20 16:16:46 +00:00
|
|
|
*/
|
2005-01-29 23:12:00 +00:00
|
|
|
ihp = &image_header[2];
|
|
|
|
offset = 0;
|
|
|
|
while (ihp < &image_header[MAXSHELLCMDLEN]) {
|
|
|
|
/* Skip any whitespace */
|
|
|
|
while ((*ihp == ' ') || (*ihp == '\t')) {
|
|
|
|
ihp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of line? */
|
|
|
|
if ((*ihp == '\n') || (*ihp == '#'))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Found a token */
|
|
|
|
while ((*ihp != ' ') && (*ihp != '\t') && (*ihp != '\n') &&
|
|
|
|
(*ihp != '#')) {
|
|
|
|
offset++;
|
|
|
|
ihp++;
|
|
|
|
}
|
|
|
|
/* Include terminating nulls in the offset */
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the script gives a null line as the interpreter, we bail */
|
|
|
|
if (offset == 0)
|
|
|
|
return (ENOEXEC);
|
|
|
|
|
|
|
|
/* Check that we aren't too big */
|
|
|
|
if (offset > MAXSHELLCMDLEN)
|
|
|
|
return (ENAMETOOLONG);
|
1993-12-20 16:16:46 +00:00
|
|
|
|
|
|
|
/*
|
2005-01-29 23:12:00 +00:00
|
|
|
* The full path name of the original script file must be tagged
|
|
|
|
* onto the end, adjust the offset to deal with it.
|
|
|
|
*
|
|
|
|
* The original argv[0] is being replaced, set 'length' to the number
|
|
|
|
* of bytes being removed. So 'offset' is the number of bytes being
|
|
|
|
* added and 'length' is the number of bytes being removed.
|
1993-12-20 16:16:46 +00:00
|
|
|
*/
|
2005-01-29 23:12:00 +00:00
|
|
|
offset += strlen(imgp->args->fname) + 1; /* add fname */
|
|
|
|
length = (imgp->args->argc == 0) ? 0 :
|
|
|
|
strlen(imgp->args->begin_argv) + 1; /* bytes to delete */
|
1993-12-20 16:16:46 +00:00
|
|
|
|
2005-01-29 23:12:00 +00:00
|
|
|
if (offset - length > imgp->args->stringspace)
|
|
|
|
return (E2BIG);
|
1993-12-20 16:16:46 +00:00
|
|
|
|
2005-01-29 23:12:00 +00:00
|
|
|
bcopy(imgp->args->begin_argv + length, imgp->args->begin_argv + offset,
|
|
|
|
imgp->args->endp - (imgp->args->begin_argv + length));
|
1993-12-20 16:16:46 +00:00
|
|
|
|
2005-01-29 23:12:00 +00:00
|
|
|
offset -= length; /* calculate actual adjustment */
|
|
|
|
imgp->args->begin_envv += offset;
|
|
|
|
imgp->args->endp += offset;
|
|
|
|
imgp->args->stringspace -= offset;
|
1993-12-20 16:16:46 +00:00
|
|
|
|
2005-01-29 23:12:00 +00:00
|
|
|
/*
|
|
|
|
* If there were no arguments then we've added one, otherwise
|
|
|
|
* decr argc remove old argv[0], incr argc for fname add, net 0
|
|
|
|
*/
|
|
|
|
if (imgp->args->argc == 0)
|
|
|
|
imgp->args->argc = 1;
|
1993-12-20 16:16:46 +00:00
|
|
|
|
2005-01-29 23:12:00 +00:00
|
|
|
/*
|
|
|
|
* Loop through the interpreter name yet again, copying as
|
|
|
|
* we go.
|
|
|
|
*/
|
1993-12-20 16:16:46 +00:00
|
|
|
ihp = &image_header[2];
|
2005-01-29 23:12:00 +00:00
|
|
|
offset = 0;
|
|
|
|
while (ihp < &image_header[MAXSHELLCMDLEN]) {
|
|
|
|
/* Skip whitespace */
|
|
|
|
while ((*ihp == ' ' || *ihp == '\t')) {
|
|
|
|
ihp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of line? */
|
|
|
|
if ((*ihp == '\n') || (*ihp == '#'))
|
|
|
|
break;
|
1993-12-20 16:16:46 +00:00
|
|
|
|
2005-01-29 23:12:00 +00:00
|
|
|
/* Found a token, copy it */
|
|
|
|
while ((*ihp != ' ') && (*ihp != '\t') &&
|
|
|
|
(*ihp != '\n') && (*ihp != '#')) {
|
|
|
|
imgp->args->begin_argv[offset++] = *ihp++;
|
1993-12-20 16:16:46 +00:00
|
|
|
}
|
2005-01-29 23:12:00 +00:00
|
|
|
imgp->args->begin_argv[offset++] = '\0';
|
|
|
|
imgp->args->argc++;
|
1993-12-20 16:16:46 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 23:12:00 +00:00
|
|
|
/*
|
|
|
|
* Finally, add the filename onto the end for the interpreter to
|
|
|
|
* use and copy the interpreter's name to imgp->interpreter_name
|
|
|
|
* for exec to use.
|
|
|
|
*/
|
|
|
|
error = copystr(imgp->args->fname, imgp->args->buf + offset,
|
|
|
|
imgp->args->stringspace, &length);
|
|
|
|
|
|
|
|
if (error == 0)
|
|
|
|
error = copystr(imgp->args->begin_argv, imgp->interpreter_name,
|
|
|
|
MAXSHELLCMDLEN, &length);
|
1993-12-20 16:16:46 +00:00
|
|
|
|
2005-01-29 23:12:00 +00:00
|
|
|
return (error);
|
1993-12-20 16:16:46 +00:00
|
|
|
}
|
1993-12-20 19:31:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell kern_execve.c about it, with a little help from the linker.
|
|
|
|
*/
|
1999-01-29 22:59:43 +00:00
|
|
|
static struct execsw shell_execsw = { exec_shell_imgact, "#!" };
|
1998-10-16 03:55:01 +00:00
|
|
|
EXEC_SET(shell, shell_execsw);
|