r5924: Use cli_credentials in libnet/.
[ira/wip.git] / source / 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         name.name = r->generic.in.domain_name;
47         name.type = NBT_NAME_PDC;
48         name.scope = NULL;
49
50         status = resolve_name(&name, mem_ctx, &address);
51         if (!NT_STATUS_IS_OK(status)) {
52                 name.type = NBT_NAME_SERVER;
53                 status = resolve_name(&name, mem_ctx, &address);
54         }
55         NT_STATUS_NOT_OK_RETURN(status);
56
57         r->generic.out.pdc_name = talloc_strdup(mem_ctx, address);
58
59         return NT_STATUS_OK;
60 }
61
62
63 /**
64  * Finds a domain pdc function
65  * 
66  * @param ctx initialised libnet context
67  * @param mem_ctx memory context of this call
68  * @param r data structure containing necessary parameters and return values
69  * @return nt status of the call
70  **/
71
72 NTSTATUS libnet_find_pdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_find_pdc *r)
73 {
74         switch (r->generic.level) {
75                 case LIBNET_FIND_PDC_GENERIC:
76                         return libnet_find_pdc_generic(ctx, mem_ctx, r);
77         }
78
79         return NT_STATUS_INVALID_LEVEL;
80 }
81
82
83 /**
84  * Connects rpc pipe on remote server
85  * 
86  * @param ctx initialised libnet context
87  * @param mem_ctx memory context of this call
88  * @param r data structure containing necessary parameters and return values
89  * @return nt status of the call
90  **/
91
92 static NTSTATUS libnet_rpc_connect_standard(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_rpc_connect *r)
93 {
94         NTSTATUS status;
95         const char *binding = NULL;
96
97         binding = talloc_asprintf(mem_ctx, "ncacn_np:%s",
98                                         r->standard.in.server_name);
99
100         status = dcerpc_pipe_connect(&r->standard.out.dcerpc_pipe,
101                                      binding,
102                                      r->standard.in.dcerpc_iface_uuid,
103                                      r->standard.in.dcerpc_iface_version,
104                                      lp_netbios_name(),
105                                      cli_credentials_get_domain(ctx->credentials),
106                                      cli_credentials_get_username(ctx->credentials),
107                                      cli_credentials_get_password(ctx->credentials)); 
108         if (!NT_STATUS_IS_OK(status)) {
109                 r->standard.out.error_string = talloc_asprintf(mem_ctx, 
110                                                 "dcerpc_pipe_connect to pipe %s failed with %s\n",
111                                                 r->standard.in.dcerpc_iface_name, binding);
112                 return status;
113         }
114
115         r->standard.out.error_string = NULL;
116
117         return status;
118 }
119
120
121 /**
122  * Connects rpc pipe on domain pdc
123  * 
124  * @param ctx initialised libnet context
125  * @param mem_ctx memory context of this call
126  * @param r data structure containing necessary parameters and return values
127  * @return nt status of the call
128  **/
129
130 static NTSTATUS libnet_rpc_connect_pdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_rpc_connect *r)
131 {
132         NTSTATUS status;
133         union libnet_rpc_connect r2;
134         union libnet_find_pdc f;
135
136         f.generic.level                 = LIBNET_FIND_PDC_GENERIC;
137         f.generic.in.domain_name        = r->pdc.in.domain_name;
138
139         status = libnet_find_pdc(ctx, mem_ctx, &f);
140         if (!NT_STATUS_IS_OK(status)) {
141                 return status;
142         }
143
144         r2.standard.level                       = LIBNET_RPC_CONNECT_STANDARD;
145         r2.standard.in.server_name              = f.generic.out.pdc_name;
146         r2.standard.in.dcerpc_iface_name        = r->standard.in.dcerpc_iface_name;
147         r2.standard.in.dcerpc_iface_uuid        = r->standard.in.dcerpc_iface_uuid;
148         r2.standard.in.dcerpc_iface_version     = r->standard.in.dcerpc_iface_version;
149         
150         status = libnet_rpc_connect(ctx, mem_ctx, &r2);
151
152         r->pdc.out.dcerpc_pipe          = r2.standard.out.dcerpc_pipe;
153         r->pdc.out.error_string         = r2.standard.out.error_string;
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_rpc_connect(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_rpc_connect *r)
169 {
170         switch (r->standard.level) {
171                 case LIBNET_RPC_CONNECT_STANDARD:
172                         return libnet_rpc_connect_standard(ctx, mem_ctx, r);
173                 case LIBNET_RPC_CONNECT_PDC:
174                         return libnet_rpc_connect_pdc(ctx, mem_ctx, r);
175         }
176
177         return NT_STATUS_INVALID_LEVEL;
178 }