odissey: implement auth query string format

This commit is contained in:
Dmitry Simonenko 2017-10-05 16:42:30 +03:00
parent 32e2bb6484
commit e72af35672
4 changed files with 62 additions and 9 deletions

View File

@ -81,7 +81,7 @@ database default
authentication "none"
#password ""
#auth_query "select 'user', 'password'"
#auth_query "select username, pass from auth where username='%u'"
#auth_query_user ""
#auth_query_db ""

View File

@ -104,7 +104,9 @@ od_auth_frontend_cleartext(od_client_t *client)
shapito_password_init(&client_password);
if (client->scheme->auth_query) {
rc = od_auth_query(client->system, client->scheme, &client_password);
rc = od_auth_query(client->system, client->scheme,
client->startup.user,
&client_password);
if (rc == -1) {
od_error(&instance->logger, "auth", client, NULL,
"failed to make auth_query");
@ -200,7 +202,9 @@ od_auth_frontend_md5(od_client_t *client)
shapito_password_init(&query_password);
if (client->scheme->auth_query) {
rc = od_auth_query(client->system, client->scheme, &query_password);
rc = od_auth_query(client->system, client->scheme,
client->startup.user,
&query_password);
if (rc == -1) {
od_error(&instance->logger, "auth", client, NULL,
"failed to make auth_query");
@ -210,6 +214,7 @@ od_auth_frontend_md5(od_client_t *client)
shapito_password_free(&query_password);
return -1;
}
query_password.password_len--;
} else {
query_password.password_len = client->scheme->password_len;
query_password.password = client->scheme->password;

View File

@ -53,6 +53,9 @@ od_auth_query_do(od_server_t *server, char *query, int len,
shapito_password_t *result)
{
od_instance_t *instance = server->system->instance;
od_debug(&instance->logger, "auth_query", server->client, server,
"%s", query);
int rc;
shapito_stream_t *stream = &server->stream;
shapito_stream_reset(stream);
@ -172,7 +175,49 @@ od_auth_query_do(od_server_t *server, char *query, int len,
return 0;
}
__attribute__((hot)) static inline int
od_auth_query_format(od_schemeroute_t *scheme, shapito_parameter_t *user,
char *output, int output_len)
{
char *dst_pos = output;
char *dst_end = output + output_len;
char *format_pos = scheme->auth_query;
char *format_end = scheme->auth_query + strlen(scheme->auth_query);
while (format_pos < format_end)
{
if (*format_pos == '%') {
format_pos++;
if (od_unlikely(format_pos == format_end))
break;
if (*format_pos == 'u') {
int len;
len = snprintf(dst_pos, dst_end - dst_pos, "%s",
shapito_parameter_value(user));
dst_pos += len;
} else {
if (od_unlikely((dst_end - dst_pos) < 2))
break;
dst_pos[0] = '%';
dst_pos[1] = *format_pos;
dst_pos += 2;
}
} else {
if (od_unlikely((dst_end - dst_pos) < 1))
break;
dst_pos[0] = *format_pos;
dst_pos += 1;
}
format_pos++;
}
if (od_unlikely((dst_end - dst_pos) < 1))
return -1;
dst_pos[0] = 0;
dst_pos++;
return dst_pos - output;
}
int od_auth_query(od_system_t *system, od_schemeroute_t *scheme,
shapito_parameter_t *user,
shapito_password_t *password)
{
od_instance_t *instance = system->instance;
@ -248,11 +293,12 @@ int od_auth_query(od_system_t *system, od_schemeroute_t *scheme,
}
}
/* execute query */
rc = od_auth_query_do(server,
scheme->auth_query,
strlen(scheme->auth_query) + 1,
password);
/* preformat and execute query */
char query[512];
int query_len;
query_len = od_auth_query_format(scheme, user, query, sizeof(query));
rc = od_auth_query_do(server, query, query_len, password);
if (rc == -1) {
od_router_close_and_unroute(auth_client);
od_client_free(auth_client);

View File

@ -7,6 +7,8 @@
* Advanced PostgreSQL connection pooler.
*/
int od_auth_query(od_system_t*, od_schemeroute_t*, shapito_password_t*);
int od_auth_query(od_system_t*, od_schemeroute_t*,
shapito_parameter_t*,
shapito_password_t*);
#endif /* OD_AUTH_QUERY_H */