/* * log-dump.c -- * * FastCGI example program to illustrate both an Authorizer and a * Responder in a single application that are used to provide access * to an ascii text file. The intent of this application is to * show the basic mechanics needed to display a log file for example * though any ascii text file should work. * * * Copyright (c) 1996 Open Market, Inc. * * See the file "LICENSE.TERMS" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * */ #ifndef lint static const char rcsid[] = "$Id: log-dump.c,v 1.1.6.1 1997/01/18 15:51:27 snapper Exp $"; #endif /* not lint */ #include "fcgi_stdio.h" #include #include #ifdef HAVE_STRINGS_H #include #endif #include #include #include #ifndef NULL #define NULL 0 #endif static int successCount = 0; static int failureCount = 0; void main(void) { char *queryString = NULL; char *rolePtr; char *authPtr; char *fileNamePtr = NULL; int fd, n, i, j; char temp[4096]; char temp2[5000]; while(FCGI_Accept() >= 0) { rolePtr = getenv("FCGI_ROLE"); if(rolePtr == NULL) { kill(getpid(), SIGQUIT); exit(-1); } if(strstr(rolePtr, "AUTHORIZER")) { queryString = getenv("QUERY_STRING"); if((queryString == NULL) || (strstr(queryString, "showme_the_log") == NULL)) { failureCount++; printf("Status: 403 Forbidden\r\n" "Content-type: text/html\r\n" "\r\n" "FastCGI Forbidden!" "

Access to URL: \"%s\" forbidden!

" "

This is password protected and you " "have not specified a valid password.

" "

Total Failed Accesses: %d

", getenv("URL_PATH"), failureCount); } else { successCount++; printf("Status: 200 OK\r\n" "Variable-LOG_ACCESS: ACCESS_OK.%d\r\n" "\r\n", successCount); } continue; } /* * If we're being invoked as a RESPONDER, make sure that we've * been granted access to return the file or that the file being * requested is beyond access control (ie. per request file data). */ if(strstr(rolePtr, "RESPONDER")) { authPtr = getenv("LOG_ACCESS"); if((authPtr == NULL) || (strstr(authPtr, "ACCESS_OK") == NULL)) { failureCount++; printf("Content-type: text/html\r\n\r\n" "

Access to log file \"%s\" denied

" "

Total Invalid Access Attempts: %d\r\n\r\n", fileNamePtr, failureCount); continue; } fileNamePtr = getenv("LOG_FILE"); if(fileNamePtr == NULL || *fileNamePtr == '\0') { failureCount++; printf("Content-type: text/html\r\n\r\n" "

No file specified.

>>" "

Total Invalid Access Attempts: %d\r\n\r\n", failureCount); continue; } fd = open(fileNamePtr, O_RDONLY, (S_IRGRP | S_IROTH | S_IRUSR)); if(fd < 0) { printf("Content-type: text/html\r\n\r\n" "

File Error trying to access file \"%s\".

" "Error = %s\r\n\r\n", fileNamePtr, strerror(errno)); continue; } printf("Content-type: text/html\r\n\r\n" "

Sending contents of file: %s

" "

Successful Accesses: %d

", fileNamePtr, successCount); while((n = read(fd, temp, 4096)) > 0) { j = 0; for(i = 0; i < n; i++) { temp2[j] = temp[i]; if(temp[i] == '\n') { strcpy(&temp2[j], "

"); printf(temp2); j = 0; } else { j++; } } } close(fd); continue; } } }