Skip to content

Commit

Permalink
Change: Send GMP logout command when logging out a user
Browse files Browse the repository at this point in the history
Merge pull request #113 from timopollmeier/add-gmp-logout
  • Loading branch information
timopollmeier committed Feb 6, 2023
2 parents 08148a0 + 887252f commit 32dd0ba
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/gsad_gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -16812,6 +16812,92 @@ authenticate_gmp (const gchar *username, const gchar *password, gchar **role,
}
}

/**
* @brief Check authentication credentials.
*
* @param[in] username Username.
* @param[in] password Password.
* @param[out] role Role.
* @param[out] timezone Timezone.
* @param[out] capabilities Capabilities of manager.
* @param[out] language User Interface Language, or NULL.
* @param[out] pw_warning Password warning message, NULL if password is OK.
*
* @return 0 success, 1 manager down, 2 failed, 3 timeout, -1 error.
*/
int
logout_gmp (const gchar *username, const gchar *password)
{
gvm_connection_t connection;
int ret;
gmp_authenticate_info_opts_t auth_opts;

entity_t entity;
const char *status;

gchar *response;

if (gvm_connection_open (&connection, manager_address, manager_port))
{
g_debug ("%s failed to acquire socket!\n", __func__);
return 1;
}

auth_opts = gmp_authenticate_info_opts_defaults;
auth_opts.username = username;
auth_opts.password = password;
auth_opts.role = NULL;
auth_opts.timezone = NULL;
auth_opts.pw_warning = NULL;

ret = gmp_authenticate_info_ext_c (&connection, auth_opts);
if (ret)
{
gvm_connection_close (&connection);

switch (ret)
{
case 1: /* manager closed connection */
case 2: /* auth failed */
case 3: /* timeout */
return ret;
default:
return -1;
}
}

ret = gvm_connection_sendf_xml (&connection, "<logout/>");
if (ret)
{
gvm_connection_close (&connection);
return -1;
}

entity = NULL;
if (read_entity_and_text_c (&connection, &entity, &response))
{
gvm_connection_close (&connection);
return 1;
}

gvm_connection_close (&connection);

status = entity_attribute (entity, "status");
if ((status == NULL) || (strlen (status) == 0))
{
free_entity (entity);
return -1;
}
else if (status[0] == '2')
{
free_entity (entity);
return 2;
}

free_entity (entity);
return 0;
}

/**
* @brief Login and create a session
*
Expand Down
3 changes: 3 additions & 0 deletions src/gsad_gmp_auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ int
authenticate_gmp (const gchar *, const gchar *, gchar **, gchar **, gchar **,
gchar **, gchar **, gchar **);

int
logout_gmp (const gchar *username, const gchar *password);

#endif /* _GSAD_GMP_AUTH_H */
6 changes: 6 additions & 0 deletions src/gsad_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ user_logout (user_t *user)

if (fuser)
{
if (fuser->username && fuser->password)
logout_gmp (fuser->username, fuser->password);
session_remove_user (fuser->token);
user_free (fuser);
return 0;
Expand Down Expand Up @@ -327,6 +329,8 @@ user_add (const gchar *username, const gchar *password, const gchar *timezone,

if (user && user_session_expired (user))
{
if (user->username && user->password)
logout_gmp (user->username, user->password);
session_remove_user (user->token);
user_free (user);
}
Expand Down Expand Up @@ -371,6 +375,8 @@ user_find (const gchar *cookie, const gchar *token, const char *address,
{
if (user_session_expired (user))
{
if (user->username && user->password)
logout_gmp (user->username, user->password);
session_remove_user (user->token);
user_free (user);
return USER_EXPIRED_TOKEN;
Expand Down

0 comments on commit 32dd0ba

Please sign in to comment.