r8095: Fix compiler warning.
[samba.git] / source4 / libnet / libnet_rpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) Stefan Metzmacher      2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "libcli/nbt/libnbt.h"
23 #include "libnet/libnet.h"
24
25 /**
26  * Finds a domain pdc (generic part)
27  * 
28  * @param ctx initialised libnet context
29  * @param mem_ctx memory context of this call
30  * @param r data structure containing necessary parameters and return values
31  * @return nt status of the call
32  **/
33
34 static NTSTATUS libnet_find_pdc_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, 
35                                         union libnet_find_pdc *r)
36 {
37         const char *address;
38         NTSTATUS status;
39         struct nbt_name name;
40
41         if (is_ipaddress(r->generic.in.domain_name)) {
42                 r->generic.out.pdc_name = r->generic.in.domain_name;
43                 return NT_STATUS_OK;
44         }
45
46         make_nbt_name(&name, r->generic.in.domain_name, NBT_NAME_PDC);
47
48         status = resolve_name(&name, mem_ctx, &address);
49         if (!NT_STATUS_IS_OK(status)) {
50                 name.type = NBT_NAME_SERVER;
51                 status = resolve_name(&name, mem_ctx, &address);
52         }
53         NT_STATUS_NOT_OK_RETURN(status);
54
55         r->generic.out.pdc_name = talloc_strdup(mem_ctx, address);
56
57         return NT_STATUS_OK;
58 }
59
60
61 /**
62  * Finds a domain pdc function
63  * 
64  * @param ctx initialised libnet context
65  * @param mem_ctx memory context of this call
66  * @param r data structure containing necessary parameters and return values
67  * @return nt status of the call
68  **/
69
70 NTSTATUS libnet_find_pdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_find_pdc *r)
71 {
72         switch (r->generic.level) {
73                 case LIBNET_FIND_PDC_GENERIC:
74                         return libnet_find_pdc_generic(ctx, mem_ctx, r);
75         }
76
77         return NT_STATUS_INVALID_LEVEL;
78 }
79
80
81 /**
82  * Connects rpc pipe on remote server
83  * 
84  * @param ctx initialised libnet context
85  * @param mem_ctx memory context of this call
86  * @param r data structure containing necessary parameters and return values
87  * @return nt status of the call
88  **/
89
90 static NTSTATUS libnet_RpcConnectSrv(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
91 {
92         NTSTATUS status;
93         const char *binding = NULL;
94
95         binding = talloc_asprintf(mem_ctx, "ncacn_np:%s", r->in.domain_name);
96
97         status = dcerpc_pipe_connect(mem_ctx, &r->out.dcerpc_pipe,
98                                      binding, r->in.dcerpc_iface_uuid,r->in.dcerpc_iface_version,
99                                      ctx->cred, ctx->event_ctx);
100
101         if (!NT_STATUS_IS_OK(status)) {
102                 r->out.error_string = talloc_asprintf(mem_ctx,
103                                                       "dcerpc_pipe_connect to pipe %s failed with %s\n",
104                                                       r->in.dcerpc_iface_name, binding);
105                 return status;
106         }
107
108         r->out.error_string = NULL;
109         ctx->pipe = r->out.dcerpc_pipe;
110
111         return status;
112 }
113
114
115 /**
116  * Connects rpc pipe on domain pdc
117  * 
118  * @param ctx initialised libnet context
119  * @param mem_ctx memory context of this call
120  * @param r data structure containing necessary parameters and return values
121  * @return nt status of the call
122  **/
123
124 static NTSTATUS libnet_RpcConnectPdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
125 {
126         NTSTATUS status;
127         struct libnet_RpcConnect r2;
128         struct libnet_Lookup f;
129         const char *address = talloc_array(ctx, const char, 16);
130
131         f.in.hostname  = r->in.domain_name;
132         f.in.methods   = NULL;
133         f.out.address  = &address;
134
135         status = libnet_LookupPdc(ctx, mem_ctx, &f);
136         if (!NT_STATUS_IS_OK(status)) {
137                 r->out.error_string = talloc_asprintf(mem_ctx, "libnet_LookupPdc failed: %s",
138                                                       nt_errstr(status));
139                 return status;
140         }
141
142         r2.level                    = LIBNET_RPC_CONNECT_SERVER;
143         r2.in.domain_name           = talloc_strdup(mem_ctx, *f.out.address);
144         r2.in.dcerpc_iface_name     = r->in.dcerpc_iface_name;
145         r2.in.dcerpc_iface_uuid     = r->in.dcerpc_iface_uuid;
146         r2.in.dcerpc_iface_version  = r->in.dcerpc_iface_version;
147         
148         status = libnet_RpcConnect(ctx, mem_ctx, &r2);
149
150         r->out.dcerpc_pipe          = r2.out.dcerpc_pipe;
151         r->out.error_string         = r2.out.error_string;
152
153         ctx->pipe = r->out.dcerpc_pipe;
154
155         return status;
156 }
157
158
159 /**
160  * Connects to rpc pipe on remote server or pdc
161  * 
162  * @param ctx initialised libnet context
163  * @param mem_ctx memory context of this call
164  * @param r data structure containing necessary parameters and return values
165  * @return nt status of the call
166  **/
167
168 NTSTATUS libnet_RpcConnect(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
169 {
170         NTSTATUS status;
171
172         switch (r->level) {
173                 case LIBNET_RPC_CONNECT_SERVER:
174                         return libnet_RpcConnectSrv(ctx, mem_ctx, r);
175                 case LIBNET_RPC_CONNECT_PDC:
176                         return libnet_RpcConnectPdc(ctx, mem_ctx, r);
177         }
178
179         return NT_STATUS_INVALID_LEVEL;
180 }