File: vpn-import-libnm.c

package info (click to toggle)
network-manager 1.52.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 71,048 kB
  • sloc: ansic: 480,022; python: 11,394; xml: 8,504; sh: 5,535; perl: 596; cpp: 178; javascript: 130; ruby: 107; makefile: 57; lisp: 22
file content (163 lines) | stat: -rw-r--r-- 4,375 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * The example shows how to import VPN connection from a file.
 *
 * @author: Jagadeesh Kotra <jagadeesh@stdin.top>
 *
 * Compile with:
 *   gcc -Wall vpn-import-libnm.c -o vpn-import-libnm `pkg-config --cflags --libs libnm`
 */

#include <glib.h>
#include <NetworkManager.h>

/*****************************************************************************/

static NMConnection *
vpn_connection_import(const char *filename)
{
    NMConnection *conn = NULL;
    GSList       *plugins;
    GSList       *iter;

    g_print("Try to import file \"%s\"...\n", filename);

    plugins = nm_vpn_plugin_info_list_load();

    for (iter = plugins; iter; iter = iter->next) {
        GError            *error  = NULL;
        NMVpnPluginInfo   *plugin = iter->data;
        NMVpnEditorPlugin *editor;
        const char        *plugin_name = nm_vpn_plugin_info_get_name(plugin);

        g_print("plugin[%s]: trying import...\n", plugin_name);

        editor = nm_vpn_plugin_info_load_editor_plugin(plugin, &error);
        if (error) {
            g_print("plugin[%s]: error loading plugin: %s\n", plugin_name, error->message);
            g_clear_error(&error);
            continue;
        }

        conn = nm_vpn_editor_plugin_import(editor, filename, &error);
        if (error) {
            g_print("plugin[%s]: error importing file: %s\n", plugin_name, error->message);
            g_clear_error(&error);
            continue;
        }

        if (!nm_connection_normalize(conn, NULL, NULL, &error)) {
            g_print("plugin[%s]: imported connection invalid: %s\n", plugin_name, error->message);
            g_clear_error(&error);
            g_clear_object(&conn);
            continue;
        }

        g_print("plugin[%s]: imported connection \"%s\" (%s)\n",
                plugin_name,
                nm_connection_get_id(conn),
                nm_connection_get_uuid(conn));
        break;
    }
    g_slist_free_full(plugins, g_object_unref);

    if (!conn) {
        g_print("Failure to import the file with any plugin\n");
        return NULL;
    }

    return conn;
}

/*****************************************************************************/

typedef struct {
    GMainLoop          *loop;
    GError             *error;
    NMRemoteConnection *rconn;
} RequestData;

static void
add_cb(GObject *source, GAsyncResult *result, gpointer user_data)
{
    RequestData *rdata = user_data;

    rdata->rconn = nm_client_add_connection_finish(NM_CLIENT(source), result, &rdata->error);
    g_main_loop_quit(rdata->loop);
}

static NMRemoteConnection *
connection_add(NMConnection *conn)
{
    GError     *error = NULL;
    NMClient   *client;
    RequestData rdata;

    g_print("Adding connection \"%s\" (%s)\n",
            nm_connection_get_id(conn),
            nm_connection_get_uuid(conn));

    client = nm_client_new(NULL, &error);
    if (!client) {
        g_print("Failure to connect with NetworkManager: %s\n", error->message);
        return NULL;
    }

    g_print("Adding connection \"%s\" (%s)\n",
            nm_connection_get_id(conn),
            nm_connection_get_uuid(conn));

    rdata = (RequestData) {
        .loop  = g_main_loop_new(NULL, FALSE),
        .rconn = NULL,
        .error = NULL,
    };

    nm_client_add_connection_async(client, conn, TRUE, NULL, add_cb, &rdata);

    g_main_loop_run(rdata.loop);

    g_clear_pointer(&rdata.loop, g_main_loop_unref);

    if (rdata.error != NULL) {
        g_print("Error: %s\n", rdata.error->message);
        g_clear_error(&rdata.error);
    } else {
        g_print("Connection successfully added: %s\n", nm_object_get_path(NM_OBJECT(rdata.rconn)));
    }

    g_clear_object(&client);

    return rdata.rconn;
}

/*****************************************************************************/

int
main(int argc, char **argv)
{
    NMRemoteConnection *rconn;
    NMConnection       *conn;
    const char         *filename;
    gboolean            success;

    if (argc < 2) {
        g_print("program takes exactly one(1) argument.\n");
        return 1;
    }

    filename = argv[1];

    conn = vpn_connection_import(filename);
    if (!conn)
        return 1;

    rconn = connection_add(conn);

    success = (rconn != NULL);

    g_clear_object(&conn);
    g_clear_object(&rconn);

    return success ? 0 : 1;
}