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