I should have done this years ago...
[ira/wip.git] / source3 / utils / net_rpc.c
index 922fc027e6c8640e1d0a8eb0299aedec9f03e6ad..eef3adbcb81b35a8582ba5e9f8c43f3212042611 100644 (file)
@@ -48,33 +48,20 @@ typedef NTSTATUS (*rpc_command_fn)(const DOM_SID *, struct cli_state *, TALLOC_C
  * @return The Domain SID of the remote machine.
  **/
 
-static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli)
+static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx)
 {
        DOM_SID *domain_sid;
        POLICY_HND pol;
        NTSTATUS result = NT_STATUS_OK;
        uint32 info_class = 5;
-       fstring domain_name;
-       TALLOC_CTX *mem_ctx;
+        char *domain_name;
        
-       if (!(domain_sid = malloc(sizeof(DOM_SID)))){
-               DEBUG(0,("net_get_remote_domain_sid: malloc returned NULL!\n"));
-               goto error;
-       }
-           
-       if (!(mem_ctx=talloc_init("net_get_remote_domain_sid")))
-       {
-               DEBUG(0,("net_get_remote_domain_sid: talloc_init returned NULL!\n"));
-               goto error;
-       }
-
-
        if (!cli_nt_session_open (cli, PI_LSARPC)) {
                fprintf(stderr, "could not initialise lsa pipe\n");
                goto error;
        }
        
-       result = cli_lsa_open_policy(cli, mem_ctx, True, 
+       result = cli_lsa_open_policy(cli, mem_ctx, False, 
                                     SEC_RIGHTS_MAXIMUM_ALLOWED,
                                     &pol);
        if (!NT_STATUS_IS_OK(result)) {
@@ -82,25 +69,22 @@ static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli)
        }
 
        result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class, 
-                                          domain_name, domain_sid);
+                                          &domain_name, &domain_sid);
        if (!NT_STATUS_IS_OK(result)) {
-               goto error;
+ error:
+               fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
+
+               if (!NT_STATUS_IS_OK(result)) {
+                       fprintf(stderr, "error: %s\n", nt_errstr(result));
+               }
+
+               exit(1);
        }
 
        cli_lsa_close(cli, mem_ctx, &pol);
        cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
 
        return domain_sid;
-
- error:
-       fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
-
-       if (!NT_STATUS_IS_OK(result)) {
-               fprintf(stderr, "error: %s\n", nt_errstr(result));
-       }
-
-       exit(1);
 }
 
 /**
@@ -134,8 +118,6 @@ static int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int co
                return -1;
        }
 
-       domain_sid = net_get_remote_domain_sid(cli);
-
        /* Create mem_ctx */
        
        if (!(mem_ctx = talloc_init("run_rpc_command"))) {
@@ -144,6 +126,8 @@ static int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int co
                return -1;
        }
        
+       domain_sid = net_get_remote_domain_sid(cli, mem_ctx);
+
        if (!cli_nt_session_open(cli, pipe_idx)) {
                DEBUG(0, ("Could not initialise pipe\n"));
        }
@@ -205,7 +189,7 @@ static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, struct cl
  * @return A shell status integer (0 for success)
  **/
 
-static int rpc_changetrustpw(int argc, const char **argv) 
+int net_rpc_changetrustpw(int argc, const char **argv) 
 {
        return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_changetrustpw_internals,
                               argc, argv);
@@ -220,7 +204,7 @@ static int rpc_changetrustpw(int argc, const char **argv)
  *
  * This uses 'machinename' as the inital password, and changes it. 
  *
- * The password should be created with 'server manager' or eqiv first.
+ * The password should be created with 'server manager' or equiv first.
  *
  * All parameters are provided by the run_rpc_command function, except for
  * argc, argv which are passes through. 
@@ -235,15 +219,27 @@ static int rpc_changetrustpw(int argc, const char **argv)
  * @return Normal NTSTATUS return.
  **/
 
-static NTSTATUS rpc_join_oldstyle_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx, 
-                                      int argc, const char **argv) {
+static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid, struct cli_state *cli, 
+                                           TALLOC_CTX *mem_ctx, 
+                                           int argc, const char **argv) {
        
        fstring trust_passwd;
        unsigned char orig_trust_passwd_hash[16];
        NTSTATUS result;
+       uint32 sec_channel_type;
 
+       /* 
+          check what type of join - if the user want's to join as
+          a BDC, the server must agree that we are a BDC.
+       */
+       if (argc >= 0) {
+               sec_channel_type = get_sec_channel_type(argv[0]);
+       } else {
+               sec_channel_type = get_sec_channel_type(NULL);
+       }
+       
        fstrcpy(trust_passwd, global_myname());
-       strlower(trust_passwd);
+       strlower_m(trust_passwd);
 
        /*
         * Machine names can be 15 characters, but the max length on
@@ -254,10 +250,18 @@ static NTSTATUS rpc_join_oldstyle_internals(const DOM_SID *domain_sid, struct cl
 
        E_md4hash(trust_passwd, orig_trust_passwd_hash);
 
-       result = trust_pw_change_and_store_it(cli, mem_ctx, orig_trust_passwd_hash);
+       result = trust_pw_change_and_store_it(cli, mem_ctx, opt_target_workgroup,
+                                             orig_trust_passwd_hash,
+                                             sec_channel_type);
 
        if (NT_STATUS_IS_OK(result))
-               printf("Joined domain %s.\n",lp_workgroup());
+               printf("Joined domain %s.\n",opt_target_workgroup);
+
+
+       if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
+               DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
+               result = NT_STATUS_UNSUCCESSFUL;
+       }
 
        return result;
 }
@@ -272,12 +276,37 @@ static NTSTATUS rpc_join_oldstyle_internals(const DOM_SID *domain_sid, struct cl
  * @return A shell status integer (0 for success)
  **/
 
-static int net_rpc_join_oldstyle(int argc, const char **argv) 
+static int net_rpc_perform_oldjoin(int argc, const char **argv)
 {
-       return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_join_oldstyle_internals,
+       return run_rpc_command(NULL, PI_NETLOGON, 
+                              NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, 
+                              rpc_oldjoin_internals,
                               argc, argv);
 }
 
+/** 
+ * Join a domain, the old way.  This function exists to allow
+ * the message to be displayed when oldjoin was explicitly 
+ * requested, but not when it was implied by "net rpc join"
+ *
+ * @param argc  Standard main() style argc
+ * @param argc  Standard main() style argv.  Initial components are already
+ *              stripped
+ *
+ * @return A shell status integer (0 for success)
+ **/
+
+static int net_rpc_oldjoin(int argc, const char **argv) 
+{
+       int rc = net_rpc_perform_oldjoin(argc, argv);
+
+       if (rc) {
+               d_printf("Failed to join domain\n");
+       }
+
+       return rc;
+}
+
 /** 
  * Basic usage function for 'net rpc join'
  * @param argc  Standard main() style argc
@@ -287,11 +316,13 @@ static int net_rpc_join_oldstyle(int argc, const char **argv)
 
 static int rpc_join_usage(int argc, const char **argv) 
 {      
-       d_printf("net rpc join -U <username>[%%password] [options]\n"\
+       d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
                 "\t to join a domain with admin username & password\n"\
-                "\t\t password will be prompted if none is specified\n");
-       d_printf("net rpc join [options except -U]\n"\
-                "\t to join a domain created in server manager\n\n\n");
+                "\t\t password will be prompted if needed and none is specified\n"\
+                "\t <type> can be (default MEMBER)\n"\
+                "\t\t BDC - Join as a BDC\n"\
+                "\t\t PDC - Join as a PDC\n"\
+                "\t\t MEMBER - Join as a MEMBER server\n");
 
        net_common_flags_usage(argc, argv);
        return -1;
@@ -305,25 +336,16 @@ static int rpc_join_usage(int argc, const char **argv)
  *
  * Main 'net_rpc_join()' (where the admain username/password is used) is 
  * in net_rpc_join.c
- * Assume if a -U is specified, it's the new style, otherwise it's the
- * old style.  If 'oldstyle' is specfied explicity, do it and don't prompt.
+ * Try to just change the password, but if that doesn't work, use/prompt
+ * for a username/password.
  **/
 
 int net_rpc_join(int argc, const char **argv) 
 {
-       struct functable func[] = {
-               {"oldstyle", net_rpc_join_oldstyle},
-               {NULL, NULL}
-       };
-
-       if (argc == 0) {
-               if ((net_rpc_join_oldstyle(argc, argv) == 0))
-                       return 0;
-               
-               return net_rpc_join_newstyle(argc, argv);
-       }
-
-       return net_run_function(argc, argv, func, rpc_join_usage);
+       if ((net_rpc_perform_oldjoin(argc, argv) == 0))
+               return 0;
+       
+       return net_rpc_join_newstyle(argc, argv);
 }
 
 
@@ -332,7 +354,7 @@ int net_rpc_join(int argc, const char **argv)
  * display info about a rpc domain
  *
  * All parameters are provided by the run_rpc_command function, except for
- * argc, argv which are passes through. 
+ * argc, argv which are passed through. 
  *
  * @param domain_sid The domain sid acquired from the remote server
  * @param cli A cli_state connected to the server.
@@ -655,6 +677,135 @@ static int rpc_user_delete(int argc, const char **argv)
                               argc, argv);
 }
 
+/** 
+ * Set a password for a user on a remote RPC server
+ *
+ * All parameters are provided by the run_rpc_command function, except for
+ * argc, argv which are passes through. 
+ *
+ * @param domain_sid The domain sid acquired from the remote server
+ * @param cli A cli_state connected to the server.
+ * @param mem_ctx Talloc context, destoyed on completion of the function.
+ * @param argc  Standard main() style argc
+ * @param argv  Standard main() style argv.  Initial components are already
+ *              stripped
+ *
+ * @return Normal NTSTATUS return.
+ **/
+
+static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid, 
+                                           struct cli_state *cli, 
+                                           TALLOC_CTX *mem_ctx, 
+                                           int argc, const char **argv)
+{
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       POLICY_HND connect_pol, domain_pol, user_pol;
+       SAM_USERINFO_CTR ctr;
+       SAM_USER_INFO_24 p24;
+       uchar pwbuf[516];
+       const char *user;
+       const char *new_password;
+       char *prompt = NULL;
+
+       if (argc < 1) {
+               d_printf("User must be specified\n");
+               rpc_user_usage(argc, argv);
+               return NT_STATUS_OK;
+       }
+       
+       user = argv[0];
+
+       if (argv[1]) {
+               new_password = argv[1];
+       } else {
+               asprintf(&prompt, "Enter new password for %s:", user);
+               new_password = getpass(prompt);
+               SAFE_FREE(prompt);
+       }
+
+       /* Get sam policy and domain handles */
+
+       result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
+                                 &connect_pol);
+
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+       result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
+                                     MAXIMUM_ALLOWED_ACCESS,
+                                     domain_sid, &domain_pol);
+
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+       /* Get handle on user */
+
+       {
+               uint32 *user_rids, num_rids, *name_types;
+               uint32 flags = 0x000003e8; /* Unknown */
+
+               result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
+                                              flags, 1, &user,
+                                              &num_rids, &user_rids,
+                                              &name_types);
+
+               if (!NT_STATUS_IS_OK(result)) {
+                       goto done;
+               }
+
+               result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
+                                           MAXIMUM_ALLOWED_ACCESS,
+                                           user_rids[0], &user_pol);
+
+               if (!NT_STATUS_IS_OK(result)) {
+                       goto done;
+               }
+       }
+
+       /* Set password on account */
+
+       ZERO_STRUCT(ctr);
+       ZERO_STRUCT(p24);
+
+       encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
+
+       init_sam_user_info24(&p24, (char *)pwbuf,24);
+
+       ctr.switch_value = 24;
+       ctr.info.id24 = &p24;
+
+       result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24, 
+                                      &cli->user_session_key, &ctr);
+
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+       /* Display results */
+
+ done:
+       return result;
+
+}      
+
+/** 
+ * Set a user's password on a remote RPC server
+ *
+ * @param argc  Standard main() style argc
+ * @param argv  Standard main() style argv.  Initial components are already
+ *              stripped
+ *
+ * @return A shell status integer (0 for success)
+ **/
+
+static int rpc_user_password(int argc, const char **argv) 
+{
+       return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
+                              argc, argv);
+}
+
 /** 
  * List user's groups on a remote RPC server
  *
@@ -825,11 +976,11 @@ rpc_user_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
                                unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
                        
                        if (opt_long_list_entries)
-                               printf("%-21.21s %-50.50s\n", user, desc);
+                               printf("%-21.21s %s\n", user, desc);
                        else
                                printf("%s\n", user);
                }
-       } while (!NT_STATUS_IS_OK(result));
+       } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
 
  done:
        return result;
@@ -848,6 +999,7 @@ int net_rpc_user(int argc, const char **argv)
                {"add", rpc_user_add},
                {"info", rpc_user_info},
                {"delete", rpc_user_delete},
+               {"password", rpc_user_password},
                {NULL, NULL}
        };
        
@@ -900,9 +1052,29 @@ rpc_group_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
 {
        POLICY_HND connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       uint32 start_idx=0, max_entries=250, num_entries, i;
+       uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
        struct acct_info *groups;
        DOM_SID global_sid_Builtin;
+       BOOL global = False;
+       BOOL local = False;
+       BOOL builtin = False;
+
+       if (argc == 0) {
+               global = True;
+               local = True;
+               builtin = True;
+       }
+
+       for (i=0; i<argc; i++) {
+               if (strequal(argv[i], "global"))
+                       global = True;
+
+               if (strequal(argv[i], "local"))
+                       local = True;
+
+               if (strequal(argv[i], "builtin"))
+                       builtin = True;
+       }
 
        string_to_sid(&global_sid_Builtin, "S-1-5-32");
 
@@ -928,34 +1100,79 @@ rpc_group_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
                d_printf("\nGroup name            Comment"\
                         "\n-----------------------------\n");
        do {
-               result = cli_samr_enum_dom_groups(cli, mem_ctx, &domain_pol,
-                                                 &start_idx, max_entries,
-                                                 &groups, &num_entries);
+               SAM_DISPINFO_CTR ctr;
+               SAM_DISPINFO_3 info3;
+               uint32 max_size;
+
+               ZERO_STRUCT(ctr);
+               ZERO_STRUCT(info3);
+               ctr.sam.info3 = &info3;
+
+               if (!global) break;
+
+               get_query_dispinfo_params(
+                       loop_count, &max_entries, &max_size);
+
+               result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
+                                                &start_idx, 3, &num_entries,
+                                                max_entries, max_size, &ctr);
                                                 
                for (i = 0; i < num_entries; i++) {
+
+                       fstring group, desc;
+
+                       unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
+                       unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
+                       
                        if (opt_long_list_entries)
-                               printf("%-21.21s %-50.50s\n", 
-                                      groups[i].acct_name,
-                                      groups[i].acct_desc);
+                               printf("%-21.21s %-50.50s\n",
+                                      group, desc);
                        else
-                               printf("%-21.21s\n", groups[i].acct_name);
+                               printf("%s\n", group);
                }
-       } while (!NT_STATUS_IS_OK(result));
+       } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
        /* query domain aliases */
+       start_idx = 0;
        do {
+               if (!local) break;
+
                result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
                                                  &start_idx, max_entries,
                                                  &groups, &num_entries);
-                                                
+
                for (i = 0; i < num_entries; i++) {
-                       if (opt_long_list_entries)
+
+                       char *description = NULL;
+
+                       if (opt_long_list_entries) {
+
+                               POLICY_HND alias_pol;
+                               ALIAS_INFO_CTR ctr;
+
+                               if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
+                                                                        &domain_pol,
+                                                                        0x8,
+                                                                        groups[i].rid,
+                                                                        &alias_pol))) &&
+                                   (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
+                                                                              &alias_pol, 3,
+                                                                              &ctr))) &&
+                                   (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
+                                                                   &alias_pol)))) {
+                                       description = unistr2_tdup(mem_ctx,
+                                                                  &ctr.alias.info3.uni_acct_desc);
+                               }
+                       }
+                       
+                       if (description != NULL) {
                                printf("%-21.21s %-50.50s\n", 
                                       groups[i].acct_name,
-                                      groups[i].acct_desc);
-                       else
-                               printf("%-21.21s\n", groups[i].acct_name);
+                                      description);
+                       } else {
+                               printf("%s\n", groups[i].acct_name);
+                       }
                }
-       } while (!NT_STATUS_IS_OK(result));
+       } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
        cli_samr_close(cli, mem_ctx, &domain_pol);
        /* Get builtin policy handle */
        
@@ -966,25 +1183,156 @@ rpc_group_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
                goto done;
        }
        /* query builtin aliases */
+       start_idx = 0;
        do {
+               if (!builtin) break;
+
                result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
                                                  &start_idx, max_entries,
                                                  &groups, &num_entries);
                                                 
                for (i = 0; i < num_entries; i++) {
-                       if (opt_long_list_entries)
+
+                       char *description = NULL;
+
+                       if (opt_long_list_entries) {
+
+                               POLICY_HND alias_pol;
+                               ALIAS_INFO_CTR ctr;
+
+                               if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
+                                                                        &domain_pol,
+                                                                        0x8,
+                                                                        groups[i].rid,
+                                                                        &alias_pol))) &&
+                                   (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
+                                                                              &alias_pol, 3,
+                                                                              &ctr))) &&
+                                   (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
+                                                                   &alias_pol)))) {
+                                       description = unistr2_tdup(mem_ctx,
+                                                                  &ctr.alias.info3.uni_acct_desc);
+                               }
+                       }
+                       
+                       if (description != NULL) {
                                printf("%-21.21s %-50.50s\n", 
                                       groups[i].acct_name,
-                                      groups[i].acct_desc);
-                       else
+                                      description);
+                       } else {
                                printf("%s\n", groups[i].acct_name);
+                       }
                }
-       } while (!NT_STATUS_IS_OK(result));
+       } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
 
  done:
        return result;
 }
 
+static int rpc_group_list(int argc, const char **argv)
+{
+       return run_rpc_command(NULL, PI_SAMR, 0,
+                              rpc_group_list_internals,
+                              argc, argv);
+}
+static NTSTATUS 
+rpc_group_members_internals(const DOM_SID *domain_sid, struct cli_state *cli,
+                           TALLOC_CTX *mem_ctx, int argc, const char **argv)
+{
+       NTSTATUS result;
+       POLICY_HND connect_pol, domain_pol, group_pol;
+       uint32 num_rids, *rids, *rid_types;
+       uint32 num_members, *group_rids, *group_attrs;
+       uint32 num_names;
+       char **names;
+       uint32 *name_types;
+       int i;
+
+       /* Get sam policy handle */
+       
+       result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
+                                 &connect_pol);
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+       
+       /* Get domain policy handle */
+       
+       result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
+                                     MAXIMUM_ALLOWED_ACCESS,
+                                     domain_sid, &domain_pol);
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+       result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
+                                      1, argv, &num_rids, &rids, &rid_types);
+
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+       if (num_rids != 1) {
+               d_printf("Could not find group %s\n", argv[0]);
+               goto done;
+       }
+
+       if (rid_types[0] != SID_NAME_DOM_GRP) {
+               d_printf("%s is not a domain group\n", argv[0]);
+               goto done;
+       }
+
+       result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
+                                    MAXIMUM_ALLOWED_ACCESS,
+                                    rids[0], &group_pol);
+
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
+
+       result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
+                                        &num_members, &group_rids,
+                                        &group_attrs);
+
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
+
+       while (num_members > 0) {
+               int this_time = 512;
+
+               if (num_members < this_time)
+                       this_time = num_members;
+
+               result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol, 1000,
+                                             this_time, group_rids,
+                                             &num_names, &names, &name_types);
+
+               if (!NT_STATUS_IS_OK(result))
+                       goto done;
+
+               for (i = 0; i < this_time; i++) {
+                       printf("%s\n", names[i]);
+               }
+
+               num_members -= this_time;
+               group_rids += 512;
+       }
+
+ done:
+       return result;
+}
+
+static int rpc_group_members(int argc, const char **argv)
+{
+       if (argc != 1) {
+               return rpc_group_usage(argc, argv);
+       }
+
+       return run_rpc_command(NULL, PI_SAMR, 0,
+                              rpc_group_members_internals,
+                              argc, argv);
+}
+
 /** 
  * 'net rpc group' entrypoint.
  * @param argc  Standard main() style argc
@@ -999,6 +1347,8 @@ int net_rpc_group(int argc, const char **argv)
                {"add", rpc_group_add},
                {"delete", rpc_group_delete},
 #endif
+               {"list", rpc_group_list},
+               {"members", rpc_group_members},
                {NULL, NULL}
        };
        
@@ -1382,7 +1732,7 @@ int net_rpc_file(int argc, const char **argv)
 
 
 /** 
- * ABORT the shutdown of a remote RPC Server
+ * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
  *
  * All parameters are provided by the run_rpc_command function, except for
  * argc, argv which are passed through. 
@@ -1397,11 +1747,47 @@ int net_rpc_file(int argc, const char **argv)
  * @return Normal NTSTATUS return.
  **/
 
-static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx, 
+static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid, 
+                                            struct cli_state *cli, 
+                                            TALLOC_CTX *mem_ctx, 
                                             int argc, const char **argv) 
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        
+       result = cli_shutdown_abort(cli, mem_ctx);
+       
+       if (NT_STATUS_IS_OK(result))
+               DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
+       else
+               DEBUG(5,("cmd_shutdown_abort: query failed\n"));
+       
+       return result;
+}
+
+
+/** 
+ * ABORT the shutdown of a remote RPC Server,  over winreg pipe
+ *
+ * All parameters are provided by the run_rpc_command function, except for
+ * argc, argv which are passed through. 
+ *
+ * @param domain_sid The domain sid aquired from the remote server
+ * @param cli A cli_state connected to the server.
+ * @param mem_ctx Talloc context, destoyed on compleation of the function.
+ * @param argc  Standard main() style argc
+ * @param argv  Standard main() style argv.  Initial components are already
+ *              stripped
+ *
+ * @return Normal NTSTATUS return.
+ **/
+
+static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid, 
+                                                struct cli_state *cli, 
+                                                TALLOC_CTX *mem_ctx, 
+                                                int argc, const char **argv) 
+{
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       
        result = cli_reg_abort_shutdown(cli, mem_ctx);
        
        if (NT_STATUS_IS_OK(result))
@@ -1412,7 +1798,6 @@ static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid, struct c
        return result;
 }
 
-
 /** 
  * ABORT the Shut down of a remote RPC server
  *
@@ -1425,7 +1810,17 @@ static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid, struct c
 
 static int rpc_shutdown_abort(int argc, const char **argv) 
 {
-       return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_abort_internals,
+       int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0, 
+                                rpc_shutdown_abort_internals,
+                                argc, argv);
+
+       if (rc == 0)
+               return rc;
+
+       DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
+
+       return run_rpc_command(NULL, PI_WINREG, 0, 
+                              rpc_reg_shutdown_abort_internals,
                               argc, argv);
 }
 
@@ -1539,8 +1934,8 @@ static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli
        uint16 acb_info;
        uint32 unknown, user_rid;
 
-       if (argc != 1) {
-               d_printf("Usage: net rpc trustdom add <domain_name>\n");
+       if (argc != 2) {
+               d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -1552,7 +1947,7 @@ static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli
                return NT_STATUS_NO_MEMORY;
        }
 
-       strupper(acct_name);
+       strupper_m(acct_name);
 
        /* Get samr policy handle */
        result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
@@ -1571,7 +1966,7 @@ static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli
 
        /* Create trusting domain's account */
        acb_info = ACB_DOMTRUST;
-       unknown = 0xe005000b; /* No idea what this is - a permission mask?
+       unknown = 0xe00500b0; /* No idea what this is - a permission mask?
                                 mimir: yes, most probably it is */
 
        result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
@@ -1581,6 +1976,31 @@ static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli
                goto done;
        }
 
+       {
+               SAM_USERINFO_CTR ctr;
+               SAM_USER_INFO_24 p24;
+               uchar pwbuf[516];
+
+               encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
+
+               ZERO_STRUCT(ctr);
+               ZERO_STRUCT(p24);
+
+               init_sam_user_info24(&p24, (char *)pwbuf, 24);
+
+               ctr.switch_value = 24;
+               ctr.info.id24 = &p24;
+
+               result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
+                                              &cli->user_session_key, &ctr);
+
+               if (!NT_STATUS_IS_OK(result)) {
+                       DEBUG(0,("Could not set trust account password: %s\n",
+                                nt_errstr(result)));
+                       goto done;
+               }
+       }
+
  done:
        SAFE_FREE(acct_name);
        return result;
@@ -1597,8 +2017,13 @@ static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli
 
 static int rpc_trustdom_add(int argc, const char **argv)
 {
-       return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
-                              argc, argv);
+       if (argc > 0) {
+               return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
+                                      argc, argv);
+       } else {
+               d_printf("Usage: net rpc trustdom add <domain>\n");
+               return -1;
+       }
 }
 
 
@@ -1614,6 +2039,7 @@ static int rpc_trustdom_add(int argc, const char **argv)
 static int rpc_trustdom_del(int argc, const char **argv)
 {
        d_printf("Sorry, not yet implemented.\n");
+       d_printf("Use 'smbpasswd -x -i' instead.\n");
        return -1;
 }
 
@@ -1635,10 +2061,11 @@ static int rpc_trustdom_establish(int argc, const char **argv)
        POLICY_HND connect_hnd;
        TALLOC_CTX *mem_ctx;
        NTSTATUS nt_status;
-       DOM_SID domain_sid;
+       DOM_SID *domain_sid;
        WKS_INFO_100 wks_info;
        
        char* domain_name;
+       char* domain_name_pol;
        char* acct_name;
        fstring pdc_name;
 
@@ -1652,11 +2079,11 @@ static int rpc_trustdom_establish(int argc, const char **argv)
        }
 
        domain_name = smb_xstrdup(argv[0]);
-       strupper(domain_name);
+       strupper_m(domain_name);
 
        /* account name used at first is our domain's name with '$' */
        asprintf(&acct_name, "%s$", lp_workgroup());
-       strupper(acct_name);
+       strupper_m(acct_name);
        
        /*
         * opt_workgroup will be used by connection functions further,
@@ -1669,8 +2096,8 @@ static int rpc_trustdom_establish(int argc, const char **argv)
        opt_user_name = acct_name;
 
        /* find the domain controller */
-       if (!net_find_dc(&server_ip, pdc_name, domain_name)) {
-               DEBUG(0, ("Coulnd find domain controller for domain %s\n", domain_name));
+       if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
+               DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
                return -1;
        }
 
@@ -1759,7 +2186,7 @@ static int rpc_trustdom_establish(int argc, const char **argv)
        /* Querying info level 5 */
        
        nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
-                                             5 /* info level */, domain_name,
+                                             5 /* info level */, &domain_name_pol,
                                              &domain_sid);
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
@@ -1779,7 +2206,7 @@ static int rpc_trustdom_establish(int argc, const char **argv)
 
        if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
                                                   wks_info.uni_lan_grp.uni_str_len, opt_password,
-                                                  domain_sid)) {
+                                                  *domain_sid)) {
                DEBUG(0, ("Storing password for trusted domain failed.\n"));
                return -1;
        }
@@ -1821,7 +2248,7 @@ static int rpc_trustdom_revoke(int argc, const char **argv)
        
        /* generate upper cased domain name */
        domain_name = smb_xstrdup(argv[0]);
-       strupper(domain_name);
+       strupper_m(domain_name);
 
        /* delete password of the trust */
        if (!trusted_domain_password_delete(domain_name)) {
@@ -1860,7 +2287,7 @@ static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, struct cli_state
        sid_to_string(str_sid, domain_sid);
        d_printf("%s\n", str_sid);
        return NT_STATUS_OK;
-};
+}
 
 
 static int rpc_trustdom_list(int argc, const char **argv)
@@ -1870,17 +2297,18 @@ static int rpc_trustdom_list(int argc, const char **argv)
        struct cli_state *cli, *remote_cli;
        NTSTATUS nt_status;
        const char *domain_name = NULL;
-       DOM_SID queried_dom_sid;
+       DOM_SID *queried_dom_sid;
        fstring ascii_sid, padding;
        int ascii_dom_name_len;
        POLICY_HND connect_hnd;
        
        /* trusted domains listing variables */
-       int enum_ctx = 0;
-       int num_domains, i, pad_len, col_len = 20;
+       unsigned int num_domains, enum_ctx = 0;
+       int i, pad_len, col_len = 20;
        DOM_SID *domain_sids;
        char **trusted_dom_names;
        fstring pdc_name;
+       char *dummy;
        
        /* trusting domains listing variables */
        POLICY_HND domain_hnd;
@@ -1918,7 +2346,7 @@ static int rpc_trustdom_list(int argc, const char **argv)
                return -1;
        };
 
-       nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
+       nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
                                        &connect_hnd);
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
@@ -1927,8 +2355,10 @@ static int rpc_trustdom_list(int argc, const char **argv)
        };
        
        /* query info level 5 to obtain sid of a domain being queried */
-       nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
-                                       5 /* info level */, domain_name, &queried_dom_sid);
+       nt_status = cli_lsa_query_info_policy(
+               cli, mem_ctx, &connect_hnd, 5 /* info level */, 
+               &dummy, &queried_dom_sid);
+
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
                        nt_errstr(nt_status)));
@@ -2009,8 +2439,8 @@ static int rpc_trustdom_list(int argc, const char **argv)
        /* SamrOpenDomain - we have to open domain policy handle in order to be
           able to enumerate accounts*/
        nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
-                                                                        SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
-                                                                        &queried_dom_sid, &domain_hnd);                                                                         
+                                        SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
+                                        queried_dom_sid, &domain_hnd);                                                                  
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0, ("Couldn't open domain object. Error was %s\n",
                        nt_errstr(nt_status)));
@@ -2053,7 +2483,7 @@ static int rpc_trustdom_list(int argc, const char **argv)
                        do padding[--pad_len] = ' '; while (pad_len);
 
                        /* set opt_* variables to remote domain */
-                       strupper(trusting_dom_names[i]);
+                       strupper_m(trusting_dom_names[i]);
                        opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
                        opt_target_workgroup = opt_workgroup;
                        
@@ -2177,6 +2607,7 @@ int net_rpc_usage(int argc, const char **argv)
 {
        d_printf("  net rpc info \t\t\tshow basic info about a domain \n");
        d_printf("  net rpc join \t\t\tto join a domain \n");
+       d_printf("  net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
        d_printf("  net rpc testjoin \t\ttests that a join is valid\n");
        d_printf("  net rpc user \t\t\tto add, delete and list users\n");
        d_printf("  net rpc group \t\tto list groups\n");
@@ -2243,12 +2674,14 @@ int net_rpc(int argc, const char **argv)
        struct functable func[] = {
                {"info", net_rpc_info},
                {"join", net_rpc_join},
+               {"oldjoin", net_rpc_oldjoin},
                {"testjoin", net_rpc_testjoin},
                {"user", net_rpc_user},
+               {"password", rpc_user_password},
                {"group", net_rpc_group},
                {"share", net_rpc_share},
                {"file", net_rpc_file},
-               {"changetrustpw", rpc_changetrustpw},
+               {"changetrustpw", net_rpc_changetrustpw},
                {"trustdom", rpc_trustdom},
                {"abortshutdown", rpc_shutdown_abort},
                {"shutdown", rpc_shutdown},