Initial import
[samba] / source / nmbd / nmbd_winsproxy.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NBT netbios routines and daemon - version 2
4
5    Copyright (C) Jeremy Allison 1994-1998
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 */
22
23 #include "includes.h"
24
25 /****************************************************************************
26 Function called when the name lookup succeeded.
27 ****************************************************************************/
28
29 static void wins_proxy_name_query_request_success( struct subnet_record *subrec,
30                         struct userdata_struct *userdata,
31                         struct nmb_name *nmbname, struct in_addr ip, struct res_rec *rrec)
32 {
33         unstring name;
34         struct packet_struct *original_packet;
35         struct subnet_record *orig_broadcast_subnet;
36         struct name_record *namerec = NULL;
37         uint16 nb_flags;
38         int num_ips;
39         int i;
40         int ttl = 3600; /* By default one hour in the cache. */
41         struct in_addr *iplist;
42
43         /* Extract the original packet and the original broadcast subnet from
44                         the userdata. */
45
46         memcpy( (char *)&orig_broadcast_subnet, userdata->data, sizeof(struct subnet_record *) );
47         memcpy( (char *)&original_packet, &userdata->data[sizeof(struct subnet_record *)],
48                         sizeof(struct packet_struct *) );
49
50         nb_flags = get_nb_flags( rrec->rdata );
51
52         num_ips = rrec->rdlength / 6;
53         if(num_ips == 0) {
54                 DEBUG(0,("wins_proxy_name_query_request_success: Invalid number of IP records (0) \
55 returned for name %s.\n", nmb_namestr(nmbname) ));
56                 return;
57         }
58
59         if(num_ips == 1) {
60                 iplist = &ip;
61         } else {
62                 if((iplist = SMB_MALLOC_ARRAY( struct in_addr, num_ips )) == NULL) {
63                         DEBUG(0,("wins_proxy_name_query_request_success: malloc fail !\n"));
64                         return;
65                 }
66
67                 for(i = 0; i < num_ips; i++) {
68                         putip( (char *)&iplist[i], (char *)&rrec->rdata[ (i*6) + 2]);
69         }
70         }
71
72         /* Add the queried name to the original subnet as a WINS_PROXY_NAME. */
73
74         if(rrec == PERMANENT_TTL) {
75                 ttl = lp_max_ttl();
76         }
77
78         pull_ascii_nstring(name, sizeof(name), nmbname->name);
79         add_name_to_subnet( orig_broadcast_subnet, name,
80                                         nmbname->name_type, nb_flags, ttl,
81                                         WINS_PROXY_NAME, num_ips, iplist );
82
83         namerec = find_name_on_subnet(orig_broadcast_subnet, nmbname, FIND_ANY_NAME);
84         if (!namerec) {
85                 DEBUG(0,("wins_proxy_name_query_request_success: failed to add "
86                         "name %s to subnet %s !\n",
87                         name,
88                         orig_broadcast_subnet->subnet_name ));
89                 return;
90         }
91
92         if(iplist != &ip) {
93                 SAFE_FREE(iplist);
94         }
95
96         /*
97          * Check that none of the IP addresses we are returning is on the
98          * same broadcast subnet as the original requesting packet. If it
99          * is then don't reply (although we still need to add the name
100          * to the cache) as the actual machine will be replying also
101          * and we don't want two replies to a broadcast query.
102          */
103
104         if(namerec && original_packet->packet.nmb.header.nm_flags.bcast) {
105                 for( i = 0; i < namerec->data.num_ips; i++) {
106                         if( same_net( namerec->data.ip[i], orig_broadcast_subnet->myip,
107                                         orig_broadcast_subnet->mask_ip ) ) {
108                                 DEBUG( 5, ( "wins_proxy_name_query_request_success: name %s is a WINS \
109 proxy name and is also on the same subnet (%s) as the requestor. \
110 Not replying.\n", nmb_namestr(&namerec->name), orig_broadcast_subnet->subnet_name ) );
111                                 return;
112                         }
113                 }
114         }
115
116         /* Finally reply to the original name query. */
117         reply_netbios_packet(original_packet,                /* Packet to reply to. */
118                                 0,                              /* Result code. */
119                                 NMB_QUERY,                      /* nmbd type code. */
120                                 NMB_NAME_QUERY_OPCODE,          /* opcode. */
121                                 ttl,                            /* ttl. */
122                                 rrec->rdata,                    /* data to send. */
123                                 rrec->rdlength);                /* data length. */
124 }
125
126 /****************************************************************************
127 Function called when the name lookup failed.
128 ****************************************************************************/
129
130 static void wins_proxy_name_query_request_fail(struct subnet_record *subrec,
131                                     struct response_record *rrec,
132                                     struct nmb_name *question_name, int fail_code)
133 {
134         DEBUG(4,("wins_proxy_name_query_request_fail: WINS server returned error code %d for lookup \
135 of name %s.\n", fail_code, nmb_namestr(question_name) ));
136 }
137
138 /****************************************************************************
139 Function to make a deep copy of the userdata we will need when the WINS
140 proxy query returns.
141 ****************************************************************************/
142
143 static struct userdata_struct *wins_proxy_userdata_copy_fn(struct userdata_struct *userdata)
144 {
145         struct packet_struct *p, *copy_of_p;
146         struct userdata_struct *new_userdata = (struct userdata_struct *)SMB_MALLOC( userdata->userdata_len );
147
148         if(new_userdata == NULL)
149                 return NULL;
150
151         new_userdata->copy_fn = userdata->copy_fn;
152         new_userdata->free_fn = userdata->free_fn;
153         new_userdata->userdata_len = userdata->userdata_len;
154
155         /* Copy the subnet_record pointer. */
156         memcpy( new_userdata->data, userdata->data, sizeof(struct subnet_record *) );
157
158         /* Extract the pointer to the packet struct */
159         memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)], sizeof(struct packet_struct *) );
160
161         /* Do a deep copy of the packet. */
162         if((copy_of_p = copy_packet(p)) == NULL) {
163                 SAFE_FREE(new_userdata);
164                 return NULL;
165         }
166
167         /* Lock the copy. */
168         copy_of_p->locked = True;
169
170         memcpy( &new_userdata->data[sizeof(struct subnet_record *)], (char *)&copy_of_p,
171                 sizeof(struct packet_struct *) );
172
173         return new_userdata;
174 }
175
176 /****************************************************************************
177 Function to free the deep copy of the userdata we used when the WINS
178 proxy query returned.
179 ****************************************************************************/
180
181 static void wins_proxy_userdata_free_fn(struct userdata_struct *userdata)
182 {
183         struct packet_struct *p;
184
185         /* Extract the pointer to the packet struct */
186         memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)],
187                 sizeof(struct packet_struct *));
188
189         /* Unlock the packet. */
190         p->locked = False;
191
192         free_packet(p);
193         ZERO_STRUCTP(userdata);
194         SAFE_FREE(userdata);
195 }
196
197 /****************************************************************************
198  Make a WINS query on behalf of a broadcast client name query request.
199 ****************************************************************************/
200
201 void make_wins_proxy_name_query_request( struct subnet_record *subrec, 
202                                          struct packet_struct *incoming_packet,
203                                          struct nmb_name *question_name)
204 {
205         union {
206             struct userdata_struct ud;
207             char c[sizeof(struct userdata_struct) + sizeof(struct subrec *) + 
208                 sizeof(struct packet_struct *)+sizeof(long*)];
209         } ud;
210         struct userdata_struct *userdata = &ud.ud;
211         unstring qname;
212
213         memset(&ud, '\0', sizeof(ud));
214  
215         userdata->copy_fn = wins_proxy_userdata_copy_fn;
216         userdata->free_fn = wins_proxy_userdata_free_fn;
217         userdata->userdata_len = sizeof(ud);
218         memcpy( userdata->data, (char *)&subrec, sizeof(struct subnet_record *));
219         memcpy( &userdata->data[sizeof(struct subnet_record *)], (char *)&incoming_packet,
220                         sizeof(struct packet_struct *));
221
222         /* Now use the unicast subnet to query the name with the WINS server. */
223         pull_ascii_nstring(qname, sizeof(qname), question_name->name);
224         query_name( unicast_subnet, qname, question_name->name_type,
225                 wins_proxy_name_query_request_success,
226                 wins_proxy_name_query_request_fail,
227                 userdata);
228 }