Skip to content

Commit eb7a8ab

Browse files
committed
Add support for SASL2 user-agent
1 parent 4d1f087 commit eb7a8ab

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
@@ -293,6 +293,9 @@ struct _xmpp_conn_t {
293293
char *domain;
294294
char *jid;
295295
char *pass;
296+
char *user_agent_id;
297+
char *user_agent_software;
298+
char *user_agent_device;
296299
char *bound_jid;
297300
char *stream_id;
298301

src/conn.c

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

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

strophe.h

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

0 commit comments

Comments
 (0)