|
root / base / usr / src / cmd / allocate / allocate3.c
allocate3.c C 1869 lines 43.4 KB
   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
/*
 * 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) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <auth_attr.h>
#include <auth_list.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <libintl.h>
#include <locale.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <bsm/devices.h>
#include <sys/acl.h>
#include <tsol/label.h>
#include <syslog.h>
#include <limits.h>
#include <user_attr.h>
#include <secdb.h>
#include <sys/mkdev.h>
#include <sys/acl.h>
#include <sys/file.h>
#include <sys/procfs.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <utime.h>
#include <libgen.h>
#include <zone.h>
#include <nss_dbdefs.h>
#include <bsm/devalloc.h>
#include <libdevinfo.h>
#include "allocate.h"

extern void print_error(int, char *);

#if	defined(DEBUG) || defined(lint)
#define	dprintf(s, a) (void) fprintf(stderr, s, a)
#define	dperror(s) perror(s)
#else	/* !DEBUG */
#define	dprintf(s, a)	0
#define	dperror(s)	0
#endif	/* DEBUG */

#define	DEV_ERRORED(sbuf)	(((sbuf).st_mode & ~S_IFMT) == ALLOC_ERR_MODE)
#define	DEV_ALLOCATED(sbuf)	((sbuf).st_uid != DA_UID || \
			!(((sbuf).st_mode & ~S_IFMT) == DEALLOC_MODE || \
			DEV_ERRORED(sbuf)))

#define	ALLOC_CLEAN		"-A"
#define	DEALLOC_CLEAN		"-D"
#define	DAC_DIR			"/etc/security/dev"
#define	DEVICE_AUTH_SEPARATOR	","
#define	LOCALDEVICE		"/dev/console"
#define	PROCFS			"/proc/"
#define	SFF_NO_ERROR		0x1

#define	ALLOC_BY_NONE		-1
#define	CHECK_DRANGE		1
#define	CHECK_URANGE		2
#define	CHECK_ZLABEL		3

extern void audit_allocate_list(char *);
extern void audit_allocate_device(char *);

extern int	system_labeled;
extern char	*newenv[];

struct state_file {
	int	sf_flags;
	char	sf_path[MAXPATHLEN];
};

struct file_info {
	struct stat	fi_stat;
	char		*fi_message;
};

struct zone_path {
	int	count;
	char	**path;
};

struct dev_names {
	char **dnames;
};

static int _dev_file_name(struct state_file *, devmap_t *);
static int lock_dev(char *, struct stat *);
static int _check_label(devalloc_t *, char *, uid_t, int);
static int create_znode(char *, struct zone_path *, devmap_t *);
static int remove_znode(char *, devmap_t *);
static int update_device(char **, char *, int);

/*
 * checks if the invoking user is local to the device
 */
/*ARGSUSED*/
int
_is_local(uid_t uid)
{
	struct stat	statbuf;

	if (stat(LOCALDEVICE, &statbuf) == 0 &&
	    statbuf.st_uid == uid)
		return (1);

	return (0);
}

/*
 * Checks if the user with the specified uid has the specified authorization
 */
int
_is_authorized(char *auths, uid_t uid)
{
	char		*dcp, *authlist, *lasts;
	char		pw_buf[NSS_BUFLEN_PASSWD];
	struct passwd	pw_ent;

	/*
	 * first, the easy cases
	 */
	if (strcmp(auths, "@") == 0)
		return (1);
	if (strcmp(auths, "*") == 0)
		return (ALLOC_BY_NONE);
	if (getpwuid_r(uid, &pw_ent, pw_buf, sizeof (pw_buf)) == NULL)
		return (0);
	if (strpbrk(auths, DEVICE_AUTH_SEPARATOR) == NULL)
		return (chkauthattr(auths, pw_ent.pw_name));
	authlist = strdup(auths);
	if (authlist == NULL)
		return (0);
	for (dcp = authlist;
	    (dcp = strtok_r(dcp, DEVICE_AUTH_SEPARATOR, &lasts)) != NULL;
	    dcp = NULL) {
		if (chkauthattr(dcp, pw_ent.pw_name))
			break;
	}
	free(authlist);

	return (dcp != NULL);
}

/*
 * Checks if the specified user has authorization for the device
 */
int
_is_dev_authorized(devalloc_t *da, uid_t uid)
{
	int	ares;
	char	*auth_list, *dcp, *subauth = NULL;

	auth_list = da->da_devauth;
	if (auth_list == NULL)
		return (0);
	dcp = strpbrk(auth_list, KV_TOKEN_DELIMIT);
	if (dcp == NULL)
		return (_is_authorized(auth_list, uid));
	if (_is_local(uid)) {
		/* the local authorization is before the separator */
		ares = dcp - auth_list;
		subauth = malloc(ares + 1);
		if (subauth == NULL)
			return (0);
		(void) strlcpy(subauth, auth_list, (ares + 1));
		auth_list = subauth;
	} else
		auth_list = dcp + 1;
	ares = _is_authorized(auth_list, uid);
	if (subauth != NULL)
		free(subauth);

	return (ares);
}

int
check_devs(devmap_t *dm)
{
	int	status = 0;
	char	**file;

	if (dm->dmap_devarray == NULL)
		return (NODMAPERR);
	for (file = dm->dmap_devarray; *file != NULL; file++) {
		if ((status = access(*file, F_OK)) == -1) {
			dprintf("Unable to access file %s\n", *file);
			break;
		}
	}

	return (status);
}

int
print_da_defs(da_defs_t *da_defs)
{
	char	optbuf[BUFSIZ];
	char	*p = NULL;

	if (da_defs->devopts == NULL) {
		dprintf("No default attributes for %s\n", da_defs->devtype);
		return (DEFATTRSERR);
	}
	(void) printf("dev_type=%s\n", da_defs->devtype);
	if (_kva2str(da_defs->devopts, optbuf, sizeof (optbuf), KV_ASSIGN,
	    KV_TOKEN_DELIMIT) == 0) {
		if (p = rindex(optbuf, ':'))
			*p = '\0';
		(void) printf("\t%s\n", optbuf);
	}

	return (0);
}

void
print_dev_attrs(int optflag, devalloc_t *da, devmap_t *dm,
    struct file_info *fip)
{
	char	*p = NULL;
	char	optbuf[BUFSIZ];

	(void) printf("device=%s%s", dm->dmap_devname, KV_DELIMITER);
	(void) printf("type=%s%s", dm->dmap_devtype, KV_DELIMITER);
	(void) printf("auths=%s%s",
	    (da->da_devauth ? da->da_devauth : ""), KV_DELIMITER);
	(void) printf("clean=%s%s",
	    (da->da_devexec ? da->da_devexec : ""), KV_DELIMITER);
	if (da->da_devopts != NULL) {
		if (_kva2str(da->da_devopts, optbuf, sizeof (optbuf),
		    KV_ASSIGN, KV_TOKEN_DELIMIT) == 0) {
			if (p = rindex(optbuf, ':'))
				*p = '\0';
			(void) printf("%s", optbuf);
		}
	}
	(void) printf("%s", KV_DELIMITER);
	if (optflag & WINDOWING) {
		if ((fip->fi_message != NULL) &&
		    (strcmp(fip->fi_message, DAOPT_CLASS) == 0))
			(void) printf("owner=/FREE%s", KV_DELIMITER);
		else if (DEV_ERRORED(fip->fi_stat))
			(void) printf("owner=/ERROR%s", KV_DELIMITER);
		else if (!DEV_ALLOCATED(fip->fi_stat))
			(void) printf("owner=/FREE%s", KV_DELIMITER);
		else
			(void) printf("owner=%u%s", fip->fi_stat.st_uid,
			    KV_DELIMITER);
	}
	(void) printf("files=%s", dm->dmap_devlist);
	(void) printf("\n");
}

void
print_dev(devmap_t *dm)
{
	char	**file;

	(void) printf(gettext("device: %s "), dm->dmap_devname);
	(void) printf(gettext("type: %s "), dm->dmap_devtype);
	(void) printf(gettext("files:"));
	file = dm->dmap_devarray;
	if (file != NULL) {
		for (; *file != NULL; file++)
			(void) printf(" %s", *file);
	}
	(void) printf("\n");
}

/* ARGSUSED */
int
_list_device(int optflag, uid_t uid, devalloc_t *da, char *zonename)
{
	int			bytes = 0;
	int			error = 0;
	int			is_authorized = 0;
	char			*fname = NULL;
	char			file_name[MAXPATHLEN];
	devmap_t		*dm;
	struct file_info	fi;
	struct state_file	sf;

	fi.fi_message = NULL;
	setdmapent();
	if ((dm = getdmapnam(da->da_devname)) == NULL) {
		enddmapent();
		dprintf("Unable to find %s in the maps database\n",
		    da->da_devname);
		return (NODMAPERR);
	}
	enddmapent();

	if ((optflag & CLASS) &&
	    (!(optflag & (LISTALL | LISTFREE | LISTALLOC)))) {
		fi.fi_message = DAOPT_CLASS;
		if (optflag & LISTATTRS)
			print_dev_attrs(optflag, da, dm, &fi);
		else
			print_dev(dm);
		goto out;
	}

	if (system_labeled) {
		if ((error = _dev_file_name(&sf, dm)) != 0) {
			freedmapent(dm);
			dprintf("Unable to find %s device files\n",
			    da->da_devname);
			error = NODMAPERR;
			goto out;
		}
		fname = sf.sf_path;
	} else {
		bytes = snprintf(file_name, MAXPATHLEN, "%s/%s", DAC_DIR,
		    da->da_devname);
		if (bytes <= 0) {
			error = DEVNAMEERR;
			goto out;
		} else if (bytes >= MAXPATHLEN) {
			dprintf("device name %s is too long.\n",
			    da->da_devname);
			error = DEVLONGERR;
			goto out;
		}
		fname = file_name;
	}
	if (stat(fname, &fi.fi_stat) != 0) {
		dprintf("Unable to stat %s\n", fname);
		dperror("Error:");
		error = DACACCERR;
		goto out;
	}
	is_authorized = _is_dev_authorized(da, uid);
	if (optflag & LISTFREE) {	/* list_devices -n */
		/*
		 * list all free devices
		 */
		if (DEV_ALLOCATED(fi.fi_stat)) {
				error = PREALLOCERR;
				goto out;
		}
		if (system_labeled) {
			/*
			 * for this free device, check if -
			 * 1. user has authorization to allocate
			 * 2. the zone label is within the label range of the
			 *    device
			 */
			if (is_authorized == ALLOC_BY_NONE) {
				error = DAUTHERR;
				goto out;
			} else if (is_authorized == 0) {
				error = UAUTHERR;
				goto out;
			}
			if (_check_label(da, zonename, uid,
			    CHECK_DRANGE) != 0) {
				error = LABELRNGERR;
				goto out;
			}
		}
	} else if (optflag & LISTALLOC) {	/*  list_devices -u */
		/*
		 * list all allocated devices
		 */
		if (!DEV_ALLOCATED(fi.fi_stat)) {
			error = DEVNALLOCERR;
			goto out;
		}
		if (fi.fi_stat.st_uid != uid) {
			error = DEVSTATEERR;
			goto out;
		}
		if (system_labeled) {
			/*
			 * check if the zone label equals the label at which
			 * the device is allocated.
			 */
			if (_check_label(da, zonename, uid,
			    CHECK_ZLABEL) != 0) {
				error = LABELRNGERR;
				goto out;
			}
		}
	} else if (optflag & LISTALL) {		/* list_devices -l */
		/*
		 * list all devices - free and allocated - available
		 */
		if (DEV_ALLOCATED(fi.fi_stat)) {
			if (optflag & WINDOWING &&
			    (is_authorized == ALLOC_BY_NONE)) {
				/*
				 * don't complain if we're here for the GUI.
				 */
				error = 0;
			} else if (fi.fi_stat.st_uid != uid) {
				if (!(optflag & WINDOWING)) {
					error = ALLOCUERR;
					goto out;
				}
			}
			if (system_labeled && !(optflag & WINDOWING)) {
				/*
				 * if we're not displaying in the GUI,
				 * check if the zone label equals the label
				 * at which the device is allocated.
				 */
				if (_check_label(da, zonename, uid,
				    CHECK_ZLABEL) != 0) {
					error = LABELRNGERR;
					goto out;
				}
			}
		} else if (system_labeled && !(optflag & WINDOWING)) {
			/*
			 * if we're not displaying in the GUI,
			 * for this free device, check if -
			 * 1. user has authorization to allocate
			 * 2. the zone label is within the label range of the
			 *    device
			 */
			if (is_authorized == ALLOC_BY_NONE) {
				error = DAUTHERR;
				goto out;
			} else if (is_authorized == 0) {
				error = UAUTHERR;
				goto out;
			}
			if (_check_label(da, zonename, uid,
			    CHECK_DRANGE) != 0) {
				error = LABELRNGERR;
				goto out;
			}
		}
	}
	if (system_labeled && DEV_ERRORED(fi.fi_stat) && !(optflag & LISTALL)) {
		error = DEVSTATEERR;
		goto out;
	}
	if (check_devs(dm) == -1) {
		error = DSPMISSERR;
		goto out;
	}
	if (optflag & LISTATTRS)
		print_dev_attrs(optflag, da, dm, &fi);
	else
		print_dev(dm);

	error = 0;

out:
	freedmapent(dm);
	return (error);
}

/* ARGSUSED */
int
list_devices(int optflag, uid_t uid, char *device, char *zonename)
{
	int		error = 0;
	char		*class = NULL;
	da_defs_t	*da_defs;
	devalloc_t	*da;

	if (system_labeled && optflag & WINDOWING && !(optflag & LISTATTRS)) {
		/*
		 * Private interface for GUI.
		 */
		(void) puts(DA_DB_LOCK);
		return (0);
	}
	if (optflag & USERID) {
		/*
		 * we need device.revoke to list someone else's devices
		 */
		if (!_is_authorized(DEVICE_REVOKE_AUTH, getuid()))
			return (UAUTHERR);
	}
	if (system_labeled) {
		if (!(optflag & USERID) &&
		    !_is_authorized(DEFAULT_DEV_ALLOC_AUTH, uid))
			/*
			 * we need device.allocate to list our devices
			 */
			return (UAUTHERR);
		if (optflag & LISTDEFS) {
			/*
			 * list default attrs from devalloc_defaults
			 */
			setdadefent();
			if (device) {
				/*
				 * list default attrs for this device type
				 */
				da_defs = getdadeftype(device);
				if (da_defs == NULL) {
					enddadefent();
					dprintf("No default attributes for "
					    "%s\n", device);
					return (DEFATTRSERR);
				}
				error = print_da_defs(da_defs);
				freedadefent(da_defs);
			} else {
				/*
				 * list everything in devalloc_defaults
				 */
				while ((da_defs = getdadefent()) != NULL) {
					(void) print_da_defs(da_defs);
					freedadefent(da_defs);
				}
			}
			enddadefent();
			return (error);
		}
	}
	/*
	 * Lock the database to make sure no body writes to it while we are
	 * reading.
	 */
	(void) lock_dev(NULL, NULL);
	setdaent();
	if (device) {
		if (optflag & CLASS) {
			/*
			 * list all devices of this class.
			 */
			while ((da = getdaent()) != NULL) {
				class =	 kva_match(da->da_devopts, DAOPT_CLASS);
				if (class && (strcmp(class, device) == 0)) {
					(void) _list_device(optflag, uid, da,
					    zonename);
				}
				freedaent(da);
			}
		} else {
			/*
			 * list this device
			 */
			if ((da = getdanam(device)) == NULL) {
				enddaent();
				return (NODAERR);
			}
			error = _list_device(optflag, uid, da, zonename);
			freedaent(da);
		}
	} else {
		/*
		 * list all devices
		 */
		while ((da = getdaent()) != NULL) {
			(void) _list_device(optflag, uid, da, zonename);
			freedaent(da);
		}
	}
	enddaent();

	return (error);
}

/*
 * Set the DAC characteristics of the file.
 * This uses a fancy chmod() by setting a minimal ACL which sets the mode
 * and discards any existing ACL.
 */
int
_newdac(char *file, uid_t owner, gid_t group, o_mode_t mode)
{
	int	err = 0;

	if (mode == ALLOC_MODE) {
		if (chown(file, owner, group) == -1) {
			dperror("newdac: unable to chown");
			err = CHOWNERR;
		}
	} else do {
		if (chown(file, owner, group) == -1) {
			dperror("newdac: unable to chown");
			err = CHOWNERR;
		}
	} while (fdetach(file) == 0);

	if (err)
		return (err);

	if (strncmp(file, "/dev/", strlen("/dev/")) != 0) {
		/*
		 * This could be a SunRay device that is in /tmp.
		 */
		if (chmod(file, mode) == -1) {
			dperror("newdac: unable to chmod");
			err = SETACLERR;
		}
	} else {
		err = acl_strip(file, owner, group, (mode_t)mode);
	}

	if (err != 0) {
		dperror("newdac: unable to setacl");
		err = SETACLERR;
	}

	return (err);
}

/*
 * lock_dev -
 *	locks a section of DA_DB_LOCK.
 *	returns lock fd if successful, else -1 on error.
 */
static int
lock_dev(char *file, struct stat *statbuf)
{
	static int	lockfd = -1;
	int		ret;
	int		count = 0;
	int		retry = 10;
	off_t		size = 0;
	off_t		offset;
	char		*lockfile;

	if (system_labeled)
		lockfile = DA_DB_LOCK;
	else
		lockfile = file;

	if (statbuf) {
		offset = statbuf->st_rdev;
		dprintf("locking %s\n", file);
	} else {
		offset = 0;
		dprintf("locking %s\n", lockfile);
	}
	if ((lockfd == -1) &&
	    (lockfd = open(lockfile, O_RDWR | O_CREAT, 0600)) == -1) {
		dperror("lock_dev: cannot open lock file");
		return (-1);
	}
	if (system_labeled) {
		(void) _newdac(lockfile, DA_UID, DA_GID, 0600);
		if (lseek(lockfd, offset, SEEK_SET) == -1) {
			dperror("lock_dev: cannot position lock file");
			return (-1);
		}
		size = 1;
	}
	errno = 0;
	while (retry) {
		count++;
		ret = lockf(lockfd, F_TLOCK, size);
		if (ret == 0)
			return (lockfd);
		if ((errno != EACCES) && (errno != EAGAIN)) {
			dperror("lock_dev: cannot set lock");
			return (-1);
		}
		retry--;
		(void) sleep(count);
		errno = 0;
	}

	return (-1);
}

int
mk_alloc(devmap_t *list, uid_t uid, struct zone_path *zpath)
{
	int	i;
	int	error = 0;
	char	**file;
	gid_t	gid = getgid();
	mode_t	mode = ALLOC_MODE;

	file = list->dmap_devarray;
	if (file == NULL)
		return (NODMAPERR);
	for (; *file != NULL; file++) {
		dprintf("Allocating %s\n", *file);
		if ((error = _newdac(*file, uid, gid, mode)) != 0) {
			(void) _newdac(*file, ALLOC_ERRID, DA_GID,
			    ALLOC_ERR_MODE);
			break;
		}
	}
	if (system_labeled && zpath->count && (error == 0)) {
		/*
		 * mark as allocated any new device nodes that we
		 * created in local zone
		 */
		for (i = 0; i < zpath->count; i++) {
			dprintf("Allocating %s\n", zpath->path[i]);
			if ((error = _newdac(zpath->path[i], uid, gid,
			    mode)) != 0) {
				(void) _newdac(zpath->path[i], ALLOC_ERRID,
				    DA_GID, ALLOC_ERR_MODE);
				break;
			}
		}
	}

	return (error);
}

/*
 * mk_revoke() is used instead of system("/usr/sbin/fuser -k file")
 * because "/usr/sbin/fuser -k file" kills all processes
 * working with the file, even "vold" (bug #4095152).
 */
int
mk_revoke(int optflag, char *file)
{
	int		r = 0, p[2], fp, lock;
	int		fuserpid;
	char		buf[MAXPATHLEN];
	FILE		*ptr;
	pid_t		c_pid;
	prpsinfo_t	info;

	(void) strcpy(buf, PROCFS);
	/*
	 * vfork() and execl() just to make the same output
	 * as before fixing of bug #4095152.
	 * The problem is that the "fuser" command prints
	 * one part of output into stderr and another into stdout,
	 * but user sees them mixed. Of course, better to change "fuser"
	 * or to intercept and not to print its output.
	 */
	if (!(optflag & SILENT)) {
		c_pid = vfork();
		if (c_pid == -1)
			return (-1);
		if (c_pid == 0) {
			dprintf("first exec fuser %s\n", file);
			(void) execl("/usr/sbin/fuser", "fuser", file, NULL);
			dperror("first exec fuser");
			_exit(1);
		}

		(void) waitpid(c_pid, &lock, 0);
		dprintf("exit status %x\n", lock);
		if (WEXITSTATUS(lock) != 0)
			return (-1);
	}
	dprintf("first continuing c_pid=%d\n", (int)c_pid);
	if (pipe(p)) {
		dperror("pipe");
		return (-1);
	}
	/* vfork() and execl() to catch output and to process it */
	c_pid = vfork();
	if (c_pid == -1) {
		dperror("second vfork");
		return (-1);
	}
	dprintf("second continuing c_pid=%d\n", (int)c_pid);
	if (c_pid == 0) {
		(void) close(p[0]);
		(void) close(1);
		(void) fcntl(p[1], F_DUPFD, 1);
		(void) close(p[1]);
		(void) close(2);
		dprintf("second exec fuser %s\n", file);
		(void) execl("/usr/sbin/fuser", "fuser", file, NULL);
		dperror("second exec fuser");
		_exit(1);
	}
	(void) close(p[1]);
	if ((ptr = fdopen(p[0], "r")) != NULL) {
		while (!feof(ptr)) {
			if (fscanf(ptr, "%d", &fuserpid) > 0) {
				(void) sprintf(buf + strlen(PROCFS), "%d",
				    fuserpid);
				if ((fp = open(buf, O_RDONLY)) == -1) {
					dperror(buf);
					continue;
				}
				if (ioctl(fp, PIOCPSINFO,
				    (char *)&info) == -1) {
					dprintf("%d psinfo failed", fuserpid);
					dperror("");
					(void) close(fp);
					continue;
				}
				(void) close(fp);
				if (strcmp(info.pr_fname, "vold") == 0) {
					dprintf("%d matched vold name\n",
					    fuserpid);
					continue;
				}
				if (strcmp(info.pr_fname, "deallocate") == 0) {
					dprintf("%d matched deallocate name\n",
					    fuserpid);
					continue;
				}
				dprintf("killing %s", info.pr_fname);
				dprintf("(%d)\n", fuserpid);
				if ((r =
				    kill((pid_t)fuserpid, SIGKILL)) == -1) {
					dprintf("kill %d", fuserpid);
					dperror("");
					break;
				}
			}
		}
	} else {
		dperror("fdopen(p[0], r)");
		r = -1;
	}
	(void) fclose(ptr);

	return (r);
}

int
mk_unalloc(int optflag, devmap_t *list)
{
	int	error = 0;
	int	status;
	char	**file;

	audit_allocate_list(list->dmap_devlist);
	file = list->dmap_devarray;
	if (file == NULL)
		return (NODMAPERR);
	for (; *file != NULL; file++) {
		dprintf("Deallocating %s\n", *file);
		if (mk_revoke(optflag, *file) < 0) {
			dprintf("mk_unalloc: unable to revoke %s\n", *file);
			dperror("");
			error = CNTFRCERR;
		}
		status = _newdac(*file, DA_UID, DA_GID, DEALLOC_MODE);
		if (error == 0)
			error = status;

	}

	return (error);
}

int
mk_error(devmap_t *list)
{
	int	status = 0;
	char	**file;

	audit_allocate_list(list->dmap_devlist);
	file = list->dmap_devarray;
	if (file == NULL)
		return (NODMAPERR);
	for (; *file != NULL; file++) {
		dprintf("Putting %s in error state\n", *file);
		status = _newdac(*file, ALLOC_ERRID, DA_GID, ALLOC_ERR_MODE);
	}

	return (status);
}

int
exec_clean(int optflag, char *devname, char *path, uid_t uid, char *zonename,
    char *clean_arg)
{
	int		c;
	int		status = 0, exit_status;
	char		*mode, *cmd, *wdwcmd, *zoneroot;
	char		*devzone = zonename;
	char		wdwpath[PATH_MAX];
	char		zonepath[MAXPATHLEN];
	char		title[100];
	char		pw_buf[NSS_BUFLEN_PASSWD];
	struct passwd	pw_ent;

	zonepath[0] = '\0';
	if (system_labeled) {
		if ((zoneroot = getzonerootbyname(zonename)) == NULL) {
			if (strcmp(clean_arg, ALLOC_CLEAN) == 0) {
				return (-1);
			} else if (optflag & FORCE) {
				(void) strcpy(zonepath, "/");
				devzone = GLOBAL_ZONENAME;
			} else {
				dprintf("unable to get label for %s zone\n",
				    zonename);
				return (-1);
			}
		} else {
			(void) strcpy(zonepath, zoneroot);
			free(zoneroot);
		}
	}
	if (getpwuid_r(uid, &pw_ent, pw_buf, sizeof (pw_buf)) == NULL)
		return (-1);
	if (optflag & FORCE_ALL)
		mode = "-I";
	else if (optflag & FORCE)
		mode = "-f";
	else
		mode = "-s";
	if (path == NULL)
		return (0);
	if ((cmd = strrchr(path, '/')) == NULL)
		cmd = path;
	else
		cmd++;	/* skip leading '/' */
	c = vfork();
	switch (c) {
	case -1:
		return (-1);
	case 0:
		(void) setuid(0);
		if (system_labeled && (optflag & WINDOWING)) {
			/* First try .windowing version of script */
			(void) strncpy(wdwpath, path, PATH_MAX);
			(void) strncat(wdwpath, ".windowing", PATH_MAX);
			if ((wdwcmd = strrchr(wdwpath, '/')) == NULL)
				wdwcmd = wdwpath;
			(void) execl(wdwpath, wdwcmd, mode, devname, clean_arg,
			    pw_ent.pw_name, devzone, zonepath, NULL);
			/* If that failed, run regular version via dtterm */
			(void) snprintf(title, sizeof (title),
			    "Device %s for %s",
			    strcmp(clean_arg, ALLOC_CLEAN) == 0 ?
			    "allocation" : "deallocation", devname);
			(void) execl("/usr/dt/bin/dtterm", "dtterm",
			    "-title", title, "-geometry", "x10+100+400",
			    "-e", "/etc/security/lib/wdwwrapper",
			    path, mode, devname, clean_arg, pw_ent.pw_name,
			    devzone, zonepath, NULL);
			/*
			 * And if that failed, continue on to try
			 * running regular version directly.
			 */
		}
		dprintf("clean script: %s, ", path);
		dprintf("cmd=%s, ", cmd);
		dprintf("mode=%s, ", mode);
		if (system_labeled) {
			dprintf("devname=%s ", devname);
			dprintf("zonename=%s ", devzone);
			dprintf("zonepath=%s ", zonepath);
			dprintf("username=%s\n", pw_ent.pw_name);
			(void) execl(path, cmd, mode, devname, clean_arg,
			    pw_ent.pw_name, devzone, zonepath, NULL);
		} else {
			dprintf("devname=%s\n", devname);
			(void) execle(path, cmd, mode, devname, NULL, newenv);
		}
		dprintf("Unable to execute clean up script %s\n", path);
		dperror("");
		exit(CNTDEXECERR);
	default:
		(void) waitpid(c, &status, 0);
		dprintf("Child %d", c);
		if (WIFEXITED(status)) {
			exit_status = WEXITSTATUS(status);
			dprintf(" exited, status: %d\n", exit_status);
			return (exit_status);
		} else if (WIFSIGNALED(status)) {
			dprintf(" killed, signal %d\n", WTERMSIG(status));
		} else {
			dprintf(": exit status %d\n", status);
		}
		return (-1);
	}
}

int
_deallocate_dev(int optflag, devalloc_t *da, devmap_t *dm_in, uid_t uid,
    char *zonename, int *lock_fd)
{
	int			bytes = 0;
	int			error = 0;
	int			is_authorized = 0;
	uid_t			nuid;
	char			*fname = NULL;
	char			file_name[MAXPATHLEN];
	char			*devzone = NULL;
	devmap_t		*dm = NULL, *dm_new = NULL;
	struct stat		stat_buf;
	struct state_file	sf;

	if (dm_in == NULL) {
		setdmapent();
		if ((dm_new = getdmapnam(da->da_devname)) == NULL) {
			enddmapent();
			dprintf("Unable to find %s in device map database\n",
			    da->da_devname);
			return (NODMAPERR);
		}
		enddmapent();
		dm = dm_new;
	} else {
		dm = dm_in;
	}
	if (system_labeled) {
		if (_dev_file_name(&sf, dm) != 0) {
			if (dm_new)
				freedmapent(dm_new);
			dprintf("Unable to find %s device files\n",
			    da->da_devname);
			error = NODMAPERR;
			goto out;
		}
		fname = sf.sf_path;
	} else {
		bytes = snprintf(file_name,  MAXPATHLEN, "%s/%s", DAC_DIR,
		    da->da_devname);
		if (bytes <= 0) {
			error = DEVNAMEERR;
			goto out;
		} else if (bytes >= MAXPATHLEN) {
			dprintf("device name %s is too long.\n",
			    da->da_devname);
			error = DEVLONGERR;
			goto out;
		}
		fname = file_name;
	}

	audit_allocate_device(fname);

	if (stat(fname, &stat_buf) != 0) {
		dprintf("Unable to stat %s\n", fname);
		error = DACACCERR;
		goto out;
	}
	is_authorized = _is_dev_authorized(da, uid);
	if (is_authorized == ALLOC_BY_NONE) {
		dprintf("Not deallocating %s, not allocatable\n",
		    da->da_devname);
		goto out;
	}
	if (!(optflag & (FORCE | FORCE_ALL)) && !is_authorized) {
		dprintf("User %d is unauthorized to deallocate\n", (int)uid);
		error = UAUTHERR;
		goto out;
	}
	if (system_labeled) {
		/*
		 * unless we're here to deallocate by force, check if the
		 * label at which the device is currently allocated is
		 * within the user label range.
		 */
		if (!(optflag & FORCE) &&
		    _check_label(da, zonename, uid, CHECK_URANGE) != 0) {
			error = LABELRNGERR;
			goto out;
		}
	}
	if (!(optflag & FORCE) && stat_buf.st_uid != uid &&
	    DEV_ALLOCATED(stat_buf)) {
		error = ALLOCUERR;
		goto out;
	}
	if (!DEV_ALLOCATED(stat_buf)) {
		if (DEV_ERRORED(stat_buf)) {
			if (!(optflag & FORCE)) {
				error = DEVSTATEERR;
				goto out;
			}
		} else {
			error = DEVNALLOCERR;
			goto out;
		}
	}
	/* All checks passed, time to lock and deallocate */
	if ((*lock_fd = lock_dev(fname, &stat_buf)) == -1) {
		error = DEVLKERR;
		goto out;
	}
	if (system_labeled) {
		devzone = kva_match(da->da_devopts, DAOPT_ZONE);
		if (devzone == NULL) {
			devzone = GLOBAL_ZONENAME;
		} else if (strcmp(devzone, GLOBAL_ZONENAME) != 0) {
			if ((remove_znode(devzone, dm) != 0) &&
			    !(optflag & FORCE)) {
				error = ZONEERR;
				goto out;
			}
		}
	}
	if ((error = mk_unalloc(optflag, dm)) != 0) {
		if (!(optflag & FORCE))
			goto out;
	}
	if (system_labeled == 0) {
		if ((error = _newdac(fname, DA_UID, DA_GID,
		    DEALLOC_MODE)) != 0) {
			(void) _newdac(file_name, DA_UID, DA_GID,
			    ALLOC_ERR_MODE);
			goto out;
		}
	}
	/*
	 * if we are deallocating device owned by someone else,
	 * pass the owner's uid to the cleaning script.
	 */
	nuid = (stat_buf.st_uid == uid) ? uid : stat_buf.st_uid;
	error = exec_clean(optflag, da->da_devname, da->da_devexec, nuid,
	    devzone, DEALLOC_CLEAN);
	if (error != 0) {
		if (!(optflag & (FORCE | FORCE_ALL))) {
			error = CLEANERR;
			(void) mk_error(dm);
		} else {
			error = 0;
		}
	}

out:
	if (dm_new)
		freedmapent(dm_new);
	return (error);
}

int
_allocate_dev(int optflag, uid_t uid, devalloc_t *da, char *zonename,
    int *lock_fd)
{
	int			i;
	int			bytes = 0;
	int			error = 0;
	int			is_authorized = 0;
	int			dealloc_optflag = 0;
	char			*fname = NULL;
	char			file_name[MAXPATHLEN];
	devmap_t		*dm;
	struct stat		stat_buf;
	struct state_file	sf;
	struct zone_path	zpath;

	zpath.count = 0;
	zpath.path = NULL;
	setdmapent();
	if ((dm = getdmapnam(da->da_devname)) == NULL) {
		enddmapent();
		dprintf("Unable to find %s in device map database\n",
		    da->da_devname);
		return (NODMAPERR);
	}
	enddmapent();
	if (system_labeled) {
		if (_dev_file_name(&sf, dm) != 0) {
			freedmapent(dm);
			dprintf("Unable to find %s device files\n",
			    da->da_devname);
			error = NODMAPERR;
			goto out;
		}
		fname = sf.sf_path;
	} else {
		bytes = snprintf(file_name,  MAXPATHLEN, "%s/%s", DAC_DIR,
		    da->da_devname);
		if (bytes <= 0) {
			error = DEVNAMEERR;
			goto out;
		} else if (bytes >= MAXPATHLEN) {
			dprintf("device name %s is too long.\n",
			    da->da_devname);
			error = DEVLONGERR;
			goto out;
		}
		fname = file_name;
	}

	(void) audit_allocate_device(fname);

	if (stat(fname, &stat_buf) != 0) {
		dprintf("Unable to stat %s\n", fname);
		dperror("Error:");
		error = DACACCERR;
		goto out;
	}
	if (DEV_ERRORED(stat_buf)) {
		error = DEVSTATEERR;
		goto out;
	}
	is_authorized = _is_dev_authorized(da, uid);
	if (is_authorized == ALLOC_BY_NONE) {
		dprintf("Device %s is not allocatable\n", da->da_devname);
		error = UAUTHERR;
		goto out;
	} else if (!is_authorized && !(optflag & USERNAME)) {
		dprintf("User %d is unauthorized to allocate\n", (int)uid);
		error = UAUTHERR;
		goto out;
	}
	if (system_labeled) {
		/*
		 * check if label of the zone to which the device is being
		 * allocated is within the device label range.
		 */
		if (_check_label(da, zonename, uid, CHECK_DRANGE) != 0) {
			error = LABELRNGERR;
			goto out;
		}
	}
	if (check_devs(dm) == -1) {
		error = DSPMISSERR;
		goto out;
	}
	if (DEV_ALLOCATED(stat_buf)) {
		if (optflag & FORCE) {
			if (optflag & SILENT)
				dealloc_optflag = FORCE|SILENT;
			else
				dealloc_optflag = FORCE;
			if (_deallocate_dev(dealloc_optflag, da, dm, uid,
			    zonename, lock_fd)) {
				dprintf("Couldn't force deallocate device %s\n",
				    da->da_devname);
				error = CNTFRCERR;
				goto out;
			}
		} else if (stat_buf.st_uid == uid) {
			error = PREALLOCERR;
			goto out;
		} else {
			error = ALLOCUERR;
			goto out;
		}
	}
	/* All checks passed, time to lock and allocate */
	if ((*lock_fd = lock_dev(fname, &stat_buf)) == -1) {
		error = DEVLKERR;
		goto out;
	}
	if (system_labeled) {
		/*
		 * Run the cleaning program; it also mounts allocated
		 * device if required.
		 */
		error = exec_clean(optflag, da->da_devname, da->da_devexec, uid,
		    zonename, ALLOC_CLEAN);
		if (error != DEVCLEAN_OK) {
			switch (error) {
			case DEVCLEAN_ERROR:
			case DEVCLEAN_SYSERR:
				dprintf("allocate: "
				    "Error in device clean program %s\n",
				    da->da_devexec);
				error = CLEANERR;
				(void) mk_error(dm);
				goto out;
			case DEVCLEAN_BADMOUNT:
				dprintf("allocate: Failed to mount device %s\n",
				    da->da_devexec);
				goto out;
			case DEVCLEAN_MOUNTOK:
				break;
			default:
				error = 0;
				goto out;
			}
		}
		/*
		 * If not mounted, create zonelinks, if this is not the
		 * global zone.
		 */
		if ((strcmp(zonename, GLOBAL_ZONENAME) != 0) &&
		    (error != DEVCLEAN_MOUNTOK)) {
			if (create_znode(zonename, &zpath, dm) != 0) {
				error = ZONEERR;
				goto out;
			}
		}
	}

	(void) audit_allocate_list(dm->dmap_devlist);

	if ((error = mk_alloc(dm, uid, &zpath)) != 0) {
		(void) mk_unalloc(optflag, dm);
		goto out;
	}

	if (system_labeled == 0) {
		if ((error = _newdac(file_name, uid, getgid(),
		    ALLOC_MODE)) != 0) {
			(void) _newdac(file_name, DA_UID, DA_GID,
			    ALLOC_ERR_MODE);
			goto out;
		}
	}
	error = 0;
out:
	if (zpath.count) {
		for (i = 0; i < zpath.count; i++)
			free(zpath.path[i]);
		free(zpath.path);
	}
	freedmapent(dm);
	return (error);
}

void
_store_devnames(int *count, struct dev_names *dnms, char *zonename,
    devalloc_t *da, int flag)
{
	int i;

	dnms->dnames = (char **)realloc(dnms->dnames,
	    (*count + 1) * sizeof (char *));
	if (da) {
		dnms->dnames[*count] = strdup(da->da_devname);
		(*count)++;
	} else {
		dnms->dnames[*count] = NULL;
		if (flag == DA_ADD_ZONE)
			(void) update_device(dnms->dnames, zonename,
			    DA_ADD_ZONE);
		else if (flag == DA_REMOVE_ZONE)
			(void) update_device(dnms->dnames, NULL,
			    DA_REMOVE_ZONE);
		for (i = 0; i < *count; i++)
			free(dnms->dnames[i]);
		free(dnms->dnames);
	}
}

int
allocate(int optflag, uid_t uid, char *device, char *zonename)
{
	int		count = 0;
	int		error = 0;
	int		lock_fd = -1;
	devalloc_t	*da;
	struct dev_names dnms;

	if (optflag & (FORCE | USERID | USERNAME)) {
		if (!_is_authorized(DEVICE_REVOKE_AUTH, getuid()))
			return (UAUTHERR);
	}
	dnms.dnames = NULL;
	setdaent();
	if (optflag & TYPE) {
		/*
		 * allocate devices of this type
		 */
		while ((da = getdatype(device)) != NULL) {
			if (system_labeled &&
			    da_check_logindevperm(da->da_devname)) {
				freedaent(da);
				continue;
			}
			dprintf("trying to allocate %s\n", da->da_devname);
			error = _allocate_dev(optflag, uid, da, zonename,
			    &lock_fd);
			if (system_labeled && (error == 0)) {
				/*
				 * we need to record in device_allocate the
				 * label (zone name) at which this device is
				 * being allocated. store this device entry.
				 */
				_store_devnames(&count, &dnms, zonename, da, 0);
			}
			freedaent(da);
			error = 0;
		}
	} else {
		/*
		 * allocate this device
		 */
		if ((da = getdanam(device)) == NULL) {
			enddaent();
			return (NODAERR);
		}
		if (system_labeled && da_check_logindevperm(device)) {
			freedaent(da);
			return (LOGINDEVPERMERR);
		}
		dprintf("trying to allocate %s\n", da->da_devname);
		error = _allocate_dev(optflag, uid, da, zonename, &lock_fd);
		/*
		 * we need to record in device_allocate the label (zone name)
		 * at which this device is being allocated. store this device
		 * entry.
		 */
		if (system_labeled && (error == 0))
			_store_devnames(&count, &dnms, zonename, da, 0);
		freedaent(da);
		if (error == DEVCLEAN_BADMOUNT)
			error = 0;
	}
	enddaent();
	if (lock_fd != -1)
		(void) close(lock_fd);
	/*
	 * add to device_allocate labels (zone names) for the devices we
	 * allocated.
	 */
	if (dnms.dnames)
		_store_devnames(&count, &dnms, zonename, NULL, DA_ADD_ZONE);

	return (error);
}

/* ARGSUSED */
int
deallocate(int optflag, uid_t uid, char *device, char *zonename)
{
	int		count = 0;
	int		error = 0;
	int		lock_fd = -1;
	char		*class = NULL;
	devalloc_t	*da;
	struct dev_names dnms;

	if (optflag & (FORCE | FORCE_ALL)) {
		if (!_is_authorized(DEVICE_REVOKE_AUTH, getuid()))
		return (UAUTHERR);
	}
	if (optflag & FORCE_ALL)
		optflag |= FORCE;
	dnms.dnames = NULL;
	setdaent();
	if (optflag & FORCE_ALL) {
		/*
		 * deallocate all devices
		 */
		while ((da = getdaent()) != NULL) {
			if (system_labeled &&
			    da_check_logindevperm(da->da_devname)) {
				freedaent(da);
				continue;
			}
			dprintf("trying to deallocate %s\n", da->da_devname);
			error = _deallocate_dev(optflag, da, NULL, uid,
			    zonename, &lock_fd);
			if (system_labeled && (error == 0)) {
				/*
				 * we need to remove this device's allocation
				 * label (zone name) from device_allocate.
				 * store this device name.
				 */
				_store_devnames(&count, &dnms, zonename, da, 0);
			}
			freedaent(da);
			error = 0;
		}
	} else if (system_labeled && (optflag & TYPE)) {
		/*
		 * deallocate all devices of this type
		 */
		while ((da = getdatype(device)) != NULL) {
			if (da_check_logindevperm(da->da_devname)) {
				freedaent(da);
				continue;
			}
			dprintf("trying to deallocate %s\n", da->da_devname);
			error = _deallocate_dev(optflag, da, NULL, uid,
			    zonename, &lock_fd);
			if (error == 0) {
				/*
				 * we need to remove this device's allocation
				 * label (zone name) from device_allocate.
				 * store this device name.
				 */
				_store_devnames(&count, &dnms, zonename, da, 0);
			}
			freedaent(da);
			error = 0;
		}
	} else if (system_labeled && (optflag & CLASS)) {
		/*
		 * deallocate all devices of this class (for sunray)
		 */
		while ((da = getdaent()) != NULL) {
			class =  kva_match(da->da_devopts, DAOPT_CLASS);
			if (class && (strcmp(class, device) == 0)) {
				dprintf("trying to deallocate %s\n",
				    da->da_devname);
				error = _deallocate_dev(optflag, da, NULL, uid,
				    zonename, &lock_fd);
				if (error == 0) {
					/*
					 * we need to remove this device's
					 * allocation label (zone name) from
					 * device_allocate. store this device
					 * name.
					 */
					_store_devnames(&count, &dnms, zonename,
					    da, 0);
				}
				error = 0;
			}
			freedaent(da);
		}
	} else if (!(optflag & TYPE)) {
		/*
		 * deallocate this device
		 */
		if ((da = getdanam(device)) == NULL) {
			enddaent();
			return (NODAERR);
		}
		if (system_labeled && da_check_logindevperm(da->da_devname)) {
			freedaent(da);
			return (LOGINDEVPERMERR);
		}
		dprintf("trying to deallocate %s\n", da->da_devname);
		error = _deallocate_dev(optflag, da, NULL, uid, zonename,
		    &lock_fd);
		if (system_labeled && (error == 0)) {
			/*
			 * we need to remove this device's allocation label
			 * (zone name) from device_allocate. store this
			 * device name.
			 */
			_store_devnames(&count, &dnms, zonename, da, 0);
		}
		freedaent(da);
		if (error == DEVCLEAN_BADMOUNT)
			error = 0;
	}
	enddaent();
	if (lock_fd != -1)
		(void) close(lock_fd);
	/*
	 * remove from device_allocate labels (zone names) for the devices we
	 * deallocated.
	 */
	if (dnms.dnames)
		_store_devnames(&count, &dnms, zonename, NULL, DA_REMOVE_ZONE);

	return (error);
}

static int
_dev_file_name(struct state_file *sfp, devmap_t *dm)
{
	sfp->sf_flags = 0;
	/* if devlist is generated, never leave device in error state */
	if (dm->dmap_devlist[0] == '`')
		sfp->sf_flags |= SFF_NO_ERROR;
	if (dm->dmap_devarray == NULL ||
	    dm->dmap_devarray[0] == NULL)
		return (NODMAPERR);
	(void) strncpy(sfp->sf_path, dm->dmap_devarray[0],
	    sizeof (sfp->sf_path));
	sfp->sf_path[sizeof (sfp->sf_path) - 1] = '\0';
	if (sfp->sf_path[0] == '\0') {
		dprintf("dev_file_name: no device list for %s\n",
		    dm->dmap_devname);
		return (NODMAPERR);
	}

	return (0);
}

/*
 * _check_label -
 *	checks the device label range against zone label, which is also
 *	user's current label.
 *	returns 0 if in range, -1 for all other conditions.
 *
 */

static int
_check_label(devalloc_t *da, char *zonename, uid_t uid, int flag)
{
	int		err;
	int		in_range = 0;
	char		*alloczone, *lstr;
	char		pw_buf[NSS_BUFLEN_PASSWD];
	blrange_t	*range;
	m_label_t	*zlabel;
	struct passwd	pw_ent;

	if ((da == NULL) || (zonename == NULL))
		return (-1);

	if ((zlabel = getzonelabelbyname(zonename)) == NULL) {
		dprintf("unable to get label for %s zone\n", zonename);
		return (-1);
	}
	if (flag == CHECK_DRANGE) {
		blrange_t	drange;

		drange.lower_bound = blabel_alloc();
		lstr = kva_match(da->da_devopts, DAOPT_MINLABEL);
		if (lstr == NULL) {
			bsllow(drange.lower_bound);
		} else if (stobsl(lstr, drange.lower_bound, NO_CORRECTION,
		    &err) == 0) {
			dprintf("bad min_label for device %s\n",
			    da->da_devname);
			free(zlabel);
			blabel_free(drange.lower_bound);
			return (-1);
		}
		drange.upper_bound = blabel_alloc();
		lstr = kva_match(da->da_devopts, DAOPT_MAXLABEL);
		if (lstr == NULL) {
			bslhigh(drange.upper_bound);
		} else if (stobsl(lstr, drange.upper_bound, NO_CORRECTION,
		    &err) == 0) {
			dprintf("bad max_label for device %s\n",
			    da->da_devname);
			free(zlabel);
			blabel_free(drange.lower_bound);
			blabel_free(drange.upper_bound);
			return (-1);
		}
		if (blinrange(zlabel, &drange) == 0) {
			char	*zlbl = NULL, *min = NULL, *max = NULL;

			(void) bsltos(zlabel, &zlbl, 0, 0);
			(void) bsltos(drange.lower_bound, &min, 0, 0);
			(void) bsltos(drange.upper_bound, &max, 0, 0);
			dprintf("%s zone label ", zonename);
			dprintf("%s outside device label range: ", zlbl);
			dprintf("min - %s, ", min);
			dprintf("max - %s\n", max);
			free(zlabel);
			blabel_free(drange.lower_bound);
			blabel_free(drange.upper_bound);
			return (-1);
		}
	} else if (flag == CHECK_URANGE) {
		if (getpwuid_r(uid, &pw_ent, pw_buf, sizeof (pw_buf)) == NULL) {
			dprintf("Unable to get passwd entry for userid %d\n",
			    (int)uid);
			free(zlabel);
			return (-1);
		}
		if ((range = getuserrange(pw_ent.pw_name)) == NULL) {
			dprintf("Unable to get label range for userid %d\n",
			    (int)uid);
			free(zlabel);
			return (-1);
		}
		in_range = blinrange(zlabel, range);
		free(zlabel);
		blabel_free(range->lower_bound);
		blabel_free(range->upper_bound);
		free(range);
		if (in_range == 0) {
			dprintf("%s device label ", da->da_devname);
			dprintf("out of user %d label range\n", (int)uid);
			return (-1);
		}
	} else if (flag == CHECK_ZLABEL) {
		alloczone = kva_match(da->da_devopts, DAOPT_ZONE);
		if (alloczone == NULL) {
			free(zlabel);
			return (-1);
		}
		if (strcmp(zonename, alloczone) != 0) {
			dprintf("%s zone is different than ", zonename);
			dprintf("%s zone to which the device ", alloczone);
			dprintf("%s is allocated\n", da->da_devname);
			free(zlabel);
			return (-1);
		}
	}
	free(zlabel);

	return (0);
}

int
create_znode(char *zonename, struct zone_path *zpath, devmap_t *list)
{
	int		size;
	int		len = 0;
	int		fcount = 0;
	char		*p, *tmpfile, *zoneroot;
	char		**file;
	char		zonepath[MAXPATHLEN];
	di_prof_t	prof = NULL;

	file = list->dmap_devarray;
	if (file == NULL)
		return (NODMAPERR);
	if ((zoneroot = getzonerootbyname(zonename)) == NULL) {
		dprintf("unable to get label for %s zone\n", zonename);
		return (1);
	}
	(void) strcpy(zonepath, zoneroot);
	free(zoneroot);
	len = strlen(zonepath);
	size = sizeof (zonepath);
	(void) strlcat(zonepath, "/dev", size);
	if (di_prof_init(zonepath, &prof)) {
		dprintf("failed to initialize dev profile at %s\n", zonepath);
		return (1);
	}
	zonepath[len] = '\0';
	for (; *file != NULL; file++) {
		/*
		 * First time initialization
		 */
		tmpfile = strdup(*file);

		/*
		 * Most devices have pathnames starting in /dev
		 * but SunRay devices do not. In SRRS 3.1 they use /tmp.
		 *
		 * If the device pathname is not in /dev then create
		 * a symbolic link to it and put the device in /dev
		 */
		if (strncmp(tmpfile, "/dev/", strlen("/dev/")) != 0) {
			char	*linkdir;
			char	srclinkdir[MAXPATHLEN];
			char	dstlinkdir[MAXPATHLEN];

			linkdir = strchr(tmpfile + 1, '/');
			p = strchr(linkdir + 1, '/');
			*p = '\0';
			(void) strcpy(dstlinkdir, "/dev");
			(void) strncat(dstlinkdir, linkdir, MAXPATHLEN);
			(void) snprintf(srclinkdir, MAXPATHLEN, "%s/root%s",
			    zonepath, tmpfile);
			(void) symlink(dstlinkdir, srclinkdir);
			*p = '/';
			(void) strncat(dstlinkdir, p, MAXPATHLEN);
			free(tmpfile);
			tmpfile = strdup(dstlinkdir);
		}
		if (di_prof_add_dev(prof, tmpfile)) {
			dprintf("failed to add %s to profile\n", tmpfile);
			di_prof_fini(prof);
			return (1);
		}
		if (strlcat(zonepath, tmpfile, size) >= size) {
			dprintf("Buffer overflow in create_znode for %s\n",
			    *file);
			free(tmpfile);
			di_prof_fini(prof);
			return (1);
		}
		free(tmpfile);
		fcount++;
		if ((zpath->path = (char **)realloc(zpath->path,
		    (fcount * sizeof (char *)))) == NULL) {
			di_prof_fini(prof);
			return (1);
		}
		zpath->path[zpath->count] = strdup(zonepath);
		zpath->count = fcount;
		zonepath[len] = '\0';
	}

	if (di_prof_commit(prof))
		dprintf("failed to add devices to zone %s\n", zonename);
	di_prof_fini(prof);

	return (0);
}

int
remove_znode(char *zonename, devmap_t *dm)
{
	int		len = 0;
	char		*zoneroot;
	char		**file;
	char		zonepath[MAXPATHLEN];
	di_prof_t	prof = NULL;

	file = dm->dmap_devarray;
	if (file == NULL)
		return (NODMAPERR);
	if ((zoneroot = getzonerootbyname(zonename)) == NULL) {
		(void) snprintf(zonepath, MAXPATHLEN, "/zone/%s", zonename);
	} else {
		(void)  strcpy(zonepath, zoneroot);
		free(zoneroot);
	}
	/*
	 * To support SunRay we will just deal with the
	 * file in /dev, not the symlinks.
	 */
	(void) strncat(zonepath, "/dev", MAXPATHLEN);
	len = strlen(zonepath);
	if (di_prof_init(zonepath, &prof)) {
		dprintf("failed to initialize dev profile at %s\n", zonepath);
		return (1);
	}
	for (; *file != NULL; file++) {
		char *devrelpath;

		/*
		 * remove device node from zone.
		 *
		 * SunRay devices don't start with /dev
		 * so skip over first directory to make
		 * sure it is /dev. SunRay devices in zones
		 * will have a symlink into /dev but
		 * we don't ever delete it.
		 */
		devrelpath = strchr(*file + 1, '/');

		if (di_prof_add_exclude(prof, devrelpath + 1)) {
			dprintf("Failed exclude %s in dev profile\n", *file);
			di_prof_fini(prof);
			return (1);
		}
		zonepath[len] = '\0';
	}

	if (di_prof_commit(prof))
		dprintf("failed to remove devices from zone %s\n", zonename);
	di_prof_fini(prof);
	return (0);
}

int
update_device(char **devnames, char *zonename, int flag)
{
	int		len, rc;
	char		*optstr = NULL;
	da_args		dargs;
	devinfo_t	devinfo;

	dargs.optflag = flag;
	dargs.optflag |= DA_UPDATE|DA_ALLOC_ONLY;
	dargs.rootdir = NULL;
	dargs.devnames = devnames;
	devinfo.devname = devinfo.devtype = devinfo.devauths = devinfo.devexec =
	    devinfo.devlist = NULL;
	if (dargs.optflag & DA_ADD_ZONE) {
		len = strlen(DAOPT_ZONE) + strlen(zonename) + 3;
		if ((optstr = (char *)malloc(len)) == NULL)
			return (-1);
		(void) snprintf(optstr, len, "%s%s%s", DAOPT_ZONE, KV_ASSIGN,
		    zonename);
		devinfo.devopts = optstr;
	}
	dargs.devinfo = &devinfo;

	rc = da_update_device(&dargs);

	if (optstr)
		free(optstr);

	return (rc);
}