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 | * |
af1b4cad |
13 | * See the file "LICENSE" for information on usage and redistribution |
0198fd3c |
14 | * of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
15 | * |
16 | */ |
0198fd3c |
17 | #ifndef lint |
c8684964 |
18 | static const char rcsid[] = "$Id: log-dump.c,v 1.5 2001/09/01 01:12:26 robs 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 | |
c8684964 |
35 | #ifdef HAVE_UNISTD_H |
344bf056 |
36 | #include <unistd.h> |
37 | #endif |
38 | |
39 | #include "fcgi_stdio.h" |
40 | |
0198fd3c |
41 | static int successCount = 0; |
42 | static int failureCount = 0; |
43 | |
3293ebdf |
44 | int main(void) |
0198fd3c |
45 | { |
46 | char *queryString = NULL; |
47 | char *rolePtr; |
48 | char *authPtr; |
49 | char *fileNamePtr = NULL; |
50 | int fd, n, i, j; |
51 | char temp[4096]; |
52 | char temp2[5000]; |
344bf056 |
53 | |
0198fd3c |
54 | while(FCGI_Accept() >= 0) { |
55 | rolePtr = getenv("FCGI_ROLE"); |
56 | if(rolePtr == NULL) { |
57 | kill(getpid(), SIGQUIT); |
58 | exit(-1); |
59 | } |
60 | if(strstr(rolePtr, "AUTHORIZER")) { |
61 | queryString = getenv("QUERY_STRING"); |
62 | if((queryString == NULL) || |
63 | (strstr(queryString, "showme_the_log") == NULL)) { |
64 | failureCount++; |
65 | printf("Status: 403 Forbidden\r\n" |
66 | "Content-type: text/html\r\n" |
67 | "\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); |
74 | } else { |
75 | successCount++; |
76 | printf("Status: 200 OK\r\n" |
77 | "Variable-LOG_ACCESS: ACCESS_OK.%d\r\n" |
78 | "\r\n", successCount); |
79 | } |
80 | continue; |
81 | } |
82 | |
83 | /* |
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). |
87 | */ |
88 | if(strstr(rolePtr, "RESPONDER")) { |
89 | authPtr = getenv("LOG_ACCESS"); |
90 | if((authPtr == NULL) || (strstr(authPtr, "ACCESS_OK") == NULL)) { |
91 | failureCount++; |
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); |
96 | continue; |
97 | } |
98 | |
99 | fileNamePtr = getenv("LOG_FILE"); |
100 | if(fileNamePtr == NULL || *fileNamePtr == '\0') { |
101 | failureCount++; |
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", |
105 | failureCount); |
106 | continue; |
107 | } |
108 | |
109 | fd = open(fileNamePtr, O_RDONLY, (S_IRGRP | S_IROTH | S_IRUSR)); |
110 | if(fd < 0) { |
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)); |
114 | continue; |
115 | } |
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, |
119 | successCount); |
120 | while((n = read(fd, temp, 4096)) > 0) { |
121 | j = 0; |
122 | for(i = 0; i < n; i++) { |
123 | temp2[j] = temp[i]; |
124 | if(temp[i] == '\n') { |
125 | strcpy(&temp2[j], "<p>"); |
126 | printf(temp2); |
127 | j = 0; |
128 | } else { |
129 | j++; |
130 | } |
131 | } |
132 | } |
133 | close(fd); |
134 | continue; |
135 | } |
136 | } |
344bf056 |
137 | |
3293ebdf |
138 | exit(0); |
0198fd3c |
139 | } |