Chapter 10: Common Gateway Interface


The Common Gateway Interface (CGI) is a language-independent gateway between a Web server and other programs on the same platform. CGI can invoke executables on your platform and pass variables to it, then return the output to the client. This chapter discusses CGI topics, including

CGI gives you the flexibility to write your Web applications in C, Perl, Java, Python, ksh, tsh, csh, or any other UNIX programming language. It can execute either a binary program or a script (assuming you have an appropriate interpreter). Both binaries and scripts behave identically in the CGI environment, and for the purposes of this chapter we will refer to both as programs.

As of Stronghold 2.4, CGI buffers are flushed whenever they contain data and Stronghold is waiting for more output from the program, allowing clients to receive partial responses while the program is finishing. Previous versions withheld the data until either the buffers were full or the program completed.


CGI Error Logging

For debugging purposes, the CGI module logs standard error data if the ScriptLog directive is set. Each error entry begins with two lines with this format:

%% [time] request-line
%% HTTP-status-code CGI-script-filename
These lines are followed by more specific error information, depending on the nature of the error. If CGI cannot start the script at all, the body of the error entry looks like this:

%%error
error-message
If the script returns incorrect header information, the body of the error entry looks like this:

%request
request-header(s) received by the script
%response
response-header(s) output by the script
%stdout CGI-standard-output
%stderr
CGI-standard-error
If the script does not output anything on stdout or stderr, those fields may be empty.


CGI Security

Two characteristics of CGI pose security issues for your server:

Since CGI executes programs at the request of users, intruders can arbitrarily invoke programs that can directly influence your platform. By manipulating the input, a clever hacker can turn a benign program into a tool that threatens the integrity of your log files, user accounts, or programs. By bypassing the HTML interface to a CGI program, an intruder avoids its constraints and can freely enter manipulative data or view "hidden" data fields.


Authoring Tips

This section offers some authoring tips that can help you avoid potential security weaknesses on your system:


suEXEC

Stronghold can run CGI programs under user IDs (UIDs) other than its own. For example, if your site hosts a number of users, some of whom have their own CGI programs, Stronghold can execute these under the UIDs of their owners. The suEXEC CGI wrapper provides this functionality.

When CGI programs are confined to the UIDs of their owners, weaknesses cannot be exploited outside the realm of that user's directories and files. In addition, the suEXEC wrapper uses these rules for added security:

These rules shore up many of the security weaknesses that CGI normally poses.

This is an advanced feature; use it with caution. If you configure it improperly, suEXEC can severely compromise the security of your site. However, if you are familiar with setuid programs and their security issues, and you configure suEXEC properly, it reinforces your site's security considerably.

To compile Stronghold with suEXEC

  1. Edit ServerRoot/src/support/suexec.h so that the following macros match your server configuration:
  2. Compile suEXEC:

    # gcc suexec.c -o suexec

  3. Add the following line to the Makefile Configuration section of ServerRoot/src/Configuration:

    EXTRA_CFLAGS= -DSUEXEC_BIN=\"/path/to/suexec\"

  4. Recompile Stronghold according to the instructions in "Recompiling Stronghold" on page 4-9.
  5. Move the suEXEC executable you created in step 2 to the location specified in step 3.
  6. Set the setuid bit:
    #   chown root suexec
    #   chmod 4711 suexec

  7. Stop the old Stronghold process.
  8. Start Stronghold. Note:   Restarting does not allow Stronghold to recognize the suEXEC wrapper. It must be stopped and then started.

    On startup, the server should print the following line:

    Configuring Apache for use with suexec wrapper.

    Stronghold can now implement suEXEC. However, if this line does not appear, suEXEC is incorrectly configured.

With suEXEC in place, Stronghold sets the UID of each CGI program to that of the user directory in which the program resides. For CGI programs that belong to a virtual host, use the User and Group directives to set the UID for that host's CGI programs.