prevent a C++ compiler error
[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
3293ebdf 19static const char rcsid[] = "$Id: log-dump.c,v 1.3 1999/07/26 05:33:01 roberts Exp $";
0198fd3c 20#endif /* not lint */
21
2fd179ab 22#if defined HAVE_UNISTD_H || defined __linux__
23#include <unistd.h>
24#endif
25#include <sys/types.h>
26#if defined __linux__
27int kill(pid_t pid, int sig);
28#endif
29
0198fd3c 30#include "fcgi_stdio.h"
31#include <stdlib.h>
32#include <signal.h>
3293ebdf 33#include <string.h>
0198fd3c 34#include <sys/stat.h>
35#include <fcntl.h>
36#include <errno.h>
37
38#ifndef NULL
39#define NULL 0
40#endif
41
42static int successCount = 0;
43static int failureCount = 0;
44
3293ebdf 45int main(void)
0198fd3c 46{
47 char *queryString = NULL;
48 char *rolePtr;
49 char *authPtr;
50 char *fileNamePtr = NULL;
51 int fd, n, i, j;
52 char temp[4096];
53 char temp2[5000];
54
55 while(FCGI_Accept() >= 0) {
56 rolePtr = getenv("FCGI_ROLE");
57 if(rolePtr == NULL) {
58 kill(getpid(), SIGQUIT);
59 exit(-1);
60 }
61 if(strstr(rolePtr, "AUTHORIZER")) {
62 queryString = getenv("QUERY_STRING");
63 if((queryString == NULL) ||
64 (strstr(queryString, "showme_the_log") == NULL)) {
65 failureCount++;
66 printf("Status: 403 Forbidden\r\n"
67 "Content-type: text/html\r\n"
68 "\r\n"
69 "<title>FastCGI Forbidden!</title>"
70 "<h2>Access to URL: \"%s\" forbidden!</h2><p>"
71 "<h2>This is password protected and you "
72 "have not specified a valid password.</h2>"
73 "<p><h3>Total Failed Accesses: %d</h3>",
74 getenv("URL_PATH"), failureCount);
75 } else {
76 successCount++;
77 printf("Status: 200 OK\r\n"
78 "Variable-LOG_ACCESS: ACCESS_OK.%d\r\n"
79 "\r\n", successCount);
80 }
81 continue;
82 }
83
84 /*
85 * If we're being invoked as a RESPONDER, make sure that we've
86 * been granted access to return the file or that the file being
87 * requested is beyond access control (ie. per request file data).
88 */
89 if(strstr(rolePtr, "RESPONDER")) {
90 authPtr = getenv("LOG_ACCESS");
91 if((authPtr == NULL) || (strstr(authPtr, "ACCESS_OK") == NULL)) {
92 failureCount++;
93 printf("Content-type: text/html\r\n\r\n"
94 "<h2>Access to log file \"%s\" denied</h2>"
95 "<p>Total Invalid Access Attempts: %d\r\n\r\n",
96 fileNamePtr, failureCount);
97 continue;
98 }
99
100 fileNamePtr = getenv("LOG_FILE");
101 if(fileNamePtr == NULL || *fileNamePtr == '\0') {
102 failureCount++;
103 printf("Content-type: text/html\r\n\r\n"
104 "<h2>No file specified.</h2>>>"
105 "<p>Total Invalid Access Attempts: %d\r\n\r\n",
106 failureCount);
107 continue;
108 }
109
110 fd = open(fileNamePtr, O_RDONLY, (S_IRGRP | S_IROTH | S_IRUSR));
111 if(fd < 0) {
112 printf("Content-type: text/html\r\n\r\n"
113 "<h2>File Error trying to access file \"%s\".</h2>"
114 "Error = %s\r\n\r\n", fileNamePtr, strerror(errno));
115 continue;
116 }
117 printf("Content-type: text/html\r\n\r\n"
118 "<h2>Sending contents of file: %s</h2><p>"
119 "<h2>Successful Accesses: %d</h2>", fileNamePtr,
120 successCount);
121 while((n = read(fd, temp, 4096)) > 0) {
122 j = 0;
123 for(i = 0; i < n; i++) {
124 temp2[j] = temp[i];
125 if(temp[i] == '\n') {
126 strcpy(&temp2[j], "<p>");
127 printf(temp2);
128 j = 0;
129 } else {
130 j++;
131 }
132 }
133 }
134 close(fd);
135 continue;
136 }
137 }
3293ebdf 138
139 exit(0);
0198fd3c 140}