xref: /illumos-gate/usr/src/cmd/fs.d/smbclnt/lsacl/lsacl.c (revision 33efde4275d24731ef87927237b0ffb0630b6b2d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * This is the smbfs/lsacl command.
29  * (just for testing - not installed)
30  */
31 
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/stat.h>
35 #include <sys/acl.h>
36 #include <sys/acl_impl.h>
37 
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <aclutils.h>
44 
45 #include <netsmb/smbfs_acl.h>
46 
47 extern acl_t *acl_alloc(acl_type_t);
48 
49 char *progname;
50 int Vflag;
51 
52 uint32_t selector =  DACL_SECURITY_INFORMATION |
53 	OWNER_SECURITY_INFORMATION |
54 	GROUP_SECURITY_INFORMATION;
55 
56 void lsacl(char *);
57 
58 void
usage(void)59 usage(void)
60 {
61 	fprintf(stderr, "Usage: %s [-v] file ...\n", progname);
62 	exit(1);
63 }
64 
65 int
main(int argc,char ** argv)66 main(int argc, char **argv)
67 {
68 	int c;
69 
70 	progname = argv[0];
71 
72 	while ((c = getopt(argc, argv, "v")) != -1) {
73 		switch (c) {
74 		case 'v':
75 			Vflag++;
76 			break;
77 
78 		default:
79 			fprintf(stderr, "%s: bad option: %c\n",
80 			    progname, c);
81 			usage();
82 			break;
83 		}
84 	}
85 
86 	if (optind == argc)
87 		usage();
88 	for (; optind < argc; optind++)
89 		lsacl(argv[optind]);
90 
91 	return (0);
92 }
93 
94 void
lsacl(char * file)95 lsacl(char *file)
96 {
97 	struct i_ntsd *sd;
98 	acl_t *acl;
99 	uid_t uid;
100 	gid_t gid;
101 	int error, fd;
102 
103 	fd = open(file, O_RDONLY, 0);
104 	if (fd < 0) {
105 		perror(file);
106 		exit(1);
107 	}
108 
109 	/* First, get the SD in internal form. */
110 	error = smbfs_acl_getsd(fd, selector, &sd);
111 	(void) close(fd);
112 
113 	if (error) {
114 		fprintf(stderr, "%s: getsd, %s\n",
115 		    progname, strerror(error));
116 		exit(1);
117 	}
118 
119 	if (Vflag) {
120 		/*
121 		 * Print it first in Windows form.  This way,
122 		 * if any of the conversion has problems,
123 		 * one can try mapping each SID by hand, i.e.:
124 		 *    idmap show sid:S-1-xxx-yyy-zzz
125 		 */
126 		printf("CIFS security data:\n");
127 		smbfs_acl_print_sd(stdout, sd);
128 		printf("\n");
129 	}
130 
131 	/*
132 	 * Convert the internal SD to a ZFS ACL.
133 	 */
134 	acl = acl_alloc(ACE_T);
135 	error = smbfs_acl_sd2zfs(sd, acl, &uid, &gid);
136 	if (error) {
137 		fprintf(stderr, "%s: sd2zfs, %s\n",
138 		    progname, strerror(error));
139 		exit(1);
140 	}
141 	smbfs_acl_free_sd(sd);
142 
143 	/*
144 	 * Print it as a ZFS-style ACL (ACE_T)
145 	 */
146 	printf("Solaris security data:\n");
147 	if (uid == (uid_t)-1)
148 		printf("owner: -1\n");
149 	else
150 		printf("owner: %u\n", uid);
151 	if (gid == (gid_t)-1)
152 		printf("group: -1\n");
153 	else
154 		printf("group: %u\n", gid);
155 	acl_printacl(acl, 80, 1);
156 	printf("\n");
157 
158 	acl_free(acl);
159 }
160