NetworkManager/examples/lua/lgi/change-vpn-username.lua
Thomas Haller 977ea352a0
all: update deprecated SPDX license identifiers
These SPDX license identifiers are deprecated ([1]). Update them.

[1] https://spdx.org/licenses/

  sed \
     -e '1 s%^/\* SPDX-License-Identifier: \(GPL-2.0\|LGPL-2.1\)+ \*/$%/* SPDX-License-Identifier: \1-or-later */%' \
     -e '1,2 s%^\(--\|#\|//\) SPDX-License-Identifier: \(GPL-2.0\|LGPL-2.1\)+$%\1 SPDX-License-Identifier: \2-or-later%' \
     -i \
     $(git grep -l SPDX-License-Identifier -- \
         ':(exclude)shared/c-*/' \
         ':(exclude)shared/n-*/' \
         ':(exclude)shared/systemd/src' \
         ':(exclude)src/systemd/src')
2021-01-05 09:46:21 +01:00

70 lines
2 KiB
Lua
Executable file

#!/usr/bin/env lua
-- SPDX-License-Identifier: GPL-2.0-or-later
--
-- Copyright (C) 2015 Red Hat, Inc.
--
-- This example changes username in a VPN profile.
-- The example uses libnm library using GObject introspection via Lua lgi module.
-- Most distribution ship the module as lua-lgi package.
-- libnm guide: https://developer.gnome.org/libnm/1.0/
-- Lua-lgi guide: https://github.com/pavouk/lgi/blob/master/docs/guide.md
local lgi = require 'lgi'
local NM = lgi.NM
-- mapping from VPN type to username data item
vpn2username = {
openvpn = "username",
vpnc = "Xauth username",
pptp = "user",
l2tp = "user",
openswan = "leftxauthusername",
}
---------------------------
-- Main code starts here --
---------------------------
-- parse command-line arguments
local profile, username, extra = ...
if (not profile or not username or extra) then
print(string.format("Usage: %s <vpn_profile> <username>", arg[0]:gsub(".*/","")))
os.exit(1)
end
-- get client object
client = NM.Client.new()
-- find the connection profile
con = client:get_connection_by_id(profile)
if not con then con = client:get_connection_by_uuid(profile) end
if not con then con = client:get_connection_by_path(profile) end
if not con then io.stderr:write(string.format("Profile %s not found.\n", profile)) os.exit(1) end
if not con:is_type(NM.SETTING_VPN_SETTING_NAME) then
io.stderr:write(string.format("Profile '%s' is not a VPN.\n", profile))
os.exit(1)
end
-- get VPN setting
vpn = con:get_setting_vpn()
service_name = vpn:get_service_type()
vpn_type = service_name:match(".*%.(.*)")
if not vpn2username[vpn_type] then
io.stderr:write(string.format("Unknown VPN type '%s'.\n", vpn_type))
os.exit(1)
end
-- update the username
vpn:add_data_item(vpn2username[vpn_type], username)
-- save changes
ok, errmsg = con:commit_changes(true)
if not ok then
io.stderr:write(string.format("Error in updating connection: %s.\n", errmsg))
os.exit(1)
end
print(string.format("Username updated to '%s'.", username))