File: grap.c

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (168 lines) | stat: -rw-r--r-- 4,979 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* graplite - General execution wRAPper LITE!
 * Copyright (C) 1999 Lion Templin <lion@leonine.com>
 * FILE: graplite.c
 * VERSION : 0.1 (991111)
 *
 * $Id: grap.c 3588 2004-11-16 21:42:04Z cbayle $
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * GOTO FOREVER!
 *
 *	Coded on Northwest Airlines Flight 1065, CHI to MSP
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* grap is a wrapper designed to verify commands before passing them to system()
   or just reporting the accepted command.  grap will report an error if the
   input is invalid.  It checks for string lengths (prevent overflows),
   specific sets of options and commands.
   
   grap, in full force, is called as:  <grap> <option> "<command> <arguments
   list ... >" Where <grap> is this program, <option> is an optional trap
   for a single option (like "-e" or "-c" used by programs that call shells,
   subject to the approval list below), <command> is the command wished to
   be run (subject to the approval list below), and <arguments list .. > is
   the list of args passed to <command>.  All are optional, allowing for
   forms such as:
   	graplite -e "foo"   graplite "foo bar"  graplite -e "foo -c foo -f bar"
   	<g     ><o ><cmd>   <g     > <cmd/args> <g     ><o> <cmd/  args       >
   	
	<options> and <command> need to be exact matched to those in the
	acceptance list.  
*/

/* Define the locations of <option> <command> and <arguements list .. >
   on the command line.  0 is this program, begin at 1.  Note that 
   ARGS_ARGC takes everything FROM that position to the end of the
   arguments.

   Undefine any of these to not use them.
*/

#define	OPTION_ARGC		1
#define ARGS_ARGC		2

#define	ARGS_ARE_SINGLE_STRING

/* Define how the <arguements list .. > is checked.
   define ARGS_ALNUMOK for A-Za-z0-9 to be OK
   define any other chars in the string ARGS_CHAROK

   Turn both these off to accept everything.
   WARNING, might be able to bad things with
   shell special chars such as & ; , etc.
*/

#define MAXSTRLEN		256		/* maximum single string length
						   (no max on final command) */
/* Define what strings are acceptable in <option> */
char *options[] = 		{ "-c", "-e", NULL };

/* Define what strings are acceptable in <command>
   define an optional execution path CMD_PATH if desired */
char *commands[] = 		{ "cvs", "server", "svnserve", NULL };

#define MAXARGS		256

/* NO USER SERVICEABLE PARTS BELOW --------------------------------- */


#define	GRAP_TRUE		1
#define GRAP_FALSE		0
#define CMD_POS			0

int main(int argc, char *argv[]) {

	int i, j, n, argslen, flag;
	char *buf;
	char **args[MAXARGS];


	if(argc < 3) {
			/* printf("FATAL: %s bailed because not enough options.\n", argv[0]); */

			printf("\nWelcome to GForge SCM\n\n");
			printf("This is a restricted Shell Account\n");
			printf("You cannot execute anything here.\n\n");

			exit(1);
	}
	

	/* process the initial option (see options array) */

	i = -1;
	while((options[++i] != NULL) && strncmp(options[i], argv[OPTION_ARGC], MAXSTRLEN));
		if(options[i] == NULL || strlen(argv[OPTION_ARGC]) > MAXSTRLEN) {
			/* printf("FATAL: %s bailed because options didn't qualify.\n", argv[0]); */

			printf("\nWelcome to GForge SCM\n\n");
			printf("This is a restricted Shell Account\n");
			printf("You cannot execute anything here.\n\n");

			exit(1);
		}
	
	/* break single command and args string into seperate strings
	   in a char** for execvp() to use */

	i = 0;
	flag = GRAP_TRUE;
	buf = argv[ARGS_ARGC];

	j = CMD_POS;
	n = 0;

	while(buf[i] != 0 && j < MAXARGS) {
		if(buf[i] == ' ') {
			buf[i] = 0;
			flag = GRAP_TRUE;
		} else 
			if(flag) {
				args[j++] = (char **)&buf[i];
				flag = GRAP_FALSE;
				args[j] = NULL;
				n++;
			}
		i++;
	}

	/* check the command to insure it's in the acceptance list */

	i = -1;
	while((options[++i] != NULL) && strncmp(commands[i], args[CMD_POS], MAXSTRLEN));
	if(options[i] == NULL || strlen((char *)args[CMD_POS]) > MAXSTRLEN) {

	/* 	printf("FATAL: %s bailed because command didn't qualify.\n", args[CMD_POS]); */

		printf("\nWelcome to GForge SCM\n\n");
		printf("This is a restricted Shell Account\n");
		printf("You cannot execute anything here.\n\n");

		exit(1);
	}


	/* ok, the command is clear, exec() it */

	execvp((char *)args[CMD_POS], (char **)args);

}