Files
cproof/src/xmpp/jid.c
Michael Vetter 5bcf859e92 feat: Add jid_is_valid_user_jid() to check for valid user JIDs
This one will need a localpart as well.
It should be used when we want to message a user for example.
While jid_is_valid() also returns TRUE for domain/server JIDs.
2026-03-06 12:35:30 +01:00

263 lines
6.3 KiB
C

/*
* jid.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
*
* 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
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* 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.
*
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "common.h"
#include "xmpp/jid.h"
#define JID_MAX_PART_LEN 1023
#define JID_MAX_TOTAL_LEN 3071
static gboolean
_is_invalid_local_char(gunichar c)
{
// RFC 6122: " & ' / : < > @
// Also space is forbidden.
return (c == ' ' || c == '"' || c == '&' || c == '\'' || c == '/' || c == ':' || c == '<' || c == '>' || c == '@');
}
Jid*
jid_create(const gchar* const str)
{
if (str == NULL) {
return NULL;
}
gchar* trimmed = g_strdup(str);
if (trimmed == NULL) {
return NULL;
}
size_t len = strlen(trimmed);
if (len == 0 || len > JID_MAX_TOTAL_LEN) {
g_free(trimmed);
return NULL;
}
if (!g_utf8_validate(trimmed, -1, NULL)) {
g_free(trimmed);
return NULL;
}
Jid* result = g_new0(Jid, 1);
result->refcnt = 1;
result->str = trimmed;
gchar* slashp = g_utf8_strchr(trimmed, -1, '/');
auto_gchar gchar* bare = NULL;
if (slashp) {
result->resourcepart = g_strdup(slashp + 1);
bare = g_utf8_substring(trimmed, 0, g_utf8_pointer_to_offset(trimmed, slashp));
result->fulljid = g_strdup(trimmed);
} else {
bare = g_strdup(trimmed);
}
// Validate resourcepart
if (result->resourcepart) {
size_t rlen = strlen(result->resourcepart);
if (rlen == 0 || rlen > JID_MAX_PART_LEN) {
jid_destroy(result);
return NULL;
}
}
gchar* atp = g_utf8_strchr(bare, -1, '@');
gchar* domain_start = bare;
if (atp) {
result->localpart = g_utf8_substring(bare, 0, g_utf8_pointer_to_offset(bare, atp));
domain_start = atp + 1;
// Validate localpart
size_t llen = strlen(result->localpart);
if (llen == 0 || llen > JID_MAX_PART_LEN) {
jid_destroy(result);
return NULL;
}
const gchar* p = result->localpart;
while (*p) {
gunichar c = g_utf8_get_char(p);
if (_is_invalid_local_char(c)) {
jid_destroy(result);
return NULL;
}
p = g_utf8_next_char(p);
}
}
result->domainpart = g_strdup(domain_start);
result->barejid = g_utf8_strdown(bare, -1);
// Validate domainpart
size_t dlen = result->domainpart ? strlen(result->domainpart) : 0;
if (dlen == 0 || dlen > JID_MAX_PART_LEN) {
jid_destroy(result);
return NULL;
}
// Domainpart cannot contain @
if (g_utf8_strchr(result->domainpart, -1, '@')) {
jid_destroy(result);
return NULL;
}
return result;
}
gboolean
jid_is_valid(const gchar* const str)
{
auto_jid Jid* jid = jid_create(str);
return (jid != NULL);
}
gboolean
jid_is_valid_user_jid(const gchar* const str)
{
auto_jid Jid* jid = jid_create(str);
return (jid != NULL && jid->localpart != NULL);
}
Jid*
jid_create_from_bare_and_resource(const gchar* const barejid, const gchar* const resource)
{
auto_gchar gchar* jid = create_fulljid(barejid, resource);
return jid_create(jid);
}
void
jid_auto_destroy(Jid** jid)
{
if (jid == NULL)
return;
jid_destroy(*jid);
}
void
jid_ref(Jid* jid)
{
jid->refcnt++;
}
void
jid_destroy(Jid* jid)
{
if (jid == NULL) {
return;
}
if (jid->refcnt > 1) {
jid->refcnt--;
return;
}
g_free(jid->str);
g_free(jid->localpart);
g_free(jid->domainpart);
g_free(jid->resourcepart);
g_free(jid->barejid);
g_free(jid->fulljid);
free(jid);
}
gboolean
jid_is_valid_room_form(Jid* jid)
{
return (jid->fulljid != NULL);
}
/*
* Given a barejid, and resourcepart, create and return a full JID of the form
* barejid/resourcepart
* Will return a newly created string that must be freed by the caller
*/
gchar*
create_fulljid(const gchar* const barejid, const gchar* const resource)
{
auto_gchar gchar* barejidlower = g_utf8_strdown(barejid, -1);
return g_strdup_printf("%s/%s", barejidlower, resource);
}
/*
* Get the nickname part of the full JID, e.g.
* Full JID = "test@conference.server/person"
* returns "person"
*/
gchar*
get_nick_from_full_jid(const gchar* const full_room_jid)
{
auto_gcharv gchar** tokens = g_strsplit(full_room_jid, "/", 0);
gchar* nick_part = NULL;
if (tokens) {
if (tokens[0] && tokens[1]) {
nick_part = strdup(tokens[1]);
}
}
return nick_part;
}
/*
* get the fulljid, fall back to the barejid
*/
const gchar*
jid_fulljid_or_barejid(Jid* jid)
{
if (jid->fulljid) {
return jid->fulljid;
} else {
return jid->barejid;
}
}
gchar*
jid_random_resource(void)
{
auto_gchar gchar* rand = get_random_string(4);
gchar* result = g_strdup_printf("profanity.%s", rand);
return result;
}