Skip to content

Commit 9638c29

Browse files
committed
Add support for SASL2 user-agent
1 parent 35b8ee5 commit 9638c29

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

src/auth.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ static xmpp_stanza_t *_make_starttls(xmpp_conn_t *conn)
682682

683683
static xmpp_stanza_t *_make_sasl_auth(xmpp_conn_t *conn, const char *mechanism, const char *initial_data)
684684
{
685-
xmpp_stanza_t *auth, *init;
685+
xmpp_stanza_t *auth, *init, *user_agent;
686686
xmpp_stanza_t *inittxt = NULL;
687687

688688
/* build auth stanza */
@@ -707,6 +707,59 @@ static xmpp_stanza_t *_make_sasl_auth(xmpp_conn_t *conn, const char *mechanism,
707707
xmpp_stanza_add_child_ex(init, inittxt, 0);
708708
xmpp_stanza_add_child_ex(auth, init, 0);
709709
}
710+
if (conn->user_agent_id || conn->user_agent_software || conn->user_agent_device) {
711+
user_agent = xmpp_stanza_new(conn->ctx);
712+
if (!user_agent) {
713+
xmpp_stanza_release(auth);
714+
return NULL;
715+
}
716+
xmpp_stanza_set_name(user_agent, "user-agent");
717+
xmpp_stanza_set_ns(user_agent, XMPP_NS_SASL2);
718+
if (conn->user_agent_id) {
719+
xmpp_stanza_set_attribute(user_agent, "id", conn->user_agent_id);
720+
}
721+
if (conn->user_agent_software) {
722+
xmpp_stanza_t *software = xmpp_stanza_new(conn->ctx);
723+
if (!software) {
724+
xmpp_stanza_release(user_agent);
725+
xmpp_stanza_release(auth);
726+
return NULL;
727+
}
728+
xmpp_stanza_set_name(software, "software");
729+
xmpp_stanza_set_ns(software, XMPP_NS_SASL2);
730+
xmpp_stanza_t *txt = xmpp_stanza_new(conn->ctx);
731+
if (!txt) {
732+
xmpp_stanza_release(software);
733+
xmpp_stanza_release(user_agent);
734+
xmpp_stanza_release(auth);
735+
return NULL;
736+
}
737+
xmpp_stanza_set_text(txt, conn->user_agent_software);
738+
xmpp_stanza_add_child_ex(software, txt, 0);
739+
xmpp_stanza_add_child_ex(user_agent, software, 0);
740+
}
741+
if (conn->user_agent_device) {
742+
xmpp_stanza_t *device = xmpp_stanza_new(conn->ctx);
743+
if (!device) {
744+
xmpp_stanza_release(user_agent);
745+
xmpp_stanza_release(auth);
746+
return NULL;
747+
}
748+
xmpp_stanza_set_name(device, "device");
749+
xmpp_stanza_set_ns(device, XMPP_NS_SASL2);
750+
xmpp_stanza_t *txt = xmpp_stanza_new(conn->ctx);
751+
if (!txt) {
752+
xmpp_stanza_release(device);
753+
xmpp_stanza_release(user_agent);
754+
xmpp_stanza_release(auth);
755+
return NULL;
756+
}
757+
xmpp_stanza_set_text(txt, conn->user_agent_device);
758+
xmpp_stanza_add_child_ex(device, txt, 0);
759+
xmpp_stanza_add_child_ex(user_agent, device, 0);
760+
}
761+
xmpp_stanza_add_child_ex(auth, user_agent, 0);
762+
}
710763
} else {
711764
xmpp_stanza_set_name(auth, "auth");
712765
xmpp_stanza_set_ns(auth, XMPP_NS_SASL);

src/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ struct _xmpp_conn_t {
288288
char *domain;
289289
char *jid;
290290
char *pass;
291+
char *user_agent_id;
292+
char *user_agent_software;
293+
char *user_agent_device;
291294
char *bound_jid;
292295
char *stream_id;
293296

src/conn.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,93 @@ void xmpp_conn_set_pass(xmpp_conn_t *conn, const char *pass)
623623
conn->pass = pass ? strophe_strdup(conn->ctx, pass) : NULL;
624624
}
625625

626+
/** Get the user-agent id used for authentication of a connection.
627+
*
628+
* @param conn a Strophe connection object
629+
*
630+
* @return a string containing the id or NULL if it has not been set
631+
*
632+
* @ingroup Connections
633+
*/
634+
const char *xmpp_conn_get_user_agent_id(const xmpp_conn_t *conn)
635+
{
636+
return conn->user_agent_id;
637+
}
638+
639+
/** Set the user-agent id used to authenticate the connection.
640+
* If any id was previously set, it will be discarded. The function
641+
* will make a copy of the string.
642+
*
643+
* @param conn a Strophe connection object
644+
* @param user_agent_id the id
645+
*
646+
* @ingroup Connections
647+
*/
648+
void xmpp_conn_set_user_agent_id(xmpp_conn_t *conn, const char *user_agent_id)
649+
{
650+
if (conn->user_agent_id)
651+
strophe_free(conn->ctx, conn->user_agent_id);
652+
conn->user_agent_id = user_agent_id ? strophe_strdup(conn->ctx, user_agent_id) : NULL;
653+
}
654+
655+
/** Get the software name used for authentication of a connection.
656+
*
657+
* @param conn a Strophe connection object
658+
*
659+
* @return a string containing the name or NULL if it has not been set
660+
*
661+
* @ingroup Connections
662+
*/
663+
const char *xmpp_conn_get_user_agent_software(const xmpp_conn_t *conn)
664+
{
665+
return conn->user_agent_software;
666+
}
667+
668+
/** Set the user-agent software name used to authenticate the connection.
669+
* If any name was previously set, it will be discarded. The function
670+
* will make a copy of the string.
671+
*
672+
* @param conn a Strophe connection object
673+
* @param user_agent_software the name
674+
*
675+
* @ingroup Connections
676+
*/
677+
void xmpp_conn_set_user_agent_software(xmpp_conn_t *conn, const char *user_agent_software)
678+
{
679+
if (conn->user_agent_software)
680+
strophe_free(conn->ctx, conn->user_agent_software);
681+
conn->user_agent_software = user_agent_software ? strophe_strdup(conn->ctx, user_agent_software) : NULL;
682+
}
683+
684+
/** Get the device name used for authentication of a connection.
685+
*
686+
* @param conn a Strophe connection object
687+
*
688+
* @return a string containing the name or NULL if it has not been set
689+
*
690+
* @ingroup Connections
691+
*/
692+
const char *xmpp_conn_get_user_agent_device(const xmpp_conn_t *conn)
693+
{
694+
return conn->user_agent_device;
695+
}
696+
697+
/** Set the user-agent device name used to authenticate the connection.
698+
* If any name was previously set, it will be discarded. The function
699+
* will make a copy of the string.
700+
*
701+
* @param conn a Strophe connection object
702+
* @param user_agent_device the name
703+
*
704+
* @ingroup Connections
705+
*/
706+
void xmpp_conn_set_user_agent_device(xmpp_conn_t *conn, const char *user_agent_device)
707+
{
708+
if (conn->user_agent_device)
709+
strophe_free(conn->ctx, conn->user_agent_device);
710+
conn->user_agent_device = user_agent_device ? strophe_strdup(conn->ctx, user_agent_device) : NULL;
711+
}
712+
626713
/** Get the strophe context that the connection is associated with.
627714
* @param conn a Strophe connection object
628715
*

strophe.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,12 @@ unsigned int xmpp_conn_cert_xmppaddr_num(xmpp_conn_t *conn);
402402
char *xmpp_conn_cert_xmppaddr(xmpp_conn_t *conn, unsigned int n);
403403
const char *xmpp_conn_get_pass(const xmpp_conn_t *conn);
404404
void xmpp_conn_set_pass(xmpp_conn_t *conn, const char *pass);
405+
const char *xmpp_conn_get_user_agent_id(const xmpp_conn_t *conn);
406+
void xmpp_conn_set_user_agent_id(xmpp_conn_t *conn, const char *user_agent_id);
407+
const char *xmpp_conn_get_user_agent_software(const xmpp_conn_t *conn);
408+
void xmpp_conn_set_user_agent_software(xmpp_conn_t *conn, const char *user_agent_software);
409+
const char *xmpp_conn_get_user_agent_device(const xmpp_conn_t *conn);
410+
void xmpp_conn_set_user_agent_device(xmpp_conn_t *conn, const char *user_agent_device);
405411
xmpp_ctx_t *xmpp_conn_get_context(xmpp_conn_t *conn);
406412
int xmpp_conn_is_secured(xmpp_conn_t *conn);
407413
void xmpp_conn_set_sockopt_callback(xmpp_conn_t *conn,

0 commit comments

Comments
 (0)