r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[tprouty/samba.git] / source / rpcclient / cmd_netlogon.c
index 0f61eaaae848c9a30660de61aebae0d610845058..b22de0bb54d3b61218ef9e7222882fa2a7350bbe 100644 (file)
@@ -6,7 +6,7 @@
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "rpcclient.h"
 
-static NTSTATUS cmd_netlogon_logon_ctrl2(struct cli_state *cli, 
+static NTSTATUS cmd_netlogon_logon_ctrl2(struct rpc_pipe_client *cli, 
                                          TALLOC_CTX *mem_ctx, int argc, 
-                                         char **argv)
+                                         const char **argv)
 {
        uint32 query_level = 1;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
@@ -34,7 +33,7 @@ static NTSTATUS cmd_netlogon_logon_ctrl2(struct cli_state *cli,
                return NT_STATUS_OK;
        }
 
-       result = cli_netlogon_logon_ctrl2(cli, mem_ctx, query_level);
+       result = rpccli_netlogon_logon_ctrl2(cli, mem_ctx, query_level);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -45,9 +44,295 @@ static NTSTATUS cmd_netlogon_logon_ctrl2(struct cli_state *cli,
        return result;
 }
 
-static NTSTATUS cmd_netlogon_logon_ctrl(struct cli_state *cli, 
+static WERROR cmd_netlogon_getanydcname(struct rpc_pipe_client *cli, 
+                                       TALLOC_CTX *mem_ctx, int argc, 
+                                       const char **argv)
+{
+       fstring dcname;
+       WERROR result = WERR_GENERAL_FAILURE;
+       int old_timeout;
+
+       if (argc != 2) {
+               fprintf(stderr, "Usage: %s domainname\n", argv[0]);
+               return WERR_OK;
+       }
+
+       /* Make sure to wait for our DC's reply */
+       old_timeout = cli_set_timeout(cli->cli, MAX(cli->cli->timeout,30000)); /* 30 seconds. */
+
+       result = rpccli_netlogon_getanydcname(cli, mem_ctx, cli->cli->desthost, argv[1], dcname);
+
+       cli_set_timeout(cli->cli, old_timeout);
+
+       if (!W_ERROR_IS_OK(result))
+               goto done;
+
+       /* Display results */
+
+       printf("%s\n", dcname);
+
+ done:
+       return result;
+}
+
+static WERROR cmd_netlogon_getdcname(struct rpc_pipe_client *cli, 
+                                    TALLOC_CTX *mem_ctx, int argc, 
+                                    const char **argv)
+{
+       fstring dcname;
+       WERROR result = WERR_GENERAL_FAILURE;
+       int old_timeout;
+
+       if (argc != 2) {
+               fprintf(stderr, "Usage: %s domainname\n", argv[0]);
+               return WERR_OK;
+       }
+
+       /* Make sure to wait for our DC's reply */
+       old_timeout = cli_set_timeout(cli->cli, MAX(cli->cli->timeout,30000)); /* 30 seconds. */
+
+       result = rpccli_netlogon_getdcname(cli, mem_ctx, cli->cli->desthost, argv[1], dcname);
+
+       cli_set_timeout(cli->cli, old_timeout);
+
+       if (!W_ERROR_IS_OK(result))
+               goto done;
+
+       /* Display results */
+
+       printf("%s\n", dcname);
+
+ done:
+       return result;
+}
+
+static void display_ds_domain_controller_info(TALLOC_CTX *mem_ctx, const struct DS_DOMAIN_CONTROLLER_INFO *info)
+{
+       d_printf("domain_controller_name: %s\n", info->domain_controller_name);
+       d_printf("domain_controller_address: %s\n", info->domain_controller_address);
+       d_printf("domain_controller_address_type: %d\n", info->domain_controller_address_type);
+       d_printf("domain_guid: %s\n", GUID_string(mem_ctx, info->domain_guid));
+       d_printf("domain_name: %s\n", info->domain_name);
+       d_printf("dns_forest_name: %s\n", info->dns_forest_name);
+       d_printf("flags: 0x%08x\n"
+                "\tIs a PDC:                                   %s\n"
+                "\tIs a GC of the forest:                      %s\n"
+                "\tIs an LDAP server:                          %s\n"
+                "\tSupports DS:                                %s\n"
+                "\tIs running a KDC:                           %s\n"
+                "\tIs running time services:                   %s\n"
+                "\tIs the closest DC:                          %s\n"
+                "\tIs writable:                                %s\n"
+                "\tHas a hardware clock:                       %s\n"
+                "\tIs a non-domain NC serviced by LDAP server: %s\n"
+                "\tDomainControllerName is a DNS name:         %s\n"
+                "\tDomainName is a DNS name:                   %s\n"
+                "\tDnsForestName is a DNS name:                %s\n",
+                info->flags,
+                (info->flags & ADS_PDC) ? "yes" : "no",
+                (info->flags & ADS_GC) ? "yes" : "no",
+                (info->flags & ADS_LDAP) ? "yes" : "no",
+                (info->flags & ADS_DS) ? "yes" : "no",
+                (info->flags & ADS_KDC) ? "yes" : "no",
+                (info->flags & ADS_TIMESERV) ? "yes" : "no",
+                (info->flags & ADS_CLOSEST) ? "yes" : "no",
+                (info->flags & ADS_WRITABLE) ? "yes" : "no",
+                (info->flags & ADS_GOOD_TIMESERV) ? "yes" : "no",
+                (info->flags & ADS_NDNC) ? "yes" : "no",
+                (info->flags & ADS_DNS_CONTROLLER) ? "yes":"no",
+                (info->flags & ADS_DNS_DOMAIN) ? "yes":"no",
+                (info->flags & ADS_DNS_FOREST) ? "yes":"no");
+
+       d_printf("dc_site_name: %s\n", info->dc_site_name);
+       d_printf("client_site_name: %s\n", info->client_site_name);
+}
+
+static WERROR cmd_netlogon_dsr_getdcname(struct rpc_pipe_client *cli,
+                                        TALLOC_CTX *mem_ctx, int argc,
+                                        const char **argv)
+{
+       WERROR result;
+       uint32 flags = DS_RETURN_DNS_NAME;
+       const char *server_name = cli->cli->desthost;
+       const char *domain_name;
+       struct GUID domain_guid = GUID_zero();
+       struct GUID site_guid = GUID_zero();
+       struct DS_DOMAIN_CONTROLLER_INFO *info = NULL;
+
+       if (argc < 2) {
+               fprintf(stderr, "Usage: %s [domainname] [domain_name] [domain_guid] [site_guid] [flags]\n", argv[0]);
+               return WERR_OK;
+       }
+
+       if (argc >= 2)
+               domain_name = argv[1];
+
+       if (argc >= 3) {
+               if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
+                       return WERR_NOMEM;
+               }
+       }
+
+       if (argc >= 4) {
+               if (!NT_STATUS_IS_OK(GUID_from_string(argv[3], &site_guid))) {
+                       return WERR_NOMEM;
+               }
+       }
+
+       if (argc >= 5)
+               sscanf(argv[4], "%x", &flags);
+       
+       result = rpccli_netlogon_dsr_getdcname(cli, mem_ctx, server_name, domain_name, 
+                                              &domain_guid, &site_guid, flags,
+                                              &info);
+
+       if (W_ERROR_IS_OK(result)) {
+               d_printf("DsGetDcName gave\n");
+               display_ds_domain_controller_info(mem_ctx, info);
+               return WERR_OK;
+       }
+
+       printf("rpccli_netlogon_dsr_getdcname returned %s\n",
+              dos_errstr(result));
+
+       return result;
+}
+
+static WERROR cmd_netlogon_dsr_getdcnameex(struct rpc_pipe_client *cli,
+                                          TALLOC_CTX *mem_ctx, int argc,
+                                          const char **argv)
+{
+       WERROR result;
+       uint32 flags = DS_RETURN_DNS_NAME;
+       const char *server_name = cli->cli->desthost;
+       const char *domain_name;
+       const char *site_name = NULL;
+       struct GUID domain_guid = GUID_zero();
+       struct DS_DOMAIN_CONTROLLER_INFO *info = NULL;
+
+       if (argc < 2) {
+               fprintf(stderr, "Usage: %s [domainname] [domain_name] [domain_guid] [site_name] [flags]\n", argv[0]);
+               return WERR_OK;
+       }
+
+       if (argc >= 2)
+               domain_name = argv[1];
+
+       if (argc >= 3) {
+               if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
+                       return WERR_NOMEM;
+               }
+       }
+
+       if (argc >= 4)
+               site_name = argv[3];
+
+       if (argc >= 5)
+               sscanf(argv[4], "%x", &flags);
+
+       result = rpccli_netlogon_dsr_getdcnameex(cli, mem_ctx, server_name, domain_name, 
+                                                &domain_guid, site_name, flags,
+                                                &info);
+
+       if (W_ERROR_IS_OK(result)) {
+               d_printf("DsGetDcNameEx gave\n");
+               display_ds_domain_controller_info(mem_ctx, info);
+               return WERR_OK;
+       }
+
+       printf("rpccli_netlogon_dsr_getdcnameex returned %s\n",
+              dos_errstr(result));
+
+       return result;
+}
+
+static WERROR cmd_netlogon_dsr_getdcnameex2(struct rpc_pipe_client *cli,
+                                           TALLOC_CTX *mem_ctx, int argc,
+                                           const char **argv)
+{
+       WERROR result;
+       uint32 flags = DS_RETURN_DNS_NAME;
+       const char *server_name = cli->cli->desthost;
+       const char *domain_name = NULL;
+       const char *client_account = NULL;
+       uint32 mask = 0;
+       const char *site_name = NULL;
+       struct GUID domain_guid = GUID_zero();
+       struct DS_DOMAIN_CONTROLLER_INFO *info = NULL;
+
+       if (argc < 2) {
+               fprintf(stderr, "Usage: %s [domainname] [client_account] [acb_mask] [domain_name] [domain_guid] [site_name] [flags]\n", argv[0]);
+               return WERR_OK;
+       }
+
+       if (argc >= 2)
+               client_account = argv[1];
+
+       if (argc >= 3)
+               mask = atoi(argv[2]);
+       
+       if (argc >= 4)
+               domain_name = argv[3];
+
+       if (argc >= 5) {
+               if (!NT_STATUS_IS_OK(GUID_from_string(argv[4], &domain_guid))) {
+                       return WERR_NOMEM;
+               }
+       }
+
+       if (argc >= 6)
+               site_name = argv[5];
+
+       if (argc >= 7)
+               sscanf(argv[6], "%x", &flags);
+
+       result = rpccli_netlogon_dsr_getdcnameex2(cli, mem_ctx, server_name, 
+                                                 client_account, mask,
+                                                 domain_name, &domain_guid,
+                                                 site_name, flags,
+                                                 &info);
+
+       if (W_ERROR_IS_OK(result)) {
+               d_printf("DsGetDcNameEx2 gave\n");
+               display_ds_domain_controller_info(mem_ctx, info);
+               return WERR_OK;
+       }
+
+       printf("rpccli_netlogon_dsr_getdcnameex2 returned %s\n",
+              dos_errstr(result));
+
+       return result;
+}
+
+
+static WERROR cmd_netlogon_dsr_getsitename(struct rpc_pipe_client *cli,
+                                          TALLOC_CTX *mem_ctx, int argc,
+                                          const char **argv)
+{
+       WERROR result;
+       char *sitename;
+
+       if (argc != 2) {
+               fprintf(stderr, "Usage: %s computername\n", argv[0]);
+               return WERR_OK;
+       }
+
+       result = rpccli_netlogon_dsr_getsitename(cli, mem_ctx, argv[1], &sitename);
+
+       if (!W_ERROR_IS_OK(result)) {
+               printf("rpccli_netlogon_dsr_gesitename returned %s\n",
+                      nt_errstr(werror_to_ntstatus(result)));
+               return result;
+       }
+
+       printf("Computer %s is on Site: %s\n", argv[1], sitename);
+
+       return WERR_OK;
+}
+
+static NTSTATUS cmd_netlogon_logon_ctrl(struct rpc_pipe_client *cli, 
                                         TALLOC_CTX *mem_ctx, int argc, 
-                                        char **argv)
+                                        const char **argv)
 {
 #if 0
        uint32 query_level = 1;
@@ -141,17 +426,14 @@ static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
 
 /* Perform sam synchronisation */
 
-static NTSTATUS cmd_netlogon_sam_sync(struct cli_state *cli, 
+static NTSTATUS cmd_netlogon_sam_sync(struct rpc_pipe_client *cli, 
                                       TALLOC_CTX *mem_ctx, int argc,
-                                      char **argv)
+                                      const char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-        unsigned char trust_passwd[16];
         uint32 database_id = 0, num_deltas;
         SAM_DELTA_HDR *hdr_deltas;
         SAM_DELTA_CTR *deltas;
-       DOM_CRED ret_creds;
-       uint32 neg_flags = 0x000001ff;
 
         if (argc > 2) {
                 fprintf(stderr, "Usage: %s [database_id]\n", argv[0]);
@@ -161,32 +443,9 @@ static NTSTATUS cmd_netlogon_sam_sync(struct cli_state *cli,
         if (argc == 2)
                 database_id = atoi(argv[1]);
 
-        if (!secrets_init()) {
-                fprintf(stderr, "Unable to initialise secrets database\n");
-                return result;
-        }
-
-        /* Initialise session credentials */
-
-       if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd,
-                                                  NULL)) {
-               fprintf(stderr, "could not fetch trust account password\n");
-               goto done;
-       }        
-
-        result = cli_nt_setup_creds(cli, get_sec_chan(), trust_passwd, &neg_flags, 2);
-
-        if (!NT_STATUS_IS_OK(result)) {
-                fprintf(stderr, "Error initialising session creds\n");
-                goto done;
-        }
-
-       /* on first call the returnAuthenticator is empty */
-       memset(&ret_creds, 0, sizeof(ret_creds));
         /* Synchronise sam database */
 
-       result = cli_netlogon_sam_sync(cli, mem_ctx, &ret_creds, database_id,
+       result = rpccli_netlogon_sam_sync(cli, mem_ctx, database_id,
                                       0, &num_deltas, &hdr_deltas, &deltas);
 
        if (!NT_STATUS_IS_OK(result))
@@ -202,17 +461,15 @@ static NTSTATUS cmd_netlogon_sam_sync(struct cli_state *cli,
 
 /* Perform sam delta synchronisation */
 
-static NTSTATUS cmd_netlogon_sam_deltas(struct cli_state *cli, 
+static NTSTATUS cmd_netlogon_sam_deltas(struct rpc_pipe_client *cli, 
                                         TALLOC_CTX *mem_ctx, int argc,
-                                        char **argv)
+                                        const char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-        unsigned char trust_passwd[16];
         uint32 database_id, num_deltas, tmp;
         SAM_DELTA_HDR *hdr_deltas;
         SAM_DELTA_CTR *deltas;
-        UINT64_S seqnum;
-       uint32 neg_flags = 0x000001ff;
+        uint64 seqnum;
 
         if (argc != 3) {
                 fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]);
@@ -222,32 +479,9 @@ static NTSTATUS cmd_netlogon_sam_deltas(struct cli_state *cli,
         database_id = atoi(argv[1]);
         tmp = atoi(argv[2]);
 
-        seqnum.low = tmp & 0xffff;
-        seqnum.high = 0;
+        seqnum = tmp & 0xffff;
 
-        if (!secrets_init()) {
-                fprintf(stderr, "Unable to initialise secrets database\n");
-                goto done;
-        }
-
-        /* Initialise session credentials */
-
-       if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd,
-                                                  NULL)) {
-               fprintf(stderr, "could not fetch trust account password\n");
-               goto done;
-       }        
-
-        result = cli_nt_setup_creds(cli, get_sec_chan(), trust_passwd, &neg_flags, 2);
-
-        if (!NT_STATUS_IS_OK(result)) {
-                fprintf(stderr, "Error initialising session creds\n");
-                goto done;
-        }
-
-        /* Synchronise sam database */
-
-       result = cli_netlogon_sam_deltas(cli, mem_ctx, database_id,
+       result = rpccli_netlogon_sam_deltas(cli, mem_ctx, database_id,
                                         seqnum, &num_deltas, 
                                         &hdr_deltas, &deltas);
 
@@ -264,52 +498,70 @@ static NTSTATUS cmd_netlogon_sam_deltas(struct cli_state *cli,
 
 /* Log on a domain user */
 
-static NTSTATUS cmd_netlogon_sam_logon(struct cli_state *cli, 
-                                       TALLOC_CTX *mem_ctx, int argc,
-                                       char **argv)
+static NTSTATUS cmd_netlogon_sam_logon(struct rpc_pipe_client *cli, 
+                                      TALLOC_CTX *mem_ctx, int argc,
+                                      const char **argv)
 {
-        unsigned char trust_passwd[16];
-        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-        int logon_type = NET_LOGON_TYPE;
-        char *username, *password;
-       uint32 neg_flags = 0x000001ff;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       int logon_type = NET_LOGON_TYPE;
+       const char *username, *password;
+       int auth_level = 2;
+       uint32 logon_param = 0;
+       const char *workstation = NULL;
 
-        /* Check arguments */
+       /* Check arguments */
 
-        if (argc < 3 || argc > 4) {
-                fprintf(stderr, "Usage: samlogon <username> <password> "
-                        "[logon_type]\n");
-                return NT_STATUS_OK;
-        }
+       if (argc < 3 || argc > 7) {
+               fprintf(stderr, "Usage: samlogon <username> <password> [workstation]"
+                       "[logon_type (1 or 2)] [auth level (2 or 3)] [logon_parameter]\n");
+               return NT_STATUS_OK;
+       }
 
-        username = argv[1];
-        password = argv[2];
+       username = argv[1];
+       password = argv[2];
 
-        if (argc == 4)
-                sscanf(argv[3], "%i", &logon_type);
+       if (argc >= 4) 
+               workstation = argv[3];
 
-        /* Authenticate ourselves with the domain controller */
+       if (argc >= 5)
+               sscanf(argv[4], "%i", &logon_type);
 
-        if (!secrets_init()) {
-                fprintf(stderr, "Unable to initialise secrets database\n");
-                return result;
-        }
+       if (argc >= 6)
+               sscanf(argv[5], "%i", &auth_level);
+
+       if (argc == 7)
+               sscanf(argv[6], "%x", &logon_param);
+
+       /* Perform the sam logon */
+
+       result = rpccli_netlogon_sam_logon(cli, mem_ctx, logon_param, lp_workgroup(), username, password, workstation, logon_type);
 
-       if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd, NULL)) {
-               fprintf(stderr, "could not fetch trust account password\n");
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }        
 
-        result = cli_nt_setup_creds(cli, get_sec_chan(), trust_passwd, &neg_flags, 2);
+ done:
+       return result;
+}
+
+/* Change the trust account password */
+
+static NTSTATUS cmd_netlogon_change_trust_pw(struct rpc_pipe_client *cli, 
+                                            TALLOC_CTX *mem_ctx, int argc,
+                                            const char **argv)
+{
+        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+        /* Check arguments */
 
-        if (!NT_STATUS_IS_OK(result)) {
-                fprintf(stderr, "Error initialising session creds\n");
-                goto done;
+        if (argc > 1) {
+                fprintf(stderr, "Usage: change_trust_pw");
+                return NT_STATUS_OK;
         }
 
         /* Perform the sam logon */
 
-        result = cli_netlogon_sam_logon(cli, mem_ctx, username, password, logon_type);
+       result = trust_pw_find_change_and_store_it(cli, mem_ctx,
+                                                  lp_workgroup());
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -318,17 +570,25 @@ static NTSTATUS cmd_netlogon_sam_logon(struct cli_state *cli,
         return result;
 }
 
+
 /* List of commands exported by this module */
 
 struct cmd_set netlogon_commands[] = {
 
        { "NETLOGON" },
 
-       { "logonctrl2", cmd_netlogon_logon_ctrl2, PIPE_NETLOGON, "Logon Control 2",     "" },
-       { "logonctrl",  cmd_netlogon_logon_ctrl,  PIPE_NETLOGON, "Logon Control",       "" },
-       { "samsync",    cmd_netlogon_sam_sync,    PIPE_NETLOGON, "Sam Synchronisation", "" },
-       { "samdeltas",  cmd_netlogon_sam_deltas,  PIPE_NETLOGON, "Query Sam Deltas",    "" },
-        { "samlogon",   cmd_netlogon_sam_logon,   PIPE_NETLOGON, "Sam Logon",           "" },
+       { "logonctrl2", RPC_RTYPE_NTSTATUS, cmd_netlogon_logon_ctrl2, NULL, PI_NETLOGON, NULL, "Logon Control 2",     "" },
+       { "getanydcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getanydcname, PI_NETLOGON, NULL, "Get trusted DC name",     "" },
+       { "getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcname, PI_NETLOGON, NULL, "Get trusted PDC name",     "" },
+       { "dsr_getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcname, PI_NETLOGON, NULL, "Get trusted DC name",     "" },
+       { "dsr_getdcnameex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex, PI_NETLOGON, NULL, "Get trusted DC name",     "" },
+       { "dsr_getdcnameex2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex2, PI_NETLOGON, NULL, "Get trusted DC name",     "" },
+       { "dsr_getsitename", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getsitename, PI_NETLOGON, NULL, "Get sitename",     "" },
+       { "logonctrl",  RPC_RTYPE_NTSTATUS, cmd_netlogon_logon_ctrl,  NULL, PI_NETLOGON, NULL, "Logon Control",       "" },
+       { "samsync",    RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_sync,    NULL, PI_NETLOGON, NULL, "Sam Synchronisation", "" },
+       { "samdeltas",  RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_deltas,  NULL, PI_NETLOGON, NULL, "Query Sam Deltas",    "" },
+       { "samlogon",   RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_logon,   NULL, PI_NETLOGON, NULL, "Sam Logon",           "" },
+       { "change_trust_pw",   RPC_RTYPE_NTSTATUS, cmd_netlogon_change_trust_pw,   NULL, PI_NETLOGON, NULL, "Change Trust Account Password",           "" },
 
        { NULL }
 };