gsasl  1.8.0
saml20/server.c
Go to the documentation of this file.
00001 /* server.c --- SAML20 mechanism, server side.
00002  * Copyright (C) 2010-2012 Simon Josefsson
00003  *
00004  * This file is part of GNU SASL Library.
00005  *
00006  * GNU SASL Library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation; either version 2.1 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * GNU SASL Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with GNU SASL Library; if not, write to the Free
00018  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include "config.h"
00025 #endif
00026 
00027 /* Get specification. */
00028 #include "saml20.h"
00029 
00030 /* Get strdup, strlen. */
00031 #include <string.h>
00032 
00033 /* Get free. */
00034 #include <stdlib.h>
00035 
00036 /* Get _gsasl_parse_gs2_header. */
00037 #include "mechtools.h"
00038 
00039 struct saml20_server_state
00040 {
00041   int step;
00042 };
00043 
00044 int
00045 _gsasl_saml20_server_start (Gsasl_session * sctx, void **mech_data)
00046 {
00047   struct saml20_server_state *state;
00048 
00049   state = (struct saml20_server_state *) calloc (sizeof (*state), 1);
00050   if (state == NULL)
00051     return GSASL_MALLOC_ERROR;
00052 
00053   *mech_data = state;
00054 
00055   return GSASL_OK;
00056 }
00057 
00058 int
00059 _gsasl_saml20_server_step (Gsasl_session * sctx,
00060                            void *mech_data,
00061                            const char *input, size_t input_len,
00062                            char **output, size_t * output_len)
00063 {
00064   struct saml20_server_state *state = mech_data;
00065   int res = GSASL_MECHANISM_CALLED_TOO_MANY_TIMES;
00066 
00067   *output_len = 0;
00068   *output = NULL;
00069 
00070   switch (state->step)
00071     {
00072     case 0:
00073       {
00074         const char *p;
00075         char *authzid;
00076         size_t headerlen;
00077 
00078         if (input_len == 0)
00079           return GSASL_NEEDS_MORE;
00080 
00081         res = _gsasl_parse_gs2_header (input, input_len,
00082                                        &authzid, &headerlen);
00083         if (res != GSASL_OK)
00084           return res;
00085 
00086         if (authzid)
00087           {
00088             gsasl_property_set (sctx, GSASL_AUTHZID, authzid);
00089             free (authzid);
00090           }
00091 
00092         input += headerlen;
00093         input_len -= headerlen;
00094 
00095         gsasl_property_set_raw (sctx, GSASL_SAML20_IDP_IDENTIFIER,
00096                                 input, input_len);
00097 
00098         p = gsasl_property_get (sctx, GSASL_SAML20_REDIRECT_URL);
00099         if (!p || !*p)
00100           return GSASL_NO_SAML20_REDIRECT_URL;
00101 
00102         *output_len = strlen (p);
00103         *output = malloc (*output_len);
00104         if (!*output)
00105           return GSASL_MALLOC_ERROR;
00106 
00107         memcpy (*output, p, *output_len);
00108 
00109         res = GSASL_NEEDS_MORE;
00110         state->step++;
00111         break;
00112       }
00113 
00114     case 1:
00115       {
00116         if (!(input_len == 1 && *input == '='))
00117           return GSASL_MECHANISM_PARSE_ERROR;
00118 
00119         res = gsasl_callback (NULL, sctx, GSASL_VALIDATE_SAML20);
00120         if (res != GSASL_OK)
00121           return res;
00122 
00123         *output = NULL;
00124         *output_len = 0;
00125 
00126         res = GSASL_OK;
00127         state->step++;
00128         break;
00129       }
00130 
00131     default:
00132       break;
00133     }
00134 
00135   return res;
00136 }
00137 
00138 void
00139 _gsasl_saml20_server_finish (Gsasl_session * sctx, void *mech_data)
00140 {
00141   struct saml20_server_state *state = mech_data;
00142 
00143   if (!state)
00144     return;
00145 
00146   free (state);
00147 }