File: cgiwrap.c

package info (click to toggle)
cgiwrap 3.5-3
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 356 kB
  • ctags: 115
  • sloc: sh: 3,954; ansic: 1,036; perl: 104; makefile: 86
file content (137 lines) | stat: -rw-r--r-- 3,576 bytes parent folder | download
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
static char *rcsid="$Id: cgiwrap.c,v 1.20 1996/08/05 21:48:24 nneul Exp $";

/**
 **  File: cgiwrap.c
 **  Purpose: Main program for cgiwrap
 **/ 

#include "cgiwrap.h"	/* Headers for all CGIwrap source files */

/*
 * Main program
 */
void main (int argc, char *argv[])
{
	char *userStr; /* User name */
	char *scrStr; /* Name of script */
	char *scriptPath; /* Path to script file */
	char *cgiBaseDir; /* Base directory for cgi scripts in user's dir */
	struct passwd *user; /* For getting uid from name */

	/* Determine if debugging output should be done */
	if ( strlen(argv[0]) >= 1 )
	{
		CONF_DEBUG = !strcmp( argv[0] + strlen(argv[0]) - 1, "d");
	}

	/* Output a status header if running in NoParseHeaders mode */
	if ( !strncmp(argv[0], "nph-", 4) )
	{
		DEBUG_Msg("HTTP/1.0 200 Ok");
	}
	
	/* Output a Content-type header if in debugging mode */
	if ( CONF_DEBUG )
	{
		SendHeader("text/plain");
		printf("\n");
	}

	/* Redirect stderr to stdout */
#if defined(CONF_REDIR_STDERR)
	DEBUG_Msg("\nRedirecting STDERR to STDOUT");
	dup2(1,2);
#endif

	/* Check who is running this script */
	VerifyExecutingUser();

	/* Set any default signal behavior */
	SetSignals();

	/* Set CPU and other limits */
	SetResourceLimits();

	/* Output the contents of important environment variables */
	OutputEnvironment();
	
	/* Get the user name from the given data */
	userStr = FetchUserString();
	DEBUG_Str("Retrieved User Name", userStr);

	/* Now, get whatever information that is available about that */
	/* user - fetch this information from the passwd file or NIS */
	if ( !(user = getpwnam(userStr)) )
	{
		DoError("User not found in passwd file.");
	}

	DEBUG_Msg("User Data Retrieved:");
	DEBUG_Str("   UserID:", user->pw_name);
	DEBUG_Int("   UID:", user->pw_uid);
	DEBUG_Int("   GID:", user->pw_gid);
	DEBUG_Str("   Home Dir:", user->pw_dir);

	/* Perform checks to make sure this user is allow to use CGI scripts */
	CheckUser(user);

	/* Determine the base directory where this user's CGI scripts
		are to be stored */
	DEBUG_Msg("");
	cgiBaseDir = GetBaseDirectory(user);	
	DEBUG_Str("Script Base Directory", cgiBaseDir);
	if ( !DirExists(cgiBaseDir) )
	{
		DoError("CGI Directory Not Found.");	
	}

	/* Get the script name from the given data */
	scrStr = FetchScriptString(cgiBaseDir);
	scriptPath = BuildScriptPath(cgiBaseDir,scrStr);

	DEBUG_Str("\tScript Name", scrStr);
	DEBUG_Str("\tScript Path", scriptPath);

	/* Set the Correct Values of environment variables */
	DEBUG_Msg("\nFixing Environment Variables.");
	SetScriptName(userStr, scrStr);
	SetPathTranslated( scriptPath );
	
	/* Output the modified environment variables */
	OutputEnvironment();

	/* Log the query request to the log file */
	Log(userStr, scrStr, "ok");

	/* Change auxilliary groups to match this user */
	ChangeAuxGroups(user);
	
	/* Change real and effective user and group id's to match this user */
	ChangeID(user);

	/* Change to the user's cgi-bin directory */
	ChangeToCGIDir(scriptPath);

	/* Check to see if ok to execute script file */
	CheckScriptFile(user, scriptPath);

	/* Perform any AFS related tasks before executing script */
	Create_AFS_PAG();
	
	/* Execute the script */
	DEBUG_Msg("\n\n");
	DEBUG_Msg("Output of script follows:");
	DEBUG_Msg("=====================================================");

#if defined(CONF_USE_SYSTEM)
#if defined(HAS_SYSTEM)
	system(scriptPath);
#else
	DoError("Configuration Error: system() call not available");
#endif
#else
	execv(scriptPath, CreateARGV(scrStr, argc,argv));
	DoPError("System Error: execv() failed\n");
#endif

}