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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <strings.h>
#include <ctype.h>
#include <libnvpair.h>
#include <libintl.h>
#include <libgen.h>
#include <pwd.h>
#include <auth_attr.h>
#include <secdb.h>
#include <libscf.h>
#include <limits.h>
#include <locale.h>
#include <libstmf.h>
#include <libiscsit.h>
/* what's this used for?? */
#define ITADM_VERSION "1.0"
/* SMF service info */
#define ISCSIT_SVC "svc:/network/iscsi/target:default"
#define STMF_STALE(ret) {\
if (ret == STMF_ERROR_PROV_DATA_STALE) {\
output_config_error(ret, NULL);\
} else if (ret != 0) {\
output_config_error(ret,\
gettext("Configuration change failed"));\
}\
}
#define ITADM_CHKAUTH(sec) {\
if (!chkauthattr(sec, itadm_uname)) {\
(void) fprintf(stderr,\
gettext("Error, operation requires authorization %s"),\
sec);\
(void) fprintf(stderr, "\n");\
return (1);\
}\
}
static struct option itadm_long[] = {
{"alias", required_argument, NULL, 'l'},
{"auth-method", required_argument, NULL, 'a'},
{"chap-secret", no_argument, NULL, 's'},
{"chap-secret-file", required_argument, NULL, 'S'},
{"chap-user", required_argument, NULL, 'u'},
{"force", no_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"help", no_argument, NULL, '?'},
{"isns", required_argument, NULL, 'i'},
{"isns-server", required_argument, NULL, 'I'},
{"node-name", required_argument, NULL, 'n'},
{"radius-secret", no_argument, NULL, 'd'},
{"radius-secret-file", required_argument, NULL, 'D'},
{"radius-server", required_argument, NULL, 'r'},
{"tpg-tag", required_argument, NULL, 't'},
{"verbose", no_argument, NULL, 'v'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}
};
char c_tgt[] = "itadm create-target [-a radius|chap|none|default] [-s] \
[-S chap-secret-path] [-u chap-username] [-n target-node-name] \
[-l alias] [-t tpg-name[,tpg-name,...]]";
static char m_tgt[] = "itadm modify-target [-a radius|chap|none|default] [-s] \
[-S chap-secret-path] [-u chap-username] [-n new-target-node-name] \
[-l alias] [-t tpg-name[,tpg-name,...]] target-node-name";
static char d_tgt[] = "itadm delete-target [-f] target-node-name";
static char l_tgt[] = "itadm list-target [-v] [target-node-name]";
static char c_tpg[] = "itadm create-tpg tpg-name IP-address[:port] \
[IP-address[:port]] [...]";
static char l_tpg[] = "itadm list-tpg [-v] [tpg-name]";
static char d_tpg[] = "itadm delete-tpg [-f] tpg-name";
static char c_ini[] = "itadm create-initiator [-s] [-S chap-secret-path] \
[-u chap-username] initiator-node-name";
static char m_ini[] = "itadm modify-initiator [-s] [-S chap-secret-path] \
[-u chap-username] initiator-node-name";
static char l_ini[] = "itadm list-initiator [-v] initiator-node-name";
static char d_ini[] = "itadm delete-inititator initiator-node-name";
static char m_def[] = "itadm modify-defaults [-a radius|chap|none] \
[-r IP-address[:port]] [-d] [-D radius-secret-path] [-i enable|disable] \
[-I IP-address[:port][,IP-adddress[:port]]]";
static char l_def[] = "itadm list-defaults";
/* keep the order of this enum in the same order as the 'subcmds' struct */
typedef enum {
CREATE_TGT,
MODIFY_TGT,
DELETE_TGT,
LIST_TGT,
CREATE_TPG,
DELETE_TPG,
LIST_TPG,
CREATE_INI,
MODIFY_INI,
LIST_INI,
DELETE_INI,
MODIFY_DEF,
LIST_DEF,
NULL_SUBCMD /* must always be last! */
} itadm_sub_t;
typedef struct {
char *name;
char *shortopts;
char *usemsg;
} itadm_subcmds_t;
static itadm_subcmds_t subcmds[] = {
{"create-target", ":a:sS:u:n:l:t:h?", c_tgt},
{"modify-target", ":a:sS:u:n:l:t:h?", m_tgt},
{"delete-target", ":fh?", d_tgt},
{"list-target", ":vh?", l_tgt},
{"create-tpg", ":h?", c_tpg},
{"delete-tpg", ":fh?", d_tpg},
{"list-tpg", ":vh?", l_tpg},
{"create-initiator", ":sS:u:h?", c_ini},
{"modify-initiator", ":sS:u:h?", m_ini},
{"list-initiator", ":vh?", l_ini},
{"delete-initiator", ":h?", d_ini},
{"modify-defaults", ":a:r:dD:i:I:h?", m_def},
{"list-defaults", ":h?", l_def},
{NULL, ":h?", NULL},
};
/* used for checking if user is authorized */
static char *itadm_uname = NULL;
/* prototypes */
static int
itadm_get_password(nvlist_t *nvl, char *key, char *passfile,
char *phrase);
static int
itadm_opt_to_arr(nvlist_t *nvl, char *key, char *opt, uint32_t *num);
static int
create_target(char *tgt, nvlist_t *proplist);
static int
modify_target(char *tgt, char *new, nvlist_t *proplist);
static int
delete_target(char *tgt, boolean_t force);
static int
list_target(char *tgt, boolean_t verbose, boolean_t script);
static int
create_tpg(char *tpg, int addrc, char **addrs);
static int
list_tpg(char *tpg, boolean_t verbose, boolean_t script);
static int
delete_tpg(char *tpg, boolean_t force);
static int
modify_initiator(char *ini, nvlist_t *proplist, boolean_t create);
static int
list_initiator(char *ini, boolean_t verbose, boolean_t script);
static int
delete_initiator(char *ini);
static int
modify_defaults(nvlist_t *proplist);
static int
list_defaults(boolean_t script);
static void
tag_name_to_num(char *tagname, uint16_t *tagnum);
/* prototype from iscsit_common.h */
extern int
sockaddr_to_str(struct sockaddr_storage *sa, char **addr);
static void output_config_error(int error_code, char *msg);
int
main(int argc, char *argv[])
{
int ret = 0;
int idx = NULL_SUBCMD;
char c;
int newargc = argc;
char **newargv = NULL;
char *objp;
int itind = 0;
nvlist_t *proplist = NULL;
boolean_t verbose = B_FALSE;
boolean_t scripting = B_FALSE;
boolean_t tbool;
char *targetname = NULL;
char *propname;
boolean_t force = B_FALSE;
struct passwd *pwd = NULL;
uint32_t count = 0;
char *smfstate = NULL;
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
if (argc < 2) {
ret = 1;
goto usage_error;
}
for (idx = 0; subcmds[idx].name != NULL; idx++) {
if (strcmp(argv[1], subcmds[idx].name) == 0) {
break;
}
}
/* get the caller's user name for subsequent chkauthattr() calls */
pwd = getpwuid(getuid());
if (pwd == NULL) {
(void) fprintf(stderr, "%s\n",
gettext("Could not determine callers user name"));
return (1);
}
itadm_uname = strdup(pwd->pw_name);
/* increment past command & subcommand */
newargc--;
newargv = &(argv[1]);
ret = nvlist_alloc(&proplist, NV_UNIQUE_NAME, 0);
if (ret != 0) {
ret = errno;
output_config_error(ret, gettext("Could not allocate nvlist"));
ret = 1;
goto usage_error;
}
while ((ret == 0) && (newargv)) {
c = getopt_long(newargc, newargv, subcmds[idx].shortopts,
itadm_long, &itind);
if (c == -1) {
break;
}
switch (c) {
case 0:
/* flag set by getopt */
break;
case 'a':
ret = nvlist_add_string(proplist,
"auth", optarg);
break;
case 'd':
ret = itadm_get_password(proplist,
"radiussecret", NULL,
gettext("Enter RADIUS secret: "));
break;
case 'D':
ret = itadm_get_password(proplist,
"radiussecret", optarg, NULL);
break;
case 'f':
force = B_TRUE;
break;
case '?':
/*
* '?' is returned for both unrecognized
* options and if explicitly provided on
* the command line. The latter should
* be handled the same as -h.
*/
if (strcmp(newargv[optind-1], "-?") != 0) {
(void) fprintf(stderr,
gettext("Unrecognized option %s"),
newargv[optind-1]);
(void) fprintf(stderr, "\n");
ret = 1;
}
goto usage_error;
case 'h':
goto usage_error;
case 'i':
if (strncmp(optarg, "enable", strlen(optarg))
== 0) {
tbool = B_TRUE;
} else if (strncmp(optarg, "disable",
strlen(optarg)) == 0) {
tbool = B_FALSE;
} else {
(void) fprintf(stderr, "%s\n",
gettext("invalid value for -i"));
ret = 1;
break;
}
ret = nvlist_add_boolean_value(proplist,
"isns", tbool);
break;
case 'I':
/* possibly multi-valued */
ret = itadm_opt_to_arr(proplist,
"isnsserver", optarg, &count);
if ((ret == 0) && (count > 8)) {
(void) fprintf(stderr, "%s\n",
gettext(
"Too many iSNS servers specified, "
"maximum of 8 allowed"));
ret = 1;
}
break;
case 'l':
ret = nvlist_add_string(proplist,
"alias", optarg);
break;
case 'n':
targetname = strdup(optarg);
if (targetname == NULL) {
ret = ENOMEM;
}
break;
case 'r':
ret = nvlist_add_string(proplist,
"radiusserver", optarg);
break;
case 's':
if ((idx == CREATE_TGT) ||
(idx == MODIFY_TGT)) {
propname = "targetchapsecret";
} else {
propname = "chapsecret";
}
ret = itadm_get_password(proplist,
propname, NULL,
gettext("Enter CHAP secret: "));
break;
case 'S':
if ((idx == CREATE_TGT) ||
(idx == MODIFY_TGT)) {
propname = "targetchapsecret";
} else {
propname = "chapsecret";
}
ret = itadm_get_password(proplist,
propname, optarg, NULL);
break;
case 't':
/* possibly multi-valued */
ret = itadm_opt_to_arr(proplist,
"tpg-tag", optarg, NULL);
break;
case 'u':
if ((idx == CREATE_TGT) ||
(idx == MODIFY_TGT)) {
propname = "targetchapuser";
} else {
propname = "chapuser";
}
ret = nvlist_add_string(proplist,
propname, optarg);
break;
case 'v':
verbose = B_TRUE;
break;
case ':':
(void) fprintf(stderr,
gettext("Option %s requires an operand"),
newargv[optind-1]);
(void) fprintf(stderr, "\n");
/* fall through to default */
default:
ret = 1;
break;
}
}
if (ret != 0) {
goto usage_error;
}
/* after getopt() to allow handling of -h option */
if ((itadm_sub_t)idx == NULL_SUBCMD) {
(void) fprintf(stderr, "%s\n",
gettext("Error, no subcommand specified"));
ret = 1;
goto usage_error;
}
/*
* some subcommands take multiple operands, so adjust now that
* getopt is complete
*/
newargc -= optind;
if (newargc == 0) {
newargv = NULL;
objp = NULL;
} else {
newargv = &(newargv[optind]);
objp = newargv[0];
}
if (objp == NULL) {
switch ((itadm_sub_t)idx) {
case MODIFY_TGT:
case DELETE_TGT:
case CREATE_TPG:
case DELETE_TPG:
case CREATE_INI:
case MODIFY_INI:
case DELETE_INI:
/* These subcommands need at least one operand */
(void) fprintf(stderr,
gettext("Error, %s requires an operand"),
subcmds[idx].name);
(void) fprintf(stderr, "\n");
ret = 1;
goto usage_error;
default:
break;
}
}
if (newargc > 1) {
switch ((itadm_sub_t)idx) {
case MODIFY_TGT:
case DELETE_TGT:
case LIST_TGT:
case DELETE_TPG:
case LIST_TPG:
case CREATE_INI:
case MODIFY_INI:
case LIST_INI:
case DELETE_INI:
/* These subcommands should have at most one operand */
(void) fprintf(stderr,
gettext("Error, %s accepts only a single operand"),
subcmds[idx].name);
(void) fprintf(stderr, "\n");
ret = 1;
goto usage_error;
default:
break;
}
}
if (newargc > 0) {
switch ((itadm_sub_t)idx) {
case CREATE_TGT:
case MODIFY_DEF:
case LIST_DEF:
/* These subcommands do not support an operand */
(void) fprintf(stderr,
gettext("Error, %s does not support any operands"),
subcmds[idx].name);
(void) fprintf(stderr, "\n");
ret = 1;
goto usage_error;
default:
break;
}
}
/*
* XXX - this should probably get pushed down to the library
* depending on the decision to allow/disallow configuratoin
* without the service running.
*/
/*
* Make sure iSCSI target service is enabled before
* proceeding.
*/
smfstate = smf_get_state(ISCSIT_SVC);
if (!smfstate ||
(strcmp(smfstate, SCF_STATE_STRING_ONLINE) != 0)) {
(void) fprintf(stderr, "%s\n",
gettext("The iSCSI target service must be online "
"before running this command."));
(void) fprintf(stderr,
gettext("Use 'svcadm enable -r %s'"), ISCSIT_SVC);
(void) fprintf(stderr, "\n");
(void) fprintf(stderr, "%s\n",
gettext("to enable the service and its prerequisite "
"services and/or"));
(void) fprintf(stderr,
gettext("'svcs -x %s' to determine why it is not online."),
ISCSIT_SVC);
(void) fprintf(stderr, "\n");
return (1);
}
switch ((itadm_sub_t)idx) {
case CREATE_TGT:
/*
* OK for targetname to be NULL here. If the
* user did not specify a target name,
* one will be generated.
*/
ret = create_target(targetname, proplist);
break;
case MODIFY_TGT:
ret = modify_target(objp, targetname, proplist);
break;
case DELETE_TGT:
ret = delete_target(objp, force);
break;
case LIST_TGT:
ret = list_target(objp, verbose, scripting);
break;
case CREATE_TPG:
ret = create_tpg(objp, newargc - 1, &(newargv[1]));
break;
case DELETE_TPG:
ret = delete_tpg(objp, force);
break;
case LIST_TPG:
ret = list_tpg(objp, verbose, scripting);
break;
case CREATE_INI:
ret = modify_initiator(objp, proplist, B_TRUE);
break;
case MODIFY_INI:
ret = modify_initiator(objp, proplist, B_FALSE);
break;
case LIST_INI:
ret = list_initiator(objp, verbose, scripting);
break;
case DELETE_INI:
ret = delete_initiator(objp);
break;
case MODIFY_DEF:
ret = modify_defaults(proplist);
break;
case LIST_DEF:
ret = list_defaults(scripting);
break;
default:
ret = 1;
goto usage_error;
}
if (ret != 0) {
(void) fprintf(stderr,
gettext("itadm %s failed with error %d"),
subcmds[idx].name, ret);
(void) fprintf(stderr, "\n");
}
return (ret);
usage_error:
if (subcmds[idx].name) {
(void) printf("%s\n", gettext(subcmds[idx].usemsg));
} else {
/* overall usage */
(void) printf("%s\n\n", gettext("itadm usage:"));
for (idx = 0; subcmds[idx].name != NULL; idx++) {
if (!subcmds[idx].usemsg) {
continue;
}
(void) printf("\t%s\n", gettext(subcmds[idx].usemsg));
}
}
return (ret);
}
static int
create_target(char *tgt, nvlist_t *proplist)
{
int ret;
it_config_t *cfg = NULL;
it_tgt_t *tgtp;
char **tags = NULL;
uint32_t count = 0;
nvlist_t *errlist = NULL;
int i;
it_tpg_t *tpg = NULL;
uint16_t tagid = 0;
it_tpgt_t *tpgt;
char *sec = "solaris.smf.modify.stmf";
boolean_t did_it_config_load = B_FALSE;
ITADM_CHKAUTH(sec);
if (tgt) {
/*
* Validate target name.
*/
if (!IS_IQN_NAME(tgt) && !IS_EUI_NAME(tgt)) {
(void) fprintf(stderr, gettext("Invalid name %s"),
tgt);
(void) fprintf(stderr, "\n");
return (EINVAL);
}
}
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
goto done;
}
did_it_config_load = B_TRUE;
ret = it_tgt_create(cfg, &tgtp, tgt);
if (ret != 0) {
if (ret == EFAULT) {
(void) fprintf(stderr,
gettext("Invalid iSCSI name %s"), tgt);
(void) fprintf(stderr, "\n");
} else if (ret == EEXIST) {
(void) fprintf(stderr,
gettext("iSCSI target %s already configured"),
tgt);
(void) fprintf(stderr, "\n");
} else if (ret == E2BIG) {
(void) fprintf(stderr,
gettext("Maximum of %d iSCSI targets"),
MAX_TARGETS);
(void) fprintf(stderr, "\n");
} else {
output_config_error(ret,
gettext("Error creating target"));
}
goto done;
}
/* set the target portal group tags */
ret = nvlist_lookup_string_array(proplist, "tpg-tag", &tags,
&count);
if (ret == ENOENT) {
/* none specified. is this ok? */
ret = 0;
} else if (ret != 0) {
output_config_error(ret, gettext("Internal error"));
goto done;
}
/* special case, don't set any TPGs */
if (tags && (count == 1) && (strcmp("default", tags[0]) == 0)) {
count = 0;
}
for (i = 0; i < count; i++) {
if (!tags[i]) {
continue;
}
/* see that all referenced groups are already defined */
tpg = cfg->config_tpg_list;
while (tpg != NULL) {
if (strcmp(tags[i], tpg->tpg_name) == 0) {
break;
}
tpg = tpg->tpg_next;
}
if (tpg == NULL) {
(void) fprintf(stderr,
gettext("Invalid tpg-tag %s, tag not defined"),
tags[i]);
(void) fprintf(stderr, "\n");
ret = 1;
goto done;
}
/* generate the tag number to use */
tag_name_to_num(tags[i], &tagid);
ret = it_tpgt_create(cfg, tgtp, &tpgt, tags[i], tagid);
if (ret != 0) {
(void) fprintf(stderr, gettext(
"Could not add target portal group tag %s: "),
tags[i]);
output_config_error(ret, NULL);
goto done;
}
tagid++;
}
/* remove the tags from the proplist before continuing */
if (tags) {
(void) nvlist_remove_all(proplist, "tpg-tag");
}
ret = it_tgt_setprop(cfg, tgtp, proplist, &errlist);
if (ret != 0) {
(void) fprintf(stderr,
gettext("Error setting target properties: %d"), ret);
(void) fprintf(stderr, "\n");
if (errlist) {
nvpair_t *nvp = NULL;
char *nn;
char *nv;
while ((nvp = nvlist_next_nvpair(errlist, nvp))
!= NULL) {
nv = NULL;
nn = nvpair_name(nvp);
(void) nvpair_value_string(nvp, &nv);
if (nv != NULL) {
(void) fprintf(stderr, "\t%s: %s\n",
nn, nv);
}
}
nvlist_free(errlist);
}
goto done;
}
if (ret == 0) {
ret = it_config_commit(cfg);
STMF_STALE(ret);
}
done:
if (ret == 0) {
(void) printf(gettext("Target %s successfully created"),
tgtp->tgt_name);
(void) printf("\n");
}
if (did_it_config_load)
it_config_free(cfg);
return (ret);
}
int
list_target(char *tgt, boolean_t verbose, boolean_t script)
{
int ret;
it_config_t *cfg;
it_tgt_t *ptr;
boolean_t found = B_FALSE;
boolean_t first = B_TRUE;
boolean_t first_tag = B_TRUE;
char *gauth = "none";
char *galias = "-";
char *auth;
char *alias;
char *chapu;
char *chaps;
it_tpgt_t *tagp;
char *sec = "solaris.smf.read.stmf";
stmfDevid devid;
stmfSessionList *sess = NULL;
stmfTargetProperties props;
char *state;
int num_sessions;
ITADM_CHKAUTH(sec);
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
return (ret);
}
ptr = cfg->config_tgt_list;
/* grab global defaults for auth, alias */
if (cfg->config_global_properties) {
(void) nvlist_lookup_string(cfg->config_global_properties,
"alias", &galias);
(void) nvlist_lookup_string(cfg->config_global_properties,
"auth", &gauth);
}
for (; ptr != NULL; ptr = ptr->tgt_next) {
if (found) {
break;
}
if (tgt) {
/*
* We do a case-insensitive match in case
* a non-lower case value got stored.
*/
if (strcasecmp(tgt, ptr->tgt_name) != 0) {
continue;
} else {
found = B_TRUE;
}
}
state = "-";
num_sessions = 0;
sess = NULL;
/*
* make a best effort to retrieve target status and
* number of active sessions from STMF.
*/
ret = stmfDevidFromIscsiName(ptr->tgt_name, &devid);
if (ret == STMF_STATUS_SUCCESS) {
ret = stmfGetTargetProperties(&devid, &props);
if (ret == STMF_STATUS_SUCCESS) {
if (props.status == STMF_TARGET_PORT_ONLINE) {
state = "online";
} else {
state = "offline";
}
}
}
if (ret == STMF_STATUS_SUCCESS) {
ret = stmfGetSessionList(&devid, &sess);
if (ret == STMF_STATUS_SUCCESS) {
num_sessions = sess->cnt;
free(sess);
}
}
/* reset ret so we don't return an error */
ret = 0;
if (!script && first) {
(void) printf("%-61s%-9s%-9s\n", "TARGET NAME",
"STATE", "SESSIONS");
first = B_FALSE;
}
if (!script) {
/*
* try not to let columns run into each other.
* Stick a tab after too-long fields.
* Lengths chosen are for the 'common' cases.
*/
(void) printf("%-61s", ptr->tgt_name);
if (strlen(ptr->tgt_name) > 60) {
(void) printf("\t");
}
(void) printf("%-9s%-9d", state, num_sessions);
} else {
(void) printf("%s\t%s\t%d", ptr->tgt_name,
state, num_sessions);
}
if (!verbose) {
(void) printf("\n");
continue;
}
auth = gauth;
alias = galias;
chapu = "-";
chaps = "unset";
if (ptr->tgt_properties) {
(void) nvlist_lookup_string(ptr->tgt_properties,
"auth", &auth);
(void) nvlist_lookup_string(ptr->tgt_properties,
"alias", &alias);
if (nvlist_exists(ptr->tgt_properties,
"targetchapsecret")) {
chaps = "set";
}
(void) nvlist_lookup_string(ptr->tgt_properties,
"targetchapuser", &chapu);
}
if (!script) {
(void) printf("\n\t%-20s\t%s\n\t%-20s\t%s %s\n"
"\t%-20s\t%s\n\t%-20s\t%s\n\t%-20s\t",
"alias:", alias, "auth:", auth,
((auth == gauth) ? "(defaults)" : ""),
"targetchapuser:",
chapu, "targetchapsecret:", chaps, "tpg-tags:");
} else {
(void) printf("\t%s\t%s %s\t%s\t%s\t",
alias, auth,
((auth == gauth) ? "(defaults)" : ""),
chapu, chaps);
}
first_tag = B_TRUE;
tagp = ptr->tgt_tpgt_list;
for (; tagp != NULL; tagp = tagp->tpgt_next) {
if (!first_tag) {
(void) printf(",");
} else {
first_tag = B_FALSE;
}
(void) printf("%s = %d",
tagp->tpgt_tpg_name, tagp->tpgt_tag);
}
if (first_tag) {
/* didn't find any */
(void) printf("default");
}
(void) printf("\n");
}
if (tgt && (!found)) {
(void) fprintf(stderr,
gettext("Target %s not found!"), tgt);
(void) fprintf(stderr, "\n");
ret = 1;
}
it_config_free(cfg);
return (ret);
}
int
delete_target(char *tgt, boolean_t force)
{
int ret;
it_config_t *cfg;
it_tgt_t *ptr;
char *sec = "solaris.smf.modify.stmf";
ITADM_CHKAUTH(sec);
if (!tgt) {
(void) fprintf(stderr, "%s\n",
gettext("Error, no target specified"));
return (EINVAL);
}
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
return (ret);
}
ptr = cfg->config_tgt_list;
while (ptr) {
/*
* We do a case-insensitive match in case
* a non-lower case value got stored.
*/
if (strcasecmp(ptr->tgt_name, tgt) == 0) {
break;
}
ptr = ptr->tgt_next;
}
if (ptr) {
ret = it_tgt_delete(cfg, ptr, force);
if (ret != 0) {
if (ret == EBUSY) {
(void) fprintf(stderr,
gettext("The target is online or busy. "
"Use the -f (force) option, or "
"'stmfadm offline-target %s'"), tgt);
(void) fprintf(stderr, "\n");
} else {
output_config_error(ret, gettext(
"Error deleting target"));
}
}
if (ret == 0) {
ret = it_config_commit(cfg);
STMF_STALE(ret);
}
} else {
(void) fprintf(stderr,
gettext("Target %s not found"), tgt);
(void) fprintf(stderr, "\n");
ret = 1;
}
it_config_free(cfg);
return (ret);
}
static int
modify_target(char *tgt, char *newname, nvlist_t *proplist)
{
int ret;
it_config_t *cfg = NULL;
it_tgt_t *ptr = NULL;
it_tgt_t *tgtp = NULL;
char **tags = NULL;
uint32_t count = 0;
nvlist_t *errlist = NULL;
int i;
it_tpg_t *tpg = NULL;
uint16_t tagid;
it_tpgt_t *tpgt = NULL;
char *sec = "solaris.smf.modify.stmf";
boolean_t did_it_config_load = B_FALSE;
ITADM_CHKAUTH(sec);
/* XXX: Do we need to offline anything here too? */
if (!tgt) {
(void) fprintf(stderr, "%s\n",
gettext("Error, no target specified"));
ret = EINVAL;
goto done;
}
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
goto done;
}
did_it_config_load = B_TRUE;
/*
* If newname is specified, ensure it is a valid name.
*/
if (newname) {
if (!validate_iscsi_name(newname)) {
(void) fprintf(stderr,
gettext("Invalid iSCSI name %s"), newname);
(void) fprintf(stderr, "\n");
ret = 1;
goto done;
}
}
/*
* Loop through to verify that the target to be modified truly
* exists. If this target is to be renamed, ensure the new
* name is not already in use.
*/
ptr = cfg->config_tgt_list;
while (ptr) {
/*
* Does a target with the new name already exist?
*/
if (newname &&
(strcasecmp(newname, ptr->tgt_name) == 0)) {
(void) fprintf(stderr,
gettext("A target with name %s already exists"),
newname);
(void) fprintf(stderr, "\n");
ret = 1;
goto done;
}
if (strcasecmp(ptr->tgt_name, tgt) == 0) {
tgtp = ptr;
}
ptr = ptr ->tgt_next;
}
if (!tgtp) {
(void) fprintf(stderr,
gettext("Target %s not found"), tgt);
(void) fprintf(stderr, "\n");
ret = EINVAL;
goto done;
}
/* set the target portal group tags */
ret = nvlist_lookup_string_array(proplist, "tpg-tag", &tags,
&count);
if (ret == ENOENT) {
/* none specified. is this ok? */
ret = 0;
} else if (ret != 0) {
output_config_error(ret, gettext("Internal error"));
goto done;
}
/* special case, remove all explicit TPGs, and don't add any */
if (tags && (count == 1) && (strcmp("default", tags[0]) == 0)) {
count = 0;
}
for (i = 0; i < count; i++) {
if (!tags || !tags[i]) {
continue;
}
/* see that all referenced groups are already defined */
tpg = cfg->config_tpg_list;
while (tpg != NULL) {
if (strcmp(tags[i], tpg->tpg_name) == 0) {
break;
}
tpg = tpg->tpg_next;
}
if (tpg == NULL) {
(void) fprintf(stderr,
gettext("Invalid tpg-name %s: not defined"),
tags[i]);
(void) fprintf(stderr, "\n");
ret = 1;
goto done;
}
}
/*
* don't recreate tags that are already associated,
* remove tags not requested.
*/
if (tags) {
tpgt = tgtp->tgt_tpgt_list;
while (tpgt) {
for (i = 0; i < count; i++) {
if (!tags[i]) {
continue;
}
if (strcmp(tpgt->tpgt_tpg_name, tags[i])
== 0) {
/* non-null tags will be created */
tags[i] = NULL;
break;
}
}
if (i == count) {
/* one to remove */
it_tpgt_t *ptr = tpgt;
tpgt = ptr->tpgt_next;
it_tpgt_delete(cfg, tgtp, ptr);
} else {
tpgt = tpgt->tpgt_next;
}
}
}
/* see if there are any left to add */
for (i = 0; i < count; i++) {
if (!tags || !tags[i]) {
continue;
}
/* generate the tag number to use */
tag_name_to_num(tags[i], &tagid);
ret = it_tpgt_create(cfg, tgtp, &tpgt, tags[i], tagid);
if (ret != 0) {
if (ret == E2BIG) {
(void) fprintf(stderr, "%s\n",
gettext("Error, no portal tag available"));
} else {
(void) fprintf(stderr, gettext(
"Could not add target portal group"
" tag %s: "), tags[i]);
output_config_error(ret, NULL);
}
goto done;
}
}
/* remove the tags from the proplist before continuing */
(void) nvlist_remove_all(proplist, "tpg-tag");
/*
* Rename this target, if requested. Save the old name in
* the property list, so the kernel knows this is a renamed
* target, and not a new one.
*/
if (newname && (strlen(newname) > 0)) {
ret = nvlist_add_string(proplist, "oldtargetname",
tgtp->tgt_name);
if (ret != 0) {
output_config_error(ret,
gettext("Error renaming target"));
goto done;
}
(void) strlcpy(tgtp->tgt_name, newname,
sizeof (tgtp->tgt_name));
}
ret = it_tgt_setprop(cfg, tgtp, proplist, &errlist);
if (ret != 0) {
(void) fprintf(stderr,
gettext("Error setting target properties: %d"), ret);
(void) fprintf(stderr, "\n");
if (errlist) {
nvpair_t *nvp = NULL;
char *nn;
char *nv;
while ((nvp = nvlist_next_nvpair(errlist, nvp))
!= NULL) {
nv = NULL;
nn = nvpair_name(nvp);
(void) nvpair_value_string(nvp, &nv);
if (nv != NULL) {
(void) fprintf(stderr, "\t%s: %s\n",
nn, nv);
}
}
nvlist_free(errlist);
}
goto done;
}
if (ret == 0) {
ret = it_config_commit(cfg);
STMF_STALE(ret);
}
done:
if (ret == 0) {
(void) printf(gettext("Target %s successfully modified"),
tgtp->tgt_name);
(void) printf("\n");
}
if (did_it_config_load)
it_config_free(cfg);
return (ret);
}
int
create_tpg(char *tpg, int addrc, char **addrs)
{
int ret;
it_config_t *cfg;
it_tpg_t *tpgp;
int count = 0;
it_portal_t *ptl;
char *sec = "solaris.smf.modify.stmf";
int i = 0;
ITADM_CHKAUTH(sec);
if (!tpg) {
(void) fprintf(stderr, "%s\n",
gettext("Error, no target portal group specified"));
return (EINVAL);
}
if (strlen(tpg) > (MAX_TPG_NAMELEN - 1)) {
(void) fprintf(stderr,
gettext("Target Portal Group name must be no longer "
"than %d characters"), (MAX_TPG_NAMELEN - 1));
(void) fprintf(stderr, "\n");
return (EINVAL);
}
if (!addrs || (addrc <= 0)) {
(void) fprintf(stderr, "%s\n",
gettext("Error, no portal addresses specified"));
return (EINVAL);
}
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
return (ret);
}
tpgp = cfg->config_tpg_list;
while (tpgp != NULL) {
if (strcmp(tpgp->tpg_name, tpg) == 0) {
(void) fprintf(stderr,
gettext("Target Portal Group %s already exists"),
tpg);
(void) fprintf(stderr, "\n");
it_config_free(cfg);
return (1);
}
tpgp = tpgp->tpg_next;
}
/*
* Ensure that the addrs don't contain commas.
*/
for (i = 0; i < addrc; i++) {
if (strchr(addrs[i], ',')) {
(void) fprintf(stderr,
gettext("Bad portal name %s"),
addrs[i]);
(void) fprintf(stderr, "\n");
it_config_free(cfg);
return (EINVAL);
}
}
/*
* Create the portal group and first portal
*/
ret = it_tpg_create(cfg, &tpgp, tpg, addrs[count]);
if (ret != 0) {
if (ret == EEXIST) {
(void) fprintf(stderr,
gettext("Portal %s already in use"),
addrs[count]);
(void) fprintf(stderr, "\n");
} else {
output_config_error(ret, gettext("Could not create the "
"target portal group"));
}
it_config_free(cfg);
return (ret);
}
/*
* Add the remaining portals
*/
for (count = 1; count < addrc; count++) {
if (!addrs[count]) {
continue;
}
ret = it_portal_create(cfg, tpgp, &ptl, addrs[count]);
if (ret != 0) {
if (ret == EEXIST) {
(void) fprintf(stderr,
gettext("Portal %s already in use"),
addrs[count]);
(void) fprintf(stderr, "\n");
} else {
(void) fprintf(stderr,
gettext("Error adding portal %s: "),
addrs[count]);
output_config_error(ret, NULL);
break;
}
}
}
if (ret == 0) {
ret = it_config_commit(cfg);
STMF_STALE(ret);
}
it_config_free(cfg);
return (ret);
}
static int
list_tpg(char *tpg, boolean_t verbose, boolean_t script)
{
int ret;
it_config_t *cfg;
it_tpg_t *ptr;
boolean_t found = B_FALSE;
it_portal_t *portal;
boolean_t first = B_TRUE;
boolean_t first_portal;
char *pstr;
char *sec = "solaris.smf.read.stmf";
ITADM_CHKAUTH(sec);
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
return (ret);
}
ptr = cfg->config_tpg_list;
for (; ptr != NULL; ptr = ptr->tpg_next) {
if (found) {
break;
}
if (tpg) {
if (strcmp(tpg, ptr->tpg_name) != 0) {
continue;
} else {
found = B_TRUE;
}
}
if (!script && first) {
(void) printf("%-30s%-9s\n", "TARGET PORTAL GROUP",
"PORTAL COUNT");
first = B_FALSE;
}
if (!script) {
(void) printf("%-30s", ptr->tpg_name);
if (strlen(ptr->tpg_name) > 30) {
(void) printf("\t");
}
(void) printf("%-9d", ptr->tpg_portal_count);
} else {
(void) printf("%s\t%d", ptr->tpg_name,
ptr->tpg_portal_count);
}
if (!verbose) {
(void) printf("\n");
continue;
}
if (!script) {
(void) printf("\n portals:");
}
first_portal = B_TRUE;
portal = ptr->tpg_portal_list;
for (; portal != NULL; portal = portal->portal_next) {
ret = sockaddr_to_str(&(portal->portal_addr), &pstr);
if (ret != 0) {
/* invalid addr? */
continue;
}
if (!first_portal) {
(void) printf(",");
} else {
(void) printf("\t");
first_portal = B_FALSE;
}
(void) printf("%s", pstr);
free(pstr);
}
if (first_portal) {
/* none found */
(void) printf("\t<none>");
}
(void) printf("\n");
}
if (tpg && (!found)) {
(void) fprintf(stderr,
gettext("Target Portal Group %s not found!\n"), tpg);
(void) fprintf(stderr, "\n");
ret = 1;
}
it_config_free(cfg);
return (ret);
}
static int
delete_tpg(char *tpg, boolean_t force)
{
int ret;
it_config_t *cfg;
it_tpg_t *ptpg = NULL;
char *sec = "solaris.smf.modify.stmf";
ITADM_CHKAUTH(sec);
if (!tpg) {
(void) fprintf(stderr, "%s\n",
gettext("Error, no target portal group specified"));
return (EINVAL);
}
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
return (ret);
}
ptpg = cfg->config_tpg_list;
for (; ptpg != NULL; ptpg = ptpg->tpg_next) {
if (strcmp(tpg, ptpg->tpg_name) == 0) {
break;
}
}
if (!ptpg) {
(void) fprintf(stderr,
gettext("Target portal group %s does not exist"),
tpg);
(void) fprintf(stderr, "\n");
ret = 1;
} else {
ret = it_tpg_delete(cfg, ptpg, force);
if (ret == EBUSY) {
(void) fprintf(stderr, "%s\n",
gettext(
"Target portal group associated with one or more "
"targets. Cannot delete."));
} else if (ret != 0) {
output_config_error(ret, gettext("Could not delete "
"target portal group"));
}
if (ret == 0) {
ret = it_config_commit(cfg);
STMF_STALE(ret);
}
}
it_config_free(cfg);
return (ret);
}
static int
modify_initiator(char *ini, nvlist_t *proplist, boolean_t create)
{
int ret;
it_config_t *cfg;
it_ini_t *inip;
nvlist_t *errlist = NULL;
nvpair_t *nvp = NULL;
char *sec = "solaris.smf.modify.stmf";
boolean_t changed = B_TRUE;
ITADM_CHKAUTH(sec);
if (!ini) {
(void) fprintf(stderr, "%s\n",
gettext("Error, no initiator specified"));
return (EINVAL);
} else if (create) {
/*
* validate input name - what are the rules for EUI
* and IQN values?
*/
if (!IS_IQN_NAME(ini) && !IS_EUI_NAME(ini)) {
(void) fprintf(stderr, gettext("Invalid name %s"),
ini);
(void) fprintf(stderr, "\n");
return (EINVAL);
}
}
/*
* See if any properties were actually specified.
*/
if (proplist) {
nvp = nvlist_next_nvpair(proplist, nvp);
}
if ((nvp == NULL) && !create) {
changed = B_FALSE;
}
/*
* If no properties, and this is really a modify op, verify
* that the requested initiator exists, but then don't do anything.
* Modifying non-existent is an error; doing nothing to a defined
* initiator is not.
*/
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
return (ret);
}
inip = cfg->config_ini_list;
while (inip) {
if (strcasecmp(inip->ini_name, ini) == 0) {
break;
}
inip = inip->ini_next;
}
if (create) {
if (inip) {
(void) fprintf(stderr,
gettext("Initiator %s already exists"),
inip->ini_name);
(void) fprintf(stderr, "\n");
ret = EINVAL;
} else {
ret = it_ini_create(cfg, &inip, ini);
if (ret != 0) {
if (ret == EFAULT) {
(void) fprintf(stderr,
gettext("Invalid iSCSI name %s"),
ini);
(void) fprintf(stderr, "\n");
} else {
output_config_error(ret, gettext(
"Error creating initiator"));
}
}
}
} else if (!inip) {
ret = ENOENT;
(void) fprintf(stderr,
gettext("Error, initiator %s not found"),
ini);
(void) fprintf(stderr, "\n");
}
if ((ret == 0) && nvp) {
ret = it_ini_setprop(inip, proplist, &errlist);
if (ret != 0) {
(void) fprintf(stderr,
gettext("Error setting initiator properties: %d"),
ret);
(void) fprintf(stderr, "\n");
if (errlist) {
nvpair_t *nvp = NULL;
char *nn;
char *nv;
while ((nvp = nvlist_next_nvpair(errlist, nvp))
!= NULL) {
nv = NULL;
nn = nvpair_name(nvp);
(void) nvpair_value_string(nvp, &nv);
if (nv != NULL) {
(void) fprintf(stderr,
"\t%s: %s\n", nn, nv);
}
}
nvlist_free(errlist);
}
}
}
if ((ret == 0) && changed) {
ret = it_config_commit(cfg);
STMF_STALE(ret);
}
it_config_free(cfg);
return (ret);
}
static int
list_initiator(char *ini, boolean_t verbose, boolean_t script) /* ARGSUSED */
{
int ret;
it_config_t *cfg;
it_ini_t *ptr;
boolean_t found = B_FALSE;
boolean_t first = B_TRUE;
char *isecret;
char *iuser;
char *sec = "solaris.smf.read.stmf";
ITADM_CHKAUTH(sec);
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
return (ret);
}
ptr = cfg->config_ini_list;
for (; ptr != NULL; ptr = ptr->ini_next) {
isecret = "unset";
iuser = "<none>";
if (found) {
break;
}
if (ini) {
if (strcasecmp(ini, ptr->ini_name) != 0) {
continue;
} else {
found = B_TRUE;
}
}
if (ptr->ini_properties) {
if (nvlist_exists(ptr->ini_properties, "chapsecret")) {
isecret = "set";
}
(void) nvlist_lookup_string(ptr->ini_properties,
"chapuser", &iuser);
}
/* there's nothing to print for verbose yet */
if (!script && first) {
(void) printf("%-61s%-10s%-7s\n", "INITIATOR NAME",
"CHAPUSER", "SECRET");
first = B_FALSE;
}
if (!script) {
/*
* try not to let columns run into each other.
* Stick a tab after too-long fields.
* Lengths chosen are for the 'common' cases.
*/
(void) printf("%-61s", ptr->ini_name);
if (strlen(ptr->ini_name) > 60) {
(void) printf("\t");
}
(void) printf("%-15s", iuser);
if (strlen(iuser) >= 15) {
(void) printf("\t");
}
(void) printf("%-4s", isecret);
} else {
(void) printf("%s\t%s\t%s", ptr->ini_name,
iuser, isecret);
}
(void) printf("\n");
}
if (ini && (!found)) {
(void) fprintf(stderr,
gettext("Initiator %s not found!"), ini);
(void) fprintf(stderr, "\n");
ret = 1;
}
it_config_free(cfg);
return (ret);
}
int
delete_initiator(char *ini)
{
int ret;
it_config_t *cfg;
it_ini_t *ptr;
char *sec = "solaris.smf.modify.stmf";
ITADM_CHKAUTH(sec);
if (!ini) {
(void) fprintf(stderr, "%s\n",
gettext("Error, no initiator specified"));
return (EINVAL);
}
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
return (ret);
}
ptr = cfg->config_ini_list;
while (ptr) {
if (strcasecmp(ptr->ini_name, ini) == 0) {
break;
}
ptr = ptr->ini_next;
}
if (ptr) {
it_ini_delete(cfg, ptr);
ret = it_config_commit(cfg);
STMF_STALE(ret);
} else {
(void) fprintf(stderr,
gettext("Initiator %s not found"), ini);
(void) fprintf(stderr, "\n");
ret = 1;
}
return (ret);
}
static int
modify_defaults(nvlist_t *proplist)
{
int ret;
it_config_t *cfg;
nvlist_t *errlist = NULL;
nvpair_t *nvp = NULL;
char *sec = "solaris.smf.modify.stmf";
ITADM_CHKAUTH(sec);
if (proplist) {
/* make sure at least one property is specified */
nvp = nvlist_next_nvpair(proplist, nvp);
}
if (nvp == NULL) {
/* empty list */
(void) fprintf(stderr, "%s\n",
gettext("Error, no properties specified"));
return (EINVAL);
}
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
return (ret);
}
ret = it_config_setprop(cfg, proplist, &errlist);
if (ret != 0) {
(void) fprintf(stderr,
gettext("Error setting global properties: %d"),
ret);
(void) fprintf(stderr, "\n");
if (errlist) {
nvpair_t *nvp = NULL;
char *nn;
char *nv;
while ((nvp = nvlist_next_nvpair(errlist, nvp))
!= NULL) {
nv = NULL;
nn = nvpair_name(nvp);
(void) nvpair_value_string(nvp, &nv);
if (nv != NULL) {
(void) fprintf(stderr, "\t%s: %s\n",
nn, nv);
}
}
nvlist_free(errlist);
}
}
if (ret == 0) {
ret = it_config_commit(cfg);
STMF_STALE(ret);
}
it_config_free(cfg);
return (ret);
}
static int
list_defaults(boolean_t script)
{
int ret;
it_config_t *cfg;
nvlist_t *nvl;
char *alias = "<none>";
char *auth = "<none>";
char *isns = "disabled";
char **isvrs = NULL;
uint32_t scount = 0;
char *rsvr = "<none>";
char *rsecret = "unset";
boolean_t val = B_FALSE;
int i;
char *sec = "solaris.smf.read.stmf";
ITADM_CHKAUTH(sec);
ret = it_config_load(&cfg);
if (ret != 0) {
output_config_error(ret,
gettext("Error retrieving iSCSI target configuration"));
return (ret);
}
nvl = cfg->config_global_properties;
/* look up all possible options */
(void) nvlist_lookup_string(nvl, "alias", &alias);
(void) nvlist_lookup_string(nvl, "auth", &auth);
(void) nvlist_lookup_boolean_value(nvl, "isns", &val);
if (val == B_TRUE) {
isns = "enabled";
}
(void) nvlist_lookup_string_array(nvl, "isnsserver", &isvrs,
&scount);
(void) nvlist_lookup_string(nvl, "radiusserver", &rsvr);
if (nvlist_exists(nvl, "radiussecret")) {
rsecret = "set";
}
if (!script) {
(void) printf("%s:\n\n",
gettext("iSCSI Target Default Properties"));
}
if (script) {
(void) printf("%s\t%s\t%s\t%s\t%s\t",
alias, auth, rsvr, rsecret, isns);
} else {
(void) printf("%-15s\t%s\n%-15s\t%s\n%-15s\t%s\n%-15s\t%s\n"
"%-15s\t%s\n%-15s\t",
"alias:", alias, "auth:", auth, "radiusserver:",
rsvr, "radiussecret:", rsecret, "isns:", isns,
"isnsserver:");
}
for (i = 0; i < scount; i++) {
if (!isvrs || !isvrs[i]) {
break;
}
if (i > 0) {
(void) printf(",");
}
(void) printf("%s", isvrs[i]);
}
if (i == 0) {
(void) printf("%s", "<none>");
}
(void) printf("\n");
it_config_free(cfg);
return (0);
}
static int
itadm_get_password(nvlist_t *nvl, char *key, char *passfile,
char *phrase)
{
int ret = 0;
char *pass;
char buf[1024];
int fd;
struct stat64 sbuf;
size_t rd;
if (!nvl || !key) {
return (EINVAL);
}
if (passfile) {
ret = stat64(passfile, &sbuf);
if ((ret != 0) || (!S_ISREG(sbuf.st_mode))) {
(void) fprintf(stderr,
gettext("Invalid secret file %s"),
passfile);
(void) fprintf(stderr, "\n");
return (EBADF);
}
fd = open64(passfile, O_RDONLY);
if (fd == -1) {
ret = errno;
(void) fprintf(stderr,
gettext("Could not open secret file %s: "),
passfile);
output_config_error(ret, NULL);
return (ret);
}
rd = read(fd, buf, sbuf.st_size);
(void) close(fd);
if (rd != sbuf.st_size) {
ret = EIO;
(void) fprintf(stderr,
gettext("Could not read secret file %s: "),
passfile);
output_config_error(ret, NULL);
return (ret);
}
/* ensure buf is properly terminated */
buf[rd] = '\0';
/* if last char is a newline, strip it off */
if (buf[rd - 1] == '\n') {
buf[rd - 1] = '\0';
}
/* validate length */
if ((strlen(buf) > 255) || (strlen(buf) < 12)) {
(void) fprintf(stderr, "%s\n",
gettext(
"Secret must be between 12 and 255 characters"));
return (EINVAL);
}
} else {
/* prompt for secret */
if (!phrase) {
return (EINVAL);
}
pass = getpassphrase(phrase);
if (!pass) {
ret = errno;
output_config_error(ret,
gettext("Could not read secret"));
return (ret);
}
/* validate length */
if ((strlen(pass) > 255) || (strlen(pass) < 12)) {
(void) fprintf(stderr, "%s\n",
gettext(
"Secret must be between 12 and 255 characters"));
return (EINVAL);
}
(void) strlcpy(buf, pass, sizeof (buf));
/* confirm entered secret */
pass = getpassphrase(gettext("Re-enter secret: "));
if (!pass) {
ret = errno;
output_config_error(ret,
gettext("Could not read secret"));
return (ret);
}
if (strcmp(buf, pass) != 0) {
ret = EINVAL;
(void) fprintf(stderr, "%s\n",
gettext("Secret validation failed"));
return (ret);
}
}
ret = nvlist_add_string(nvl, key, buf);
return (ret);
}
static int
itadm_opt_to_arr(nvlist_t *nvl, char *key, char *opt, uint32_t *num)
{
int count;
char *bufp;
char **arr;
if (!opt || !key || !nvl) {
return (EINVAL);
}
bufp = opt;
count = 1;
for (;;) {
bufp = strchr(bufp, ',');
if (!bufp) {
break;
}
bufp++;
count++;
}
arr = calloc(count, sizeof (char *));
if (!arr) {
return (ENOMEM);
}
bufp = opt;
/* set delimiter to comma */
(void) bufsplit(",", 0, NULL);
/* split up that buf! */
(void) bufsplit(bufp, count, arr);
/* if requested, return the number of array members found */
if (num) {
*num = count;
}
return (nvlist_add_string_array(nvl, key, arr, count));
}
static void
tag_name_to_num(char *tagname, uint16_t *tagnum)
{
ulong_t id;
char *ptr = NULL;
if (!tagname || !tagnum) {
return;
}
*tagnum = 0;
id = strtoul(tagname, &ptr, 10);
/* Must be entirely numeric and in-range */
if (ptr && (*ptr != '\0')) {
return;
}
if ((id <= UINT16_MAX) && (id > 1)) {
*tagnum = (uint16_t)id;
}
}
/*
* Print error messages to stderr for errnos and expected stmf errors.
* This function should generally not be used for cases where the
* calling code can generate a more detailed error message based on
* the contextual knowledge of the meaning of specific errors.
*/
static void
output_config_error(int error, char *msg)
{
if (msg) {
(void) fprintf(stderr, "%s: ", msg);
}
if (error & STMF_STATUS_ERROR) {
switch (error) {
case STMF_ERROR_PERM:
(void) fprintf(stderr, "%s",
gettext("permission denied"));
break;
case STMF_ERROR_BUSY:
(void) fprintf(stderr, "%s",
gettext("resource busy"));
break;
case STMF_ERROR_NOMEM:
(void) fprintf(stderr, "%s",
gettext("out of memory"));
break;
case STMF_ERROR_SERVICE_NOT_FOUND:
(void) fprintf(stderr, "%s",
gettext("STMF service not found"));
break;
case STMF_ERROR_SERVICE_DATA_VERSION:
(void) fprintf(stderr, "%s",
gettext("STMF service version incorrect"));
break;
case STMF_ERROR_PROV_DATA_STALE:
(void) fprintf(stderr, "%s",
gettext("Configuration changed during processing. "
"Check the configuration, then retry this "
"command if appropriate."));
break;
default:
(void) fprintf(stderr, "%s", gettext("unknown error"));
break;
}
} else {
char buf[80] = "";
(void) strerror_r(error, buf, sizeof (buf));
(void) fprintf(stderr, "%s", buf);
}
(void) fprintf(stderr, "\n");
}
|