r10491: First step towards wbinfo -t: This issues a name request for the primary
[bbaumbach/samba-autobuild/.git] / source4 / libcli / dgram / ntlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    handling for ntlogon 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "dlinklist.h"
26 #include "libcli/nbt/libnbt.h"
27 #include "libcli/dgram/libdgram.h"
28 #include "lib/socket/socket.h"
29 #include "librpc/gen_ndr/ndr_nbt.h"
30
31 /* 
32    send a ntlogon mailslot request 
33 */
34 NTSTATUS dgram_mailslot_ntlogon_send(struct nbt_dgram_socket *dgmsock,
35                                      enum dgram_msg_type msg_type,
36                                      struct nbt_name *dest_name,
37                                      const char *dest_address,
38                                      int dest_port,
39                                      struct nbt_name *src_name,
40                                      struct nbt_ntlogon_packet *request)
41 {
42         NTSTATUS status;
43         DATA_BLOB blob;
44         TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
45
46         status = ndr_push_struct_blob(&blob, tmp_ctx, request, 
47                                       (ndr_push_flags_fn_t)ndr_push_nbt_ntlogon_packet);
48         if (!NT_STATUS_IS_OK(status)) {
49                 talloc_free(tmp_ctx);
50                 return status;
51         }
52
53
54         status = dgram_mailslot_send(dgmsock, msg_type,
55                                      NBT_MAILSLOT_NTLOGON,
56                                      dest_name, dest_address, dest_port, 
57                                      src_name, &blob);
58         talloc_free(tmp_ctx);
59         return status;
60 }
61
62
63 /* 
64    send a ntlogon mailslot reply
65 */
66 NTSTATUS dgram_mailslot_ntlogon_reply(struct nbt_dgram_socket *dgmsock,
67                                       struct nbt_dgram_packet *request,
68                                       const char *mailslot_name,
69                                       struct nbt_ntlogon_packet *reply)
70 {
71         NTSTATUS status;
72         DATA_BLOB blob;
73         TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
74         struct nbt_name myname;
75
76         status = ndr_push_struct_blob(&blob, tmp_ctx, reply, 
77                                       (ndr_push_flags_fn_t)ndr_push_nbt_ntlogon_packet);
78         if (!NT_STATUS_IS_OK(status)) {
79                 talloc_free(tmp_ctx);
80                 return status;
81         }
82
83         make_nbt_name_client(&myname, lp_netbios_name());
84
85         status = dgram_mailslot_send(dgmsock, DGRAM_DIRECT_UNIQUE, 
86                                      mailslot_name,
87                                      &request->data.msg.source_name,
88                                      request->source, request->src_port,
89                                      &myname, &blob);
90         talloc_free(tmp_ctx);
91         return status;
92 }
93
94
95 /*
96   parse a ntlogon response. The packet must be a valid mailslot packet
97 */
98 NTSTATUS dgram_mailslot_ntlogon_parse(struct dgram_mailslot_handler *dgmslot,
99                                       TALLOC_CTX *mem_ctx,
100                                       struct nbt_dgram_packet *dgram,
101                                       struct nbt_ntlogon_packet *ntlogon)
102 {
103         DATA_BLOB data = dgram_mailslot_data(dgram);
104         NTSTATUS status;
105
106         status = ndr_pull_struct_blob(&data, mem_ctx, ntlogon, 
107                                       (ndr_pull_flags_fn_t)ndr_pull_nbt_ntlogon_packet);
108         if (!NT_STATUS_IS_OK(status)) {
109                 DEBUG(0,("Failed to parse ntlogon packet of length %d\n", 
110                          (int)data.length));
111                 if (DEBUGLVL(10)) {
112                         file_save("ntlogon.dat", data.data, data.length);
113                 }
114         }
115         return status;
116 }