Misc. updates to get a clean make on Linux. A bit of
[catagits/fcgi2.git] / examples / log-dump.c
CommitLineData
0198fd3c 1/*
2 * log-dump.c --
3 *
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.
9 *
10 *
11 * Copyright (c) 1996 Open Market, Inc.
12 *
13 * See the file "LICENSE.TERMS" for information on usage and redistribution
14 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 *
16 */
17
18#ifndef lint
19static const char rcsid[] = "$Id: log-dump.c,v 1.1 1997/09/16 15:36:28 stanleyg Exp $";
20#endif /* not lint */
21
22#include "fcgi_stdio.h"
23#include <stdlib.h>
24#include <signal.h>
25#ifdef HAVE_STRINGS_H
26#include <strings.h>
27#endif
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <errno.h>
31
32#ifndef NULL
33#define NULL 0
34#endif
35
36static int successCount = 0;
37static int failureCount = 0;
38
39void main(void)
40{
41 char *queryString = NULL;
42 char *rolePtr;
43 char *authPtr;
44 char *fileNamePtr = NULL;
45 int fd, n, i, j;
46 char temp[4096];
47 char temp2[5000];
48
49 while(FCGI_Accept() >= 0) {
50 rolePtr = getenv("FCGI_ROLE");
51 if(rolePtr == NULL) {
52 kill(getpid(), SIGQUIT);
53 exit(-1);
54 }
55 if(strstr(rolePtr, "AUTHORIZER")) {
56 queryString = getenv("QUERY_STRING");
57 if((queryString == NULL) ||
58 (strstr(queryString, "showme_the_log") == NULL)) {
59 failureCount++;
60 printf("Status: 403 Forbidden\r\n"
61 "Content-type: text/html\r\n"
62 "\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);
69 } else {
70 successCount++;
71 printf("Status: 200 OK\r\n"
72 "Variable-LOG_ACCESS: ACCESS_OK.%d\r\n"
73 "\r\n", successCount);
74 }
75 continue;
76 }
77
78 /*
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).
82 */
83 if(strstr(rolePtr, "RESPONDER")) {
84 authPtr = getenv("LOG_ACCESS");
85 if((authPtr == NULL) || (strstr(authPtr, "ACCESS_OK") == NULL)) {
86 failureCount++;
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);
91 continue;
92 }
93
94 fileNamePtr = getenv("LOG_FILE");
95 if(fileNamePtr == NULL || *fileNamePtr == '\0') {
96 failureCount++;
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",
100 failureCount);
101 continue;
102 }
103
104 fd = open(fileNamePtr, O_RDONLY, (S_IRGRP | S_IROTH | S_IRUSR));
105 if(fd < 0) {
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));
109 continue;
110 }
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,
114 successCount);
115 while((n = read(fd, temp, 4096)) > 0) {
116 j = 0;
117 for(i = 0; i < n; i++) {
118 temp2[j] = temp[i];
119 if(temp[i] == '\n') {
120 strcpy(&temp2[j], "<p>");
121 printf(temp2);
122 j = 0;
123 } else {
124 j++;
125 }
126 }
127 }
128 close(fd);
129 continue;
130 }
131 }
132}