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