Skip to content

Commit 1828039

Browse files
committed
conn: added new API xmpp_conn_set_flags()
xmpp_conn_set_flags() and xmpp_conn_get_flags() unify interface of connection configuration. This interface allows compile-time check of supported features or even run-time check. Therefore, applications can be built with older libstrophe which doesn't support some optional flags.
1 parent e41b7a0 commit 1828039

File tree

7 files changed

+105
-27
lines changed

7 files changed

+105
-27
lines changed

ChangeLog

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
0.8.9
22
- IPv6 support
3-
- Old style SSL support
3+
- Legacy SSL support
44
- New API:
55
- xmpp_uuid_gen()
6-
- xmpp_conn_set_old_style_ssl()
6+
- xmpp_conn_get_flags()
7+
- xmpp_conn_set_flags()
78
- xmpp_conn_is_secured()
89
- Exposed private API:
910
- xmpp_jid_new()

examples/basic.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,28 @@ int main(int argc, char **argv)
4242
xmpp_conn_t *conn;
4343
xmpp_log_t *log;
4444
char *jid, *pass, *host = NULL;
45+
long flags = 0;
4546
int i;
46-
int opt_disable_tls = 0;
47-
int opt_old_ssl = 0;
4847

4948
/* take a jid and password on the command line */
5049
for (i = 1; i < argc; ++i) {
5150
if (strcmp(argv[i], "--disable-tls") == 0)
52-
opt_disable_tls = 1;
53-
else if (strcmp(argv[i], "--old-ssl") == 0)
54-
opt_old_ssl = 1;
51+
flags |= XMPP_CONN_FLAG_DISABLE_TLS;
52+
else if (strcmp(argv[i], "--mandatory-tls") == 0)
53+
flags |= XMPP_CONN_FLAG_MANDATORY_TLS;
54+
else if (strcmp(argv[i], "--legacy-ssl") == 0)
55+
flags |= XMPP_CONN_FLAG_LEGACY_SSL;
5556
else
5657
break;
5758
}
5859
if ((argc - i) < 2 || (argc - i) > 3) {
5960
fprintf(stderr, "Usage: basic [options] <jid> <pass> [<host>]\n\n"
6061
"Options:\n"
6162
" --disable-tls Disable TLS.\n"
62-
" --old-ssl Use old style SSL.\n");
63+
" --mandatory-tls Deny plaintext connection.\n"
64+
" --legacy-ssl Use old style SSL.\n\n"
65+
"Note: --disable-tls conflicts with --mandatory-tls or "
66+
"--legacy-ssl\n");
6367
return 1;
6468
}
6569

@@ -68,6 +72,11 @@ int main(int argc, char **argv)
6872
if (i + 2 < argc)
6973
host = argv[i + 2];
7074

75+
/*
76+
* Note, this example doesn't handle errors. Applications should check
77+
* return values of non-void functions.
78+
*/
79+
7180
/* init library */
7281
xmpp_initialize();
7382

@@ -79,10 +88,7 @@ int main(int argc, char **argv)
7988
conn = xmpp_conn_new(ctx);
8089

8190
/* configure connection properties (optional) */
82-
if (opt_disable_tls)
83-
xmpp_conn_disable_tls(conn);
84-
if (opt_old_ssl)
85-
xmpp_conn_set_old_style_ssl(conn);
91+
xmpp_conn_set_flags(conn, flags);
8692

8793
/* setup authentication information */
8894
xmpp_conn_set_jid(conn, jid);

src/auth.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,18 @@ static void _auth(xmpp_conn_t * const conn)
592592

593593
/* TLS was tried, unset flag */
594594
conn->tls_support = 0;
595-
} else if (anonjid && conn->sasl_support & SASL_MASK_ANONYMOUS) {
595+
/* _auth() will be called later */
596+
return;
597+
}
598+
599+
if (conn->tls_mandatory && !xmpp_conn_is_secured(conn)) {
600+
xmpp_error(conn->ctx, "xmpp", "TLS is not supported, but set as"
601+
"mandatory for this connection");
602+
conn_disconnect(conn);
603+
return;
604+
}
605+
606+
if (anonjid && conn->sasl_support & SASL_MASK_ANONYMOUS) {
596607
/* some crap here */
597608
auth = _make_sasl_auth(conn, "ANONYMOUS");
598609
if (!auth) {

src/common.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ struct _xmpp_handlist_t {
137137

138138
enum {
139139
XMPP_PORT_CLIENT = 5222,
140-
XMPP_PORT_CLIENT_OLD_SSL = 5223,
140+
XMPP_PORT_CLIENT_LEGACY_SSL = 5223,
141141
XMPP_PORT_COMPONENT = 5347,
142142
};
143143

@@ -157,7 +157,8 @@ struct _xmpp_conn_t {
157157

158158
int tls_support;
159159
int tls_disabled;
160-
int tls_is_old_ssl;
160+
int tls_mandatory;
161+
int tls_legacy_ssl;
161162
int tls_failed; /* set when tls fails, so we don't try again */
162163
int sasl_support; /* if true, field is a bitfield of supported
163164
mechanisms */

src/conn.c

+63-9
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx)
108108

109109
conn->tls_support = 0;
110110
conn->tls_disabled = 0;
111-
conn->tls_is_old_ssl = 0;
111+
conn->tls_mandatory = 0;
112+
conn->tls_legacy_ssl = 0;
112113
conn->tls_failed = 0;
113114
conn->sasl_support = 0;
114115
conn->secured = 0;
@@ -422,11 +423,11 @@ int xmpp_connect_client(xmpp_conn_t * const conn,
422423
prefdomain = conn->domain;
423424
port = altport ? altport : _conn_default_port(conn);
424425
}
425-
if (conn->tls_is_old_ssl) {
426+
if (conn->tls_legacy_ssl) {
426427
/* SSL tunneled connection on 5223 port is legacy and doesn't
427428
* have an SRV record. Force port 5223 here.
428429
*/
429-
port = XMPP_PORT_CLIENT_OLD_SSL;
430+
port = XMPP_PORT_CLIENT_LEGACY_SSL;
430431
}
431432
}
432433
if (prefdomain != NULL) {
@@ -780,22 +781,75 @@ int conn_tls_start(xmpp_conn_t * const conn)
780781
return rc;
781782
}
782783

784+
/** Return applied flags for the connection.
785+
*
786+
* @param conn a Strophe connection object
787+
*
788+
* @return ORed connection flags that are applied for the connection.
789+
*/
790+
long xmpp_conn_get_flags(const xmpp_conn_t * const conn)
791+
{
792+
long flags;
793+
794+
flags = XMPP_CONN_FLAG_DISABLE_TLS * conn->tls_disabled |
795+
XMPP_CONN_FLAG_MANDATORY_TLS * conn->tls_mandatory |
796+
XMPP_CONN_FLAG_LEGACY_SSL * conn->tls_legacy_ssl;
797+
798+
return flags;
799+
}
800+
801+
/** Set flags for the connection.
802+
* This function applies set flags and resets unset ones. Default connection
803+
* configuration is all flags unset. Flags can be applied only for a connection
804+
* in disconnected state.
805+
* All unsupported flags are ignored. If a flag is unset after successful set
806+
* operation then the flag is not supported by current version.
807+
*
808+
* Supported flags are:
809+
*
810+
* - XMPP_CONN_FLAG_DISABLE_TLS
811+
* - XMPP_CONN_FLAG_MANDATORY_TLS
812+
* - XMPP_CONN_FLAG_LEGACY_SSL
813+
*
814+
* @param conn a Strophe connection object
815+
* @param flags ORed connection flags
816+
*
817+
* @return 0 on success or -1 if flags can't be applied.
818+
*/
819+
int xmpp_conn_set_flags(xmpp_conn_t * const conn, long flags)
820+
{
821+
if (conn->state != XMPP_STATE_DISCONNECTED) {
822+
xmpp_error(conn->ctx, "conn", "Flags can be set only "
823+
"for disconnected connection");
824+
return -1;
825+
}
826+
if (flags & XMPP_CONN_FLAG_DISABLE_TLS &&
827+
flags & (XMPP_CONN_FLAG_MANDATORY_TLS | XMPP_CONN_FLAG_LEGACY_SSL)) {
828+
xmpp_error(conn->ctx, "conn", "Flags 0x%04lx conflict", flags);
829+
return -1;
830+
}
831+
832+
conn->tls_disabled = (flags & XMPP_CONN_FLAG_DISABLE_TLS) ? 1 : 0;
833+
conn->tls_mandatory = (flags & XMPP_CONN_FLAG_MANDATORY_TLS) ? 1 : 0;
834+
conn->tls_legacy_ssl = (flags & XMPP_CONN_FLAG_LEGACY_SSL) ? 1 : 0;
835+
836+
return 0;
837+
}
838+
783839
/** Disable TLS for this connection, called by users of the library.
784840
* Occasionally a server will be misconfigured to send the starttls
785841
* feature, but will not support the handshake.
786842
*
787843
* @param conn a Strophe connection object
844+
*
845+
* @note this function is deprecated
846+
* @see xmpp_conn_set_flags()
788847
*/
789848
void xmpp_conn_disable_tls(xmpp_conn_t * const conn)
790849
{
791850
conn->tls_disabled = 1;
792851
}
793852

794-
void xmpp_conn_set_old_style_ssl(xmpp_conn_t * const conn)
795-
{
796-
conn->tls_is_old_ssl = 1;
797-
}
798-
799853
/** Returns whether TLS session is established or not. */
800854
int xmpp_conn_is_secured(xmpp_conn_t * const conn)
801855
{
@@ -903,7 +957,7 @@ static int _conn_default_port(xmpp_conn_t * const conn)
903957
{
904958
switch (conn->type) {
905959
case XMPP_CLIENT:
906-
return conn->tls_is_old_ssl ? XMPP_PORT_CLIENT_OLD_SSL :
960+
return conn->tls_legacy_ssl ? XMPP_PORT_CLIENT_LEGACY_SSL :
907961
XMPP_PORT_CLIENT;
908962
case XMPP_COMPONENT:
909963
return XMPP_PORT_COMPONENT;

src/event.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
269269
conn->state = XMPP_STATE_CONNECTED;
270270
xmpp_debug(ctx, "xmpp", "connection successful");
271271

272-
if (conn->tls_is_old_ssl) {
273-
xmpp_debug(ctx, "xmpp", "using old style SSL connection");
272+
if (conn->tls_legacy_ssl) {
273+
xmpp_debug(ctx, "xmpp", "using legacy SSL connection");
274274
ret = conn_tls_start(conn);
275275
if (ret != 0) {
276276
conn_disconnect(conn);

strophe.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ typedef enum {
192192
XMPP_SE_XML_NOT_WELL_FORMED
193193
} xmpp_error_type_t;
194194

195+
#define XMPP_CONN_FLAG_DISABLE_TLS 0x0001
196+
#define XMPP_CONN_FLAG_MANDATORY_TLS 0x0002
197+
#define XMPP_CONN_FLAG_LEGACY_SSL 0x0004
198+
195199
typedef struct {
196200
xmpp_error_type_t type;
197201
char *text;
@@ -208,14 +212,15 @@ xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx);
208212
xmpp_conn_t * xmpp_conn_clone(xmpp_conn_t * const conn);
209213
int xmpp_conn_release(xmpp_conn_t * const conn);
210214

215+
long xmpp_conn_get_flags(const xmpp_conn_t * const conn);
216+
int xmpp_conn_set_flags(xmpp_conn_t * const conn, long flags);
211217
const char *xmpp_conn_get_jid(const xmpp_conn_t * const conn);
212218
const char *xmpp_conn_get_bound_jid(const xmpp_conn_t * const conn);
213219
void xmpp_conn_set_jid(xmpp_conn_t * const conn, const char * const jid);
214220
const char *xmpp_conn_get_pass(const xmpp_conn_t * const conn);
215221
void xmpp_conn_set_pass(xmpp_conn_t * const conn, const char * const pass);
216222
xmpp_ctx_t* xmpp_conn_get_context(xmpp_conn_t * const conn);
217223
void xmpp_conn_disable_tls(xmpp_conn_t * const conn);
218-
void xmpp_conn_set_old_style_ssl(xmpp_conn_t * const conn);
219224
int xmpp_conn_is_secured(xmpp_conn_t * const conn);
220225

221226
int xmpp_connect_client(xmpp_conn_t * const conn,

0 commit comments

Comments
 (0)