imgact_shell.c revision 147151
1139804Simp/*-
2885Swollman * Copyright (c) 1993, David Greenman
3885Swollman * All rights reserved.
4885Swollman *
5885Swollman * Redistribution and use in source and binary forms, with or without
6885Swollman * modification, are permitted provided that the following conditions
7885Swollman * are met:
8885Swollman * 1. Redistributions of source code must retain the above copyright
9885Swollman *    notice, this list of conditions and the following disclaimer.
10885Swollman * 2. Redistributions in binary form must reproduce the above copyright
11885Swollman *    notice, this list of conditions and the following disclaimer in the
12885Swollman *    documentation and/or other materials provided with the distribution.
13885Swollman *
14885Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15885Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16885Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1710625Sdg * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18885Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19885Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20885Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21885Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22885Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23885Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24885Swollman * SUCH DAMAGE.
25885Swollman */
26885Swollman
27116182Sobrien#include <sys/cdefs.h>
28116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/imgact_shell.c 147151 2005-06-09 00:27:02Z gad $");
29116182Sobrien
302056Swollman#include <sys/param.h>
31142448Ssobomax#include <sys/vnode.h>
32142448Ssobomax#include <sys/proc.h>
3340435Speter#include <sys/systm.h>
3411332Sswallace#include <sys/sysproto.h>
351549Srgrimes#include <sys/exec.h>
362056Swollman#include <sys/imgact.h>
372056Swollman#include <sys/kernel.h>
38885Swollman
39886Swollman#if BYTE_ORDER == LITTLE_ENDIAN
40885Swollman#define SHELLMAGIC	0x2123 /* #! */
41886Swollman#else
42886Swollman#define SHELLMAGIC	0x2321
43886Swollman#endif
44886Swollman
45885Swollman/*
46146731Sgad * At the time of this writing, MAXSHELLCMDLEN == PAGE_SIZE.  This is
47146731Sgad * significant because the caller has only mapped in one page of the
48146731Sgad * file we're reading.  This code should be changed to know how to
49146731Sgad * read in the second page, but I'm not doing that just yet...
50885Swollman */
51146731Sgad#if MAXSHELLCMDLEN > PAGE_SIZE
52146731Sgad#error "MAXSHELLCMDLEN is larger than a single page!"
53146731Sgad#endif
54146731Sgad
55146731Sgad/**
56146731Sgad * Shell interpreter image activator. An interpreter name beginning at
57146731Sgad * imgp->args->begin_argv is the minimal successful exit requirement.
58146731Sgad *
59146731Sgad * If the given file is a shell-script, then the first line will start
60146731Sgad * with the two characters `#!' (aka SHELLMAGIC), followed by the name
61146731Sgad * of the shell-interpreter to run, followed by zero or more tokens.
62146731Sgad *
63146731Sgad * The interpreter is then started up such that it will see:
64146731Sgad *    arg[0] -> The name of interpreter as specified after `#!' in the
65146731Sgad *		first line of the script.  The interpreter name must
66146731Sgad *		not be longer than MAXSHELLCMDLEN bytes.
67146731Sgad *    arg[1] -> *If* there are any additional tokens on the first line,
68146731Sgad *		then we add a new arg[1], which is a copy of the rest of
69146731Sgad *		that line.  The copy starts at the first token after the
70146731Sgad *		interpreter name.  We leave it to the interpreter to
71146731Sgad *		parse the tokens in that value.
72146731Sgad *    arg[x] -> the full pathname of the script.  This will either be
73146731Sgad *		arg[2] or arg[1], depending on whether or not tokens
74146731Sgad *		were found after the interpreter name.
75146731Sgad *  arg[x+1] -> all the arguments that were specified on the original
76146731Sgad *		command line.
77146731Sgad *
78146731Sgad * This processing is described in the execve(2) man page.
79146731Sgad */
80146731Sgad
81146731Sgad/*
82146731Sgad * HISTORICAL NOTE: From 1993 to mid-2005, FreeBSD parsed out the tokens as
83146731Sgad * found on the first line of the script, and setup each token as a separate
84146731Sgad * value in arg[].  This extra processing did not match the behavior of other
85146731Sgad * OS's, and caused a few subtle problems.  For one, it meant the kernel was
86146731Sgad * deciding how those values should be parsed (wrt characters for quoting or
87146731Sgad * comments, etc), while the interpreter might have other rules for parsing.
88146731Sgad * It also meant the interpreter had no way of knowing which arguments came
89146731Sgad * from the first line of the shell script, and which arguments were specified
90147151Sgad * by the user on the command line.  That extra processing was dropped in the
91147151Sgad * 6.x branch on May 28, 2005 (matching __FreeBSD_version 600029).
92146731Sgad */
9359663Sdillonint
9412130Sdgexec_shell_imgact(imgp)
9512130Sdg	struct image_params *imgp;
96885Swollman{
9712130Sdg	const char *image_header = imgp->image_header;
98146731Sgad	const char *ihp, *interpb, *interpe, *maxp, *optb, *opte;
99141028Ssobomax	int error, offset;
100142448Ssobomax	size_t length, clength;
101142448Ssobomax	struct vattr vattr;
102885Swollman
103885Swollman	/* a shell script? */
10417974Sbde	if (((const short *) image_header)[0] != SHELLMAGIC)
105885Swollman		return(-1);
106885Swollman
107885Swollman	/*
108885Swollman	 * Don't allow a shell script to be the shell for a shell
109885Swollman	 *	script. :-)
110885Swollman	 */
11112130Sdg	if (imgp->interpreted)
112885Swollman		return(ENOEXEC);
113885Swollman
11412130Sdg	imgp->interpreted = 1;
115885Swollman
116885Swollman	/*
117142448Ssobomax	 * At this point we have the first page of the file mapped.
118142448Ssobomax	 * However, we don't know how far into the page the contents are
119142448Ssobomax	 * valid -- the actual file might be much shorter than the page.
120142448Ssobomax	 * So find out the file size.
121142448Ssobomax 	 */
122142448Ssobomax	error = VOP_GETATTR(imgp->vp, &vattr, imgp->proc->p_ucred, curthread);
123142448Ssobomax	if (error)
124142448Ssobomax		return (error);
125142448Ssobomax
126142448Ssobomax	/*
127146731Sgad	 * Copy shell name and arguments from image_header into a string
128146731Sgad	 *	buffer.  Remember that the caller has mapped only the
129146731Sgad	 *	first page of the file into memory.
130146731Sgad	 */
131146731Sgad	clength = (vattr.va_size > PAGE_SIZE) ? PAGE_SIZE : vattr.va_size;
132146731Sgad
133146731Sgad	maxp = &image_header[clength];
134146731Sgad	ihp = &image_header[2];
135146731Sgad
136146731Sgad	/*
137146731Sgad	 * Find the beginning and end of the interpreter_name.  If the
138146731Sgad	 * line does not include any interpreter, or if the name which
139146731Sgad	 * was found is too long, we bail out.
140146731Sgad	 */
141146731Sgad	while (ihp < maxp && ((*ihp == ' ') || (*ihp == '\t')))
142146731Sgad		ihp++;
143146731Sgad	interpb = ihp;
144146731Sgad	while (ihp < maxp && ((*ihp != ' ') && (*ihp != '\t') && (*ihp != '\n')
145146731Sgad	    && (*ihp != '\0')))
146146731Sgad		ihp++;
147146731Sgad	interpe = ihp;
148146731Sgad	if (interpb == interpe)
149146731Sgad		return (ENOEXEC);
150146731Sgad	if ((interpe - interpb) >= MAXSHELLCMDLEN)
151146731Sgad		return (ENAMETOOLONG);
152885Swollman
153140992Ssobomax	/*
154146731Sgad	 * Find the beginning of the options (if any), and the end-of-line.
155146731Sgad	 * Then trim the trailing blanks off the value.  Note that some
156146731Sgad	 * other operating systems do *not* trim the trailing whitespace...
157146731Sgad	 */
158146731Sgad	while (ihp < maxp && ((*ihp == ' ') || (*ihp == '\t')))
159146731Sgad		ihp++;
160146731Sgad	optb = ihp;
161146731Sgad	while (ihp < maxp && ((*ihp != '\n') && (*ihp != '\0')))
162146731Sgad		ihp++;
163146731Sgad	opte = ihp;
164146731Sgad	while (--ihp > interpe && ((*ihp == ' ') || (*ihp == '\t')))
165146731Sgad		opte = ihp;
166146731Sgad
167146731Sgad	/*
168146731Sgad	 * We need to "pop" (remove) the present value of arg[0], and "push"
169146731Sgad	 * either two or three new values in the arg[] list.  To do this,
170146731Sgad	 * we first shift all the other values in the `begin_argv' area to
171146731Sgad	 * provide the exact amount of room for the values added.  Set up
172146731Sgad	 * `offset' as the number of bytes to be added to the `begin_argv'
173146731Sgad	 * area, and 'length' as the number of bytes being removed.
174146731Sgad	 */
175146731Sgad	offset = interpe - interpb + 1;			/* interpreter */
176146731Sgad	if (opte != optb)				/* options (if any) */
177146731Sgad		offset += opte - optb + 1;
178146731Sgad	offset += strlen(imgp->args->fname) + 1;	/* fname of script */
179146731Sgad	length = (imgp->args->argc == 0) ? 0 :
180146731Sgad	    strlen(imgp->args->begin_argv) + 1;		/* bytes to delete */
181146731Sgad
182146731Sgad	if (offset - length > imgp->args->stringspace)
183146731Sgad		return (E2BIG);
184146731Sgad
185146731Sgad	bcopy(imgp->args->begin_argv + length, imgp->args->begin_argv + offset,
186146731Sgad	    imgp->args->endp - (imgp->args->begin_argv + length));
187146731Sgad
188146731Sgad	offset -= length;		/* calculate actual adjustment */
189146731Sgad	imgp->args->begin_envv += offset;
190146731Sgad	imgp->args->endp += offset;
191146731Sgad	imgp->args->stringspace -= offset;
192146731Sgad
193146731Sgad	/*
194146731Sgad	 * If there was no arg[0] when we started, then the interpreter_name
195146731Sgad	 * is adding an argument (instead of replacing the arg[0] we started
196146731Sgad	 * with).  And we're always adding an argument when we include the
197146731Sgad	 * full pathname of the original script.
198146731Sgad	 */
199146731Sgad	if (imgp->args->argc == 0)
200146731Sgad		imgp->args->argc = 1;
201146731Sgad	imgp->args->argc++;
202146731Sgad
203146731Sgad	/*
204146731Sgad	 * The original arg[] list has been shifted appropriately.  Copy in
205146731Sgad	 * the interpreter name and options-string.
206146731Sgad	 */
207146731Sgad	length = interpe - interpb;
208146731Sgad	bcopy(interpb, imgp->args->buf, length);
209146731Sgad	*(imgp->args->buf + length) = '\0';
210146731Sgad	offset = length + 1;
211146731Sgad	if (opte != optb) {
212146731Sgad		length = opte - optb;
213146731Sgad		bcopy(optb, imgp->args->buf + offset, length);
214146731Sgad		*(imgp->args->buf + offset + length) = '\0';
215146731Sgad		offset += length + 1;
216146731Sgad		imgp->args->argc++;
217146731Sgad	}
218146731Sgad
219146731Sgad	/*
220140992Ssobomax	 * Finally, add the filename onto the end for the interpreter to
221140992Ssobomax	 * use and copy the interpreter's name to imgp->interpreter_name
222140992Ssobomax	 * for exec to use.
223140992Ssobomax	 */
224140992Ssobomax	error = copystr(imgp->args->fname, imgp->args->buf + offset,
225140992Ssobomax	    imgp->args->stringspace, &length);
226885Swollman
227140992Ssobomax	if (error == 0)
228140992Ssobomax		error = copystr(imgp->args->begin_argv, imgp->interpreter_name,
229140992Ssobomax		    MAXSHELLCMDLEN, &length);
230140992Ssobomax
231140992Ssobomax	return (error);
232885Swollman}
233886Swollman
234886Swollman/*
235886Swollman * Tell kern_execve.c about it, with a little help from the linker.
236886Swollman */
23743402Sdillonstatic struct execsw shell_execsw = { exec_shell_imgact, "#!" };
23840435SpeterEXEC_SET(shell, shell_execsw);
239