1f546d89a4a03d169d1e991ddb62cb99a55cdc01
[samba.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 /* find a domain pdc generic */
26 static NTSTATUS libnet_find_pdc_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, 
27                                         union libnet_find_pdc *r)
28 {
29         const char *address;
30         NTSTATUS status;
31         struct nbt_name name;
32
33         if (is_ipaddress(r->generic.in.domain_name)) {
34                 r->generic.out.pdc_name = r->generic.in.domain_name;
35                 return NT_STATUS_OK;
36         }
37
38         name.name = r->generic.in.domain_name;
39         name.type = NBT_NAME_PDC;
40         name.scope = NULL;
41
42         status = resolve_name(&name, mem_ctx, &address);
43         if (!NT_STATUS_IS_OK(status)) {
44                 name.type = NBT_NAME_SERVER;
45                 status = resolve_name(&name, mem_ctx, &address);
46         }
47         NT_STATUS_NOT_OK_RETURN(status);
48
49         r->generic.out.pdc_name = talloc_strdup(mem_ctx, address);
50
51         return NT_STATUS_OK;
52 }
53
54 /* find a domain pdc */
55 NTSTATUS libnet_find_pdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_find_pdc *r)
56 {
57         switch (r->generic.level) {
58                 case LIBNET_FIND_PDC_GENERIC:
59                         return libnet_find_pdc_generic(ctx, mem_ctx, r);
60         }
61
62         return NT_STATUS_INVALID_LEVEL;
63 }
64
65 /* connect to a dcerpc interface of a server */
66 static NTSTATUS libnet_rpc_connect_standard(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_rpc_connect *r)
67 {
68         NTSTATUS status;
69         const char *binding = NULL;
70
71         binding = talloc_asprintf(mem_ctx, "ncacn_np:%s",
72                                         r->standard.in.server_name);
73
74         status = dcerpc_pipe_connect(&r->standard.out.dcerpc_pipe,
75                                         binding,
76                                         r->standard.in.dcerpc_iface_uuid,
77                                         r->standard.in.dcerpc_iface_version,
78                                         ctx->user.domain_name,
79                                         ctx->user.account_name,
80                                         ctx->user.password); 
81         if (!NT_STATUS_IS_OK(status)) {
82                 r->standard.out.error_string = talloc_asprintf(mem_ctx, 
83                                                 "dcerpc_pipe_connect to pipe %s failed with %s\n",
84                                                 r->standard.in.dcerpc_iface_name, binding);
85                 return status;
86         }
87
88         r->standard.out.error_string = NULL;
89
90         return status;
91 }
92
93 /* connect to a dcerpc interface of a time server */
94 static NTSTATUS libnet_rpc_connect_pdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_rpc_connect *r)
95 {
96         NTSTATUS status;
97         union libnet_rpc_connect r2;
98         union libnet_find_pdc f;
99
100         f.generic.level                 = LIBNET_FIND_PDC_GENERIC;
101         f.generic.in.domain_name        = r->pdc.in.domain_name;
102
103         status = libnet_find_pdc(ctx, mem_ctx, &f);
104         if (!NT_STATUS_IS_OK(status)) {
105                 return status;
106         }
107
108         r2.standard.level                       = LIBNET_RPC_CONNECT_STANDARD;
109         r2.standard.in.server_name              = f.generic.out.pdc_name;
110         r2.standard.in.dcerpc_iface_name        = r->standard.in.dcerpc_iface_name;
111         r2.standard.in.dcerpc_iface_uuid        = r->standard.in.dcerpc_iface_uuid;
112         r2.standard.in.dcerpc_iface_version     = r->standard.in.dcerpc_iface_version;
113         
114         status = libnet_rpc_connect(ctx, mem_ctx, &r2);
115
116         r->pdc.out.dcerpc_pipe          = r2.standard.out.dcerpc_pipe;
117         r->pdc.out.error_string         = r2.standard.out.error_string;
118
119         return status;
120 }
121
122 /* connect to a dcerpc interface */
123 NTSTATUS libnet_rpc_connect(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_rpc_connect *r)
124 {
125         switch (r->standard.level) {
126                 case LIBNET_RPC_CONNECT_STANDARD:
127                         return libnet_rpc_connect_standard(ctx, mem_ctx, r);
128                 case LIBNET_RPC_CONNECT_PDC:
129                         return libnet_rpc_connect_pdc(ctx, mem_ctx, r);
130         }
131
132         return NT_STATUS_INVALID_LEVEL;
133 }