1 /* zxidhlowsf.c  -  Hello World CGI binary for SAML 2 SP and ID_WSF
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: zxidhlowsf.c,v 1.16 2010-01-08 02:10:09 sampo Exp $
9  *
10  * 25.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 <zx/platform.h>
17 
18 #include <string.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 
26 #include <zx/errmac.h>
27 #include <zx/zxid.h>      /* ZXID main API, including zxid_simple(). */
28 #include <zx/zxidpriv.h>
29 #include <zx/zxidconf.h>  /* Default and compile-time configuration options. */
30 #include <zx/wsf.h>
31 #include <zx/c/zxidvers.h>
32 #include <zx/c/zx-e-data.h>
33 
34 char* help =
35 "zxidhlowsf  -  SAML 2.0 SP + WSC CGI - R" ZXID_REL "\n\
36 SAML 2.0 is a standard for federated identity and Single Sign-On.\n\
37 Copyright (c) 2010 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved.\n\
38 Copyright (c) 2007 Symlabs (symlabs@symlabs.com), All Rights Reserved.\n\
39 Author: Sampo Kellomaki (sampo@iki.fi)\n\
40 NO WARRANTY, not even implied warranties. Licensed under Apache License v2.0\n\
41 See http://www.apache.org/licenses/LICENSE-2.0\n\
42 Send well-researched bug reports to the author. Home: zxid.org\n\
43 \n\
44 Usage: zxidhlowsf [options]   (when used as CGI, no options can be supplied)\n\
45   -h               This help message\n\
46   --               End of options\n";
47 
48 
49 /* ============== M A I N ============== */
50 
51 #if 1
52 #define ZXIDHLO "zxidhlowsf"
53 #define CONF "PATH=/var/zxid/&URL=https://sp1.zxidsp.org:8443/" ZXIDHLO
54 #else
55 #define ZXIDHLO "zxidhlowsf"
56 #define CONF "PATH=/var/zxid/&URL=https://sampo:8443/" ZXIDHLO
57 #endif
58 
59 /* Called by: */
main(int argc,char ** argv)60 int main(int argc, char** argv)
61 {
62   zxid_conf* cf;
63   zxid_ses sess;
64   zxid_ses* ses;
65   //struct zx_root_s* r;
66   struct zx_e_Envelope_s* env;
67   zxid_epr* epr;
68   char* p;
69   char* sid;
70   char* nid;
71   char* res;
72   char* qs;
73   //char* hrxml_resp = 0;
74 
75   qs = getenv("QUERY_STRING");
76   qs = strdup(qs);
77 
78 #if 1
79   /* Helps debugging CGI scripts if you see stderr. */
80   close(2);
81   if (open("tmp/zxid.stderr", O_WRONLY | O_CREAT | O_APPEND, 0666) != 2)
82     exit(2);
83   fprintf(stderr, "=================== Running ===================\n");
84 #endif
85 
86   if (argc > 1) {
87     fprintf(stderr, "This is a CGI script (written in C). No arguments are accepted.\n%s", help);
88     exit(1);
89   }
90   cf = zxid_new_conf_to_cf(CONF);
91 
92   res = zxid_simple_cf(cf, 0, 0, 0, 0x1fff);
93   switch (res[0]) {
94   default:
95     ERR("Unknown zxid_simple() response(%s)", res);
96   case 'd': break; /* Logged in case */
97   }
98 
99   /* Parse the LDIF to figure out session ID and the federated ID */
100 
101   sid = strstr(res, "sesid: ");
102   nid = strstr(res, "idpnid: ");
103   if (sid) {
104     sid += sizeof("sesid: ") - 1;
105     p = strchr(sid, '\n');
106     if (p)
107       *p = 0;  /* nul termination */
108   }
109   if (nid) {
110     nid += sizeof("idpnid: ") - 1;
111     p = strchr(nid, '\n');
112     if (p)
113       *p = 0;  /* nul termination */
114   }
115 
116   /* Discover ID-DAP (call discovery service using bootstrap) */
117 
118   ses = &sess;
119   zxid_get_ses(cf, ses, sid);
120   env = zx_NEW_e_Envelope(cf->ctx,0);
121   env->Header = zx_NEW_e_Header(cf->ctx, &env->gg);
122   env->Body = zx_NEW_e_Body(cf->ctx, &env->gg);
123   env->Body->Query = zxid_mk_di_query(cf, &env->Body->gg, XMLNS_DAP, 0,0,0);
124   epr = zxid_find_epr(cf, ses, XMLNS_DISCO_2_0, 0,0,0, 1);
125   env = zxid_wsc_call(cf, ses, epr, env, 0);
126 
127 #if 0
128   /* Call ID-DAP */
129 
130   r = zxid_wsc_call(cf, ses, res->epr, body);
131 #endif
132 
133   /* Render protected content page. You should replace this
134    * with your own content, or establishment of your own session
135    * and then redirection to your own content. Whatever makes sense. */
136 
137   printf("Content-Type: text/html\r\n\r\n");
138   printf("<title>ZXID HELLO SP Mgmt</title>" ZXID_BODY_TAG "<h1>ZXID HELLO SP Management (user logged in, session active)</h1><pre>\n");
139   printf("</pre><form method=post action=\"" ZXIDHLO "?o=P\">");
140   //if (err) printf("<p><font color=red><i>%s</i></font></p>\n", err);
141   //if (msg) printf("<p><i>%s</i></p>\n", msg);
142   if (sid) {
143     printf("<input type=hidden name=s value=\"%s\">", sid);
144     printf("<input type=submit name=gl value=\" Local Logout \">\n");
145     printf("<input type=submit name=gr value=\" Single Logout (Redir) \">\n");
146     printf("<input type=submit name=gs value=\" Single Logout (SOAP) \">\n");
147     printf("<input type=submit name=gt value=\" Defederate (Redir) \">\n");
148     printf("<input type=submit name=gu value=\" Defederate (SOAP) \"><br>\n");
149     printf("sid(%s) nid(%s) <a href=\"" ZXIDHLO "?s=%s\">Reload</a>", sid, nid?nid:"?!?", sid);
150   }
151 
152   printf("</form><hr>");
153   printf("<a href=\"http://zxid.org/\">zxid.org</a>, %s", zxid_version_str());
154   return 0;
155 }
156 
157 /* EOF  --  zxidhlowsf.c */
158