s3: last part of TYPESAFE_QSORT() conversion
[abartlet/samba.git/.git] / source3 / utils / smbcacls.c
index 10b55014c227c2b05d0a7c078fec7a873bfe7d8b..5fd18ff58c2694ea49c4b33575cd247e9e58c3d6 100644 (file)
@@ -23,6 +23,8 @@
 
 #include "includes.h"
 
+extern bool AllowDebugChange;
+
 static int test_args;
 
 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
@@ -68,7 +70,7 @@ static NTSTATUS cli_lsa_lookup_sid(struct cli_state *cli,
                                   char **domain, char **name)
 {
        uint16 orig_cnum = cli->cnum;
-       struct rpc_pipe_client *p;
+       struct rpc_pipe_client *p = NULL;
        struct policy_handle handle;
        NTSTATUS status;
        TALLOC_CTX *frame = talloc_stackframe();
@@ -76,8 +78,9 @@ static NTSTATUS cli_lsa_lookup_sid(struct cli_state *cli,
        char **domains;
        char **names;
 
-       if (!cli_send_tconX(cli, "IPC$", "?????", "", 0)) {
-               return cli_nt_error(cli);
+       status = cli_tcon_andx(cli, "IPC$", "?????", "", 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
        status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
@@ -124,8 +127,9 @@ static NTSTATUS cli_lsa_lookup_name(struct cli_state *cli,
        DOM_SID *sids;
        enum lsa_SidType *types;
 
-       if (!cli_send_tconX(cli, "IPC$", "?????", "", 0)) {
-               return cli_nt_error(cli);
+       status = cli_tcon_andx(cli, "IPC$", "?????", "", 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
        status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
@@ -658,15 +662,14 @@ dump the acls for a file
 static int cacl_dump(struct cli_state *cli, char *filename)
 {
        int result = EXIT_FAILED;
-       int fnum = -1;
+       uint16_t fnum = (uint16_t)-1;
        SEC_DESC *sd;
 
        if (test_args) 
                return EXIT_OK;
 
-       fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
-
-       if (fnum == -1) {
+       if (!NT_STATUS_IS_OK(cli_ntcreate(cli, filename, 0, CREATE_ACCESS_READ, 0,
+                               FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
                printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
                goto done;
        }
@@ -683,7 +686,7 @@ static int cacl_dump(struct cli_state *cli, char *filename)
        result = EXIT_OK;
 
 done:
-       if (fnum != -1)
+       if (fnum != (uint16_t)-1)
                cli_close(cli, fnum);
 
        return result;
@@ -697,14 +700,13 @@ because the NT docs say this can't be done :-). JRA.
 static int owner_set(struct cli_state *cli, enum chown_mode change_mode, 
                        const char *filename, const char *new_username)
 {
-       int fnum;
+       uint16_t fnum;
        DOM_SID sid;
        SEC_DESC *sd, *old;
        size_t sd_size;
 
-       fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
-
-       if (fnum == -1) {
+       if (!NT_STATUS_IS_OK(cli_ntcreate(cli, filename, 0, CREATE_ACCESS_READ, 0,
+                               FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
                printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
                return EXIT_FAILED;
        }
@@ -726,15 +728,16 @@ static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
                                (change_mode == REQUEST_CHGRP) ? &sid : NULL,
                           NULL, NULL, &sd_size);
 
-       fnum = cli_nt_create(cli, filename, WRITE_OWNER_ACCESS);
-
-       if (fnum == -1) {
+       if (!NT_STATUS_IS_OK(cli_ntcreate(cli, filename, 0, WRITE_OWNER_ACCESS, 0,
+                       FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
                printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
                return EXIT_FAILED;
        }
 
        if (!cli_set_secdesc(cli, fnum, sd)) {
                printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
+               cli_close(cli, fnum);
+               return EXIT_FAILED;
        }
 
        cli_close(cli, fnum);
@@ -789,7 +792,7 @@ static void sort_acl(SEC_ACL *the_acl)
        uint32 i;
        if (!the_acl) return;
 
-       qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]), QSORT_CAST ace_compare);
+       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])) {
@@ -811,7 +814,7 @@ set the ACLs on a file given an ascii description
 static int cacl_set(struct cli_state *cli, char *filename, 
                    char *the_acl, enum acl_mode mode)
 {
-       int fnum;
+       uint16_t fnum;
        SEC_DESC *sd, *old;
        uint32 i, j;
        size_t sd_size;
@@ -825,9 +828,8 @@ static int cacl_set(struct cli_state *cli, char *filename,
        /* The desired access below is the only one I could find that works
           with NT4, W2KP and Samba */
 
-       fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
-
-       if (fnum == -1) {
+       if (!NT_STATUS_IS_OK(cli_ntcreate(cli, filename, 0, CREATE_ACCESS_READ, 0,
+                               FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
                printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
                return EXIT_FAILED;
        }
@@ -926,9 +928,8 @@ static int cacl_set(struct cli_state *cli, char *filename,
                           old->owner_sid, old->group_sid,
                           NULL, old->dacl, &sd_size);
 
-       fnum = cli_nt_create(cli, filename, WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS);
-
-       if (fnum == -1) {
+       if (!NT_STATUS_IS_OK(cli_ntcreate(cli, filename, 0, WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS, 0,
+                       FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
                printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
                return EXIT_FAILED;
        }
@@ -949,50 +950,46 @@ static int cacl_set(struct cli_state *cli, char *filename,
 /*****************************************************
  Return a connection to a server.
 *******************************************************/
-static struct cli_state *connect_one(const char *server, const char *share)
+static struct cli_state *connect_one(struct user_auth_info *auth_info,
+                                    const char *server, const char *share)
 {
        struct cli_state *c = NULL;
        struct sockaddr_storage ss;
        NTSTATUS nt_status;
        uint32_t flags = 0;
 
-       zero_addr(&ss);
+       zero_sockaddr(&ss);
 
-       if (get_cmdline_auth_info_use_kerberos()) {
+       if (get_cmdline_auth_info_use_kerberos(auth_info)) {
                flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
                         CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
        }
 
-       if (get_cmdline_auth_info_use_machine_account() &&
-           !set_cmdline_auth_info_machine_account_creds()) {
+       if (get_cmdline_auth_info_use_machine_account(auth_info) &&
+           !set_cmdline_auth_info_machine_account_creds(auth_info)) {
                return NULL;
        }
 
-       if (!get_cmdline_auth_info_got_pass()) {
-               char *pass = getpass("Password: ");
-               if (pass) {
-                       set_cmdline_auth_info_password(pass);
-               }
-       }
+       set_cmdline_auth_info_getpass(auth_info);
 
        nt_status = cli_full_connection(&c, global_myname(), server, 
                                &ss, 0,
                                share, "?????",
-                               get_cmdline_auth_info_username(),
+                               get_cmdline_auth_info_username(auth_info),
                                lp_workgroup(),
-                               get_cmdline_auth_info_password(),
+                               get_cmdline_auth_info_password(auth_info),
                                flags,
-                               get_cmdline_auth_info_signing_state(),
+                               get_cmdline_auth_info_signing_state(auth_info),
                                NULL);
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
                return NULL;
        }
 
-       if (get_cmdline_auth_info_smb_encrypt()) {
+       if (get_cmdline_auth_info_smb_encrypt(auth_info)) {
                nt_status = cli_cm_force_encryption(c,
-                                       get_cmdline_auth_info_username(),
-                                       get_cmdline_auth_info_password(),
+                                       get_cmdline_auth_info_username(auth_info),
+                                       get_cmdline_auth_info_password(auth_info),
                                        lp_workgroup(),
                                        share);
                 if (!NT_STATUS_IS_OK(nt_status)) {
@@ -1038,6 +1035,7 @@ static struct cli_state *connect_one(const char *server, const char *share)
        TALLOC_CTX *frame = talloc_stackframe();
        const char *owner_username = "";
        char *server;
+       struct user_auth_info *auth_info;
 
        load_case_tables();
 
@@ -1047,12 +1045,19 @@ static struct cli_state *connect_one(const char *server, const char *share)
        DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
        dbf = x_stderr;
        x_setbuf( x_stderr, NULL );
+       AllowDebugChange = false;
 
        setlinebuf(stdout);
 
        lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
        load_interfaces();
 
+       auth_info = user_auth_info_init(frame);
+       if (auth_info == NULL) {
+               exit(1);
+       }
+       popt_common_set_auth_info(auth_info);
+
        pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
 
        poptSetOtherOptionHelp(pc, "//server1/share1 filename\nACLs look like: "
@@ -1129,7 +1134,7 @@ static struct cli_state *connect_one(const char *server, const char *share)
        share++;
 
        if (!test_args) {
-               cli = connect_one(server, share);
+               cli = connect_one(auth_info, server, share);
                if (!cli) {
                        exit(EXIT_FAILED);
                }