r7633: this patch started as an attempt to make the dcerpc code use a given
[metze/samba/wip.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_rpc_connect_standard(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_rpc_connect *r)
91 {
92         NTSTATUS status;
93         const char *binding = NULL;
94
95         binding = talloc_asprintf(mem_ctx, "ncacn_np:%s",
96                                         r->standard.in.server_name);
97
98         status = dcerpc_pipe_connect(mem_ctx, 
99                                      &r->standard.out.dcerpc_pipe,
100                                      binding,
101                                      r->standard.in.dcerpc_iface_uuid,
102                                      r->standard.in.dcerpc_iface_version,
103                                      ctx->cred, ctx->event_ctx);
104
105         if (!NT_STATUS_IS_OK(status)) {
106                 r->standard.out.error_string = talloc_asprintf(mem_ctx, 
107                                                 "dcerpc_pipe_connect to pipe %s failed with %s\n",
108                                                 r->standard.in.dcerpc_iface_name, binding);
109                 return status;
110         }
111
112         r->standard.out.error_string = NULL;
113
114         return status;
115 }
116
117
118 /**
119  * Connects rpc pipe on domain pdc
120  * 
121  * @param ctx initialised libnet context
122  * @param mem_ctx memory context of this call
123  * @param r data structure containing necessary parameters and return values
124  * @return nt status of the call
125  **/
126
127 static NTSTATUS libnet_rpc_connect_pdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_rpc_connect *r)
128 {
129         NTSTATUS status;
130         union libnet_rpc_connect r2;
131         union libnet_find_pdc f;
132
133         f.generic.level                 = LIBNET_FIND_PDC_GENERIC;
134         f.generic.in.domain_name        = r->pdc.in.domain_name;
135
136         status = libnet_find_pdc(ctx, mem_ctx, &f);
137         if (!NT_STATUS_IS_OK(status)) {
138                 return status;
139         }
140
141         r2.standard.level                       = LIBNET_RPC_CONNECT_STANDARD;
142         r2.standard.in.server_name              = f.generic.out.pdc_name;
143         r2.standard.in.dcerpc_iface_name        = r->standard.in.dcerpc_iface_name;
144         r2.standard.in.dcerpc_iface_uuid        = r->standard.in.dcerpc_iface_uuid;
145         r2.standard.in.dcerpc_iface_version     = r->standard.in.dcerpc_iface_version;
146         
147         status = libnet_rpc_connect(ctx, mem_ctx, &r2);
148
149         r->pdc.out.dcerpc_pipe          = r2.standard.out.dcerpc_pipe;
150         r->pdc.out.error_string         = r2.standard.out.error_string;
151
152         return status;
153 }
154
155
156 /**
157  * Connects to rpc pipe on remote server or pdc
158  * 
159  * @param ctx initialised libnet context
160  * @param mem_ctx memory context of this call
161  * @param r data structure containing necessary parameters and return values
162  * @return nt status of the call
163  **/
164
165 NTSTATUS libnet_rpc_connect(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_rpc_connect *r)
166 {
167         switch (r->standard.level) {
168                 case LIBNET_RPC_CONNECT_STANDARD:
169                         return libnet_rpc_connect_standard(ctx, mem_ctx, r);
170                 case LIBNET_RPC_CONNECT_PDC:
171                         return libnet_rpc_connect_pdc(ctx, mem_ctx, r);
172         }
173
174         return NT_STATUS_INVALID_LEVEL;
175 }