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.
18 static const char rcsid[] = "$Id: log-dump.c,v 1.5 2001/09/01 01:12:26 robs Exp $";
21 #include "fcgi_config.h"
23 #include <sys/types.h>
32 int kill(pid_t pid, int sig);
39 #include "fcgi_stdio.h"
41 static int successCount = 0;
42 static int failureCount = 0;
46 char *queryString = NULL;
49 char *fileNamePtr = NULL;
54 while(FCGI_Accept() >= 0) {
55 rolePtr = getenv("FCGI_ROLE");
57 kill(getpid(), SIGQUIT);
60 if(strstr(rolePtr, "AUTHORIZER")) {
61 queryString = getenv("QUERY_STRING");
62 if((queryString == NULL) ||
63 (strstr(queryString, "showme_the_log") == NULL)) {
65 printf("Status: 403 Forbidden\r\n"
66 "Content-type: text/html\r\n"
68 "<title>FastCGI Forbidden!</title>"
69 "<h2>Access to URL: \"%s\" forbidden!</h2><p>"
70 "<h2>This is password protected and you "
71 "have not specified a valid password.</h2>"
72 "<p><h3>Total Failed Accesses: %d</h3>",
73 getenv("URL_PATH"), failureCount);
76 printf("Status: 200 OK\r\n"
77 "Variable-LOG_ACCESS: ACCESS_OK.%d\r\n"
78 "\r\n", successCount);
84 * If we're being invoked as a RESPONDER, make sure that we've
85 * been granted access to return the file or that the file being
86 * requested is beyond access control (ie. per request file data).
88 if(strstr(rolePtr, "RESPONDER")) {
89 authPtr = getenv("LOG_ACCESS");
90 if((authPtr == NULL) || (strstr(authPtr, "ACCESS_OK") == NULL)) {
92 printf("Content-type: text/html\r\n\r\n"
93 "<h2>Access to log file \"%s\" denied</h2>"
94 "<p>Total Invalid Access Attempts: %d\r\n\r\n",
95 fileNamePtr, failureCount);
99 fileNamePtr = getenv("LOG_FILE");
100 if(fileNamePtr == NULL || *fileNamePtr == '\0') {
102 printf("Content-type: text/html\r\n\r\n"
103 "<h2>No file specified.</h2>>>"
104 "<p>Total Invalid Access Attempts: %d\r\n\r\n",
109 fd = open(fileNamePtr, O_RDONLY, (S_IRGRP | S_IROTH | S_IRUSR));
111 printf("Content-type: text/html\r\n\r\n"
112 "<h2>File Error trying to access file \"%s\".</h2>"
113 "Error = %s\r\n\r\n", fileNamePtr, strerror(errno));
116 printf("Content-type: text/html\r\n\r\n"
117 "<h2>Sending contents of file: %s</h2><p>"
118 "<h2>Successful Accesses: %d</h2>", fileNamePtr,
120 while((n = read(fd, temp, 4096)) > 0) {
122 for(i = 0; i < n; i++) {
124 if(temp[i] == '\n') {
125 strcpy(&temp2[j], "<p>");