Skip to content

Commit

Permalink
box: convert authentication.cc to C
Browse files Browse the repository at this point in the history
C++ features aren't really needed there. Let's drop exceptions and
convert to C to simplify further development.

While we are at it, shorten the License text, replace ifdef guards
with pragma, and clean up the include list.

Needed for tarantool#7986

NO_DOC=refactoring
NO_TEST=refactoring
NO_CHANGELOG=refactoring
  • Loading branch information
locker committed Dec 8, 2022
1 parent 1b15fab commit 4c56eb2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/box/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ set(box_sources
checkpoint_schedule.c
user_def.c
user.cc
authentication.cc
authentication.c
replication.cc
recovery.cc
xstream.cc
Expand Down
77 changes: 34 additions & 43 deletions src/box/authentication.cc → src/box/authentication.c
Original file line number Diff line number Diff line change
@@ -1,41 +1,26 @@
/*
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* Copyright 2010-2022, Tarantool AUTHORS, please see AUTHORS file.
*/
#include "authentication.h"
#include "user.h"
#include "session.h"
#include "msgpuck.h"

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>

#include "base64.h"
#include "diag.h"
#include "errcode.h"
#include "error.h"
#include <base64.h>
#include "msgpuck.h"
#include "scramble.h"
#include "session.h"
#include "user.h"
#include "user_def.h"

void
int
authenticate(const char *user_name, uint32_t user_name_len,
const char *salt, const char *tuple)
{
Expand All @@ -46,7 +31,7 @@ authenticate(const char *user_name, uint32_t user_name_len,
};
struct user *user = user_find_by_name(user_name, user_name_len);
if (user == NULL && diag_get()->last->code != ER_NO_SUCH_USER)
diag_raise();
return -1;
/*
* Check the request body as usual even if the user doesn't exist
* to prevent user enumeration by analyzing error codes.
Expand All @@ -66,8 +51,9 @@ authenticate(const char *user_name, uint32_t user_name_len,

if (part_count < 2) {
/* Expected at least: authentication mechanism and data. */
tnt_raise(ClientError, ER_INVALID_MSGPACK,
"authentication request body");
diag_set(ClientError, ER_INVALID_MSGPACK,
"authentication request body");
return -1;
}
mp_next(&tuple); /* Skip authentication mechanism. */
if (mp_typeof(*tuple) == MP_STR) {
Expand All @@ -79,26 +65,31 @@ authenticate(const char *user_name, uint32_t user_name_len,
*/
scramble = mp_decode_bin(&tuple, &scramble_len);
} else {
tnt_raise(ClientError, ER_INVALID_MSGPACK,
"authentication scramble");
diag_set(ClientError, ER_INVALID_MSGPACK,
"authentication scramble");
return -1;
}
if (scramble_len != SCRAMBLE_SIZE) {
/* Authentication mechanism, data. */
tnt_raise(ClientError, ER_INVALID_MSGPACK,
"invalid scramble size");
diag_set(ClientError, ER_INVALID_MSGPACK,
"invalid scramble size");
return -1;
}
if (user == NULL ||
scramble_check(scramble, salt, user->def->hash2) != 0) {
auth_res.is_authenticated = false;
if (session_run_on_auth_triggers(&auth_res) != 0)
diag_raise();
tnt_raise(ClientError, ER_CREDS_MISMATCH);
return -1;
diag_set(ClientError, ER_CREDS_MISMATCH);
return -1;
}
access_check_session_xc(user);
if (access_check_session(user) != 0)
return -1;
ok:
/* check and run auth triggers on success */
if (! rlist_empty(&session_on_auth) &&
session_run_on_auth_triggers(&auth_res) != 0)
diag_raise();
return -1;
credentials_reset(&current_session()->credentials, user);
return 0;
}
44 changes: 12 additions & 32 deletions src/box/authentication.h
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
#ifndef INCLUDES_TARANTOOL_BOX_AUTHENTICATION_H
#define INCLUDES_TARANTOOL_BOX_AUTHENTICATION_H
/*
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* Copyright 2010-2022, Tarantool AUTHORS, please see AUTHORS file.
*/
#pragma once

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#if defined(__cplusplus)
extern "C" {
#endif /* defined(__cplusplus) */

/**
* State passed to authentication trigger.
*/
Expand All @@ -56,10 +34,12 @@ struct on_auth_trigger_ctx {
* salt: random salt sent in the greeting message.
* tuple: value of the IPROTO_TUPLE key sent in the IPROTO_AUTH request body.
*
* Raises an exception on error.
* Returns 0 on success. On error, sets diag and returns -1.
*/
void
int
authenticate(const char *user_name, uint32_t user_name_len,
const char *salt, const char *tuple);

#endif /* INCLUDES_TARANTOOL_BOX_AUTHENTICATION_H */
#if defined(__cplusplus)
} /* extern "C" */
#endif /* defined(__cplusplus) */
3 changes: 2 additions & 1 deletion src/box/box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3250,7 +3250,8 @@ box_process_auth(struct auth_request *request, const char *salt)

const char *user = request->user_name;
uint32_t len = mp_decode_strl(&user);
authenticate(user, len, salt, request->scramble);
if (authenticate(user, len, salt, request->scramble) != 0)
diag_raise();
}

void
Expand Down
7 changes: 0 additions & 7 deletions src/box/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,6 @@ generic_session_sync(struct session *session);

#include "diag.h"

static inline void
access_check_session_xc(struct user *user)
{
if (access_check_session(user) != 0)
diag_raise();
}

static inline void
access_check_universe_xc(user_access_t access)
{
Expand Down

0 comments on commit 4c56eb2

Please sign in to comment.