root/source3/utils/smbcacls.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. cli_lsa_lookup_sid
  2. cli_lsa_lookup_name
  3. SidToString
  4. StringToSid
  5. print_ace_flags
  6. print_ace
  7. parse_ace_flags
  8. parse_ace
  9. add_ace
  10. sec_desc_parse
  11. sec_desc_print
  12. cacl_dump
  13. owner_set
  14. ace_compare
  15. sort_acl
  16. cacl_set
  17. connect_one
  18. main

   1 /* 
   2    Unix SMB/CIFS implementation.
   3    ACL get/set utility
   4    
   5    Copyright (C) Andrew Tridgell 2000
   6    Copyright (C) Tim Potter      2000
   7    Copyright (C) Jeremy Allison  2000
   8    Copyright (C) Jelmer Vernooij 2003
   9    
  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 3 of the License, or
  13    (at your option) any later version.
  14    
  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.
  19    
  20    You should have received a copy of the GNU General Public License
  21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  22 */
  23 
  24 #include "includes.h"
  25 
  26 static int test_args;
  27 
  28 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
  29 
  30 /* numeric is set when the user wants numeric SIDs and ACEs rather
  31    than going via LSA calls to resolve them */
  32 static int numeric;
  33 
  34 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
  35 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP};
  36 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
  37 
  38 struct perm_value {
  39         const char *perm;
  40         uint32 mask;
  41 };
  42 
  43 /* These values discovered by inspection */
  44 
  45 static const struct perm_value special_values[] = {
  46         { "R", 0x00120089 },
  47         { "W", 0x00120116 },
  48         { "X", 0x001200a0 },
  49         { "D", 0x00010000 },
  50         { "P", 0x00040000 },
  51         { "O", 0x00080000 },
  52         { NULL, 0 },
  53 };
  54 
  55 static const struct perm_value standard_values[] = {
  56         { "READ",   0x001200a9 },
  57         { "CHANGE", 0x001301bf },
  58         { "FULL",   0x001f01ff },
  59         { NULL, 0 },
  60 };
  61 
  62 /* Open cli connection and policy handle */
  63 
  64 static NTSTATUS cli_lsa_lookup_sid(struct cli_state *cli,
     /* [<][>][^][v][top][bottom][index][help] */
  65                                    const DOM_SID *sid,
  66                                    TALLOC_CTX *mem_ctx,
  67                                    enum lsa_SidType *type,
  68                                    char **domain, char **name)
  69 {
  70         uint16 orig_cnum = cli->cnum;
  71         struct rpc_pipe_client *p;
  72         struct policy_handle handle;
  73         NTSTATUS status;
  74         TALLOC_CTX *frame = talloc_stackframe();
  75         enum lsa_SidType *types;
  76         char **domains;
  77         char **names;
  78 
  79         status = cli_tcon_andx(cli, "IPC$", "?????", "", 0);
  80         if (!NT_STATUS_IS_OK(status)) {
  81                 return status;
  82         }
  83 
  84         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
  85                                           &p);
  86         if (!NT_STATUS_IS_OK(status)) {
  87                 goto fail;
  88         }
  89 
  90         status = rpccli_lsa_open_policy(p, talloc_tos(), True,
  91                                         GENERIC_EXECUTE_ACCESS, &handle);
  92         if (!NT_STATUS_IS_OK(status)) {
  93                 goto fail;
  94         }
  95 
  96         status = rpccli_lsa_lookup_sids(p, talloc_tos(), &handle, 1, sid,
  97                                         &domains, &names, &types);
  98         if (!NT_STATUS_IS_OK(status)) {
  99                 goto fail;
 100         }
 101 
 102         *type = types[0];
 103         *domain = talloc_move(mem_ctx, &domains[0]);
 104         *name = talloc_move(mem_ctx, &names[0]);
 105 
 106         status = NT_STATUS_OK;
 107  fail:
 108         TALLOC_FREE(p);
 109         cli_tdis(cli);
 110         cli->cnum = orig_cnum;
 111         TALLOC_FREE(frame);
 112         return status;
 113 }
 114 
 115 static NTSTATUS cli_lsa_lookup_name(struct cli_state *cli,
     /* [<][>][^][v][top][bottom][index][help] */
 116                                     const char *name,
 117                                     enum lsa_SidType *type,
 118                                     DOM_SID *sid)
 119 {
 120         uint16 orig_cnum = cli->cnum;
 121         struct rpc_pipe_client *p;
 122         struct policy_handle handle;
 123         NTSTATUS status;
 124         TALLOC_CTX *frame = talloc_stackframe();
 125         DOM_SID *sids;
 126         enum lsa_SidType *types;
 127 
 128         status = cli_tcon_andx(cli, "IPC$", "?????", "", 0);
 129         if (!NT_STATUS_IS_OK(status)) {
 130                 return status;
 131         }
 132 
 133         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
 134                                           &p);
 135         if (!NT_STATUS_IS_OK(status)) {
 136                 goto fail;
 137         }
 138 
 139         status = rpccli_lsa_open_policy(p, talloc_tos(), True,
 140                                         GENERIC_EXECUTE_ACCESS, &handle);
 141         if (!NT_STATUS_IS_OK(status)) {
 142                 goto fail;
 143         }
 144 
 145         status = rpccli_lsa_lookup_names(p, talloc_tos(), &handle, 1, &name,
 146                                          NULL, 1, &sids, &types);
 147         if (!NT_STATUS_IS_OK(status)) {
 148                 goto fail;
 149         }
 150 
 151         *type = types[0];
 152         *sid = sids[0];
 153 
 154         status = NT_STATUS_OK;
 155  fail:
 156         TALLOC_FREE(p);
 157         cli_tdis(cli);
 158         cli->cnum = orig_cnum;
 159         TALLOC_FREE(frame);
 160         return status;
 161 }
 162 
 163 /* convert a SID to a string, either numeric or username/group */
 164 static void SidToString(struct cli_state *cli, fstring str, const DOM_SID *sid)
     /* [<][>][^][v][top][bottom][index][help] */
 165 {
 166         char *domain = NULL;
 167         char *name = NULL;
 168         enum lsa_SidType type;
 169         NTSTATUS status;
 170 
 171         sid_to_fstring(str, sid);
 172 
 173         if (numeric) {
 174                 return;
 175         }
 176 
 177         status = cli_lsa_lookup_sid(cli, sid, talloc_tos(), &type,
 178                                     &domain, &name);
 179 
 180         if (!NT_STATUS_IS_OK(status)) {
 181                 return;
 182         }
 183 
 184         if (*domain) {
 185                 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
 186                         domain, lp_winbind_separator(), name);
 187         } else {
 188                 fstrcpy(str, name);
 189         }
 190 }
 191 
 192 /* convert a string to a SID, either numeric or username/group */
 193 static bool StringToSid(struct cli_state *cli, DOM_SID *sid, const char *str)
     /* [<][>][^][v][top][bottom][index][help] */
 194 {
 195         enum lsa_SidType type;
 196 
 197         if (strncmp(str, "S-", 2) == 0) {
 198                 return string_to_sid(sid, str);
 199         }
 200 
 201         return NT_STATUS_IS_OK(cli_lsa_lookup_name(cli, str, &type, sid));
 202 }
 203 
 204 static void print_ace_flags(FILE *f, uint8_t flags)
     /* [<][>][^][v][top][bottom][index][help] */
 205 {
 206         char *str = talloc_strdup(NULL, "");
 207 
 208         if (!str) {
 209                 goto out;
 210         }
 211 
 212         if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
 213                 str = talloc_asprintf(str, "%s%s",
 214                                 str, "OI|");
 215                 if (!str) {
 216                         goto out;
 217                 }
 218         }
 219         if (flags & SEC_ACE_FLAG_CONTAINER_INHERIT) {
 220                 str = talloc_asprintf(str, "%s%s",
 221                                 str, "CI|");
 222                 if (!str) {
 223                         goto out;
 224                 }
 225         }
 226         if (flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
 227                 str = talloc_asprintf(str, "%s%s",
 228                                 str, "NP|");
 229                 if (!str) {
 230                         goto out;
 231                 }
 232         }
 233         if (flags & SEC_ACE_FLAG_INHERIT_ONLY) {
 234                 str = talloc_asprintf(str, "%s%s",
 235                                 str, "IO|");
 236                 if (!str) {
 237                         goto out;
 238                 }
 239         }
 240         if (flags & SEC_ACE_FLAG_INHERITED_ACE) {
 241                 str = talloc_asprintf(str, "%s%s",
 242                                 str, "I|");
 243                 if (!str) {
 244                         goto out;
 245                 }
 246         }
 247         /* Ignore define SEC_ACE_FLAG_SUCCESSFUL_ACCESS ( 0x40 )
 248            and SEC_ACE_FLAG_FAILED_ACCESS ( 0x80 ) as they're
 249            audit ace flags. */
 250 
 251         if (str[strlen(str)-1] == '|') {
 252                 str[strlen(str)-1] = '\0';
 253                 fprintf(f, "/%s/", str);
 254         } else {
 255                 fprintf(f, "/0x%x/", flags);
 256         }
 257         TALLOC_FREE(str);
 258         return;
 259 
 260   out:
 261         fprintf(f, "/0x%x/", flags);
 262 }
 263 
 264 /* print an ACE on a FILE, using either numeric or ascii representation */
 265 static void print_ace(struct cli_state *cli, FILE *f, SEC_ACE *ace)
     /* [<][>][^][v][top][bottom][index][help] */
 266 {
 267         const struct perm_value *v;
 268         fstring sidstr;
 269         int do_print = 0;
 270         uint32 got_mask;
 271 
 272         SidToString(cli, sidstr, &ace->trustee);
 273 
 274         fprintf(f, "%s:", sidstr);
 275 
 276         if (numeric) {
 277                 fprintf(f, "%d/0x%x/0x%08x",
 278                         ace->type, ace->flags, ace->access_mask);
 279                 return;
 280         }
 281 
 282         /* Ace type */
 283 
 284         if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
 285                 fprintf(f, "ALLOWED");
 286         } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
 287                 fprintf(f, "DENIED");
 288         } else {
 289                 fprintf(f, "%d", ace->type);
 290         }
 291 
 292         print_ace_flags(f, ace->flags);
 293 
 294         /* Standard permissions */
 295 
 296         for (v = standard_values; v->perm; v++) {
 297                 if (ace->access_mask == v->mask) {
 298                         fprintf(f, "%s", v->perm);
 299                         return;
 300                 }
 301         }
 302 
 303         /* Special permissions.  Print out a hex value if we have
 304            leftover bits in the mask. */
 305 
 306         got_mask = ace->access_mask;
 307 
 308  again:
 309         for (v = special_values; v->perm; v++) {
 310                 if ((ace->access_mask & v->mask) == v->mask) {
 311                         if (do_print) {
 312                                 fprintf(f, "%s", v->perm);
 313                         }
 314                         got_mask &= ~v->mask;
 315                 }
 316         }
 317 
 318         if (!do_print) {
 319                 if (got_mask != 0) {
 320                         fprintf(f, "0x%08x", ace->access_mask);
 321                 } else {
 322                         do_print = 1;
 323                         goto again;
 324                 }
 325         }
 326 }
 327 
 328 static bool parse_ace_flags(const char *str, unsigned int *pflags)
     /* [<][>][^][v][top][bottom][index][help] */
 329 {
 330         const char *p = str;
 331         *pflags = 0;
 332 
 333         while (*p) {
 334                 if (strnequal(p, "OI", 2)) {
 335                         *pflags |= SEC_ACE_FLAG_OBJECT_INHERIT;
 336                         p += 2;
 337                 } else if (strnequal(p, "CI", 2)) {
 338                         *pflags |= SEC_ACE_FLAG_CONTAINER_INHERIT;
 339                         p += 2;
 340                 } else if (strnequal(p, "NP", 2)) {
 341                         *pflags |= SEC_ACE_FLAG_NO_PROPAGATE_INHERIT;
 342                         p += 2;
 343                 } else if (strnequal(p, "IO", 2)) {
 344                         *pflags |= SEC_ACE_FLAG_INHERIT_ONLY;
 345                         p += 2;
 346                 } else if (*p == 'I') {
 347                         *pflags |= SEC_ACE_FLAG_INHERITED_ACE;
 348                         p += 1;
 349                 } else if (*p) {
 350                         return false;
 351                 }
 352 
 353                 if (*p != '|' && *p != '\0') {
 354                         return false;
 355                 }
 356         }
 357         return true;
 358 }
 359 
 360 /* parse an ACE in the same format as print_ace() */
 361 static bool parse_ace(struct cli_state *cli, SEC_ACE *ace,
     /* [<][>][^][v][top][bottom][index][help] */
 362                       const char *orig_str)
 363 {
 364         char *p;
 365         const char *cp;
 366         char *tok;
 367         unsigned int atype = 0;
 368         unsigned int aflags = 0;
 369         unsigned int amask = 0;
 370         DOM_SID sid;
 371         uint32_t mask;
 372         const struct perm_value *v;
 373         char *str = SMB_STRDUP(orig_str);
 374         TALLOC_CTX *frame = talloc_stackframe();
 375 
 376         if (!str) {
 377                 TALLOC_FREE(frame);
 378                 return False;
 379         }
 380 
 381         ZERO_STRUCTP(ace);
 382         p = strchr_m(str,':');
 383         if (!p) {
 384                 printf("ACE '%s': missing ':'.\n", orig_str);
 385                 SAFE_FREE(str);
 386                 TALLOC_FREE(frame);
 387                 return False;
 388         }
 389         *p = '\0';
 390         p++;
 391         /* Try to parse numeric form */
 392 
 393         if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
 394             StringToSid(cli, &sid, str)) {
 395                 goto done;
 396         }
 397 
 398         /* Try to parse text form */
 399 
 400         if (!StringToSid(cli, &sid, str)) {
 401                 printf("ACE '%s': failed to convert '%s' to SID\n",
 402                         orig_str, str);
 403                 SAFE_FREE(str);
 404                 TALLOC_FREE(frame);
 405                 return False;
 406         }
 407 
 408         cp = p;
 409         if (!next_token_talloc(frame, &cp, &tok, "/")) {
 410                 printf("ACE '%s': failed to find '/' character.\n",
 411                         orig_str);
 412                 SAFE_FREE(str);
 413                 TALLOC_FREE(frame);
 414                 return False;
 415         }
 416 
 417         if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
 418                 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
 419         } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
 420                 atype = SEC_ACE_TYPE_ACCESS_DENIED;
 421         } else {
 422                 printf("ACE '%s': missing 'ALLOWED' or 'DENIED' entry at '%s'\n",
 423                         orig_str, tok);
 424                 SAFE_FREE(str);
 425                 TALLOC_FREE(frame);
 426                 return False;
 427         }
 428 
 429         /* Only numeric form accepted for flags at present */
 430 
 431         if (!next_token_talloc(frame, &cp, &tok, "/")) {
 432                 printf("ACE '%s': bad flags entry at '%s'\n",
 433                         orig_str, tok);
 434                 SAFE_FREE(str);
 435                 TALLOC_FREE(frame);
 436                 return False;
 437         }
 438 
 439         if (tok[0] < '0' || tok[0] > '9') {
 440                 if (!parse_ace_flags(tok, &aflags)) {
 441                         printf("ACE '%s': bad named flags entry at '%s'\n",
 442                                 orig_str, tok);
 443                         SAFE_FREE(str);
 444                         TALLOC_FREE(frame);
 445                         return False;
 446                 }
 447         } else if (strnequal(tok, "0x", 2)) {
 448                 if (!sscanf(tok, "%x", &aflags)) {
 449                         printf("ACE '%s': bad hex flags entry at '%s'\n",
 450                                 orig_str, tok);
 451                         SAFE_FREE(str);
 452                         TALLOC_FREE(frame);
 453                         return False;
 454                 }
 455         } else {
 456                 if (!sscanf(tok, "%i", &aflags)) {
 457                         printf("ACE '%s': bad integer flags entry at '%s'\n",
 458                                 orig_str, tok);
 459                         SAFE_FREE(str);
 460                         TALLOC_FREE(frame);
 461                         return False;
 462                 }
 463         }
 464 
 465         if (!next_token_talloc(frame, &cp, &tok, "/")) {
 466                 printf("ACE '%s': missing / at '%s'\n",
 467                         orig_str, tok);
 468                 SAFE_FREE(str);
 469                 TALLOC_FREE(frame);
 470                 return False;
 471         }
 472 
 473         if (strncmp(tok, "0x", 2) == 0) {
 474                 if (sscanf(tok, "%i", &amask) != 1) {
 475                         printf("ACE '%s': bad hex number at '%s'\n",
 476                                 orig_str, tok);
 477                         SAFE_FREE(str);
 478                         TALLOC_FREE(frame);
 479                         return False;
 480                 }
 481                 goto done;
 482         }
 483 
 484         for (v = standard_values; v->perm; v++) {
 485                 if (strcmp(tok, v->perm) == 0) {
 486                         amask = v->mask;
 487                         goto done;
 488                 }
 489         }
 490 
 491         p = tok;
 492 
 493         while(*p) {
 494                 bool found = False;
 495 
 496                 for (v = special_values; v->perm; v++) {
 497                         if (v->perm[0] == *p) {
 498                                 amask |= v->mask;
 499                                 found = True;
 500                         }
 501                 }
 502 
 503                 if (!found) {
 504                         printf("ACE '%s': bad permission value at '%s'\n",
 505                                 orig_str, p);
 506                         SAFE_FREE(str);
 507                         TALLOC_FREE(frame);
 508                         return False;
 509                 }
 510                 p++;
 511         }
 512 
 513         if (*p) {
 514                 TALLOC_FREE(frame);
 515                 SAFE_FREE(str);
 516                 return False;
 517         }
 518 
 519  done:
 520         mask = amask;
 521         init_sec_ace(ace, &sid, atype, mask, aflags);
 522         TALLOC_FREE(frame);
 523         SAFE_FREE(str);
 524         return True;
 525 }
 526 
 527 /* add an ACE to a list of ACEs in a SEC_ACL */
 528 static bool add_ace(SEC_ACL **the_acl, SEC_ACE *ace)
     /* [<][>][^][v][top][bottom][index][help] */
 529 {
 530         SEC_ACL *new_ace;
 531         SEC_ACE *aces;
 532         if (! *the_acl) {
 533                 return (((*the_acl) = make_sec_acl(talloc_tos(), 3, 1, ace))
 534                         != NULL);
 535         }
 536 
 537         if (!(aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces))) {
 538                 return False;
 539         }
 540         memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
 541         memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
 542         new_ace = make_sec_acl(talloc_tos(),(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
 543         SAFE_FREE(aces);
 544         (*the_acl) = new_ace;
 545         return True;
 546 }
 547 
 548 /* parse a ascii version of a security descriptor */
 549 static SEC_DESC *sec_desc_parse(TALLOC_CTX *ctx, struct cli_state *cli, char *str)
     /* [<][>][^][v][top][bottom][index][help] */
 550 {
 551         const char *p = str;
 552         char *tok;
 553         SEC_DESC *ret = NULL;
 554         size_t sd_size;
 555         DOM_SID *grp_sid=NULL, *owner_sid=NULL;
 556         SEC_ACL *dacl=NULL;
 557         int revision=1;
 558 
 559         while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
 560                 if (strncmp(tok,"REVISION:", 9) == 0) {
 561                         revision = strtol(tok+9, NULL, 16);
 562                         continue;
 563                 }
 564 
 565                 if (strncmp(tok,"OWNER:", 6) == 0) {
 566                         if (owner_sid) {
 567                                 printf("Only specify owner once\n");
 568                                 goto done;
 569                         }
 570                         owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
 571                         if (!owner_sid ||
 572                             !StringToSid(cli, owner_sid, tok+6)) {
 573                                 printf("Failed to parse owner sid\n");
 574                                 goto done;
 575                         }
 576                         continue;
 577                 }
 578 
 579                 if (strncmp(tok,"GROUP:", 6) == 0) {
 580                         if (grp_sid) {
 581                                 printf("Only specify group once\n");
 582                                 goto done;
 583                         }
 584                         grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
 585                         if (!grp_sid ||
 586                             !StringToSid(cli, grp_sid, tok+6)) {
 587                                 printf("Failed to parse group sid\n");
 588                                 goto done;
 589                         }
 590                         continue;
 591                 }
 592 
 593                 if (strncmp(tok,"ACL:", 4) == 0) {
 594                         SEC_ACE ace;
 595                         if (!parse_ace(cli, &ace, tok+4)) {
 596                                 goto done;
 597                         }
 598                         if(!add_ace(&dacl, &ace)) {
 599                                 printf("Failed to add ACL %s\n", tok);
 600                                 goto done;
 601                         }
 602                         continue;
 603                 }
 604 
 605                 printf("Failed to parse token '%s' in security descriptor,\n", tok);
 606                 goto done;
 607         }
 608 
 609         ret = make_sec_desc(ctx,revision, SEC_DESC_SELF_RELATIVE, owner_sid, grp_sid,
 610                             NULL, dacl, &sd_size);
 611 
 612   done:
 613         SAFE_FREE(grp_sid);
 614         SAFE_FREE(owner_sid);
 615 
 616         return ret;
 617 }
 618 
 619 
 620 /* print a ascii version of a security descriptor on a FILE handle */
 621 static void sec_desc_print(struct cli_state *cli, FILE *f, SEC_DESC *sd)
     /* [<][>][^][v][top][bottom][index][help] */
 622 {
 623         fstring sidstr;
 624         uint32 i;
 625 
 626         fprintf(f, "REVISION:%d\n", sd->revision);
 627         fprintf(f, "CONTROL:0x%x\n", sd->type);
 628 
 629         /* Print owner and group sid */
 630 
 631         if (sd->owner_sid) {
 632                 SidToString(cli, sidstr, sd->owner_sid);
 633         } else {
 634                 fstrcpy(sidstr, "");
 635         }
 636 
 637         fprintf(f, "OWNER:%s\n", sidstr);
 638 
 639         if (sd->group_sid) {
 640                 SidToString(cli, sidstr, sd->group_sid);
 641         } else {
 642                 fstrcpy(sidstr, "");
 643         }
 644 
 645         fprintf(f, "GROUP:%s\n", sidstr);
 646 
 647         /* Print aces */
 648         for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
 649                 SEC_ACE *ace = &sd->dacl->aces[i];
 650                 fprintf(f, "ACL:");
 651                 print_ace(cli, f, ace);
 652                 fprintf(f, "\n");
 653         }
 654 
 655 }
 656 
 657 /***************************************************** 
 658 dump the acls for a file
 659 *******************************************************/
 660 static int cacl_dump(struct cli_state *cli, char *filename)
     /* [<][>][^][v][top][bottom][index][help] */
 661 {
 662         int result = EXIT_FAILED;
 663         int fnum = -1;
 664         SEC_DESC *sd;
 665 
 666         if (test_args) 
 667                 return EXIT_OK;
 668 
 669         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
 670 
 671         if (fnum == -1) {
 672                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
 673                 goto done;
 674         }
 675 
 676         sd = cli_query_secdesc(cli, fnum, talloc_tos());
 677 
 678         if (!sd) {
 679                 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
 680                 goto done;
 681         }
 682 
 683         sec_desc_print(cli, stdout, sd);
 684 
 685         result = EXIT_OK;
 686 
 687 done:
 688         if (fnum != -1)
 689                 cli_close(cli, fnum);
 690 
 691         return result;
 692 }
 693 
 694 /***************************************************** 
 695 Change the ownership or group ownership of a file. Just
 696 because the NT docs say this can't be done :-). JRA.
 697 *******************************************************/
 698 
 699 static int owner_set(struct cli_state *cli, enum chown_mode change_mode, 
     /* [<][>][^][v][top][bottom][index][help] */
 700                         const char *filename, const char *new_username)
 701 {
 702         int fnum;
 703         DOM_SID sid;
 704         SEC_DESC *sd, *old;
 705         size_t sd_size;
 706 
 707         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
 708 
 709         if (fnum == -1) {
 710                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
 711                 return EXIT_FAILED;
 712         }
 713 
 714         if (!StringToSid(cli, &sid, new_username))
 715                 return EXIT_PARSE_ERROR;
 716 
 717         old = cli_query_secdesc(cli, fnum, talloc_tos());
 718 
 719         cli_close(cli, fnum);
 720 
 721         if (!old) {
 722                 printf("owner_set: Failed to query old descriptor\n");
 723                 return EXIT_FAILED;
 724         }
 725 
 726         sd = make_sec_desc(talloc_tos(),old->revision, old->type,
 727                                 (change_mode == REQUEST_CHOWN) ? &sid : NULL,
 728                                 (change_mode == REQUEST_CHGRP) ? &sid : NULL,
 729                            NULL, NULL, &sd_size);
 730 
 731         fnum = cli_nt_create(cli, filename, WRITE_OWNER_ACCESS);
 732 
 733         if (fnum == -1) {
 734                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
 735                 return EXIT_FAILED;
 736         }
 737 
 738         if (!cli_set_secdesc(cli, fnum, sd)) {
 739                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
 740                 cli_close(cli, fnum);
 741                 return EXIT_FAILED;
 742         }
 743 
 744         cli_close(cli, fnum);
 745 
 746         return EXIT_OK;
 747 }
 748 
 749 
 750 /* The MSDN is contradictory over the ordering of ACE entries in an
 751    ACL.  However NT4 gives a "The information may have been modified
 752    by a computer running Windows NT 5.0" if denied ACEs do not appear
 753    before allowed ACEs. At
 754    http://technet.microsoft.com/en-us/library/cc781716.aspx the
 755    canonical order is specified as "Explicit Deny, Explicit Allow,
 756    Inherited ACEs unchanged" */
 757 
 758 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
     /* [<][>][^][v][top][bottom][index][help] */
 759 {
 760         if (sec_ace_equal(ace1, ace2))
 761                 return 0;
 762 
 763         if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
 764                         !(ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
 765                 return 1;
 766         if (!(ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
 767                         (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
 768                 return -1;
 769         if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
 770                         (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
 771                 return ace1 - ace2;
 772 
 773         if (ace1->type != ace2->type)
 774                 return ace2->type - ace1->type;
 775 
 776         if (sid_compare(&ace1->trustee, &ace2->trustee))
 777                 return sid_compare(&ace1->trustee, &ace2->trustee);
 778 
 779         if (ace1->flags != ace2->flags)
 780                 return ace1->flags - ace2->flags;
 781 
 782         if (ace1->access_mask != ace2->access_mask)
 783                 return ace1->access_mask - ace2->access_mask;
 784 
 785         if (ace1->size != ace2->size)
 786                 return ace1->size - ace2->size;
 787 
 788         return memcmp(ace1, ace2, sizeof(SEC_ACE));
 789 }
 790 
 791 static void sort_acl(SEC_ACL *the_acl)
     /* [<][>][^][v][top][bottom][index][help] */
 792 {
 793         uint32 i;
 794         if (!the_acl) return;
 795 
 796         qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]), QSORT_CAST ace_compare);
 797 
 798         for (i=1;i<the_acl->num_aces;) {
 799                 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
 800                         int j;
 801                         for (j=i; j<the_acl->num_aces-1; j++) {
 802                                 the_acl->aces[j] = the_acl->aces[j+1];
 803                         }
 804                         the_acl->num_aces--;
 805                 } else {
 806                         i++;
 807                 }
 808         }
 809 }
 810 
 811 /***************************************************** 
 812 set the ACLs on a file given an ascii description
 813 *******************************************************/
 814 
 815 static int cacl_set(struct cli_state *cli, char *filename, 
     /* [<][>][^][v][top][bottom][index][help] */
 816                     char *the_acl, enum acl_mode mode)
 817 {
 818         int fnum;
 819         SEC_DESC *sd, *old;
 820         uint32 i, j;
 821         size_t sd_size;
 822         int result = EXIT_OK;
 823 
 824         sd = sec_desc_parse(talloc_tos(), cli, the_acl);
 825 
 826         if (!sd) return EXIT_PARSE_ERROR;
 827         if (test_args) return EXIT_OK;
 828 
 829         /* The desired access below is the only one I could find that works
 830            with NT4, W2KP and Samba */
 831 
 832         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
 833 
 834         if (fnum == -1) {
 835                 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
 836                 return EXIT_FAILED;
 837         }
 838 
 839         old = cli_query_secdesc(cli, fnum, talloc_tos());
 840 
 841         if (!old) {
 842                 printf("calc_set: Failed to query old descriptor\n");
 843                 return EXIT_FAILED;
 844         }
 845 
 846         cli_close(cli, fnum);
 847 
 848         /* the logic here is rather more complex than I would like */
 849         switch (mode) {
 850         case SMB_ACL_DELETE:
 851                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
 852                         bool found = False;
 853 
 854                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
 855                                 if (sec_ace_equal(&sd->dacl->aces[i],
 856                                                   &old->dacl->aces[j])) {
 857                                         uint32 k;
 858                                         for (k=j; k<old->dacl->num_aces-1;k++) {
 859                                                 old->dacl->aces[k] = old->dacl->aces[k+1];
 860                                         }
 861                                         old->dacl->num_aces--;
 862                                         found = True;
 863                                         break;
 864                                 }
 865                         }
 866 
 867                         if (!found) {
 868                                 printf("ACL for ACE:");
 869                                 print_ace(cli, stdout, &sd->dacl->aces[i]);
 870                                 printf(" not found\n");
 871                         }
 872                 }
 873                 break;
 874 
 875         case SMB_ACL_MODIFY:
 876                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
 877                         bool found = False;
 878 
 879                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
 880                                 if (sid_equal(&sd->dacl->aces[i].trustee,
 881                                               &old->dacl->aces[j].trustee)) {
 882                                         old->dacl->aces[j] = sd->dacl->aces[i];
 883                                         found = True;
 884                                 }
 885                         }
 886 
 887                         if (!found) {
 888                                 fstring str;
 889 
 890                                 SidToString(cli, str,
 891                                             &sd->dacl->aces[i].trustee);
 892                                 printf("ACL for SID %s not found\n", str);
 893                         }
 894                 }
 895 
 896                 if (sd->owner_sid) {
 897                         old->owner_sid = sd->owner_sid;
 898                 }
 899 
 900                 if (sd->group_sid) {
 901                         old->group_sid = sd->group_sid;
 902                 }
 903 
 904                 break;
 905 
 906         case SMB_ACL_ADD:
 907                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
 908                         add_ace(&old->dacl, &sd->dacl->aces[i]);
 909                 }
 910                 break;
 911 
 912         case SMB_ACL_SET:
 913                 old = sd;
 914                 break;
 915         }
 916 
 917         /* Denied ACE entries must come before allowed ones */
 918         sort_acl(old->dacl);
 919 
 920         /* Create new security descriptor and set it */
 921 
 922         /* We used to just have "WRITE_DAC_ACCESS" without WRITE_OWNER.
 923            But if we're sending an owner, even if it's the same as the one
 924            that already exists then W2K3 insists we open with WRITE_OWNER access.
 925            I need to check that setting a SD with no owner set works against WNT
 926            and W2K. JRA.
 927         */
 928 
 929         sd = make_sec_desc(talloc_tos(),old->revision, old->type,
 930                            old->owner_sid, old->group_sid,
 931                            NULL, old->dacl, &sd_size);
 932 
 933         fnum = cli_nt_create(cli, filename, WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS);
 934 
 935         if (fnum == -1) {
 936                 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
 937                 return EXIT_FAILED;
 938         }
 939 
 940         if (!cli_set_secdesc(cli, fnum, sd)) {
 941                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
 942                 result = EXIT_FAILED;
 943         }
 944 
 945         /* Clean up */
 946 
 947         cli_close(cli, fnum);
 948 
 949         return result;
 950 }
 951 
 952 
 953 /*****************************************************
 954  Return a connection to a server.
 955 *******************************************************/
 956 static struct cli_state *connect_one(struct user_auth_info *auth_info,
     /* [<][>][^][v][top][bottom][index][help] */
 957                                      const char *server, const char *share)
 958 {
 959         struct cli_state *c = NULL;
 960         struct sockaddr_storage ss;
 961         NTSTATUS nt_status;
 962         uint32_t flags = 0;
 963 
 964         zero_sockaddr(&ss);
 965 
 966         if (get_cmdline_auth_info_use_kerberos(auth_info)) {
 967                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
 968                          CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
 969         }
 970 
 971         if (get_cmdline_auth_info_use_machine_account(auth_info) &&
 972             !set_cmdline_auth_info_machine_account_creds(auth_info)) {
 973                 return NULL;
 974         }
 975 
 976         set_cmdline_auth_info_getpass(auth_info);
 977 
 978         nt_status = cli_full_connection(&c, global_myname(), server, 
 979                                 &ss, 0,
 980                                 share, "?????",
 981                                 get_cmdline_auth_info_username(auth_info),
 982                                 lp_workgroup(),
 983                                 get_cmdline_auth_info_password(auth_info),
 984                                 flags,
 985                                 get_cmdline_auth_info_signing_state(auth_info),
 986                                 NULL);
 987         if (!NT_STATUS_IS_OK(nt_status)) {
 988                 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
 989                 return NULL;
 990         }
 991 
 992         if (get_cmdline_auth_info_smb_encrypt(auth_info)) {
 993                 nt_status = cli_cm_force_encryption(c,
 994                                         get_cmdline_auth_info_username(auth_info),
 995                                         get_cmdline_auth_info_password(auth_info),
 996                                         lp_workgroup(),
 997                                         share);
 998                 if (!NT_STATUS_IS_OK(nt_status)) {
 999                         cli_shutdown(c);
1000                         c = NULL;
1001                 }
1002         }
1003 
1004         return c;
1005 }
1006 
1007 /****************************************************************************
1008   main program
1009 ****************************************************************************/
1010  int main(int argc, const char *argv[])
     /* [<][>][^][v][top][bottom][index][help] */
1011 {
1012         char *share;
1013         int opt;
1014         enum acl_mode mode = SMB_ACL_SET;
1015         static char *the_acl = NULL;
1016         enum chown_mode change_mode = REQUEST_NONE;
1017         int result;
1018         char *path;
1019         char *filename = NULL;
1020         poptContext pc;
1021         struct poptOption long_options[] = {
1022                 POPT_AUTOHELP
1023                 { "delete", 'D', POPT_ARG_STRING, NULL, 'D', "Delete an acl", "ACL" },
1024                 { "modify", 'M', POPT_ARG_STRING, NULL, 'M', "Modify an acl", "ACL" },
1025                 { "add", 'a', POPT_ARG_STRING, NULL, 'a', "Add an acl", "ACL" },
1026                 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls", "ACLS" },
1027                 { "chown", 'C', POPT_ARG_STRING, NULL, 'C', "Change ownership of a file", "USERNAME" },
1028                 { "chgrp", 'G', POPT_ARG_STRING, NULL, 'G', "Change group ownership of a file", "GROUPNAME" },
1029                 { "numeric", 0, POPT_ARG_NONE, &numeric, 1, "Don't resolve sids or masks to names" },
1030                 { "test-args", 't', POPT_ARG_NONE, &test_args, 1, "Test arguments"},
1031                 POPT_COMMON_SAMBA
1032                 POPT_COMMON_CONNECTION
1033                 POPT_COMMON_CREDENTIALS
1034                 POPT_TABLEEND
1035         };
1036 
1037         struct cli_state *cli;
1038         TALLOC_CTX *frame = talloc_stackframe();
1039         const char *owner_username = "";
1040         char *server;
1041         struct user_auth_info *auth_info;
1042 
1043         load_case_tables();
1044 
1045 
1046         /* set default debug level to 1 regardless of what smb.conf sets */
1047         setup_logging( "smbcacls", True );
1048         DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
1049         dbf = x_stderr;
1050         x_setbuf( x_stderr, NULL );
1051 
1052         setlinebuf(stdout);
1053 
1054         lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
1055         load_interfaces();
1056 
1057         auth_info = user_auth_info_init(frame);
1058         if (auth_info == NULL) {
1059                 exit(1);
1060         }
1061         popt_common_set_auth_info(auth_info);
1062 
1063         pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
1064 
1065         poptSetOtherOptionHelp(pc, "//server1/share1 filename\nACLs look like: "
1066                 "'ACL:user:[ALLOWED|DENIED]/flags/permissions'");
1067 
1068         while ((opt = poptGetNextOpt(pc)) != -1) {
1069                 switch (opt) {
1070                 case 'S':
1071                         the_acl = smb_xstrdup(poptGetOptArg(pc));
1072                         mode = SMB_ACL_SET;
1073                         break;
1074 
1075                 case 'D':
1076                         the_acl = smb_xstrdup(poptGetOptArg(pc));
1077                         mode = SMB_ACL_DELETE;
1078                         break;
1079 
1080                 case 'M':
1081                         the_acl = smb_xstrdup(poptGetOptArg(pc));
1082                         mode = SMB_ACL_MODIFY;
1083                         break;
1084 
1085                 case 'a':
1086                         the_acl = smb_xstrdup(poptGetOptArg(pc));
1087                         mode = SMB_ACL_ADD;
1088                         break;
1089 
1090                 case 'C':
1091                         owner_username = poptGetOptArg(pc);
1092                         change_mode = REQUEST_CHOWN;
1093                         break;
1094 
1095                 case 'G':
1096                         owner_username = poptGetOptArg(pc);
1097                         change_mode = REQUEST_CHGRP;
1098                         break;
1099                 }
1100         }
1101 
1102         /* Make connection to server */
1103         if(!poptPeekArg(pc)) {
1104                 poptPrintUsage(pc, stderr, 0);
1105                 return -1;
1106         }
1107 
1108         path = talloc_strdup(frame, poptGetArg(pc));
1109         if (!path) {
1110                 return -1;
1111         }
1112 
1113         if(!poptPeekArg(pc)) {
1114                 poptPrintUsage(pc, stderr, 0);
1115                 return -1;
1116         }
1117 
1118         filename = talloc_strdup(frame, poptGetArg(pc));
1119         if (!filename) {
1120                 return -1;
1121         }
1122 
1123         string_replace(path,'/','\\');
1124 
1125         server = talloc_strdup(frame, path+2);
1126         if (!server) {
1127                 return -1;
1128         }
1129         share = strchr_m(server,'\\');
1130         if (!share) {
1131                 printf("Invalid argument: %s\n", share);
1132                 return -1;
1133         }
1134 
1135         *share = 0;
1136         share++;
1137 
1138         if (!test_args) {
1139                 cli = connect_one(auth_info, server, share);
1140                 if (!cli) {
1141                         exit(EXIT_FAILED);
1142                 }
1143         } else {
1144                 exit(0);
1145         }
1146 
1147         string_replace(filename, '/', '\\');
1148         if (filename[0] != '\\') {
1149                 filename = talloc_asprintf(frame,
1150                                 "\\%s",
1151                                 filename);
1152                 if (!filename) {
1153                         return -1;
1154                 }
1155         }
1156 
1157         /* Perform requested action */
1158 
1159         if (change_mode != REQUEST_NONE) {
1160                 result = owner_set(cli, change_mode, filename, owner_username);
1161         } else if (the_acl) {
1162                 result = cacl_set(cli, filename, the_acl, mode);
1163         } else {
1164                 result = cacl_dump(cli, filename);
1165         }
1166 
1167         TALLOC_FREE(frame);
1168 
1169         return result;
1170 }

/* [<][>][^][v][top][bottom][index][help] */