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