xref: /freebsd/sys/kern/imgact_shell.c (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
1 /*
2  * Copyright (c) 1993, David Greenman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by David Greenman
16  * 4. The name of the developer may be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	$Id: imgact_shell.c,v 1.9 1995/11/06 12:52:31 davidg Exp $
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sysproto.h>
37 #include <sys/resourcevar.h>
38 #include <sys/exec.h>
39 #include <sys/imgact.h>
40 #include <sys/kernel.h>
41 #include <machine/endian.h>
42 
43 #if BYTE_ORDER == LITTLE_ENDIAN
44 #define SHELLMAGIC	0x2123 /* #! */
45 #else
46 #define SHELLMAGIC	0x2321
47 #endif
48 
49 #define MAXSHELLCMDLEN	64
50 
51 static int	exec_shell_imgact __P((struct image_params *imgp));
52 
53 /*
54  * Shell interpreter image activator. A interpreter name beginning
55  *	at imgp->stringbase is the minimal successful exit requirement.
56  */
57 static int
58 exec_shell_imgact(imgp)
59 	struct image_params *imgp;
60 {
61 	const char *image_header = imgp->image_header;
62 	const char *ihp, *line_endp;
63 	char *interp;
64 
65 	/* a shell script? */
66 	if (((short *) image_header)[0] != SHELLMAGIC)
67 		return(-1);
68 
69 	/*
70 	 * Don't allow a shell script to be the shell for a shell
71 	 *	script. :-)
72 	 */
73 	if (imgp->interpreted)
74 		return(ENOEXEC);
75 
76 	imgp->interpreted = 1;
77 
78 	/*
79 	 * Copy shell name and arguments from image_header into string
80 	 *	buffer.
81 	 */
82 
83 	/*
84 	 * Find end of line; return if the line > MAXSHELLCMDLEN long.
85 	 */
86 	for (ihp = &image_header[2]; *ihp != '\n'; ++ihp) {
87 		if (ihp >= &image_header[MAXSHELLCMDLEN])
88 			return(ENOEXEC);
89 	}
90 	line_endp = ihp;
91 
92 	/* reset for another pass */
93 	ihp = &image_header[2];
94 
95 	/* Skip over leading spaces - until the interpreter name */
96 	while ((*ihp == ' ') || (*ihp == '\t')) ihp++;
97 
98 	/* copy the interpreter name */
99 	interp = imgp->interpreter_name;
100 	while ((ihp < line_endp) && (*ihp != ' ') && (*ihp != '\t'))
101 		*interp++ = *ihp++;
102 	*interp = '\0';
103 
104 	/* Disallow a null interpreter filename */
105 	if (*imgp->interpreter_name == '\0')
106 		return(ENOEXEC);
107 
108 	/* reset for another pass */
109 	ihp = &image_header[2];
110 
111 	/* copy the interpreter name and arguments */
112 	while (ihp < line_endp) {
113 		/* Skip over leading spaces */
114 		while ((*ihp == ' ') || (*ihp == '\t')) ihp++;
115 
116 		if (ihp < line_endp) {
117 			/*
118 			 * Copy to end of token. No need to watch stringspace
119 			 *	because this is at the front of the string buffer
120 			 *	and the maximum shell command length is tiny.
121 			 */
122 			while ((ihp < line_endp) && (*ihp != ' ') && (*ihp != '\t')) {
123 				*imgp->stringp++ = *ihp++;
124 				imgp->stringspace--;
125 			}
126 
127 			*imgp->stringp++ = 0;
128 			imgp->stringspace--;
129 
130 			imgp->argc++;
131 		}
132 	}
133 
134 	/* set argv[0] to point to original file name */
135 	suword(imgp->uap->argv, (int)imgp->uap->fname);
136 
137 	return(0);
138 }
139 
140 /*
141  * Tell kern_execve.c about it, with a little help from the linker.
142  * Since `const' objects end up in the text segment, TEXT_SET is the
143  * correct directive to use.
144  */
145 static const struct execsw shell_execsw = { exec_shell_imgact, "#!" };
146 TEXT_SET(execsw_set, shell_execsw);
147