16711Sgwr /*
26711Sgwr * CDDL HEADER START
36711Sgwr *
46711Sgwr * The contents of this file are subject to the terms of the
56711Sgwr * Common Development and Distribution License (the "License").
66711Sgwr * You may not use this file except in compliance with the License.
76711Sgwr *
86711Sgwr * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96711Sgwr * or http://www.opensolaris.org/os/licensing.
106711Sgwr * See the License for the specific language governing permissions
116711Sgwr * and limitations under the License.
126711Sgwr *
136711Sgwr * When distributing Covered Code, include this CDDL HEADER in each
146711Sgwr * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156711Sgwr * If applicable, add the following below this CDDL HEADER, with the
166711Sgwr * fields enclosed by brackets "[]" replaced with your own identifying
176711Sgwr * information: Portions Copyright [yyyy] [name of copyright owner]
186711Sgwr *
196711Sgwr * CDDL HEADER END
206711Sgwr */
216711Sgwr
226711Sgwr /*
23*11564SGordon.Ross@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
246711Sgwr * Use is subject to license terms.
256711Sgwr */
266711Sgwr
276711Sgwr /*
286711Sgwr * This is the smbfs/lsacl command.
296711Sgwr * (just for testing - not installed)
306711Sgwr */
316711Sgwr
326711Sgwr #include <sys/types.h>
336711Sgwr #include <sys/errno.h>
346711Sgwr #include <sys/stat.h>
356711Sgwr #include <sys/acl.h>
36*11564SGordon.Ross@Sun.COM #include <sys/acl_impl.h>
376711Sgwr
386711Sgwr #include <fcntl.h>
396711Sgwr #include <stdio.h>
406711Sgwr #include <stdlib.h>
416711Sgwr #include <unistd.h>
426711Sgwr #include <string.h>
43*11564SGordon.Ross@Sun.COM #include <aclutils.h>
446711Sgwr
456711Sgwr #include <netsmb/smbfs_acl.h>
466711Sgwr
47*11564SGordon.Ross@Sun.COM extern acl_t *acl_alloc(acl_type_t);
48*11564SGordon.Ross@Sun.COM
496711Sgwr char *progname;
50*11564SGordon.Ross@Sun.COM int Vflag;
516711Sgwr
52*11564SGordon.Ross@Sun.COM uint32_t selector = DACL_SECURITY_INFORMATION |
53*11564SGordon.Ross@Sun.COM OWNER_SECURITY_INFORMATION |
54*11564SGordon.Ross@Sun.COM GROUP_SECURITY_INFORMATION;
556711Sgwr
56*11564SGordon.Ross@Sun.COM void lsacl(char *);
576711Sgwr
586711Sgwr void
usage(void)596711Sgwr usage(void)
606711Sgwr {
61*11564SGordon.Ross@Sun.COM fprintf(stderr, "Usage: %s [-v] file ...\n", progname);
626711Sgwr exit(1);
636711Sgwr }
646711Sgwr
656711Sgwr int
main(int argc,char ** argv)666711Sgwr main(int argc, char **argv)
676711Sgwr {
68*11564SGordon.Ross@Sun.COM int c;
69*11564SGordon.Ross@Sun.COM
70*11564SGordon.Ross@Sun.COM progname = argv[0];
71*11564SGordon.Ross@Sun.COM
72*11564SGordon.Ross@Sun.COM while ((c = getopt(argc, argv, "v")) != -1) {
73*11564SGordon.Ross@Sun.COM switch (c) {
74*11564SGordon.Ross@Sun.COM case 'v':
75*11564SGordon.Ross@Sun.COM Vflag++;
76*11564SGordon.Ross@Sun.COM break;
77*11564SGordon.Ross@Sun.COM
78*11564SGordon.Ross@Sun.COM badopt:
79*11564SGordon.Ross@Sun.COM default:
80*11564SGordon.Ross@Sun.COM fprintf(stderr, "%s: bad option: %c\n",
81*11564SGordon.Ross@Sun.COM progname, c);
82*11564SGordon.Ross@Sun.COM usage();
83*11564SGordon.Ross@Sun.COM break;
84*11564SGordon.Ross@Sun.COM }
85*11564SGordon.Ross@Sun.COM }
86*11564SGordon.Ross@Sun.COM
87*11564SGordon.Ross@Sun.COM if (optind == argc)
88*11564SGordon.Ross@Sun.COM usage();
89*11564SGordon.Ross@Sun.COM for (; optind < argc; optind++)
90*11564SGordon.Ross@Sun.COM lsacl(argv[optind]);
91*11564SGordon.Ross@Sun.COM
92*11564SGordon.Ross@Sun.COM return (0);
93*11564SGordon.Ross@Sun.COM }
94*11564SGordon.Ross@Sun.COM
95*11564SGordon.Ross@Sun.COM void
lsacl(char * file)96*11564SGordon.Ross@Sun.COM lsacl(char *file)
97*11564SGordon.Ross@Sun.COM {
98*11564SGordon.Ross@Sun.COM struct i_ntsd *sd;
99*11564SGordon.Ross@Sun.COM acl_t *acl;
1006711Sgwr uid_t uid;
1016711Sgwr gid_t gid;
1026711Sgwr int error, fd;
1036711Sgwr
104*11564SGordon.Ross@Sun.COM fd = open(file, O_RDONLY, 0);
1056711Sgwr if (fd < 0) {
106*11564SGordon.Ross@Sun.COM perror(file);
1076711Sgwr exit(1);
1086711Sgwr }
1096711Sgwr
110*11564SGordon.Ross@Sun.COM /* First, get the SD in internal form. */
111*11564SGordon.Ross@Sun.COM error = smbfs_acl_getsd(fd, selector, &sd);
112*11564SGordon.Ross@Sun.COM (void) close(fd);
113*11564SGordon.Ross@Sun.COM
1146711Sgwr if (error) {
115*11564SGordon.Ross@Sun.COM fprintf(stderr, "%s: getsd, %s\n",
116*11564SGordon.Ross@Sun.COM progname, strerror(error));
1176711Sgwr exit(1);
1186711Sgwr }
1196711Sgwr
120*11564SGordon.Ross@Sun.COM if (Vflag) {
121*11564SGordon.Ross@Sun.COM /*
122*11564SGordon.Ross@Sun.COM * Print it first in Windows form. This way,
123*11564SGordon.Ross@Sun.COM * if any of the conversion has problems,
124*11564SGordon.Ross@Sun.COM * one can try mapping each SID by hand, i.e.:
125*11564SGordon.Ross@Sun.COM * idmap show sid:S-1-xxx-yyy-zzz
126*11564SGordon.Ross@Sun.COM */
127*11564SGordon.Ross@Sun.COM printf("CIFS security data:\n");
128*11564SGordon.Ross@Sun.COM smbfs_acl_print_sd(stdout, sd);
129*11564SGordon.Ross@Sun.COM printf("\n");
130*11564SGordon.Ross@Sun.COM }
131*11564SGordon.Ross@Sun.COM
1326711Sgwr /*
133*11564SGordon.Ross@Sun.COM * Convert the internal SD to a ZFS ACL.
1346711Sgwr */
135*11564SGordon.Ross@Sun.COM acl = acl_alloc(ACE_T);
136*11564SGordon.Ross@Sun.COM error = smbfs_acl_sd2zfs(sd, acl, &uid, &gid);
137*11564SGordon.Ross@Sun.COM if (error) {
138*11564SGordon.Ross@Sun.COM fprintf(stderr, "%s: sd2zfs, %s\n",
139*11564SGordon.Ross@Sun.COM progname, strerror(error));
140*11564SGordon.Ross@Sun.COM exit(1);
141*11564SGordon.Ross@Sun.COM }
142*11564SGordon.Ross@Sun.COM smbfs_acl_free_sd(sd);
1436711Sgwr
1446711Sgwr /*
145*11564SGordon.Ross@Sun.COM * Print it as a ZFS-style ACL (ACE_T)
1466711Sgwr */
1476711Sgwr printf("Solaris security data:\n");
1486711Sgwr if (uid == (uid_t)-1)
1496711Sgwr printf("owner: -1\n");
1506711Sgwr else
1516711Sgwr printf("owner: %u\n", uid);
1526711Sgwr if (gid == (gid_t)-1)
1536711Sgwr printf("group: -1\n");
1546711Sgwr else
1556711Sgwr printf("group: %u\n", gid);
156*11564SGordon.Ross@Sun.COM acl_printacl(acl, 80, 1);
1576711Sgwr printf("\n");
1586711Sgwr
159*11564SGordon.Ross@Sun.COM acl_free(acl);
1606711Sgwr }
161