s3-rpcclient: fix uninitialized variable in wkssvc_enumerateusers.
[ira/wip.git] / source3 / rpcclient / cmd_wkssvc.c
index 99524cba778b2a822c9cd76fbcd6a062977dd4ea..147501049aef620ce3f199095b4147e06c1d0c9e 100644 (file)
-/* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
-   NT Domain Authentication SMB / MSRPC client
-   Copyright (C) Andrew Tridgell 1994-1997
-   Copyright (C) Luke Kenneth Casson Leighton 1996-1997
-   
+/*
+   Unix SMB/CIFS implementation.
+   RPC pipe client
+
+   Copyright (C) Günther Deschner 2007
+
    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,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    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"
+#include "../librpc/gen_ndr/cli_wkssvc.h"
 
+static WERROR cmd_wkssvc_wkstagetinfo(struct rpc_pipe_client *cli,
+                                     TALLOC_CTX *mem_ctx,
+                                     int argc,
+                                     const char **argv)
+{
+       NTSTATUS status;
+       WERROR werr;
+       uint32_t level = 100;
+       union wkssvc_NetWkstaInfo info;
+       const char *server_name;
+
+       if (argc > 2) {
+               printf("usage: %s <level>\n", argv[0]);
+               return WERR_OK;
+       }
 
-#ifdef SYSLOG
-#undef SYSLOG
-#endif
+       if (argc > 1) {
+               level = atoi(argv[1]);
+       }
 
-#include "includes.h"
-#include "nterr.h"
+       server_name = cli->desthost;
 
-extern int DEBUGLEVEL;
+       status = rpccli_wkssvc_NetWkstaGetInfo(cli, mem_ctx,
+                                              server_name,
+                                              level,
+                                              &info,
+                                              &werr);
+       if (!NT_STATUS_IS_OK(status)) {
+               return ntstatus_to_werror(status);
+       }
 
-#define DEBUG_TESTING
+       return werr;
+}
 
-extern struct cli_state *smb_cli;
-extern int smb_tidx;
+static WERROR cmd_wkssvc_getjoininformation(struct rpc_pipe_client *cli,
+                                           TALLOC_CTX *mem_ctx,
+                                           int argc,
+                                           const char **argv)
+{
+       const char *server_name;
+       const char *name_buffer;
+       enum wkssvc_NetJoinStatus name_type;
+       NTSTATUS status;
+       WERROR werr;
+
+       server_name = cli->desthost;
+       name_buffer = "";
+
+       status = rpccli_wkssvc_NetrGetJoinInformation(cli, mem_ctx,
+                                                     server_name,
+                                                     &name_buffer,
+                                                     &name_type,
+                                                     &werr);
+       if (!NT_STATUS_IS_OK(status)) {
+               return ntstatus_to_werror(status);
+       }
 
-extern FILE* out_hnd;
+       if (W_ERROR_IS_OK(werr)) {
+               printf("%s (%d)\n", name_buffer, name_type);
+       }
 
+       return werr;
+}
 
-/****************************************************************************
-workstation get info query
-****************************************************************************/
-void cmd_wks_query_info(struct client_info *info)
+static WERROR cmd_wkssvc_messagebuffersend(struct rpc_pipe_client *cli,
+                                          TALLOC_CTX *mem_ctx,
+                                          int argc,
+                                          const char **argv)
 {
-       fstring dest_wks;
-       fstring tmp;
-       WKS_INFO_100 ctr;
-       uint32 info_level = 100;
+       const char *server_name = cli->desthost;
+       const char *message_name = cli->desthost;
+       const char *message_sender_name = cli->desthost;
+       smb_ucs2_t *message_buffer = NULL;
+       size_t message_size = 0;
+       const char *message = "my message";
+       NTSTATUS status;
+       WERROR werr;
+
+       if (argc > 1) {
+               message = argv[1];
+       }
+
+       if (!push_ucs2_talloc(mem_ctx, &message_buffer, message,
+                             &message_size))
+       {
+               return WERR_NOMEM;
+       }
 
-       BOOL res = True;
+       status = rpccli_wkssvc_NetrMessageBufferSend(cli, mem_ctx,
+                                                    server_name,
+                                                    message_name,
+                                                    message_sender_name,
+                                                    (uint8_t *)message_buffer,
+                                                    message_size,
+                                                    &werr);
+       if (!NT_STATUS_IS_OK(status)) {
+               return ntstatus_to_werror(status);
+       }
 
-       bzero(&ctr, sizeof(ctr));
+       return werr;
+}
 
-       strcpy(dest_wks, "\\\\");
-       strcat(dest_wks, info->dest_host);
-       strupper(dest_wks);
+static WERROR cmd_wkssvc_enumeratecomputernames(struct rpc_pipe_client *cli,
+                                               TALLOC_CTX *mem_ctx,
+                                               int argc,
+                                               const char **argv)
+{
+       const char *server_name;
+       enum wkssvc_ComputerNameType name_type = NetAllComputerNames;
+       NTSTATUS status;
+       struct wkssvc_ComputerNamesCtr *ctr = NULL;
+       WERROR werr;
 
-       if (next_token(NULL, tmp, NULL))
-       {
-               info_level = strtoul(tmp, (char**)NULL, 10);
+       server_name = cli->desthost;
+
+       if (argc >= 2) {
+               name_type = atoi(argv[1]);
        }
 
-       DEBUG(4,("cmd_wks_query_info: server:%s info level: %D\n",
-                               dest_wks, info_level));
+       status = rpccli_wkssvc_NetrEnumerateComputerNames(cli, mem_ctx,
+                                                         server_name,
+                                                         name_type, 0,
+                                                         &ctr,
+                                                         &werr);
+       if (!NT_STATUS_IS_OK(status)) {
+               return ntstatus_to_werror(status);
+       }
 
-       DEBUG(5, ("cmd_wks_query_info: smb_cli->fd:%d\n", smb_cli->fd));
+       if (W_ERROR_IS_OK(werr)) {
+               int i=0;
+               for (i = 0; i < ctr->count; i++) {
+                       printf("name: %d %s\n", i, ctr->computer_name->string);
+               }
+       }
 
-       /* open LSARPC session. */
-       res = res ? do_session_open(smb_cli, smb_tidx, PIPE_WKSSVC, &(info->dom.wkssvc_fnum)) : False;
+       return werr;
+}
 
-       /* send info level: receive requested info.  hopefully. */
-       res = res ? do_wks_query_info(smb_cli, smb_tidx, info->dom.wkssvc_fnum,
-                               dest_wks, info_level, &ctr) : False;
+static WERROR cmd_wkssvc_enumerateusers(struct rpc_pipe_client *cli,
+                                       TALLOC_CTX *mem_ctx,
+                                       int argc,
+                                       const char **argv)
+{
+       const char *server_name;
+       NTSTATUS status;
+       struct wkssvc_NetWkstaEnumUsersInfo info;
+       WERROR werr;
+       uint32_t i, num_entries, resume_handle;
 
-       /* close the session */
-       do_session_close(smb_cli, smb_tidx, info->dom.wkssvc_fnum);
+       server_name = cli->desthost;
 
-       if (res)
-       {
-               DEBUG(5,("cmd_wks_query_info: query succeeded\n"));
+       ZERO_STRUCT(info);
 
-#if 0
-               display_wks_info_100(out_hnd, DISPLAY_TXT, ACTION_HEADER   , &ctr);
-               display_wks_info_100(out_hnd, DISPLAY_TXT, ACTION_ENUMERATE, &ctr);
-               display_wks_info_100(out_hnd, DISPLAY_TXT, ACTION_FOOTER   , &ctr);
-#endif
+       if (argc >= 2) {
+               info.level = atoi(argv[1]);
+       }
 
+       status = rpccli_wkssvc_NetWkstaEnumUsers(cli, mem_ctx, server_name,
+                                                &info, 1000, &num_entries,
+                                                &resume_handle, &werr);
+       if (!NT_STATUS_IS_OK(status)) {
+               return ntstatus_to_werror(status);
        }
-       else
-       {
-               DEBUG(5,("cmd_wks_query_info: query failed\n"));
+       if (!W_ERROR_IS_OK(werr)) {
+               return werr;
+       }
+
+       for (i=0; i<num_entries; i++) {
+               const char *user = NULL;
+               switch (info.level) {
+               case 0:
+                       user = info.ctr.user0->user0[i].user_name;
+                       break;
+               case 1:
+                       user = talloc_asprintf(
+                               talloc_tos(), "%s\\%s",
+                               info.ctr.user1->user1[i].logon_domain,
+                               info.ctr.user1->user1[i].user_name);
+                       break;
+               }
+               printf("%s\n", user ? user : "(null)");
        }
+
+       return werr;
 }
 
+struct cmd_set wkssvc_commands[] = {
+
+       { "WKSSVC" },
+       { "wkssvc_wkstagetinfo", RPC_RTYPE_WERROR, NULL, cmd_wkssvc_wkstagetinfo, &ndr_table_wkssvc.syntax_id, NULL, "Query WKSSVC Workstation Information", "" },
+       { "wkssvc_getjoininformation", RPC_RTYPE_WERROR, NULL, cmd_wkssvc_getjoininformation, &ndr_table_wkssvc.syntax_id, NULL, "Query WKSSVC Join Information", "" },
+       { "wkssvc_messagebuffersend", RPC_RTYPE_WERROR, NULL, cmd_wkssvc_messagebuffersend, &ndr_table_wkssvc.syntax_id, NULL, "Send WKSSVC message", "" },
+       { "wkssvc_enumeratecomputernames", RPC_RTYPE_WERROR, NULL, cmd_wkssvc_enumeratecomputernames, &ndr_table_wkssvc.syntax_id, NULL, "Enumerate WKSSVC computer names", "" },
+       { "wkssvc_enumerateusers", RPC_RTYPE_WERROR, NULL,
+         cmd_wkssvc_enumerateusers, &ndr_table_wkssvc.syntax_id, NULL,
+         "Enumerate WKSSVC users", "" },
+       { NULL }
+};