and actually use the HAVE_SOCKLEN define
[catagits/fcgi2.git] / examples / tiny-authorizer.c
CommitLineData
0198fd3c 1/*
2 * tiny-authorizer.c --
3 *
4 * FastCGI example Authorizer program using fcgi_stdio library
5 *
6 *
7 * Be sure to run this program in a region with CGIPassword
8 * in order to get REMOTE_USER and REMOTE_PASSWD in place
9 * of HTTP_AUTHORIZATION.
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: tiny-authorizer.c,v 1.2 1999/07/26 05:32:59 roberts Exp $";
0198fd3c 20#endif /* not lint */
21
22#include "fcgi_stdio.h"
23#include <stdlib.h>
24#include <string.h>
25
3293ebdf 26int main(void)
0198fd3c 27{
28 char *user, *password;
3293ebdf 29
0198fd3c 30 user = getenv("USER");
3293ebdf 31 if (user == NULL) {
0198fd3c 32 user = "doe";
33 }
3293ebdf 34
0198fd3c 35 password = getenv("PASSWORD");
3293ebdf 36 if (password == NULL) {
0198fd3c 37 password = "xxxx";
38 }
39
3293ebdf 40 while (FCGI_Accept() >= 0) {
0198fd3c 41 char *remoteUser, *remotePassword;
3293ebdf 42
0198fd3c 43 remoteUser = getenv("REMOTE_USER");
44 remotePassword = getenv("REMOTE_PASSWD");
3293ebdf 45 if ((remoteUser == NULL) || (remotePassword == NULL)
46 || strcmp(remoteUser, user) || strcmp(remotePassword, password))
47 {
48 printf("Status: 401 Unauthorized\r\n"
49 "WWW-Authenticate: Basic realm=\"Test\"\r\n"
50 "\r\n");
51 }
52 else {
0198fd3c 53 char *processId = getenv("QUERY_STRING");
3293ebdf 54 if (processId == NULL || strlen(processId) == 0) {
0198fd3c 55 processId = "0";
56 }
57 printf("Status: 200 OK\r\n"
3293ebdf 58 "Variable-AUTH_TYPE: Basic\r\n"
59 "Variable-REMOTE_PASSWD:\r\n"
60 "Variable-PROCESS_ID: %s\r\n"
61 "\r\n", processId);
0198fd3c 62 }
63 }
3293ebdf 64
65 return 0;
0198fd3c 66}