cgi & perl

The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with information servers such as HTTP servers, maintained by the NCSA. The current version is CGI/1.1 and CGI/1.2 is under progress.

Essentially, a CGI is just a program which runs on the server. It can be written in any programming language, but Perl has become a popular choice for CGI programming because it is available for all platforms, and it has many useful tools that are ideal for the web. By convention cgi programs have the file extension .cgi.

Perl is an interpreted language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It’s also a good language for any system management tasks. The language is intended to be practical rather than beautiful. Perl was created by Larry Wall. Perl scripts have the file extension .pl.

There is no real difference between .cgi and  .pl file extensions. Web servers can be configured for a specific extension or you can even leave off the extension, because it’s the first line called shebang in the script that tells the server where and which interpreter to use. For perl programs I prefer to use the extension .pl.

A typical shebang line for perl is: #!/usr/bin/perl

To let the server know it is a cgi program, the files are generally placed in a special directory on the server called /cgi-bin. For security reasons the webserver does not allow chmod permission settings of 777 or 775 for scripts. I set them to 755.

I use the following reference test perl file to check the correct configuration of my hosted webserver.

#!/usr/bin/perl -w

use strict;
use CGI::Carp qw(fatalsToBrowser);

my $headline = “Perl Reference Script”;

print “Content-type: text/htmlnn”;
print ‘<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>’, “n”;
print “<html><head><title>Perl Test</title></head><body>n”;
print “<h1>$headline</h1>n”;
print “<p>This test script opens a data.txt file, appends a name to it and creates a new file new.txt</p>n”;
print “</body></html>n”;

open (MYFILE, ‘data.txt’) || die “Could not open file data.txt”;;
while (<MYFILE>) {
chomp;
print “$_n”;
}
close (MYFILE);

open (MYFILE, ‘>>data.txt’) || die “Could not write to file data.txt”;;
print MYFILE “Bobn”;
close (MYFILE);

open (MYFILE, ‘>new.txt’) || die “Could not create file new.txt”;
print MYFILE “This file has been createdn”;
close (MYFILE);

The following commands are used to run the program:

  • -w : switch to turning on warnings
  • use strict : pragma for the interpreter to make it harder to write bad software
  • use CGI::Carp qw(fatalsToBrowser) : command to redirect fatal errors such as compiler or other errors to the browser
  • MYFILE : filehandler

A testfile to show my webserver cgi environment variables is available at the following link.  An advanced testfile can be started here. The access is protected with .htaccess and .htpasswd.

CHMOD

CHMOD (abréviation de change mode) est une commande exécutable dans un environnement de type Unix qui permet de changer les permissions d’accès (spéciales ou non) sur un fichier ou dossier. Il arrive fréquemment qu’on lit dans un guide d’installation d’une application sur un serveur qu’il faut configurer un fichier ou dossier en chmod 777 ou chmod 644 ou -wx.

Sur un serveur, on trouve, sous propriétés d’un fichier ou dossier, un tableau avec des champs à cocher avec trois colonnes “lire ( r = read), écrire (w = write) et exécuter (x = execute)” et trois rangées “propriétaire (owner), groupe (group) et autres (all users)”. Lire a la valeur 4, écrire a la valeur 2 et exécuter a la valeur 1.

Par exemple CHMOD 764 veut donc dire :

  • le propriétaire peut lire, écrire et exécuter: 4+2+1=7
  • le groupe peut lire et écrire:4+2=6
  • les autres ne peuvent que le lire: 4

Il n’est pas recommandé de mettre les permissions des fichiers et répertoires sur un serveur sur 777, car tout le monde peut alors les modifier s’il a accès au serveur. Il est conseillé de mettre les fichiers sur 664 et les répertoires sur 775, sauf exceptions justifiées.