r8136: remove unused var
[kai/samba.git] / source4 / libnet / libnet_rpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) Stefan Metzmacher  2004
5    Copyright (C) Rafal Szczesniak   2005
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "libnet/libnet.h"
24
25
26 /**
27  * Connects rpc pipe on remote server
28  * 
29  * @param ctx initialised libnet context
30  * @param mem_ctx memory context of this call
31  * @param r data structure containing necessary parameters and return values
32  * @return nt status of the call
33  **/
34
35 static NTSTATUS libnet_RpcConnectSrv(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
36 {
37         NTSTATUS status;
38         const char *binding = NULL;
39
40         binding = talloc_asprintf(mem_ctx, "ncacn_np:%s", r->in.domain_name);
41
42         status = dcerpc_pipe_connect(mem_ctx, &r->out.dcerpc_pipe,
43                                      binding, r->in.dcerpc_iface_uuid,r->in.dcerpc_iface_version,
44                                      ctx->cred, ctx->event_ctx);
45
46         if (!NT_STATUS_IS_OK(status)) {
47                 r->out.error_string = talloc_asprintf(mem_ctx,
48                                                       "dcerpc_pipe_connect to pipe %s failed with %s\n",
49                                                       r->in.dcerpc_iface_name, binding);
50                 return status;
51         }
52
53         r->out.error_string = NULL;
54         ctx->pipe = r->out.dcerpc_pipe;
55
56         return status;
57 }
58
59
60 /**
61  * Connects rpc pipe on domain pdc
62  * 
63  * @param ctx initialised libnet context
64  * @param mem_ctx memory context of this call
65  * @param r data structure containing necessary parameters and return values
66  * @return nt status of the call
67  **/
68
69 static NTSTATUS libnet_RpcConnectPdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
70 {
71         NTSTATUS status;
72         struct libnet_RpcConnect r2;
73         struct libnet_Lookup f;
74         const char *address = talloc_array(ctx, const char, 16);
75
76         f.in.hostname  = r->in.domain_name;
77         f.in.methods   = NULL;
78         f.out.address  = &address;
79
80         status = libnet_LookupPdc(ctx, mem_ctx, &f);
81         if (!NT_STATUS_IS_OK(status)) {
82                 r->out.error_string = talloc_asprintf(mem_ctx, "libnet_LookupPdc failed: %s",
83                                                       nt_errstr(status));
84                 return status;
85         }
86
87         r2.level                    = LIBNET_RPC_CONNECT_SERVER;
88         r2.in.domain_name           = talloc_strdup(mem_ctx, *f.out.address);
89         r2.in.dcerpc_iface_name     = r->in.dcerpc_iface_name;
90         r2.in.dcerpc_iface_uuid     = r->in.dcerpc_iface_uuid;
91         r2.in.dcerpc_iface_version  = r->in.dcerpc_iface_version;
92         
93         status = libnet_RpcConnect(ctx, mem_ctx, &r2);
94
95         r->out.dcerpc_pipe          = r2.out.dcerpc_pipe;
96         r->out.error_string         = r2.out.error_string;
97
98         ctx->pipe = r->out.dcerpc_pipe;
99
100         return status;
101 }
102
103
104 /**
105  * Connects to rpc pipe on remote server or pdc
106  * 
107  * @param ctx initialised libnet context
108  * @param mem_ctx memory context of this call
109  * @param r data structure containing necessary parameters and return values
110  * @return nt status of the call
111  **/
112
113 NTSTATUS libnet_RpcConnect(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
114 {
115         switch (r->level) {
116                 case LIBNET_RPC_CONNECT_SERVER:
117                         return libnet_RpcConnectSrv(ctx, mem_ctx, r);
118                 case LIBNET_RPC_CONNECT_PDC:
119                         return libnet_RpcConnectPdc(ctx, mem_ctx, r);
120         }
121
122         return NT_STATUS_INVALID_LEVEL;
123 }