cinterion: move auth helpers to the helpers sources

This commit is contained in:
Aleksander Morgado
2020-11-04 09:50:13 +01:00
parent 9ce496613b
commit eb338c967f
6 changed files with 130 additions and 125 deletions
+1
View File
@@ -583,6 +583,7 @@ test_modem_helpers_cinterion_CPPFLAGS = \
$(NULL)
test_modem_helpers_cinterion_LDADD = \
$(builddir)/libhelpers-cinterion.la \
$(top_builddir)/src/libport.la \
$(top_builddir)/src/libhelpers.la \
$(top_builddir)/libmm-glib/libmm-glib.la \
$(NULL)
@@ -166,116 +166,6 @@ load_connection_status (MMBaseBearer *bearer,
user_data);
}
/*****************************************************************************/
/* Auth helpers */
typedef enum {
BEARER_CINTERION_AUTH_UNKNOWN = -1,
BEARER_CINTERION_AUTH_NONE = 0,
BEARER_CINTERION_AUTH_PAP = 1,
BEARER_CINTERION_AUTH_CHAP = 2,
BEARER_CINTERION_AUTH_MSCHAPV2 = 3,
} BearerCinterionAuthType;
static BearerCinterionAuthType
parse_auth_type (MMBearerAllowedAuth mm_auth)
{
switch (mm_auth) {
case MM_BEARER_ALLOWED_AUTH_NONE:
return BEARER_CINTERION_AUTH_NONE;
case MM_BEARER_ALLOWED_AUTH_PAP:
return BEARER_CINTERION_AUTH_PAP;
case MM_BEARER_ALLOWED_AUTH_CHAP:
return BEARER_CINTERION_AUTH_CHAP;
case MM_BEARER_ALLOWED_AUTH_MSCHAPV2:
return BEARER_CINTERION_AUTH_MSCHAPV2;
case MM_BEARER_ALLOWED_AUTH_UNKNOWN:
case MM_BEARER_ALLOWED_AUTH_MSCHAP:
case MM_BEARER_ALLOWED_AUTH_EAP:
default:
return BEARER_CINTERION_AUTH_UNKNOWN;
}
}
MMBearerAllowedAuth
mm_auth_type_from_cinterion_auth_type (guint cinterion_auth)
{
switch (cinterion_auth) {
case BEARER_CINTERION_AUTH_NONE:
return MM_BEARER_ALLOWED_AUTH_NONE;
case BEARER_CINTERION_AUTH_PAP:
return MM_BEARER_ALLOWED_AUTH_PAP;
case BEARER_CINTERION_AUTH_CHAP:
return MM_BEARER_ALLOWED_AUTH_CHAP;
default:
return MM_BEARER_ALLOWED_AUTH_UNKNOWN;
}
}
/* Cinterion authentication is done with the command AT^SGAUTH,
whose syntax depends on the modem family, as follow:
- AT^SGAUTH=<cid>[, <auth_type>[, <user>, <passwd>]] for the IMT family
- AT^SGAUTH=<cid>[, <auth_type>[, <passwd>, <user>]] for the rest */
gchar *
mm_broadband_bearer_cinterion_build_auth_string (gpointer log_object,
MMCinterionModemFamily modem_family,
MMBearerProperties *config,
guint cid)
{
MMBearerAllowedAuth auth;
BearerCinterionAuthType encoded_auth = BEARER_CINTERION_AUTH_UNKNOWN;
gboolean has_user;
gboolean has_passwd;
const gchar *user;
const gchar *passwd;
g_autofree gchar *quoted_user = NULL;
g_autofree gchar *quoted_passwd = NULL;
user = mm_bearer_properties_get_user (config);
passwd = mm_bearer_properties_get_password (config);
auth = mm_bearer_properties_get_allowed_auth (config);
has_user = (user && user[0]);
has_passwd = (passwd && passwd[0]);
encoded_auth = parse_auth_type (auth);
/* When 'none' requested, we won't require user/password */
if (encoded_auth == BEARER_CINTERION_AUTH_NONE) {
if (has_user || has_passwd)
mm_obj_warn (log_object, "APN user/password given but 'none' authentication requested");
if (modem_family == MM_CINTERION_MODEM_FAMILY_IMT)
return g_strdup_printf ("^SGAUTH=%u,%d,\"\",\"\"", cid, encoded_auth);
return g_strdup_printf ("^SGAUTH=%u,%d", cid, encoded_auth);
}
/* No explicit auth type requested? */
if (encoded_auth == BEARER_CINTERION_AUTH_UNKNOWN) {
/* If no user/passwd given, do nothing */
if (!has_user && !has_passwd)
return NULL;
/* If user/passwd given, default to CHAP (more common than PAP) */
mm_obj_dbg (log_object, "APN user/password given but no authentication type explicitly requested: defaulting to 'CHAP'");
encoded_auth = BEARER_CINTERION_AUTH_CHAP;
}
quoted_user = mm_port_serial_at_quote_string (user ? user : "");
quoted_passwd = mm_port_serial_at_quote_string (passwd ? passwd : "");
if (modem_family == MM_CINTERION_MODEM_FAMILY_IMT)
return g_strdup_printf ("^SGAUTH=%u,%d,%s,%s",
cid,
encoded_auth,
quoted_user,
quoted_passwd);
return g_strdup_printf ("^SGAUTH=%u,%d,%s,%s",
cid,
encoded_auth,
quoted_passwd,
quoted_user);
}
/******************************************************************************/
/* Dial 3GPP */
@@ -429,10 +319,10 @@ dial_3gpp_context_step (GTask *task)
case DIAL_3GPP_CONTEXT_STEP_AUTH: {
gchar *command;
command = mm_broadband_bearer_cinterion_build_auth_string (self,
mm_broadband_modem_cinterion_get_family (MM_BROADBAND_MODEM_CINTERION (ctx->modem)),
mm_base_bearer_peek_config (MM_BASE_BEARER (ctx->self)),
ctx->cid);
command = mm_cinterion_build_auth_string (self,
mm_broadband_modem_cinterion_get_family (MM_BROADBAND_MODEM_CINTERION (ctx->modem)),
mm_base_bearer_peek_config (MM_BASE_BEARER (ctx->self)),
ctx->cid);
if (command) {
mm_obj_dbg (self, "dial step %u/%u: authenticating...", ctx->step, DIAL_3GPP_CONTEXT_STEP_LAST);
@@ -51,11 +51,4 @@ void mm_broadband_bearer_cinterion_new (MMBroadbandModemCinterio
MMBaseBearer *mm_broadband_bearer_cinterion_new_finish (GAsyncResult *res,
GError **error);
gchar *mm_broadband_bearer_cinterion_build_auth_string (gpointer log_object,
MMCinterionModemFamily modem_family,
MMBearerProperties *config,
guint cid);
MMBearerAllowedAuth mm_auth_type_from_cinterion_auth_type (guint cinterion_auth);
#endif /* MM_BROADBAND_BEARER_CINTERION_H */
@@ -1260,10 +1260,10 @@ set_initial_eps_step (GTask *task)
case SET_INITIAL_EPS_STEP_AUTH: {
g_autofree gchar *auth_cmd = NULL;
auth_cmd = mm_broadband_bearer_cinterion_build_auth_string (self,
MM_BROADBAND_MODEM_CINTERION (self)->priv->modem_family,
ctx->properties,
ctx->cid);
auth_cmd = mm_cinterion_build_auth_string (self,
MM_BROADBAND_MODEM_CINTERION (self)->priv->modem_family,
ctx->properties,
ctx->cid);
mm_base_modem_at_command (
MM_BASE_MODEM (self),
auth_cmd,
@@ -29,6 +29,7 @@
#include "mm-errors-types.h"
#include "mm-modem-helpers-cinterion.h"
#include "mm-modem-helpers.h"
#include "mm-port-serial-at.h"
/* Setup relationship between the 3G band bitmask in the modem and the bitmask
* in ModemManager. */
@@ -1450,3 +1451,113 @@ mm_cinterion_provcfg_response_to_cid (const gchar *response,
/* in all other cases no change to the preset value */
}
}
/*****************************************************************************/
/* Auth related helpers */
typedef enum {
BEARER_CINTERION_AUTH_UNKNOWN = -1,
BEARER_CINTERION_AUTH_NONE = 0,
BEARER_CINTERION_AUTH_PAP = 1,
BEARER_CINTERION_AUTH_CHAP = 2,
BEARER_CINTERION_AUTH_MSCHAPV2 = 3,
} BearerCinterionAuthType;
static BearerCinterionAuthType
parse_auth_type (MMBearerAllowedAuth mm_auth)
{
switch (mm_auth) {
case MM_BEARER_ALLOWED_AUTH_NONE:
return BEARER_CINTERION_AUTH_NONE;
case MM_BEARER_ALLOWED_AUTH_PAP:
return BEARER_CINTERION_AUTH_PAP;
case MM_BEARER_ALLOWED_AUTH_CHAP:
return BEARER_CINTERION_AUTH_CHAP;
case MM_BEARER_ALLOWED_AUTH_MSCHAPV2:
return BEARER_CINTERION_AUTH_MSCHAPV2;
case MM_BEARER_ALLOWED_AUTH_UNKNOWN:
case MM_BEARER_ALLOWED_AUTH_MSCHAP:
case MM_BEARER_ALLOWED_AUTH_EAP:
default:
return BEARER_CINTERION_AUTH_UNKNOWN;
}
}
MMBearerAllowedAuth
mm_auth_type_from_cinterion_auth_type (guint cinterion_auth)
{
switch (cinterion_auth) {
case BEARER_CINTERION_AUTH_NONE:
return MM_BEARER_ALLOWED_AUTH_NONE;
case BEARER_CINTERION_AUTH_PAP:
return MM_BEARER_ALLOWED_AUTH_PAP;
case BEARER_CINTERION_AUTH_CHAP:
return MM_BEARER_ALLOWED_AUTH_CHAP;
default:
return MM_BEARER_ALLOWED_AUTH_UNKNOWN;
}
}
/* Cinterion authentication is done with the command AT^SGAUTH,
whose syntax depends on the modem family, as follow:
- AT^SGAUTH=<cid>[, <auth_type>[, <user>, <passwd>]] for the IMT family
- AT^SGAUTH=<cid>[, <auth_type>[, <passwd>, <user>]] for the rest */
gchar *
mm_cinterion_build_auth_string (gpointer log_object,
MMCinterionModemFamily modem_family,
MMBearerProperties *config,
guint cid)
{
MMBearerAllowedAuth auth;
BearerCinterionAuthType encoded_auth = BEARER_CINTERION_AUTH_UNKNOWN;
gboolean has_user;
gboolean has_passwd;
const gchar *user;
const gchar *passwd;
g_autofree gchar *quoted_user = NULL;
g_autofree gchar *quoted_passwd = NULL;
user = mm_bearer_properties_get_user (config);
passwd = mm_bearer_properties_get_password (config);
auth = mm_bearer_properties_get_allowed_auth (config);
has_user = (user && user[0]);
has_passwd = (passwd && passwd[0]);
encoded_auth = parse_auth_type (auth);
/* When 'none' requested, we won't require user/password */
if (encoded_auth == BEARER_CINTERION_AUTH_NONE) {
if (has_user || has_passwd)
mm_obj_warn (log_object, "APN user/password given but 'none' authentication requested");
if (modem_family == MM_CINTERION_MODEM_FAMILY_IMT)
return g_strdup_printf ("^SGAUTH=%u,%d,\"\",\"\"", cid, encoded_auth);
return g_strdup_printf ("^SGAUTH=%u,%d", cid, encoded_auth);
}
/* No explicit auth type requested? */
if (encoded_auth == BEARER_CINTERION_AUTH_UNKNOWN) {
/* If no user/passwd given, do nothing */
if (!has_user && !has_passwd)
return NULL;
/* If user/passwd given, default to CHAP (more common than PAP) */
mm_obj_dbg (log_object, "APN user/password given but no authentication type explicitly requested: defaulting to 'CHAP'");
encoded_auth = BEARER_CINTERION_AUTH_CHAP;
}
quoted_user = mm_port_serial_at_quote_string (user ? user : "");
quoted_passwd = mm_port_serial_at_quote_string (passwd ? passwd : "");
if (modem_family == MM_CINTERION_MODEM_FAMILY_IMT)
return g_strdup_printf ("^SGAUTH=%u,%d,%s,%s",
cid,
encoded_auth,
quoted_user,
quoted_passwd);
return g_strdup_printf ("^SGAUTH=%u,%d,%s,%s",
cid,
encoded_auth,
quoted_passwd,
quoted_user);
}
@@ -172,4 +172,14 @@ void mm_cinterion_provcfg_response_to_cid (const gchar *response,
gpointer log_object,
guint *cid);
/*****************************************************************************/
/* Auth related helpers */
MMBearerAllowedAuth mm_auth_type_from_cinterion_auth_type (guint cinterion_auth);
gchar *mm_cinterion_build_auth_string (gpointer log_object,
MMCinterionModemFamily modem_family,
MMBearerProperties *config,
guint cid);
#endif /* MM_MODEM_HELPERS_CINTERION_H */