2014-01-22 17:22:01 -05:00
|
|
|
/*
|
|
|
|
* account.c
|
2019-11-13 06:11:05 -05:00
|
|
|
* vim: expandtab:ts=4:sts=4:sw=4
|
2014-01-22 17:22:01 -05:00
|
|
|
*
|
2019-01-22 05:31:45 -05:00
|
|
|
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
|
2014-01-22 17:22:01 -05:00
|
|
|
*
|
|
|
|
* This file is part of Profanity.
|
|
|
|
*
|
|
|
|
* Profanity is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Profanity is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-07-23 20:14:49 -04:00
|
|
|
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
|
2014-01-22 17:22:01 -05:00
|
|
|
*
|
2014-08-24 15:57:39 -04:00
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link the code of portions of this program with the OpenSSL library under
|
|
|
|
* certain conditions as described in each individual source file, and
|
|
|
|
* distribute linked combinations including the two.
|
|
|
|
*
|
|
|
|
* You must obey the GNU General Public License in all respects for all of the
|
|
|
|
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
|
|
|
* may extend this exception to your version of the file(s), but you are not
|
|
|
|
* obligated to do so. If you do not wish to do so, delete this exception
|
|
|
|
* statement from your version. If you delete this exception statement from all
|
|
|
|
* source files in the program, then also delete it here.
|
|
|
|
*
|
2014-01-22 17:22:01 -05:00
|
|
|
*/
|
|
|
|
|
2021-03-26 14:51:46 -04:00
|
|
|
#include "config.h"
|
|
|
|
|
2014-01-22 17:22:01 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-07-07 07:53:30 -04:00
|
|
|
#include <assert.h>
|
2020-12-13 11:19:40 -05:00
|
|
|
#include <errno.h>
|
2014-01-22 17:22:01 -05:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2014-05-21 16:39:31 -04:00
|
|
|
#include "common.h"
|
2020-07-07 03:43:28 -04:00
|
|
|
#include "log.h"
|
2020-07-07 07:53:30 -04:00
|
|
|
#include "config/account.h"
|
2016-07-24 10:43:51 -04:00
|
|
|
#include "xmpp/jid.h"
|
2016-07-24 11:27:39 -04:00
|
|
|
#include "xmpp/resource.h"
|
2014-01-22 17:22:01 -05:00
|
|
|
|
|
|
|
ProfAccount*
|
2020-07-07 08:18:57 -04:00
|
|
|
account_new(const gchar* const name, const gchar* const jid,
|
|
|
|
const gchar* const password, const gchar* eval_password, gboolean enabled, const gchar* const server,
|
|
|
|
int port, const gchar* const resource, const gchar* const last_presence,
|
|
|
|
const gchar* const login_presence, int priority_online, int priority_chat,
|
|
|
|
int priority_away, int priority_xa, int priority_dnd,
|
|
|
|
const gchar* const muc_service, const gchar* const muc_nick,
|
|
|
|
const gchar* const otr_policy, GList* otr_manual, GList* otr_opportunistic,
|
|
|
|
GList* otr_always, const gchar* const omemo_policy, GList* omemo_enabled,
|
|
|
|
GList* omemo_disabled, const gchar* const pgp_keyid, const char* const startscript,
|
|
|
|
const char* const theme, gchar* tls_policy, gchar* auth_policy)
|
2014-01-22 17:22:01 -05:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfAccount* new_account = malloc(sizeof(ProfAccount));
|
2019-10-14 05:35:18 -04:00
|
|
|
memset(new_account, 0, sizeof(ProfAccount));
|
2014-01-22 17:22:01 -05:00
|
|
|
|
|
|
|
new_account->name = strdup(name);
|
|
|
|
|
2015-05-04 17:29:51 -04:00
|
|
|
if (jid) {
|
2014-01-22 17:22:01 -05:00
|
|
|
new_account->jid = strdup(jid);
|
|
|
|
} else {
|
|
|
|
new_account->jid = strdup(name);
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:29:51 -04:00
|
|
|
if (password) {
|
2014-01-22 17:22:01 -05:00
|
|
|
new_account->password = strdup(password);
|
|
|
|
} else {
|
|
|
|
new_account->password = NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:29:51 -04:00
|
|
|
if (eval_password) {
|
2015-01-07 00:00:02 -05:00
|
|
|
new_account->eval_password = strdup(eval_password);
|
|
|
|
} else {
|
|
|
|
new_account->eval_password = NULL;
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:22:01 -05:00
|
|
|
new_account->enabled = enabled;
|
|
|
|
|
2015-05-04 17:29:51 -04:00
|
|
|
if (server) {
|
2014-01-22 17:22:01 -05:00
|
|
|
new_account->server = strdup(server);
|
|
|
|
} else {
|
|
|
|
new_account->server = NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:29:51 -04:00
|
|
|
if (resource) {
|
2014-01-22 17:22:01 -05:00
|
|
|
new_account->resource = strdup(resource);
|
|
|
|
} else {
|
|
|
|
new_account->resource = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_account->port = port;
|
|
|
|
|
|
|
|
if (last_presence == NULL || !valid_resource_presence_string(last_presence)) {
|
|
|
|
new_account->last_presence = strdup("online");
|
|
|
|
} else {
|
|
|
|
new_account->last_presence = strdup(last_presence);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (login_presence == NULL) {
|
|
|
|
new_account->login_presence = strdup("online");
|
|
|
|
} else if (strcmp(login_presence, "last") == 0) {
|
|
|
|
new_account->login_presence = strdup(login_presence);
|
|
|
|
} else if (!valid_resource_presence_string(login_presence)) {
|
|
|
|
new_account->login_presence = strdup("online");
|
|
|
|
} else {
|
|
|
|
new_account->login_presence = strdup(login_presence);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_account->priority_online = priority_online;
|
|
|
|
new_account->priority_chat = priority_chat;
|
|
|
|
new_account->priority_away = priority_away;
|
|
|
|
new_account->priority_xa = priority_xa;
|
|
|
|
new_account->priority_dnd = priority_dnd;
|
|
|
|
|
2016-11-19 21:09:34 -05:00
|
|
|
if (muc_service) {
|
2014-01-22 17:22:01 -05:00
|
|
|
new_account->muc_service = strdup(muc_service);
|
2016-11-19 21:09:34 -05:00
|
|
|
} else {
|
|
|
|
new_account->muc_service = NULL;
|
2014-01-22 17:22:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (muc_nick == NULL) {
|
2020-07-07 08:18:57 -04:00
|
|
|
Jid* jidp = jid_create(new_account->jid);
|
2014-01-22 17:22:01 -05:00
|
|
|
new_account->muc_nick = strdup(jidp->domainpart);
|
|
|
|
jid_destroy(jidp);
|
|
|
|
} else {
|
|
|
|
new_account->muc_nick = strdup(muc_nick);
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:29:51 -04:00
|
|
|
if (otr_policy) {
|
2014-05-11 09:13:15 -04:00
|
|
|
new_account->otr_policy = strdup(otr_policy);
|
|
|
|
} else {
|
|
|
|
new_account->otr_policy = NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-11 14:32:07 -04:00
|
|
|
new_account->otr_manual = otr_manual;
|
|
|
|
new_account->otr_opportunistic = otr_opportunistic;
|
|
|
|
new_account->otr_always = otr_always;
|
|
|
|
|
2019-04-15 16:09:47 -04:00
|
|
|
if (omemo_policy) {
|
|
|
|
new_account->omemo_policy = strdup(omemo_policy);
|
|
|
|
} else {
|
|
|
|
new_account->omemo_policy = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_account->omemo_enabled = omemo_enabled;
|
|
|
|
new_account->omemo_disabled = omemo_disabled;
|
|
|
|
|
2015-03-23 19:38:06 -04:00
|
|
|
if (pgp_keyid != NULL) {
|
|
|
|
new_account->pgp_keyid = strdup(pgp_keyid);
|
|
|
|
} else {
|
|
|
|
new_account->pgp_keyid = NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-14 20:19:24 -04:00
|
|
|
if (startscript != NULL) {
|
|
|
|
new_account->startscript = strdup(startscript);
|
|
|
|
} else {
|
|
|
|
new_account->startscript = NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:06:28 -05:00
|
|
|
if (theme != NULL) {
|
|
|
|
new_account->theme = strdup(theme);
|
|
|
|
} else {
|
|
|
|
new_account->theme = NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:06:23 -04:00
|
|
|
if (tls_policy != NULL) {
|
|
|
|
new_account->tls_policy = strdup(tls_policy);
|
|
|
|
} else {
|
|
|
|
new_account->tls_policy = NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-26 18:22:05 -05:00
|
|
|
if (auth_policy != NULL) {
|
|
|
|
new_account->auth_policy = strdup(auth_policy);
|
|
|
|
} else {
|
|
|
|
new_account->auth_policy = NULL;
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:22:01 -05:00
|
|
|
return new_account;
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:35:39 -04:00
|
|
|
char*
|
2020-07-07 08:18:57 -04:00
|
|
|
account_create_connect_jid(ProfAccount* account)
|
2014-01-22 17:22:01 -05:00
|
|
|
{
|
2015-05-04 17:29:51 -04:00
|
|
|
if (account->resource) {
|
2014-01-22 17:22:01 -05:00
|
|
|
return create_fulljid(account->jid, account->resource);
|
|
|
|
} else {
|
|
|
|
return strdup(account->jid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 16:56:48 -04:00
|
|
|
gboolean
|
2020-07-07 08:18:57 -04:00
|
|
|
account_eval_password(ProfAccount* account)
|
2015-04-23 16:56:48 -04:00
|
|
|
{
|
|
|
|
assert(account != NULL);
|
|
|
|
assert(account->eval_password != NULL);
|
|
|
|
|
2020-12-13 11:19:40 -05:00
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
FILE* stream = popen(account->eval_password, "r");
|
|
|
|
if (stream == NULL) {
|
|
|
|
const char* errmsg = strerror(errno);
|
|
|
|
if (errmsg) {
|
|
|
|
log_error("Could not execute `eval_password` command (%s).",
|
|
|
|
errmsg);
|
|
|
|
} else {
|
|
|
|
log_error("Failed to allocate memory for `eval_password` command.");
|
|
|
|
}
|
2020-05-24 10:39:32 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-12-13 11:19:40 -05:00
|
|
|
account->password = g_malloc(READ_BUF_SIZE);
|
|
|
|
if (!account->password) {
|
|
|
|
log_error("Failed to allocate enough memory to read `eval_password` "
|
|
|
|
"output.");
|
2020-05-24 10:39:32 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-12-13 11:19:40 -05:00
|
|
|
account->password = fgets(account->password, READ_BUF_SIZE, stream);
|
|
|
|
if (!account->password) {
|
|
|
|
log_error("Failed to read password from stream.");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2020-12-11 09:51:01 -05:00
|
|
|
|
2020-12-13 11:19:40 -05:00
|
|
|
int exit_status = pclose(stream);
|
|
|
|
if (exit_status > 0) {
|
|
|
|
log_error("Command for `eval_password` returned error status (%s).",
|
|
|
|
exit_status);
|
|
|
|
return FALSE;
|
|
|
|
} else if (exit_status < 0) {
|
|
|
|
log_error("Failed to close stream for `eval_password` command output "
|
|
|
|
"(%s).",
|
|
|
|
strerror(errno));
|
|
|
|
return FALSE;
|
|
|
|
};
|
2020-05-24 10:39:32 -04:00
|
|
|
|
2020-12-13 11:19:40 -05:00
|
|
|
// Remove leading and trailing whitespace from output.
|
|
|
|
g_strstrip(account->password);
|
2020-05-24 10:39:32 -04:00
|
|
|
if (!account->password) {
|
2020-12-13 11:19:40 -05:00
|
|
|
log_error("Empty password returned by `eval_password` command.");
|
2015-04-23 16:56:48 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:22:01 -05:00
|
|
|
void
|
2020-07-07 08:18:57 -04:00
|
|
|
account_free(ProfAccount* account)
|
2014-01-22 17:22:01 -05:00
|
|
|
{
|
2016-11-19 21:09:34 -05:00
|
|
|
if (account == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(account->name);
|
|
|
|
free(account->jid);
|
|
|
|
free(account->password);
|
|
|
|
free(account->eval_password);
|
|
|
|
free(account->resource);
|
|
|
|
free(account->server);
|
|
|
|
free(account->last_presence);
|
|
|
|
free(account->login_presence);
|
|
|
|
free(account->muc_service);
|
|
|
|
free(account->muc_nick);
|
|
|
|
free(account->otr_policy);
|
2019-04-15 16:09:47 -04:00
|
|
|
free(account->omemo_policy);
|
2016-11-19 21:09:34 -05:00
|
|
|
free(account->pgp_keyid);
|
|
|
|
free(account->startscript);
|
|
|
|
free(account->theme);
|
|
|
|
free(account->tls_policy);
|
2020-02-26 18:22:05 -05:00
|
|
|
free(account->auth_policy);
|
2016-11-19 21:09:34 -05:00
|
|
|
g_list_free_full(account->otr_manual, g_free);
|
|
|
|
g_list_free_full(account->otr_opportunistic, g_free);
|
|
|
|
g_list_free_full(account->otr_always, g_free);
|
2019-04-15 16:09:47 -04:00
|
|
|
g_list_free_full(account->omemo_enabled, g_free);
|
|
|
|
g_list_free_full(account->omemo_disabled, g_free);
|
2016-11-19 21:09:34 -05:00
|
|
|
free(account);
|
2015-01-07 00:00:02 -05:00
|
|
|
}
|
2018-11-07 07:56:43 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
void
|
|
|
|
account_set_server(ProfAccount* account, const char* server)
|
2018-11-07 07:56:43 -05:00
|
|
|
{
|
|
|
|
free(account->server);
|
|
|
|
account->server = strdup(server);
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
void
|
|
|
|
account_set_port(ProfAccount* account, int port)
|
2018-11-07 07:56:43 -05:00
|
|
|
{
|
|
|
|
account->port = port;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
void
|
|
|
|
account_set_tls_policy(ProfAccount* account, const char* tls_policy)
|
2018-11-07 07:56:43 -05:00
|
|
|
{
|
|
|
|
free(account->tls_policy);
|
|
|
|
account->tls_policy = strdup(tls_policy);
|
|
|
|
}
|
2020-02-26 18:22:05 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
void
|
|
|
|
account_set_auth_policy(ProfAccount* account, const char* auth_policy)
|
2020-02-26 18:22:05 -05:00
|
|
|
{
|
|
|
|
free(account->auth_policy);
|
|
|
|
account->auth_policy = strdup(auth_policy);
|
|
|
|
}
|