xref: /titanic_52/usr/src/cmd/fs.d/smbclnt/lsacl/lsacl.c (revision dd51520e127b452179a2ce4ea3bd8dee949f9afe)
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
59 usage(void)
60 {
61 	fprintf(stderr, "Usage: %s [-v] file ...\n", progname);
62 	exit(1);
63 }
64 
65 int
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 		badopt:
79 		default:
80 			fprintf(stderr, "%s: bad option: %c\n",
81 			    progname, c);
82 			usage();
83 			break;
84 		}
85 	}
86 
87 	if (optind == argc)
88 		usage();
89 	for (; optind < argc; optind++)
90 		lsacl(argv[optind]);
91 
92 	return (0);
93 }
94 
95 void
96 lsacl(char *file)
97 {
98 	struct i_ntsd *sd;
99 	acl_t *acl;
100 	uid_t uid;
101 	gid_t gid;
102 	int error, fd;
103 
104 	fd = open(file, O_RDONLY, 0);
105 	if (fd < 0) {
106 		perror(file);
107 		exit(1);
108 	}
109 
110 	/* First, get the SD in internal form. */
111 	error = smbfs_acl_getsd(fd, selector, &sd);
112 	(void) close(fd);
113 
114 	if (error) {
115 		fprintf(stderr, "%s: getsd, %s\n",
116 		    progname, strerror(error));
117 		exit(1);
118 	}
119 
120 	if (Vflag) {
121 		/*
122 		 * Print it first in Windows form.  This way,
123 		 * if any of the conversion has problems,
124 		 * one can try mapping each SID by hand, i.e.:
125 		 *    idmap show sid:S-1-xxx-yyy-zzz
126 		 */
127 		printf("CIFS security data:\n");
128 		smbfs_acl_print_sd(stdout, sd);
129 		printf("\n");
130 	}
131 
132 	/*
133 	 * Convert the internal SD to a ZFS ACL.
134 	 */
135 	acl = acl_alloc(ACE_T);
136 	error = smbfs_acl_sd2zfs(sd, acl, &uid, &gid);
137 	if (error) {
138 		fprintf(stderr, "%s: sd2zfs, %s\n",
139 		    progname, strerror(error));
140 		exit(1);
141 	}
142 	smbfs_acl_free_sd(sd);
143 
144 	/*
145 	 * Print it as a ZFS-style ACL (ACE_T)
146 	 */
147 	printf("Solaris security data:\n");
148 	if (uid == (uid_t)-1)
149 		printf("owner: -1\n");
150 	else
151 		printf("owner: %u\n", uid);
152 	if (gid == (gid_t)-1)
153 		printf("group: -1\n");
154 	else
155 		printf("group: %u\n", gid);
156 	acl_printacl(acl, 80, 1);
157 	printf("\n");
158 
159 	acl_free(acl);
160 }
161