/usr/lib/2013.com.canonical.certification:checkbox/bin/create_connection is in plainbox-provider-checkbox 0.4-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | #!/usr/bin/env python3
import sys
import os
import time
from subprocess import check_call, check_output, CalledProcessError
from uuid import uuid4
from argparse import ArgumentParser
CONNECTIONS_PATH = '/etc/NetworkManager/system-connections/'
def wifi_connection_section(ssid, uuid):
if not uuid:
uuid = uuid4()
connection = """
[connection]
id=%s
uuid=%s
type=802-11-wireless
""" % (ssid, uuid)
wireless = """
[802-11-wireless]
ssid=%s
mode=infrastructure""" % (ssid)
return connection + wireless
def wifi_security_section(security, key):
# Add security field to 802-11-wireless section
wireless_security = """
security=802-11-wireless-security
[802-11-wireless-security]
"""
if security.lower() == 'wpa':
wireless_security += """
key-mgmt=wpa-psk
auth-alg=open
psk=%s
""" % key
elif security.lower() == 'wep':
wireless_security += """
key-mgmt=none
wep-key=%s
""" % key
return wireless_security
def wifi_ip_sections():
ip = """
[ipv4]
method=auto
[ipv6]
method=auto
"""
return ip
def mobilebroadband_connection_section(name, uuid, connection_type):
if not uuid:
uuid = uuid4()
connection_section = """
[connection]
id={name}
uuid={uuid}
type={type}
autoconnect=false
""".format(name=name, uuid=uuid, type=connection_type)
return connection_section
def mobilebroadband_type_section(connection_type, apn,
username, password, pin):
number = ('*99#' if connection_type == 'gsm' else '#777')
type_section = """
[{type}]
number={number}
""".format(type=connection_type, number=number)
if apn:
type_section += "\napn={apn}".format(apn=apn)
if username:
type_section += "\nusername={username}".format(username=username)
if password:
type_section += "\npassword={password}".format(password=password)
if pin:
type_section += "\npin={pin}".format(pin=pin)
return type_section
def mobilebroadband_ppp_section():
return """
[ppp]
lcp-echo-interval=4
lcp-echo-failure=30
"""
def mobilebroadband_ip_section():
return """
[ipv4]
method=auto
"""
def mobilebroadband_serial_section():
return """
[serial]
baud=115200
"""
def block_until_created(connection, retries, interval):
while retries > 0:
nmcli_con_list = check_output(['nmcli', 'con', 'list'],
universal_newlines=True)
if connection in nmcli_con_list:
print("Connection %s registered" % connection)
break
time.sleep(interval)
retries = retries - 1
if retries <= 0:
print("Failed to register %s." % connection, file=sys.stderr)
sys.exit(1)
else:
try:
nmcli_con_up = check_call(['nmcli', 'con', 'up', 'id', connection])
print("Connection %s activated." % connection)
except CalledProcessError as error:
print("Failed to activate %s." % connection, file=sys.stderr)
sys.exit(error.returncode)
def write_connection_file(name, connection_info):
try:
connection_file = open(CONNECTIONS_PATH + name, 'w')
connection_file.write(connection_info)
os.fchmod(connection_file.fileno(), 0o600)
connection_file.close()
except IOError:
print("Can't write to " + CONNECTIONS_PATH + name
+ ". Is this command being run as root?", file=sys.stderr)
sys.exit(1)
def create_wifi_connection(args):
wifi_connection = wifi_connection_section(args.ssid, args.uuid)
if args.security:
# Set security options
if not args.key:
print("You need to specify a key using --key "
"if using wireless security.", file=sys.stderr)
sys.exit(1)
wifi_connection += wifi_security_section(args.security, args.key)
elif args.key:
print("You specified an encryption key "
"but did not give a security type "
"using --security.", file=sys.stderr)
sys.exit(1)
try:
check_call(['rfkill', 'unblock', 'wlan', 'wifi'])
except CalledProcessError:
print("Could not unblock wireless "
"devices with rfkill.", file=sys.stderr)
# Don't fail the script if unblock didn't work though
wifi_connection += wifi_ip_sections()
# NetworkManager replaces forward-slashes in SSIDs with asterisks
name = args.ssid.replace('/', '*')
write_connection_file(name, wifi_connection)
return name
def create_mobilebroadband_connection(args):
name = args.name
mobilebroadband_connection = mobilebroadband_connection_section(name,
args.uuid,
args.type)
mobilebroadband_connection += mobilebroadband_type_section(args.type,
args.apn,
args.username,
args.password,
args.pin)
if args.type == 'cdma':
mobilebroadband_connection += mobilebroadband_ppp_section()
mobilebroadband_connection += mobilebroadband_ip_section()
mobilebroadband_connection += mobilebroadband_serial_section()
write_connection_file(name, mobilebroadband_connection)
return name
def main():
parser = ArgumentParser()
subparsers = parser.add_subparsers(help="sub command help")
wifi_parser = subparsers.add_parser('wifi',
help='Create a Wifi connection.')
wifi_parser.add_argument('ssid',
help="The SSID to connect to.")
wifi_parser.add_argument('-S', '--security',
choices=['wpa', 'wep'],
help=("The type of security to be used by the "
"connection. No security will be used if "
"nothing is specified."))
wifi_parser.add_argument('-K', '--key',
help="The encryption key required by the router.")
wifi_parser.set_defaults(func=create_wifi_connection)
mobilebroadband_parser = subparsers.add_parser('mobilebroadband',
help="Create a "
"mobile "
"broadband "
"connection.")
mobilebroadband_parser.add_argument('type',
choices=['gsm', 'cdma'],
help="The type of connection.")
mobilebroadband_parser.add_argument('-n', '--name',
default='MobileBB',
help="The name of the connection.")
mobilebroadband_parser.add_argument('-a', '--apn',
help="The APN to connect to.")
mobilebroadband_parser.add_argument('-u', '--username',
help="The username required by the "
"mobile broadband access point.")
mobilebroadband_parser.add_argument('-p', '--password',
help="The password required by the "
"mobile broadband access point.")
mobilebroadband_parser.add_argument('-P', '--pin',
help="The PIN of the SIM "
"card, if set.")
mobilebroadband_parser.set_defaults(func=create_mobilebroadband_connection)
parser.add_argument('-U', '--uuid',
help="""The uuid to assign to the connection for use by
NetworkManager. One will be generated if not
specified here.""")
parser.add_argument('-R', '--retries',
help="""The number of times to attempt bringing up the
connection until it is confirmed as active.""",
default=5)
parser.add_argument('-I', '--interval',
help=("The time to wait between attempts to detect "
"the registration of the connection."),
default=2)
args = parser.parse_args()
# Call function to create the appropriate connection type
connection_name = args.func(args)
# Make sure we don't exit until the connection is fully created
block_until_created(connection_name, args.retries, args.interval)
if __name__ == "__main__":
main()
|