diff options
author | Jiří Klimeš <jklimes@redhat.com> | 2015-02-11 13:29:30 +0100 |
---|---|---|
committer | Jiří Klimeš <jklimes@redhat.com> | 2015-02-11 13:34:38 +0100 |
commit | 37becd2ee653667e8d7f10c56ec46e988f1b8c35 (patch) | |
tree | 35e40f1b4b3e12af9464e8d2d5b3eb8de2717e24 /examples | |
parent | 0ff9b75387b7ac2fd0235b23699990834426c467 (diff) | |
download | NetworkManager-37becd2ee653667e8d7f10c56ec46e988f1b8c35.tar.gz |
examples: add a Lua example setting user name for a VPN connection
Diffstat (limited to 'examples')
-rw-r--r-- | examples/lua/lgi/Makefile.am | 3 | ||||
-rwxr-xr-x | examples/lua/lgi/change-vpn-username.lua | 85 |
2 files changed, 87 insertions, 1 deletions
diff --git a/examples/lua/lgi/Makefile.am b/examples/lua/lgi/Makefile.am index b282028967..2e83676e1a 100644 --- a/examples/lua/lgi/Makefile.am +++ b/examples/lua/lgi/Makefile.am @@ -4,4 +4,5 @@ EXTRA_DIST = \ list-devices.lua \ show-wifi-networks.lua \ get-basic-nm-info.lua \ - get-ips.lua + get-ips.lua \ + change-vpn-username.lua diff --git a/examples/lua/lgi/change-vpn-username.lua b/examples/lua/lgi/change-vpn-username.lua new file mode 100755 index 0000000000..2e93f9dd3a --- /dev/null +++ b/examples/lua/lgi/change-vpn-username.lua @@ -0,0 +1,85 @@ +#!/usr/bin/env lua +-- -*- Mode: Lua; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- +-- vim: ft=lua ts=2 sts=2 sw=2 et ai +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 2 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along +-- with this program; if not, write to the Free Software Foundation, Inc., +-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +-- +-- Copyright 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)) + |