2 Unix SMB/Netbios implementation.
6 Copyright (C) Andrew Tridgell 2000
7 Copyright (C) Tim Potter 2000
8 Copyright (C) Jeremy Allison 2000
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 static fstring password;
28 static pstring username;
29 static pstring owner_username;
30 static fstring server;
35 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
36 #define CREATE_ACCESS_WRITE (WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS)
38 /* numeric is set when the user wants numeric SIDs and ACEs rather
39 than going via LSA calls to resolve them */
42 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
43 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP};
44 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
51 /* These values discovered by inspection */
53 static struct perm_value special_values[] = {
63 static struct perm_value standard_values[] = {
64 { "READ", 0x001200a9 },
65 { "CHANGE", 0x001301bf },
66 { "FULL", 0x001f01ff },
70 struct cli_state lsa_cli;
72 struct ntuser_creds creds;
75 /* Open cli connection and policy handle */
77 static BOOL cacls_open_policy_hnd(void)
79 creds.pwd.null_pwd = 1;
81 /* Initialise cli LSA connection */
83 if (!lsa_cli.initialised &&
84 !cli_lsa_initialise(&lsa_cli, server, &creds)) {
88 /* Open policy handle */
90 if (!got_policy_hnd) {
92 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
93 but NT sends 0x2000000 so we might as well do it too. */
95 if (!NT_STATUS_IS_OK(cli_lsa_open_policy(&lsa_cli, lsa_cli.mem_ctx, True,
96 GENERIC_EXECUTE_ACCESS, &pol))) {
100 got_policy_hnd = True;
106 /* convert a SID to a string, either numeric or username/group */
107 static void SidToString(fstring str, DOM_SID *sid)
110 uint32 *types = NULL;
113 sid_to_string(str, sid);
117 /* Ask LSA to convert the sid to a name */
119 if (!cacls_open_policy_hnd() ||
120 !NT_STATUS_IS_OK(cli_lsa_lookup_sids(&lsa_cli, lsa_cli.mem_ctx, &pol, 1, sid, &names,
121 &types, &num_names)) ||
122 !names || !names[0]) {
128 fstrcpy(str, names[0]);
131 /* convert a string to a SID, either numeric or username/group */
132 static BOOL StringToSid(DOM_SID *sid, char *str)
134 uint32 *types = NULL;
135 DOM_SID *sids = NULL;
139 if (strncmp(str, "S-", 2) == 0) {
140 return string_to_sid(sid, str);
143 if (!cacls_open_policy_hnd() ||
144 !NT_STATUS_IS_OK(cli_lsa_lookup_names(&lsa_cli, lsa_cli.mem_ctx, &pol, 1, &str,
145 &sids, &types, &num_sids))) {
150 sid_copy(sid, &sids[0]);
158 /* print an ACE on a FILE, using either numeric or ascii representation */
159 static void print_ace(FILE *f, SEC_ACE *ace)
161 struct perm_value *v;
166 SidToString(sidstr, &ace->sid);
168 fprintf(f, "%s:", sidstr);
171 fprintf(f, "%d/%d/0x%08x",
172 ace->type, ace->flags, ace->info.mask);
178 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
179 fprintf(f, "ALLOWED");
180 } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
181 fprintf(f, "DENIED");
183 fprintf(f, "%d", ace->type);
186 /* Not sure what flags can be set in a file ACL */
188 fprintf(f, "/%d/", ace->flags);
190 /* Standard permissions */
192 for (v = standard_values; v->perm; v++) {
193 if (ace->info.mask == v->mask) {
194 fprintf(f, "%s", v->perm);
199 /* Special permissions. Print out a hex value if we have
200 leftover bits in the mask. */
202 got_mask = ace->info.mask;
205 for (v = special_values; v->perm; v++) {
206 if ((ace->info.mask & v->mask) == v->mask) {
208 fprintf(f, "%s", v->perm);
210 got_mask &= ~v->mask;
216 fprintf(f, "0x%08x", ace->info.mask);
225 /* parse an ACE in the same format as print_ace() */
226 static BOOL parse_ace(SEC_ACE *ace, char *str)
230 unsigned atype, aflags, amask;
233 struct perm_value *v;
236 p = strchr_m(str,':');
237 if (!p) return False;
241 /* Try to parse numeric form */
243 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
244 StringToSid(&sid, str)) {
248 /* Try to parse text form */
250 if (!StringToSid(&sid, str)) {
254 if (!next_token(&p, tok, "/", sizeof(fstring))) {
258 if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
259 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
260 } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
261 atype = SEC_ACE_TYPE_ACCESS_DENIED;
266 /* Only numeric form accepted for flags at present */
268 if (!(next_token(&p, tok, "/", sizeof(fstring)) &&
269 sscanf(tok, "%i", &aflags))) {
273 if (!next_token(&p, tok, "/", sizeof(fstring))) {
277 if (strncmp(tok, "0x", 2) == 0) {
278 if (sscanf(tok, "%i", &amask) != 1) {
284 for (v = standard_values; v->perm; v++) {
285 if (strcmp(tok, v->perm) == 0) {
296 for (v = special_values; v->perm; v++) {
297 if (v->perm[0] == *p) {
303 if (!found) return False;
313 init_sec_ace(ace, &sid, atype, mask, aflags);
317 /* add an ACE to a list of ACEs in a SEC_ACL */
318 static BOOL add_ace(SEC_ACL **the_acl, SEC_ACE *ace)
323 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
327 aces = calloc(1+(*the_acl)->num_aces,sizeof(SEC_ACE));
328 memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
329 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
330 new = make_sec_acl(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
336 /* parse a ascii version of a security descriptor */
337 static SEC_DESC *sec_desc_parse(char *str)
343 DOM_SID *grp_sid=NULL, *owner_sid=NULL;
347 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
349 if (strncmp(tok,"REVISION:", 9) == 0) {
350 revision = strtol(tok+9, NULL, 16);
354 if (strncmp(tok,"OWNER:", 6) == 0) {
355 owner_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
357 !StringToSid(owner_sid, tok+6)) {
358 printf("Failed to parse owner sid\n");
364 if (strncmp(tok,"GROUP:", 6) == 0) {
365 grp_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
367 !StringToSid(grp_sid, tok+6)) {
368 printf("Failed to parse group sid\n");
374 if (strncmp(tok,"ACL:", 4) == 0) {
376 if (!parse_ace(&ace, tok+4)) {
377 printf("Failed to parse ACL %s\n", tok);
380 if(!add_ace(&dacl, &ace)) {
381 printf("Failed to add ACL %s\n", tok);
387 printf("Failed to parse security descriptor\n");
391 ret = make_sec_desc(ctx,revision, owner_sid, grp_sid,
392 NULL, dacl, &sd_size);
395 SAFE_FREE(owner_sid);
401 /* print a ascii version of a security descriptor on a FILE handle */
402 static void sec_desc_print(FILE *f, SEC_DESC *sd)
407 printf("REVISION:%d\n", sd->revision);
409 /* Print owner and group sid */
412 SidToString(sidstr, sd->owner_sid);
417 printf("OWNER:%s\n", sidstr);
420 SidToString(sidstr, sd->grp_sid);
425 fprintf(f, "GROUP:%s\n", sidstr);
428 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
429 SEC_ACE *ace = &sd->dacl->ace[i];
437 /*****************************************************
438 dump the acls for a file
439 *******************************************************/
440 static int cacl_dump(struct cli_state *cli, char *filename)
445 if (test_args) return EXIT_OK;
447 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
449 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
453 sd = cli_query_secdesc(cli, fnum, ctx);
456 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
460 sec_desc_print(stdout, sd);
462 cli_close(cli, fnum);
467 /*****************************************************
468 Change the ownership or group ownership of a file. Just
469 because the NT docs say this can't be done :-). JRA.
470 *******************************************************/
472 static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
473 char *filename, char *new_username)
480 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
483 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
487 if (!StringToSid(&sid, new_username))
488 return EXIT_PARSE_ERROR;
490 old = cli_query_secdesc(cli, fnum, ctx);
492 cli_close(cli, fnum);
495 printf("owner_set: Failed to query old descriptor\n");
499 sd = make_sec_desc(ctx,old->revision,
500 (change_mode == REQUEST_CHOWN) ? &sid : old->owner_sid,
501 (change_mode == REQUEST_CHGRP) ? &sid : old->grp_sid,
502 NULL, old->dacl, &sd_size);
504 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
507 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
511 if (!cli_set_secdesc(cli, fnum, sd)) {
512 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
515 cli_close(cli, fnum);
521 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
522 However NT4 gives a "The information may have been modified by a
523 computer running Windows NT 5.0" if denied ACEs do not appear before
526 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
528 if (sec_ace_equal(ace1, ace2)) return 0;
529 if (ace1->type != ace2->type) return ace2->type - ace1->type;
530 if (sid_compare(&ace1->sid, &ace2->sid)) return sid_compare(&ace1->sid, &ace2->sid);
531 if (ace1->flags != ace2->flags) return ace1->flags - ace2->flags;
532 if (ace1->info.mask != ace2->info.mask) return ace1->info.mask - ace2->info.mask;
533 if (ace1->size != ace2->size) return ace1->size - ace2->size;
534 return memcmp(ace1, ace2, sizeof(SEC_ACE));
537 static void sort_acl(SEC_ACL *the_acl)
540 if (!the_acl) return;
542 qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
544 for (i=1;i<the_acl->num_aces;) {
545 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
547 for (j=i; j<the_acl->num_aces-1; j++) {
548 the_acl->ace[j] = the_acl->ace[j+1];
557 /*****************************************************
558 set the ACLs on a file given an ascii description
559 *******************************************************/
560 static int cacl_set(struct cli_state *cli, char *filename,
561 char *the_acl, enum acl_mode mode)
567 int result = EXIT_OK;
569 sd = sec_desc_parse(the_acl);
571 if (!sd) return EXIT_PARSE_ERROR;
572 if (test_args) return EXIT_OK;
574 /* The desired access below is the only one I could find that works
575 with NT4, W2KP and Samba */
577 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
580 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
584 old = cli_query_secdesc(cli, fnum, ctx);
587 printf("calc_set: Failed to query old descriptor\n");
591 cli_close(cli, fnum);
593 /* the logic here is rather more complex than I would like */
596 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
599 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
600 if (sec_ace_equal(&sd->dacl->ace[i],
601 &old->dacl->ace[j])) {
603 for (k=j; k<old->dacl->num_aces-1;k++) {
604 old->dacl->ace[k] = old->dacl->ace[k+1];
606 old->dacl->num_aces--;
607 if (old->dacl->num_aces == 0) {
608 SAFE_FREE(old->dacl->ace);
609 SAFE_FREE(old->dacl);
618 printf("ACL for ACE:");
619 print_ace(stdout, &sd->dacl->ace[i]);
620 printf(" not found\n");
626 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
629 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
630 if (sid_equal(&sd->dacl->ace[i].sid,
631 &old->dacl->ace[j].sid)) {
632 old->dacl->ace[j] = sd->dacl->ace[i];
640 SidToString(str, &sd->dacl->ace[i].sid);
641 printf("ACL for SID %s not found\n", str);
648 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
649 add_ace(&old->dacl, &sd->dacl->ace[i]);
658 /* Denied ACE entries must come before allowed ones */
661 /* Create new security descriptor and set it */
662 sd = make_sec_desc(ctx,old->revision, old->owner_sid, old->grp_sid,
663 NULL, old->dacl, &sd_size);
665 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
668 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
672 if (!cli_set_secdesc(cli, fnum, sd)) {
673 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
674 result = EXIT_FAILED;
679 cli_close(cli, fnum);
685 /*****************************************************
686 return a connection to a server
687 *******************************************************/
688 struct cli_state *connect_one(char *share)
691 struct nmb_name called, calling;
693 extern struct in_addr ipzero;
694 extern pstring global_myname;
696 fstrcpy(server,share+2);
697 share = strchr_m(server,'\\');
698 if (!share) return NULL;
704 make_nmb_name(&calling, global_myname, 0x0);
705 make_nmb_name(&called , server, 0x20);
710 /* have to open a new connection */
711 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server, &ip)) {
712 DEBUG(0,("Connection to %s failed\n", server));
718 if (!cli_session_request(c, &calling, &called)) {
719 DEBUG(0,("session request to %s failed\n", called.name));
722 if (strcmp(called.name, "*SMBSERVER")) {
723 make_nmb_name(&called , "*SMBSERVER", 0x20);
729 DEBUG(4,(" session request ok\n"));
731 if (!cli_negprot(c)) {
732 DEBUG(0,("protocol negotiation failed\n"));
739 char *pass = getpass("Password: ");
741 pstrcpy(password, pass);
745 if (!cli_session_setup(c, username,
746 password, strlen(password),
747 password, strlen(password),
749 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
755 DEBUG(4,(" session setup ok\n"));
757 if (!cli_send_tconX(c, share, "?????",
758 password, strlen(password)+1)) {
759 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
765 DEBUG(4,(" tconx ok\n"));
771 static void usage(void)
774 "Usage: smbcacls //server1/share1 filename [options]\n\
776 \t-D <acls> delete an acl\n\
777 \t-M <acls> modify an acl\n\
778 \t-A <acls> add an acl\n\
779 \t-S <acls> set acls\n\
780 \t-C username change ownership of a file\n\
781 \t-G username change group ownership of a file\n\
782 \t-n don't resolve sids or masks to names\n\
784 \t-d debuglevel set debug output level\n\
785 \t-U username user to autheticate as\n\
787 The username can be of the form username%%password or\n\
788 workgroup\\username%%password.\n\n\
789 An acl is of the form ACL:<SID>:type/flags/mask\n\
790 You can string acls together with spaces, commas or newlines\n\
794 /****************************************************************************
796 ****************************************************************************/
797 int main(int argc,char *argv[])
805 struct cli_state *cli=NULL;
806 enum acl_mode mode = SMB_ACL_SET;
807 char *the_acl = NULL;
808 enum chown_mode change_mode = REQUEST_NONE;
817 if (argc < 3 || argv[1][0] == '-') {
820 exit(EXIT_PARSE_ERROR);
823 setup_logging(argv[0],True);
826 pstrcpy(filename, argv[2]);
827 all_string_sub(share,"/","\\",0);
834 lp_load(dyn_CONFIGFILE,True,False,False);
837 if (getenv("USER")) {
838 pstrcpy(username,getenv("USER"));
840 if ((p=strchr_m(username,'%'))) {
842 pstrcpy(password,p+1);
844 memset(strchr_m(getenv("USER"), '%') + 1, 'X',
849 while ((opt = getopt(argc, argv, "U:nhS:D:A:M:C:G:td:")) != EOF) {
852 pstrcpy(username,optarg);
853 p = strchr_m(username,'%');
856 pstrcpy(password, p+1);
868 mode = SMB_ACL_DELETE;
873 mode = SMB_ACL_MODIFY;
882 pstrcpy(owner_username,optarg);
883 change_mode = REQUEST_CHOWN;
887 pstrcpy(owner_username,optarg);
888 change_mode = REQUEST_CHGRP;
902 exit(EXIT_PARSE_ERROR);
905 DEBUGLEVEL = atoi(optarg);
909 printf("Unknown option %c (%d)\n", (char)opt, opt);
911 exit(EXIT_PARSE_ERROR);
921 exit(EXIT_PARSE_ERROR);
924 /* Make connection to server */
927 cli = connect_one(share);
934 all_string_sub(filename, "/", "\\", 0);
935 if (filename[0] != '\\') {
938 safe_strcpy(&s[1], filename, sizeof(pstring)-1);
939 pstrcpy(filename, s);
942 /* Perform requested action */
944 if (change_mode != REQUEST_NONE) {
945 result = owner_set(cli, change_mode, filename, owner_username);
946 } else if (the_acl) {
947 result = cacl_set(cli, filename, the_acl, mode);
949 result = cacl_dump(cli, filename);