Skip to content

Commit bc141a3

Browse files
committed
Add support for SASL2 user-agent
1 parent 16d2c9f commit bc141a3

File tree

4 files changed

+160
-1
lines changed

4 files changed

+160
-1
lines changed

src/auth.c

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ static xmpp_stanza_t *_make_sasl_auth(xmpp_conn_t *conn,
686686
const char *mechanism,
687687
const char *initial_data)
688688
{
689-
xmpp_stanza_t *auth, *init;
689+
xmpp_stanza_t *auth, *init, *user_agent;
690690
xmpp_stanza_t *inittxt = NULL;
691691

692692
/* build auth stanza */
@@ -712,6 +712,61 @@ static xmpp_stanza_t *_make_sasl_auth(xmpp_conn_t *conn,
712712
xmpp_stanza_add_child_ex(init, inittxt, 0);
713713
xmpp_stanza_add_child_ex(auth, init, 0);
714714
}
715+
if (conn->user_agent_id || conn->user_agent_software ||
716+
conn->user_agent_device) {
717+
user_agent = xmpp_stanza_new(conn->ctx);
718+
if (!user_agent) {
719+
xmpp_stanza_release(auth);
720+
return NULL;
721+
}
722+
xmpp_stanza_set_name(user_agent, "user-agent");
723+
xmpp_stanza_set_ns(user_agent, XMPP_NS_SASL2);
724+
if (conn->user_agent_id) {
725+
xmpp_stanza_set_attribute(user_agent, "id",
726+
conn->user_agent_id);
727+
}
728+
if (conn->user_agent_software) {
729+
xmpp_stanza_t *software = xmpp_stanza_new(conn->ctx);
730+
if (!software) {
731+
xmpp_stanza_release(user_agent);
732+
xmpp_stanza_release(auth);
733+
return NULL;
734+
}
735+
xmpp_stanza_set_name(software, "software");
736+
xmpp_stanza_set_ns(software, XMPP_NS_SASL2);
737+
xmpp_stanza_t *txt = xmpp_stanza_new(conn->ctx);
738+
if (!txt) {
739+
xmpp_stanza_release(software);
740+
xmpp_stanza_release(user_agent);
741+
xmpp_stanza_release(auth);
742+
return NULL;
743+
}
744+
xmpp_stanza_set_text(txt, conn->user_agent_software);
745+
xmpp_stanza_add_child_ex(software, txt, 0);
746+
xmpp_stanza_add_child_ex(user_agent, software, 0);
747+
}
748+
if (conn->user_agent_device) {
749+
xmpp_stanza_t *device = xmpp_stanza_new(conn->ctx);
750+
if (!device) {
751+
xmpp_stanza_release(user_agent);
752+
xmpp_stanza_release(auth);
753+
return NULL;
754+
}
755+
xmpp_stanza_set_name(device, "device");
756+
xmpp_stanza_set_ns(device, XMPP_NS_SASL2);
757+
xmpp_stanza_t *txt = xmpp_stanza_new(conn->ctx);
758+
if (!txt) {
759+
xmpp_stanza_release(device);
760+
xmpp_stanza_release(user_agent);
761+
xmpp_stanza_release(auth);
762+
return NULL;
763+
}
764+
xmpp_stanza_set_text(txt, conn->user_agent_device);
765+
xmpp_stanza_add_child_ex(device, txt, 0);
766+
xmpp_stanza_add_child_ex(user_agent, device, 0);
767+
}
768+
xmpp_stanza_add_child_ex(auth, user_agent, 0);
769+
}
715770
} else {
716771
xmpp_stanza_set_name(auth, "auth");
717772
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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,99 @@ 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 =
653+
user_agent_id ? strophe_strdup(conn->ctx, user_agent_id) : NULL;
654+
}
655+
656+
/** Get the software name used for authentication of a connection.
657+
*
658+
* @param conn a Strophe connection object
659+
*
660+
* @return a string containing the name or NULL if it has not been set
661+
*
662+
* @ingroup Connections
663+
*/
664+
const char *xmpp_conn_get_user_agent_software(const xmpp_conn_t *conn)
665+
{
666+
return conn->user_agent_software;
667+
}
668+
669+
/** Set the user-agent software name used to authenticate the connection.
670+
* If any name was previously set, it will be discarded. The function
671+
* will make a copy of the string.
672+
*
673+
* @param conn a Strophe connection object
674+
* @param user_agent_software the name
675+
*
676+
* @ingroup Connections
677+
*/
678+
void xmpp_conn_set_user_agent_software(xmpp_conn_t *conn,
679+
const char *user_agent_software)
680+
{
681+
if (conn->user_agent_software)
682+
strophe_free(conn->ctx, conn->user_agent_software);
683+
conn->user_agent_software =
684+
user_agent_software ? strophe_strdup(conn->ctx, user_agent_software)
685+
: NULL;
686+
}
687+
688+
/** Get the device name used for authentication of a connection.
689+
*
690+
* @param conn a Strophe connection object
691+
*
692+
* @return a string containing the name or NULL if it has not been set
693+
*
694+
* @ingroup Connections
695+
*/
696+
const char *xmpp_conn_get_user_agent_device(const xmpp_conn_t *conn)
697+
{
698+
return conn->user_agent_device;
699+
}
700+
701+
/** Set the user-agent device name used to authenticate the connection.
702+
* If any name was previously set, it will be discarded. The function
703+
* will make a copy of the string.
704+
*
705+
* @param conn a Strophe connection object
706+
* @param user_agent_device the name
707+
*
708+
* @ingroup Connections
709+
*/
710+
void xmpp_conn_set_user_agent_device(xmpp_conn_t *conn,
711+
const char *user_agent_device)
712+
{
713+
if (conn->user_agent_device)
714+
strophe_free(conn->ctx, conn->user_agent_device);
715+
conn->user_agent_device =
716+
user_agent_device ? strophe_strdup(conn->ctx, user_agent_device) : NULL;
717+
}
718+
626719
/** Get the strophe context that the connection is associated with.
627720
* @param conn a Strophe connection object
628721
*

strophe.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,14 @@ 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,
409+
const char *user_agent_software);
410+
const char *xmpp_conn_get_user_agent_device(const xmpp_conn_t *conn);
411+
void xmpp_conn_set_user_agent_device(xmpp_conn_t *conn,
412+
const char *user_agent_device);
405413
xmpp_ctx_t *xmpp_conn_get_context(xmpp_conn_t *conn);
406414
int xmpp_conn_is_secured(xmpp_conn_t *conn);
407415
void xmpp_conn_set_sockopt_callback(xmpp_conn_t *conn,

0 commit comments

Comments
 (0)