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