Removed version number from file header.
[ira/wip.git] / source3 / rpcclient / cmd_netlogon.c
index 1b493875585ab73598bf5647714bfb051af9acd4..4d67cba1b5272d452438dc422a9c04e6f0f696cd 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 2.2
+   Unix SMB/CIFS implementation.
    RPC pipe client
 
    Copyright (C) Tim Potter 2000
 */
 
 #include "includes.h"
+#include "rpcclient.h"
 
-extern int DEBUGLEVEL;
-
-static uint32 cmd_netlogon_logon_ctrl2(struct cli_state *cli, int argc,
-                                      char **argv)
+static NTSTATUS cmd_netlogon_logon_ctrl2(struct cli_state *cli, 
+                                         TALLOC_CTX *mem_ctx, int argc, 
+                                         char **argv)
 {
        uint32 query_level = 1;
-       TALLOC_CTX *mem_ctx;
-       uint32 result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 
        if (argc > 1) {
-               printf("Usage: %s\n", argv[0]);
-               return 0;
+               fprintf(stderr, "Usage: %s\n", argv[0]);
+               return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_srvsvc_srv_query_info: talloc_init failed\n"));
-               goto done;
-       }
+       result = cli_netlogon_logon_ctrl2(cli, mem_ctx, query_level);
 
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
-               DEBUG(0, ("Could not initialize srvsvc pipe!\n"));
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }
-
-       if ((result = cli_netlogon_logon_ctrl2(cli, mem_ctx, query_level))
-            != NT_STATUS_OK) {
-               goto done;
-       }
 
        /* Display results */
 
@@ -59,42 +45,29 @@ static uint32 cmd_netlogon_logon_ctrl2(struct cli_state *cli, int argc,
        return result;
 }
 
-static uint32 cmd_netlogon_logon_ctrl(struct cli_state *cli, int argc,
-                                     char **argv)
+static NTSTATUS cmd_netlogon_logon_ctrl(struct cli_state *cli, 
+                                        TALLOC_CTX *mem_ctx, int argc, 
+                                        char **argv)
 {
 #if 0
        uint32 query_level = 1;
 #endif
-       TALLOC_CTX *mem_ctx;
-       uint32 result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 
        if (argc > 1) {
-               printf("Usage: %s\n", argv[0]);
-               return 0;
-       }
-
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_srvsvc_srv_query_info: talloc_init failed\n"));
-               goto done;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
-               DEBUG(0, ("Could not initialize netlogon pipe!\n"));
-               goto done;
+               fprintf(stderr, "Usage: %s\n", argv[0]);
+               return NT_STATUS_OK;
        }
 
 #if 0
-       if ((result = cli_netlogon_logon_ctrl(cli, mem_ctx, query_level))
-            != NT_STATUS_OK) {
+       result = cli_netlogon_logon_ctrl(cli, mem_ctx, query_level);
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 #endif
 
        /* Display results */
 
- done:
        return result;
 }
 
@@ -112,25 +85,25 @@ static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
                         unistr2_to_ascii(name,
                                          &deltas[i].domain_info.uni_dom_name,
                                          sizeof(name) - 1);
-                        DEBUG(0, ("Domain: %s\n", name));
+                        printf("Domain: %s\n", name);
                         break;
                 case SAM_DELTA_GROUP_INFO:
                         unistr2_to_ascii(name,
                                          &deltas[i].group_info.uni_grp_name,
                                          sizeof(name) - 1);
-                        DEBUG(0, ("Group: %s\n", name));
+                        printf("Group: %s\n", name);
                         break;
                 case SAM_DELTA_ACCOUNT_INFO:
                         unistr2_to_ascii(name, 
                                          &deltas[i].account_info.uni_acct_name,
                                          sizeof(name) - 1);
-                        DEBUG(0, ("Account: %s\n", name));
+                        printf("Account: %s\n", name);
                         break;
                 case SAM_DELTA_ALIAS_INFO:
                         unistr2_to_ascii(name, 
                                          &deltas[i].alias_info.uni_als_name,
                                          sizeof(name) - 1);
-                        DEBUG(0, ("Alias: %s\n", name));
+                        printf("Alias: %s\n", name);
                         break;
                 case SAM_DELTA_ALIAS_MEM: {
                         SAM_ALIAS_MEM_INFO *alias = &deltas[i].als_mem_info;
@@ -140,7 +113,7 @@ static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
 
                                 sid_to_string(sid_str, &alias->sids[j].sid);
 
-                                DEBUG(0, ("%s\n", sid_str));
+                                printf("%s\n", sid_str);
                         }
                         break;
                 }
@@ -148,20 +121,20 @@ static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
                         SAM_GROUP_MEM_INFO *group = &deltas[i].grp_mem_info;
 
                         for (j = 0; j < group->num_members; j++)
-                                DEBUG(0, ("rid 0x%x, attrib 0x%08x\n", 
-                                          group->rids[j], group->attribs[j]));
+                                printf("rid 0x%x, attrib 0x%08x\n", 
+                                          group->rids[j], group->attribs[j]);
                         break;
                 }
                 case SAM_DELTA_SAM_STAMP: {
                         SAM_DELTA_STAMP *stamp = &deltas[i].stamp;
 
-                        DEBUG(0, ("sam sequence update: 0x%04x\n",
-                                  stamp->seqnum));
+                        printf("sam sequence update: 0x%04x\n",
+                                  stamp->seqnum);
                         break;
                 }                                  
                 default:
-                        DEBUG(0, ("unknown delta type 0x%02x\n", 
-                                  hdr_deltas[i].type));
+                        printf("unknown delta type 0x%02x\n", 
+                                  hdr_deltas[i].type);
                         break;
                 }
         }
@@ -169,89 +142,80 @@ static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
 
 /* Perform sam synchronisation */
 
-static uint32 cmd_netlogon_sam_sync(struct cli_state *cli, int argc,
-                                    char **argv)
+static NTSTATUS cmd_netlogon_sam_sync(struct cli_state *cli, 
+                                      TALLOC_CTX *mem_ctx, int argc,
+                                      char **argv)
 {
-       uint32 result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
         unsigned char trust_passwd[16];
-        TALLOC_CTX *mem_ctx;
         uint32 database_id = 0, num_deltas;
         SAM_DELTA_HDR *hdr_deltas;
         SAM_DELTA_CTR *deltas;
+       DOM_CRED ret_creds;
 
         if (argc > 2) {
-                printf("Usage: %s [database_id]\n", argv[0]);
-                return 0;
+                fprintf(stderr, "Usage: %s [database_id]\n", argv[0]);
+                return NT_STATUS_OK;
         }
 
         if (argc == 2)
                 database_id = atoi(argv[1]);
 
         if (!secrets_init()) {
-                DEBUG(0, ("Unable to initialise secrets database\n"));
+                fprintf(stderr, "Unable to initialise secrets database\n");
                 return result;
         }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("talloc_init failed\n"));
-               return result;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
-               DEBUG(0, ("Could not initialize netlogon pipe!\n"));
-               goto done;
-       }
-
         /* Initialise session credentials */
 
        if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd,
                                                   NULL)) {
-               DEBUG(0, ("could not fetch trust account password\n"));
+               fprintf(stderr, "could not fetch trust account password\n");
                goto done;
        }        
 
-        if (!cli_nt_setup_creds(cli, trust_passwd)) {
-                DEBUG(0, ("Error initialising session creds\n"));
+        result = cli_nt_setup_creds(cli, trust_passwd);
+
+        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 */
 
-       if ((result = cli_netlogon_sam_sync(cli, mem_ctx, database_id,
-                                            &num_deltas, &hdr_deltas, &deltas))
-            != NT_STATUS_OK) {
+       result = cli_netlogon_sam_sync(cli, mem_ctx, &ret_creds, database_id,
+                                      &num_deltas, &hdr_deltas, &deltas);
+
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }
 
         /* Display results */
 
         display_sam_sync(num_deltas, hdr_deltas, deltas);
 
  done:
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-        
         return result;
 }
 
 /* Perform sam delta synchronisation */
 
-static uint32 cmd_netlogon_sam_deltas(struct cli_state *cli, int argc,
-                                      char **argv)
+static NTSTATUS cmd_netlogon_sam_deltas(struct cli_state *cli, 
+                                        TALLOC_CTX *mem_ctx, int argc,
+                                        char **argv)
 {
-       uint32 result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
         unsigned char trust_passwd[16];
-        TALLOC_CTX *mem_ctx = NULL;
         uint32 database_id, num_deltas, tmp;
         SAM_DELTA_HDR *hdr_deltas;
         SAM_DELTA_CTR *deltas;
         UINT64_S seqnum;
 
         if (argc != 3) {
-                printf("Usage: %s database_id seqnum\n", argv[0]);
-                return 0;
+                fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]);
+                return NT_STATUS_OK;
         }
 
         database_id = atoi(argv[1]);
@@ -261,52 +225,96 @@ static uint32 cmd_netlogon_sam_deltas(struct cli_state *cli, int argc,
         seqnum.high = 0;
 
         if (!secrets_init()) {
-                DEBUG(0, ("Unable to initialise secrets database\n"));
+                fprintf(stderr, "Unable to initialise secrets database\n");
                 goto done;
         }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("talloc_init failed\n"));
-               goto done;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
-               DEBUG(0, ("Could not initialize netlogon pipe!\n"));
-               goto done;
-       }
-
         /* Initialise session credentials */
 
        if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd,
                                                   NULL)) {
-               DEBUG(0, ("could not fetch trust account password\n"));
+               fprintf(stderr, "could not fetch trust account password\n");
                goto done;
        }        
 
-        if (!cli_nt_setup_creds(cli, trust_passwd)) {
-                DEBUG(0, ("Error initialising session creds\n"));
+        result = cli_nt_setup_creds(cli, trust_passwd);
+
+        if (!NT_STATUS_IS_OK(result)) {
+                fprintf(stderr, "Error initialising session creds\n");
                 goto done;
         }
 
         /* Synchronise sam database */
 
-       if ((result = cli_netlogon_sam_deltas(cli, mem_ctx, database_id,
-                                              seqnum, &num_deltas, 
-                                              &hdr_deltas, &deltas))
-            != NT_STATUS_OK) {
+       result = cli_netlogon_sam_deltas(cli, mem_ctx, database_id,
+                                        seqnum, &num_deltas, 
+                                        &hdr_deltas, &deltas);
+
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }
 
         /* Display results */
 
         display_sam_sync(num_deltas, hdr_deltas, deltas);
         
  done:
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
+        return result;
+}
+
+/* Log on a domain user */
+
+static NTSTATUS cmd_netlogon_sam_logon(struct cli_state *cli, 
+                                       TALLOC_CTX *mem_ctx, int argc,
+                                       char **argv)
+{
+        unsigned char trust_passwd[16];
+        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+        int logon_type = NET_LOGON_TYPE;
+        char *username, *password;
+
+        /* Check arguments */
+
+        if (argc < 3 || argc > 4) {
+                fprintf(stderr, "Usage: samlogon <username> <password> "
+                        "[logon_type]\n");
+                return NT_STATUS_OK;
+        }
+
+        username = argv[1];
+        password = argv[2];
+
+        if (argc == 4)
+                sscanf(argv[3], "%i", &logon_type);
+
+        /* Authenticate ourselves with the domain controller */
+
+        if (!secrets_init()) {
+                fprintf(stderr, "Unable to initialise secrets database\n");
+                return result;
+        }
+
+       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, trust_passwd);
 
+        if (!NT_STATUS_IS_OK(result)) {
+                fprintf(stderr, "Error initialising session creds\n");
+                goto done;
+        }
+
+        /* Perform the sam logon */
+
+        result = cli_netlogon_sam_logon(cli, mem_ctx, username, password,
+                                        logon_type);
+
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
+
+ done:
         return result;
 }
 
@@ -316,10 +324,11 @@ struct cmd_set netlogon_commands[] = {
 
        { "NETLOGON" },
 
-       { "logonctrl2", cmd_netlogon_logon_ctrl2, "Logon Control 2",     "" },
-       { "logonctrl",  cmd_netlogon_logon_ctrl,  "Logon Control",       "" },
-       { "samsync",    cmd_netlogon_sam_sync,    "Sam Synchronisation", "" },
-       { "samdeltas",  cmd_netlogon_sam_deltas,  "Query Sam Deltas", "" },
+       { "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",           "" },
 
        { NULL }
 };