gsasl-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CVS gsasl/lib/login


From: gsasl-commit
Subject: CVS gsasl/lib/login
Date: Sat, 18 Sep 2004 18:29:29 +0200

Update of /home/cvs/gsasl/lib/login
In directory dopio:/tmp/cvs-serv31305/lib/login

Modified Files:
        Makefile.am 
Added Files:
        client.c server.c 
Removed Files:
        login.c 
Log Message:
Cleanup LOGIN.


--- /home/cvs/gsasl/lib/login/Makefile.am       2004/04/16 11:16:39     1.5
+++ /home/cvs/gsasl/lib/login/Makefile.am       2004/09/18 16:29:29     1.6
@@ -21,4 +21,12 @@
 AM_CPPFLAGS = -I$(srcdir)/../src -I../src
 
 noinst_LTLIBRARIES = libgsasl-login.la
-libgsasl_login_la_SOURCES = login.h login.c
+libgsasl_login_la_SOURCES = login.h
+
+if CLIENT
+libgsasl_login_la_SOURCES += client.c
+endif
+
+if SERVER
+libgsasl_login_la_SOURCES += server.c
+endif

--- /home/cvs/gsasl/lib/login/client.c  2004/09/18 16:29:29     NONE
+++ /home/cvs/gsasl/lib/login/client.c  2004/09/18 16:29:29     1.1
/* client.c --- Non-standard SASL mechanism LOGIN, client side.
 * Copyright (C) 2002, 2003, 2004  Simon Josefsson
 *
 * This file is part of GNU SASL Library.
 *
 * GNU SASL 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.
 *
 * GNU SASL 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 GNU SASL Library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 *
 */

#include "login.h"

struct _Gsasl_login_client_state
{
  int step;
};

int
_gsasl_login_client_start (Gsasl_session_ctx * sctx, void **mech_data)
{
  struct _Gsasl_login_client_state *state;

  state = malloc (sizeof (*state));
  if (state == NULL)
    return GSASL_MALLOC_ERROR;

  state->step = 0;

  *mech_data = state;

  return GSASL_OK;
}

int
_gsasl_login_client_step (Gsasl_session_ctx * sctx,
                          void *mech_data,
                          const char *input, size_t input_len,
                          char **output, size_t * output_len)
{
  struct _Gsasl_login_client_state *state = mech_data;
  const char *p;
  char *tmp;
  int res;

  switch (state->step)
    {
    case 0:
      p = gsasl_property_get (sctx, GSASL_CLIENT_AUTHZID);
      if (!p)
        return GSASL_NO_AUTHZID;

      tmp = gsasl_stringprep_nfkc (p, -1);
      if (tmp == NULL)
        return GSASL_UNICODE_NORMALIZATION_ERROR;

      *output = tmp;
      *output_len = strlen (tmp);

      state->step++;
      res = GSASL_NEEDS_MORE;
      break;

    case 1:
      p = gsasl_property_get (sctx, GSASL_CLIENT_PASSWORD);
      if (!p)
        return GSASL_NO_PASSWORD;

      tmp = gsasl_stringprep_nfkc (p, -1);
      if (tmp == NULL)
        return GSASL_UNICODE_NORMALIZATION_ERROR;

      *output = tmp;
      *output_len = strlen (tmp);

      state->step++;
      res = GSASL_OK;
      break;

    default:
      res = GSASL_MECHANISM_CALLED_TOO_MANY_TIMES;
      break;
    }

  return res;
}

int
_gsasl_login_client_finish (Gsasl_session_ctx * sctx, void *mech_data)
{
  struct _Gsasl_login_client_state *state = mech_data;

  free (state);

  return GSASL_OK;
}
--- /home/cvs/gsasl/lib/login/server.c  2004/09/18 16:29:29     NONE
+++ /home/cvs/gsasl/lib/login/server.c  2004/09/18 16:29:29     1.1
/* server.c --- Non-standard SASL mechanism LOGIN, server side.
 * Copyright (C) 2002, 2003, 2004  Simon Josefsson
 *
 * This file is part of GNU SASL Library.
 *
 * GNU SASL 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.
 *
 * GNU SASL 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 GNU SASL Library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 *
 */

#include "login.h"

struct _Gsasl_login_server_state
{
  int step;
  char *username;
};

#define CHALLENGE_USERNAME "User Name"
#define CHALLENGE_PASSWORD "Password"

int
_gsasl_login_server_start (Gsasl_session_ctx * sctx, void **mech_data)
{
  struct _Gsasl_login_server_state *state;
  Gsasl_ctx *ctx;

  ctx = gsasl_server_ctx_get (sctx);
  if (ctx == NULL)
    return GSASL_CANNOT_GET_CTX;

  if (gsasl_server_callback_validate_get (ctx) == NULL &&
      gsasl_server_callback_retrieve_get (ctx) == NULL)
    return GSASL_NEED_SERVER_VALIDATE_CALLBACK;

  state = malloc (sizeof (*state));
  if (state == NULL)
    return GSASL_MALLOC_ERROR;

  state->step = 0;
  state->username = NULL;

  *mech_data = state;

  return GSASL_OK;
}

int
_gsasl_login_server_step (Gsasl_session_ctx * sctx,
                          void *mech_data,
                          const char *input, size_t input_len,
                          char *output, size_t * output_len)
{
  struct _Gsasl_login_server_state *state = mech_data;
  Gsasl_server_callback_validate cb_validate;
  Gsasl_server_callback_retrieve cb_retrieve;
  Gsasl_ctx *ctx;
  char *password;
  int res;

  ctx = gsasl_server_ctx_get (sctx);
  if (ctx == NULL)
    return GSASL_CANNOT_GET_CTX;

  cb_validate = gsasl_server_callback_validate_get (ctx);
  cb_retrieve = gsasl_server_callback_retrieve_get (ctx);
  if (cb_validate == NULL && cb_retrieve == NULL)
    return GSASL_NEED_SERVER_VALIDATE_CALLBACK;

  switch (state->step)
    {
    case 0:
      if (*output_len < strlen (CHALLENGE_USERNAME))
        return GSASL_TOO_SMALL_BUFFER;

      memcpy (output, CHALLENGE_USERNAME, strlen (CHALLENGE_USERNAME));
      *output_len = strlen (CHALLENGE_USERNAME);

      state->step++;
      res = GSASL_NEEDS_MORE;
      break;

    case 1:
      if (input_len == 0)
        return GSASL_MECHANISM_PARSE_ERROR;

      if (*output_len < strlen (CHALLENGE_PASSWORD))
        return GSASL_TOO_SMALL_BUFFER;

      state->username = malloc (input_len + 1);
      if (state->username == NULL)
        return GSASL_MALLOC_ERROR;

      memcpy (state->username, input, input_len);
      state->username[input_len] = '\0';

      memcpy (output, CHALLENGE_PASSWORD, strlen (CHALLENGE_PASSWORD));
      *output_len = strlen (CHALLENGE_PASSWORD);

      state->step++;
      res = GSASL_NEEDS_MORE;
      break;

    case 2:
      if (input_len == 0)
        return GSASL_MECHANISM_PARSE_ERROR;

      password = malloc (input_len + 1);
      if (password == NULL)
        return GSASL_MALLOC_ERROR;

      memcpy (password, input, input_len);
      password[input_len] = '\0';

      if (cb_validate)
        {
          res = cb_validate (sctx, state->username, NULL, password);
        }
      else
        {
          size_t keylen;
          char *key;
          char *normkey;

          res =
            cb_retrieve (sctx, state->username, NULL, NULL, NULL, &keylen);
          if (res != GSASL_OK)
            return res;
          key = malloc (keylen);
          if (key == NULL)
            return GSASL_MALLOC_ERROR;
          res = cb_retrieve (sctx, state->username, NULL, NULL, key, &keylen);
          if (res != GSASL_OK)
            {
              free (key);
              return res;
            }
          normkey = gsasl_stringprep_nfkc (key, keylen);
          free (key);
          if (normkey == NULL)
            return GSASL_UNICODE_NORMALIZATION_ERROR;
          if (strlen (password) == strlen (normkey) &&
              memcmp (normkey, password, strlen (normkey)) == 0)
            res = GSASL_OK;
          else
            res = GSASL_AUTHENTICATION_ERROR;
          free (normkey);
        }

      free (password);

      *output_len = 0;
      state->step++;
      break;

    default:
      res = GSASL_MECHANISM_CALLED_TOO_MANY_TIMES;
      break;
    }

  return res;
}

int
_gsasl_login_server_finish (Gsasl_session_ctx * sctx, void *mech_data)
{
  struct _Gsasl_login_server_state *state = mech_data;

  if (state->username)
    free (state->username);
  free (state);

  return GSASL_OK;
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]