[network-manager-applet/lr/pkcs11: 10/18] utils: move the key/cert file filters from wireless-security



commit 701cd188f252d319d8075889c8c002732af751e3
Author: Lubomir Rintel <lkundrak v3 sk>
Date:   Fri Mar 3 16:02:01 2017 +0100

    utils: move the key/cert file filters from wireless-security
    
    They're going to be useful elsewhere.
    
    Co-authored-by: Thomas Haller <thaller redhat com>

 src/utils/utils.c                  |   57 ++++++++++++++++++++++++++++
 src/utils/utils.h                  |    5 ++-
 src/wireless-security/eap-method.c |   72 ++----------------------------------
 3 files changed, 65 insertions(+), 69 deletions(-)
---
diff --git a/src/utils/utils.c b/src/utils/utils.c
index c3b806e..684987b 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -519,3 +519,60 @@ out:
                g_free (item);
        return success;
 }
+
+static gboolean
+file_has_extension (const char *filename, const char *const*extensions)
+{
+       const char *p;
+       gs_free char *ext = NULL;
+
+       if (!filename)
+               return FALSE;
+
+       p = strrchr (filename, '.');
+       if (!p)
+               return FALSE;
+
+       ext = g_ascii_strdown (p, -1);
+       return g_strv_contains (extensions, ext);
+}
+
+static gboolean
+cert_filter (const GtkFileFilterInfo *filter_info, gpointer data)
+{
+       static const char *const extensions[] = { ".der", ".pem", ".crt", ".cer", ".p12", NULL };
+
+       return file_has_extension (filter_info->filename, extensions);
+}
+
+static gboolean
+privkey_filter (const GtkFileFilterInfo *filter_info, gpointer user_data)
+{
+       static const char *const extensions[] = { ".der", ".pem", ".p12", ".key", NULL };
+
+       return file_has_extension (filter_info->filename, extensions);
+}
+
+GtkFileFilter *
+utils_cert_filter (void)
+{
+       GtkFileFilter *filter;
+
+       filter = gtk_file_filter_new ();
+       gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_FILENAME, cert_filter, NULL, NULL);
+       gtk_file_filter_set_name (filter, _("PEM certificates (*.pem, *.crt, *.cer)"));
+
+       return filter;
+}
+
+GtkFileFilter *
+utils_key_filter (void)
+{
+       GtkFileFilter *filter;
+
+       filter = gtk_file_filter_new ();
+       gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_FILENAME, privkey_filter, NULL, NULL);
+       gtk_file_filter_set_name (filter, _("DER, PEM, or PKCS#12 private keys (*.der, *.pem, *.p12, 
*.key)"));
+
+       return filter;
+}
diff --git a/src/utils/utils.h b/src/utils/utils.h
index 544833a..ccc653f 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -108,5 +108,8 @@ gboolean utils_tree_model_get_ip4_prefix (GtkTreeModel *model,
                                           guint32 *out,
                                           char **out_raw);
 
-#endif /* UTILS_H */
+GtkFileFilter *utils_cert_filter (void);
+
+GtkFileFilter *utils_key_filter (void);
 
+#endif /* UTILS_H */
diff --git a/src/wireless-security/eap-method.c b/src/wireless-security/eap-method.c
index cb733e8..c92eb7c 100644
--- a/src/wireless-security/eap-method.c
+++ b/src/wireless-security/eap-method.c
@@ -270,31 +270,6 @@ out:
        return success;
 }
 
-static gboolean
-file_has_extension (const char *filename, const char *extensions[])
-{
-       char *p, *ext;
-       int i = 0;
-       gboolean found = FALSE;
-
-       p = strrchr (filename, '.');
-       if (!p)
-               return FALSE;
-
-       ext = g_ascii_strdown (p, -1);
-       if (ext) {
-               while (extensions[i]) {
-                       if (!strcmp (ext, extensions[i++])) {
-                               found = TRUE;
-                               break;
-                       }
-               }
-       }
-       g_free (ext);
-
-       return found;
-}
-
 #if !LIBNM_BUILD
 static const char *
 find_tag (const char *tag, const char *buf, gsize len)
@@ -400,59 +375,20 @@ out:
 }
 #endif
 
-static gboolean
-default_filter_privkey (const GtkFileFilterInfo *filter_info, gpointer user_data)
-{
-       const char *extensions[] = { ".der", ".pem", ".p12", ".key", NULL };
-
-       if (!filter_info->filename)
-               return FALSE;
-
-       if (!file_has_extension (filter_info->filename, extensions))
-               return FALSE;
-
-       return TRUE;
-}
-
-static gboolean
-default_filter_cert (const GtkFileFilterInfo *filter_info, gpointer user_data)
-{
-       const char *extensions[] = { ".der", ".pem", ".crt", ".cer", NULL };
-
-       if (!filter_info->filename)
-               return FALSE;
-
-       if (!file_has_extension (filter_info->filename, extensions))
-               return FALSE;
-
-       return TRUE;
-}
-
 GtkFileFilter *
 eap_method_default_file_chooser_filter_new (gboolean privkey)
 {
-       GtkFileFilter *filter;
-
-       filter = gtk_file_filter_new ();
-       if (privkey) {
-               gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_FILENAME, default_filter_privkey, NULL, 
NULL);
-               gtk_file_filter_set_name (filter, _("DER, PEM, or PKCS#12 private keys (*.der, *.pem, *.p12, 
*.key)"));
-       } else {
-               gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_FILENAME, default_filter_cert, NULL, 
NULL);
-               gtk_file_filter_set_name (filter, _("DER or PEM certificates (*.der, *.pem, *.crt, *.cer)"));
-       }
-       return filter;
+       if (privkey)
+               return utils_key_filter ();
+       else
+               return utils_cert_filter ();
 }
 
 gboolean
 eap_method_is_encrypted_private_key (const char *path)
 {
-       GtkFileFilterInfo info = { .filename = path };
        gboolean is_encrypted;
 
-       if (!default_filter_privkey (&info, NULL))
-               return FALSE;
-
 #if LIBNM_BUILD
        is_encrypted = FALSE;
        if (!nm_utils_file_is_private_key (path, &is_encrypted))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]