auth_plugin_delayed.c 5.6 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE auth_plugin_delayed.c
 *
27
 * @brief Plugin for delayed authentication.
Pekka Pessi's avatar
Pekka Pessi committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 *
 * This authentication plugin provides authentication operation that is
 * intentionally delayed. It serves as an example of server-side
 * authentication plugins.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
 *
 * @date Created: Wed Apr 11 15:14:03 2001 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

#define SU_MSG_ARG_T struct auth_splugin_t

#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
static char const __func__[] = "auth_plugin_delayed";
#endif

55 56
#include <sofia-sip/su_debug.h>
#include <sofia-sip/su_wait.h>
Pekka Pessi's avatar
Pekka Pessi committed
57

58 59
#include <sofia-sip/su_alloc.h>
#include <sofia-sip/su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
60

61 62
#include "sofia-sip/auth_module.h"
#include "sofia-sip/auth_plugin.h"
Pekka Pessi's avatar
Pekka Pessi committed
63

64
struct auth_plugin_t
Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
{
  su_root_t      *ap_root;
  auth_scheme_t  *ap_base;
  auth_splugin_t *ap_list;
  auth_splugin_t**ap_tail;
};

/* Digest (or Basic) with delay */

static int delayed_auth_init(auth_mod_t *am,
			     auth_scheme_t *base,
			     su_root_t *root,
			     tag_type_t tag, tag_value_t value, ...);

static void delayed_auth_method(auth_mod_t *am,
				auth_status_t *as,
				msg_auth_t *auth,
				auth_challenger_t const *ach);

84
static void delayed_auth_challenge(auth_mod_t *am,
Pekka Pessi's avatar
Pekka Pessi committed
85 86 87 88 89 90 91
				   auth_status_t *as,
				   auth_challenger_t const *ach);

static void delayed_auth_cancel(auth_mod_t *am, auth_status_t *as);

static void delayed_auth_destroy(auth_mod_t *am);

92
auth_scheme_t auth_scheme_delayed[1] =
Pekka Pessi's avatar
Pekka Pessi committed
93 94 95 96
  {{
      "Delayed",
      sizeof (struct { auth_mod_t mod[1]; auth_plugin_t plug[1]; }),
      delayed_auth_init,
97
      delayed_auth_method,
Pekka Pessi's avatar
Pekka Pessi committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
      delayed_auth_challenge,
      delayed_auth_cancel,
      delayed_auth_destroy
  }};

static int delayed_auth_init(auth_mod_t *am,
			     auth_scheme_t *base,
			     su_root_t *root,
			     tag_type_t tag, tag_value_t value, ...)
{
  auth_plugin_t *ap = AUTH_PLUGIN(am);
  int retval = -1;
  ta_list ta;

  ta_start(ta, tag, value);

  if (root && base && auth_init_default(am, base, root, ta_tags(ta)) != -1) {
    ap->ap_root = root;
    ap->ap_base = base;
    ap->ap_tail = &ap->ap_list;

    retval = 0;
  }
121

Pekka Pessi's avatar
Pekka Pessi committed
122 123 124 125 126
  ta_end(ta);

  return retval;
}

127
struct auth_splugin_t
Pekka Pessi's avatar
Pekka Pessi committed
128
{
129
  void const      *asp_cookie;
Pekka Pessi's avatar
Pekka Pessi committed
130 131 132 133 134 135 136 137 138
  auth_splugin_t  *asp_next;
  auth_splugin_t **asp_prev;
  auth_mod_t      *asp_am;
  auth_status_t   *asp_as;
  msg_auth_t      *asp_header;
  auth_challenger_t const *asp_ach;
  int              asp_canceled;
};

139 140
/* This is unique identifier */
#define delayed_asp_cookie ((void const *)(intptr_t)delayed_auth_cancel)
Pekka Pessi's avatar
Pekka Pessi committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154

static void delayed_auth_method_recv(su_root_magic_t *rm,
				     su_msg_r msg,
				     auth_splugin_t *u);

static void delayed_auth_method(auth_mod_t *am,
				auth_status_t *as,
				msg_auth_t *auth,
				auth_challenger_t const *ach)
{
  auth_plugin_t *ap = AUTH_PLUGIN(am);
  su_msg_r mamc = SU_MSG_R_INIT;
  auth_splugin_t *asp;

155
  if (su_msg_create(mamc,
Pekka Pessi's avatar
Pekka Pessi committed
156 157 158 159 160 161 162 163 164 165 166
		    su_root_task(ap->ap_root),
		    su_root_task(ap->ap_root),
		    delayed_auth_method_recv,
		    sizeof *asp) == SU_FAILURE) {
    as->as_status = 500;
    as->as_phrase = "Asynchronous authentication failure";
    return;
  }

  asp = su_msg_data(mamc); assert(asp);

167
  asp->asp_cookie = delayed_asp_cookie;
Pekka Pessi's avatar
Pekka Pessi committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  asp->asp_am = am;
  asp->asp_as = as;
  asp->asp_header = auth;
  asp->asp_ach = ach;
  asp->asp_canceled = 0;

  if (su_msg_send(mamc) == SU_FAILURE) {
    su_msg_destroy(mamc);
    as->as_status = 500;
    as->as_phrase = "Asynchronous authentication failure";
    return;
  }

  as->as_plugin = asp;

  as->as_status = 100;
  as->as_phrase = "Trying";

  return;
}

static void delayed_auth_method_recv(su_root_magic_t *rm,
				     su_msg_r msg,
				     auth_splugin_t *asp)
{
  auth_mod_t *am = asp->asp_am;
  auth_plugin_t *ap = AUTH_PLUGIN(am);

  if (asp->asp_canceled)
    return;

  ap->ap_base->asch_check(am, asp->asp_as, asp->asp_header, asp->asp_ach);

  if (asp->asp_as->as_callback)
    asp->asp_as->as_callback(asp->asp_as->as_magic, asp->asp_as);
}

205
static void delayed_auth_challenge(auth_mod_t *am,
Pekka Pessi's avatar
Pekka Pessi committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219
				   auth_status_t *as,
				   auth_challenger_t const *ach)
{
  auth_plugin_t *ap = AUTH_PLUGIN(am);

  /* Invoke member function of base scheme */
  ap->ap_base->asch_challenge(am, as, ach);
}

static void delayed_auth_cancel(auth_mod_t *am, auth_status_t *as)
{
  auth_plugin_t *ap = AUTH_PLUGIN(am);

  (void)ap;			/* xyzzy */
220

221
  if (as->as_plugin && as->as_plugin->asp_cookie == delayed_asp_cookie)
Pekka Pessi's avatar
Pekka Pessi committed
222 223 224 225 226 227 228 229 230 231 232 233
    as->as_plugin->asp_canceled = 1;

  as->as_status = 500, as->as_phrase = "Authentication canceled";
}

static void delayed_auth_destroy(auth_mod_t *am)
{
  auth_plugin_t *ap = AUTH_PLUGIN(am);

  /* Invoke member function of base scheme */
  ap->ap_base->asch_destroy(am);
}