r1925: now we lookup the domain controller
[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
23 /* find a domain pdc generic */
24 static NTSTATUS libnet_find_pdc_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_find_pdc *r)
25 {
26         BOOL ret;
27         struct in_addr ip;
28
29         ret = get_pdc_ip(mem_ctx, r->generic.in.domain_name, &ip);
30         if (!ret) {
31                 /* fallback to a workstation name */
32                 ret = resolve_name(mem_ctx, r->generic.in.domain_name, &ip, 0x20);
33                 if (!ret) {
34                         return NT_STATUS_NO_LOGON_SERVERS;
35                 }
36         }
37
38         r->generic.out.pdc_name = talloc_strdup(mem_ctx, inet_ntoa(ip));
39
40         return NT_STATUS_OK;
41 }
42
43 /* find a domain pdc */
44 NTSTATUS libnet_find_pdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_find_pdc *r)
45 {
46         switch (r->generic.level) {
47                 case LIBNET_FIND_PDC_GENERIC:
48                         return libnet_find_pdc_generic(ctx, mem_ctx, r);
49         }
50
51         return NT_STATUS_INVALID_LEVEL;
52 }
53
54 /* connect to a dcerpc interface of a domains PDC */
55 static NTSTATUS libnet_rpc_connect_pdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_rpc_connect *r)
56 {
57         NTSTATUS status;
58         const char *binding = NULL;
59         union libnet_find_pdc f;
60
61         f.generic.level                 = LIBNET_FIND_PDC_GENERIC;
62         f.generic.in.domain_name        = r->pdc.in.domain_name;
63
64         status = libnet_find_pdc(ctx, mem_ctx, &f);
65         if (!NT_STATUS_IS_OK(status)) {
66                 return status;
67         }
68
69         binding = talloc_asprintf(mem_ctx, "ncacn_np:%s",
70                                         f.generic.out.pdc_name);
71
72         status = dcerpc_pipe_connect(&r->pdc.out.dcerpc_pipe,
73                                         binding,
74                                         r->pdc.in.dcerpc_iface_uuid,
75                                         r->pdc.in.dcerpc_iface_version,
76                                         ctx->user.domain_name,
77                                         ctx->user.account_name,
78                                         ctx->user.password); 
79
80         return status;
81 }
82
83 /* connect to a dcerpc interface */
84 NTSTATUS libnet_rpc_connect(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_rpc_connect *r)
85 {
86         switch (r->pdc.level) {
87                 case LIBNET_RPC_CONNECT_PDC:
88                         return libnet_rpc_connect_pdc(ctx, mem_ctx, r);
89         }
90
91         return NT_STATUS_INVALID_LEVEL;
92 }