4 * FastCGI example program to illustrate both an Authorizer and a
5 * Responder in a single application that are used to provide access
6 * to an ascii text file. The intent of this application is to
7 * show the basic mechanics needed to display a log file for example
8 * though any ascii text file should work.
11 * Copyright (c) 1996 Open Market, Inc.
13 * See the file "LICENSE.TERMS" for information on usage and redistribution
14 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
19 static const char rcsid[] = "$Id: log-dump.c,v 1.1 1997/09/16 15:36:28 stanleyg Exp $";
22 #include "fcgi_stdio.h"
36 static int successCount = 0;
37 static int failureCount = 0;
41 char *queryString = NULL;
44 char *fileNamePtr = NULL;
49 while(FCGI_Accept() >= 0) {
50 rolePtr = getenv("FCGI_ROLE");
52 kill(getpid(), SIGQUIT);
55 if(strstr(rolePtr, "AUTHORIZER")) {
56 queryString = getenv("QUERY_STRING");
57 if((queryString == NULL) ||
58 (strstr(queryString, "showme_the_log") == NULL)) {
60 printf("Status: 403 Forbidden\r\n"
61 "Content-type: text/html\r\n"
63 "<title>FastCGI Forbidden!</title>"
64 "<h2>Access to URL: \"%s\" forbidden!</h2><p>"
65 "<h2>This is password protected and you "
66 "have not specified a valid password.</h2>"
67 "<p><h3>Total Failed Accesses: %d</h3>",
68 getenv("URL_PATH"), failureCount);
71 printf("Status: 200 OK\r\n"
72 "Variable-LOG_ACCESS: ACCESS_OK.%d\r\n"
73 "\r\n", successCount);
79 * If we're being invoked as a RESPONDER, make sure that we've
80 * been granted access to return the file or that the file being
81 * requested is beyond access control (ie. per request file data).
83 if(strstr(rolePtr, "RESPONDER")) {
84 authPtr = getenv("LOG_ACCESS");
85 if((authPtr == NULL) || (strstr(authPtr, "ACCESS_OK") == NULL)) {
87 printf("Content-type: text/html\r\n\r\n"
88 "<h2>Access to log file \"%s\" denied</h2>"
89 "<p>Total Invalid Access Attempts: %d\r\n\r\n",
90 fileNamePtr, failureCount);
94 fileNamePtr = getenv("LOG_FILE");
95 if(fileNamePtr == NULL || *fileNamePtr == '\0') {
97 printf("Content-type: text/html\r\n\r\n"
98 "<h2>No file specified.</h2>>>"
99 "<p>Total Invalid Access Attempts: %d\r\n\r\n",
104 fd = open(fileNamePtr, O_RDONLY, (S_IRGRP | S_IROTH | S_IRUSR));
106 printf("Content-type: text/html\r\n\r\n"
107 "<h2>File Error trying to access file \"%s\".</h2>"
108 "Error = %s\r\n\r\n", fileNamePtr, strerror(errno));
111 printf("Content-type: text/html\r\n\r\n"
112 "<h2>Sending contents of file: %s</h2><p>"
113 "<h2>Successful Accesses: %d</h2>", fileNamePtr,
115 while((n = read(fd, temp, 4096)) > 0) {
117 for(i = 0; i < n; i++) {
119 if(temp[i] == '\n') {
120 strcpy(&temp2[j], "<p>");