79acf609f61469e0c1bc9513472bcb54648986e3
[tprouty/samba.git] / source4 / libcli / dgram / netlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    handling for netlogon dgram requests
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/dgram/libdgram.h"
24 #include "lib/socket/socket.h"
25 #include "libcli/resolve/resolve.h"
26 #include "librpc/gen_ndr/ndr_nbt.h"
27 #include "param/param.h"
28
29 /* 
30    send a netlogon mailslot request 
31 */
32 NTSTATUS dgram_mailslot_netlogon_send(struct nbt_dgram_socket *dgmsock,
33                                       struct nbt_name *dest_name,
34                                       struct socket_address *dest,
35                                       struct nbt_name *src_name,
36                                       struct nbt_netlogon_packet *request)
37 {
38         NTSTATUS status;
39         DATA_BLOB blob;
40         TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
41
42         status = ndr_push_struct_blob(&blob, tmp_ctx, request, 
43                                       (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
44         if (!NT_STATUS_IS_OK(status)) {
45                 talloc_free(tmp_ctx);
46                 return status;
47         }
48
49
50         status = dgram_mailslot_send(dgmsock, DGRAM_DIRECT_UNIQUE, 
51                                      NBT_MAILSLOT_NETLOGON,
52                                      dest_name, dest, 
53                                      src_name, &blob);
54         talloc_free(tmp_ctx);
55         return status;
56 }
57
58
59 /* 
60    send a netlogon mailslot reply
61 */
62 NTSTATUS dgram_mailslot_netlogon_reply(struct nbt_dgram_socket *dgmsock,
63                                        struct nbt_dgram_packet *request,
64                                        const char *mailslot_name,
65                                        struct nbt_netlogon_packet *reply)
66 {
67         NTSTATUS status;
68         DATA_BLOB blob;
69         TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
70         struct nbt_name myname;
71         struct socket_address *dest;
72
73         status = ndr_push_struct_blob(&blob, tmp_ctx, reply, 
74                                       (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
75         if (!NT_STATUS_IS_OK(status)) {
76                 talloc_free(tmp_ctx);
77                 return status;
78         }
79
80         make_nbt_name_client(&myname, lp_netbios_name());
81
82         dest = socket_address_from_strings(tmp_ctx, dgmsock->sock->backend_name, 
83                                            request->src_addr, request->src_port);
84         if (!dest) {
85                 talloc_free(tmp_ctx);
86                 return NT_STATUS_NO_MEMORY;
87         }
88
89         status = dgram_mailslot_send(dgmsock, DGRAM_DIRECT_UNIQUE, 
90                                      mailslot_name,
91                                      &request->data.msg.source_name,
92                                      dest,
93                                      &myname, &blob);
94         talloc_free(tmp_ctx);
95         return status;
96 }
97
98
99 /*
100   parse a netlogon response. The packet must be a valid mailslot packet
101 */
102 NTSTATUS dgram_mailslot_netlogon_parse(struct dgram_mailslot_handler *dgmslot,
103                                        TALLOC_CTX *mem_ctx,
104                                        struct nbt_dgram_packet *dgram,
105                                        struct nbt_netlogon_packet *netlogon)
106 {
107         DATA_BLOB data = dgram_mailslot_data(dgram);
108         NTSTATUS status;
109
110         status = ndr_pull_struct_blob(&data, mem_ctx, netlogon, 
111                                       (ndr_pull_flags_fn_t)ndr_pull_nbt_netlogon_packet);
112         if (!NT_STATUS_IS_OK(status)) {
113                 DEBUG(0,("Failed to parse netlogon packet of length %d\n", 
114                          (int)data.length));
115                 if (DEBUGLVL(10)) {
116                         file_save("netlogon.dat", data.data, data.length);
117                 }
118         }
119         return status;
120 }