Skip to content

Commit 027cc67

Browse files
singpolymasjaeckel
authored andcommitted
Add support for SASL2 user-agent
1 parent 0d2421c commit 027cc67

4 files changed

Lines changed: 160 additions & 1 deletion

File tree

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
@@ -304,6 +304,9 @@ struct _xmpp_conn_t {
304304
char *domain;
305305
char *jid;
306306
char *pass;
307+
char *user_agent_id;
308+
char *user_agent_software;
309+
char *user_agent_device;
307310
char *bound_jid;
308311
char *stream_id;
309312

src/conn.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,99 @@ void xmpp_conn_set_pass(xmpp_conn_t *conn, const char *pass)
626626
conn->pass = pass ? strophe_strdup(conn->ctx, pass) : NULL;
627627
}
628628

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

strophe.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,14 @@ unsigned int xmpp_conn_cert_xmppaddr_num(xmpp_conn_t *conn);
405405
char *xmpp_conn_cert_xmppaddr(xmpp_conn_t *conn, unsigned int n);
406406
const char *xmpp_conn_get_pass(const xmpp_conn_t *conn);
407407
void xmpp_conn_set_pass(xmpp_conn_t *conn, const char *pass);
408+
const char *xmpp_conn_get_user_agent_id(const xmpp_conn_t *conn);
409+
void xmpp_conn_set_user_agent_id(xmpp_conn_t *conn, const char *user_agent_id);
410+
const char *xmpp_conn_get_user_agent_software(const xmpp_conn_t *conn);
411+
void xmpp_conn_set_user_agent_software(xmpp_conn_t *conn,
412+
const char *user_agent_software);
413+
const char *xmpp_conn_get_user_agent_device(const xmpp_conn_t *conn);
414+
void xmpp_conn_set_user_agent_device(xmpp_conn_t *conn,
415+
const char *user_agent_device);
408416
xmpp_ctx_t *xmpp_conn_get_context(xmpp_conn_t *conn);
409417
int xmpp_conn_is_secured(xmpp_conn_t *conn);
410418
void xmpp_conn_set_sockopt_callback(xmpp_conn_t *conn,

0 commit comments

Comments
 (0)