r5396: fixed parsing of NBT type 0xc0 compressed name pointers
[jelmer/samba4-debian.git] / source / libcli / nbt / namerelease.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    send out a name release request
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 "libcli/nbt/libnbt.h"
25
26 /*
27   send a nbt name release request
28 */
29 struct nbt_name_request *nbt_name_release_send(struct nbt_name_socket *nbtsock,
30                                                struct nbt_name_release *io)
31 {
32         struct nbt_name_request *req;
33         struct nbt_name_packet *packet;
34
35         packet = talloc_zero(nbtsock, struct nbt_name_packet);
36         if (packet == NULL) return NULL;
37
38         packet->qdcount = 1;
39         packet->arcount = 1;
40         packet->operation = NBT_OPCODE_RELEASE;
41         if (io->in.broadcast) {
42                 packet->operation |= NBT_FLAG_BROADCAST;
43         }
44
45         packet->questions = talloc_array(packet, struct nbt_name_question, 1);
46         if (packet->questions == NULL) goto failed;
47
48         packet->questions[0].name           = io->in.name;
49         packet->questions[0].question_type  = NBT_QTYPE_NETBIOS;
50         packet->questions[0].question_class = NBT_QCLASS_IP;
51
52         packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
53         if (packet->additional == NULL) goto failed;
54
55         packet->additional[0].name                   = io->in.name;
56         packet->additional[0].rr_type                = NBT_QTYPE_NETBIOS;
57         packet->additional[0].rr_class               = NBT_QCLASS_IP;
58         packet->additional[0].ttl                    = 0;
59         packet->additional[0].rdata.netbios.length   = 6;
60         packet->additional[0].rdata.netbios.addresses = talloc_array(packet->additional,
61                                                                      struct nbt_rdata_address, 1);
62         if (packet->additional[0].rdata.netbios.addresses == NULL) goto failed;
63         packet->additional[0].rdata.netbios.addresses[0].nb_flags = io->in.nb_flags;
64         packet->additional[0].rdata.netbios.addresses[0].ipaddr = 
65                 talloc_strdup(packet->additional, io->in.address);
66         
67         req = nbt_name_request_send(nbtsock, io->in.dest_addr, lp_nbt_port(), packet,
68                                     io->in.timeout, io->in.retries, False);
69         if (req == NULL) goto failed;
70
71         talloc_free(packet);
72         return req;
73
74 failed:
75         talloc_free(packet);
76         return NULL;    
77 }
78
79 /*
80   wait for a release reply
81 */
82 NTSTATUS nbt_name_release_recv(struct nbt_name_request *req, 
83                                TALLOC_CTX *mem_ctx, struct nbt_name_release *io)
84 {
85         NTSTATUS status;
86         struct nbt_name_packet *packet;
87
88         status = nbt_name_request_recv(req);
89         if (!NT_STATUS_IS_OK(status) ||
90             req->num_replies == 0) {
91                 talloc_free(req);
92                 return status;
93         }
94         
95         packet = req->replies[0].packet;
96         io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].reply_addr);
97
98         if (packet->ancount != 1 ||
99             packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
100             packet->answers[0].rr_class != NBT_QCLASS_IP) {
101                 talloc_free(req);
102                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
103         }
104
105         io->out.rcode = packet->operation & NBT_RCODE;
106         io->out.name = packet->answers[0].name;
107         if (packet->answers[0].rdata.netbios.length < 6) {
108                 talloc_free(req);
109                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
110         }
111         io->out.reply_addr = talloc_steal(mem_ctx, 
112                                           packet->answers[0].rdata.netbios.addresses[0].ipaddr);
113         talloc_steal(mem_ctx, io->out.name.name);
114         talloc_steal(mem_ctx, io->out.name.scope);
115
116         talloc_free(req);
117
118         return NT_STATUS_OK;
119 }
120
121 /*
122   synchronous name release request
123 */
124 NTSTATUS nbt_name_release(struct nbt_name_socket *nbtsock, 
125                            TALLOC_CTX *mem_ctx, struct nbt_name_release *io)
126 {
127         struct nbt_name_request *req = nbt_name_release_send(nbtsock, io);
128         return nbt_name_release_recv(req, mem_ctx, io);
129 }