]> code.delx.au - gnu-emacs-elpa/blob - packages/enwc/enwc-nm.el
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / enwc / enwc-nm.el
1 ;;; enwc-nm.el - The NetworkManager backend to ENWC
2
3 ;; Copyright (C) 2012,2013 Free Software Foundation, Inc.
4
5 ;; Author: Ian Dunn
6 ;; Keywords: enwc, network, wicd, manager, nm
7
8 ;; This file is part of ENWC
9
10 ;; ENWC is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; ENWC is distributed in the hope that it will be useful, but WITHOUT
16 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 ;; License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with ENWC; see the file COPYING. If not, write to the Free
22 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 ;; 02110-1301, USA.
24
25
26 ;;; Commentary:
27 ;;
28 ;; This requires NetworkManager >= 0.9.6
29 ;;
30
31 (require 'enwc)
32
33 (defgroup enwc-nm nil
34 "*NetworkManager variables for ENWC"
35 :prefix "enwc-nm-"
36 :group 'enwc)
37
38 (defcustom enwc-nm-dbus-service "org.freedesktop.NetworkManager"
39 "NetworkManager D-Bus service."
40 :group 'enwc-nm
41 :type 'string)
42
43 (defcustom enwc-nm-dbus-path "/org/freedesktop/NetworkManager"
44 "The default D-Bus path for NetworkManager."
45 :group 'enwc-nm
46 :type 'string)
47
48 (defcustom enwc-nm-dbus-settings-path "/org/freedesktop/NetworkManager/Settings"
49 "The settings D-Bus path for NetworkManager."
50 :group 'enwc-nm
51 :type 'string)
52
53 (defcustom enwc-nm-dbus-interface "org.freedesktop.NetworkManager"
54 "The default D-Bus interface for NetworkManager."
55 :group 'enwc-nm
56 :type 'string)
57
58 (defcustom enwc-nm-dbus-wireless-interface "org.freedesktop.NetworkManager.Device.Wireless"
59 "The wireless D-Bus interface for NetworkManager."
60 :group 'enwc-nm
61 :type 'string)
62
63 (defcustom enwc-nm-dbus-wired-interface "org.freedesktop.NetworkManager.Device.Wired"
64 "The wired D-Bus interface for NetworkManager."
65 :group 'enwc-nm
66 :type 'string)
67
68 (defcustom enwc-nm-dbus-device-interface "org.freedesktop.NetworkManager.Device"
69 "The device D-Bus interface for NetworkManager."
70 :group 'enwc-nm
71 :type 'string)
72
73 (defcustom enwc-nm-dbus-accesspoint-interface "org.freedesktop.NetworkManager.AccessPoint"
74 "The access point D-Bus interface for NetworkManager."
75 :group 'enwc-nm
76 :type 'string)
77
78 (defcustom enwc-nm-dbus-settings-interface "org.freedesktop.NetworkManager.Settings"
79 "The settings D-Bus interface for NetworkManager."
80 :group 'enwc-nm
81 :type 'string)
82
83 (defcustom enwc-nm-dbus-connections-interface "org.freedesktop.NetworkManager.Settings.Connection"
84 "The connections D-Bus interface for NetworkManager."
85 :group 'enwc-nm
86 :type 'string)
87
88 (defcustom enwc-nm-dbus-active-interface "org.freedesktop.NetworkManager.Connection.Active"
89 "The active connection D-Bus interface for NetworkManager."
90 :group 'enwc-nm
91 :type 'string)
92
93 (defvar enwc-nm-details-list
94 '("Ssid" "HwAddress" "Strength" "Flags" "Mode" "Frequency")
95 "The list of the desired details to be obtained from each network.")
96
97 (defvar enwc-nm-sec-types
98 '(("eap-leap" . (("Name" . "eap-leap")
99 ("reqs" . ((("identity" . "Username")
100 ("password" . "Password"))))))
101 ("eap-peap" . (("Name" . "eap-peap")
102 ("reqs" . ((("anonymous-identity" . "Anonymous Identity")
103 ("ca-cert" . "CA Certificate")
104 ("phase2-auth" . "Inner Authentication")
105 ("phase1-peapver" . "PEAP Version")
106 ("identity" . "Username")
107 ("password" . "Password"))))))
108 ("eap-tls" . (("Name" . "eap-tls")
109 ("reqs" . ((("identity" . "Identity")
110 ("client-cert" . "User Certificate")
111 ("ca-cert" . "CA Certificate")
112 ("private-key" . "Private Key")
113 ("private-key-password" . "Private Key Password"))))))
114 ("eap-ttls" . (("Name" . "eap-ttls")
115 ("reqs" . ((("anonymous-identity" . "Anonymous Identity")
116 ("ca-cert" . "CA Certificate")
117 ("phase2-auth" . "Inner Authentication")
118 ("identity" . "Username")
119 ("password" . "Password"))))))
120 ("wpa-psk" . (("Name" . "wpa2")
121 ("reqs" . ((("psk" . "PSK"))))))
122 ("wep" . (("Name" . "wep")
123 ("reqs" . ((("wep-key0" . "WEP Key")
124 ("wep-key-type" . "WEP Key Type"))))))
125 ("leap" . (("Name" . "leap")
126 ("reqs" . ((("leap-username" . "Username")
127 ("leap-password" . "Password"))))))
128 )
129 "The security types for NetworkManager.
130 This is still in the process of being worked on."
131 )
132
133 (defvar enwc-nm-wired-dev nil
134 "The wired device object path.")
135
136 (defvar enwc-nm-wireless-dev nil
137 "The wireless device object path.")
138
139 (defvar enwc-nm-active-ap nil
140 "The active access point object path.")
141
142 (defvar enwc-nm-connecting-p nil
143 "Whether or not NetworkManager is connecting.")
144
145 (defvar enwc-nm-wired-p nil
146 "Whether or not NetworkManager is wired.")
147
148 (defvar enwc-nm-edit-info nil
149 "The information for the network connection being edited.")
150
151 (defun enwc-nm-get-settings (conn)
152 "Gets the connection settings.
153 CONN is an object path to the connection."
154 (dbus-call-method :system
155 enwc-nm-dbus-service
156 conn
157 enwc-nm-dbus-connections-interface
158 "GetSettings"
159 :timeout 25000))
160
161
162 (defun enwc-nm-list-connections ()
163 "List the connections."
164 (dbus-call-method :system
165 enwc-nm-dbus-service
166 enwc-nm-dbus-settings-path
167 enwc-nm-dbus-settings-interface
168 "ListConnections"
169 :timeout 25000))
170
171 ;; Default
172 (defun enwc-nm-get-device-by-name (name)
173 (dbus-call-method :system
174 enwc-nm-dbus-service
175 enwc-nm-dbus-path
176 enwc-nm-dbus-interface
177 "GetDeviceByIpIface"
178 :timeout 25000
179 :string name))
180
181 ;; Settings, Connections
182 (defun enwc-nm-get-uuid-by-ssid (ssid)
183 "Gets the uuid of the network with ssid SSID."
184 (let ((conns (enwc-nm-list-connections))
185 cur-conn cur-ssid uuid)
186 (while (and conns (not uuid))
187 (setq cur-conn (pop conns))
188 (let ((settings (enwc-nm-get-settings cur-conn)))
189 (if (not (assoc "802-11-wireless" settings))
190 nil
191 (setq cur-ssid
192 (dbus-byte-array-to-string (car (cadr (assoc "ssid"
193 (cadr (assoc "802-11-wireless"
194 settings)))))))
195 (if (string= cur-ssid ssid)
196 (setq uuid
197 (car (cadr (assoc "uuid"
198 (cadr (assoc "connection"
199 settings))))))))))
200 uuid))
201
202 (defun enwc-nm-get-uuid-by-id (id)
203 "Gets a network connection's uuid by the network's id.
204 ID is a string that NetworkManager uses to identify this network."
205 (let ((conns (enwc-nm-list-connections))
206 cur-conn cur-id uuid)
207 (while (and conns (not uuid))
208 (setq cur-conn (pop conns))
209 (let ((settings (enwc-nm-get-settings cur-conn))
210 conn-set)
211 (if (not (assoc "connection" settings))
212 nil
213 (setq conn-set (assoc "connection" settings))
214 (setq cur-id (car (cadr (assoc "id" (cadr conn-set)))))
215 (if (string= cur-id id)
216 (setq uuid (car (cadr (assoc "uuid" (cadr conn-set)))))))))))
217
218 (defun enwc-nm-get-conn-by-uuid (uuid)
219 (dbus-call-method :system
220 enwc-nm-dbus-service
221 enwc-nm-dbus-settings-path
222 enwc-nm-dbus-settings-interface
223 "GetConnectionByUuid"
224 :timeout 25000
225 uuid))
226
227 ;; Settings
228 (defun enwc-nm-get-conn-by-ssid (ssid)
229 "Gets the connection path for the access point with ssid SSID."
230 (let ((uuid (enwc-nm-get-uuid-by-ssid ssid)))
231 (if uuid
232 (enwc-nm-get-conn-by-uuid uuid)
233 nil)))
234
235 (defun enwc-nm-get-conn-by-id (id)
236 "Gets a connection object with the id ID.
237 ID is the identifier used by Network Manager."
238 (let ((uuid (enwc-nm-get-uuid-by-id id)))
239 (enwc-nm-get-conn-by-uuid uuid)))
240
241 ;; Wireless
242 (defun enwc-nm-scan ()
243 "The NetworkManager scan function."
244 (dbus-call-method :system
245 enwc-nm-dbus-service
246 enwc-nm-wireless-dev
247 enwc-nm-dbus-wireless-interface
248 "RequestScan"
249 :timeout 25000
250 '(:array :signature "{sv}")))
251
252 ;; Wireless
253 (defun enwc-nm-get-networks ()
254 "The NetworkManager get networks function.
255 This returns a list of D-Bus paths to the access points."
256 (dbus-call-method :system
257 enwc-nm-dbus-service
258 enwc-nm-wireless-dev
259 enwc-nm-dbus-wireless-interface
260
261 "GetAccessPoints"))
262
263 ;; Accesspoint
264 (defun enwc-nm-get-wireless-network-property (id prop)
265 "The NetworkManager get wireless network property function.
266 This runs like normal, using element ID of `enwc-access-points'
267 to determine the access point path, then obtains the property
268 PROP from that access point. It also sets the channel from the
269 frequency if necessary."
270 (let ((ap (nth id enwc-access-points))
271 (mod-prop prop)
272 ret)
273 (if (string= mod-prop "essid")
274 (setq mod-prop "Ssid"))
275 (setq ret (dbus-get-property :system
276 enwc-nm-dbus-service
277 ap
278 enwc-nm-dbus-accesspoint-interface
279 mod-prop))
280 (if (not ret)
281 (progn
282 (if (not enwc-nm-edit-info)
283 (setq ret ""))
284 (progn
285 (setq ret (assoc mod-prop
286 (cadr (assoc "802-1x"
287 enwc-nm-edit-info))))
288 (if (not ret)
289 (setq ret (assoc mod-prop
290 (cadr (assoc "802-11-wireless-security"
291 enwc-nm-edit-info)))))
292 (setq ret (car (cadr ret))))))
293 (if (string= mod-prop "Frequency")
294 (setq ret (number-to-string (1+ (/ (- ret 2412) 5)))))
295 (if (string= mod-prop "Ssid")
296 (setq ret (dbus-byte-array-to-string ret)))
297 (if (string= mod-prop "Mode")
298 (setq ret (cond ((= ret 0) "Unknown")
299 ((= ret 1) "Ad-Hoc")
300 ((= ret 2) "Infrastructure"))))
301 ret))
302
303 (defun enwc-nm-get-wireless-nw-props (id)
304 (let ((ap (nth id enwc-access-points))
305 tmp-val
306 ret)
307 (setq ret (dbus-get-all-properties :system
308 enwc-nm-dbus-service
309 ap
310 enwc-nm-dbus-accesspoint-interface))
311 (setq tmp-val (cdr (assoc "Mode" ret)))
312 (setcdr (assoc "Frequency" ret)
313 (number-to-string (1+ (/ (- (cdr (assoc "Frequency" ret))
314 2412) 5))))
315 (setcdr (assoc "Ssid" ret)
316 (dbus-byte-array-to-string (cdr (assoc "Ssid" ret))))
317 (setcdr (assoc "Mode" ret)
318 (cond ((= tmp-val 0) "Unkown")
319 ((= tmp-val 1) "Ad-Hoc")
320 ((= tmp-val 2) "Infrastructure")))
321 ret))
322
323 (defun enwc-nm-get-conn-by-nid (nid)
324 "Gets a connection object with the network id NID."
325 (let* ((ssid (enwc-nm-get-wireless-network-property nid
326 "Ssid"))
327 (uuid (enwc-nm-get-uuid-by-ssid ssid)))
328 (if uuid
329 (enwc-nm-get-conn-by-uuid uuid)
330 nil)))
331
332 (defun enwc-nm-get-encryption-type (id)
333 "The NetworkManager get encryption type function.
334 This gets the WPA flags and RSN flags from access point in ID.
335 If both are 0, then it returns WEP, otherwise WPA."
336 (let ((wpa-flags (enwc-nm-get-wireless-network-property id "WpaFlags"))
337 (rsn-flags (enwc-nm-get-wireless-network-property id "RsnFlags")))
338 (if (and (= wpa-flags 0) (= rsn-flags 0))
339 "WEP"
340 "WPA")))
341
342 ;; Default
343 (defun enwc-nm-connect (id)
344 "The NetworkManager connect function.
345 This gets the connection path from ID, and connects to it."
346 (let ((ssid (enwc-nm-get-wireless-network-property id "Ssid")))
347 (dbus-call-method :system
348 enwc-nm-dbus-service
349 enwc-nm-dbus-path
350 enwc-nm-dbus-interface
351 "ActivateConnection"
352 :timeout 25000
353 :object-path (enwc-nm-get-conn-by-ssid ssid)
354 :object-path enwc-nm-wireless-dev
355 :object-path (nth id enwc-access-points))))
356
357
358 (defun enwc-nm-string-idx (obj list)
359 (let ((tmp-list list)
360 (cur-pos -1)
361 cur-obj)
362 (while (and (not cur-obj) tmp-list)
363 (setq cur-obj (pop tmp-list))
364 (setq cur-pos (1+ cur-pos))
365 (if (not (string= obj cur-obj))
366 (setq cur-obj nil)))
367 cur-pos))
368
369 (defun enwc-nm-wireless-prop-changed (props)
370 (if (assoc "ActiveAccessPoint" props)
371 (setq enwc-nm-active-ap (car (cadr (assoc "ActiveAccessPoint"
372 props))))))
373
374 ;; Wireless
375 (defun enwc-nm-get-current-nw-id (wired)
376 "The NetworkManager get current network id function.
377 This simply checks for the active access point."
378 (if (or wired (string= enwc-nm-active-ap "/"))
379 -1
380 (enwc-nm-string-idx enwc-nm-active-ap enwc-access-points)))
381
382 (defun enwc-nm-prop-changed (state)
383 (setq enwc-nm-connecting-p (eq state 40)))
384
385 ;; Default
386 (defun enwc-nm-check-connecting ()
387 "The NetworkManager check connecting function."
388 enwc-nm-connecting-p)
389
390 ;; Device
391 (defun enwc-nm-disconnect-wireless ()
392 (dbus-call-method :system
393 enwc-nm-dbus-service
394 enwc-nm-wireless-dev
395 enwc-nm-dbus-device-interface
396 "Disconnect"))
397
398 ;; Settings, Connections
399 (defun enwc-nm-get-wired-profiles ()
400 (let ((profs-list (enwc-nm-list-connections)))
401 (mapcar (lambda (x)
402 (let ((props (enwc-nm-get-settings x)))
403 (if (string= (caar props) "connection")
404 (car (cadr (car (cadr (car props))))))))
405 profs-list)))
406
407 (defun enwc-nm-wired-connect (id)
408 (let ((nid (nth id enwc-access-points)))
409 (dbus-call-method :system
410 enwc-nm-dbus-service
411 enwc-nm-dbus-path
412 enwc-nm-dbus-interface
413 :timeout 25000
414 :object-path (enwc-nm-get-conn-by-id nid)
415 :object-path enwc-nm-wired-dev
416 :objec-path "/")))
417
418 ;; Device
419 (defun enwc-nm-wired-disconnect ()
420 (dbus-call-method :system
421 enwc-nm-dbus-service
422 enwc-nm-wired-dev
423 enwc-nm-dbus-device-interface
424 "Disconnect"))
425
426 (defun enwc-nm-dev-prop-changed (new-state old-state reason)
427 (setq enwc-nm-wired-p (eq new-state 100)))
428
429 ;; Default
430 (defun enwc-nm-is-wired ()
431 enwc-nm-wired-p)
432
433 ;; Each entry in sec-types should be:
434 ;; ("IDENT" (("Name" . "NAME") ("reqs" . (("key1" . "Entry1") ("key2" . "Entry2") ... ))))
435 ;; Where:
436 ;; "IDENT" => String that identifies this to the backend.
437 ;; "NAME" => String that ENWC displays
438 ;; "reqs" => Constant string, but the association list holds entries
439 ;; required by the security type, i.e. user, passphrase, etc.
440 ;; "keyXX" => String that the backend uses for this security entry.
441 ;; "EntryXX" => String that ENWC displays for this security entry.
442
443 (defun enwc-nm-get-sec-types (wired)
444 "Get security types."
445 (if wired
446 nil
447 enwc-nm-sec-types)
448 )
449
450 (defun enwc-nm-gen-uuid ()
451 (random t)
452 (let ((hex-nums
453 (mapcar (lambda (x)
454 (random 65535))
455 (number-sequence 0 7)))
456 fin-str)
457 (setq fin-str (format "%04x%04x-%04x-%04x-%04x-%04x%04x%04x"
458 (nth 0 hex-nums)
459 (nth 1 hex-nums)
460 (nth 2 hex-nums)
461 (nth 3 hex-nums)
462 (nth 4 hex-nums)
463 (nth 5 hex-nums)
464 (nth 6 hex-nums)
465 (nth 7 hex-nums)))))
466
467 (defun enwc-nm-convert-addr (addr)
468 (if addr
469 (let* ((hex-addr (format "%08x" addr))
470 (ret-addr (format "%i.%i.%i.%i"
471 (string-to-number (substring hex-addr
472 6 8)
473 16)
474 (string-to-number (substring hex-addr
475 4 6)
476 16)
477 (string-to-number (substring hex-addr
478 2 4)
479 16)
480 (string-to-number (substring hex-addr
481 0 2)
482 16))))
483 ret-addr)
484 ""))
485
486 (defun enwc-nm-addr-back (addr)
487 (let ((bytes (split-string addr "."))
488 ret-val)
489 (setq ret-val (logior (lsh (string-to-number (nth 3 bytes))
490 0)
491 (lsh (string-to-number (nth 2 bytes))
492 1)
493 (lsh (string-to-number (nth 1 bytes))
494 2)
495 (lsh (string-to-number (nth 0 bytes))
496 3)))))
497
498 ;; These next two come from libnm-util/nm-utils.c in NM's source.
499
500 (defun enwc-nm-netmask-to-prefix (netmask)
501 "Converts a netmask to a prefix.
502 NETMASK is an ip address in network byte order."
503 (if netmask
504 (let* ((mask netmask)
505 (cur-pos 3)
506 (cur-mark (logand (lsh mask (* -8 cur-pos)) 255))
507 (pf 0))
508 (while (and (eq cur-mark 255) (>= cur-pos 0))
509 (setq pf (+ pf 8))
510 (setq cur-pos (1- cur-pos))
511 (setq cur-mark (logand (lsh mask (* -8 cur-pos)) 255))
512 )
513
514 (if (>= cur-pos 0)
515 (let ((v (logand (lsh mask (* -8 cur-pos)) 255)))
516 (while (not (eq v 0))
517 (setq pf (1+ pf))
518 (setq v (lsh v 1)))))
519 pf)
520 0))
521
522 (defun enwc-nm-prefix-to-netmask (prefix)
523 "Converts a prefix to a netmask.
524 PREFIX is an integer <= 32."
525 (let ((pf prefix)
526 (netmask 0)
527 (msk #x80000000))
528 (while (> pf 0)
529 (setq netmask (logior netmask msk))
530 (setq msk (lsh msk -1))
531 (setq pf (1- pf)))
532 netmask))
533
534 (defun enwc-nm-get-nw-info (wired id)
535 (let ((conn (enwc-nm-get-conn-by-nid id)))
536 (if conn
537 (setq enwc-nm-edit-info
538 (enwc-nm-get-settings conn))))
539
540 (let (ip-addr netmask gateway dns-list nw-info)
541 (if enwc-nm-edit-info
542 (progn
543 (setq ip-addr (nth 0 (caar (cadr (assoc "addresses"
544 (cadr (assoc "ipv4"
545 enwc-nm-edit-info))))))
546 netmask (nth 3 (caar (cadr (assoc "addresses"
547 (cadr (assoc "ipv4"
548 enwc-nm-edit-info))))))
549 gateway (nth 2 (caar (cadr (assoc "address"
550 (cadr (assoc "ipv4"
551 enwc-nm-edit-info))))))
552 dns-list (car (cadr (assoc "dns" (cadr (assoc "ipv4"
553 enwc-nm-edit-info))))))
554 (setq ip-addr (enwc-nm-convert-addr ip-addr)
555 netmask (enwc-nm-convert-addr netmask)
556 gateway (enwc-nm-convert-addr gateway)
557 dns-list (mapcar 'enwc-nm-convert-addr
558 dns-list))
559 (setq nw-info (list (cons (cons "addr"
560 ip-addr) nil)
561 (cons (cons "netmask"
562 netmask) nil)
563 (cons (cons "gateway"
564 gateway) nil)
565 (cons (cons "dns1"
566 (nth 0
567 dns-list))
568 nil)
569 (cons (cons "dns2"
570 (nth 1
571 dns-list))
572 nil)
573 (cons (cons "enctype"
574 "None") nil)
575 )))
576 nil)))
577
578 (defun enwc-nm-get-ip-addr (wired id)
579 "Gets the IP Address of a connection profile."
580 (let ((props (enwc-nm-get-settings (enwc-nm-get-conn-by-nid id)))
581 ipaddr ret-addr)
582 (if (not props)
583 (setq ret-addr "")
584 (setq ipaddr (nth 0 (caar (cadr (assoc "addresses"
585 (cadr (assoc "ipv4"
586 props)))))))
587 (setq ret-addr (enwc-nm-convert-addr ipaddr)))))
588
589 (defun enwc-nm-get-netmask (wired id)
590 "Gets the Netmask of a connection profile."
591 (let ((props (enwc-nm-get-settings (enwc-nm-get-conn-by-nid id)))
592 ipaddr hex-addr ret-addr)
593 (if (not props)
594 (setq ret-addr "")
595 (setq ipaddr (nth 3 (caar (cadr (assoc "addresses"
596 (cadr (assoc "ipv4"
597 props)))))))
598 (setq hex-addr (enwc-nm-prefix-to-netmask ipaddr))
599 (setq ret-addr (format "%i.%i.%i.%i"
600 (logand hex-addr 255)
601 (logand (lsh hex-addr -8) 255)
602 (logand (lsh hex-addr -16) 255)
603 (logand (lsh hex-addr -24) 255))))))
604
605
606 (defun enwc-nm-get-gateway (wired id)
607 "Gets the Gateway of a connection profile."
608 (let ((props (enwc-nm-get-settings (enwc-nm-get-conn-by-nid id)))
609 ipaddr ret-addr)
610 (if (not props)
611 (setq ret-addr "")
612 (setq ipaddr (nth 2 (caar (cadr (assoc "addresses"
613 (cadr (assoc "ipv4"
614 props)))))))
615 (setq ret-addr (enwc-nm-convert-addr ipaddr)))))
616
617 (defun enwc-nm-get-dns (wired id)
618 "Gets the DNS settings of a connection profile."
619 (let ((props (enwc-nm-get-settings (enwc-nm-get-conn-by-nid id)))
620 dns-list)
621 (setq dns-list (car (cadr (assoc "dns" (cadr (assoc "ipv4"
622 props))))))
623 (mapcar 'enwc-nm-convert-addr
624 dns-list)))
625
626 (defun enwc-nm-process-enctype (settings nw-settings)
627 "Process the encryption type.
628 Sets up the encryption type passed in through SETTINGS."
629 (let* ((ret-list nw-settings)
630 (req-list (nthcdr 6 settings))
631 (enctype (cdr (assoc "enctype" settings)))
632 key-mgmt
633 new-list name-list
634 ;;(name-list (cdr (assoc "reqs" (cdr (assoc enctype enwc-nm-sec-types)))))
635 key-name)
636
637 ;; There is a possibility that any of these don't exist in
638 ;; nw-settings.
639
640 (setq new-list `("802-11-wireless-security" (("pairwise" (("wep40" "wep104")))
641 ("group" (("wep40" "wep104")))
642 ("auth-alg" (nil))
643 ("key-mgmt" (nil)))))
644
645 (if (not (assoc "802-11-wireless-security" ret-list))
646 (setq ret-list (append ret-list (list new-list))))
647
648 (if (or (string= enctype "eap-leap")
649 (string= enctype "eap-peap")
650 (string= enctype "eap-tls")
651 (string= enctype "eap-ttls"))
652 (progn
653 (setq key-name "802-1x")
654 (setq key-mgmt "ieee8021x")
655 (setq req-list (push (cons "eap" (substring enctype 4)) req-list))
656 (print req-list)
657 ;;(setcdr (assoc "eap" (cadr (assoc "802-1x" ret-list))) (list (list (list (substring enctype 4)))))
658 )
659 (setq key-name "802-11-wireless-security")
660 (setq key-mgmt
661 (cond ((string= enctype "wep") "none")
662 ((string= enctype "wpa-psk") "wpa-psk")
663 ((string= enctype "leap") "iee8021x"))))
664 (setcdr (assoc "key-mgmt" (cadr (assoc "802-11-wireless-security" ret-list)))
665 (list (list key-mgmt)))
666
667 (if (string= enctype "leap")
668 (setcdr (assoc "auth-alg" (cadr (assoc "802-11-wireless-security" ret-list)))
669 (list (list "leap"))))
670
671 (setq name-list (cons key-name
672 (list (mapcar (lambda (x)
673 (cons (car x)
674 (cons (cons (cdr x) nil)
675 nil)))
676 req-list))))
677
678 (if (not (assoc key-name ret-list))
679 (setq ret-list (append ret-list (list name-list)))
680 (setcdr (assoc key-name ret-list) (list name-list)))
681
682
683 (if (or (string= enctype "eap-leap")
684 (string= enctype "eap-peap")
685 (string= enctype "eap-tls")
686 (string= enctype "eap-ttls"))
687 (setcdr (assoc "eap" (cadr (assoc "802-1x" ret-list)))
688 (cons (cons (cons (substring enctype 4) nil) nil) nil)))
689
690 ret-list))
691
692 (defun enwc-nm-finalize-settings (settings)
693 "Sets up all of the D-BUS types of a settings list.
694 SETTINGS is the list of settings list to setup.
695 This will place all of the necessary markers in the list,
696 such as :array, :dict-entry, etc."
697 (cons :array
698 (let (first-one)
699 (mapc (lambda (x)
700 (setq first-one (cons :dict-entry first-one))
701 (setq first-one
702 (cons (cons :string
703 (cons (car x)
704 (cons (cons :array
705 (let (this-one)
706 (mapc (lambda (y)
707 (setq this-one
708 (cons :dict-entry this-one))
709 (setq this-one
710 (cons (list :string (car y)
711 :variant
712 (if (string= (car y) "ssid")
713 (cons (dbus-string-to-byte-array
714 (dbus-byte-array-to-string (car (cadr y))))
715 nil)
716 (cadr y)))
717 this-one)))
718 (cadr x))
719 (nreverse this-one))) nil)))
720 first-one)))
721 settings)
722 (nreverse first-one))))
723
724 (defun enwc-nm-create-settings (wired ssid)
725 (let ((uuid (enwc-nm-gen-uuid))
726 (id (concat ssid " settings"))
727 type
728 ret-list
729 conn-list
730 ipv4-list ipv6-list
731 mod-list
732 new-ssid
733 80211-list)
734
735 (if (not wired)
736 (setq new-ssid (dbus-string-to-byte-array ssid))
737 (progn
738 (setq 80211-list `("802-11-wireless" (("security" (nil))
739 ("ssid" (,new-ssid))
740 ("mode" ("infrastructure")))))
741
742 (setq ret-list (append ret-list (list 80211-list))
743 type "802-11-wireless"))
744 (setq type "802-3-ethernet"))
745 (setq conn-list `("connection" (("id" (,id))
746 ("uuid" (,uuid))
747 ("autoconnect" (nil))
748 ("type" (,type)))))
749
750 (setq ipv4-list '("ipv4" (("addresses" (nil))
751 ("dns" (nil))
752 ("method" ("auto"))
753 ("routes" (nil)))))
754
755 (setq ipv6-list '("ipv4" (("addresses" (nil))
756 ("dns" (nil))
757 ("method" ("auto"))
758 ("routes" (nil)))))
759
760 (setq ret-list (append ret-list (list conn-list)))
761 (setq ret-list (append ret-list (list ipv4-list)))
762 (setq ret-list (append ret-list (list ipv6-list)))
763 ))
764
765 (defun enwc-nm-setup-settings (wired id settings)
766 "Sets up NetworkManager settings.
767 Gets the current network properties of network ID
768 and uses the information in the association list SETTINGS
769 to put it in the form that NetworkManager will recognize."
770 (print settings)
771 (let (ssid uuid conn props)
772
773 (if (not enwc-nm-edit-info)
774 (progn
775 (setq ssid
776 (enwc-nm-get-wireless-network-property (nth id
777 enwc-access-points)
778 "Ssid"))
779 (setq props (enwc-nm-create-settings wired ssid)))
780 (setq props enwc-nm-edit-info))
781
782 (print enwc-nm-edit-info)
783 ;; (setq uuid (enwc-nm-get-uuid-by-ssid ssid))
784 ;; (if uuid
785 ;; (setq conn (enwc-nm-get-conn-by-uuid uuid)))))
786
787 ;; (setq props
788 ;; (if conn
789 ;; (enwc-nm-get-settings (enwc-nm-get-conn-by-nid id))
790 ;; (enwc-nm-create-settings wired ssid)))
791
792 (setcdr (assoc "type" (cadr (assoc "connection" props)))
793 (list (list (cond (wired "802-3-ethernet")
794 ((not wired) "802-11-wireless")))))
795
796 (if (= (length (cdr (assoc "addr" settings))) 0)
797 (setcdr (assoc "addresses" (cadr (assoc "ipv4" props)))
798 (cons (cons nil nil) nil))
799
800 (setcdr (assoc "addresses" (cadr (assoc "ipv4" props)))
801 (list (list (list (list (enwc-nm-addr-back
802 (cdr (assoc "addr" settings)))
803 (enwc-nm-netmask-to-prefix (enwc-nm-addr-back
804 (cdr (assoc "netmask"
805 settings))))
806 (enwc-nm-addr-back
807 (cdr (assoc "gateway" settings)))))))))
808
809 (if (= (length (cdr (assoc "dns1" settings))) 0)
810 (setcdr (assoc "dns" (cadr (assoc "ipv4" props)))
811 (cons (cons nil nil) nil))
812 (setcdr (assoc "dns" (cadr (assoc "ipv4" props)))
813 (list (list (list (enwc-nm-addr-back
814 (cdr (assoc "dns1" settings)))
815 (enwc-nm-addr-back
816 (cdr (assoc "dns2" settings))))))))
817
818 (setq props (enwc-nm-process-enctype settings props))
819 (print props)
820
821 (enwc-nm-finalize-settings props)))
822
823 (defun enwc-nm-save-nw-settings (wired id settings)
824 "Saves network settings.
825 ID is the network id of the profile to save,
826 WIRED denotes whether or not this is a wired profile,
827 and SETTINGS is the list of settings."
828 (let ((mod-sets (enwc-nm-setup-settings wired id settings)))
829 (print mod-sets)
830 (print id)
831 (dbus-call-method :system
832 enwc-nm-dbus-service
833 ;;(nth id enwc-access-points)
834 (enwc-nm-get-conn-by-nid id)
835 enwc-nm-dbus-connections-interface
836 (if (not enwc-nm-edit-info)
837 "AddConnection"
838 "Update")
839 :timeout 25000
840 :array mod-sets)
841 ))
842
843 (defun enwc-nm-setup ()
844 (setq enwc-nm-wired-dev (enwc-nm-get-device-by-name enwc-wired-device)
845 enwc-nm-wireless-dev (enwc-nm-get-device-by-name enwc-wireless-device))
846
847 (dbus-register-signal :system
848 enwc-nm-dbus-service
849 enwc-nm-wireless-dev
850 enwc-nm-dbus-wireless-interface
851 "AccessPointAdded"
852 'enwc-process-scan)
853
854 (dbus-register-signal :system
855 enwc-nm-dbus-service
856 enwc-nm-wireless-dev
857 enwc-nm-dbus-wireless-interface
858 "AccessPointRemoved"
859 'enwc-process-scan)
860
861 (setq enwc-nm-active-ap
862 (let ((cur-net (dbus-get-property :system
863 enwc-nm-dbus-service
864 enwc-nm-wireless-dev
865 enwc-nm-dbus-wireless-interface
866 "ActiveAccessPoint")))
867 (if (string= cur-net "/")
868 "/"
869 cur-net)))
870
871 (dbus-register-signal :system
872 enwc-nm-dbus-service
873 enwc-nm-wireless-dev
874 enwc-nm-dbus-wireless-interface
875 "PropertiesChanged"
876 'enwc-nm-wireless-prop-changed)
877
878 (setq enwc-nm-connecting-p
879 (let ((state (dbus-get-property :system
880 enwc-nm-dbus-service
881 enwc-nm-dbus-path
882 enwc-nm-dbus-interface
883 "State")))
884 (eq state 40)))
885
886 (dbus-register-signal :system
887 enwc-nm-dbus-service
888 enwc-nm-wired-dev
889 enwc-nm-dbus-device-interface
890 "StateChanged"
891 'enwc-nm-dev-prop-changed)
892
893 (setq enwc-nm-wired-p
894 (let ((state (dbus-get-property :system
895 enwc-nm-dbus-service
896 enwc-nm-wired-dev
897 enwc-nm-dbus-device-interface
898 "State")))
899 (eq state 100)))
900 (dbus-register-signal :system
901 enwc-nm-dbus-service
902 enwc-nm-dbus-path
903 enwc-nm-dbus-interface
904 "StateChanged"
905 'enwc-nm-prop-changed)
906 )
907
908
909 (provide 'enwc-nm)
910
911 ;;; End of File.