smbcacls: Move StringToSid to common file
[samba.git] / source3 / utils / smbcacls.c
index 20ce73ddc718a231ceda14441ed35e7c699f3334..778d30d4548b5b9a2d18d1990f767348124494a0 100644 (file)
 #include "libsmb/clirap.h"
 #include "passdb/machine_sid.h"
 #include "../librpc/gen_ndr/ndr_lsa_c.h"
+#include "util_sd.h"
 
 static int test_args;
 
 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
 
-/* numeric is set when the user wants numeric SIDs and ACEs rather
-   than going via LSA calls to resolve them */
-static int numeric;
-
 static int sddl;
 static int query_sec_info = -1;
 static int set_sec_info = -1;
@@ -58,126 +55,23 @@ struct perm_value {
 /* These values discovered by inspection */
 
 static const struct perm_value special_values[] = {
-       { "R", 0x00120089 },
-       { "W", 0x00120116 },
-       { "X", 0x001200a0 },
-       { "D", 0x00010000 },
-       { "P", 0x00040000 },
-       { "O", 0x00080000 },
+       { "R", SEC_RIGHTS_FILE_READ },
+       { "W", SEC_RIGHTS_FILE_WRITE },
+       { "X", SEC_RIGHTS_FILE_EXECUTE },
+       { "D", SEC_STD_DELETE },
+       { "P", SEC_STD_WRITE_DAC },
+       { "O", SEC_STD_WRITE_OWNER },
        { NULL, 0 },
 };
 
 static const struct perm_value standard_values[] = {
-       { "READ",   0x001200a9 },
-       { "CHANGE", 0x001301bf },
-       { "FULL",   0x001f01ff },
+       { "READ",   SEC_RIGHTS_DIR_READ|SEC_DIR_TRAVERSE },
+       { "CHANGE", SEC_RIGHTS_DIR_READ|SEC_STD_DELETE|\
+         SEC_RIGHTS_DIR_WRITE|SEC_DIR_TRAVERSE },
+       { "FULL",   SEC_RIGHTS_DIR_ALL },
        { NULL, 0 },
 };
 
-/* Open cli connection and policy handle */
-
-static NTSTATUS cli_lsa_lookup_sid(struct cli_state *cli,
-                                  const struct dom_sid *sid,
-                                  TALLOC_CTX *mem_ctx,
-                                  enum lsa_SidType *type,
-                                  char **domain, char **name)
-{
-       uint16 orig_cnum = cli_state_get_tid(cli);
-       struct rpc_pipe_client *p = NULL;
-       struct policy_handle handle;
-       NTSTATUS status;
-       TALLOC_CTX *frame = talloc_stackframe();
-       enum lsa_SidType *types;
-       char **domains;
-       char **names;
-
-       status = cli_tree_connect(cli, "IPC$", "?????", "", 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto tcon_fail;
-       }
-
-       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
-                                         &p);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto fail;
-       }
-
-       status = rpccli_lsa_open_policy(p, talloc_tos(), True,
-                                       GENERIC_EXECUTE_ACCESS, &handle);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto fail;
-       }
-
-       status = rpccli_lsa_lookup_sids(p, talloc_tos(), &handle, 1, sid,
-                                       &domains, &names, &types);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto fail;
-       }
-
-       *type = types[0];
-       *domain = talloc_move(mem_ctx, &domains[0]);
-       *name = talloc_move(mem_ctx, &names[0]);
-
-       status = NT_STATUS_OK;
- fail:
-       TALLOC_FREE(p);
-       cli_tdis(cli);
- tcon_fail:
-       cli_state_set_tid(cli, orig_cnum);
-       TALLOC_FREE(frame);
-       return status;
-}
-
-static NTSTATUS cli_lsa_lookup_name(struct cli_state *cli,
-                                   const char *name,
-                                   enum lsa_SidType *type,
-                                   struct dom_sid *sid)
-{
-       uint16 orig_cnum = cli_state_get_tid(cli);
-       struct rpc_pipe_client *p;
-       struct policy_handle handle;
-       NTSTATUS status;
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct dom_sid *sids;
-       enum lsa_SidType *types;
-
-       status = cli_tree_connect(cli, "IPC$", "?????", "", 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto tcon_fail;
-       }
-
-       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
-                                         &p);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto fail;
-       }
-
-       status = rpccli_lsa_open_policy(p, talloc_tos(), True,
-                                       GENERIC_EXECUTE_ACCESS, &handle);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto fail;
-       }
-
-       status = rpccli_lsa_lookup_names(p, talloc_tos(), &handle, 1, &name,
-                                        NULL, 1, &sids, &types);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto fail;
-       }
-
-       *type = types[0];
-       *sid = sids[0];
-
-       status = NT_STATUS_OK;
- fail:
-       TALLOC_FREE(p);
-       cli_tdis(cli);
- tcon_fail:
-       cli_state_set_tid(cli, orig_cnum);
-       TALLOC_FREE(frame);
-       return status;
-}
-
-
 static NTSTATUS cli_lsa_lookup_domain_sid(struct cli_state *cli,
                                          struct dom_sid *sid)
 {
@@ -253,48 +147,6 @@ static struct dom_sid *get_domain_sid(struct cli_state *cli)
        return sid;
 }
 
-
-/* convert a SID to a string, either numeric or username/group */
-static void SidToString(struct cli_state *cli, fstring str, const struct dom_sid *sid)
-{
-       char *domain = NULL;
-       char *name = NULL;
-       enum lsa_SidType type;
-       NTSTATUS status;
-
-       sid_to_fstring(str, sid);
-
-       if (numeric) {
-               return;
-       }
-
-       status = cli_lsa_lookup_sid(cli, sid, talloc_tos(), &type,
-                                   &domain, &name);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               return;
-       }
-
-       if (*domain) {
-               slprintf(str, sizeof(fstring) - 1, "%s%s%s",
-                       domain, lp_winbind_separator(), name);
-       } else {
-               fstrcpy(str, name);
-       }
-}
-
-/* convert a string to a SID, either numeric or username/group */
-static bool StringToSid(struct cli_state *cli, struct dom_sid *sid, const char *str)
-{
-       enum lsa_SidType type;
-
-       if (string_to_sid(sid, str)) {
-               return true;
-       }
-
-       return NT_STATUS_IS_OK(cli_lsa_lookup_name(cli, str, &type, sid));
-}
-
 static void print_ace_flags(FILE *f, uint8_t flags)
 {
        char *str = talloc_strdup(NULL, "");
@@ -356,14 +208,15 @@ static void print_ace_flags(FILE *f, uint8_t flags)
 }
 
 /* print an ACE on a FILE, using either numeric or ascii representation */
-static void print_ace(struct cli_state *cli, FILE *f, struct security_ace *ace)
+static void print_ace(struct cli_state *cli, FILE *f, struct security_ace *ace,
+                     bool numeric)
 {
        const struct perm_value *v;
        fstring sidstr;
        int do_print = 0;
        uint32 got_mask;
 
-       SidToString(cli, sidstr, &ace->trustee);
+       SidToString(cli, sidstr, &ace->trustee, numeric);
 
        fprintf(f, "%s:", sidstr);
 
@@ -489,7 +342,7 @@ static bool parse_ace(struct cli_state *cli, struct security_ace *ace,
        p++;
        /* Try to parse numeric form */
 
-       if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
+       if (sscanf(p, "%u/%u/%u", &atype, &aflags, &amask) == 3 &&
            StringToSid(cli, &sid, str)) {
                goto done;
        }
@@ -552,7 +405,7 @@ static bool parse_ace(struct cli_state *cli, struct security_ace *ace,
                        return False;
                }
        } else {
-               if (!sscanf(tok, "%i", &aflags)) {
+               if (!sscanf(tok, "%u", &aflags)) {
                        printf("ACE '%s': bad integer flags entry at '%s'\n",
                                orig_str, tok);
                        SAFE_FREE(str);
@@ -570,7 +423,7 @@ static bool parse_ace(struct cli_state *cli, struct security_ace *ace,
        }
 
        if (strncmp(tok, "0x", 2) == 0) {
-               if (sscanf(tok, "%i", &amask) != 1) {
+               if (sscanf(tok, "%u", &amask) != 1) {
                        printf("ACE '%s': bad hex number at '%s'\n",
                                orig_str, tok);
                        SAFE_FREE(str);
@@ -739,7 +592,7 @@ static const struct {
        {SEC_DESC_SELF_RELATIVE ,        "SR", "Self Relative"},
 };
 
-static void print_acl_ctrl(FILE *file, uint16_t ctrl)
+static void print_acl_ctrl(FILE *file, uint16_t ctrl, bool numeric)
 {
        int i;
        const char* separator = "";
@@ -760,18 +613,19 @@ static void print_acl_ctrl(FILE *file, uint16_t ctrl)
 }
 
 /* print a ascii version of a security descriptor on a FILE handle */
-static void sec_desc_print(struct cli_state *cli, FILE *f, struct security_descriptor *sd)
+static void sec_desc_print(struct cli_state *cli, FILE *f,
+                          struct security_descriptor *sd, bool numeric)
 {
        fstring sidstr;
        uint32 i;
 
        fprintf(f, "REVISION:%d\n", sd->revision);
-       print_acl_ctrl(f, sd->type);
+       print_acl_ctrl(f, sd->type, numeric);
 
        /* Print owner and group sid */
 
        if (sd->owner_sid) {
-               SidToString(cli, sidstr, sd->owner_sid);
+               SidToString(cli, sidstr, sd->owner_sid, numeric);
        } else {
                fstrcpy(sidstr, "");
        }
@@ -779,7 +633,7 @@ static void sec_desc_print(struct cli_state *cli, FILE *f, struct security_descr
        fprintf(f, "OWNER:%s\n", sidstr);
 
        if (sd->group_sid) {
-               SidToString(cli, sidstr, sd->group_sid);
+               SidToString(cli, sidstr, sd->group_sid, numeric);
        } else {
                fstrcpy(sidstr, "");
        }
@@ -790,7 +644,7 @@ static void sec_desc_print(struct cli_state *cli, FILE *f, struct security_descr
        for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
                struct security_ace *ace = &sd->dacl->aces[i];
                fprintf(f, "ACL:");
-               print_ace(cli, f, ace);
+               print_ace(cli, f, ace, numeric);
                fprintf(f, "\n");
        }
 
@@ -810,7 +664,7 @@ static uint16 get_fileinfo(struct cli_state *cli, const char *filename)
 
        status = cli_ntcreate(cli, filename, 0, CREATE_ACCESS_READ,
                              0, FILE_SHARE_READ|FILE_SHARE_WRITE,
-                             FILE_OPEN, 0x0, 0x0, &fnum);
+                             FILE_OPEN, 0x0, 0x0, &fnum, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("Failed to open %s: %s\n", filename, nt_errstr(status));
                return 0;
@@ -858,7 +712,7 @@ static struct security_descriptor *get_secdesc(struct cli_state *cli, const char
 
        status = cli_ntcreate(cli, filename, 0, desired_access,
                              0, FILE_SHARE_READ|FILE_SHARE_WRITE,
-                             FILE_OPEN, 0x0, 0x0, &fnum);
+                             FILE_OPEN, 0x0, 0x0, &fnum, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("Failed to open %s: %s\n", filename, nt_errstr(status));
                return NULL;
@@ -922,7 +776,7 @@ static bool set_secdesc(struct cli_state *cli, const char *filename,
        status = cli_ntcreate(cli, filename, 0,
                              desired_access,
                              0, FILE_SHARE_READ|FILE_SHARE_WRITE,
-                             FILE_OPEN, 0x0, 0x0, &fnum);
+                             FILE_OPEN, 0x0, 0x0, &fnum, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("Failed to open %s: %s\n", filename, nt_errstr(status));
                return false;
@@ -942,7 +796,7 @@ static bool set_secdesc(struct cli_state *cli, const char *filename,
 /*****************************************************
 dump the acls for a file
 *******************************************************/
-static int cacl_dump(struct cli_state *cli, const char *filename)
+static int cacl_dump(struct cli_state *cli, const char *filename, bool numeric)
 {
        struct security_descriptor *sd;
 
@@ -963,7 +817,7 @@ static int cacl_dump(struct cli_state *cli, const char *filename)
                printf("%s\n", str);
                TALLOC_FREE(str);
        } else {
-               sec_desc_print(cli, stdout, sd);
+               sec_desc_print(cli, stdout, sd, numeric);
        }
 
        return EXIT_OK;
@@ -990,7 +844,7 @@ static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
                return EXIT_FAILED;
        }
 
-       sd = make_sec_desc(talloc_tos(),old->revision, old->type,
+       sd = make_sec_desc(talloc_tos(),old->revision, SEC_DESC_SELF_RELATIVE,
                                (change_mode == REQUEST_CHOWN) ? &sid : NULL,
                                (change_mode == REQUEST_CHGRP) ? &sid : NULL,
                           NULL, NULL, &sd_size);
@@ -1013,7 +867,7 @@ static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
 
 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
 {
-       if (sec_ace_equal(ace1, ace2))
+       if (security_ace_equal(ace1, ace2))
                return 0;
 
        if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
@@ -1052,7 +906,8 @@ static void sort_acl(struct security_acl *the_acl)
        TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
 
        for (i=1;i<the_acl->num_aces;) {
-               if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
+               if (security_ace_equal(&the_acl->aces[i-1],
+                                      &the_acl->aces[i])) {
                        int j;
                        for (j=i; j<the_acl->num_aces-1; j++) {
                                the_acl->aces[j] = the_acl->aces[j+1];
@@ -1069,7 +924,7 @@ set the ACLs on a file given an ascii description
 *******************************************************/
 
 static int cacl_set(struct cli_state *cli, const char *filename,
-                   char *the_acl, enum acl_mode mode)
+                   char *the_acl, enum acl_mode mode, bool numeric)
 {
        struct security_descriptor *sd, *old;
        uint32 i, j;
@@ -1098,8 +953,8 @@ static int cacl_set(struct cli_state *cli, const char *filename,
                        bool found = False;
 
                        for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
-                               if (sec_ace_equal(&sd->dacl->aces[i],
-                                                 &old->dacl->aces[j])) {
+                               if (security_ace_equal(&sd->dacl->aces[i],
+                                                      &old->dacl->aces[j])) {
                                        uint32 k;
                                        for (k=j; k<old->dacl->num_aces-1;k++) {
                                                old->dacl->aces[k] = old->dacl->aces[k+1];
@@ -1112,7 +967,8 @@ static int cacl_set(struct cli_state *cli, const char *filename,
 
                        if (!found) {
                                printf("ACL for ACE:");
-                               print_ace(cli, stdout, &sd->dacl->aces[i]);
+                               print_ace(cli, stdout, &sd->dacl->aces[i],
+                                         numeric);
                                printf(" not found\n");
                        }
                }
@@ -1134,7 +990,8 @@ static int cacl_set(struct cli_state *cli, const char *filename,
                                fstring str;
 
                                SidToString(cli, str,
-                                           &sd->dacl->aces[i].trustee);
+                                           &sd->dacl->aces[i].trustee,
+                                           numeric);
                                printf("ACL for SID %s not found\n", str);
                        }
                }
@@ -1355,8 +1212,9 @@ static struct cli_state *connect_one(struct user_auth_info *auth_info,
 /****************************************************************************
   main program
 ****************************************************************************/
- int main(int argc, const char *argv[])
+int main(int argc, char *argv[])
 {
+       const char **argv_const = discard_const_p(const char *, argv);
        char *share;
        int opt;
        enum acl_mode mode = SMB_ACL_SET;
@@ -1366,6 +1224,10 @@ static struct cli_state *connect_one(struct user_auth_info *auth_info,
        char *path;
        char *filename = NULL;
        poptContext pc;
+       /* numeric is set when the user wants numeric SIDs and ACEs rather
+          than going via LSA calls to resolve them */
+       int numeric;
+
        struct poptOption long_options[] = {
                POPT_AUTOHELP
                { "delete", 'D', POPT_ARG_STRING, NULL, 'D', "Delete an acl", "ACL" },
@@ -1398,7 +1260,7 @@ static struct cli_state *connect_one(struct user_auth_info *auth_info,
        char *server;
        struct user_auth_info *auth_info;
 
-       load_case_tables();
+       smb_init_locale();
 
        /* set default debug level to 1 regardless of what smb.conf sets */
        setup_logging( "smbcacls", DEBUG_STDERR);
@@ -1406,8 +1268,6 @@ static struct cli_state *connect_one(struct user_auth_info *auth_info,
 
        setlinebuf(stdout);
 
-       lp_load_global(get_dyn_CONFIGFILE());
-       load_interfaces();
 
        auth_info = user_auth_info_init(frame);
        if (auth_info == NULL) {
@@ -1415,7 +1275,7 @@ static struct cli_state *connect_one(struct user_auth_info *auth_info,
        }
        popt_common_set_auth_info(auth_info);
 
-       pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
+       pc = poptGetContext("smbcacls", argc, argv_const, long_options, 0);
 
        poptSetOtherOptionHelp(pc, "//server1/share1 filename\nACLs look like: "
                "'ACL:user:[ALLOWED|DENIED]/flags/permissions'");
@@ -1478,6 +1338,9 @@ static struct cli_state *connect_one(struct user_auth_info *auth_info,
                return -1;
        }
 
+       lp_load_global(get_dyn_CONFIGFILE());
+       load_interfaces();
+
        filename = talloc_strdup(frame, poptGetArg(pc));
        if (!filename) {
                return -1;
@@ -1527,9 +1390,9 @@ static struct cli_state *connect_one(struct user_auth_info *auth_info,
        } else if (change_mode != REQUEST_NONE) {
                result = owner_set(cli, change_mode, filename, owner_username);
        } else if (the_acl) {
-               result = cacl_set(cli, filename, the_acl, mode);
+               result = cacl_set(cli, filename, the_acl, mode, numeric);
        } else {
-               result = cacl_dump(cli, filename);
+               result = cacl_dump(cli, filename, numeric);
        }
 
        TALLOC_FREE(frame);