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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
#!/usr/bin/env lua
-- SPDX-License-Identifier: GPL-2.0-or-later
--
-- Copyright (C) 2015 Red Hat, Inc.
--
-- Script for importing/converting OpenConnect VPN configuration files for NetworkManager
-- In general, the implementation follows the logic of import() from
-- https://git.gnome.org/browse/network-manager-openconnect/tree/properties/nm-openconnect.c
----------------------
-- Helper functions --
----------------------
function read_all(in_file)
local f, msg = io.open(in_file, "r")
if not f then return nil, msg; end
local content = f:read("*all")
f:close()
return content
end
function uuid()
math.randomseed(os.time())
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
local uuid = string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and math.random(0, 0xf) or math.random(8, 0xb)
return string.format('%x', v)
end)
return uuid
end
function vpn_settings_to_text(vpn_settings)
local t = {}
for k,v in pairs(vpn_settings) do
t[#t+1] = k.."="..v
end
return table.concat(t, "\n")
end
function usage()
local basename = string.match(arg[0], '[^/\\]+$') or arg[0]
print(basename .. " - convert/import OpenConnect VPN configuration to NetworkManager")
print("Usage:")
print(" " .. basename .. " <input-file> <output-file>")
print(" - converts OpenConnect VPN config to NetworkManager keyfile")
print("")
print(" " .. basename .. " --import <input-file1> <input-file2> ...")
print(" - imports OpenConnect VPN config(s) to NetworkManager")
os.exit(1)
end
-------------------------------------------
-- Functions for VPN options translation --
-------------------------------------------
function handle_yes(t, option, value)
t[option] = "yes"
end
function handle_generic(t, option, value)
if not value[2] then io.stderr:write(string.format("Warning: ignoring invalid option '%s'\n", value[1])) end
t[option] = value[2]
end
-- global variables
g_con_data = {}
g_vpn_data = {}
vpn2nm = {
["Description"] = { nm_opt="id", func=handle_generic, tbl=g_con_data },
["Host"] = { nm_opt="gateway", func=handle_generic, tbl=g_vpn_data },
["CACert"] = { nm_opt="cacert", func=handle_generic, tbl=g_vpn_data },
["Proxy"] = { nm_opt="proxy", func=handle_generic, tbl=g_vpn_data },
["CSDEnable"] = { nm_opt="enable_csd_trojan", func=handle_yes, tbl=g_vpn_data },
["CSDWrapper"] = { nm_opt="csd_wrapper", func=handle_generic, tbl=g_vpn_data },
["UserCertificate"] = { nm_opt="usercert", func=handle_generic, tbl=g_vpn_data },
["PrivateKey"] = { nm_opt="userkey", func=handle_generic, tbl=g_vpn_data },
["FSID"] = { nm_opt="pem_passphrase_fsid", func=handle_yes, tbl=g_vpn_data },
["StokenSource"] = { nm_opt="stoken_source", func=handle_generic, tbl=g_vpn_data },
["StokenString"] = { nm_opt="stoken_string", func=handle_generic, tbl=g_vpn_data },
}
------------------------------------------------------
-- Read and convert the config into the global vars --
------------------------------------------------------
function read_and_convert(in_file)
local function line_split(str)
-- split at '=' character
local sep, fields = "=", {}
local pattern = string.format("([^%s]+)%s(.+)", sep, sep)
fields[1], fields[2] = str:match(pattern)
return fields
end
in_text, msg = read_all(in_file)
if not in_text then return false, msg end
-- loop through the config and convert it
for line in in_text:gmatch("[^\r\n]+") do
repeat
-- skip comments and empty lines
if line:find("^%s*[#;]") or line:find("^%s*$") then break end
-- trim leading and trailing spaces
line = line:find("^%s*$") and "" or line:match("^%s*(.*%S)")
local words = line_split(line)
local val = vpn2nm[words[1]]
if val then
if type(val) == "table" then val.func(val.tbl, val.nm_opt, words)
else print(string.format("debug: '%s' : val=%s"..val)) end
end
until true
end
-- check mandatory parameters
if not g_vpn_data["gateway"] then
local msg = in_file .. ": Not a valid OpenConnect VPN configuration"
return false, msg
end
return true
end
--------------------------------------------------------
-- Create and write connection file in keyfile format --
--------------------------------------------------------
function write_vpn_to_keyfile(in_file, out_file)
connection = [[
[connection]
id=__NAME_PLACEHOLDER__
uuid=__UUID_PLACEHOLDER__
type=vpn
autoconnect=no
[ipv4]
method=auto
never-default=true
[ipv6]
method=auto
[vpn]
service-type=org.freedesktop.NetworkManager.openconnect
]]
connection = connection .. vpn_settings_to_text(g_vpn_data)
local con_name = g_con_data["id"] or (out_file:gsub(".*/", ""))
connection = string.gsub(connection, "__NAME_PLACEHOLDER__", con_name)
connection = string.gsub(connection, "__UUID_PLACEHOLDER__", uuid())
-- write output file
local f, err = io.open(out_file, "w")
if not f then io.stderr:write(err) return false end
f:write(connection)
f:close()
local ofname = out_file:gsub(".*/", "")
io.stderr:write("Successfully converted VPN configuration: " .. in_file .. " => " .. out_file .. "\n")
io.stderr:write("To use the connection, do:\n")
io.stderr:write("# cp " .. out_file .. " /etc/NetworkManager/system-connections\n")
io.stderr:write("# chmod 600 /etc/NetworkManager/system-connections/" .. ofname .. "\n")
io.stderr:write("# nmcli con load /etc/NetworkManager/system-connections/" .. ofname .. "\n")
return true
end
---------------------------------------------
-- Import VPN connection to NetworkManager --
---------------------------------------------
function import_vpn_to_NM(filename)
local lgi = require 'lgi'
local GLib = lgi.GLib
local NM = lgi.NM
-- function creating NMConnection
local function create_profile(name)
local profile = NM.SimpleConnection.new()
s_con = NM.SettingConnection.new()
s_vpn = NM.SettingVpn.new()
s_con[NM.SETTING_CONNECTION_ID] = name
s_con[NM.SETTING_CONNECTION_UUID] = uuid()
s_con[NM.SETTING_CONNECTION_TYPE] = "vpn"
s_vpn[NM.SETTING_VPN_SERVICE_TYPE] = "org.freedesktop.NetworkManager.openconnect"
for k,v in pairs(g_vpn_data) do
s_vpn:add_data_item(k, v)
end
profile:add_setting(s_con)
profile:add_setting(s_vpn)
return profile
end
-- callback function for add_connection()
local function added_cb(client, result, data)
local con,err,code = client:add_connection_finish(result)
if con then
print(string.format("%s: Imported to NetworkManager: %s - %s",
filename, con:get_uuid(), con:get_id()))
else
io.stderr:write(code .. ": " .. err .. "\n");
return false
end
main_loop:quit()
end
local profile_name = g_con_data["id"] or string.match(filename, '[^/\\]+$') or filename
main_loop = GLib.MainLoop(nil, false)
local con = create_profile(profile_name)
local client = NM.Client.new()
-- send the connection to NetworkManager
client:add_connection_async(con, true, nil, added_cb, nil)
-- run main loop so that the callback could be called
main_loop:run()
return true
end
---------------------------
-- Main code starts here --
---------------------------
local import_mode = false
local infile, outfile
-- parse command-line arguments
if not arg[1] or arg[1] == "--help" or arg[1] == "-h" then usage() end
if arg[1] == "--import" or arg[1] == "-i" then
infile = arg[2]
if not infile then usage() end
import_mode = true
else
infile = arg[1]
outfile = arg[2]
if not infile or not outfile then usage() end
if arg[3] then usage() end
end
if import_mode then
-- check if lgi is available
local success,msg = pcall(require, 'lgi')
if not success then
io.stderr:write("Lua lgi module is not available, please install it (usually lua-lgi package)\n")
-- print(msg)
os.exit(1)
end
-- read configs, convert them and import to NM
for i = 2, #arg do
ok, err_msg = read_and_convert(arg[i])
if ok then import_vpn_to_NM(arg[i])
else io.stderr:write(err_msg .. "\n") end
-- reset global vars
g_con_data = {}
g_vpn_data = {}
end
else
-- read configs, convert them and write as NM keyfile connection
ok, err_msg = read_and_convert(infile)
if ok then write_vpn_to_keyfile(infile, outfile)
else io.stderr:write(err_msg .. "\n") end
end
|