Skip to content

Commit

Permalink
Merge pull request #475 from timopollmeier/credential-login-character…
Browse files Browse the repository at this point in the history
…s-8.0

 Allow special characters in credential login names (8.0)
  • Loading branch information
mattmundell authored Apr 12, 2019
2 parents c0a21b8 + 049be28 commit 81a6291
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
9 changes: 5 additions & 4 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -22264,8 +22264,8 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context,
SEND_TO_CLIENT_OR_FAIL
(XML_ERROR_SYNTAX ("create_credential",
"Login may only contain alphanumeric"
" characters if autogenerating"
" credential"));
" characters or the following:"
" - _ \\ . @"));
break;
case 3:
SEND_TO_CLIENT_OR_FAIL
Expand Down Expand Up @@ -26161,8 +26161,9 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context,
case 4:
SEND_TO_CLIENT_OR_FAIL
(XML_ERROR_SYNTAX ("modify_credential",
"Login name must not be empty and contain"
" only alphanumeric characters"));
"Login name must not be empty and may"
" contain only alphanumeric characters"
" or the following: - _ \\ . @"));
log_event_fail ("credential", "Credential",
modify_credential_data->credential_id,
"modified");
Expand Down
81 changes: 51 additions & 30 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -41589,6 +41589,31 @@ set_credential_data (credential_t credential,
return 0;
}

/**
* @brief Test if a username is valid to use in a credential.
*
* Valid usernames may only contain alphanumeric characters and a few
* special ones to avoid problems with installer package generation.
*
* @param[in] username The username string to test.
*
* @return Whether the username is valid.
*/
static int
validate_credential_username (const gchar *username)
{
const char *s;
s = username;
while (*s)
if (isalnum (*s)
|| strchr ("-_\\.@", *s))
s++;
else
return 0;

return 1;
}

/**
* @brief Test if a username is valid for a credential export format.
*
Expand Down Expand Up @@ -41662,7 +41687,7 @@ validate_credential_username_for_format (const gchar *username,
* @param[in] allow_insecure Whether to allow insecure uses.
* @param[out] credential Created Credential.
*
* @return 0 success, 1 LSC credential exists already, 2 name contains space,
* @return 0 success, 1 LSC credential exists already, 2 invalid username,
* 3 Failed to create public key from private key/password,
* 4 Invalid credential type, 5 login username missing,
* 6 password missing, 7 private key missing, 8 certificate missing,
Expand Down Expand Up @@ -41846,8 +41871,21 @@ create_credential (const char* name, const char* comment, const char* login,

/* Add non-secret data */
if (login)
set_credential_data (new_credential,
"username", login);
{
/*
* Ensure the login does not contain characters that cause problems
* with package generation.
*/
if (validate_credential_username (login) == 0)
{
sql_rollback ();
return 2;
}

set_credential_data (new_credential,
"username", login);
}

if (key_public)
set_credential_data (new_credential, "public_key", key_public);

Expand All @@ -41859,7 +41897,10 @@ create_credential (const char* name, const char* comment, const char* login,
set_credential_data (new_credential,
"certificate", certificate_truncated);
else
return 17;
{
sql_rollback();
return 17;
}
g_free (certificate_truncated);
}
if (auth_algorithm)
Expand Down Expand Up @@ -42016,23 +42057,6 @@ create_credential (const char* name, const char* comment, const char* login,
* Auto-generate credential
*/

/* Ensure the login is alphanumeric, to help the package generation. */

if (login)
{
const char *s;
s = login;
while (*s)
if (isalnum (*s))
s++;
else
{
g_free (quoted_name);
sql_rollback ();
return 2;
}
}

/* Create the keys and packages. */

rand = g_rand_new ();
Expand Down Expand Up @@ -42239,16 +42263,13 @@ modify_credential (const char *credential_id,

if (login && ret == 0)
{
const char *s;
s = login;
// Check if login contains only alphanumeric characters
if (strcmp (login, "") == 0)
/*
* Ensure the login is not empty and does not contain characters that
* cause problems with package generation.
*/
if (strcmp (login, "") == 0
|| validate_credential_username (login) == 0)
ret = 4;
while (*s && ret == 0)
if (isalnum (*s))
s++;
else
ret = 4;

if (ret == 0)
set_credential_login (credential, login);
Expand Down

0 comments on commit 81a6291

Please sign in to comment.