auth_plugin.c 3.57 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
 * 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
 *
 */

Pekka Pessi's avatar
Pekka Pessi committed
25
/**@internal
Pekka Pessi's avatar
Pekka Pessi committed
26 27
 * @file auth_plugin.c
 * @brief Plugin interface for authentication verification modules.
28
 *
Pekka Pessi's avatar
Pekka Pessi committed
29
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
30
 *
Pekka Pessi's avatar
Pekka Pessi committed
31 32 33 34 35 36 37 38 39 40 41
 * @date Created: Tue Apr 27 15:23:31 2004 ppessi
 */

#include "config.h"

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

42
#include <sofia-sip/auth_digest.h>
Pekka Pessi's avatar
Pekka Pessi committed
43

44 45 46 47 48
#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
static char const __func__[] = "auth_plugin";
Pekka Pessi's avatar
Pekka Pessi committed
49 50
#endif

51
#include <sofia-sip/su_debug.h>
Pekka Pessi's avatar
Pekka Pessi committed
52

53 54 55
#include <sofia-sip/su_wait.h>
#include <sofia-sip/su_alloc.h>
#include <sofia-sip/su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
56

57 58
#include "sofia-sip/auth_module.h"
#include "sofia-sip/auth_plugin.h"
Pekka Pessi's avatar
Pekka Pessi committed
59 60 61 62 63 64 65 66 67 68 69 70 71

extern auth_scheme_t auth_scheme_basic[];
extern auth_scheme_t auth_scheme_digest[];
extern auth_scheme_t auth_scheme_delayed[];

enum { N = 32 };

static auth_scheme_t *schemes[N] = {
  auth_scheme_basic,
  auth_scheme_digest,
  auth_scheme_delayed
};

72
/** Register an authentication plugin.
Pekka Pessi's avatar
Pekka Pessi committed
73 74 75 76 77 78 79 80 81 82 83 84
 *
 * @retval 0 when successful
 * @retval -1 upon an error
 */
int auth_mod_register_plugin(auth_scheme_t *asch)
{
  int i;

  for (i = 0; schemes[i]; i++) {
    if (i == N)
      return -1;
  }
85

Pekka Pessi's avatar
Pekka Pessi committed
86 87 88 89 90
  schemes[i] = asch;

  return 0;
}

91
/**Create an authentication plugin module.
Pekka Pessi's avatar
Pekka Pessi committed
92 93 94
 *
 * The function auth_mod_create() creates a module used to authenticate the
 * requests.
95
 *
Pekka Pessi's avatar
Pekka Pessi committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
 * @param root pointer to a su_root_t object
 * @param tag,value,... tagged argument list
 *
 * @TAGS
 * AUTHTAG_METHOD(), AUTHTAG_REALM(), AUTHTAG_DB(), AUTHTAG_ALLOW(),
 * AUTHTAG_QOP(), AUTHTAG_ALGORITHM(), AUTHTAG_EXPIRES(),
 * AUTHTAG_BLACKLIST(), AUTHTAG_FORBIDDEN(), AUTHTAG_ANONYMOUS(),
 * AUTHTAG_REMOTE().
 */
auth_mod_t *auth_mod_create(su_root_t *root,
			    tag_type_t tag, tag_value_t value, ...)
{
  auth_mod_t *am = NULL;

  ta_list ta;

  char const *method = NULL;

  ta_start(ta, tag, value);

  tl_gets(ta_args(ta),
	  AUTHTAG_METHOD_REF(method),
	  TAG_NULL());

  if (method) {
    auth_scheme_t *bscheme = NULL;
    char const *base;
123
    size_t len;
Pekka Pessi's avatar
Pekka Pessi committed
124 125

    base = strrchr(method, '+');
126
    if (base)
Pekka Pessi's avatar
Pekka Pessi committed
127 128 129 130 131 132 133 134
      len = base++ - method;
    else
      len = strlen(method);

    if (base == NULL)
      ;
    else if (strcasecmp(base, "Basic") == 0)
      bscheme = auth_scheme_basic;
135
    else if (strcasecmp(base, "Digest") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      bscheme = auth_scheme_digest;

    if (base == NULL || bscheme) {
      int i;

      for (i = 0; schemes[i] && i < N; i++) {
	if (strncasecmp(schemes[i]->asch_method, method, len) == 0 &&
	    schemes[i]->asch_method[len] == 0) {
	  am = auth_mod_alloc(schemes[i], ta_tags(ta));
	  if (schemes[i]->asch_init(am, bscheme, root, ta_tags(ta)) == -1) {
	    auth_mod_destroy(am), am = NULL;
	  }
	  break;
	}
      }
    }
  }

  ta_end(ta);
155

Pekka Pessi's avatar
Pekka Pessi committed
156 157
  return am;
}