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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
|
/* nmcli - command-line tool to control NetworkManager
*
* 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 2010 - 2015 Red Hat, Inc.
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <glib/gi18n.h>
#include "nm-glib.h"
#include "polkit-agent.h"
#include "utils.h"
#include "general.h"
/* Available fields for 'general status' */
static NmcOutputField nmc_fields_nm_status[] = {
{"RUNNING", N_("RUNNING"), 15}, /* 0 */
{"VERSION", N_("VERSION"), 10}, /* 1 */
{"STATE", N_("STATE"), 15}, /* 2 */
{"STARTUP", N_("STARTUP"), 10}, /* 3 */
{"CONNECTIVITY", N_("CONNECTIVITY"), 15}, /* 4 */
{"NETWORKING", N_("NETWORKING"), 13}, /* 5 */
{"WIFI-HW", N_("WIFI-HW"), 15}, /* 6 */
{"WIFI", N_("WIFI"), 10}, /* 7 */
{"WWAN-HW", N_("WWAN-HW"), 15}, /* 8 */
{"WWAN", N_("WWAN"), 10}, /* 9 */
{"WIMAX-HW", N_("WIMAX-HW"), 15}, /* 10 */
{"WIMAX", N_("WIMAX"), 10}, /* 11 */
{NULL, NULL, 0}
};
#define NMC_FIELDS_NM_STATUS_ALL "RUNNING,VERSION,STATE,STARTUP,CONNECTIVITY,NETWORKING,WIFI-HW,WIFI,WWAN-HW,WWAN"
#define NMC_FIELDS_NM_STATUS_SWITCH "NETWORKING,WIFI-HW,WIFI,WWAN-HW,WWAN"
#define NMC_FIELDS_NM_STATUS_RADIO "WIFI-HW,WIFI,WWAN-HW,WWAN"
#define NMC_FIELDS_NM_STATUS_COMMON "STATE,CONNECTIVITY,WIFI-HW,WIFI,WWAN-HW,WWAN"
#define NMC_FIELDS_NM_NETWORKING "NETWORKING"
#define NMC_FIELDS_NM_WIFI "WIFI"
#define NMC_FIELDS_NM_WWAN "WWAN"
#define NMC_FIELDS_NM_WIMAX "WIMAX"
#define NMC_FIELDS_NM_CONNECTIVITY "CONNECTIVITY"
/* Available fields for 'general permissions' */
static NmcOutputField nmc_fields_nm_permissions[] = {
{"PERMISSION", N_("PERMISSION"), 57}, /* 0 */
{"VALUE", N_("VALUE"), 10}, /* 1 */
{NULL, NULL, 0}
};
#define NMC_FIELDS_NM_PERMISSIONS_ALL "PERMISSION,VALUE"
#define NMC_FIELDS_NM_PERMISSIONS_COMMON "PERMISSION,VALUE"
/* Available fields for 'general logging' */
static NmcOutputField nmc_fields_nm_logging[] = {
{"LEVEL", N_("LEVEL"), 10}, /* 0 */
{"DOMAINS", N_("DOMAINS"), 70}, /* 1 */
{NULL, NULL, 0}
};
#define NMC_FIELDS_NM_LOGGING_ALL "LEVEL,DOMAINS"
#define NMC_FIELDS_NM_LOGGING_COMMON "LEVEL,DOMAINS"
/* glib main loop variable - defined in nmcli.c */
extern GMainLoop *loop;
static void
usage_general (void)
{
g_printerr (_("Usage: nmcli general { COMMAND | help }\n\n"
"COMMAND := { status | hostname | permissions | logging }\n\n"
" status\n\n"
" hostname [<hostname>]\n\n"
" permissions\n\n"
" logging [level <log level>] [domains <log domains>]\n\n"));
}
static void
usage_general_status (void)
{
g_printerr (_("Usage: nmcli general status { help }\n"
"\n"
"Show overall status of NetworkManager.\n"
"'status' is the default action, which means 'nmcli gen' calls 'nmcli gen status'\n\n"));
}
static void
usage_general_hostname (void)
{
g_printerr (_("Usage: nmcli general hostname { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [<hostname>]\n"
"\n"
"Get or change persistent system hostname.\n"
"With no arguments, this prints currently configured hostname. When you pass\n"
"a hostname, NetworkManager will set it as the new persistent system hostname.\n\n"));
}
static void
usage_general_permissions (void)
{
g_printerr (_("Usage: nmcli general permissions { help }\n"
"\n"
"Show caller permissions for authenticated operations.\n\n"));
}
static void
usage_general_logging (void)
{
g_printerr (_("Usage: nmcli general logging { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [level <log level>] [domains <log domains>]\n"
"\n"
"Get or change NetworkManager logging level and domains.\n"
"Without any argument current logging level and domains are shown. In order to\n"
"change logging state, provide level and/or domain. Please refer to the man page\n"
"for the list of possible logging domains.\n\n"));
}
static void
usage_networking (void)
{
g_printerr (_("Usage: nmcli networking { COMMAND | help }\n\n"
"COMMAND := { [ on | off | connectivity ] }\n\n"
" on\n\n"
" off\n\n"
" connectivity [check]\n\n"));
}
static void
usage_networking_on (void)
{
g_printerr (_("Usage: nmcli networking on { help }\n"
"\n"
"Switch networking on.\n\n"));
}
static void
usage_networking_off (void)
{
g_printerr (_("Usage: nmcli networking off { help }\n"
"\n"
"Switch networking off.\n\n"));
}
static void
usage_networking_connectivity (void)
{
g_printerr (_("Usage: nmcli networking connectivity { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [check]\n"
"\n"
"Get network connectivity state.\n"
"The optional 'check' argument makes NetworkManager re-check the connectivity.\n\n"));
}
static void
usage_radio (void)
{
g_printerr (_("Usage: nmcli radio { COMMAND | help }\n\n"
"COMMAND := { all | wifi | wwan }\n\n"
" all | wifi | wwan [ on | off ]\n\n"
));
}
static void
usage_radio_all (void)
{
g_printerr (_("Usage: nmcli radio all { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [on | off]\n"
"\n"
"Get status of all radio switches, or turn them on/off.\n\n"));
}
static void
usage_radio_wifi (void)
{
g_printerr (_("Usage: nmcli radio wifi { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [on | off]\n"
"\n"
"Get status of Wi-Fi radio switch, or turn it on/off.\n\n"));
}
static void
usage_radio_wwan (void)
{
g_printerr (_("Usage: nmcli radio wwan { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [on | off]\n"
"\n"
"Get status of mobile broadband radio switch, or turn it on/off.\n\n"));
}
/* quit main loop */
static void
quit (void)
{
g_main_loop_quit (loop); /* quit main loop */
}
static const char *
nm_state_to_string (NMState state)
{
switch (state) {
case NM_STATE_ASLEEP:
return _("asleep");
case NM_STATE_CONNECTING:
return _("connecting");
case NM_STATE_CONNECTED_LOCAL:
return _("connected (local only)");
case NM_STATE_CONNECTED_SITE:
return _("connected (site only)");
case NM_STATE_CONNECTED_GLOBAL:
return _("connected");
case NM_STATE_DISCONNECTING:
return _("disconnecting");
case NM_STATE_DISCONNECTED:
return _("disconnected");
case NM_STATE_UNKNOWN:
default:
return _("unknown");
}
}
static NmcTermColor
state_to_color (NMState state)
{
switch (state) {
case NM_STATE_CONNECTING:
return NMC_TERM_COLOR_YELLOW;
case NM_STATE_CONNECTED_LOCAL:
case NM_STATE_CONNECTED_SITE:
case NM_STATE_CONNECTED_GLOBAL:
return NMC_TERM_COLOR_GREEN;
case NM_STATE_DISCONNECTING:
return NMC_TERM_COLOR_YELLOW;
case NM_STATE_ASLEEP:
case NM_STATE_DISCONNECTED:
return NMC_TERM_COLOR_RED;
default:
return NMC_TERM_COLOR_NORMAL;
}
}
static const char *
nm_connectivity_to_string (NMConnectivityState connectivity)
{
switch (connectivity) {
case NM_CONNECTIVITY_NONE:
return _("none");
case NM_CONNECTIVITY_PORTAL:
return _("portal");
case NM_CONNECTIVITY_LIMITED:
return _("limited");
case NM_CONNECTIVITY_FULL:
return _("full");
case NM_CONNECTIVITY_UNKNOWN:
default:
return _("unknown");
}
}
static NmcTermColor
connectivity_to_color (NMConnectivityState connectivity)
{
switch (connectivity) {
case NM_CONNECTIVITY_NONE:
return NMC_TERM_COLOR_RED;
case NM_CONNECTIVITY_PORTAL:
case NM_CONNECTIVITY_LIMITED:
return NMC_TERM_COLOR_YELLOW;
case NM_CONNECTIVITY_FULL:
return NMC_TERM_COLOR_GREEN;
default:
return NMC_TERM_COLOR_NORMAL;
}
}
static gboolean
show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_flds)
{
gboolean startup = FALSE;
NMState state = NM_STATE_UNKNOWN;
NMConnectivityState connectivity = NM_CONNECTIVITY_UNKNOWN;
gboolean net_enabled;
gboolean wireless_hw_enabled, wireless_enabled;
gboolean wwan_hw_enabled, wwan_enabled;
GError *error = NULL;
const char *fields_str;
const char *fields_all = print_flds ? print_flds : NMC_FIELDS_NM_STATUS_ALL;
const char *fields_common = print_flds ? print_flds : NMC_FIELDS_NM_STATUS_COMMON;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
fields_str = fields_common;
else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0)
fields_str = fields_all;
else
fields_str = nmc->required_fields;
tmpl = nmc_fields_nm_status;
tmpl_len = sizeof (nmc_fields_nm_status);
nmc->print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: only these fields are allowed: %s"), fields_all);
g_error_free (error);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
return FALSE;
}
nmc->get_client (nmc); /* create NMClient */
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
return FALSE;
}
if (!nmc_versions_match (nmc))
return FALSE;
state = nm_client_get_state (nmc->client);
startup = nm_client_get_startup (nmc->client);
connectivity = nm_client_get_connectivity (nmc->client);
net_enabled = nm_client_networking_get_enabled (nmc->client);
wireless_hw_enabled = nm_client_wireless_hardware_get_enabled (nmc->client);
wireless_enabled = nm_client_wireless_get_enabled (nmc->client);
wwan_hw_enabled = nm_client_wwan_hardware_get_enabled (nmc->client);
wwan_enabled = nm_client_wwan_get_enabled (nmc->client);
nmc->print_fields.header_name = pretty_header_name ? (char *) pretty_header_name : _("NetworkManager status");
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (nmc->output_data, arr);
arr = nmc_dup_fields_array (tmpl, tmpl_len, 0);
set_val_strc (arr, 0, _("running"));
set_val_strc (arr, 1, nm_client_get_version (nmc->client));
set_val_strc (arr, 2, nm_state_to_string (state));
set_val_strc (arr, 3, startup ? _("starting") : _("started"));
set_val_strc (arr, 4, nm_connectivity_to_string (connectivity));
set_val_strc (arr, 5, net_enabled ? _("enabled") : _("disabled"));
set_val_strc (arr, 6, wireless_hw_enabled ? _("enabled") : _("disabled"));
set_val_strc (arr, 7, wireless_enabled ? _("enabled") : _("disabled"));
set_val_strc (arr, 8, wwan_hw_enabled ? _("enabled") : _("disabled"));
set_val_strc (arr, 9, wwan_enabled ? _("enabled") : _("disabled"));
/* Set colors */
arr[2].color = state_to_color (state);
arr[3].color = startup ? NMC_TERM_COLOR_YELLOW : NMC_TERM_COLOR_GREEN;
arr[4].color = connectivity_to_color (connectivity);
arr[5].color = net_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED;
arr[6].color = wireless_hw_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED;
arr[7].color = wireless_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED;
arr[8].color = wwan_hw_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED;
arr[9].color = wwan_enabled ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED;
g_ptr_array_add (nmc->output_data, arr);
print_data (nmc); /* Print all data */
return TRUE;
}
#define NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK "org.freedesktop.NetworkManager.enable-disable-network"
#define NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI "org.freedesktop.NetworkManager.enable-disable-wifi"
#define NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN "org.freedesktop.NetworkManager.enable-disable-wwan"
#define NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX "org.freedesktop.NetworkManager.enable-disable-wimax"
#define NM_AUTH_PERMISSION_SLEEP_WAKE "org.freedesktop.NetworkManager.sleep-wake"
#define NM_AUTH_PERMISSION_NETWORK_CONTROL "org.freedesktop.NetworkManager.network-control"
#define NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED "org.freedesktop.NetworkManager.wifi.share.protected"
#define NM_AUTH_PERMISSION_WIFI_SHARE_OPEN "org.freedesktop.NetworkManager.wifi.share.open"
#define NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM "org.freedesktop.NetworkManager.settings.modify.system"
#define NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN "org.freedesktop.NetworkManager.settings.modify.own"
#define NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME "org.freedesktop.NetworkManager.settings.modify.hostname"
static const char *
permission_to_string (NMClientPermission perm)
{
switch (perm) {
case NM_CLIENT_PERMISSION_ENABLE_DISABLE_NETWORK:
return NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK;
case NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIFI:
return NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI;
case NM_CLIENT_PERMISSION_ENABLE_DISABLE_WWAN:
return NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN;
case NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIMAX:
return NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX;
case NM_CLIENT_PERMISSION_SLEEP_WAKE:
return NM_AUTH_PERMISSION_SLEEP_WAKE;
case NM_CLIENT_PERMISSION_NETWORK_CONTROL:
return NM_AUTH_PERMISSION_NETWORK_CONTROL;
case NM_CLIENT_PERMISSION_WIFI_SHARE_PROTECTED:
return NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED;
case NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN:
return NM_AUTH_PERMISSION_WIFI_SHARE_OPEN;
case NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM:
return NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM;
case NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN:
return NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN;
case NM_CLIENT_PERMISSION_SETTINGS_MODIFY_HOSTNAME:
return NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME;
default:
return _("unknown");
}
}
static const char *
permission_result_to_string (NMClientPermissionResult perm_result)
{
switch (perm_result) {
case NM_CLIENT_PERMISSION_RESULT_YES:
return _("yes");
case NM_CLIENT_PERMISSION_RESULT_NO:
return _("no");
case NM_CLIENT_PERMISSION_RESULT_AUTH:
return _("auth");
default:
return _("unknown");
}
}
static gboolean
show_nm_permissions (NmCli *nmc)
{
NMClientPermission perm;
GError *error = NULL;
const char *fields_str;
const char *fields_all = NMC_FIELDS_NM_PERMISSIONS_ALL;
const char *fields_common = NMC_FIELDS_NM_PERMISSIONS_COMMON;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
fields_str = fields_common;
else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0)
fields_str = fields_all;
else
fields_str = nmc->required_fields;
tmpl = nmc_fields_nm_permissions;
tmpl_len = sizeof (nmc_fields_nm_permissions);
nmc->print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: 'general permissions': %s"), error->message);
g_error_free (error);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
return FALSE;
}
nmc->get_client (nmc); /* create NMClient */
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
return FALSE;
}
nmc->print_fields.header_name = _("NetworkManager permissions");
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (nmc->output_data, arr);
for (perm = NM_CLIENT_PERMISSION_NONE + 1; perm <= NM_CLIENT_PERMISSION_LAST; perm++) {
NMClientPermissionResult perm_result = nm_client_get_permission_result (nmc->client, perm);
arr = nmc_dup_fields_array (tmpl, tmpl_len, 0);
set_val_strc (arr, 0, permission_to_string (perm));
set_val_strc (arr, 1, permission_result_to_string (perm_result));
g_ptr_array_add (nmc->output_data, arr);
}
print_data (nmc); /* Print all data */
return TRUE;
}
static gboolean
show_general_logging (NmCli *nmc)
{
char *level = NULL;
char *domains = NULL;
GError *error = NULL;
const char *fields_str;
const char *fields_all = NMC_FIELDS_NM_LOGGING_ALL;
const char *fields_common = NMC_FIELDS_NM_LOGGING_COMMON;
NmcOutputField *tmpl, *arr;
size_t tmpl_len;
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
fields_str = fields_common;
else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0)
fields_str = fields_all;
else
fields_str = nmc->required_fields;
tmpl = nmc_fields_nm_logging;
tmpl_len = sizeof (nmc_fields_nm_logging);
nmc->print_fields.indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: 'general logging': %s"), error->message);
g_error_free (error);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
return FALSE;
}
nmc->get_client (nmc); /* create NMClient */
nm_client_get_logging (nmc->client, &level, &domains, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
g_error_free (error);
return FALSE;
}
nmc->print_fields.header_name = _("NetworkManager logging");
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (nmc->output_data, arr);
arr = nmc_dup_fields_array (tmpl, tmpl_len, 0);
set_val_str (arr, 0, level);
set_val_str (arr, 1, domains);
g_ptr_array_add (nmc->output_data, arr);
print_data (nmc); /* Print all data */
return TRUE;
}
static void
save_hostname_cb (GObject *object, GAsyncResult *result, gpointer user_data)
{
NmCli *nmc = (NmCli *) user_data;
GError *error = NULL;
nm_client_save_hostname_finish (NM_CLIENT (object), result, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: failed to set hostname: %s"),
error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_error_free (error);
}
quit ();
}
/*
* Entry point function for general operations 'nmcli general'
*/
NMCResultCode
do_general (NmCli *nmc, int argc, char **argv)
{
GError *error = NULL;
/* Register polkit agent */
nmc_start_polkit_agent_start_try (nmc);
if (argc == 0) {
if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error)) {
g_string_printf (nmc->return_text, _("Error: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
goto finish;
}
show_nm_status (nmc, NULL, NULL);
}
if (argc > 0) {
if (nmc_arg_is_help (*argv)) {
usage_general ();
}
else if (matches (*argv, "status") == 0) {
if (nmc_arg_is_help (*(argv+1))) {
usage_general_status ();
goto finish;
}
if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error)) {
g_string_printf (nmc->return_text, _("Error: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
goto finish;
}
show_nm_status (nmc, NULL, NULL);
}
else if (matches (*argv, "hostname") == 0) {
if (nmc_arg_is_help (*(argv+1))) {
usage_general_hostname ();
goto finish;
}
if (next_arg (&argc, &argv) != 0) {
/* no arguments -> get hostname */
char *hostname = NULL;
nmc->get_client (nmc); /* create NMClient */
g_object_get (nmc->client, NM_CLIENT_HOSTNAME, &hostname, NULL);
if (hostname)
g_print ("%s\n", hostname);
g_free (hostname);
} else {
/* hostname provided -> set it */
const char *hostname = *argv;
if (next_arg (&argc, &argv) == 0)
g_print ("Warning: ignoring extra garbage after '%s' hostname\n", hostname);
nmc->should_wait = TRUE;
nmc->get_client (nmc); /* create NMClient */
nm_client_save_hostname_async (nmc->client, hostname, NULL, save_hostname_cb, nmc);
}
}
else if (matches (*argv, "permissions") == 0) {
if (nmc_arg_is_help (*(argv+1))) {
usage_general_permissions ();
goto finish;
}
if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error)) {
g_string_printf (nmc->return_text, _("Error: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
goto finish;
}
show_nm_permissions (nmc);
}
else if (matches (*argv, "logging") == 0) {
if (nmc_arg_is_help (*(argv+1))) {
usage_general_logging ();
goto finish;
}
if (next_arg (&argc, &argv) != 0) {
/* no arguments -> get logging level and domains */
if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error)) {
g_string_printf (nmc->return_text, _("Error: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
goto finish;
}
show_general_logging (nmc);
} else {
/* arguments provided -> set logging level and domains */
const char *level = NULL;
const char *domains = NULL;
nmc_arg_t exp_args[] = { {"level", TRUE, &level, TRUE},
{"domains", TRUE, &domains, TRUE},
{NULL} };
if (!nmc_parse_args (exp_args, FALSE, &argc, &argv, &error)) {
g_string_assign (nmc->return_text, error->message);
nmc->return_value = error->code;
goto finish;
}
nmc->get_client (nmc); /* create NMClient */
nm_client_set_logging (nmc->client, level, domains, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: failed to set logging: %s"),
error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
goto finish;
}
}
}
else {
usage_general ();
g_string_printf (nmc->return_text, _("Error: 'general' command '%s' is not valid."), *argv);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
}
}
finish:
if (error)
g_error_free (error);
return nmc->return_value;
}
static gboolean
nmc_switch_show (NmCli *nmc, const char *switch_name, const char *header)
{
g_return_val_if_fail (nmc != NULL, FALSE);
g_return_val_if_fail (switch_name != NULL, FALSE);
if (nmc->required_fields && strcasecmp (nmc->required_fields, switch_name) != 0) {
g_string_printf (nmc->return_text, _("Error: '--fields' value '%s' is not valid here (allowed field: %s)"),
nmc->required_fields, switch_name);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
return FALSE;
}
if (nmc->print_output == NMC_PRINT_NORMAL)
nmc->print_output = NMC_PRINT_TERSE;
if (!nmc->required_fields)
nmc->required_fields = g_strdup (switch_name);
return show_nm_status (nmc, header, NULL);
}
static gboolean
nmc_switch_parse_on_off (NmCli *nmc, const char *arg1, const char *arg2, gboolean *res)
{
g_return_val_if_fail (nmc != NULL, FALSE);
g_return_val_if_fail (arg1 && arg2, FALSE);
g_return_val_if_fail (res != NULL, FALSE);
if (!strcmp (arg2, "on"))
*res = TRUE;
else if (!strcmp (arg2, "off"))
*res = FALSE;
else {
g_string_printf (nmc->return_text, _("Error: invalid '%s' argument: '%s' (use on/off)."), arg1, arg2);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
return FALSE;
}
return TRUE;
}
static gboolean
show_networking_connectivity (NmCli *nmc)
{
return nmc_switch_show (nmc, NMC_FIELDS_NM_CONNECTIVITY, _("Connectivity"));
}
/*
* Entry point function for 'nmcli networking'
*/
NMCResultCode
do_networking (NmCli *nmc, int argc, char **argv)
{
gboolean enable_flag;
/* Register polkit agent */
nmc_start_polkit_agent_start_try (nmc);
if (argc == 0)
nmc_switch_show (nmc, NMC_FIELDS_NM_NETWORKING, _("Networking"));
else if (argc > 0) {
if (nmc_arg_is_help (*argv)) {
usage_networking ();
} else if (matches (*argv, "connectivity") == 0) {
if (nmc_arg_is_help (*(argv+1))) {
usage_networking_connectivity ();
goto finish;
}
if (next_arg (&argc, &argv) != 0) {
/* no arguments -> get current state */
show_networking_connectivity (nmc);
} else if (matches (*argv, "check") == 0) {
GError *error = NULL;
nmc->get_client (nmc); /* create NMClient */
nm_client_check_connectivity (nmc->client, NULL, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_clear_error (&error);
} else
show_networking_connectivity (nmc);
} else {
usage_networking ();
g_string_printf (nmc->return_text, _("Error: 'networking connectivity' command '%s' is not valid."), *argv);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
}
} else if (nmc_switch_parse_on_off (nmc, *(argv-1), *argv, &enable_flag)) {
if (nmc_arg_is_help (*(argv+1))) {
if (enable_flag)
usage_networking_on ();
else
usage_networking_off ();
goto finish;
}
nmc->get_client (nmc); /* create NMClient */
nm_client_networking_set_enabled (nmc->client, enable_flag, NULL);
} else {
usage_networking ();
g_string_printf (nmc->return_text, _("Error: 'networking' command '%s' is not valid."), *argv);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
}
}
finish:
quit ();
return nmc->return_value;
}
/*
* Entry point function for radio switch commands 'nmcli radio'
*/
NMCResultCode
do_radio (NmCli *nmc, int argc, char **argv)
{
GError *error = NULL;
gboolean enable_flag;
/* Register polkit agent */
nmc_start_polkit_agent_start_try (nmc);
if (argc == 0) {
if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error)) {
g_string_printf (nmc->return_text, _("Error: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
g_error_free (error);
goto finish;
}
show_nm_status (nmc, _("Radio switches"), NMC_FIELDS_NM_STATUS_RADIO);
}
if (argc > 0) {
if (nmc_arg_is_help (*argv)) {
usage_radio ();
}
else if (matches (*argv, "all") == 0) {
if (nmc_arg_is_help (*(argv+1))) {
usage_radio_all ();
goto finish;
}
if (next_arg (&argc, &argv) != 0) {
/* no argument, show all radio switches */
if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error)) {
g_string_printf (nmc->return_text, _("Error: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
g_error_free (error);
goto finish;
}
show_nm_status (nmc, _("Radio switches"), NMC_FIELDS_NM_STATUS_RADIO);
} else {
if (!nmc_switch_parse_on_off (nmc, *(argv-1), *argv, &enable_flag))
goto finish;
nmc->get_client (nmc); /* create NMClient */
nm_client_wireless_set_enabled (nmc->client, enable_flag);
nm_client_wimax_set_enabled (nmc->client, enable_flag);
nm_client_wwan_set_enabled (nmc->client, enable_flag);
}
}
else if (matches (*argv, "wifi") == 0) {
if (nmc_arg_is_help (*(argv+1))) {
usage_radio_wifi ();
goto finish;
}
if (next_arg (&argc, &argv) != 0) {
/* no argument, show current WiFi state */
nmc_switch_show (nmc, NMC_FIELDS_NM_WIFI, _("Wi-Fi radio switch"));
} else {
if (!nmc_switch_parse_on_off (nmc, *(argv-1), *argv, &enable_flag))
goto finish;
nmc->get_client (nmc); /* create NMClient */
nm_client_wireless_set_enabled (nmc->client, enable_flag);
}
}
else if (matches (*argv, "wwan") == 0) {
if (nmc_arg_is_help (*(argv+1))) {
usage_radio_wwan ();
goto finish;
}
if (next_arg (&argc, &argv) != 0) {
/* no argument, show current WWAN (mobile broadband) state */
nmc_switch_show (nmc, NMC_FIELDS_NM_WWAN, _("WWAN radio switch"));
} else {
if (!nmc_switch_parse_on_off (nmc, *(argv-1), *argv, &enable_flag))
goto finish;
nmc->get_client (nmc); /* create NMClient */
nm_client_wwan_set_enabled (nmc->client, enable_flag);
}
}
else {
usage_radio ();
g_string_printf (nmc->return_text, _("Error: 'radio' command '%s' is not valid."), *argv);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
}
}
finish:
quit ();
return nmc->return_value;
}
|