Skip to content

that is the WIP for the auth branch #2828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
2d4e965
added mysql basic auth
tomatolog Dec 8, 2024
d2879e1
added use of the htpasswd compatible file and check for multiple user…
tomatolog Dec 10, 2024
c6b9680
added auth into HTTP interface; fixed buddy requests to skip auth cod…
tomatolog Dec 11, 2024
a803bc2
merged master into wip_auth
tomatolog Dec 12, 2024
b9df610
Merge branch 'master' into wip_auth
tomatolog Dec 17, 2024
e53a63d
merged master into wip_auth
tomatolog Jan 10, 2025
62c5b11
added auth into API interface; fixed php and libsphinxclient to suppo…
tomatolog Jan 10, 2025
27f80e3
fixed update mva API method; fixed smoke test at libsphinxclient
tomatolog Jan 10, 2025
78fec75
merged master into wip_auth
tomatolog Jan 17, 2025
4ea2737
replaced APIHeader without token with APIAnswer;
tomatolog Jan 17, 2025
e8402d0
Add authentification test
donhardman Feb 5, 2025
573b9fb
merged master into wip_auth
tomatolog Feb 27, 2025
ce50e74
added load of users and permissions from the json file; changed http …
tomatolog Feb 27, 2025
264c8e2
merged master into wip_auth
tomatolog Mar 12, 2025
1198a7d
Merge remote-tracking branch 'remotes/gh/master' into wip_auth
tomatolog Mar 20, 2025
93d6a85
fixed replication to work without auth; added show users, show permis…
tomatolog Mar 20, 2025
bae1471
merged master into wip_auth
tomatolog Apr 22, 2025
d26cef2
fixed statements without target; added loading and show of the budget;
tomatolog Apr 22, 2025
cc8e834
fixed linux build; disabled user auth check at ubertests
tomatolog Apr 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 200 additions & 23 deletions api/libsphinxclient/sphinxclient.c

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions api/libsphinxclient/sphinxclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ sphinx_keyword_info * sphinx_build_keywords ( sphinx_client * client, const c
char ** sphinx_status ( sphinx_client * client, int * num_rows, int * num_cols );
char ** sphinx_status_extended ( sphinx_client * client, int * num_rows, int * num_cols, int local );
void sphinx_status_destroy ( char ** status, int num_rows, int num_cols );
sphinx_bool sphinx_set_user ( sphinx_client * client, const char * user, const char * password );

/////////////////////////////////////////////////////////////////////////////

Expand Down
43 changes: 35 additions & 8 deletions api/libsphinxclient/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ void test_excerpt ( sphinx_client * client )
g_failed += ( res==NULL );
if ( !g_smoke )
die ( "query failed: %s", sphinx_error(client) );
} else
{
for ( i=0; i<ndocs; i++ )
printf ( "n=%d, res=%s\n", 1+i, res[i] );
}

for ( i=0; i<ndocs; i++ )
printf ( "n=%d, res=%s\n", 1+i, res[i] );
}


Expand Down Expand Up @@ -205,11 +206,18 @@ void test_excerpt_spz ( sphinx_client * client )

res = sphinx_build_excerpts ( client, ndocs, (const char **)docs, index, words, &opts );
if ( !res )
die ( "query failed: %s", sphinx_error(client) );
{
if ( !g_smoke )
die ( "query failed: %s", sphinx_error(client) );
else
printf ( "query failed: %s", sphinx_error(client) );

for ( i=0; i<ndocs; i++ )
printf ( "n=%d, res=%s\n", 1+i, res[i] );
printf ( "\n" );
} else
{
for ( i=0; i<ndocs; i++ )
printf ( "n=%d, res=%s\n", 1+i, res[i] );
printf ( "\n" );
}
}
}

Expand Down Expand Up @@ -426,17 +434,32 @@ void title ( const char * name )

int main ( int argc, char ** argv )
{
int i, port = 0;
int i, port = 9312;
sphinx_client * client;
// sphinx_uint64_t override_docid = 2;
// unsigned int override_value = 2000;
const char * user = NULL;
const char * password = NULL;

for ( i=1; i<argc; i++ )
{
if ( strcmp ( argv[i], "--smoke" )==0 )
g_smoke = SPH_TRUE;
else if ( strcmp ( argv[i], "--port" )==0 && i+1<argc )
{
port = (int)strtoul ( argv[i+1], NULL, 10 );
i++;
}
else if ( strcmp ( argv[i], "--user" )==0 && i+1<argc )
{
user = argv[i+1];
i++;
}
else if ( strcmp ( argv[i], "--password" )==0 && i+1<argc )
{
password = argv[i+1];
i++;
}
}

net_init ();
Expand All @@ -447,6 +470,8 @@ int main ( int argc, char ** argv )

if ( port )
sphinx_set_server ( client, "127.0.0.1", port );
if ( user )
sphinx_set_user ( client, user, password );

sphinx_set_match_mode ( client, SPH_MATCH_EXTENDED2 );
sphinx_set_sort_mode ( client, SPH_SORT_RELEVANCE, NULL );
Expand Down Expand Up @@ -495,6 +520,8 @@ int main ( int argc, char ** argv )
test_query ( client, "is", "test1" );

sphinx_cleanup ( client );
if ( user )
sphinx_set_user ( client, user, password );

// group_by (attr; mva) + filter + post update
title ( "group_by (attr; mva) + filter + post update" );
Expand Down
69 changes: 60 additions & 9 deletions api/sphinxapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
define ( "SPH_UPDATE_STRING", 2 );
define ( "SPH_UPDATE_JSON", 3 );

/// known auth types
define ( "AUTH_NO", 1 );
define ( "AUTH_SHA1", 2 );
define ( "AUTH_SHA256", 3 );


// important properties of PHP's integers:
// - always signed (one bit short of PHP_INT_SIZE)
// - conversion from string to int is saturated
Expand Down Expand Up @@ -459,6 +465,9 @@ class SphinxClient
var $_token_filter_library; ///< token_filter plugin library name
var $_token_filter_name; ///< token_filter plugin name
var $_token_filter_opts; ///< token_filter plugin options

var $user; ///< user name
var $user_token; ///< user hashed password

var $_error; ///< last error message
var $_warning; ///< last warning message
Expand Down Expand Up @@ -528,6 +537,9 @@ function __construct ()
$this->_mbenc = "";
$this->_arrayresult = false;
$this->_timeout = 0;

$this->user = '';
$this->user_token = '';
}

function __destruct()
Expand Down Expand Up @@ -584,8 +596,18 @@ function SetConnectTimeout ( $timeout )
}


function _Send ( $handle, $data, $length )
function _Send ( $handle, $data, $length, $add_auth )
{
if ( $add_auth )
{
// auth data head
$auth_head = $this->GetUserData();
$length += strlen ( $auth_head );

$auth_head .= $data;
$data = $auth_head;
}

if ( feof($handle) || fwrite ( $handle, $data, $length ) !== $length )
{
$this->_error = 'connection unexpectedly closed (timed out?)';
Expand Down Expand Up @@ -666,7 +688,7 @@ function _Connect ()
// this is a subtle part. we must do it before (!) reading back from searchd.
// because otherwise under some conditions (reported on FreeBSD for instance)
// TCP stack could throttle write-write-read pattern because of Nagle.
if ( !$this->_Send ( $fp, pack ( "N", 1 ), 4 ) )
if ( !$this->_Send ( $fp, pack ( "N", 2 ), 4, false ) )
{
fclose ( $fp );
$this->_error = "failed to send client protocol version";
Expand Down Expand Up @@ -1294,7 +1316,7 @@ function RunQueries ()
$len = 8+strlen($req);
$req = pack ( "nnNNN", SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, 0, $nreqs ) . $req; // add header

if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
if ( !( $this->_Send ( $fp, $req, $len+8, true ) ) ||
!( $response = $this->_GetResponse ( $fp, VER_COMMAND_SEARCH ) ) )
{
$this->_MBPop ();
Expand Down Expand Up @@ -1577,7 +1599,7 @@ function BuildExcerpts ( $docs, $index, $words, $opts=array() )

$len = strlen($req);
$req = pack ( "nnN", SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len ) . $req; // add header
if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
if ( !( $this->_Send ( $fp, $req, $len+8, true ) ) ||
!( $response = $this->_GetResponse ( $fp, VER_COMMAND_EXCERPT ) ) )
{
$this->_MBPop ();
Expand Down Expand Up @@ -1647,7 +1669,7 @@ function BuildKeywords ( $query, $index, $hits )

$len = strlen($req);
$req = pack ( "nnN", SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len ) . $req; // add header
if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
if ( !( $this->_Send ( $fp, $req, $len+8, true ) ) ||
!( $response = $this->_GetResponse ( $fp, VER_COMMAND_KEYWORDS ) ) )
{
$this->_MBPop ();
Expand Down Expand Up @@ -1781,7 +1803,7 @@ function UpdateAttributes ( $index, $attrs, $values, $type=SPH_UPDATE_INT, $igno

$len = strlen($req);
$req = pack ( "nnN", SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len ) . $req; // add header
if ( !$this->_Send ( $fp, $req, $len+8 ) )
if ( !$this->_Send ( $fp, $req, $len+8, true ) )
{
$this->_MBPop ();
return -1;
Expand Down Expand Up @@ -1815,7 +1837,7 @@ function Open()

// command, command version = 0, body length = 4, body = 1
$req = pack ( "nnNN", SEARCHD_COMMAND_PERSIST, 0, 4, 1 );
if ( !$this->_Send ( $fp, $req, 12 ) )
if ( !$this->_Send ( $fp, $req, 12, true ) )
return false;

$this->_socket = $fp;
Expand Down Expand Up @@ -1852,7 +1874,7 @@ function Status ($session=false)
}

$req = pack ( "nnNN", SEARCHD_COMMAND_STATUS, VER_COMMAND_STATUS, 4, $session?0:1 ); // len=4, body=1
if ( !( $this->_Send ( $fp, $req, 12 ) ) ||
if ( !( $this->_Send ( $fp, $req, 12, true ) ) ||
!( $response = $this->_GetResponse ( $fp, VER_COMMAND_STATUS ) ) )
{
$this->_MBPop ();
Expand Down Expand Up @@ -1888,7 +1910,7 @@ function FlushAttributes ()
}

$req = pack ( "nnN", SEARCHD_COMMAND_FLUSHATTRS, VER_COMMAND_FLUSHATTRS, 0 ); // len=0
if ( !( $this->_Send ( $fp, $req, 8 ) ) ||
if ( !( $this->_Send ( $fp, $req, 8, true ) ) ||
!( $response = $this->_GetResponse ( $fp, VER_COMMAND_FLUSHATTRS ) ) )
{
$this->_MBPop ();
Expand All @@ -1904,6 +1926,35 @@ function FlushAttributes ()
$this->_MBPop ();
return $tag;
}

//////////////////////////////////////////////////////////////////////////
// user
//////////////////////////////////////////////////////////////////////////

function SetUser ($user, $password)
{
$this->user = $user;

$password_hash = hash ( "sha256", $password, true );
$pwd_hash = hash ( "sha256", $user . $password_hash, true );
$this->user_token = $pwd_hash;
}

function GetUserData ()
{
$data = '';
if ( $this->user=='' )
{
$data .= pack ( "C", AUTH_NO );
} else
{
$data .= pack ( "C", AUTH_SHA256 );
$data .= $this->user_token;
}

return $data;
}

}

//
Expand Down
2 changes: 1 addition & 1 deletion manual/References.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ spelldump [options] <dictionary> <affix> [result] [locale-name]
A comprehensive alphabetical list of keywords currently reserved in Manticore SQL syntax (thus, they cannot be used as identifiers).

```
AND, AS, BY, COLUMNARSCAN, DISTINCT, DIV, DOCIDINDEX, EXPLAIN, FACET, FALSE, FORCE, FROM, IGNORE, IN, INDEXES, INNER, IS, JOIN, KNN, LEFT, LIMIT, MOD, NOT, NO_COLUMNARSCAN, NO_DOCIDINDEX, NO_SECONDARYINDEX, NULL, OFFSET, ON, OR, ORDER, RELOAD, SECONDARYINDEX, SELECT, SYSFILTERS, TRUE
AND, AS, BY, COLUMNARSCAN, DISTINCT, DIV, DOCIDINDEX, EXPLAIN, FACET, FALSE, FORCE, FROM, IGNORE, IN, INDEXES, INNER, IS, JOIN, KNN, LEFT, LIMIT, MOD, NOT, NO_COLUMNARSCAN, NO_DOCIDINDEX, NO_SECONDARYINDEX, NULL, OFFSET, ON, OR, ORDER, RELOAD, SECONDARYINDEX, SELECT, SYSFILTERS, TOKEN, TRUE
```

## Documentation for old Manticore versions
Expand Down
14 changes: 11 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ set ( SEARCHD_H searchdaemon.h searchdconfig.h searchdddl.h searchdexpr.h search
compressed_zlib_mysql.h sphinxql_debug.h stackmock.h searchdssl.h digest_sha1.h
client_session.h compressed_zstd_mysql.h docs_collector.h index_rotator.h config_reloader.h searchdhttp.h timeout_queue.h
netpoll.h pollable_event.h netfetch.h searchdbuddy.h sphinxql_second.h sphinxql_extra.h sphinxjsonquery.h
frontendschema.h debug_cmds.h dynamic_idx.h sphinxexcerpt.h)
frontendschema.h debug_cmds.h dynamic_idx.h sphinxexcerpt.h
auth/auth.h auth/auth_common.h auth/auth_proto_http.h auth/auth_proto_mysql.h auth/auth_proto_api.h digest_sha256.h auth/auth_perms.h)


# add the extra targets in the case we want on-the-fly grammar compiler
Expand Down Expand Up @@ -255,11 +256,15 @@ add_library ( lsearchd OBJECT searchdha.cpp http/http_parser.c searchdhttp.cpp
netreceive_http.cpp netreceive_ql.cpp query_status.cpp
sphinxql_debug.cpp sphinxql_second.cpp stackmock.cpp docs_collector.cpp index_rotator.cpp config_reloader.cpp netpoll.cpp
pollable_event.cpp netfetch.cpp searchdbuddy.cpp searchdhttpcompat.cpp sphinxql_extra.cpp searchdreplication.cpp sphinxjsonquery.cpp
frontendschema.cpp compressed_http.cpp debug_cmds.cpp jsonqueryfilter.cpp dynamic_idx.cpp sphinxexcerpt.cpp searchdexpr.cpp)
frontendschema.cpp compressed_http.cpp debug_cmds.cpp jsonqueryfilter.cpp dynamic_idx.cpp sphinxexcerpt.cpp searchdexpr.cpp
auth/auth.cpp auth/auth_common.cpp auth/auth_proto_http.cpp auth/auth_proto_mysql.cpp auth/auth_proto_api.cpp auth/auth_perms.cpp)

target_sources ( lsearchd PUBLIC ${SEARCHD_SRCS_TESTABLE} ${SEARCHD_H} ${SEARCHD_BISON} ${SEARCHD_FLEX} )
add_library ( digest_sha1 digest_sha1.cpp )
add_library ( digest_sha256 digest_sha256.cpp )
target_link_libraries ( digest_sha1 PRIVATE lextra )
target_link_libraries ( lsearchd PUBLIC digest_sha1 lextra nlohmann_json::nlohmann_json )
target_link_libraries ( digest_sha256 PRIVATE lextra )
target_link_libraries ( lsearchd PUBLIC digest_sha1 digest_sha256 lextra nlohmann_json::nlohmann_json )
target_link_libraries ( lsearchd INTERFACE Boost::filesystem )

function (stackmock processors compiler versions config values)
Expand Down Expand Up @@ -332,9 +337,12 @@ if (WITH_SSL)
target_link_libraries ( searchd_ssl PRIVATE OpenSSL::Crypto )
target_link_libraries ( digest_sha1 PRIVATE OpenSSL::SSL )
target_link_libraries ( digest_sha1 PRIVATE OpenSSL::Crypto )
target_link_libraries ( digest_sha256 PRIVATE OpenSSL::SSL )
target_link_libraries ( digest_sha256 PRIVATE OpenSSL::Crypto )
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
target_link_options ( searchd_ssl INTERFACE $<${ONLYGNUCLANGC_CXX}:-Wl,--exclude-libs,libssl.a,--exclude-libs,libcrypto.a> )
target_link_options ( digest_sha1 INTERFACE $<${ONLYGNUCLANGC_CXX}:-Wl,--exclude-libs,libssl.a,--exclude-libs,libcrypto.a> )
target_link_options ( digest_sha256 INTERFACE $<${ONLYGNUCLANGC_CXX}:-Wl,--exclude-libs,libssl.a,--exclude-libs,libcrypto.a> )
endif()
target_compile_options ( searchd_ssl PRIVATE "$<$<COMPILE_LANG_AND_ID:CXX,GNU>:-Wno-deprecated-declarations>" )
include ( CheckFunctionExists )
Expand Down
Loading
Loading