Branch data Line data Source code
1 : : /* zxididp.c - CGI binary for SAML 2 IdP
2 : : * Copyright (c) 2008-2010 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved.
3 : : * This is confidential unpublished proprietary source code of the author.
4 : : * NO WARRANTY, not even implied warranties. Contains trade secrets.
5 : : * Distribution prohibited unless authorized in writing.
6 : : * Licensed under Apache License 2.0, see file COPYING.
7 : : * $Id: zxididp.c,v 1.9 2010-01-08 02:10:09 sampo Exp $
8 : : *
9 : : * 12.11.2008, created --Sampo
10 : : * 24.8.2009, perfected for TAS3 workshop --Sampo
11 : : *
12 : : * See zxid_idp_dispatch() in zxididpx.c for most interesting parts of IdP implementation.
13 : : *
14 : : * See also: http://hoohoo.ncsa.uiuc.edu/cgi/interface.html (CGI specification)
15 : : * README-zxid, section 10 "zxid_simple() API"
16 : : */
17 : :
18 : : #include <zx/platform.h>
19 : :
20 : : #include <string.h>
21 : : #include <stdio.h>
22 : : #include <stdlib.h>
23 : : #include <sys/types.h>
24 : : #include <sys/stat.h>
25 : : #include <fcntl.h>
26 : :
27 : : #include <zx/errmac.h>
28 : : #include <zx/zxid.h> /* ZXID main API, including zxid_simple(). */
29 : : #include <zx/zxidconf.h> /* Default and compile-time configuration options. */
30 : : #include <zx/c/zxidvers.h>
31 : :
32 : : char* help =
33 : : "zxididp - SAML 2.0 IdP CGI (also DI, IM, and PS) - R" ZXID_REL "\n\
34 : : SAML 2.0 is a standard for federated identity and Single Sign-On.\n\
35 : : Copyright (c) 2008-2010 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved.\n\
36 : : NO WARRANTY, not even implied warranties. Licensed under Apache License v2.0\n\
37 : : See http://www.apache.org/licenses/LICENSE-2.0\n\
38 : : Send well-researched bug reports to the author. Home: zxid.org\n\
39 : : \n\
40 : : Usage: zxididp [options] (when used as CGI, no options can be supplied)\n\
41 : : -h This help message\n\
42 : : -- End of options\n";
43 : :
44 : : /* ============== M A I N ============== */
45 : :
46 : : /* CONFIG: You must have created /var/zxid directory hierarchy. See `make dir' */
47 : : /* CONFIG: You must edit the URL to match your domain name and port */
48 : :
49 : : #define CONF "URL=https://idp1.zxidp.org:8443/zxididp&NICE_NAME=ZXIdP&NOSIG_FATAL=0&SES_COOKIE_NAME=ZXIDPSES&IDP_ENA=1&PDP_ENA=1&PATH=/var/zxid/idp"
50 : :
51 : : /* Called by: */
52 : : int main(int argc, char** argv)
53 : 156 : {
54 : : char* p;
55 : : char* sid;
56 : : char* nid;
57 : : char* res;
58 : : char* setcookie;
59 : :
60 : : #if 1
61 : : /* Helps debugging CGI scripts if you see stderr. */
62 : : /* Reopen stderr only in mini_httpd case */
63 : 156 : p = getenv("SERVER_SOFTWARE");
64 [ + + + - ]: 156 : if (p && !memcmp(p, "mini_httpd", sizeof("mini_httpd")-1)) {
65 : 149 : close(2);
66 [ - + ]: 149 : if (open("/var/tmp/zxid.stderr", O_WRONLY | O_CREAT | O_APPEND, 0666) != 2)
67 : 0 : exit(2);
68 : : }
69 : 156 : fprintf(stderr, "=================== Running zxididp %s ===================\n", ZXID_REL);
70 : : //fprintf(stderr, "p(%s)\n", p);
71 : : #endif
72 : :
73 [ + + ]: 156 : if (argc > 1) {
74 : 1 : fprintf(stderr, "This is a CGI script (written in C). No arguments are accepted.\n%s", help);
75 : 1 : exit(1);
76 : : }
77 : :
78 : : #if 1
79 : 155 : strncpy(zx_instance, "\t\e[47mzxidp\e[0m", sizeof(zx_instance));
80 : : #else
81 : : strncpy(zx_instance, "\tzxidp", sizeof(zx_instance));
82 : : #endif
83 : : //zx_debug = 1;
84 : 155 : res = zxid_simple(CONF, 0, 0x1fff); /* 0xfff == full CGI automation */
85 [ - + ]: 2 : switch (res[0]) {
86 : : default:
87 : 0 : ERR("Unknown zxid_simple() response(%s)", res);
88 : : case 'd': break; /* Logged in case */
89 : : }
90 : :
91 : : /* Parse the LDIF to figure out session ID and the federated ID */
92 : :
93 : 2 : sid = strstr(res, "sesid: ");
94 : 2 : nid = strstr(res, "idpnid: ");
95 : 2 : setcookie = strstr(res, "setcookie: ");
96 [ + - ]: 2 : if (sid) {
97 : 2 : sid += sizeof("sesid: ") - 1;
98 : 2 : p = strchr(sid, '\n');
99 [ + - ]: 2 : if (p)
100 : 2 : *p = 0; /* nul termination */
101 : : }
102 [ + - ]: 2 : if (nid) {
103 : 2 : nid += sizeof("idpnid: ") - 1;
104 : 2 : p = strchr(nid, '\n');
105 [ + - ]: 2 : if (p)
106 : 2 : *p = 0; /* nul termination */
107 : : }
108 [ + - ]: 2 : if (setcookie) {
109 : 2 : setcookie += sizeof("setcookie: ") - 1;
110 : 2 : p = strchr(setcookie, '\n');
111 [ + - ]: 2 : if (p)
112 : 2 : *p = 0; /* nul termination */
113 : : }
114 : :
115 : : /* Render protected content page. You should replace this
116 : : * with your own content, or establishment of your own session
117 : : * and then redirection to your own content. Whatever makes sense. */
118 : :
119 [ + - + - : 2 : if (setcookie && !ONE_OF_2(*setcookie, '-', 0))
+ - ]
120 : 2 : printf("SET-COOKIE: %s\r\n", setcookie);
121 : 2 : printf("Content-Type: text/html\r\n\r\n");
122 : 2 : printf("<title>ZXID IdP Mgmt</title>" ZXID_BODY_TAG "<h1>ZXID IdP Management (user logged in, session active)</h1><pre>\n");
123 : 2 : printf("</pre><form method=post action=\"?o=P\">");
124 : : //if (err) printf("<p><font color=red><i>%s</i></font></p>\n", err);
125 : : //if (msg) printf("<p><i>%s</i></p>\n", msg);
126 [ + - ]: 2 : if (sid) {
127 : 2 : printf("<input type=hidden name=s value=\"%s\">", sid);
128 : 2 : printf("<input type=submit name=gl value=\" Local Logout \">\n");
129 : 2 : printf("<input type=submit name=gr value=\" Single Logout (Redir) \">\n");
130 : 2 : printf("<input type=submit name=gs value=\" Single Logout (SOAP) \">\n");
131 : 2 : printf("<input type=submit name=gt value=\" Defederate (Redir) \">\n");
132 : 2 : printf("<input type=submit name=gu value=\" Defederate (SOAP) \"><br>\n");
133 [ + - ]: 2 : printf("sid(%s) nid(%s) <a href=\"?s=%s\">Reload</a>", sid, nid?nid:"?!?", sid);
134 : : }
135 : :
136 : 2 : printf("</form><hr>");
137 : 2 : printf("<a href=\"http://zxid.org/\">zxid.org</a>, %s", zxid_version_str());
138 : 2 : return 0;
139 : : }
140 : :
141 : : /* EOF -- zxididp.c */
|