auth_common.c 4.63 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * 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_common.c
 *
 * Functions common to both client and server authentication.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Original Created: Thu Feb 22 12:10:37 2001 ppessi
 * @date Created: Wed May 17 13:37:50 EEST 2006 ppessi
 */

#include "config.h"

37 38
#include "sofia-sip/auth_common.h"
#include "sofia-sip/msg_header.h"
39
#include <sofia-sip/su_string.h>
40

41
#include <stdio.h>
Pekka Pessi's avatar
Pekka Pessi committed
42 43 44 45
#include <string.h>
#include <stdarg.h>
#include <assert.h>

46
su_inline int has_token(char const *qstring, char const *token);
Pekka Pessi's avatar
Pekka Pessi committed
47 48 49 50 51 52 53

/**
 * Parse authentication parameters.
 *
 * The function auth_get_params() searches for the authentication parameters
 * in @a params. The parameter list @a params is seached for each parameter
 * given in in vararg section, and if it is found, its value is assigned to
54
 * the given address.
Pekka Pessi's avatar
Pekka Pessi committed
55 56 57 58 59 60 61
 *
 * @note The field name should contain the equal ("=") sign.
 *
 * @return
 * The function auth_get_params() returns number of parameters found in
 * params, or -1 upon an error.
 */
62 63 64
issize_t auth_get_params(su_home_t *home,
			 char const * const params[], ...
			 /* char const *fmt, char const **return_value */)
Pekka Pessi's avatar
Pekka Pessi committed
65
{
66 67
  int n, j;
  size_t len, namelen;
Pekka Pessi's avatar
Pekka Pessi committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  char const *fmt, *expected;
  char const *value, *p, **return_value;
  va_list(ap);

  assert(params);

  if (!params) return -1;

  va_start(ap, params);

  for (n = 0; (fmt = va_arg(ap, char const *));) {
    return_value = va_arg(ap, char const **);
    len = strlen(fmt);
    if (!len)
      continue;
    namelen = strcspn(fmt, "=");
    expected = fmt + namelen + 1;
    value = NULL;

    if (expected[0]) {
      /* value match: format is name=expected,
	 if expected is found in parameter value,
	 return non-NULL pointer in *return_value */
      for (j = 0; (p = params[j++]);) {
92
	if (su_casematch(p, fmt)) {
Pekka Pessi's avatar
Pekka Pessi committed
93 94 95 96
	  /* Matched the whole parameter with fmt name=expected */
	  value = p;
	  break;
	}
97
	else if (!su_casenmatch(p, fmt, namelen) ||
Pekka Pessi's avatar
Pekka Pessi committed
98 99 100 101 102 103 104 105 106 107 108
		 p[namelen] != '=')
	  continue;

	p = p + namelen + 1;

	if (p[0] == '"' && has_token(p, expected)) {
	  /* Quoted parameter value has expected value,
	   * e.g., qop=auth matches qop="auth,auth-int" */
	  value = p;
	  break;
	}
109
	else if (su_casematch(p, expected)) {
Pekka Pessi's avatar
Pekka Pessi committed
110 111 112 113 114 115 116 117 118 119
	  /* Parameter value matches with extected value
	   * e.g., qop=auth matches qop=auth */
	  value = p;
	  break;
	}
      }
    }
    else {
      /* format is name= , return unquoted parameter value after = */
      for (j = 0; (p = params[j++]);) {
120
	if (!su_casenmatch(p, fmt, len))
Pekka Pessi's avatar
Pekka Pessi committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	  continue;

	if (p[len] == '"')
	  value = msg_unquote_dup(home, p + len);
	else
	  value = su_strdup(home, p + len);

	if (value == NULL)
	  return -1;

	break;
      }
    }

    if (value) {
      *return_value = value;
      n++;
    }
  }

  va_end(ap);

  return n;
}

146
int auth_struct_copy(void *dst, void const *src, isize_t s_size)
Pekka Pessi's avatar
Pekka Pessi committed
147 148 149
{
  int d_size = *(int *)dst;

150 151 152
  if (d_size < 0)
    return -1;

153
  if ((size_t)d_size > s_size) {
Pekka Pessi's avatar
Pekka Pessi committed
154 155 156 157 158 159 160 161 162 163
    memcpy(dst, src, s_size);
    memset((char *)dst + s_size, 0, d_size - s_size);
  }
  else {
    memcpy(dst, src, d_size);
    *(int *)dst = d_size;
  }
  return 0;
}

164
su_inline int has_token(char const *qstring, char const *token)
Pekka Pessi's avatar
Pekka Pessi committed
165
{
166
  size_t n = strlen(token);
Pekka Pessi's avatar
Pekka Pessi committed
167 168
  char const *q;

169 170
  q = su_strcasestr(qstring, token);

Pekka Pessi's avatar
Pekka Pessi committed
171 172 173 174 175 176 177 178 179
  return (q &&
	  (q[n] == 0 || strchr("\", \t", q[n])) &&
	  (q == qstring || strchr("\", \t", q[-1])));
}


/** Compare two strings, even if they are quoted */
int auth_strcmp(char const *quoted, char const *unquoted)
{
180
  size_t i, j;
Pekka Pessi's avatar
Pekka Pessi committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

  if (quoted[0] != '"')
    return strcmp(quoted, unquoted);

  /* Compare quoted with unquoted  */

  for (i = 1, j = 0; ; i++, j++) {
    char q = quoted[i], u = unquoted[j];

    if (q == '"')
      q = '\0';
    else if (q == '\\' && u != '\0')
      q = quoted[i++];

    if (q - u)
      return q - u;

    if (q == '\0')
      return 0;
  }
}